summaryrefslogtreecommitdiff
path: root/JPratt/src/main/java/com/ashardalon
diff options
context:
space:
mode:
Diffstat (limited to 'JPratt/src/main/java/com/ashardalon')
-rw-r--r--JPratt/src/main/java/com/ashardalon/pratt/ParserContext.java56
-rw-r--r--JPratt/src/main/java/com/ashardalon/pratt/PrattParser.java334
-rw-r--r--JPratt/src/main/java/com/ashardalon/pratt/blocks/ChainParseBlock.java86
-rw-r--r--JPratt/src/main/java/com/ashardalon/pratt/blocks/GrammarParseBlock.java92
-rw-r--r--JPratt/src/main/java/com/ashardalon/pratt/blocks/ParseBlock.java38
-rw-r--r--JPratt/src/main/java/com/ashardalon/pratt/blocks/ParseBlocks.java98
-rw-r--r--JPratt/src/main/java/com/ashardalon/pratt/blocks/RepeatingParseBlock.java101
-rw-r--r--JPratt/src/main/java/com/ashardalon/pratt/blocks/SimpleParseBlock.java98
-rw-r--r--JPratt/src/main/java/com/ashardalon/pratt/blocks/TriggeredParseBlock.java63
-rw-r--r--JPratt/src/main/java/com/ashardalon/pratt/commands/AbstractInitialCommand.java45
-rw-r--r--JPratt/src/main/java/com/ashardalon/pratt/commands/BinaryCommand.java55
-rw-r--r--JPratt/src/main/java/com/ashardalon/pratt/commands/BinaryPostCommand.java36
-rw-r--r--JPratt/src/main/java/com/ashardalon/pratt/commands/BranchInitialCommand.java39
-rw-r--r--JPratt/src/main/java/com/ashardalon/pratt/commands/CommandResult.java113
-rw-r--r--JPratt/src/main/java/com/ashardalon/pratt/commands/InitialCommand.java40
-rw-r--r--JPratt/src/main/java/com/ashardalon/pratt/commands/MetaInitialCommand.java27
-rw-r--r--JPratt/src/main/java/com/ashardalon/pratt/commands/MetaNonInitialCommand.java28
-rw-r--r--JPratt/src/main/java/com/ashardalon/pratt/commands/NonInitialCommand.java66
-rw-r--r--JPratt/src/main/java/com/ashardalon/pratt/commands/impls/BlockInitialCommand.java43
-rw-r--r--JPratt/src/main/java/com/ashardalon/pratt/commands/impls/ChainCommand.java83
-rw-r--r--JPratt/src/main/java/com/ashardalon/pratt/commands/impls/ConstantCommand.java43
-rw-r--r--JPratt/src/main/java/com/ashardalon/pratt/commands/impls/DefaultInitialCommand.java30
-rw-r--r--JPratt/src/main/java/com/ashardalon/pratt/commands/impls/DefaultNonInitialCommand.java35
-rw-r--r--JPratt/src/main/java/com/ashardalon/pratt/commands/impls/DenestingCommand.java50
-rw-r--r--JPratt/src/main/java/com/ashardalon/pratt/commands/impls/GroupingCommand.java55
-rw-r--r--JPratt/src/main/java/com/ashardalon/pratt/commands/impls/InitialCommands.java242
-rw-r--r--JPratt/src/main/java/com/ashardalon/pratt/commands/impls/LeafCommand.java31
-rw-r--r--JPratt/src/main/java/com/ashardalon/pratt/commands/impls/LeftBinaryCommand.java34
-rw-r--r--JPratt/src/main/java/com/ashardalon/pratt/commands/impls/NonBinaryCommand.java39
-rw-r--r--JPratt/src/main/java/com/ashardalon/pratt/commands/impls/NonInitialCommands.java167
-rw-r--r--JPratt/src/main/java/com/ashardalon/pratt/commands/impls/PanfixCommand.java55
-rw-r--r--JPratt/src/main/java/com/ashardalon/pratt/commands/impls/PostCircumfixCommand.java64
-rw-r--r--JPratt/src/main/java/com/ashardalon/pratt/commands/impls/PostfixCommand.java43
-rw-r--r--JPratt/src/main/java/com/ashardalon/pratt/commands/impls/PreTernaryCommand.java85
-rw-r--r--JPratt/src/main/java/com/ashardalon/pratt/commands/impls/RightBinaryCommand.java32
-rw-r--r--JPratt/src/main/java/com/ashardalon/pratt/commands/impls/TernaryCommand.java78
-rw-r--r--JPratt/src/main/java/com/ashardalon/pratt/commands/impls/TransformingInitialCommand.java59
-rw-r--r--JPratt/src/main/java/com/ashardalon/pratt/commands/impls/UnaryCommand.java52
-rw-r--r--JPratt/src/main/java/com/ashardalon/pratt/parsing/package-info.java1
-rw-r--r--JPratt/src/main/java/com/ashardalon/pratt/tokens/ExpectionNotMet.java23
-rw-r--r--JPratt/src/main/java/com/ashardalon/pratt/tokens/SimpleToken.java60
-rw-r--r--JPratt/src/main/java/com/ashardalon/pratt/tokens/SimpleTokenStream.java82
-rw-r--r--JPratt/src/main/java/com/ashardalon/pratt/tokens/StringToken.java83
-rw-r--r--JPratt/src/main/java/com/ashardalon/pratt/tokens/StringTokenStream.java84
-rw-r--r--JPratt/src/main/java/com/ashardalon/pratt/tokens/Token.java30
-rw-r--r--JPratt/src/main/java/com/ashardalon/pratt/tokens/TokenStream.java106
46 files changed, 3204 insertions, 0 deletions
diff --git a/JPratt/src/main/java/com/ashardalon/pratt/ParserContext.java b/JPratt/src/main/java/com/ashardalon/pratt/ParserContext.java
new file mode 100644
index 0000000..cd6fcc2
--- /dev/null
+++ b/JPratt/src/main/java/com/ashardalon/pratt/ParserContext.java
@@ -0,0 +1,56 @@
+package com.ashardalon.pratt;
+
+import com.ashardalon.pratt.tokens.TokenStream;
+
+/**
+ * Represents the contextual state passed to a command.
+ *
+ * @author EVE
+ *
+ * @param <K>
+ * The key type of the tokens.
+ * @param <V>
+ * The value type of the tokens.
+ *
+ * @param <C>
+ * The state type of the parser.
+ */
+public class ParserContext<K, V, C> {
+ /**
+ * The source of tokens.
+ */
+ public TokenStream<K, V> tokens;
+
+ /**
+ * The parser for sub-expressions.
+ */
+ public PrattParser<K, V, C> parse;
+
+ /**
+ * The state of the parser.
+ */
+ public C state;
+
+ /**
+ * The initial command for the current expression.
+ */
+ public K initial;
+
+ /**
+ * Create a new parser context.
+ *
+ * @param tokns
+ * The source of tokens.
+ *
+ * @param prse
+ * The parser to call for sub expressions.
+ *
+ * @param stte
+ * Any state needing to be kept during parsing.
+ */
+ public ParserContext(final TokenStream<K, V> tokns, final PrattParser<K, V, C> prse, final C stte) {
+ tokens = tokns;
+ parse = prse;
+ state = stte;
+ }
+} \ No newline at end of file
diff --git a/JPratt/src/main/java/com/ashardalon/pratt/PrattParser.java b/JPratt/src/main/java/com/ashardalon/pratt/PrattParser.java
new file mode 100644
index 0000000..ea7afd8
--- /dev/null
+++ b/JPratt/src/main/java/com/ashardalon/pratt/PrattParser.java
@@ -0,0 +1,334 @@
+package com.ashardalon.pratt;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+
+import com.ashardalon.pratt.commands.CommandResult;
+import com.ashardalon.pratt.commands.InitialCommand;
+import com.ashardalon.pratt.commands.MetaInitialCommand;
+import com.ashardalon.pratt.commands.MetaNonInitialCommand;
+import com.ashardalon.pratt.commands.NonInitialCommand;
+import com.ashardalon.pratt.commands.CommandResult.Status;
+import com.ashardalon.pratt.commands.impls.DefaultInitialCommand;
+import com.ashardalon.pratt.commands.impls.DefaultNonInitialCommand;
+import com.ashardalon.pratt.tokens.ExpectionNotMet;
+import com.ashardalon.pratt.tokens.Token;
+import com.ashardalon.pratt.tokens.TokenStream;
+
+import bjc.data.TransformIterator;
+import bjc.data.Tree;
+import bjc.utils.funcutils.NumberUtils;
+import bjc.utils.parserutils.ParserException;
+
+/**
+ * A configurable Pratt parser for expressions.
+ *
+ * @author EVE
+ *
+ * @param <K> The key type for the tokens.
+ *
+ * @param <V> The value type for the tokens.
+ *
+ * @param <C> The state type of the parser.
+ *
+ *
+ */
+public class PrattParser<K, V, C> {
+ /*
+ * Default commands that error when used.
+ */
+ private final NonInitialCommand<K, V, C> DEFAULT_LEFT_COMMAND = new DefaultNonInitialCommand<>();
+ private final List<NonInitialCommand<K, V, C>> DEFAULT_LEFT_LIST = Arrays.asList(DEFAULT_LEFT_COMMAND);
+
+ private final InitialCommand<K, V, C> DEFAULT_NULL_COMMAND = new DefaultInitialCommand<>();
+ private final List<InitialCommand<K, V, C>> DEFAULT_NULL_LIST = Arrays.asList(DEFAULT_NULL_COMMAND);
+
+ /*
+ * Left-commands that depend on what the null command was.
+ */
+ private final Map<K, Map<K, List<NonInitialCommand<K, V, C>>>> dependantLeftCommands;
+ private final Map<K, Map<K, List<MetaNonInitialCommand<K, V, C>>>> dependantMetaLeftCommands;
+ /*
+ * The left commands.
+ */
+ private final Map<K, List<NonInitialCommand<K, V, C>>> leftCommands;
+ private final Map<K, List<MetaNonInitialCommand<K, V, C>>> metaLeftCommands;
+
+ /*
+ * The initial commands.
+ */
+ private final Map<K, List<InitialCommand<K, V, C>>> nullCommands;
+ private final Map<K, List<MetaInitialCommand<K, V, C>>> metaNullCommands;
+
+ /*
+ * Initial commands only checked for statements.
+ */
+ private final Map<K, List<InitialCommand<K, V, C>>> statementCommands;
+ private final Map<K, List<MetaInitialCommand<K, V, C>>> metaStatementCommands;
+
+ /**
+ * Create a new Pratt parser.
+ *
+ */
+ public PrattParser() {
+ dependantLeftCommands = new HashMap<>();
+ dependantMetaLeftCommands = new HashMap<>();
+
+ leftCommands = new HashMap<>();
+ metaLeftCommands = new HashMap<>();
+
+ nullCommands = new HashMap<>();
+ metaNullCommands = new HashMap<>();
+
+ statementCommands = new HashMap<>();
+ metaStatementCommands = new HashMap<>();
+ }
+
+ /**
+ * Parse an expression.
+ *
+ * @param precedence The initial precedence for the expression.
+ *
+ * @param tokens The tokens for the expression.
+ *
+ * @param state The state of the parser.
+ *
+ * @param isStatement Whether or not to parse statements.
+ *
+ * @return The expression as an AST.
+ *
+ * @throws ParserException If something goes wrong during parsing.
+ */
+ public CommandResult<K, V> parseExpression(final int precedence, final TokenStream<K, V> tokens, final C state,
+ final boolean isStatement) throws ParserException {
+ if (precedence < 0)
+ throw new IllegalArgumentException("Precedence must be greater than zero");
+
+ ParserContext<K, V, C> parserContext = new ParserContext<>(tokens, this, state);
+
+ Tree<Token<K, V>> ast = null;
+ CommandResult<K, V> result;
+
+ tokens.mark();
+ final Token<K, V> initToken = tokens.current();
+ tokens.next();
+ tokens.mark();
+
+ final K initKey = initToken.getKey();
+ Iterator<InitialCommand<K, V, C>> nullCommandIter = getInitialCommand(isStatement, initKey, parserContext);
+ do {
+ if (!nullCommandIter.hasNext()) {
+ // Restore to the state we were in before we tried to parse this token.
+ // Need the commit because rollback doesn't remove marks
+ tokens.rollback();
+ tokens.commit();
+ tokens.rollback();
+ return CommandResult.backtrack();
+ }
+
+ InitialCommand<K, V, C> nullCommand = nullCommandIter.next();
+ try {
+ result = nullCommand.denote(initToken, parserContext);
+ } catch (ExpectionNotMet enm) {
+ // TODO: Should enm be used for something here?
+ result = CommandResult.backtrack();
+ }
+ switch (result.status) {
+ case SUCCESS:
+ ast = result.success();
+ break;
+ case FAIL:
+ return result;
+ case BACKTRACK:
+ tokens.rollback();
+ break;
+ default:
+ throw new IllegalStateException("Unhandled result status " + result.status);
+ }
+ parserContext.initial = initKey;
+ } while (result.status != Status.SUCCESS);
+ tokens.commit();
+ // Think this is right...
+ // Will get rid of all our active marks.
+ tokens.commit();
+
+ int rightPrec = Integer.MAX_VALUE;
+
+ outer: while (true) {
+ tokens.mark();
+ final Token<K, V> tok = tokens.current();
+
+ final K key = tok.getKey();
+
+ Iterator<NonInitialCommand<K, V, C>> leftCommandIter = getNonInitialCommand(key, parserContext);
+ do {
+ if (!leftCommandIter.hasNext()) {
+ // Restore to the state we were in before we tried to parse this token.
+ // Need the commit because rollback doesn't remove marks
+ tokens.rollback();
+ tokens.commit();
+ tokens.rollback();
+ return CommandResult.backtrack();
+ }
+
+ NonInitialCommand<K, V, C> leftCommand = leftCommandIter.next();
+ final int leftBind = leftCommand.leftBinding();
+
+ if (NumberUtils.between(precedence, rightPrec, leftBind)) {
+ tokens.next();
+ tokens.mark();
+
+ try {
+ result = leftCommand.denote(ast, tok, parserContext);
+ rightPrec = leftCommand.nextBinding();
+ } catch (ExpectionNotMet enm) {
+ result = CommandResult.backtrack();
+ }
+
+ switch (result.status) {
+ case SUCCESS:
+ tokens.commit();
+ tokens.commit();
+ ast = result.success();
+ break;
+ case FAIL:
+ return result;
+ case BACKTRACK:
+ tokens.rollback();
+ break;
+ default:
+ throw new IllegalStateException("Unhandled result status " + result.status);
+ }
+ } else {
+ tokens.commit();
+ break outer;
+ }
+ } while (result.status != Status.SUCCESS);
+ }
+
+ return CommandResult.success(ast);
+ }
+
+ /**
+ * Add a non-initial command to this parser.
+ *
+ * @param marker The key that marks the command.
+ *
+ * @param comm The command.
+ */
+ public void addNonInitialCommand(final K marker, final NonInitialCommand<K, V, C> comm) {
+ leftCommands.computeIfAbsent(marker, mrk -> new ArrayList<>()).add(comm);
+ }
+
+ /**
+ * Add a initial command to this parser.
+ *
+ * @param marker The key that marks the command.
+ *
+ * @param comm The command.
+ */
+ public void addInitialCommand(final K marker, final InitialCommand<K, V, C> comm) {
+ nullCommands.computeIfAbsent(marker, mrk -> new ArrayList<>()).add(comm);
+ }
+
+ /**
+ * Add a statement command to this parser.
+ *
+ * The difference between statements and initial commands is that statements can
+ * only appear at the start of the expression.
+ *
+ * @param marker The key that marks the command.
+ *
+ * @param comm The command.
+ */
+ public void addStatementCommand(final K marker, final InitialCommand<K, V, C> comm) {
+ statementCommands.computeIfAbsent(marker, mrk -> new ArrayList<>()).add(comm);
+ }
+
+ /**
+ * Add a dependent non-initial command to this parser.
+ *
+ * @param dependant The dependent that precedes the command.
+ *
+ * @param marker The token key that marks the command.
+ *
+ * @param comm The command.
+ */
+ public void addDependantCommand(final K dependant, final K marker, final NonInitialCommand<K, V, C> comm) {
+ Map<K, List<NonInitialCommand<K, V, C>>> dependantMap = dependantLeftCommands.getOrDefault(dependant,
+ new HashMap<>());
+
+ dependantMap.computeIfAbsent(marker, mrk -> new ArrayList<>()).add(comm);
+ }
+
+ /**
+ * Lookup an initial command.
+ *
+ * @param isStatement Whether to look for statement commands or not.
+ *
+ * @param key The key of the command.
+ *
+ * @param ctx The context for meta-commands.
+ *
+ * @return A command attached to that key, or a default implementation.
+ */
+ public Iterator<InitialCommand<K, V, C>> getInitialCommand(boolean isStatement, K key, ParserContext<K, V, C> ctx) {
+ if (isStatement) {
+ if (metaStatementCommands.containsKey(key)) {
+ List<MetaInitialCommand<K, V, C>> lst = metaStatementCommands.get(key);
+
+ return new TransformIterator<>(lst.iterator(), (itm) -> itm.getCommand(ctx));
+ } else if (statementCommands.containsKey(key)) {
+ return statementCommands.get(key).iterator();
+ }
+ }
+
+ if (metaNullCommands.containsKey(key)) {
+ List<MetaInitialCommand<K, V, C>> lst = metaNullCommands.get(key);
+
+ return new TransformIterator<>(lst.iterator(), (itm) -> itm.getCommand(ctx));
+ }
+
+ return nullCommands.getOrDefault(key, DEFAULT_NULL_LIST).iterator();
+ }
+
+ /**
+ * Lookup a non-initial command.
+ *
+ * @param key The key of the command.
+ *
+ * @param ctx The context for meta-commands.
+ *
+ * @return A command attached to that key, or a default implementation.
+ */
+ public Iterator<NonInitialCommand<K, V, C>> getNonInitialCommand(K key, ParserContext<K, V, C> ctx) {
+ if (dependantMetaLeftCommands.containsKey(ctx.initial)) {
+ Map<K, List<MetaNonInitialCommand<K, V, C>>> dependantCommands = dependantMetaLeftCommands.get(ctx.initial);
+
+ if (dependantCommands.containsKey(key)) {
+ List<MetaNonInitialCommand<K, V, C>> lst = dependantCommands.get(key);
+
+ return new TransformIterator<>(lst.iterator(), (itm) -> itm.getCommand(ctx));
+ }
+ }
+
+ if (dependantLeftCommands.containsKey(ctx.initial)) {
+ Map<K, List<NonInitialCommand<K, V, C>>> dependantCommands = dependantLeftCommands.get(ctx.initial);
+
+ if (dependantCommands.containsKey(key)) {
+ return dependantCommands.getOrDefault(key, DEFAULT_LEFT_LIST).iterator();
+ }
+ }
+
+ if (metaLeftCommands.containsKey(key)) {
+ List<MetaNonInitialCommand<K, V, C>> lst = metaLeftCommands.get(key);
+ return new TransformIterator<>(lst.iterator(), (itm) -> itm.getCommand(ctx));
+ }
+
+ return leftCommands.getOrDefault(key, DEFAULT_LEFT_LIST).iterator();
+ }
+} \ No newline at end of file
diff --git a/JPratt/src/main/java/com/ashardalon/pratt/blocks/ChainParseBlock.java b/JPratt/src/main/java/com/ashardalon/pratt/blocks/ChainParseBlock.java
new file mode 100644
index 0000000..fe04070
--- /dev/null
+++ b/JPratt/src/main/java/com/ashardalon/pratt/blocks/ChainParseBlock.java
@@ -0,0 +1,86 @@
+package com.ashardalon.pratt.blocks;
+
+import java.util.Set;
+
+import com.ashardalon.pratt.ParserContext;
+import com.ashardalon.pratt.commands.CommandResult;
+import com.ashardalon.pratt.commands.CommandResult.Status;
+import com.ashardalon.pratt.tokens.Token;
+
+import bjc.data.Tree;
+import bjc.data.SimpleTree;
+import bjc.utils.parserutils.ParserException;
+
+/**
+ * A {@link ParseBlock} for a series of parse blocks, linked by a set of tokens.
+ *
+ * Roughly analogous to Perl 6s list associative operators.
+ *
+ * @author bjculkin
+ *
+ * @param <K>
+ * The token key type.
+ *
+ * @param <V>
+ * The token value type.
+ *
+ * @param <C>
+ * The parser state type.
+ *
+ */
+public class ChainParseBlock<K, V, C> implements ParseBlock<K, V, C> {
+ private ParseBlock<K, V, C> iner;
+
+ private Set<K> indicators;
+
+ private Token<K, V> trm;
+
+ /**
+ * Create a new chain parser block.
+ *
+ * @param inner
+ * The block for the chains interior.
+ *
+ * @param chainIndicators
+ * The set of markers that indicate continuing the chain
+ *
+ * @param term
+ * The node in the AST for the expression.
+ */
+ public ChainParseBlock(ParseBlock<K, V, C> inner, Set<K> chainIndicators, Token<K, V> term) {
+ iner = inner;
+ indicators = chainIndicators;
+ trm = term;
+ }
+
+ @Override
+ public CommandResult<K, V> parse(ParserContext<K, V, C> ctx) throws ParserException {
+ CommandResult<K,V> resOuter = iner.parse(ctx);
+ if (resOuter.status != Status.SUCCESS) return resOuter;
+
+ Tree<Token<K, V>> expression = resOuter.success();
+ Token<K, V> currentToken = ctx.tokens.current();
+ if(indicators.contains(currentToken.getKey())) {
+ Tree<Token<K, V>> res = new SimpleTree<>(trm);
+ res.addChild(expression);
+
+ while(indicators.contains(currentToken.getKey())) {
+ res.addChild(new SimpleTree<>(currentToken));
+ ctx.tokens.next();
+
+ CommandResult<K,V> resInner = iner.parse(ctx);
+ if (resInner.status != Status.SUCCESS) return resInner;
+
+ Tree<Token<K, V>> innerExpression = resInner.success();
+ res.addChild(innerExpression);
+
+ currentToken = ctx.tokens.current();
+ }
+
+ return CommandResult.success(res);
+ }
+
+ return resOuter;
+ }
+
+}
diff --git a/JPratt/src/main/java/com/ashardalon/pratt/blocks/GrammarParseBlock.java b/JPratt/src/main/java/com/ashardalon/pratt/blocks/GrammarParseBlock.java
new file mode 100644
index 0000000..048e9f6
--- /dev/null
+++ b/JPratt/src/main/java/com/ashardalon/pratt/blocks/GrammarParseBlock.java
@@ -0,0 +1,92 @@
+package com.ashardalon.pratt.blocks;
+
+import java.util.function.Function;
+
+import com.ashardalon.pratt.ParserContext;
+import com.ashardalon.pratt.PrattParser;
+import com.ashardalon.pratt.commands.CommandResult;
+import com.ashardalon.pratt.tokens.Token;
+import com.ashardalon.pratt.tokens.TokenStream;
+
+import bjc.data.Tree;
+import bjc.typeclasses.Isomorphism;
+import bjc.data.Tree;
+import bjc.functypes.*;
+import bjc.utils.parserutils.ParserException;
+
+/**
+ * A {@link ParseBlock} that parses an expression from a 'inner' grammar.
+ *
+ * @author bjculkin
+ *
+ * @param <K> The key type of the outer tokens.
+ *
+ * @param <V> The value type of the outer tokens.
+ *
+ * @param <C> The state type of the outer parser.
+ *
+ * @param <K2> The key type of the inner tokens.
+ *
+ * @param <V2> The value type of the inner tokens.
+ *
+ * @param <C2> The state type of the outer parser.
+ */
+public class GrammarParseBlock<K, V, C, K2, V2, C2> implements ParseBlock<K, V, C> {
+ private final PrattParser<K2, V2, C2> innr;
+
+ private final int prcedence;
+ private final boolean isStatemnt;
+
+ private final Function<TokenStream<K, V>, TokenStream<K2, V2>> tkenTransform;
+ private final Isomorphism<C, C2> stteTransform;
+ private final Function<Tree<Token<K2, V2>>, Tree<Token<K, V>>> xpressionTransform;
+
+ /**
+ * Create a new grammar parser block.
+ *
+ * @param inner The inner grammar to parse.
+ * @param precedence The precedence of the expression to parse.
+ * @param isStatement Is the expression being parsed in statement
+ * context?
+ * @param tokenTransform Function to transform to the new token type.
+ * @param stateTransform Function to toggle between state types.
+ * @param expressionTransform Function to transform back to the normal token
+ * type.
+ */
+ public GrammarParseBlock(final PrattParser<K2, V2, C2> inner, final int precedence, final boolean isStatement,
+ final Function<TokenStream<K, V>, TokenStream<K2, V2>> tokenTransform,
+ final Isomorphism<C, C2> stateTransform,
+ final Function<Tree<Token<K2, V2>>, Tree<Token<K, V>>> expressionTransform) {
+ innr = inner;
+ prcedence = precedence;
+ isStatemnt = isStatement;
+ tkenTransform = tokenTransform;
+ stteTransform = stateTransform;
+ xpressionTransform = expressionTransform;
+ }
+
+ @Override
+ public CommandResult<K, V> parse(final ParserContext<K, V, C> ctx) throws ParserException {
+ final C2 newState = stteTransform.to(ctx.state);
+
+ final TokenStream<K2, V2> newTokens = tkenTransform.apply(ctx.tokens);
+
+ final CommandResult<K2, V2> res = innr.parseExpression(prcedence, newTokens, newState, isStatemnt);
+ switch (res.status) {
+ case SUCCESS:
+ break;
+ case FAIL:
+ return CommandResult.fail();
+ case BACKTRACK:
+ return CommandResult.backtrack();
+ default:
+ throw new IllegalStateException("Unhandled status " + res.status);
+ }
+
+ Tree<Token<K2, V2>> expression = res.success();
+
+ ctx.state = stteTransform.from(newState);
+
+ return CommandResult.success(xpressionTransform.apply(expression));
+ }
+} \ No newline at end of file
diff --git a/JPratt/src/main/java/com/ashardalon/pratt/blocks/ParseBlock.java b/JPratt/src/main/java/com/ashardalon/pratt/blocks/ParseBlock.java
new file mode 100644
index 0000000..51aa7fa
--- /dev/null
+++ b/JPratt/src/main/java/com/ashardalon/pratt/blocks/ParseBlock.java
@@ -0,0 +1,38 @@
+package com.ashardalon.pratt.blocks;
+
+import com.ashardalon.pratt.ParserContext;
+import com.ashardalon.pratt.commands.CommandResult;
+
+import bjc.utils.parserutils.ParserException;
+
+/**
+ * Represents a embedded block in an expression.
+ *
+ * @author bjculkin
+ *
+ * @param <K>
+ * The key type of the token.
+ *
+ * @param <V>
+ * The value type of the token.
+ *
+ * @param <C>
+ * The state type of the parser.
+ */
+@FunctionalInterface
+public interface ParseBlock<K, V, C> {
+
+ /**
+ * Parse the block this represents.
+ *
+ * @param ctx
+ * The context for parsing.
+ *
+ * @return A AST for this block.
+ *
+ * @throws ParserException
+ * If something goes wrong during parsing, or the block fails
+ * validation.
+ */
+ CommandResult<K, V> parse(ParserContext<K, V, C> ctx) throws ParserException;
+} \ No newline at end of file
diff --git a/JPratt/src/main/java/com/ashardalon/pratt/blocks/ParseBlocks.java b/JPratt/src/main/java/com/ashardalon/pratt/blocks/ParseBlocks.java
new file mode 100644
index 0000000..89989fd
--- /dev/null
+++ b/JPratt/src/main/java/com/ashardalon/pratt/blocks/ParseBlocks.java
@@ -0,0 +1,98 @@
+package com.ashardalon.pratt.blocks;
+
+import java.util.function.Predicate;
+import java.util.function.UnaryOperator;
+
+import com.ashardalon.pratt.tokens.Token;
+
+import bjc.data.Tree;
+
+/**
+ * Utility class for creating common implementations of {@link ParseBlock}
+ *
+ * @author bjculkin
+ *
+ */
+public class ParseBlocks {
+ /*
+ * Grammar parse blocks are complex enough to not get a builder method.
+ */
+
+ /**
+ * Create a new repeating parse block.
+ *
+ * @param <K> The key type for the tokens.
+ * @param <V> The value type for the tokens.
+ * @param <C> The context type for the tokens.
+ *
+ * @param inner
+ * The parse block to repeat.
+ *
+ * @param delim
+ * The token type that separates repetitions.
+ *
+ * @param term
+ * The token type that terminates repetitions.
+ *
+ * @param mark
+ * The token to use as the node in the AST.
+ *
+ * @param action
+ * The action to perform on the state after every repetition.
+ *
+ * @return A configured repeating parse block.
+ */
+ public static <K, V, C> ParseBlock<K, V, C> repeating(final ParseBlock<K, V, C> inner, final K delim,
+ final K term, final Token<K, V> mark, final UnaryOperator<C> action) {
+ return new RepeatingParseBlock<>(inner, delim, term, mark, action);
+ }
+
+ /**
+ * Create a new triggered parse block.
+ *
+ * @param <K> The key type for the tokens.
+ * @param <V> The value type for the tokens.
+ * @param <C> The context type for the tokens.
+ *
+ * @param source
+ * The block to trigger around.
+ *
+ * @param onEnter
+ * The action to perform upon the state before entering the
+ * block.
+ *
+ * @param onExit
+ * The action to perform upon the state after exiting the block.
+ *
+ * @return A configured trigger parse block.
+ */
+ public static <K, V, C> ParseBlock<K, V, C> trigger(final ParseBlock<K, V, C> source,
+ final UnaryOperator<C> onEnter, final UnaryOperator<C> onExit) {
+ return new TriggeredParseBlock<>(onEnter, onExit, source);
+ }
+
+ /**
+ * Create a new simple parse block.
+ *
+ * @param <K> The key type for the tokens.
+ * @param <V> The value type for the tokens.
+ * @param <C> The context type for the tokens.
+ *
+ * @param precedence
+ * The precedence of the expression inside the block.
+ *
+ * @param terminator
+ * The key type of the token expected after this block, or null
+ * if none is expected.
+ *
+ * @param validator
+ * The predicate to use to validate parsed expressions, or null
+ * if none is used.
+ *
+ * @return A configured simple parse block.
+ */
+ public static <K, V, C> ParseBlock<K, V, C> simple(final int precedence, final K terminator,
+ final Predicate<Tree<Token<K, V>>> validator) {
+ return new SimpleParseBlock<>(precedence, validator, terminator);
+ }
+} \ No newline at end of file
diff --git a/JPratt/src/main/java/com/ashardalon/pratt/blocks/RepeatingParseBlock.java b/JPratt/src/main/java/com/ashardalon/pratt/blocks/RepeatingParseBlock.java
new file mode 100644
index 0000000..25827b4
--- /dev/null
+++ b/JPratt/src/main/java/com/ashardalon/pratt/blocks/RepeatingParseBlock.java
@@ -0,0 +1,101 @@
+package com.ashardalon.pratt.blocks;
+
+import java.util.function.UnaryOperator;
+
+import com.ashardalon.pratt.ParserContext;
+import com.ashardalon.pratt.commands.CommandResult;
+import com.ashardalon.pratt.commands.CommandResult.Status;
+import com.ashardalon.pratt.tokens.Token;
+
+import bjc.data.Tree;
+import bjc.data.SimpleTree;
+import bjc.utils.parserutils.ParserException;
+
+/**
+ * A parse block that can parse a sequnce of zero or more occurances of another
+ * block.
+ *
+ * @author bjculkin
+ *
+ * @param <K>
+ * The key type of the tokens.
+ *
+ * @param <V>
+ * The value type of the tokens.
+ *
+ * @param <C>
+ * The state type of the parser.
+ */
+public class RepeatingParseBlock<K, V, C> implements ParseBlock<K, V, C> {
+ private final ParseBlock<K, V, C> innerBlock;
+
+ private final K delim;
+ private final K term;
+
+ private final UnaryOperator<C> onDelim;
+
+ private final Token<K, V> mark;
+
+ /**
+ * Create a new repeating block.
+ *
+ * @param inner
+ * The inner block for elements.
+ *
+ * @param delimiter
+ * The token that delimits elements in the sequence.
+ *
+ * @param terminator
+ * The token that terminates the sequence.
+ *
+ * @param marker
+ * The token to use as the node in the AST.
+ *
+ * @param action
+ * The action to apply to the state after every delimiter.
+ */
+ public RepeatingParseBlock(final ParseBlock<K, V, C> inner, final K delimiter, final K terminator,
+ final Token<K, V> marker, final UnaryOperator<C> action) {
+ super();
+
+ if(inner == null)
+ throw new NullPointerException("Inner block must not be null");
+ else if(delimiter == null)
+ throw new NullPointerException("Delimiter must not be null");
+ else if(terminator == null) throw new NullPointerException("Terminator must not be null");
+
+ innerBlock = inner;
+
+ delim = delimiter;
+ term = terminator;
+
+ mark = marker;
+
+ onDelim = action;
+ }
+
+ @Override
+ public CommandResult<K, V> parse(final ParserContext<K, V, C> ctx) throws ParserException {
+ final Tree<Token<K, V>> ret = new SimpleTree<>(mark);
+
+ Token<K, V> tok = ctx.tokens.current();
+
+ while(!tok.getKey().equals(term)) {
+ final CommandResult<K,V> resKid = innerBlock.parse(ctx);
+ if (resKid.status != Status.SUCCESS) return resKid;
+ Tree<Token<K, V>> kid = resKid.success();
+ ret.addChild(kid);
+
+ tok = ctx.tokens.current();
+
+ ctx.tokens.expect(delim, term);
+
+ if(onDelim != null) {
+ ctx.state = onDelim.apply(ctx.state);
+ }
+ }
+
+ return CommandResult.success(ret);
+ }
+
+}
diff --git a/JPratt/src/main/java/com/ashardalon/pratt/blocks/SimpleParseBlock.java b/JPratt/src/main/java/com/ashardalon/pratt/blocks/SimpleParseBlock.java
new file mode 100644
index 0000000..8c4a046
--- /dev/null
+++ b/JPratt/src/main/java/com/ashardalon/pratt/blocks/SimpleParseBlock.java
@@ -0,0 +1,98 @@
+package com.ashardalon.pratt.blocks;
+
+import java.util.function.Predicate;
+
+import com.ashardalon.pratt.ParserContext;
+import com.ashardalon.pratt.commands.CommandResult;
+import com.ashardalon.pratt.commands.CommandResult.Status;
+import com.ashardalon.pratt.tokens.Token;
+
+import bjc.data.Tree;
+import bjc.utils.parserutils.ParserException;
+
+/**
+ * Simple implementation of {@link ParseBlock}
+ *
+ * @author bjculkin
+ *
+ * @param <K>
+ * The key type of the tokens.
+ *
+ * @param <V>
+ * The value type of the tokens.
+ *
+ * @param <C>
+ * The state type of the parser.
+ */
+public class SimpleParseBlock<K, V, C> implements ParseBlock<K, V, C> {
+ private final int pow;
+
+ private final K term;
+
+ private final Predicate<Tree<Token<K, V>>> validatr;
+
+ /**
+ * Create a new block.
+ *
+ * @param precedence
+ * The precedence of this block.
+ * @param validator
+ * The predicate to apply to blocks.
+ * @param terminator
+ * The token type that terminates the block. If this is null,
+ * don't check for a terminator.
+ */
+ public SimpleParseBlock(final int precedence, final Predicate<Tree<Token<K, V>>> validator,
+ final K terminator) {
+ if(precedence < 0) throw new IllegalArgumentException("Precedence must be non-negative");
+
+ pow = precedence;
+ term = terminator;
+ validatr = validator;
+ }
+
+ @Override
+ public CommandResult<K, V> parse(final ParserContext<K, V, C> ctx) throws ParserException {
+ final CommandResult<K,V> resBlock = ctx.parse.parseExpression(pow, ctx.tokens, ctx.state, false);
+ if (resBlock.status != Status.SUCCESS) return resBlock;
+
+ Tree<Token<K, V>> res = resBlock.success();
+ if(term != null) {
+ ctx.tokens.expect(term);
+ }
+
+ if(validatr == null || validatr.test(res)) return CommandResult.success(res);
+
+ // TODO: Figure out the right way to handle error context w/ CommandResult
+ throw new ParserException("Block failed validation");
+ }
+
+ @Override
+ public int hashCode() {
+ final int prime = 31;
+
+ int result = 1;
+
+ result = prime * result + pow;
+ result = prime * result + (term == null ? 0 : term.hashCode());
+
+ return result;
+ }
+
+ @Override
+ public boolean equals(final Object obj) {
+ if(this == obj) return true;
+ if(obj == null) return false;
+ if(!(obj instanceof SimpleParseBlock)) return false;
+
+ final SimpleParseBlock<?, ?, ?> other = (SimpleParseBlock<?, ?, ?>) obj;
+
+ if(pow != other.pow) return false;
+
+ if(term == null) {
+ if(other.term != null) return false;
+ } else if(!term.equals(other.term)) return false;
+
+ return true;
+ }
+} \ No newline at end of file
diff --git a/JPratt/src/main/java/com/ashardalon/pratt/blocks/TriggeredParseBlock.java b/JPratt/src/main/java/com/ashardalon/pratt/blocks/TriggeredParseBlock.java
new file mode 100644
index 0000000..1aeabdb
--- /dev/null
+++ b/JPratt/src/main/java/com/ashardalon/pratt/blocks/TriggeredParseBlock.java
@@ -0,0 +1,63 @@
+package com.ashardalon.pratt.blocks;
+
+import java.util.function.UnaryOperator;
+
+import com.ashardalon.pratt.ParserContext;
+import com.ashardalon.pratt.commands.CommandResult;
+import com.ashardalon.pratt.commands.CommandResult.Status;
+
+import bjc.utils.parserutils.ParserException;
+
+/**
+ * A parse block that can adjust the state before handling its context.
+ *
+ * @author bjculkin
+ *
+ * @param <K>
+ * The key type of the tokens.
+ * @param <V>
+ * The value type of the tokens.
+ * @param <C>
+ * The state type of the parser.
+ */
+public class TriggeredParseBlock<K, V, C> implements ParseBlock<K, V, C> {
+ private final UnaryOperator<C> onEntr;
+ private final UnaryOperator<C> onExt;
+
+ private final ParseBlock<K, V, C> sourc;
+
+ /**
+ * Create a new triggered parse block.
+ *
+ * @param onEnter
+ * The action to fire before parsing the block.
+ *
+ * @param onExit
+ * The action to fire after parsing the block.
+ *
+ * @param source
+ * The block to use for parsing.
+ */
+ public TriggeredParseBlock(final UnaryOperator<C> onEnter, final UnaryOperator<C> onExit,
+ final ParseBlock<K, V, C> source) {
+ onEntr = onEnter;
+ onExt = onExit;
+ sourc = source;
+ }
+
+ @Override
+ public CommandResult<K, V> parse(final ParserContext<K, V, C> ctx) throws ParserException {
+ final C newState = onEntr.apply(ctx.state);
+
+ final ParserContext<K, V, C> newCtx = new ParserContext<>(ctx.tokens, ctx.parse, newState);
+
+ final CommandResult<K,V> res = sourc.parse(newCtx);
+
+ if (res.status != Status.SUCCESS) return res;
+
+ ctx.state = onExt.apply(newState);
+
+ return res;
+ }
+
+}
diff --git a/JPratt/src/main/java/com/ashardalon/pratt/commands/AbstractInitialCommand.java b/JPratt/src/main/java/com/ashardalon/pratt/commands/AbstractInitialCommand.java
new file mode 100644
index 0000000..c38aa9b
--- /dev/null
+++ b/JPratt/src/main/java/com/ashardalon/pratt/commands/AbstractInitialCommand.java
@@ -0,0 +1,45 @@
+package com.ashardalon.pratt.commands;
+
+import com.ashardalon.pratt.ParserContext;
+import com.ashardalon.pratt.tokens.Token;
+
+import bjc.utils.parserutils.ParserException;
+
+/**
+ * Abstract base for initial commands.
+ *
+ * @author bjculkin
+ *
+ * @param <K>
+ * The key type of the tokens.
+ *
+ * @param <V>
+ * The value type of the tokens.
+ *
+ * @param <C>
+ * The state type of the parser.
+ */
+public abstract class AbstractInitialCommand<K, V, C> implements InitialCommand<K, V, C> {
+ @Override
+ public CommandResult<K, V> denote(final Token<K, V> operator,
+ final ParserContext<K, V, C> ctx) throws ParserException {
+ return intNullDenotation(operator, ctx);
+ }
+
+ /**
+ * Internal null denotation method.
+ *
+ * @param operator
+ * The operator that was parsed.
+ * @param ctx
+ * The parser context at this point.
+ *
+ * @return The tree that this method parsed.
+ *
+ * @throws ParserException
+ * If something went wrong while parsing.
+ */
+ protected abstract CommandResult<K, V> intNullDenotation(Token<K, V> operator,
+ ParserContext<K, V, C> ctx) throws ParserException;
+
+} \ No newline at end of file
diff --git a/JPratt/src/main/java/com/ashardalon/pratt/commands/BinaryCommand.java b/JPratt/src/main/java/com/ashardalon/pratt/commands/BinaryCommand.java
new file mode 100644
index 0000000..739cb44
--- /dev/null
+++ b/JPratt/src/main/java/com/ashardalon/pratt/commands/BinaryCommand.java
@@ -0,0 +1,55 @@
+package com.ashardalon.pratt.commands;
+
+import bjc.data.Tree;
+
+import com.ashardalon.pratt.ParserContext;
+import com.ashardalon.pratt.commands.CommandResult.Status;
+import com.ashardalon.pratt.tokens.Token;
+
+import bjc.data.SimpleTree;
+import bjc.utils.parserutils.ParserException;
+
+/**
+ * A binary operator.
+ *
+ * @author bjculkin
+ *
+ * @param <K>
+ * The key type of the tokens.
+ *
+ * @param <V>
+ * The value type of the tokens.
+ *
+ * @param <C>
+ * The state type of the parser.
+ */
+public abstract class BinaryCommand<K, V, C> extends BinaryPostCommand<K, V, C> {
+ /**
+ * Create a new binary operator with the specified precedence.
+ *
+ * @param precedence
+ * The precedence of the operator.
+ */
+ public BinaryCommand(final int precedence) {
+ super(precedence);
+ }
+
+ /**
+ * The right-binding power (right-precedence) of this command.
+ *
+ * @return The right binding power of this command.
+ */
+ protected abstract int rightBinding();
+
+ @Override
+ public CommandResult<K, V> denote(final Tree<Token<K, V>> operand,
+ final Token<K, V> operator, final ParserContext<K, V, C> ctx)
+ throws ParserException {
+ final CommandResult<K,V> opr
+ = ctx.parse.parseExpression(rightBinding(), ctx.tokens, ctx.state, false);
+
+ if (opr.status != Status.SUCCESS) return opr;
+
+ return CommandResult.success(new SimpleTree<>(operator, operand, opr.success()));
+ }
+} \ No newline at end of file
diff --git a/JPratt/src/main/java/com/ashardalon/pratt/commands/BinaryPostCommand.java b/JPratt/src/main/java/com/ashardalon/pratt/commands/BinaryPostCommand.java
new file mode 100644
index 0000000..bacab23
--- /dev/null
+++ b/JPratt/src/main/java/com/ashardalon/pratt/commands/BinaryPostCommand.java
@@ -0,0 +1,36 @@
+package com.ashardalon.pratt.commands;
+
+/**
+ * A operator with fixed precedence.
+ *
+ * @author bjculkin
+ *
+ * @param <K>
+ * The key type of the tokens.
+ *
+ * @param <V>
+ * The value type of the tokens.
+ *
+ * @param <C>
+ * The state type of the parser.
+ */
+public abstract class BinaryPostCommand<K, V, C> extends NonInitialCommand<K, V, C> {
+ private final int leftPower;
+
+ /**
+ * Create a new operator with fixed precedence.
+ *
+ * @param precedence
+ * The precedence of the operator.
+ */
+ public BinaryPostCommand(final int precedence) {
+ if(precedence < 0) throw new IllegalArgumentException("Precedence must be non-negative");
+
+ leftPower = precedence;
+ }
+
+ @Override
+ public int leftBinding() {
+ return leftPower;
+ }
+} \ No newline at end of file
diff --git a/JPratt/src/main/java/com/ashardalon/pratt/commands/BranchInitialCommand.java b/JPratt/src/main/java/com/ashardalon/pratt/commands/BranchInitialCommand.java
new file mode 100644
index 0000000..0e17287
--- /dev/null
+++ b/JPratt/src/main/java/com/ashardalon/pratt/commands/BranchInitialCommand.java
@@ -0,0 +1,39 @@
+package com.ashardalon.pratt.commands;
+
+import java.util.Map;
+
+import com.ashardalon.pratt.ParserContext;
+import com.ashardalon.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/com/ashardalon/pratt/commands/CommandResult.java b/JPratt/src/main/java/com/ashardalon/pratt/commands/CommandResult.java
new file mode 100644
index 0000000..45c1788
--- /dev/null
+++ b/JPratt/src/main/java/com/ashardalon/pratt/commands/CommandResult.java
@@ -0,0 +1,113 @@
+package com.ashardalon.pratt.commands;
+
+import com.ashardalon.pratt.tokens.Token;
+
+import bjc.data.Tree;
+
+/**
+ * Represents the result of executing a command.
+ *
+ * @author bjcul
+ *
+ * @param <K> The key type of the tokens
+ * @param <V> The value type of the tokens
+ */
+public class CommandResult<K, V> {
+ /**
+ * Represents the status of a command execution
+ *
+ * @author bjcul
+ *
+ */
+ public static enum Status {
+ /**
+ * The command successfully parsed.
+ */
+ SUCCESS,
+ /**
+ * The command failed, in a non-recoverable way
+ */
+ FAIL,
+ /**
+ * The command failed. Attempt recovery via backtracking
+ */
+ BACKTRACK
+ }
+
+ /**
+ * The status of this command.
+ */
+ public final Status status;
+
+ private Tree<Token<K, V>> success;
+
+ private CommandResult(Status status) {
+ this.status = status;
+ }
+
+ /**
+ * Get the success value of this command, or null if it failed.
+ *
+ * @return The success value of the command
+ */
+ public Tree<Token<K, V>> success() {
+ return success;
+ }
+
+ /**
+ * Create a success result
+ *
+ * @param <K2> The key type of the token
+ * @param <V2> The value type of the token
+ *
+ * @param succ The tree produced by the command
+ *
+ * @return A command result representing a success
+ */
+ public static <K2, V2> CommandResult<K2, V2> success(Tree<Token<K2, V2>> succ) {
+ CommandResult<K2, V2> result = new CommandResult<>(Status.SUCCESS);
+ result.success = succ;
+ return result;
+ }
+
+ /**
+ * Create a non-backtracking failure result.
+ *
+ * @param <K2> The key type of the token
+ * @param <V2> The value type of the token
+ *
+ * @return A command result representing a non-backtracking fail
+ */
+ public static <K2, V2> CommandResult<K2, V2> fail() {
+ CommandResult<K2, V2> result = new CommandResult<>(Status.FAIL);
+ return result;
+ }
+
+ /**
+ * Create a backtracking failure result.
+ *
+ * @param <K2> The key type of the token
+ * @param <V2> The value type of the token
+ *
+ * @return A command result representing a backtracking fail
+ */
+ public static <K2, V2> CommandResult<K2, V2> backtrack() {
+ 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/com/ashardalon/pratt/commands/InitialCommand.java b/JPratt/src/main/java/com/ashardalon/pratt/commands/InitialCommand.java
new file mode 100644
index 0000000..3b613a3
--- /dev/null
+++ b/JPratt/src/main/java/com/ashardalon/pratt/commands/InitialCommand.java
@@ -0,0 +1,40 @@
+package com.ashardalon.pratt.commands;
+
+import com.ashardalon.pratt.ParserContext;
+import com.ashardalon.pratt.tokens.Token;
+
+import bjc.utils.parserutils.ParserException;
+
+/**
+ * Represents an initial command in parsing.
+ *
+ * @author EVE
+ *
+ * @param <K>
+ * The key type for the tokens.
+ *
+ * @param <V>
+ * The value type for the tokens.
+ *
+ * @param <C>
+ * The state type of the parser.
+ *
+ *
+ */
+@FunctionalInterface
+public interface InitialCommand<K, V, C> {
+ /**
+ * Construct the null denotation of this command.
+ *
+ * @param operator
+ * The operator for this command.
+ * @param ctx
+ * The context for the command.
+ *
+ * @return The result of executing the command.
+ *
+ * @throws ParserException
+ * If something goes wrong during parsing.
+ */
+ CommandResult<K, V> denote(Token<K, V> operator, ParserContext<K, V, C> ctx) throws ParserException;
+}
diff --git a/JPratt/src/main/java/com/ashardalon/pratt/commands/MetaInitialCommand.java b/JPratt/src/main/java/com/ashardalon/pratt/commands/MetaInitialCommand.java
new file mode 100644
index 0000000..7b9aae0
--- /dev/null
+++ b/JPratt/src/main/java/com/ashardalon/pratt/commands/MetaInitialCommand.java
@@ -0,0 +1,27 @@
+package com.ashardalon.pratt.commands;
+
+import com.ashardalon.pratt.ParserContext;
+
+/**
+ * A 'meta-command' that yields the actual command to use.
+ *
+ * @author bjculkin
+ *
+ * @param <K>
+ * The key type of the context.
+ * @param <V>
+ * The value type of the context.
+ * @param <C>
+ * The storage type of the context.
+ *
+ */
+public interface MetaInitialCommand<K, V, C> {
+ /**
+ * Get the command to use.
+ *
+ * @param ctx
+ * The current parser context.
+ * @return The command to use.
+ */
+ InitialCommand<K, V, C> getCommand(ParserContext<K, V, C> ctx);
+}
diff --git a/JPratt/src/main/java/com/ashardalon/pratt/commands/MetaNonInitialCommand.java b/JPratt/src/main/java/com/ashardalon/pratt/commands/MetaNonInitialCommand.java
new file mode 100644
index 0000000..4aa5120
--- /dev/null
+++ b/JPratt/src/main/java/com/ashardalon/pratt/commands/MetaNonInitialCommand.java
@@ -0,0 +1,28 @@
+package com.ashardalon.pratt.commands;
+
+import com.ashardalon.pratt.ParserContext;
+
+/**
+ * A 'meta-command' for non-initial commands.
+ *
+ * @author bjculkin
+ *
+ * @param <K>
+ * The token key type.
+ *
+ * @param <V>
+ * The token value type.
+ *
+ * @param <C>
+ * The parser state type.
+ */
+public interface MetaNonInitialCommand<K, V, C> {
+ /**
+ * Get the command to use.
+ *
+ * @param ctx
+ * The context to use.
+ * @return The command to use.
+ */
+ NonInitialCommand<K, V, C> getCommand(ParserContext<K, V, C> ctx);
+}
diff --git a/JPratt/src/main/java/com/ashardalon/pratt/commands/NonInitialCommand.java b/JPratt/src/main/java/com/ashardalon/pratt/commands/NonInitialCommand.java
new file mode 100644
index 0000000..a521e5c
--- /dev/null
+++ b/JPratt/src/main/java/com/ashardalon/pratt/commands/NonInitialCommand.java
@@ -0,0 +1,66 @@
+package com.ashardalon.pratt.commands;
+
+import com.ashardalon.pratt.ParserContext;
+import com.ashardalon.pratt.tokens.Token;
+
+import bjc.data.Tree;
+import bjc.utils.parserutils.ParserException;
+
+/**
+ * Represents a non-initial command in parsing.
+ *
+ * @author EVE
+ *
+ * @param <K>
+ * The key type for the tokens.
+ *
+ * @param <V>
+ * The value type for the tokens.
+ *
+ * @param <C>
+ * The state type of the parser.
+ *
+ */
+public abstract class NonInitialCommand<K, V, C> {
+ /**
+ * Construct the left denotation of this command.
+ *
+ * @param operand
+ * The left-hand operand of this command.
+ * @param operator
+ * The operator for this command.
+ *
+ * @param ctx
+ * The state needed for commands.
+ *
+ * @return The result of executing the command.
+ *
+ * @throws ParserException
+ * If something went wrong during parsing.
+ */
+ public abstract CommandResult<K, V> denote(Tree<Token<K, V>> operand, Token<K, V> operator,
+ ParserContext<K, V, C> ctx) throws ParserException;
+
+ /**
+ * Get the left-binding power of this command.
+ *
+ * This represents the general precedence of this command.
+ *
+ * @return The left-binding power of this command.
+ */
+ public abstract int leftBinding();
+
+ /**
+ * Get the next-binding power of this command.
+ *
+ * This represents the highest precedence of command this command can be
+ * the left operand of.
+ *
+ * This is the same as the left-binding power by default.
+ *
+ * @return The next-binding power of this command.
+ */
+ public int nextBinding() {
+ return leftBinding();
+ }
+}
diff --git a/JPratt/src/main/java/com/ashardalon/pratt/commands/impls/BlockInitialCommand.java b/JPratt/src/main/java/com/ashardalon/pratt/commands/impls/BlockInitialCommand.java
new file mode 100644
index 0000000..612167a
--- /dev/null
+++ b/JPratt/src/main/java/com/ashardalon/pratt/commands/impls/BlockInitialCommand.java
@@ -0,0 +1,43 @@
+package com.ashardalon.pratt.commands.impls;
+
+import com.ashardalon.pratt.ParserContext;
+import com.ashardalon.pratt.blocks.ParseBlock;
+import com.ashardalon.pratt.commands.AbstractInitialCommand;
+import com.ashardalon.pratt.commands.CommandResult;
+import com.ashardalon.pratt.tokens.Token;
+
+import bjc.utils.parserutils.ParserException;
+
+/**
+ * An initial command that delegates all the work to a {@link ParseBlock}
+ *
+ * @author bjculkin
+ * @param <K>
+ * The token key type.
+ *
+ * @param <V>
+ * The token value type.
+ *
+ * @param <C>
+ * The parser state type.
+ *
+ */
+public class BlockInitialCommand<K, V, C> extends AbstractInitialCommand<K, V, C> {
+ private final ParseBlock<K, V, C> blck;
+
+ /**
+ * Create a new block initial command.
+ *
+ * @param block
+ * The block to delegate to.
+ */
+ public BlockInitialCommand(final ParseBlock<K, V, C> block) {
+ blck = block;
+ }
+
+ @Override
+ protected CommandResult<K, V> intNullDenotation(final Token<K, V> operator, final ParserContext<K, V, C> ctx)
+ throws ParserException {
+ return blck.parse(ctx);
+ }
+} \ No newline at end of file
diff --git a/JPratt/src/main/java/com/ashardalon/pratt/commands/impls/ChainCommand.java b/JPratt/src/main/java/com/ashardalon/pratt/commands/impls/ChainCommand.java
new file mode 100644
index 0000000..5ba84ef
--- /dev/null
+++ b/JPratt/src/main/java/com/ashardalon/pratt/commands/impls/ChainCommand.java
@@ -0,0 +1,83 @@
+package com.ashardalon.pratt.commands.impls;
+
+import java.util.Set;
+
+import com.ashardalon.pratt.ParserContext;
+import com.ashardalon.pratt.commands.BinaryPostCommand;
+import com.ashardalon.pratt.commands.CommandResult;
+import com.ashardalon.pratt.commands.CommandResult.Status;
+import com.ashardalon.pratt.tokens.Token;
+
+import bjc.data.Tree;
+import bjc.data.SimpleTree;
+import bjc.utils.parserutils.ParserException;
+
+/**
+ * Create a new chained operator.
+ *
+ * @author bjculkin
+ *
+ * @param <K>
+ * The key type of the tokens.
+ *
+ * @param <V>
+ * The value type of the tokens.
+ *
+ * @param <C>
+ * The state type of the parser.
+ */
+public class ChainCommand<K, V, C> extends BinaryPostCommand<K, V, C> {
+ private final Set<K> chainWith;
+
+ private final Token<K, V> chain;
+
+ /**
+ * Create a new chained operator.
+ *
+ * @param precedence
+ * The precedence of this operator.
+ *
+ * @param chainSet
+ * The operators to chain with.
+ *
+ * @param chainMarker
+ * The token to use as the node in the AST.
+ */
+ public ChainCommand(final int precedence, final Set<K> chainSet, final Token<K, V> chainMarker) {
+ super(precedence);
+
+ chainWith = chainSet;
+ chain = chainMarker;
+ }
+
+ @Override
+ public CommandResult<K, V> denote(final Tree<Token<K, V>> operand, final Token<K, V> operator,
+ final ParserContext<K, V, C> ctx) throws ParserException {
+ CommandResult<K, V> resOuter = ctx.parse.parseExpression(1 + leftBinding(), ctx.tokens, ctx.state,
+ false);
+ if (resOuter.status != Status.SUCCESS) return resOuter;
+ final Tree<Token<K, V>> tree = resOuter.success();
+
+ final Tree<Token<K, V>> res = new SimpleTree<>(operator, operand, tree);
+
+ if(chainWith.contains(ctx.tokens.current().getKey())) {
+ final Token<K, V> tok = ctx.tokens.current();
+ ctx.tokens.next();
+
+ CommandResult<K, V> resOther = denote(tree, tok,
+ new ParserContext<>(ctx.tokens, ctx.parse, ctx.state));
+ if (resOther.status != Status.SUCCESS) return resOther;
+
+ final Tree<Token<K, V>> other = resOther.success();
+
+ return CommandResult.success(new SimpleTree<>(chain, res, other));
+ }
+
+ return CommandResult.success(res);
+ }
+
+ @Override
+ public int nextBinding() {
+ return leftBinding() - 1;
+ }
+} \ No newline at end of file
diff --git a/JPratt/src/main/java/com/ashardalon/pratt/commands/impls/ConstantCommand.java b/JPratt/src/main/java/com/ashardalon/pratt/commands/impls/ConstantCommand.java
new file mode 100644
index 0000000..dd80205
--- /dev/null
+++ b/JPratt/src/main/java/com/ashardalon/pratt/commands/impls/ConstantCommand.java
@@ -0,0 +1,43 @@
+package com.ashardalon.pratt.commands.impls;
+
+import com.ashardalon.pratt.ParserContext;
+import com.ashardalon.pratt.commands.CommandResult;
+import com.ashardalon.pratt.commands.InitialCommand;
+import com.ashardalon.pratt.tokens.Token;
+
+import bjc.data.Tree;
+import bjc.utils.parserutils.ParserException;
+
+/**
+ * A command that represents a specific tree.
+ *
+ * @author bjculkin
+ *
+ * @param <K>
+ * The key type of the tokens.
+ *
+ * @param <V>
+ * The value type of the tokens.
+ *
+ * @param <C>
+ * The state type of the parser.
+ */
+public class ConstantCommand<K, V, C> implements InitialCommand<K, V, C> {
+ private final Tree<Token<K, V>> val;
+
+ /**
+ * Create a new constant.
+ *
+ * @param con
+ * The tree this constant represents.
+ */
+ public ConstantCommand(final Tree<Token<K, V>> con) {
+ val = con;
+ }
+
+ @Override
+ public CommandResult<K, V> denote(final Token<K, V> operator, final ParserContext<K, V, C> ctx)
+ throws ParserException {
+ return CommandResult.success(val);
+ }
+} \ No newline at end of file
diff --git a/JPratt/src/main/java/com/ashardalon/pratt/commands/impls/DefaultInitialCommand.java b/JPratt/src/main/java/com/ashardalon/pratt/commands/impls/DefaultInitialCommand.java
new file mode 100644
index 0000000..1c218c9
--- /dev/null
+++ b/JPratt/src/main/java/com/ashardalon/pratt/commands/impls/DefaultInitialCommand.java
@@ -0,0 +1,30 @@
+package com.ashardalon.pratt.commands.impls;
+
+import com.ashardalon.pratt.ParserContext;
+import com.ashardalon.pratt.commands.CommandResult;
+import com.ashardalon.pratt.commands.InitialCommand;
+import com.ashardalon.pratt.tokens.Token;
+
+import bjc.utils.parserutils.ParserException;
+
+/**
+ * Default implementation of an initial command.
+ *
+ * @author EVE
+ *
+ * @param <K>
+ * The key type of the token.
+ *
+ * @param <V>
+ * The value type of the token.
+ *
+ * @param <C>
+ * The state type of the parser.
+ */
+public class DefaultInitialCommand<K, V, C> implements InitialCommand<K, V, C> {
+ @Override
+ public CommandResult<K, V> denote(final Token<K, V> operator, final ParserContext<K, V, C> ctx)
+ throws ParserException {
+ throw new ParserException("Unexpected token " + operator);
+ }
+}
diff --git a/JPratt/src/main/java/com/ashardalon/pratt/commands/impls/DefaultNonInitialCommand.java b/JPratt/src/main/java/com/ashardalon/pratt/commands/impls/DefaultNonInitialCommand.java
new file mode 100644
index 0000000..8666713
--- /dev/null
+++ b/JPratt/src/main/java/com/ashardalon/pratt/commands/impls/DefaultNonInitialCommand.java
@@ -0,0 +1,35 @@
+package com.ashardalon.pratt.commands.impls;
+
+import com.ashardalon.pratt.ParserContext;
+import com.ashardalon.pratt.commands.CommandResult;
+import com.ashardalon.pratt.commands.NonInitialCommand;
+import com.ashardalon.pratt.tokens.Token;
+
+import bjc.data.Tree;
+
+/**
+ * Default implementation of a non-initial command.
+ *
+ * @author EVE
+ *
+ * @param <K>
+ * The key type of the tokens.
+ *
+ * @param <V>
+ * The value type of the tokens.
+ *
+ * @param <C>
+ * The state type of the parser.
+ */
+public class DefaultNonInitialCommand<K, V, C> extends NonInitialCommand<K, V, C> {
+ @Override
+ public CommandResult<K, V> denote(final Tree<Token<K, V>> operand, final Token<K, V> operator,
+ final ParserContext<K, V, C> ctx) {
+ throw new UnsupportedOperationException("Default command has no left denotation");
+ }
+
+ @Override
+ public int leftBinding() {
+ return -1;
+ }
+}
diff --git a/JPratt/src/main/java/com/ashardalon/pratt/commands/impls/DenestingCommand.java b/JPratt/src/main/java/com/ashardalon/pratt/commands/impls/DenestingCommand.java
new file mode 100644
index 0000000..438101e
--- /dev/null
+++ b/JPratt/src/main/java/com/ashardalon/pratt/commands/impls/DenestingCommand.java
@@ -0,0 +1,50 @@
+package com.ashardalon.pratt.commands.impls;
+
+import com.ashardalon.pratt.ParserContext;
+import com.ashardalon.pratt.commands.AbstractInitialCommand;
+import com.ashardalon.pratt.commands.CommandResult;
+import com.ashardalon.pratt.commands.InitialCommand;
+import com.ashardalon.pratt.commands.CommandResult.Status;
+import com.ashardalon.pratt.tokens.Token;
+
+import bjc.utils.parserutils.ParserException;
+
+/**
+ * A command that denests a input tree.
+ *
+ * Useful for processing the result of passing a complex parse group to a
+ * command.
+ *
+ * @author bjculkin
+ *
+ * @param <K>
+ * The key type of the tokens.
+ *
+ * @param <V>
+ * The value type of the tokens.
+ *
+ * @param <C>
+ * The state type of the parser.
+ *
+ */
+public class DenestingCommand<K, V, C> extends AbstractInitialCommand<K, V, C> {
+ private final InitialCommand<K, V, C> wrapped;
+
+ /**
+ * Create a new transforming initial command.
+ *
+ * @param internal
+ * The initial command to delegate to.
+ */
+ public DenestingCommand(final InitialCommand<K, V, C> internal) {
+ wrapped = internal;
+ }
+
+ @Override
+ protected CommandResult<K, V> intNullDenotation(final Token<K, V> operator, final ParserContext<K, V, C> ctx)
+ throws ParserException {
+ CommandResult<K, V> res = wrapped.denote(operator, ctx);
+ if (res.status != Status.SUCCESS) return res;
+ return CommandResult.success(res.success().getChild(0));
+ }
+} \ No newline at end of file
diff --git a/JPratt/src/main/java/com/ashardalon/pratt/commands/impls/GroupingCommand.java b/JPratt/src/main/java/com/ashardalon/pratt/commands/impls/GroupingCommand.java
new file mode 100644
index 0000000..284c1e9
--- /dev/null
+++ b/JPratt/src/main/java/com/ashardalon/pratt/commands/impls/GroupingCommand.java
@@ -0,0 +1,55 @@
+package com.ashardalon.pratt.commands.impls;
+
+import bjc.data.Tree;
+
+import com.ashardalon.pratt.ParserContext;
+import com.ashardalon.pratt.blocks.ParseBlock;
+import com.ashardalon.pratt.commands.AbstractInitialCommand;
+import com.ashardalon.pratt.commands.CommandResult;
+import com.ashardalon.pratt.tokens.Token;
+
+import bjc.data.SimpleTree;
+import bjc.utils.parserutils.ParserException;
+
+/**
+ * A grouping operator.
+ *
+ * @author bjculkin
+ *
+ * @param <K>
+ * The key type of the tokens.
+ *
+ * @param <V>
+ * The value type of the tokens.
+ *
+ * @param <C>
+ * The state type of the parser.
+ */
+public class GroupingCommand<K, V, C> extends AbstractInitialCommand<K, V, C> {
+ private final ParseBlock<K, V, C> innerBlock;
+
+ private final Token<K, V> mark;
+
+ /**
+ * Create a new grouping command.
+ *
+ * @param inner
+ * The inner block.
+ *
+ * @param marker
+ * The token to use as the node in the AST.
+ */
+ public GroupingCommand(final ParseBlock<K, V, C> inner, final Token<K, V> marker) {
+ innerBlock = inner;
+
+ mark = marker;
+ }
+
+ @Override
+ protected CommandResult<K, V> intNullDenotation(final Token<K, V> operator, final ParserContext<K, V, C> ctx)
+ throws ParserException {
+ final CommandResult<K,V> resOpr = innerBlock.parse(ctx);
+ Tree<Token<K, V>> opr = resOpr.success();
+ return CommandResult.success(new SimpleTree<>(mark, opr));
+ }
+} \ No newline at end of file
diff --git a/JPratt/src/main/java/com/ashardalon/pratt/commands/impls/InitialCommands.java b/JPratt/src/main/java/com/ashardalon/pratt/commands/impls/InitialCommands.java
new file mode 100644
index 0000000..f4b0bbe
--- /dev/null
+++ b/JPratt/src/main/java/com/ashardalon/pratt/commands/impls/InitialCommands.java
@@ -0,0 +1,242 @@
+package com.ashardalon.pratt.commands.impls;
+
+import static com.ashardalon.pratt.blocks.ParseBlocks.repeating;
+import static com.ashardalon.pratt.blocks.ParseBlocks.simple;
+import static com.ashardalon.pratt.blocks.ParseBlocks.trigger;
+
+import java.util.function.UnaryOperator;
+
+import com.ashardalon.pratt.blocks.ParseBlock;
+import com.ashardalon.pratt.commands.*;
+import com.ashardalon.pratt.tokens.Token;
+
+import bjc.data.Tree;
+import bjc.functypes.MapBuilder;
+
+/**
+ * * Contains factory methods for producing common implementations of
+ * {@link InitialCommand}
+ *
+ * @author EVE
+ *
+ */
+public class InitialCommands {
+ /**
+ * Create a new unary operator.
+ *
+ * @param <K> The key type for the tokens.
+ * @param <V> The value type for the tokens.
+ * @param <C> The context type for the tokens.
+ *
+ * @param precedence
+ * The precedence of the operator.
+ *
+ * @return A command implementing that operator.
+ */
+ public static <K, V, C> InitialCommand<K, V, C> unary(final int precedence) {
+ return new UnaryCommand<>(precedence);
+ }
+
+ /**
+ * Create a new grouping operator.
+ *
+ * @param <K> The key type for the tokens.
+ * @param <V> The value type for the tokens.
+ * @param <C> The context type for the tokens.
+ *
+ * @param precedence
+ * The precedence of the expression in the operator.
+ *
+ * @param term
+ * The type that closes the group.
+ *
+ * @param mark
+ * The token for the AST node of the group.
+ *
+ * @return A command implementing the operator.
+ */
+ public static <K, V, C> InitialCommand<K, V, C> grouping(final int precedence, final K term,
+ final Token<K, V> mark) {
+ final ParseBlock<K, V, C> innerBlock = simple(precedence, term, null);
+
+ return new GroupingCommand<>(innerBlock, mark);
+ }
+
+ /**
+ * Create a new leaf operator.
+ *
+ * @param <K> The key type for the tokens.
+ * @param <V> The value type for the tokens.
+ * @param <C> The context type for the tokens.
+ *
+ * @return A command implementing the operator.
+ */
+ public static <K, V, C> InitialCommand<K, V, C> leaf() {
+ return new LeafCommand<>();
+ }
+
+ /**
+ * Create a new pre-ternary operator, like an if-then-else statement.
+ *
+ * @param <K> The key type for the tokens.
+ * @param <V> The value type for the tokens.
+ * @param <C> The context type for the tokens.
+ *
+ * @param cond1
+ * The priority of the first block.
+ *
+ * @param block1
+ * The priority of the second block.
+ *
+ * @param block2
+ * The priority of the third block.
+ *
+ * @param mark1
+ * The marker that ends the first block.
+ *
+ * @param mark2
+ * The marker that ends the second block.
+ *
+ * @param term
+ * The token for the AST node of the group.
+ *
+ * @return A command implementing the operator.
+ */
+ public static <K, V, C> InitialCommand<K, V, C> preTernary(final int cond1, final int block1, final int block2,
+ final K mark1, final K mark2, final Token<K, V> term) {
+ final ParseBlock<K, V, C> condBlock = simple(cond1, mark1, null);
+ final ParseBlock<K, V, C> opblock1 = simple(block1, mark2, null);
+ final ParseBlock<K, V, C> opblock2 = simple(block2, null, null);
+
+ return new PreTernaryCommand<>(condBlock, opblock1, opblock2, term);
+ }
+
+ /**
+ * Create a new named constant.
+ *
+ * @param <K> The key type for the tokens.
+ * @param <V> The value type for the tokens.
+ * @param <C> The context type for the tokens.
+ *
+ * @param val
+ * The value of the constant.
+ *
+ * @return A command implementing the constant.
+ */
+ public static <K, V, C> InitialCommand<K, V, C> constant(final Tree<Token<K, V>> val) {
+ return new ConstantCommand<>(val);
+ }
+
+ /**
+ * Create a new delimited command. This is for block-like constructs.
+ *
+ * @param <K> The key type for the tokens.
+ * @param <V> The value type for the tokens.
+ * @param <C> The context type for the tokens.
+ *
+ * @param inner
+ * The precedence of the inner blocks.
+ *
+ * @param delim
+ * The marker between sub-blocks.
+ *
+ * @param mark
+ * The block terminator.
+ *
+ * @param term
+ * The token for the AST node of the group.
+ *
+ * @param onEnter
+ * The function to apply to the state on entering the block.
+ *
+ * @param onDelim
+ * The function to apply to the state on finishing a sub-block.
+ *
+ * @param onExit
+ * The function to apply to the state on exiting the block.
+ *
+ * @param statement
+ * Whether or not the sub-blocks are statements or expressions.
+ *
+ * @return A command implementing the operator.
+ */
+ public static <K, V, C> InitialCommand<K, V, C> delimited(final int inner, final K delim, final K mark,
+ final Token<K, V> term, final UnaryOperator<C> onEnter, final UnaryOperator<C> onDelim,
+ final UnaryOperator<C> onExit, final boolean statement) {
+ final ParseBlock<K, V, C> innerBlock = simple(inner, null, null);
+ final ParseBlock<K, V, C> delimsBlock = repeating(innerBlock, delim, mark, term, onDelim);
+ final ParseBlock<K, V, C> scopedBlock = trigger(delimsBlock, onEnter, onExit);
+
+ final GroupingCommand<K, V, C> command = new GroupingCommand<>(scopedBlock, term);
+
+ /*
+ * Remove the wrapper layer from grouping-command on top of
+ * RepeatingParseBlock.
+ */
+ return denest(command);
+ }
+
+ /**
+ * Create a new denesting command.
+ *
+ * This removes one tree-level, and is useful when combining complex
+ * parse blocks with commands.
+ *
+ * @param <K> The key type for the tokens.
+ * @param <V> The value type for the tokens.
+ * @param <C> The context type for the tokens.
+ *
+ * @param comm
+ * The command to denest.
+ *
+ * @return A command that denests the result of the provided command.
+ */
+ public static <K, V, C> InitialCommand<K, V, C> denest(final InitialCommand<K, V, C> comm) {
+ return new DenestingCommand<>(comm);
+ }
+
+ /**
+ * Create a new 'panfix' command.
+ *
+ * Form is <term> <expr> <term> <expr> <term>
+ *
+ * @param <K> The key type for the tokens.
+ * @param <V> The value type for the tokens.
+ * @param <C> The context type for the tokens.
+ *
+ * @param precedence The precedence for this operator
+ * @param term The indicator for the operator
+ * @param marker The token to mark this tree
+ *
+ * @return A command that implements a panfix operator
+ */
+ 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/com/ashardalon/pratt/commands/impls/LeafCommand.java b/JPratt/src/main/java/com/ashardalon/pratt/commands/impls/LeafCommand.java
new file mode 100644
index 0000000..886a377
--- /dev/null
+++ b/JPratt/src/main/java/com/ashardalon/pratt/commands/impls/LeafCommand.java
@@ -0,0 +1,31 @@
+package com.ashardalon.pratt.commands.impls;
+
+import com.ashardalon.pratt.ParserContext;
+import com.ashardalon.pratt.commands.CommandResult;
+import com.ashardalon.pratt.commands.InitialCommand;
+import com.ashardalon.pratt.tokens.Token;
+
+import bjc.data.SimpleTree;
+import bjc.utils.parserutils.ParserException;
+
+/**
+ * A operator that stands for itself.
+ *
+ * @author bjculkin
+ *
+ * @param <K>
+ * The key type of the tokens.
+ *
+ * @param <V>
+ * The value type of the tokens.
+ *
+ * @param <C>
+ * The state type of the parser.
+ */
+public class LeafCommand<K, V, C> implements InitialCommand<K, V, C> {
+ @Override
+ public CommandResult<K, V> denote(final Token<K, V> operator, final ParserContext<K, V, C> ctx)
+ throws ParserException {
+ return CommandResult.success(new SimpleTree<>(operator));
+ }
+} \ No newline at end of file
diff --git a/JPratt/src/main/java/com/ashardalon/pratt/commands/impls/LeftBinaryCommand.java b/JPratt/src/main/java/com/ashardalon/pratt/commands/impls/LeftBinaryCommand.java
new file mode 100644
index 0000000..c5408d6
--- /dev/null
+++ b/JPratt/src/main/java/com/ashardalon/pratt/commands/impls/LeftBinaryCommand.java
@@ -0,0 +1,34 @@
+package com.ashardalon.pratt.commands.impls;
+
+import com.ashardalon.pratt.commands.BinaryCommand;
+
+/**
+ * A left-associative operator.
+ *
+ * @author bjculkin
+ *
+ * @param <K>
+ * The key type of the tokens.
+ *
+ * @param <V>
+ * The value type of the tokens.
+ *
+ * @param <C>
+ * The state type of the parser.
+ */
+public class LeftBinaryCommand<K, V, C> extends BinaryCommand<K, V, C> {
+ /**
+ * Create a new left-associative operator.
+ *
+ * @param precedence
+ * The precedence of the operator.
+ */
+ public LeftBinaryCommand(final int precedence) {
+ super(precedence);
+ }
+
+ @Override
+ protected int rightBinding() {
+ return 1 + leftBinding();
+ }
+} \ No newline at end of file
diff --git a/JPratt/src/main/java/com/ashardalon/pratt/commands/impls/NonBinaryCommand.java b/JPratt/src/main/java/com/ashardalon/pratt/commands/impls/NonBinaryCommand.java
new file mode 100644
index 0000000..aca3784
--- /dev/null
+++ b/JPratt/src/main/java/com/ashardalon/pratt/commands/impls/NonBinaryCommand.java
@@ -0,0 +1,39 @@
+package com.ashardalon.pratt.commands.impls;
+
+import com.ashardalon.pratt.commands.BinaryCommand;
+
+/**
+ * A non-associative operator.
+ *
+ * @author bjculkin
+ *
+ * @param <K>
+ * The key type of the tokens.
+ *
+ * @param <V>
+ * The value type of the tokens.
+ *
+ * @param <C>
+ * The state type of the parser.
+ */
+public class NonBinaryCommand<K, V, C> extends BinaryCommand<K, V, C> {
+ /**
+ * Create a new non-associative operator.
+ *
+ * @param precedence
+ * The precedence of the operator.
+ */
+ public NonBinaryCommand(final int precedence) {
+ super(precedence);
+ }
+
+ @Override
+ protected int rightBinding() {
+ return 1 + leftBinding();
+ }
+
+ @Override
+ public int nextBinding() {
+ return leftBinding() - 1;
+ }
+} \ No newline at end of file
diff --git a/JPratt/src/main/java/com/ashardalon/pratt/commands/impls/NonInitialCommands.java b/JPratt/src/main/java/com/ashardalon/pratt/commands/impls/NonInitialCommands.java
new file mode 100644
index 0000000..41f0bab
--- /dev/null
+++ b/JPratt/src/main/java/com/ashardalon/pratt/commands/impls/NonInitialCommands.java
@@ -0,0 +1,167 @@
+package com.ashardalon.pratt.commands.impls;
+
+import java.util.Set;
+
+import com.ashardalon.pratt.blocks.ParseBlock;
+import com.ashardalon.pratt.blocks.SimpleParseBlock;
+import com.ashardalon.pratt.commands.NonInitialCommand;
+import com.ashardalon.pratt.tokens.Token;
+
+/**
+ * Contains factory methods for producing common implementations of
+ * {@link NonInitialCommand}
+ *
+ * @author EVE
+ *
+ */
+public class NonInitialCommands {
+ /**
+ * Create a left-associative infix operator.
+ *
+ * @param <K> The key type for the tokens.
+ * @param <V> The value type for the tokens.
+ * @param <C> The context type for the tokens.
+ *
+ * @param precedence
+ * The precedence of the operator.
+ *
+ * @return A command implementing that operator.
+ */
+ public static <K, V, C> NonInitialCommand<K, V, C> infixLeft(final int precedence) {
+ return new LeftBinaryCommand<>(precedence);
+ }
+
+ /**
+ * Create a right-associative infix operator.
+ *
+ * @param <K> The key type for the tokens.
+ * @param <V> The value type for the tokens.
+ * @param <C> The context type for the tokens.
+ *
+ * @param precedence
+ * The precedence of the operator.
+ *
+ * @return A command implementing that operator.
+ */
+ public static <K, V, C> NonInitialCommand<K, V, C> infixRight(final int precedence) {
+ return new RightBinaryCommand<>(precedence);
+ }
+
+ /**
+ * Create a non-associative infix operator.
+ *
+ * @param <K> The key type for the tokens.
+ * @param <V> The value type for the tokens.
+ * @param <C> The context type for the tokens.
+ *
+ * @param precedence
+ * The precedence of the operator.
+ *
+ * @return A command implementing that operator.
+ */
+ public static <K, V, C> NonInitialCommand<K, V, C> infixNon(final int precedence) {
+ return new NonBinaryCommand<>(precedence);
+ }
+
+ /**
+ * Create a chained operator.
+ *
+ * @param <K> The key type for the tokens.
+ * @param <V> The value type for the tokens.
+ * @param <C> The context type for the tokens.
+ *
+ * @param precedence
+ * The precedence of the operator.
+ *
+ * @param chainSet
+ * The operators it forms a chain with.
+ *
+ * @param marker
+ * The token to use as the AST node for the chained operators.
+ *
+ * @return A command implementing that operator.
+ */
+ public static <K, V, C> NonInitialCommand<K, V, C> chain(final int precedence, final Set<K> chainSet,
+ final Token<K, V> marker) {
+ return new ChainCommand<>(precedence, chainSet, marker);
+ }
+
+ /**
+ * Create a postfix operator.
+ *
+ * @param <K> The key type for the tokens.
+ * @param <V> The value type for the tokens.
+ * @param <C> The context type for the tokens.
+ *
+ * @param precedence
+ * The precedence of the operator.
+ *
+ * @return A command implementing that operator.
+ */
+ public static <K, V, C> NonInitialCommand<K, V, C> postfix(final int precedence) {
+ return new PostfixCommand<>(precedence);
+ }
+
+ /**
+ * Create a post-circumfix operator.
+ *
+ * This is an operator in form similar to array indexing.
+ *
+ * @param <K> The key type for the tokens.
+ * @param <V> The value type for the tokens.
+ * @param <C> The context type for the tokens.
+ *
+ * @param precedence
+ * The precedence of this operator
+ *
+ * @param insidePrecedence
+ * The precedence of the expression inside the operator
+ *
+ * @param closer
+ * The token that closes the circumfix.
+ *
+ * @param marker
+ * The token to use as the AST node for the operator.
+ *
+ * @return A command implementing that operator.
+ */
+ public static <K, V, C> NonInitialCommand<K, V, C> postCircumfix(final int precedence,
+ final int insidePrecedence, final K closer, final Token<K, V> marker) {
+ final ParseBlock<K, V, C> innerBlock = new SimpleParseBlock<>(insidePrecedence, null, closer);
+
+ return new PostCircumfixCommand<>(precedence, innerBlock, marker);
+ }
+
+ /**
+ * Create a ternary operator.
+ *
+ * This is like C's ?: operator.
+ *
+ * @param <K> The key type for the tokens.
+ * @param <V> The value type for the tokens.
+ * @param <C> The context type for the tokens.
+ *
+ * @param precedence
+ * The precedence of the operator.
+ *
+ * @param insidePrecedence
+ * The precedence of the inner section of the operator.
+ *
+ * @param closer
+ * The token that marks the end of the inner section.
+ *
+ * @param marker
+ * The token to use as the AST node for the operator.
+ *
+ * @param nonassoc
+ * True if the command is non-associative, false otherwise.
+ *
+ * @return A command implementing this operator.
+ */
+ public static <K, V, C> NonInitialCommand<K, V, C> ternary(final int precedence, final int insidePrecedence,
+ final K closer, final Token<K, V> marker, final boolean nonassoc) {
+ final ParseBlock<K, V, C> innerBlock = new SimpleParseBlock<>(insidePrecedence, null, closer);
+
+ return new TernaryCommand<>(precedence, innerBlock, marker, nonassoc);
+ }
+}
diff --git a/JPratt/src/main/java/com/ashardalon/pratt/commands/impls/PanfixCommand.java b/JPratt/src/main/java/com/ashardalon/pratt/commands/impls/PanfixCommand.java
new file mode 100644
index 0000000..99d43c8
--- /dev/null
+++ b/JPratt/src/main/java/com/ashardalon/pratt/commands/impls/PanfixCommand.java
@@ -0,0 +1,55 @@
+package com.ashardalon.pratt.commands.impls;
+
+import com.ashardalon.pratt.ParserContext;
+import com.ashardalon.pratt.commands.CommandResult;
+import com.ashardalon.pratt.commands.InitialCommand;
+import com.ashardalon.pratt.commands.CommandResult.Status;
+import com.ashardalon.pratt.tokens.Token;
+
+import bjc.data.SimpleTree;
+import bjc.data.Tree;
+import bjc.utils.parserutils.ParserException;
+
+/**
+ * Represents a 'panfix' command, one where the operator is repeated prefix, infix and postfix.
+ * @author bjcul
+ *
+ * @param <K> The key type of the token
+ * @param <V> The value type of the token
+ * @param <C> The context type of the parser
+ */
+public final class PanfixCommand<K, V, C> implements InitialCommand<K, V, C> {
+ private final Token<K, V> marker;
+ private final K term;
+ private final int precedence;
+
+ /**
+ * Create a new panfix command.
+ *
+ * @param marker The marker token.
+ * @param term The value to use as the root of the result-tree
+ * @param precedence The precedence for this command
+ */
+ public PanfixCommand(Token<K, V> marker, K term, int precedence) {
+ this.marker = marker;
+ this.term = term;
+ this.precedence = precedence;
+ }
+
+ @Override
+ public CommandResult<K, V> denote(Token<K, V> operator, ParserContext<K, V, C> ctx) throws ParserException {
+ CommandResult<K,V> resLeftSide = ctx.parse.parseExpression(precedence + 1, ctx.tokens, ctx.state, false);
+ if (resLeftSide.status != Status.SUCCESS) return resLeftSide;
+ Tree<Token<K, V>> leftSide = resLeftSide.success();
+ ctx.tokens.expect(term);
+ ctx.tokens.next();
+
+ CommandResult<K, V> resRightSide = ctx.parse.parseExpression(precedence + 1, ctx.tokens, ctx.state, false);
+ if (resLeftSide.status != Status.SUCCESS) return resRightSide;
+ Tree<Token<K,V>> rightSide = resRightSide.success();
+ ctx.tokens.expect(term);
+ ctx.tokens.next();
+
+ return CommandResult.success(new SimpleTree<>(marker, leftSide, rightSide));
+ }
+} \ No newline at end of file
diff --git a/JPratt/src/main/java/com/ashardalon/pratt/commands/impls/PostCircumfixCommand.java b/JPratt/src/main/java/com/ashardalon/pratt/commands/impls/PostCircumfixCommand.java
new file mode 100644
index 0000000..c5b60a1
--- /dev/null
+++ b/JPratt/src/main/java/com/ashardalon/pratt/commands/impls/PostCircumfixCommand.java
@@ -0,0 +1,64 @@
+package com.ashardalon.pratt.commands.impls;
+
+import bjc.data.Tree;
+
+import com.ashardalon.pratt.ParserContext;
+import com.ashardalon.pratt.blocks.ParseBlock;
+import com.ashardalon.pratt.commands.BinaryPostCommand;
+import com.ashardalon.pratt.commands.CommandResult;
+import com.ashardalon.pratt.commands.CommandResult.Status;
+import com.ashardalon.pratt.tokens.Token;
+
+import bjc.data.SimpleTree;
+import bjc.utils.parserutils.ParserException;
+
+/**
+ * A post-circumfix operator, like array indexing.
+ *
+ * @author bjculkin
+ *
+ * @param <K>
+ * The key type of the tokens.
+ *
+ * @param <V>
+ * The value type of the tokens.
+ *
+ * @param <C>
+ * The state type of the parser.
+ */
+public class PostCircumfixCommand<K, V, C> extends BinaryPostCommand<K, V, C> {
+ private final ParseBlock<K, V, C> innerBlock;
+
+ private final Token<K, V> mark;
+
+ /**
+ * Create a new post-circumfix operator.
+ *
+ * @param precedence
+ * The precedence of the operator.
+ *
+ * @param inner
+ * The block inside the expression.
+ *
+ * @param marker
+ * The token to use as the node for the AST.
+ */
+ public PostCircumfixCommand(final int precedence, final ParseBlock<K, V, C> inner, final Token<K, V> marker) {
+ super(precedence);
+
+ if(inner == null) throw new NullPointerException("Inner block must not be null");
+
+ innerBlock = inner;
+
+ mark = marker;
+ }
+
+ @Override
+ public CommandResult<K, V> denote(final Tree<Token<K, V>> operand, final Token<K, V> operator,
+ final ParserContext<K, V, C> ctx) throws ParserException {
+ final CommandResult<K,V> insideRes = innerBlock.parse(ctx);
+ if (insideRes.status != Status.SUCCESS) return insideRes;
+ Tree<Token<K, V>> inside = insideRes.success();
+ return CommandResult.success(new SimpleTree<>(mark, operand, inside));
+ }
+} \ No newline at end of file
diff --git a/JPratt/src/main/java/com/ashardalon/pratt/commands/impls/PostfixCommand.java b/JPratt/src/main/java/com/ashardalon/pratt/commands/impls/PostfixCommand.java
new file mode 100644
index 0000000..84ac61d
--- /dev/null
+++ b/JPratt/src/main/java/com/ashardalon/pratt/commands/impls/PostfixCommand.java
@@ -0,0 +1,43 @@
+package com.ashardalon.pratt.commands.impls;
+
+import bjc.data.Tree;
+
+import com.ashardalon.pratt.ParserContext;
+import com.ashardalon.pratt.commands.BinaryPostCommand;
+import com.ashardalon.pratt.commands.CommandResult;
+import com.ashardalon.pratt.tokens.Token;
+
+import bjc.data.SimpleTree;
+import bjc.utils.parserutils.ParserException;
+
+/**
+ * A postfix operator.
+ *
+ * @author bjculkin
+ *
+ * @param <K>
+ * The key type of the tokens.
+ *
+ * @param <V>
+ * The value type of the tokens.
+ *
+ * @param <C>
+ * The state type of the parser.
+ */
+public class PostfixCommand<K, V, C> extends BinaryPostCommand<K, V, C> {
+ /**
+ * Create a new postfix operator.
+ *
+ * @param precedence
+ * The precedence of the operator.
+ */
+ public PostfixCommand(final int precedence) {
+ super(precedence);
+ }
+
+ @Override
+ public CommandResult<K, V> denote(final Tree<Token<K, V>> operand, final Token<K, V> operator,
+ final ParserContext<K, V, C> ctx) throws ParserException {
+ return CommandResult.success(new SimpleTree<>(operator, operand));
+ }
+} \ No newline at end of file
diff --git a/JPratt/src/main/java/com/ashardalon/pratt/commands/impls/PreTernaryCommand.java b/JPratt/src/main/java/com/ashardalon/pratt/commands/impls/PreTernaryCommand.java
new file mode 100644
index 0000000..cd01333
--- /dev/null
+++ b/JPratt/src/main/java/com/ashardalon/pratt/commands/impls/PreTernaryCommand.java
@@ -0,0 +1,85 @@
+package com.ashardalon.pratt.commands.impls;
+
+import bjc.data.Tree;
+
+import com.ashardalon.pratt.ParserContext;
+import com.ashardalon.pratt.blocks.ParseBlock;
+import com.ashardalon.pratt.commands.AbstractInitialCommand;
+import com.ashardalon.pratt.commands.CommandResult;
+import com.ashardalon.pratt.commands.CommandResult.Status;
+import com.ashardalon.pratt.tokens.Token;
+
+import bjc.data.SimpleTree;
+import bjc.utils.parserutils.ParserException;
+
+/**
+ * A prefix ternary operator, like an if/then/else group.
+ *
+ * @author bjculkin
+ *
+ * @param <K>
+ * The key type of the tokens.
+ *
+ * @param <V>
+ * The value type of the tokens.
+ *
+ * @param <C>
+ * The state type of the parser.
+ */
+public class PreTernaryCommand<K, V, C> extends AbstractInitialCommand<K, V, C> {
+ private final Token<K, V> trm;
+
+ private final ParseBlock<K, V, C> condBlock;
+
+ private final ParseBlock<K, V, C> opblock1;
+ private final ParseBlock<K, V, C> opblock2;
+
+ /**
+ * Create a new ternary statement.
+ *
+ * @param cond
+ * The block for handling the condition.
+ *
+ * @param op1
+ * The block for handling the first operator.
+ *
+ * @param op2
+ * The block for handling the second operator.
+ *
+ * @param term
+ * The token to use as the node for the AST.
+ */
+ public PreTernaryCommand(final ParseBlock<K, V, C> cond, final ParseBlock<K, V, C> op1,
+ final ParseBlock<K, V, C> op2, final Token<K, V> term) {
+ super();
+
+ if(cond == null)
+ throw new NullPointerException("Cond block must not be null");
+ else if(op1 == null)
+ throw new NullPointerException("Op block #1 must not be null");
+ else if(op2 == null) throw new NullPointerException("Op block #2 must not be null");
+
+ condBlock = cond;
+ opblock1 = op1;
+ opblock2 = op2;
+
+ trm = term;
+ }
+
+ @Override
+ protected CommandResult<K, V> intNullDenotation(final Token<K, V> operator, final ParserContext<K, V, C> ctx)
+ throws ParserException {
+ final CommandResult<K,V> condRes = condBlock.parse(ctx);
+ if (condRes.status != Status.SUCCESS) return condRes;
+ Tree<Token<K, V>> cond = condRes.success();
+
+ final CommandResult<K,V> op1Res = opblock1.parse(ctx);
+ if (op1Res.status != Status.SUCCESS) return op1Res;
+ Tree<Token<K, V>> op1 = op1Res.success();
+
+ final CommandResult<K,V> op2Res = opblock2.parse(ctx);
+ if (op2Res.status != Status.SUCCESS) return op2Res;
+ Tree<Token<K, V>> op2 = op2Res.success();
+ return CommandResult.success(new SimpleTree<>(trm, cond, op1, op2));
+ }
+} \ No newline at end of file
diff --git a/JPratt/src/main/java/com/ashardalon/pratt/commands/impls/RightBinaryCommand.java b/JPratt/src/main/java/com/ashardalon/pratt/commands/impls/RightBinaryCommand.java
new file mode 100644
index 0000000..9145fd6
--- /dev/null
+++ b/JPratt/src/main/java/com/ashardalon/pratt/commands/impls/RightBinaryCommand.java
@@ -0,0 +1,32 @@
+package com.ashardalon.pratt.commands.impls;
+
+import com.ashardalon.pratt.commands.BinaryCommand;
+
+/**
+ * A right-associative binary operator.
+ *
+ * @author bjculkin
+ *
+ * @param <K>
+ * The key type of the tokens.
+ * @param <V>
+ * The value type of the tokens.
+ * @param <C>
+ * The state type of the parser.
+ */
+public class RightBinaryCommand<K, V, C> extends BinaryCommand<K, V, C> {
+ /**
+ * Create a new right-associative operator.
+ *
+ * @param precedence
+ * The precedence of the operator.
+ */
+ public RightBinaryCommand(final int precedence) {
+ super(precedence);
+ }
+
+ @Override
+ protected int rightBinding() {
+ return leftBinding();
+ }
+} \ No newline at end of file
diff --git a/JPratt/src/main/java/com/ashardalon/pratt/commands/impls/TernaryCommand.java b/JPratt/src/main/java/com/ashardalon/pratt/commands/impls/TernaryCommand.java
new file mode 100644
index 0000000..b52107f
--- /dev/null
+++ b/JPratt/src/main/java/com/ashardalon/pratt/commands/impls/TernaryCommand.java
@@ -0,0 +1,78 @@
+package com.ashardalon.pratt.commands.impls;
+
+import bjc.data.Tree;
+
+import com.ashardalon.pratt.ParserContext;
+import com.ashardalon.pratt.blocks.ParseBlock;
+import com.ashardalon.pratt.commands.BinaryPostCommand;
+import com.ashardalon.pratt.commands.CommandResult;
+import com.ashardalon.pratt.commands.CommandResult.Status;
+import com.ashardalon.pratt.tokens.Token;
+
+import bjc.data.SimpleTree;
+import bjc.utils.parserutils.ParserException;
+
+/**
+ * A ternary command, like C's ?:
+ *
+ * @author bjculkin
+ *
+ * @param <K> The key type of the tokens.
+ *
+ * @param <V> The value type of the tokens.
+ *
+ * @param <C> The state type of the parser.
+ */
+public class TernaryCommand<K, V, C> extends BinaryPostCommand<K, V, C> {
+ private final ParseBlock<K, V, C> innerBlck;
+
+ private final Token<K, V> mark;
+
+ private final boolean nonassoc;
+
+ /**
+ * Create a new ternary command.
+ *
+ * @param precedence The precedence of this operator.
+ *
+ * @param innerBlock The representation of the inner block of the expression.
+ *
+ * @param marker The token to use as the root of the AST node.
+ *
+ * @param isNonassoc Whether or not the conditional is associative.
+ */
+ public TernaryCommand(final int precedence, final ParseBlock<K, V, C> innerBlock, final Token<K, V> marker,
+ final boolean isNonassoc) {
+ super(precedence);
+
+ if (innerBlock == null)
+ throw new NullPointerException("Inner block must not be null");
+ else if (marker == null)
+ throw new NullPointerException("Marker must not be null");
+
+ innerBlck = innerBlock;
+ mark = marker;
+ nonassoc = isNonassoc;
+ }
+
+ @Override
+ public CommandResult<K, V> denote(final Tree<Token<K, V>> operand, final Token<K, V> operator,
+ final ParserContext<K, V, C> ctx) throws ParserException {
+ final CommandResult<K, V> innerRes = innerBlck.parse(ctx);
+ if (innerRes.status != Status.SUCCESS) return innerRes;
+ Tree<Token<K, V>> inner = innerRes.success();
+
+ final CommandResult<K,V> outerRes = ctx.parse.parseExpression(1 + leftBinding(), ctx.tokens, ctx.state, false);
+ if (outerRes.status != Status.SUCCESS) return outerRes;
+ Tree<Token<K, V>> outer = outerRes.success();
+ return CommandResult.success(new SimpleTree<>(mark, inner, operand, outer));
+ }
+
+ @Override
+ public int nextBinding() {
+ if (nonassoc)
+ return leftBinding() - 1;
+
+ return leftBinding();
+ }
+} \ No newline at end of file
diff --git a/JPratt/src/main/java/com/ashardalon/pratt/commands/impls/TransformingInitialCommand.java b/JPratt/src/main/java/com/ashardalon/pratt/commands/impls/TransformingInitialCommand.java
new file mode 100644
index 0000000..9a08c44
--- /dev/null
+++ b/JPratt/src/main/java/com/ashardalon/pratt/commands/impls/TransformingInitialCommand.java
@@ -0,0 +1,59 @@
+package com.ashardalon.pratt.commands.impls;
+
+import java.util.function.UnaryOperator;
+
+import com.ashardalon.pratt.ParserContext;
+import com.ashardalon.pratt.commands.AbstractInitialCommand;
+import com.ashardalon.pratt.commands.CommandResult;
+import com.ashardalon.pratt.commands.InitialCommand;
+import com.ashardalon.pratt.commands.CommandResult.Status;
+import com.ashardalon.pratt.tokens.Token;
+
+import bjc.data.Tree;
+import bjc.utils.parserutils.ParserException;
+
+/**
+ * An initial command that transforms the result of another command.
+ *
+ * @author bjculkin
+ *
+ * @param <K>
+ * The key type of the tokens.
+ *
+ * @param <V>
+ * The value type of the tokens.
+ *
+ * @param <C>
+ * The state type of the parser.
+ */
+public class TransformingInitialCommand<K, V, C> extends AbstractInitialCommand<K, V, C> {
+ private final InitialCommand<K, V, C> internl;
+
+ private final UnaryOperator<Tree<Token<K, V>>> transfrm;
+
+ /**
+ * Create a new transforming initial command.
+ *
+ * @param internal
+ * The initial command to delegate to.
+ *
+ * @param transform
+ * The transform to apply to the returned tree.
+ */
+ public TransformingInitialCommand(final InitialCommand<K, V, C> internal,
+ final UnaryOperator<Tree<Token<K, V>>> transform) {
+ super();
+ internl = internal;
+ transfrm = transform;
+ }
+
+ @Override
+ protected CommandResult<K, V> intNullDenotation(final Token<K, V> operator, final ParserContext<K, V, C> ctx)
+ throws ParserException {
+ CommandResult<K,V> result = internl.denote(operator, ctx);
+ if (result.status != Status.SUCCESS) return result;
+
+ return CommandResult.success(transfrm.apply(result.success()));
+ }
+
+}
diff --git a/JPratt/src/main/java/com/ashardalon/pratt/commands/impls/UnaryCommand.java b/JPratt/src/main/java/com/ashardalon/pratt/commands/impls/UnaryCommand.java
new file mode 100644
index 0000000..a7efdc3
--- /dev/null
+++ b/JPratt/src/main/java/com/ashardalon/pratt/commands/impls/UnaryCommand.java
@@ -0,0 +1,52 @@
+package com.ashardalon.pratt.commands.impls;
+
+import bjc.data.Tree;
+
+import com.ashardalon.pratt.ParserContext;
+import com.ashardalon.pratt.commands.AbstractInitialCommand;
+import com.ashardalon.pratt.commands.CommandResult;
+import com.ashardalon.pratt.commands.CommandResult.Status;
+import com.ashardalon.pratt.tokens.Token;
+
+import bjc.data.SimpleTree;
+import bjc.utils.parserutils.ParserException;
+
+/**
+ * A unary operator.
+ *
+ * @author bjculkin
+ *
+ * @param <K>
+ * The key type of the tokens.
+ *
+ * @param <V>
+ * The value type of the tokens.
+ *
+ * @param <C>
+ * The state type of the parser.
+ */
+public class UnaryCommand<K, V, C> extends AbstractInitialCommand<K, V, C> {
+ private final int nullPwer;
+
+ /**
+ * Create a new unary command.
+ *
+ * @param precedence
+ * The precedence of this operator.
+ */
+ public UnaryCommand(final int precedence) {
+ if(precedence < 0) throw new IllegalArgumentException("Precedence must be non-negative");
+
+ nullPwer = precedence;
+ }
+
+ @Override
+ protected CommandResult<K, V> intNullDenotation(final Token<K, V> operator, final ParserContext<K, V, C> ctx)
+ throws ParserException {
+ CommandResult<K,V> result = ctx.parse.parseExpression(nullPwer, ctx.tokens, ctx.state, false);
+ if (result.status != Status.SUCCESS) return result;
+ final Tree<Token<K, V>> opr = result.success();
+
+ return CommandResult.success(new SimpleTree<>(operator, opr));
+ }
+} \ No newline at end of file
diff --git a/JPratt/src/main/java/com/ashardalon/pratt/parsing/package-info.java b/JPratt/src/main/java/com/ashardalon/pratt/parsing/package-info.java
new file mode 100644
index 0000000..7108bc2
--- /dev/null
+++ b/JPratt/src/main/java/com/ashardalon/pratt/parsing/package-info.java
@@ -0,0 +1 @@
+package com.ashardalon.pratt.parsing; \ No newline at end of file
diff --git a/JPratt/src/main/java/com/ashardalon/pratt/tokens/ExpectionNotMet.java b/JPratt/src/main/java/com/ashardalon/pratt/tokens/ExpectionNotMet.java
new file mode 100644
index 0000000..b700203
--- /dev/null
+++ b/JPratt/src/main/java/com/ashardalon/pratt/tokens/ExpectionNotMet.java
@@ -0,0 +1,23 @@
+package com.ashardalon.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/com/ashardalon/pratt/tokens/SimpleToken.java b/JPratt/src/main/java/com/ashardalon/pratt/tokens/SimpleToken.java
new file mode 100644
index 0000000..ee68835
--- /dev/null
+++ b/JPratt/src/main/java/com/ashardalon/pratt/tokens/SimpleToken.java
@@ -0,0 +1,60 @@
+package com.ashardalon.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/com/ashardalon/pratt/tokens/SimpleTokenStream.java b/JPratt/src/main/java/com/ashardalon/pratt/tokens/SimpleTokenStream.java
new file mode 100644
index 0000000..cc751d1
--- /dev/null
+++ b/JPratt/src/main/java/com/ashardalon/pratt/tokens/SimpleTokenStream.java
@@ -0,0 +1,82 @@
+package com.ashardalon.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/com/ashardalon/pratt/tokens/StringToken.java b/JPratt/src/main/java/com/ashardalon/pratt/tokens/StringToken.java
new file mode 100644
index 0000000..e9b17dc
--- /dev/null
+++ b/JPratt/src/main/java/com/ashardalon/pratt/tokens/StringToken.java
@@ -0,0 +1,83 @@
+package com.ashardalon.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/com/ashardalon/pratt/tokens/StringTokenStream.java b/JPratt/src/main/java/com/ashardalon/pratt/tokens/StringTokenStream.java
new file mode 100644
index 0000000..fb25016
--- /dev/null
+++ b/JPratt/src/main/java/com/ashardalon/pratt/tokens/StringTokenStream.java
@@ -0,0 +1,84 @@
+package com.ashardalon.pratt.tokens;
+
+import static com.ashardalon.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/com/ashardalon/pratt/tokens/Token.java b/JPratt/src/main/java/com/ashardalon/pratt/tokens/Token.java
new file mode 100644
index 0000000..86f0d05
--- /dev/null
+++ b/JPratt/src/main/java/com/ashardalon/pratt/tokens/Token.java
@@ -0,0 +1,30 @@
+package com.ashardalon.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/com/ashardalon/pratt/tokens/TokenStream.java b/JPratt/src/main/java/com/ashardalon/pratt/tokens/TokenStream.java
new file mode 100644
index 0000000..3f2681b
--- /dev/null
+++ b/JPratt/src/main/java/com/ashardalon/pratt/tokens/TokenStream.java
@@ -0,0 +1,106 @@
+package com.ashardalon.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);
+ }
+}