diff options
| author | Ben Culkin <scorpress@gmail.com> | 2023-10-25 19:33:51 -0400 |
|---|---|---|
| committer | Ben Culkin <scorpress@gmail.com> | 2023-10-25 19:33:51 -0400 |
| commit | f2037af115d8459aeaed003bb7c4aa0557a040b9 (patch) | |
| tree | 364333a240024b39ba6e40384ed92a3da29f9b2e | |
| parent | c3a5210cbcda9b5a80f37c31698e35ca6fb5a2d8 (diff) | |
Tweak
7 files changed, 237 insertions, 1 deletions
diff --git a/JPratt/.classpath b/JPratt/.classpath index 929a07e..2236a24 100644 --- a/JPratt/.classpath +++ b/JPratt/.classpath @@ -10,11 +10,13 @@ <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-17"> <attributes> <attribute name="maven.pomderived" value="true"/> + <attribute name="module" value="true"/> </attributes> </classpathentry> <classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER"> <attributes> <attribute name="maven.pomderived" value="true"/> + <attribute name="module" value="true"/> </attributes> </classpathentry> <classpathentry kind="src" output="target/test-classes" path="src/test/java"> diff --git a/JPratt/src/main/java/bjc/pratt/commands/BranchInitialCommand.java b/JPratt/src/main/java/bjc/pratt/commands/BranchInitialCommand.java new file mode 100644 index 0000000..3f3093e --- /dev/null +++ b/JPratt/src/main/java/bjc/pratt/commands/BranchInitialCommand.java @@ -0,0 +1,38 @@ +package bjc.pratt.commands; + +import java.util.Map; + +import bjc.pratt.ParserContext; +import bjc.pratt.tokens.Token; +import bjc.utils.parserutils.ParserException; + +/** + * Represents a initial command that has a number of 'sub-commands' in the way that Go/Git CLI does. + * + * @author bjcul + * + * @param <K> Token key type + * @param <V> Token value type + * @param <C> Parser context type + */ +public class BranchInitialCommand<K, V, C> implements InitialCommand<K, V, C> { + private Map<K, InitialCommand<K, V, C>> comMap; + + /** + * Create a new branch initial command + * + * @param mep The map containing the commands + */ + public BranchInitialCommand(Map<K, InitialCommand<K, V, C>> mep) { + this.comMap = mep; + } + + @Override + public CommandResult<K, V> denote(Token<K, V> operator, ParserContext<K, V, C> ctx) throws ParserException { + Token<K, V> curToken = ctx.tokens.current(); + ctx.tokens.expect(comMap.keySet()); + + return comMap.get(curToken.getKey()).denote(curToken, ctx); + } + +} diff --git a/JPratt/src/main/java/bjc/pratt/commands/CommandResult.java b/JPratt/src/main/java/bjc/pratt/commands/CommandResult.java index d27400f..38a55ae 100644 --- a/JPratt/src/main/java/bjc/pratt/commands/CommandResult.java +++ b/JPratt/src/main/java/bjc/pratt/commands/CommandResult.java @@ -94,4 +94,19 @@ public class CommandResult<K, V> { CommandResult<K2, V2> result = new CommandResult<>(Status.BACKTRACK); return result; } + + @Override + public String toString() { + StringBuilder builder = new StringBuilder(); + builder.append("CommandResult [status="); + builder.append(status); + if (status == Status.SUCCESS) { + builder.append(", success="); + builder.append(success); + } + builder.append("]"); + return builder.toString(); + } + + } diff --git a/JPratt/src/main/java/bjc/pratt/commands/impls/InitialCommands.java b/JPratt/src/main/java/bjc/pratt/commands/impls/InitialCommands.java index bfc4e4f..6af6954 100644 --- a/JPratt/src/main/java/bjc/pratt/commands/impls/InitialCommands.java +++ b/JPratt/src/main/java/bjc/pratt/commands/impls/InitialCommands.java @@ -7,9 +7,10 @@ import static bjc.pratt.blocks.ParseBlocks.trigger; import java.util.function.UnaryOperator; import bjc.pratt.blocks.ParseBlock; -import bjc.pratt.commands.InitialCommand; +import bjc.pratt.commands.*; import bjc.pratt.tokens.Token; import bjc.data.Tree; +import bjc.functypes.MapBuilder; /** * * Contains factory methods for producing common implementations of @@ -211,4 +212,30 @@ public class InitialCommands { public static <K, V, C> InitialCommand<K, V, C> panfix(final int precedence, final K term, final Token<K, V> marker) { return new PanfixCommand<>(marker, term, precedence); } + + /** + * Create a command that unconditionally returns a failure result. + * + * @param <K> Token key type + * @param <V> Token value type + * @param <C> Context type + * + * @return A command that unconditionally fails + */ + public static <K, V, C> InitialCommand<K, V, C> fail() { + return (operator, ctx) -> CommandResult.fail(); + } + + /** + * Create a new builder for branching/sub-command style commands. + * + * @param <K> Token key type + * @param <V> Value key type + * @param <C> Context type + * + * @return A builder for branching/sub-command style commands + */ + public static <K, V, C> MapBuilder<K, InitialCommand<K, V, C>, InitialCommand<K, V, C>> branch() { + return MapBuilder.from(BranchInitialCommand::new); + } }
\ 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 new file mode 100644 index 0000000..18e2e5a --- /dev/null +++ b/JPratt/src/main/java/bjc/pratt/tokens/SimpleToken.java @@ -0,0 +1,60 @@ +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 new file mode 100644 index 0000000..fba38bb --- /dev/null +++ b/JPratt/src/main/java/bjc/pratt/tokens/SimpleTokenStream.java @@ -0,0 +1,82 @@ +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/module-info.java b/JPratt/src/main/java/module-info.java new file mode 100644 index 0000000..8bed9b1 --- /dev/null +++ b/JPratt/src/main/java/module-info.java @@ -0,0 +1,12 @@ +module jpratt { + exports bjc.pratt.tokens; + exports bjc.pratt.commands; + exports bjc.pratt.blocks; + exports bjc.pratt.commands.impls; + exports bjc.pratt; + exports bjc.pratt.parsing; + + requires bjc.utils; + requires esodata; + requires junit; +}
\ No newline at end of file |
