diff options
Diffstat (limited to 'JPratt/src/main/java/bjc/pratt/blocks')
7 files changed, 286 insertions, 94 deletions
diff --git a/JPratt/src/main/java/bjc/pratt/blocks/ChainParseBlock.java b/JPratt/src/main/java/bjc/pratt/blocks/ChainParseBlock.java new file mode 100644 index 0000000..5c728d9 --- /dev/null +++ b/JPratt/src/main/java/bjc/pratt/blocks/ChainParseBlock.java @@ -0,0 +1,78 @@ +package bjc.pratt.blocks; + +import java.util.Set; + +import bjc.pratt.ParserContext; +import bjc.pratt.tokens.Token; +import bjc.utils.data.ITree; +import bjc.utils.data.Tree; +import bjc.utils.parserutils.ParserException; + +/** + * A {@link ParseBlock} for a series of parse blocks, linked by a set of tokens. + * + * Roughly analogous to Perl 6s list associative operators. + * + * @author bjculkin + * + * @param <K> + * The token key type. + * + * @param <V> + * The token value type. + * + * @param <C> + * The parser state type. + * + */ +public class ChainParseBlock<K, V, C> implements ParseBlock<K, V, C> { + private ParseBlock<K, V, C> iner; + + private Set<K> indicators; + + private Token<K, V> trm; + + /** + * Create a new chain parser block. + * + * @param inner + * The block for the chains interior. + * + * @param chainIndicators + * The set of markers that indicate continuing the chain + * + * @param term + * The node in the AST for the expression. + */ + public ChainParseBlock(ParseBlock<K, V, C> inner, Set<K> chainIndicators, Token<K, V> term) { + iner = inner; + indicators = chainIndicators; + trm = term; + } + + @Override + public ITree<Token<K, V>> parse(ParserContext<K, V, C> ctx) throws ParserException { + ITree<Token<K, V>> expression = iner.parse(ctx); + + Token<K, V> currentToken = ctx.tokens.current(); + if (indicators.contains(currentToken.getKey())) { + ITree<Token<K, V>> res = new Tree<>(trm); + res.addChild(expression); + + while (indicators.contains(currentToken.getKey())) { + res.addChild(new Tree<>(currentToken)); + ctx.tokens.next(); + + ITree<Token<K, V>> innerExpression = iner.parse(ctx); + res.addChild(innerExpression); + + currentToken = ctx.tokens.current(); + } + + return res; + } + + return expression; + } + +} 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..6bf5582 --- /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.ParserContext; +import bjc.pratt.PrattParser; +import bjc.pratt.tokens.Token; +import bjc.pratt.tokens.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 final PrattParser<K2, V2, C2> innr; + + private final int prcedence; + private final boolean isStatemnt; + + private final Function<TokenStream<K, V>, TokenStream<K2, V2>> tkenTransform; + private final Isomorphism<C, C2> stteTransform; + private final Function<ITree<Token<K2, V2>>, ITree<Token<K, V>>> xpressionTransform; + + /** + * Create a new grammar parser block. + * + * @param inner + * @param precedence + * @param isStatement + * @param tokenTransform + * @param stateTransform + * @param expressionTransform + */ + public GrammarParseBlock(final PrattParser<K2, V2, C2> inner, final int precedence, final boolean isStatement, + final Function<TokenStream<K, V>, TokenStream<K2, V2>> tokenTransform, + final Isomorphism<C, C2> stateTransform, + final Function<ITree<Token<K2, V2>>, ITree<Token<K, V>>> expressionTransform) { + innr = inner; + prcedence = precedence; + isStatemnt = isStatement; + tkenTransform = tokenTransform; + stteTransform = stateTransform; + xpressionTransform = expressionTransform; + } + + @Override + public ITree<Token<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 ITree<Token<K2, V2>> expression = innr.parseExpression(prcedence, newTokens, newState, + isStatemnt); + + ctx.state = stteTransform.from(newState); + + return 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 new file mode 100644 index 0000000..2fddac0 --- /dev/null +++ b/JPratt/src/main/java/bjc/pratt/blocks/ParseBlock.java @@ -0,0 +1,39 @@ +package bjc.pratt.blocks; + +import bjc.pratt.ParserContext; +import bjc.pratt.tokens.Token; +import bjc.utils.data.ITree; +import bjc.utils.parserutils.ParserException; + +/** + * Represents a embedded block in an expression. + * + * @author bjculkin + * + * @param <K> + * The key type of the token. + * + * @param <V> + * The value type of the token. + * + * @param <C> + * The state type of the parser. + */ +@FunctionalInterface +public interface ParseBlock<K, V, C> { + + /** + * Parse the block this represents. + * + * @param ctx + * The context for parsing. + * + * @return A AST for this block. + * + * @throws ParserException + * If something goes wrong during parsing, or the block + * fails validation. + */ + ITree<Token<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/ParseBlocks.java b/JPratt/src/main/java/bjc/pratt/blocks/ParseBlocks.java index e0dea48..21fa7e1 100644 --- a/JPratt/src/main/java/bjc/pratt/blocks/ParseBlocks.java +++ b/JPratt/src/main/java/bjc/pratt/blocks/ParseBlocks.java @@ -3,82 +3,85 @@ package bjc.pratt.blocks; import java.util.function.Predicate; import java.util.function.UnaryOperator; -import bjc.pratt.ParseBlock; -import bjc.pratt.Token; +import bjc.pratt.tokens.Token; import bjc.utils.data.ITree; /** * Utility class for creating common implementations of {@link ParseBlock} - * + * * @author bjculkin * */ public class ParseBlocks { + /* + * Grammar parse blocks are complex enough to not get a builder method. + */ + /** * Create a new repeating parse block. - * + * * @param inner * 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. */ - public static <K, V, C> ParseBlock<K, V, C> repeating(ParseBlock<K, V, C> inner, K delim, K term, - Token<K, V> mark, UnaryOperator<C> action) { + public static <K, V, C> ParseBlock<K, V, C> repeating(final ParseBlock<K, V, C> inner, final K delim, + final K term, final Token<K, V> mark, final UnaryOperator<C> action) { return new RepeatingParseBlock<>(inner, delim, term, mark, action); } /** * Create a new triggered parse block. - * + * * @param source * The block to trigger around. - * + * * @param onEnter * The action to perform upon the state before entering * the block. - * + * * @param onExit * The action to perform upon the state after exiting the * block. - * + * * @return A configured trigger parse block. */ - public static <K, V, C> ParseBlock<K, V, C> trigger(ParseBlock<K, V, C> source, UnaryOperator<C> onEnter, - UnaryOperator<C> onExit) { + public static <K, V, C> ParseBlock<K, V, C> trigger(final ParseBlock<K, V, C> source, + final UnaryOperator<C> onEnter, final UnaryOperator<C> onExit) { return new TriggeredParseBlock<>(onEnter, onExit, source); } /** * Create a new simple parse block. - * + * * @param precedence * The precedence of the expression inside the block. - * + * * @param terminator * The key type of the token expected after this block, * or null if none is expected. - * + * * @param validator * The predicate to use to validate parsed expressions, * or null if none is used. - * + * * @return A configured simple parse block. */ - public static <K, V, C> ParseBlock<K, V, C> simple(int precedence, K terminator, - Predicate<ITree<Token<K, V>>> validator) { - return new SimpleParseBlock<>(precedence, terminator, validator); + 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, validator, terminator); } }
\ 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 1c82b36..4146648 100644 --- a/JPratt/src/main/java/bjc/pratt/blocks/RepeatingParseBlock.java +++ b/JPratt/src/main/java/bjc/pratt/blocks/RepeatingParseBlock.java @@ -2,9 +2,8 @@ package bjc.pratt.blocks; import java.util.function.UnaryOperator; -import bjc.pratt.ParseBlock; import bjc.pratt.ParserContext; -import bjc.pratt.Token; +import bjc.pratt.tokens.Token; import bjc.utils.data.ITree; import bjc.utils.data.Tree; import bjc.utils.parserutils.ParserException; @@ -12,49 +11,49 @@ import bjc.utils.parserutils.ParserException; /** * A parse block that can parse a sequnce of zero or more occurances of another * block. - * + * * @author bjculkin * * @param <K> * The key type of the tokens. - * + * * @param <V> * The value type of the tokens. - * + * * @param <C> * The state type of the parser. */ public class RepeatingParseBlock<K, V, C> implements ParseBlock<K, V, C> { - private ParseBlock<K, V, C> innerBlock; + private final ParseBlock<K, V, C> innerBlock; - private K delim; - private K term; + private final K delim; + private final K term; - private UnaryOperator<C> onDelim; + private final UnaryOperator<C> onDelim; - private Token<K, V> mark; + private final Token<K, V> mark; /** * Create a new repeating block. - * + * * @param inner * The inner block for elements. - * + * * @param delimiter * The token that delimits elements in the sequence. - * + * * @param terminator * The token that terminates the sequence. - * + * * @param marker * The token to use as the node in the AST. - * + * * @param action * The action to apply to the state after every * delimiter. */ - public RepeatingParseBlock(ParseBlock<K, V, C> inner, K delimiter, K terminator, Token<K, V> marker, - UnaryOperator<C> action) { + public RepeatingParseBlock(final ParseBlock<K, V, C> inner, final K delimiter, final K terminator, + final Token<K, V> marker, final UnaryOperator<C> action) { super(); if (inner == null) @@ -74,20 +73,22 @@ public class RepeatingParseBlock<K, V, C> implements ParseBlock<K, V, C> { } @Override - public ITree<Token<K, V>> parse(ParserContext<K, V, C> ctx) throws ParserException { - ITree<Token<K, V>> ret = new Tree<>(mark); + public ITree<Token<K, V>> parse(final ParserContext<K, V, C> ctx) throws ParserException { + final ITree<Token<K, V>> ret = new Tree<>(mark); Token<K, V> tok = ctx.tokens.current(); while (!tok.getKey().equals(term)) { - ITree<Token<K, V>> kid = innerBlock.parse(ctx); + final ITree<Token<K, V>> kid = innerBlock.parse(ctx); ret.addChild(kid); tok = ctx.tokens.current(); ctx.tokens.expect(delim, term); - if (onDelim != null) ctx.state = onDelim.apply(ctx.state); + if (onDelim != null) { + ctx.state = onDelim.apply(ctx.state); + } } return ret; diff --git a/JPratt/src/main/java/bjc/pratt/blocks/SimpleParseBlock.java b/JPratt/src/main/java/bjc/pratt/blocks/SimpleParseBlock.java index acddd3b..1ff561c 100644 --- a/JPratt/src/main/java/bjc/pratt/blocks/SimpleParseBlock.java +++ b/JPratt/src/main/java/bjc/pratt/blocks/SimpleParseBlock.java @@ -2,47 +2,45 @@ package bjc.pratt.blocks; import java.util.function.Predicate; -import bjc.pratt.ParseBlock; import bjc.pratt.ParserContext; -import bjc.pratt.Token; +import bjc.pratt.tokens.Token; import bjc.utils.data.ITree; import bjc.utils.parserutils.ParserException; /** * Simple implementation of {@link ParseBlock} - * + * * @author bjculkin * * @param <K> * The key type of the tokens. - * + * * @param <V> * The value type of the tokens. - * + * * @param <C> * The state type of the parser. */ public class SimpleParseBlock<K, V, C> implements ParseBlock<K, V, C> { - private int pow; + private final int pow; - private K term; + private final K term; - private Predicate<ITree<Token<K, V>>> validatr; + private final Predicate<ITree<Token<K, V>>> validatr; /** * Create a new block. - * + * * @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(int precedence, K terminator, 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; @@ -51,16 +49,14 @@ public class SimpleParseBlock<K, V, C> implements ParseBlock<K, V, C> { } @Override - public ITree<Token<K, V>> parse(ParserContext<K, V, C> ctx) throws ParserException { - ITree<Token<K, V>> res = ctx.parse.parseExpression(pow, ctx.tokens, ctx.state, false); + public ITree<Token<K, V>> parse(final ParserContext<K, V, C> ctx) throws ParserException { + final ITree<Token<K, V>> res = ctx.parse.parseExpression(pow, ctx.tokens, ctx.state, false); if (term != null) { ctx.tokens.expect(term); } - if (validatr == null || validatr.test(res)) { - return res; - } + if (validatr == null || validatr.test(res)) return res; throw new ParserException("Block failed validation"); } @@ -72,18 +68,18 @@ public class SimpleParseBlock<K, V, C> implements ParseBlock<K, V, C> { int result = 1; result = prime * result + pow; - result = prime * result + ((term == null) ? 0 : term.hashCode()); + result = prime * result + (term == null ? 0 : term.hashCode()); return result; } @Override - public boolean equals(Object obj) { + public boolean equals(final Object obj) { if (this == obj) return true; if (obj == null) return false; if (!(obj instanceof SimpleParseBlock)) return false; - SimpleParseBlock<?, ?, ?> other = (SimpleParseBlock<?, ?, ?>) obj; + final SimpleParseBlock<?, ?, ?> other = (SimpleParseBlock<?, ?, ?>) obj; if (pow != other.pow) return false; @@ -93,9 +89,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 diff --git a/JPratt/src/main/java/bjc/pratt/blocks/TriggeredParseBlock.java b/JPratt/src/main/java/bjc/pratt/blocks/TriggeredParseBlock.java index 5e561fc..844a4f8 100644 --- a/JPratt/src/main/java/bjc/pratt/blocks/TriggeredParseBlock.java +++ b/JPratt/src/main/java/bjc/pratt/blocks/TriggeredParseBlock.java @@ -2,15 +2,14 @@ package bjc.pratt.blocks; import java.util.function.UnaryOperator; -import bjc.pratt.ParseBlock; import bjc.pratt.ParserContext; -import bjc.pratt.Token; +import bjc.pratt.tokens.Token; import bjc.utils.data.ITree; import bjc.utils.parserutils.ParserException; /** * A parse block that can adjust the state before handling its context. - * + * * @author bjculkin * * @param <K> @@ -21,39 +20,39 @@ import bjc.utils.parserutils.ParserException; * The state type of the parser. */ public class TriggeredParseBlock<K, V, C> implements ParseBlock<K, V, C> { - private UnaryOperator<C> onEnter; - private UnaryOperator<C> onExit; + private final UnaryOperator<C> onEntr; + private final UnaryOperator<C> onExt; - private ParseBlock<K, V, C> source; + private final ParseBlock<K, V, C> sourc; /** * Create a new triggered parse block. - * + * * @param onEnter * The action to fire before parsing the block. - * + * * @param onExit * The action to fire after parsing the block. - * + * * @param source * The block to use for parsing. */ - public TriggeredParseBlock(UnaryOperator<C> onEnter, UnaryOperator<C> onExit, ParseBlock<K, V, C> source) { - super(); - this.onEnter = onEnter; - this.onExit = onExit; - this.source = source; + public TriggeredParseBlock(final UnaryOperator<C> onEnter, final UnaryOperator<C> onExit, + final ParseBlock<K, V, C> source) { + onEntr = onEnter; + onExt = onExit; + sourc = source; } @Override - public ITree<Token<K, V>> parse(ParserContext<K, V, C> ctx) throws ParserException { - C newState = onEnter.apply(ctx.state); + public ITree<Token<K, V>> parse(final ParserContext<K, V, C> ctx) throws ParserException { + final C newState = onEntr.apply(ctx.state); - ParserContext<K, V, C> newCtx = new ParserContext<>(ctx.tokens, ctx.parse, newState); + final ParserContext<K, V, C> newCtx = new ParserContext<>(ctx.tokens, ctx.parse, newState); - ITree<Token<K, V>> res = source.parse(newCtx); + final ITree<Token<K, V>> res = sourc.parse(newCtx); - ctx.state = onExit.apply(newState); + ctx.state = onExt.apply(newState); return res; } |
