summaryrefslogtreecommitdiff
path: root/JPratt/src/main/java/bjc/pratt/tokens
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/bjc/pratt/tokens
parent39ba97edf49270715ec61bedb7d4a62ada819ba0 (diff)
Rename package to new domainHEADtrunk
Rename the package to the new domain
Diffstat (limited to 'JPratt/src/main/java/bjc/pratt/tokens')
-rw-r--r--JPratt/src/main/java/bjc/pratt/tokens/ExpectionNotMet.java23
-rw-r--r--JPratt/src/main/java/bjc/pratt/tokens/SimpleToken.java60
-rw-r--r--JPratt/src/main/java/bjc/pratt/tokens/SimpleTokenStream.java82
-rw-r--r--JPratt/src/main/java/bjc/pratt/tokens/StringToken.java83
-rw-r--r--JPratt/src/main/java/bjc/pratt/tokens/StringTokenStream.java84
-rw-r--r--JPratt/src/main/java/bjc/pratt/tokens/Token.java30
-rw-r--r--JPratt/src/main/java/bjc/pratt/tokens/TokenStream.java106
7 files changed, 0 insertions, 468 deletions
diff --git a/JPratt/src/main/java/bjc/pratt/tokens/ExpectionNotMet.java b/JPratt/src/main/java/bjc/pratt/tokens/ExpectionNotMet.java
deleted file mode 100644
index 2bd45e2..0000000
--- a/JPratt/src/main/java/bjc/pratt/tokens/ExpectionNotMet.java
+++ /dev/null
@@ -1,23 +0,0 @@
-package bjc.pratt.tokens;
-
-import bjc.utils.parserutils.ParserException;
-
-/**
- * The exception thrown when an expectation fails.
- *
- * @author EVE
- *
- */
-public class ExpectionNotMet extends ParserException {
- private static final long serialVersionUID = 4299299480127680805L;
-
- /**
- * Create a new exception with the specified message.
- *
- * @param msg
- * The message of the exception.
- */
- public ExpectionNotMet(final String msg) {
- super(msg);
- }
-} \ No newline at end of file
diff --git a/JPratt/src/main/java/bjc/pratt/tokens/SimpleToken.java b/JPratt/src/main/java/bjc/pratt/tokens/SimpleToken.java
deleted file mode 100644
index 18e2e5a..0000000
--- a/JPratt/src/main/java/bjc/pratt/tokens/SimpleToken.java
+++ /dev/null
@@ -1,60 +0,0 @@
-package bjc.pratt.tokens;
-
-import java.util.Objects;
-
-/**
- * Simple token implementation
- *
- * @author bjcul
- *
- * @param <K> The key type
- * @param <V> The value type
- *
- */
-public class SimpleToken<K, V> implements Token<K, V> {
- private K key;
- private V value;
-
- /**
- * Create a new token
- * @param key The key
- * @param value The value
- */
- public SimpleToken(K key, V value) {
- super();
- this.key = key;
- this.value = value;
- }
-
- @Override
- public K getKey() {
- return key;
- }
-
- @Override
- public V getValue() {
- return value;
- }
-
- @Override
- public int hashCode() {
- return Objects.hash(key, value);
- }
-
- @Override
- public boolean equals(Object obj) {
- if (this == obj)
- return true;
- if (obj == null)
- return false;
- if (getClass() != obj.getClass())
- return false;
- SimpleToken<?, ?> other = (SimpleToken<?, ?>) obj;
- return Objects.equals(key, other.key) && Objects.equals(value, other.value);
- }
-
- @Override
- public String toString() {
- return "SimpleToken [key=" + key + ", value=" + value + "]";
- }
-}
diff --git a/JPratt/src/main/java/bjc/pratt/tokens/SimpleTokenStream.java b/JPratt/src/main/java/bjc/pratt/tokens/SimpleTokenStream.java
deleted file mode 100644
index fba38bb..0000000
--- a/JPratt/src/main/java/bjc/pratt/tokens/SimpleTokenStream.java
+++ /dev/null
@@ -1,82 +0,0 @@
-package bjc.pratt.tokens;
-
-import java.util.Iterator;
-
-import bjc.data.MarkListIterator;
-
-/**
- * Simple token stream implementation
- * @author bjcul
- *
- * @param <K> The key type
- * @param <V> The value type
- */
-public class SimpleTokenStream<K, V> extends TokenStream<K, V> {
- private final MarkListIterator<Token<K, V>> iter;
-
- private Token<K, V> curr;
-
- private Token<K, V> terminal;
- /**
- * Create a new token stream from a iterator.
- *
- * @param itr The iterator to use.
- * @param terminal The terminal token to use for end-of-stream
- *
- */
- public SimpleTokenStream(final Iterator<Token<K, V>> itr, Token<K, V> terminal) {
- this.iter = new MarkListIterator<>(itr);
- this.terminal = terminal;
- }
-
- @Override
- public Token<K, V> current() {
- // Prime stream if necessary
- if (curr == null)
- return next();
- return curr;
- }
-
- @Override
- public Token<K, V> next() {
- if (iter.hasNext()) {
- curr = iter.next();
- } else {
- curr = terminal;
- }
-
- return curr;
- }
-
- @Override
- public boolean hasNext() {
- return iter.hasNext();
- }
-
- @Override
- public void mark() {
- iter.mark();
- }
-
- @Override
- public void commit() {
- iter.commit();
-
- if (!iter.hasMark()) {
- // No marks outstanding; we can release the previous state
- iter.reset();
- }
- }
-
- @Override
- public void rollback() {
- iter.rollback();
-
- curr = iter.current();
- }
-
- @Override
- public boolean hasMark() {
- return iter.hasMark();
- }
-}
diff --git a/JPratt/src/main/java/bjc/pratt/tokens/StringToken.java b/JPratt/src/main/java/bjc/pratt/tokens/StringToken.java
deleted file mode 100644
index 750518a..0000000
--- a/JPratt/src/main/java/bjc/pratt/tokens/StringToken.java
+++ /dev/null
@@ -1,83 +0,0 @@
-package bjc.pratt.tokens;
-
-/**
- * Simple token implementation for strings.
- *
- * @author EVE
- *
- */
-public class StringToken implements Token<String, String> {
- 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(final String ky, final 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(final Object obj) {
- if(this == obj) return true;
- if(obj == null) return false;
- if(!(obj instanceof StringToken)) return false;
-
- final 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);
- }
-
- /**
- * 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
deleted file mode 100644
index 70876f2..0000000
--- a/JPratt/src/main/java/bjc/pratt/tokens/StringTokenStream.java
+++ /dev/null
@@ -1,84 +0,0 @@
-package bjc.pratt.tokens;
-
-import static bjc.pratt.tokens.StringToken.litToken;
-
-import java.util.Iterator;
-
-import bjc.data.MarkListIterator;
-
-/**
- * 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 final MarkListIterator<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(final Iterator<Token<String, String>> itr) {
- iter = new MarkListIterator<>(itr);
- }
-
- @Override
- public Token<String, String> current() {
- // Prime stream if necessary
- if (curr == null)
- return next();
- return curr;
- }
-
- @Override
- public Token<String, String> next() {
- if(iter.hasNext()) {
- curr = iter.next();
- } else {
- curr = litToken("(end)");
- }
-
- return curr;
- }
-
- @Override
- public boolean hasNext() {
- return iter.hasNext();
- }
-
- @Override
- public void mark() {
- iter.mark();
- }
-
- @Override
- public void commit() {
- iter.commit();
-
- if (!iter.hasMark()) {
- // No marks outstanding; we can release the previous state
- iter.reset();
- }
- }
-
- @Override
- public void rollback() {
- iter.rollback();
-
- curr = iter.current();
- }
-
- @Override
- public boolean hasMark() {
- return iter.hasMark();
- }
-}
diff --git a/JPratt/src/main/java/bjc/pratt/tokens/Token.java b/JPratt/src/main/java/bjc/pratt/tokens/Token.java
deleted file mode 100644
index 7de4ada..0000000
--- a/JPratt/src/main/java/bjc/pratt/tokens/Token.java
+++ /dev/null
@@ -1,30 +0,0 @@
-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
deleted file mode 100644
index dc09bcc..0000000
--- a/JPratt/src/main/java/bjc/pratt/tokens/TokenStream.java
+++ /dev/null
@@ -1,106 +0,0 @@
-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;
-
-/**
- * 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);
- }
-}