summaryrefslogtreecommitdiff
path: root/BJC-Utils2/src/main/java/bjc/utils/parserutils/pratt/TokenStream.java
diff options
context:
space:
mode:
authorbjculkin <bjculkin@mix.wvu.edu>2017-03-24 09:54:17 -0400
committerbjculkin <bjculkin@mix.wvu.edu>2017-03-24 09:54:17 -0400
commit41c2a41eaf3c2dd158a2a51947180f402918229e (patch)
tree47cbe22f24c7c0898ae9154734973846224332d8 /BJC-Utils2/src/main/java/bjc/utils/parserutils/pratt/TokenStream.java
parentb0d27faf67ec23b3d55786e00d4fd3b0d07567ee (diff)
Implement Pratt parser.
Diffstat (limited to 'BJC-Utils2/src/main/java/bjc/utils/parserutils/pratt/TokenStream.java')
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/parserutils/pratt/TokenStream.java88
1 files changed, 88 insertions, 0 deletions
diff --git a/BJC-Utils2/src/main/java/bjc/utils/parserutils/pratt/TokenStream.java b/BJC-Utils2/src/main/java/bjc/utils/parserutils/pratt/TokenStream.java
new file mode 100644
index 0000000..8decc09
--- /dev/null
+++ b/BJC-Utils2/src/main/java/bjc/utils/parserutils/pratt/TokenStream.java
@@ -0,0 +1,88 @@
+package bjc.utils.parserutils.pratt;
+
+import bjc.utils.funcutils.StringUtils;
+import bjc.utils.parserutils.ParserException;
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.Set;
+
+/**
+ * A stream of tokens.
+ *
+ * @author EVE
+ *
+ * @param <K>
+ * The key type of the token.
+ *
+ * @param <V>
+ * The value type of the token.
+ */
+public interface TokenStream<K, V> {
+ /**
+ * The exception thrown when an expectation fails.
+ *
+ * @author EVE
+ *
+ */
+ public static class ExpectationException extends ParserException {
+ /**
+ * Create a new exception with the specified message.
+ *
+ * @param msg
+ * The message of the exception.
+ */
+ public ExpectationException(String msg) {
+ super(msg);
+ }
+ }
+
+ /**
+ * Get the current token.
+ *
+ * @return The current token.
+ */
+ Token<K, V> current();
+
+ /**
+ * Advance to the next token in the stream.
+ *
+ * Has no effect when the end of the stream is reached.
+ */
+ void next();
+
+ /**
+ * Utility method for checking that the next token is one of a specific
+ * set of types, and then consuming it.
+ *
+ * @param expectedKeys
+ * The expected values
+ *
+ * @throws ExpectationException
+ * If the token is not one of the expected types.
+ */
+ default void expect(Set<K> expectedKeys) throws ExpectationException {
+ K curKey = current().getKey();
+
+ if(!expectedKeys.contains(curKey)) {
+ String expectedList = StringUtils.toEnglishList(expectedKeys.toArray(), false);
+
+ throw new ExpectationException("One of '" + expectedList + "' was expected, not " + curKey);
+ } else {
+ next();
+ }
+ }
+
+ /**
+ * Utility method for checking that the next token is one of a specific
+ * set of types, and then consuming it.
+ *
+ * @param expectedKeys
+ * The expected values
+ *
+ * @throws ExpectationException
+ * If the token is not one of the expected types.
+ */
+ default void expect(@SuppressWarnings("unchecked") K... expectedKeys) throws ExpectationException {
+ expect(new HashSet<>(Arrays.asList(expectedKeys)));
+ }
+}