summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNathanael Sensfelder <SpamShield0@MultiAgentSystems.org>2017-08-03 15:28:17 +0200
committerNathanael Sensfelder <SpamShield0@MultiAgentSystems.org>2017-08-03 15:28:17 +0200
commit90bb7e959496c3a12bebe055f6344b9f06f22809 (patch)
tree6635decc697d91c8cba6da9db8959b706ad9842f /sol-pretty-printer/src/QuickSolParser.java
parentc5a23ef9d6ab1e89b85016831fc8b2431f68f87f (diff)
Improving clarity through better Makefiles.
Diffstat (limited to 'sol-pretty-printer/src/QuickSolParser.java')
-rw-r--r--sol-pretty-printer/src/QuickSolParser.java117
1 files changed, 117 insertions, 0 deletions
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("\\((?<list>[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<String[]> next_solution ()
+ throws IOException
+ {
+ final List<String[]> result;
+ final Matcher matcher;
+ boolean has_started_sol;
+ String line;
+
+ result = new ArrayList<String[]>();
+ 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);
+ }
+ }
+ }
+}