From 251419e1f0ab8eb04d21287b708b06a552f4c58a Mon Sep 17 00:00:00 2001 From: bculkin2442 Date: Mon, 10 Apr 2017 16:49:31 -0400 Subject: Warning resolution --- .../main/java/bjc/pratt/tokens/StringToken.java | 31 +++++++++++----------- 1 file changed, 16 insertions(+), 15 deletions(-) (limited to 'JPratt/src/main/java/bjc/pratt/tokens') diff --git a/JPratt/src/main/java/bjc/pratt/tokens/StringToken.java b/JPratt/src/main/java/bjc/pratt/tokens/StringToken.java index 2e75702..9f97e33 100644 --- a/JPratt/src/main/java/bjc/pratt/tokens/StringToken.java +++ b/JPratt/src/main/java/bjc/pratt/tokens/StringToken.java @@ -49,26 +49,19 @@ public class StringToken implements Token { @Override public boolean equals(Object obj) { - if (this == obj) - return true; - if (obj == null) - return false; - if (!(obj instanceof StringToken)) - return false; + if (this == obj) return true; + if (obj == null) return false; + if (!(obj instanceof StringToken)) return false; 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,7 +70,15 @@ public class StringToken implements Token { public String toString() { return String.format("StringToken [key='%s', val='%s']", key, 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(String val) { return new StringToken(val, val); } -- cgit v1.2.3 From 56f07e9a3aaa873fe385d224f088f048dbafa8f7 Mon Sep 17 00:00:00 2001 From: bculkin2442 Date: Mon, 10 Apr 2017 16:49:54 -0400 Subject: Cleanup --- .../main/java/bjc/pratt/tokens/StringToken.java | 26 +++++++++++----------- .../java/bjc/pratt/tokens/StringTokenStream.java | 18 +++++++-------- 2 files changed, 22 insertions(+), 22 deletions(-) (limited to 'JPratt/src/main/java/bjc/pratt/tokens') diff --git a/JPratt/src/main/java/bjc/pratt/tokens/StringToken.java b/JPratt/src/main/java/bjc/pratt/tokens/StringToken.java index 9f97e33..6d3c1a5 100644 --- a/JPratt/src/main/java/bjc/pratt/tokens/StringToken.java +++ b/JPratt/src/main/java/bjc/pratt/tokens/StringToken.java @@ -4,24 +4,24 @@ import bjc.pratt.Token; /** * Simple token implementation for strings. - * + * * @author EVE * */ public class StringToken implements Token { - 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,19 +41,19 @@ public class StringToken implements Token { 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) { + 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; @@ -73,13 +73,13 @@ public class StringToken implements Token { /** * 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(String val) { + 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..b33dae8 100644 --- a/JPratt/src/main/java/bjc/pratt/tokens/StringTokenStream.java +++ b/JPratt/src/main/java/bjc/pratt/tokens/StringTokenStream.java @@ -1,34 +1,34 @@ 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; +import bjc.pratt.Token; +import bjc.pratt.TokenStream; /** * 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 { - private Iterator> iter; + private final Iterator> iter; private Token curr; /** * Create a new token stream from a iterator. - * + * * @param itr * The iterator to use. - * + * */ - public StringTokenStream(Iterator> itr) { + public StringTokenStream(final Iterator> itr) { iter = itr; } -- cgit v1.2.3 From f394306a4b65a3328551f9f6b8d4abff8bfd5b27 Mon Sep 17 00:00:00 2001 From: bjculkin Date: Wed, 12 Apr 2017 10:46:51 -0400 Subject: Package reorganization --- .../main/java/bjc/pratt/tokens/StringToken.java | 2 - .../java/bjc/pratt/tokens/StringTokenStream.java | 3 - JPratt/src/main/java/bjc/pratt/tokens/Token.java | 30 ++++++ .../main/java/bjc/pratt/tokens/TokenStream.java | 104 +++++++++++++++++++++ 4 files changed, 134 insertions(+), 5 deletions(-) create mode 100644 JPratt/src/main/java/bjc/pratt/tokens/Token.java create mode 100644 JPratt/src/main/java/bjc/pratt/tokens/TokenStream.java (limited to 'JPratt/src/main/java/bjc/pratt/tokens') 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 + * The key type of this token. Represents the type of the token. + * + * @param + * The value type of this token. Represents any additional data + * for the token. + * + */ +public interface Token { + /** + * 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 + * The key type of the token. + * + * @param + * The value type of the token. + */ +public abstract class TokenStream implements Iterator> { + /** + * 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 current(); + + @Override + public abstract Token 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 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); + } +} -- cgit v1.2.3