diff options
| author | bculkin2442 <bjculkin@mix.wvu.edu> | 2017-03-25 19:13:42 -0400 |
|---|---|---|
| committer | bculkin2442 <bjculkin@mix.wvu.edu> | 2017-03-25 19:13:42 -0400 |
| commit | 42990231fee502552b769b9af4c04ac0dcaeb195 (patch) | |
| tree | 4dbe0ba0bc54fffafacc9ab12349efd76c52041c /BJC-Utils2/src/main/java/bjc/utils/parserutils/pratt/tokens | |
| parent | 674d9769821775484fe6913b93c650189fbedfed (diff) | |
Update Pratt parser
Diffstat (limited to 'BJC-Utils2/src/main/java/bjc/utils/parserutils/pratt/tokens')
| -rw-r--r-- | BJC-Utils2/src/main/java/bjc/utils/parserutils/pratt/tokens/StringToken.java | 80 | ||||
| -rw-r--r-- | BJC-Utils2/src/main/java/bjc/utils/parserutils/pratt/tokens/StringTokenStream.java | 54 |
2 files changed, 134 insertions, 0 deletions
diff --git a/BJC-Utils2/src/main/java/bjc/utils/parserutils/pratt/tokens/StringToken.java b/BJC-Utils2/src/main/java/bjc/utils/parserutils/pratt/tokens/StringToken.java new file mode 100644 index 0000000..7e779aa --- /dev/null +++ b/BJC-Utils2/src/main/java/bjc/utils/parserutils/pratt/tokens/StringToken.java @@ -0,0 +1,80 @@ +package bjc.utils.parserutils.pratt.tokens; + +import bjc.utils.parserutils.pratt.Token; + +/** + * Simple token implementation for strings. + * + * @author EVE + * + */ +public class StringToken implements Token<String, String> { + private String key; + private 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) { + key = ky; + val = vl; + } + + @Override + public String getKey() { + return key; + } + + @Override + public String getValue() { + return val; + } + + @Override + public int hashCode() { + final int prime = 31; + + int result = 1; + 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; + + StringToken other = (StringToken) obj; + + if (key == null) { + 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; + + return true; + } + + @Override + public String toString() { + return String.format("StringToken [key='%s', val='%s']", key, val); + } +} diff --git a/BJC-Utils2/src/main/java/bjc/utils/parserutils/pratt/tokens/StringTokenStream.java b/BJC-Utils2/src/main/java/bjc/utils/parserutils/pratt/tokens/StringTokenStream.java new file mode 100644 index 0000000..d7d4a66 --- /dev/null +++ b/BJC-Utils2/src/main/java/bjc/utils/parserutils/pratt/tokens/StringTokenStream.java @@ -0,0 +1,54 @@ +package bjc.utils.parserutils.pratt.tokens; + +import java.util.Iterator; + +import bjc.utils.parserutils.pratt.Token; +import bjc.utils.parserutils.pratt.TokenStream; + +/** + * Simple implementation of token stream for strings. + * + * The terminal token here is represented by a token with type '(end)' and null + * value. + * + * @author EVE + * + */ +public class StringTokenStream extends TokenStream<String, String> { + private 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) { + iter = itr; + + } + + @Override + public Token<String, String> current() { + return curr; + } + + @Override + public Token<String, String> next() { + if (iter.hasNext()) { + curr = iter.next(); + } else { + curr = new StringToken("(end)", null); + } + + return curr; + } + + @Override + public boolean hasNext() { + return iter.hasNext(); + } +} |
