From b168fd38be0bb344d268bfd11d14df36bb9fd4f2 Mon Sep 17 00:00:00 2001 From: student Date: Fri, 24 Mar 2017 11:51:10 -0400 Subject: Update Pratt parser --- .../bjc/utils/parserutils/pratt/NullCommands.java | 112 +++++++++++++++++++-- 1 file changed, 102 insertions(+), 10 deletions(-) (limited to 'BJC-Utils2/src/main/java/bjc/utils/parserutils/pratt/NullCommands.java') diff --git a/BJC-Utils2/src/main/java/bjc/utils/parserutils/pratt/NullCommands.java b/BJC-Utils2/src/main/java/bjc/utils/parserutils/pratt/NullCommands.java index cf57241..f0c2a80 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/parserutils/pratt/NullCommands.java +++ b/BJC-Utils2/src/main/java/bjc/utils/parserutils/pratt/NullCommands.java @@ -16,7 +16,7 @@ public class NullCommands { @Override public ITree> nullDenotation(Token operator, ParserContext ctx) throws ParserException { - //tokens.next(); + // tokens.next(); return intNullDenotation(operator, ctx); } @@ -36,16 +36,16 @@ public class NullCommands { @Override protected ITree> intNullDenotation(Token operator, ParserContext ctx) throws ParserException { - ITree> opr = ctx.parse.parseExpression(nullPwer, ctx.tokens, ctx.state); + ITree> opr = ctx.parse.parseExpression(nullPwer, ctx.tokens, ctx.state, false); return new Tree<>(operator, opr); } } private static class GroupingCommand extends AbstractNullCommand { - private K term; - private Token mark; - private int inner; + private K term; + private Token mark; + private int inner; public GroupingCommand(int innerPrec, K terminator, Token marker) { inner = innerPrec; @@ -57,7 +57,7 @@ public class NullCommands { @Override protected ITree> intNullDenotation(Token operator, ParserContext ctx) throws ParserException { - ITree> opr = ctx.parse.parseExpression(inner, ctx.tokens, ctx.state); + ITree> opr = ctx.parse.parseExpression(inner, ctx.tokens, ctx.state, false); ctx.tokens.expect(term); @@ -74,11 +74,63 @@ public class NullCommands { } } + private static class ConstantCommand extends NullCommand { + private ITree> val; + + public ConstantCommand(ITree> con) { + val = con; + } + + @Override + public ITree> nullDenotation(Token operator, ParserContext ctx) + throws ParserException { + return val; + } + } + + private static class PreTernaryCommand extends AbstractNullCommand { + private int cond1; + private int block1; + private int block2; + + private K mark1; + private K mark2; + + private Token term; + + public PreTernaryCommand(int cond1, int block1, int block2, K mark1, K mark2, Token term) { + super(); + this.cond1 = cond1; + this.block1 = block1; + this.block2 = block2; + this.mark1 = mark1; + this.mark2 = mark2; + this.term = term; + } + + @SuppressWarnings("unchecked") + @Override + protected ITree> intNullDenotation(Token operator, ParserContext ctx) + throws ParserException { + ITree> cond = ctx.parse.parseExpression(cond1, ctx.tokens, ctx.state, false); + + ctx.tokens.expect(mark1); + + ITree> fstBlock = ctx.parse.parseExpression(block1, ctx.tokens, ctx.state, false); + + ctx.tokens.expect(mark2); + + ITree> sndBlock = ctx.parse.parseExpression(block2, ctx.tokens, ctx.state, false); + + return new Tree<>(term, cond, fstBlock, sndBlock); + } + } + /** * Create a new unary operator. * * @param precedence - * The precedence of the operator. + * The precedence of the operator. * * @return A command implementing that operator. */ @@ -90,13 +142,13 @@ public class NullCommands { * Create a new grouping operator. * * @param precedence - * The precedence of the expression in the operator. + * The precedence of the expression in the operator. * * @param term - * The type that closes the group. + * The type that closes the group. * * @param mark - * The token for the AST node of the group. + * The token for the AST node of the group. * * @return A command implementing the operator. */ @@ -112,4 +164,44 @@ public class NullCommands { public static NullCommand leaf() { return new LeafCommand<>(); } + + /** + * Create a new pre-ternary operator, like an if-then-else statement. + * + * @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 NullCommand preTernary(int cond1, int block1, int block2, K mark1, K mark2, + Token term) { + return new PreTernaryCommand<>(cond1, block1, block2, mark1, mark2, term); + } + + /** + * Create a new named constant. + * + * @param val + * The value of the constant. + * + * @return A command implementing the constant. + */ + public static NullCommand constant(ITree> val) { + return new ConstantCommand<>(val); + } } -- cgit v1.2.3