summaryrefslogtreecommitdiff
path: root/JPratt/src/main/java/bjc/pratt/tokens
diff options
context:
space:
mode:
authorStudent <student@Administrators-iMac-2.local>2017-04-12 11:05:57 -0400
committerStudent <student@Administrators-iMac-2.local>2017-04-12 11:05:57 -0400
commit22c356cd411cf0fcc18d548291af26bc7588a3aa (patch)
tree4f24fdda182b358ca96aed2249bb4e8a19994747 /JPratt/src/main/java/bjc/pratt/tokens
parent2dc1b5dd145ab0e2b3e3df67f967a9c07ed6d303 (diff)
parentf394306a4b65a3328551f9f6b8d4abff8bfd5b27 (diff)
Merge branch 'master' of https://github.com/bculkin2442/JPratt.git
Diffstat (limited to 'JPratt/src/main/java/bjc/pratt/tokens')
-rw-r--r--JPratt/src/main/java/bjc/pratt/tokens/StringToken.java55
-rw-r--r--JPratt/src/main/java/bjc/pratt/tokens/StringTokenStream.java17
-rw-r--r--JPratt/src/main/java/bjc/pratt/tokens/Token.java30
-rw-r--r--JPratt/src/main/java/bjc/pratt/tokens/TokenStream.java104
4 files changed, 168 insertions, 38 deletions
diff --git a/JPratt/src/main/java/bjc/pratt/tokens/StringToken.java b/JPratt/src/main/java/bjc/pratt/tokens/StringToken.java
index 2e75702..ff47667 100644
--- a/JPratt/src/main/java/bjc/pratt/tokens/StringToken.java
+++ b/JPratt/src/main/java/bjc/pratt/tokens/StringToken.java
@@ -1,27 +1,25 @@
package bjc.pratt.tokens;
-import bjc.pratt.Token;
-
/**
* Simple token implementation for strings.
- *
+ *
* @author EVE
*
*/
public class StringToken implements Token<String, String> {
- private String key;
- private String val;
+ private final String key;
+ private final String val;
/**
* Create a new string token.
- *
+ *
* @param ky
* The key for the token.
- *
+ *
* @param vl
* The value for the token.
*/
- public StringToken(String ky, String vl) {
+ public StringToken(final String ky, final String vl) {
key = ky;
val = vl;
}
@@ -41,34 +39,27 @@ public class StringToken implements Token<String, String> {
final int prime = 31;
int result = 1;
- result = prime * result + ((key == null) ? 0 : key.hashCode());
- result = prime * result + ((val == null) ? 0 : val.hashCode());
+ result = prime * result + (key == null ? 0 : key.hashCode());
+ result = prime * result + (val == null ? 0 : val.hashCode());
return result;
}
@Override
- public boolean equals(Object obj) {
- if (this == obj)
- return true;
- if (obj == null)
- return false;
- if (!(obj instanceof StringToken))
- return false;
+ public boolean equals(final Object obj) {
+ if (this == obj) return true;
+ if (obj == null) return false;
+ if (!(obj instanceof StringToken)) return false;
- StringToken other = (StringToken) obj;
+ final StringToken other = (StringToken) obj;
if (key == null) {
- if (other.key != null)
- return false;
- } else if (!key.equals(other.key))
- return false;
+ if (other.key != null) return false;
+ } else if (!key.equals(other.key)) return false;
if (val == null) {
- if (other.val != null)
- return false;
- } else if (!val.equals(other.val))
- return false;
+ if (other.val != null) return false;
+ } else if (!val.equals(other.val)) return false;
return true;
}
@@ -77,8 +68,16 @@ public class StringToken implements Token<String, String> {
public String toString() {
return String.format("StringToken [key='%s', val='%s']", key, val);
}
-
- public static StringToken litToken(String val) {
+
+ /**
+ * Create a new literal token (has same key/value).
+ *
+ * @param val
+ * The value for the literal token.
+ *
+ * @return A literal token with that key.
+ */
+ public static StringToken litToken(final String val) {
return new StringToken(val, val);
}
}
diff --git a/JPratt/src/main/java/bjc/pratt/tokens/StringTokenStream.java b/JPratt/src/main/java/bjc/pratt/tokens/StringTokenStream.java
index 7f8215a..07e1827 100644
--- a/JPratt/src/main/java/bjc/pratt/tokens/StringTokenStream.java
+++ b/JPratt/src/main/java/bjc/pratt/tokens/StringTokenStream.java
@@ -1,34 +1,31 @@
package bjc.pratt.tokens;
-import bjc.pratt.Token;
-import bjc.pratt.TokenStream;
+import static bjc.pratt.tokens.StringToken.litToken;
import java.util.Iterator;
-import static bjc.pratt.tokens.StringToken.litToken;
-
/**
* Simple implementation of token stream for strings.
- *
+ *
* The terminal token here is represented by a token with type and value
* '(end)'.
- *
+ *
* @author EVE
*
*/
public class StringTokenStream extends TokenStream<String, String> {
- private Iterator<Token<String, String>> iter;
+ private final Iterator<Token<String, String>> iter;
private Token<String, String> curr;
/**
* Create a new token stream from a iterator.
- *
+ *
* @param itr
* The iterator to use.
- *
+ *
*/
- public StringTokenStream(Iterator<Token<String, String>> itr) {
+ public StringTokenStream(final Iterator<Token<String, String>> itr) {
iter = itr;
}
diff --git a/JPratt/src/main/java/bjc/pratt/tokens/Token.java b/JPratt/src/main/java/bjc/pratt/tokens/Token.java
new file mode 100644
index 0000000..b07d2e1
--- /dev/null
+++ b/JPratt/src/main/java/bjc/pratt/tokens/Token.java
@@ -0,0 +1,30 @@
+package bjc.pratt.tokens;
+
+/**
+ * Represents a simple parsing token.
+ *
+ * @author EVE
+ *
+ * @param <K>
+ * The key type of this token. Represents the type of the token.
+ *
+ * @param <V>
+ * The value type of this token. Represents any additional data
+ * for the token.
+ *
+ */
+public interface Token<K, V> {
+ /**
+ * Get the key for this token.
+ *
+ * @return The key for this token
+ */
+ K getKey();
+
+ /**
+ * Get the value for this token.
+ *
+ * @return The value for this token.
+ */
+ V getValue();
+}
diff --git a/JPratt/src/main/java/bjc/pratt/tokens/TokenStream.java b/JPratt/src/main/java/bjc/pratt/tokens/TokenStream.java
new file mode 100644
index 0000000..a5febcc
--- /dev/null
+++ b/JPratt/src/main/java/bjc/pratt/tokens/TokenStream.java
@@ -0,0 +1,104 @@
+package bjc.pratt.tokens;
+
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.Set;
+
+import bjc.utils.funcutils.StringUtils;
+import bjc.utils.parserutils.ParserException;
+
+/**
+ * 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>> {
+ /**
+ * The exception thrown when an expectation fails.
+ *
+ * @author EVE
+ *
+ */
+ public static class ExpectationException extends ParserException {
+ private static final long serialVersionUID = 4299299480127680805L;
+
+ /**
+ * Create a new exception with the specified message.
+ *
+ * @param msg
+ * The message of the exception.
+ */
+ public ExpectationException(final String msg) {
+ super(msg);
+ }
+ }
+
+ /**
+ * 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();
+
+ /**
+ * 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.
+ */
+ public void expect(final Set<K> expectedKeys) throws ExpectationException {
+ final K curKey = current().getKey();
+
+ if (!expectedKeys.contains(curKey)) {
+ final String expectedList = StringUtils.toEnglishList(expectedKeys.toArray(), false);
+
+ throw new ExpectationException("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 ExpectationException
+ * If the token is not one of the expected types.
+ */
+ @SafeVarargs
+ public final void expect(final K... expectedKeys) throws ExpectationException {
+ expect(new HashSet<>(Arrays.asList(expectedKeys)));
+ }
+
+ /**
+ * 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);
+ }
+}