From cea3e47938322b97c318dea38dc0d649e196dc1b Mon Sep 17 00:00:00 2001 From: Ben Culkin Date: Tue, 16 Aug 2022 23:03:27 -0400 Subject: Refactor to add backtracking support This probably doesn't help w/ error messages, but it enables some cool ideas where syntax can be reused in cases where it would otherwise be invalid --- .../java/bjc/pratt/blocks/GrammarParseBlock.java | 53 +++++++++++++--------- 1 file changed, 31 insertions(+), 22 deletions(-) (limited to 'JPratt/src/main/java/bjc/pratt/blocks/GrammarParseBlock.java') diff --git a/JPratt/src/main/java/bjc/pratt/blocks/GrammarParseBlock.java b/JPratt/src/main/java/bjc/pratt/blocks/GrammarParseBlock.java index 2432d6e..3f79665 100644 --- a/JPratt/src/main/java/bjc/pratt/blocks/GrammarParseBlock.java +++ b/JPratt/src/main/java/bjc/pratt/blocks/GrammarParseBlock.java @@ -4,6 +4,8 @@ import java.util.function.Function; import bjc.pratt.ParserContext; import bjc.pratt.PrattParser; +import bjc.pratt.commands.CommandResult; +import bjc.pratt.commands.CommandResult.Status; import bjc.pratt.tokens.Token; import bjc.pratt.tokens.TokenStream; import bjc.data.Tree; @@ -15,23 +17,17 @@ import bjc.utils.parserutils.ParserException; * * @author bjculkin * - * @param - * The key type of the outer tokens. + * @param The key type of the outer tokens. * - * @param - * The value type of the outer tokens. + * @param The value type of the outer tokens. * - * @param - * The state type of the outer parser. + * @param The state type of the outer parser. * - * @param - * The key type of the inner tokens. + * @param The key type of the inner tokens. * - * @param - * The value type of the inner tokens. + * @param The value type of the inner tokens. * - * @param - * The state type of the outer parser. + * @param The state type of the outer parser. */ public class GrammarParseBlock implements ParseBlock { private final PrattParser innr; @@ -46,12 +42,14 @@ public class GrammarParseBlock implements ParseBlock inner, final int precedence, final boolean isStatement, final Function, TokenStream> tokenTransform, @@ -66,16 +64,27 @@ public class GrammarParseBlock implements ParseBlock> parse(final ParserContext ctx) throws ParserException { + public CommandResult parse(final ParserContext ctx) throws ParserException { final C2 newState = stteTransform.to(ctx.state); final TokenStream newTokens = tkenTransform.apply(ctx.tokens); - final Tree> expression = innr.parseExpression(prcedence, newTokens, newState, - isStatemnt); + final CommandResult 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> expression = res.success(); ctx.state = stteTransform.from(newState); - return xpressionTransform.apply(expression); + return CommandResult.success(xpressionTransform.apply(expression)); } } \ No newline at end of file -- cgit v1.2.3