summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--JPratt/src/examples/java/bjc/pratt/examples/lang/PrattParserTest.java2
-rw-r--r--JPratt/src/main/java/bjc/pratt/commands/impls/InitialCommands.java30
2 files changed, 31 insertions, 1 deletions
diff --git a/JPratt/src/examples/java/bjc/pratt/examples/lang/PrattParserTest.java b/JPratt/src/examples/java/bjc/pratt/examples/lang/PrattParserTest.java
index 15c0b81..ba957c9 100644
--- a/JPratt/src/examples/java/bjc/pratt/examples/lang/PrattParserTest.java
+++ b/JPratt/src/examples/java/bjc/pratt/examples/lang/PrattParserTest.java
@@ -266,7 +266,7 @@ public class PrattParserTest {
parser.addNonInitialCommand("|>", infixLeft(12));
/*
- * Non-short circuiting condtionals.
+ * Non-short circuiting conditionals.
*/
final NonInitialCommand<String, String, TestContext> nonSSRelJoin = infixLeft(13);
parser.addNonInitialCommand("and", nonSSRelJoin);
diff --git a/JPratt/src/main/java/bjc/pratt/commands/impls/InitialCommands.java b/JPratt/src/main/java/bjc/pratt/commands/impls/InitialCommands.java
index 8a9108b..0cfec29 100644
--- a/JPratt/src/main/java/bjc/pratt/commands/impls/InitialCommands.java
+++ b/JPratt/src/main/java/bjc/pratt/commands/impls/InitialCommands.java
@@ -9,6 +9,7 @@ import java.util.function.UnaryOperator;
import bjc.pratt.blocks.ParseBlock;
import bjc.pratt.commands.InitialCommand;
import bjc.pratt.tokens.Token;
+import bjc.data.SimpleTree;
import bjc.data.Tree;
/**
@@ -192,4 +193,33 @@ public class InitialCommands {
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 (operator, ctx) -> {
+ Tree<Token<K,V>> leftSide = ctx.parse.parseExpression(precedence + 1, ctx.tokens, ctx.state, false);
+ ctx.tokens.expect(term);
+ ctx.tokens.next();
+
+ Tree<Token<K,V>> rightSide = ctx.parse.parseExpression(precedence + 1, ctx.tokens, ctx.state, false);
+ ctx.tokens.expect(term);
+ ctx.tokens.next();
+
+ return new SimpleTree<>(marker, leftSide, rightSide);
+ };
+ }
} \ No newline at end of file