diff options
| author | Student <student@Administrators-iMac-2.local> | 2017-04-12 11:05:57 -0400 |
|---|---|---|
| committer | Student <student@Administrators-iMac-2.local> | 2017-04-12 11:05:57 -0400 |
| commit | 22c356cd411cf0fcc18d548291af26bc7588a3aa (patch) | |
| tree | 4f24fdda182b358ca96aed2249bb4e8a19994747 /JPratt/src/main/java/bjc/pratt/blocks/GrammarParseBlock.java | |
| parent | 2dc1b5dd145ab0e2b3e3df67f967a9c07ed6d303 (diff) | |
| parent | f394306a4b65a3328551f9f6b8d4abff8bfd5b27 (diff) | |
Merge branch 'master' of https://github.com/bculkin2442/JPratt.git
Diffstat (limited to 'JPratt/src/main/java/bjc/pratt/blocks/GrammarParseBlock.java')
| -rw-r--r-- | JPratt/src/main/java/bjc/pratt/blocks/GrammarParseBlock.java | 81 |
1 files changed, 81 insertions, 0 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..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 |
