diff options
| author | bjculkin <bjculkin@WIT-136XG42.wvu-ad.wvu.edu> | 2017-04-12 10:46:51 -0400 |
|---|---|---|
| committer | bjculkin <bjculkin@WIT-136XG42.wvu-ad.wvu.edu> | 2017-04-12 10:46:51 -0400 |
| commit | f394306a4b65a3328551f9f6b8d4abff8bfd5b27 (patch) | |
| tree | 08b0de69bf15ae49077851eecfa3ca2efbc9e736 /JPratt/src/main/java/bjc/pratt/tokens | |
| parent | 694bed833470393ee00eae0a85bff0c6c90e692a (diff) | |
Package reorganization
Diffstat (limited to 'JPratt/src/main/java/bjc/pratt/tokens')
4 files changed, 134 insertions, 5 deletions
diff --git a/JPratt/src/main/java/bjc/pratt/tokens/StringToken.java b/JPratt/src/main/java/bjc/pratt/tokens/StringToken.java index 6d3c1a5..ff47667 100644 --- a/JPratt/src/main/java/bjc/pratt/tokens/StringToken.java +++ b/JPratt/src/main/java/bjc/pratt/tokens/StringToken.java @@ -1,7 +1,5 @@ package bjc.pratt.tokens; -import bjc.pratt.Token; - /** * Simple token implementation for strings. * diff --git a/JPratt/src/main/java/bjc/pratt/tokens/StringTokenStream.java b/JPratt/src/main/java/bjc/pratt/tokens/StringTokenStream.java index b33dae8..07e1827 100644 --- a/JPratt/src/main/java/bjc/pratt/tokens/StringTokenStream.java +++ b/JPratt/src/main/java/bjc/pratt/tokens/StringTokenStream.java @@ -4,9 +4,6 @@ import static bjc.pratt.tokens.StringToken.litToken; import java.util.Iterator; -import bjc.pratt.Token; -import bjc.pratt.TokenStream; - /** * Simple implementation of token stream for strings. * 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); + } +} |
