From 42990231fee502552b769b9af4c04ac0dcaeb195 Mon Sep 17 00:00:00 2001 From: bculkin2442 Date: Sat, 25 Mar 2017 19:13:42 -0400 Subject: Update Pratt parser --- .../pratt/commands/PostCircumfixCommand.java | 54 ++++++++++++++++------ 1 file changed, 41 insertions(+), 13 deletions(-) (limited to 'BJC-Utils2/src/main/java/bjc/utils/parserutils/pratt/commands/PostCircumfixCommand.java') diff --git a/BJC-Utils2/src/main/java/bjc/utils/parserutils/pratt/commands/PostCircumfixCommand.java b/BJC-Utils2/src/main/java/bjc/utils/parserutils/pratt/commands/PostCircumfixCommand.java index ba5099e..90fca00 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/parserutils/pratt/commands/PostCircumfixCommand.java +++ b/BJC-Utils2/src/main/java/bjc/utils/parserutils/pratt/commands/PostCircumfixCommand.java @@ -3,29 +3,57 @@ package bjc.utils.parserutils.pratt.commands; import bjc.utils.data.ITree; import bjc.utils.data.Tree; import bjc.utils.parserutils.ParserException; +import bjc.utils.parserutils.pratt.ParseBlock; import bjc.utils.parserutils.pratt.ParserContext; import bjc.utils.parserutils.pratt.Token; +/** + * A post-circumfix operator, like array indexing. + * + * @author bjculkin + * + * @param + * The key type of the tokens. + * + * @param + * The value type of the tokens. + * + * @param + * The state type of the parser. + */ public class PostCircumfixCommand extends BinaryPostCommand { - private int insidePrec; - private K term; - private Token mark; + private ParseBlock innerBlock; - public PostCircumfixCommand(int leftPower, int insidePower, K terminator, Token marker) { - super(leftPower); + private Token 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(int precedence, ParseBlock inner, Token marker) { + super(precedence); + + if (inner == null) { + throw new NullPointerException("Inner block must not be null"); + } + + innerBlock = inner; - insidePrec = insidePower; - term = terminator; mark = marker; } - @SuppressWarnings("unchecked") @Override - public ITree> denote(ITree> operand, Token operator, - ParserContext ctx) throws ParserException { - ITree> inside = ctx.parse.parseExpression(insidePrec, ctx.tokens, ctx.state, false); - - ctx.tokens.expect(term); + public ITree> denote(ITree> operand, Token operator, ParserContext ctx) + throws ParserException { + ITree> inside = innerBlock.parse(ctx); return new Tree<>(mark, operand, inside); } -- cgit v1.2.3