diff options
Diffstat (limited to 'JPratt/src/main/java/bjc/pratt/blocks')
3 files changed, 97 insertions, 15 deletions
diff --git a/JPratt/src/main/java/bjc/pratt/blocks/GrammarParseBlock.java b/JPratt/src/main/java/bjc/pratt/blocks/GrammarParseBlock.java new file mode 100644 index 0000000..459f83d --- /dev/null +++ b/JPratt/src/main/java/bjc/pratt/blocks/GrammarParseBlock.java @@ -0,0 +1,81 @@ +package bjc.pratt.blocks; + +import java.util.function.Function; + +import bjc.pratt.ParseBlock; +import bjc.pratt.ParserContext; +import bjc.pratt.PrattParser; +import bjc.pratt.Token; +import bjc.pratt.TokenStream; +import bjc.utils.data.ITree; +import bjc.utils.funcutils.Isomorphism; +import bjc.utils.parserutils.ParserException; + +/** + * A {@link ParseBlock} that parses an expression from a 'inner' grammar. + * + * @author bjculkin + * + * @param <K> + * The key type of the outer tokens. + * + * @param <V> + * The value type of the outer tokens. + * + * @param <C> + * The state type of the outer parser. + * + * @param <K2> + * The key type of the inner tokens. + * + * @param <V2> + * The value type of the inner tokens. + * + * @param <C2> + * The state type of the outer parser. + */ +public class GrammarParseBlock<K, V, C, K2, V2, C2> implements ParseBlock<K, V, C> { + private PrattParser<K2, V2, C2> inner; + + private int precedence; + private boolean isStatement; + + private Function<TokenStream<K, V>, TokenStream<K2, V2>> tokenTransform; + private Isomorphism<C, C2> stateTransform; + private Function<ITree<Token<K2, V2>>, ITree<Token<K, V>>> expressionTransform; + + /** + * Create a new grammar parser block. + * + * @param inner + * @param precedence + * @param isStatement + * @param tokenTransform + * @param stateTransform + * @param expressionTransform + */ + public GrammarParseBlock(PrattParser<K2, V2, C2> inner, int precedence, boolean isStatement, + Function<TokenStream<K, V>, TokenStream<K2, V2>> tokenTransform, + Isomorphism<C, C2> stateTransform, + Function<ITree<Token<K2, V2>>, ITree<Token<K, V>>> expressionTransform) { + this.inner = inner; + this.precedence = precedence; + this.isStatement = isStatement; + this.tokenTransform = tokenTransform; + this.stateTransform = stateTransform; + this.expressionTransform = expressionTransform; + } + + @Override + public ITree<Token<K, V>> parse(ParserContext<K, V, C> ctx) throws ParserException { + C2 newState = stateTransform.to(ctx.state); + + TokenStream<K2, V2> newTokens = tokenTransform.apply(ctx.tokens); + + ITree<Token<K2, V2>> expression = inner.parseExpression(precedence, newTokens, newState, isStatement); + + ctx.state = stateTransform.from(newState); + + return expressionTransform.apply(expression); + } +}
\ No newline at end of file diff --git a/JPratt/src/main/java/bjc/pratt/blocks/ParseBlocks.java b/JPratt/src/main/java/bjc/pratt/blocks/ParseBlocks.java index d236a71..a3e3147 100644 --- a/JPratt/src/main/java/bjc/pratt/blocks/ParseBlocks.java +++ b/JPratt/src/main/java/bjc/pratt/blocks/ParseBlocks.java @@ -1,11 +1,15 @@ package bjc.pratt.blocks; +import java.util.function.Function; import java.util.function.Predicate; import java.util.function.UnaryOperator; import bjc.pratt.ParseBlock; +import bjc.pratt.PrattParser; import bjc.pratt.Token; +import bjc.pratt.TokenStream; import bjc.utils.data.ITree; +import bjc.utils.funcutils.Isomorphism; /** * Utility class for creating common implementations of {@link ParseBlock} @@ -14,6 +18,10 @@ import bjc.utils.data.ITree; * */ public class ParseBlocks { + /* + * Grammar parse blocks are complex enough to not get a builder method. + */ + /** * Create a new repeating parse block. * @@ -21,17 +29,17 @@ public class ParseBlocks { * The parse block to repeat. * * @param delim - * The token type that seperates repetitions. + * The token type that separates repetitions. * * @param term - * The token type that terminates repititions. + * The token type that terminates repetitions. * * @param mark * The token to use as the node in the AST. * * @param action * The action to perform on the state after every - * repitition. + * repetition. * * @return A configured repeating parse block. */ @@ -79,6 +87,6 @@ public class ParseBlocks { */ public static <K, V, C> ParseBlock<K, V, C> simple(final int precedence, final K terminator, final Predicate<ITree<Token<K, V>>> validator) { - return new SimpleParseBlock<>(precedence, terminator, validator); + return new SimpleParseBlock<>(precedence, validator, terminator); } }
\ No newline at end of file diff --git a/JPratt/src/main/java/bjc/pratt/blocks/SimpleParseBlock.java b/JPratt/src/main/java/bjc/pratt/blocks/SimpleParseBlock.java index db94034..0fb5097 100644 --- a/JPratt/src/main/java/bjc/pratt/blocks/SimpleParseBlock.java +++ b/JPratt/src/main/java/bjc/pratt/blocks/SimpleParseBlock.java @@ -34,16 +34,14 @@ public class SimpleParseBlock<K, V, C> implements ParseBlock<K, V, C> { * * @param precedence * The precedence of this block. - * + * @param validator + * The predicate to apply to blocks. * @param terminator * The token type that terminates the block. If this is * null, don't check for a terminator. - * - * @param validator - * The predicate to apply to blocks. */ - public SimpleParseBlock(final int precedence, final K terminator, - final Predicate<ITree<Token<K, V>>> validator) { + public SimpleParseBlock(final int precedence, final Predicate<ITree<Token<K, V>>> validator, + final K terminator) { if (precedence < 0) throw new IllegalArgumentException("Precedence must be non-negative"); pow = precedence; @@ -92,9 +90,4 @@ public class SimpleParseBlock<K, V, C> implements ParseBlock<K, V, C> { return true; } - - @Override - public String toString() { - return String.format("ParseBlock [pow=%s, term='%s']", pow, term); - } }
\ No newline at end of file |
