summaryrefslogtreecommitdiff
path: root/JPratt/src/main/java/com/ashardalon/pratt/tokens/TokenStream.java
diff options
context:
space:
mode:
authorBenjamin Culkin <scorpress@gmail.com>2024-06-03 17:33:53 -0400
committerBenjamin Culkin <scorpress@gmail.com>2024-06-03 17:33:53 -0400
commit15a2b29e48f134bc93cfd0a3d8512001e9242f3d (patch)
treeb3f5c4c5f0e474479cd47ad0ac0f35770fc44881 /JPratt/src/main/java/com/ashardalon/pratt/tokens/TokenStream.java
parent39ba97edf49270715ec61bedb7d4a62ada819ba0 (diff)
Rename package to new domainHEADtrunk
Rename the package to the new domain
Diffstat (limited to 'JPratt/src/main/java/com/ashardalon/pratt/tokens/TokenStream.java')
-rw-r--r--JPratt/src/main/java/com/ashardalon/pratt/tokens/TokenStream.java106
1 files changed, 106 insertions, 0 deletions
diff --git a/JPratt/src/main/java/com/ashardalon/pratt/tokens/TokenStream.java b/JPratt/src/main/java/com/ashardalon/pratt/tokens/TokenStream.java
new file mode 100644
index 0000000..3f2681b
--- /dev/null
+++ b/JPratt/src/main/java/com/ashardalon/pratt/tokens/TokenStream.java
@@ -0,0 +1,106 @@
+package com.ashardalon.pratt.tokens;
+
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.Set;
+
+import bjc.utils.funcutils.StringUtils;
+
+/**
+ * A stream of tokens.
+ *
+ * @author EVE
+ *
+ * @param <K>
+ * The key type of the token.
+ *
+ * @param <V>
+ * The value type of the token.
+ */
+public abstract class TokenStream<K, V> implements Iterator<Token<K, V>> {
+ /**
+ * Get the current token.
+ *
+ * @return The current token.
+ */
+ public abstract Token<K, V> current();
+
+ @Override
+ public abstract Token<K, V> next();
+
+ @Override
+ public abstract boolean hasNext();
+
+ /**
+ * Place a mark in the current stream, which can be either returned to or abandoned later on.
+ */
+ public abstract void mark();
+
+ /**
+ * Reset the stream to the state it was in when the last mark was taken.
+ */
+ public abstract void rollback();
+
+ /**
+ * Check if the stream has at least one mark.
+ *
+ * @return Whether or not at least one mark exists.
+ */
+ public abstract boolean hasMark();
+
+ /**
+ * Remove the last mark placed into the stream. This prevents returning to it later on.
+ */
+ public abstract void commit();
+
+ /**
+ * 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 ExpectionNotMet
+ * If the token is not one of the expected types.
+ */
+ public void expect(final Set<K> expectedKeys) throws ExpectionNotMet {
+ final K curKey = current().getKey();
+
+ if(!expectedKeys.contains(curKey)) {
+ final String expectedList = StringUtils.toEnglishList(expectedKeys.toArray(), false);
+
+ throw new ExpectionNotMet("One of '" + expectedList + "' was expected, not " + curKey);
+ }
+
+ 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 ExpectionNotMet
+ * If the token is not one of the expected types.
+ */
+ @SafeVarargs
+ public final void expect(final K... expectedKeys) throws ExpectionNotMet {
+ HashSet<K> keys = new HashSet<>(Arrays.asList(expectedKeys));
+ expect(keys);
+ }
+
+ /**
+ * Check whether the head token is a certain type.
+ *
+ * @param val
+ * The type to check for.
+ *
+ * @return Whether or not the head token is of that type.
+ */
+ public boolean headIs(final K val) {
+ return current().getKey().equals(val);
+ }
+}