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/commands/impls/ChainCommand.java | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) (limited to 'JPratt/src/main/java/bjc/pratt/commands/impls/ChainCommand.java') diff --git a/JPratt/src/main/java/bjc/pratt/commands/impls/ChainCommand.java b/JPratt/src/main/java/bjc/pratt/commands/impls/ChainCommand.java index ed7a088..7311eb9 100644 --- a/JPratt/src/main/java/bjc/pratt/commands/impls/ChainCommand.java +++ b/JPratt/src/main/java/bjc/pratt/commands/impls/ChainCommand.java @@ -4,6 +4,8 @@ import java.util.Set; import bjc.pratt.ParserContext; import bjc.pratt.commands.BinaryPostCommand; +import bjc.pratt.commands.CommandResult; +import bjc.pratt.commands.CommandResult.Status; import bjc.pratt.tokens.Token; import bjc.data.Tree; import bjc.data.SimpleTree; @@ -48,10 +50,12 @@ public class ChainCommand extends BinaryPostCommand { } @Override - public Tree> denote(final Tree> operand, final Token operator, + public CommandResult denote(final Tree> operand, final Token operator, final ParserContext ctx) throws ParserException { - final Tree> tree = ctx.parse.parseExpression(1 + leftBinding(), ctx.tokens, ctx.state, + CommandResult resOuter = ctx.parse.parseExpression(1 + leftBinding(), ctx.tokens, ctx.state, false); + if (resOuter.status != Status.SUCCESS) return resOuter; + final Tree> tree = resOuter.success(); final Tree> res = new SimpleTree<>(operator, operand, tree); @@ -59,13 +63,16 @@ public class ChainCommand extends BinaryPostCommand { final Token tok = ctx.tokens.current(); ctx.tokens.next(); - final Tree> other = denote(tree, tok, + CommandResult resOther = denote(tree, tok, new ParserContext<>(ctx.tokens, ctx.parse, ctx.state)); + if (resOther.status != Status.SUCCESS) return resOther; + + final Tree> other = resOther.success(); - return new SimpleTree<>(chain, res, other); + return CommandResult.success(new SimpleTree<>(chain, res, other)); } - return res; + return CommandResult.success(res); } @Override -- cgit v1.2.3