From 90bb7e959496c3a12bebe055f6344b9f06f22809 Mon Sep 17 00:00:00 2001 From: Nathanael Sensfelder Date: Thu, 3 Aug 2017 15:28:17 +0200 Subject: Improving clarity through better Makefiles. --- sol-pretty-printer/src/QuickSolParser.java | 117 +++++++++++++++++++++++++++++ 1 file changed, 117 insertions(+) create mode 100644 sol-pretty-printer/src/QuickSolParser.java (limited to 'sol-pretty-printer/src/QuickSolParser.java') diff --git a/sol-pretty-printer/src/QuickSolParser.java b/sol-pretty-printer/src/QuickSolParser.java new file mode 100644 index 0000000..e162096 --- /dev/null +++ b/sol-pretty-printer/src/QuickSolParser.java @@ -0,0 +1,117 @@ +/* FIXME: Finer imports */ +import java.io.*; +import java.util.regex.*; +import java.util.*; + +public class QuickSolParser +{ + private static final Pattern SOL_ITEM_PATTERN; + private final BufferedReader buffered_reader; + + static + { + SOL_ITEM_PATTERN = Pattern.compile("\\((?[a-zA-Z_0-9 \t]+)\\)"); + } + public QuickSolParser (final String filename) + throws FileNotFoundException + { + buffered_reader = new BufferedReader(new FileReader(filename)); + } + + public void finalize () + throws IOException + { + buffered_reader.close(); + } + + public List next_solution () + throws IOException + { + final List result; + final Matcher matcher; + boolean has_started_sol; + String line; + + result = new ArrayList(); + has_started_sol = false; + + matcher = SOL_ITEM_PATTERN.matcher(""); + + for (;;) + { + line = buffered_reader.readLine(); + + if (line == null) + { + return null; + } + + line = line.replaceAll("\\s+"," "); + + if (line.equals(")")) + { + if (!has_started_sol) + { + throw + new IOException + ( + "[E] Incorrect solution structure. (found a \")\" before a" + + " \"(solution\"" + ); + } + + return result; + } + else if (line.equals("(solution")) + { + if (has_started_sol) + { + throw + new IOException + ( + "[E] Incorrect solution structure. (found a second" + + "\"(solution\" before the \")\" ending the previous one." + ); + } + + has_started_sol = true; + } + else if (line.startsWith(";") || line.length() < 3) + { + continue; + } + else + { + final String[] item; + + matcher.reset(line); + + if (!matcher.find()) + { + throw + new IOException + ( + "[E] Incorrect solution structure. \"" + + line + + "\" does not form a correct solution item." + ); + } + + item = matcher.group(1).split(" |\t"); + + if (item.length != 3) + { + throw + new IOException + ( + "[E] Incorrect solution item. \"" + + line + + "\" should match the form \"(NAME ID TAG)\"." + ); + } + + result.add(item); + } + } + } +} -- cgit v1.2.3-70-g09d2