diff options
| author | Ben Culkin <scorpress@gmail.com> | 2022-08-16 23:03:27 -0400 |
|---|---|---|
| committer | Ben Culkin <scorpress@gmail.com> | 2022-08-16 23:03:27 -0400 |
| commit | cea3e47938322b97c318dea38dc0d649e196dc1b (patch) | |
| tree | 0ceef0bafbfed7aa5802e8fc526c0c98276f1fff /JPratt/src/main/java/bjc/pratt/blocks | |
| parent | 4869146748ed51eb212935d2b971388fb9e73d37 (diff) | |
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
Diffstat (limited to 'JPratt/src/main/java/bjc/pratt/blocks')
6 files changed, 68 insertions, 39 deletions
diff --git a/JPratt/src/main/java/bjc/pratt/blocks/ChainParseBlock.java b/JPratt/src/main/java/bjc/pratt/blocks/ChainParseBlock.java index 038b6ea..2717e42 100644 --- a/JPratt/src/main/java/bjc/pratt/blocks/ChainParseBlock.java +++ b/JPratt/src/main/java/bjc/pratt/blocks/ChainParseBlock.java @@ -3,6 +3,8 @@ package bjc.pratt.blocks; import java.util.Set; import bjc.pratt.ParserContext; +import bjc.pratt.commands.CommandResult; +import bjc.pratt.commands.CommandResult.Status; import bjc.pratt.tokens.Token; import bjc.data.Tree; import bjc.data.SimpleTree; @@ -51,9 +53,11 @@ public class ChainParseBlock<K, V, C> implements ParseBlock<K, V, C> { } @Override - public Tree<Token<K, V>> parse(ParserContext<K, V, C> ctx) throws ParserException { - Tree<Token<K, V>> expression = iner.parse(ctx); - + public CommandResult<K, V> parse(ParserContext<K, V, C> ctx) throws ParserException { + CommandResult<K,V> resOuter = iner.parse(ctx); + if (resOuter.status != Status.SUCCESS) return resOuter; + + Tree<Token<K, V>> expression = resOuter.success(); Token<K, V> currentToken = ctx.tokens.current(); if(indicators.contains(currentToken.getKey())) { Tree<Token<K, V>> res = new SimpleTree<>(trm); @@ -63,16 +67,19 @@ public class ChainParseBlock<K, V, C> implements ParseBlock<K, V, C> { res.addChild(new SimpleTree<>(currentToken)); ctx.tokens.next(); - Tree<Token<K, V>> innerExpression = iner.parse(ctx); + CommandResult<K,V> resInner = iner.parse(ctx); + if (resInner.status != Status.SUCCESS) return resInner; + + Tree<Token<K, V>> innerExpression = resInner.success(); res.addChild(innerExpression); currentToken = ctx.tokens.current(); } - return res; + return CommandResult.success(res); } - return expression; + return resOuter; } } 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 <K> - * The key type of the outer tokens. + * @param <K> The key type of the outer tokens. * - * @param <V> - * The value type of the outer tokens. + * @param <V> The value type of the outer tokens. * - * @param <C> - * The state type of the outer parser. + * @param <C> The state type of the outer parser. * - * @param <K2> - * The key type of the inner tokens. + * @param <K2> The key type of the inner tokens. * - * @param <V2> - * The value type of the inner tokens. + * @param <V2> The value type of the inner tokens. * - * @param <C2> - * The state type of the outer parser. + * @param <C2> The state type of the outer parser. */ public class GrammarParseBlock<K, V, C, K2, V2, C2> implements ParseBlock<K, V, C> { private final PrattParser<K2, V2, C2> innr; @@ -46,12 +42,14 @@ public class GrammarParseBlock<K, V, C, K2, V2, C2> implements ParseBlock<K, V, /** * Create a new grammar parser block. * - * @param inner The inner grammar to parse. - * @param precedence The precedence of the expression to parse. - * @param isStatement Is the expression being parsed in statement context? - * @param tokenTransform Function to transform to the new token type. - * @param stateTransform Function to toggle between state types. - * @param expressionTransform Function to transform back to the normal token type. + * @param inner The inner grammar to parse. + * @param precedence The precedence of the expression to parse. + * @param isStatement Is the expression being parsed in statement + * context? + * @param tokenTransform Function to transform to the new token type. + * @param stateTransform Function to toggle between state types. + * @param expressionTransform Function to transform back to the normal token + * type. */ public GrammarParseBlock(final PrattParser<K2, V2, C2> inner, final int precedence, final boolean isStatement, final Function<TokenStream<K, V>, TokenStream<K2, V2>> tokenTransform, @@ -66,16 +64,27 @@ public class GrammarParseBlock<K, V, C, K2, V2, C2> implements ParseBlock<K, V, } @Override - public Tree<Token<K, V>> parse(final ParserContext<K, V, C> ctx) throws ParserException { + public CommandResult<K, V> parse(final ParserContext<K, V, C> ctx) throws ParserException { final C2 newState = stteTransform.to(ctx.state); final TokenStream<K2, V2> newTokens = tkenTransform.apply(ctx.tokens); - final Tree<Token<K2, V2>> expression = innr.parseExpression(prcedence, newTokens, newState, - isStatemnt); + final CommandResult<K2, V2> 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<Token<K2, V2>> expression = res.success(); ctx.state = stteTransform.from(newState); - return xpressionTransform.apply(expression); + return CommandResult.success(xpressionTransform.apply(expression)); } }
\ No newline at end of file diff --git a/JPratt/src/main/java/bjc/pratt/blocks/ParseBlock.java b/JPratt/src/main/java/bjc/pratt/blocks/ParseBlock.java index 81ba508..fede096 100644 --- a/JPratt/src/main/java/bjc/pratt/blocks/ParseBlock.java +++ b/JPratt/src/main/java/bjc/pratt/blocks/ParseBlock.java @@ -1,6 +1,7 @@ package bjc.pratt.blocks; import bjc.pratt.ParserContext; +import bjc.pratt.commands.CommandResult; import bjc.pratt.tokens.Token; import bjc.data.Tree; import bjc.utils.parserutils.ParserException; @@ -34,6 +35,5 @@ public interface ParseBlock<K, V, C> { * If something goes wrong during parsing, or the block fails * validation. */ - Tree<Token<K, V>> parse(ParserContext<K, V, C> ctx) throws ParserException; - + CommandResult<K, V> parse(ParserContext<K, V, C> ctx) throws ParserException; }
\ No newline at end of file diff --git a/JPratt/src/main/java/bjc/pratt/blocks/RepeatingParseBlock.java b/JPratt/src/main/java/bjc/pratt/blocks/RepeatingParseBlock.java index 4c21358..722e395 100644 --- a/JPratt/src/main/java/bjc/pratt/blocks/RepeatingParseBlock.java +++ b/JPratt/src/main/java/bjc/pratt/blocks/RepeatingParseBlock.java @@ -3,6 +3,8 @@ package bjc.pratt.blocks; import java.util.function.UnaryOperator; import bjc.pratt.ParserContext; +import bjc.pratt.commands.CommandResult; +import bjc.pratt.commands.CommandResult.Status; import bjc.pratt.tokens.Token; import bjc.data.Tree; import bjc.data.SimpleTree; @@ -72,13 +74,15 @@ public class RepeatingParseBlock<K, V, C> implements ParseBlock<K, V, C> { } @Override - public Tree<Token<K, V>> parse(final ParserContext<K, V, C> ctx) throws ParserException { + public CommandResult<K, V> parse(final ParserContext<K, V, C> ctx) throws ParserException { final Tree<Token<K, V>> ret = new SimpleTree<>(mark); Token<K, V> tok = ctx.tokens.current(); while(!tok.getKey().equals(term)) { - final Tree<Token<K, V>> kid = innerBlock.parse(ctx); + final CommandResult<K,V> resKid = innerBlock.parse(ctx); + if (resKid.status != Status.SUCCESS) return resKid; + Tree<Token<K, V>> kid = resKid.success(); ret.addChild(kid); tok = ctx.tokens.current(); @@ -90,7 +94,7 @@ public class RepeatingParseBlock<K, V, C> implements ParseBlock<K, V, C> { } } - return ret; + return CommandResult.success(ret); } } diff --git a/JPratt/src/main/java/bjc/pratt/blocks/SimpleParseBlock.java b/JPratt/src/main/java/bjc/pratt/blocks/SimpleParseBlock.java index 83e1d91..b674815 100644 --- a/JPratt/src/main/java/bjc/pratt/blocks/SimpleParseBlock.java +++ b/JPratt/src/main/java/bjc/pratt/blocks/SimpleParseBlock.java @@ -3,6 +3,8 @@ package bjc.pratt.blocks; import java.util.function.Predicate; import bjc.pratt.ParserContext; +import bjc.pratt.commands.CommandResult; +import bjc.pratt.commands.CommandResult.Status; import bjc.pratt.tokens.Token; import bjc.data.Tree; import bjc.utils.parserutils.ParserException; @@ -49,15 +51,18 @@ public class SimpleParseBlock<K, V, C> implements ParseBlock<K, V, C> { } @Override - public Tree<Token<K, V>> parse(final ParserContext<K, V, C> ctx) throws ParserException { - final Tree<Token<K, V>> res = ctx.parse.parseExpression(pow, ctx.tokens, ctx.state, false); - + public CommandResult<K, V> parse(final ParserContext<K, V, C> ctx) throws ParserException { + final CommandResult<K,V> resBlock = ctx.parse.parseExpression(pow, ctx.tokens, ctx.state, false); + if (resBlock.status != Status.SUCCESS) return resBlock; + + Tree<Token<K, V>> res = resBlock.success(); if(term != null) { ctx.tokens.expect(term); } - if(validatr == null || validatr.test(res)) return res; + if(validatr == null || validatr.test(res)) return CommandResult.success(res); + // TODO: Figure out the right way to handle error context w/ CommandResult throw new ParserException("Block failed validation"); } diff --git a/JPratt/src/main/java/bjc/pratt/blocks/TriggeredParseBlock.java b/JPratt/src/main/java/bjc/pratt/blocks/TriggeredParseBlock.java index bfe5ab3..df17595 100644 --- a/JPratt/src/main/java/bjc/pratt/blocks/TriggeredParseBlock.java +++ b/JPratt/src/main/java/bjc/pratt/blocks/TriggeredParseBlock.java @@ -3,6 +3,8 @@ package bjc.pratt.blocks; import java.util.function.UnaryOperator; import bjc.pratt.ParserContext; +import bjc.pratt.commands.CommandResult; +import bjc.pratt.commands.CommandResult.Status; import bjc.pratt.tokens.Token; import bjc.data.Tree; import bjc.utils.parserutils.ParserException; @@ -45,13 +47,15 @@ public class TriggeredParseBlock<K, V, C> implements ParseBlock<K, V, C> { } @Override - public Tree<Token<K, V>> parse(final ParserContext<K, V, C> ctx) throws ParserException { + public CommandResult<K, V> parse(final ParserContext<K, V, C> ctx) throws ParserException { final C newState = onEntr.apply(ctx.state); final ParserContext<K, V, C> newCtx = new ParserContext<>(ctx.tokens, ctx.parse, newState); - final Tree<Token<K, V>> res = sourc.parse(newCtx); + final CommandResult<K,V> res = sourc.parse(newCtx); + if (res.status != Status.SUCCESS) return res; + ctx.state = onExt.apply(newState); return res; |
