From 7a510ceb37780a7d0da37117a5cfce23c2919257 Mon Sep 17 00:00:00 2001 From: bculkin2442 Date: Mon, 10 Apr 2017 22:55:22 -0400 Subject: More work on parse blocks --- .../java/bjc/pratt/blocks/GrammarParseBlock.java | 81 ++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 JPratt/src/main/java/bjc/pratt/blocks/GrammarParseBlock.java (limited to 'JPratt/src/main/java/bjc/pratt/blocks/GrammarParseBlock.java') 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 + * The key type of the outer tokens. + * + * @param + * The value type of the outer tokens. + * + * @param + * The state type of the outer parser. + * + * @param + * The key type of the inner tokens. + * + * @param + * The value type of the inner tokens. + * + * @param + * The state type of the outer parser. + */ +public class GrammarParseBlock implements ParseBlock { + private PrattParser inner; + + private int precedence; + private boolean isStatement; + + private Function, TokenStream> tokenTransform; + private Isomorphism stateTransform; + private Function>, ITree>> expressionTransform; + + /** + * Create a new grammar parser block. + * + * @param inner + * @param precedence + * @param isStatement + * @param tokenTransform + * @param stateTransform + * @param expressionTransform + */ + public GrammarParseBlock(PrattParser inner, int precedence, boolean isStatement, + Function, TokenStream> tokenTransform, + Isomorphism stateTransform, + Function>, ITree>> expressionTransform) { + this.inner = inner; + this.precedence = precedence; + this.isStatement = isStatement; + this.tokenTransform = tokenTransform; + this.stateTransform = stateTransform; + this.expressionTransform = expressionTransform; + } + + @Override + public ITree> parse(ParserContext ctx) throws ParserException { + C2 newState = stateTransform.to(ctx.state); + + TokenStream newTokens = tokenTransform.apply(ctx.tokens); + + ITree> expression = inner.parseExpression(precedence, newTokens, newState, isStatement); + + ctx.state = stateTransform.from(newState); + + return expressionTransform.apply(expression); + } +} \ No newline at end of file -- cgit v1.2.3 From 3f74e1e25fd572adab34e53eb90edcf49404fbe5 Mon Sep 17 00:00:00 2001 From: bculkin2442 Date: Tue, 11 Apr 2017 21:57:37 -0400 Subject: Cleanup --- .../java/bjc/pratt/blocks/GrammarParseBlock.java | 59 +++++++++++----------- 1 file changed, 30 insertions(+), 29 deletions(-) (limited to 'JPratt/src/main/java/bjc/pratt/blocks/GrammarParseBlock.java') diff --git a/JPratt/src/main/java/bjc/pratt/blocks/GrammarParseBlock.java b/JPratt/src/main/java/bjc/pratt/blocks/GrammarParseBlock.java index 459f83d..b714940 100644 --- a/JPratt/src/main/java/bjc/pratt/blocks/GrammarParseBlock.java +++ b/JPratt/src/main/java/bjc/pratt/blocks/GrammarParseBlock.java @@ -13,40 +13,40 @@ import bjc.utils.parserutils.ParserException; /** * A {@link ParseBlock} that parses an expression from a 'inner' grammar. - * + * * @author bjculkin * * @param * The key type of the outer tokens. - * + * * @param * The value type of the outer tokens. - * + * * @param * The state type of the outer parser. - * + * * @param * The key type of the inner tokens. - * + * * @param * The value type of the inner tokens. - * + * * @param * The state type of the outer parser. */ public class GrammarParseBlock implements ParseBlock { - private PrattParser inner; + private final PrattParser innr; - private int precedence; - private boolean isStatement; + private final int prcedence; + private final boolean isStatemnt; - private Function, TokenStream> tokenTransform; - private Isomorphism stateTransform; - private Function>, ITree>> expressionTransform; + private final Function, TokenStream> tkenTransform; + private final Isomorphism stteTransform; + private final Function>, ITree>> xpressionTransform; /** * Create a new grammar parser block. - * + * * @param inner * @param precedence * @param isStatement @@ -54,28 +54,29 @@ public class GrammarParseBlock implements ParseBlock inner, int precedence, boolean isStatement, - Function, TokenStream> tokenTransform, - Isomorphism stateTransform, - Function>, ITree>> expressionTransform) { - this.inner = inner; - this.precedence = precedence; - this.isStatement = isStatement; - this.tokenTransform = tokenTransform; - this.stateTransform = stateTransform; - this.expressionTransform = expressionTransform; + public GrammarParseBlock(final PrattParser inner, final int precedence, final boolean isStatement, + final Function, TokenStream> tokenTransform, + final Isomorphism stateTransform, + final Function>, ITree>> expressionTransform) { + innr = inner; + prcedence = precedence; + isStatemnt = isStatement; + tkenTransform = tokenTransform; + stteTransform = stateTransform; + xpressionTransform = expressionTransform; } @Override - public ITree> parse(ParserContext ctx) throws ParserException { - C2 newState = stateTransform.to(ctx.state); + public ITree> parse(final ParserContext ctx) throws ParserException { + final C2 newState = stteTransform.to(ctx.state); - TokenStream newTokens = tokenTransform.apply(ctx.tokens); + final TokenStream newTokens = tkenTransform.apply(ctx.tokens); - ITree> expression = inner.parseExpression(precedence, newTokens, newState, isStatement); + final ITree> expression = innr.parseExpression(prcedence, newTokens, newState, + isStatemnt); - ctx.state = stateTransform.from(newState); + ctx.state = stteTransform.from(newState); - return expressionTransform.apply(expression); + return xpressionTransform.apply(expression); } } \ No newline at end of file -- cgit v1.2.3 From f394306a4b65a3328551f9f6b8d4abff8bfd5b27 Mon Sep 17 00:00:00 2001 From: bjculkin Date: Wed, 12 Apr 2017 10:46:51 -0400 Subject: Package reorganization --- JPratt/src/main/java/bjc/pratt/blocks/GrammarParseBlock.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'JPratt/src/main/java/bjc/pratt/blocks/GrammarParseBlock.java') diff --git a/JPratt/src/main/java/bjc/pratt/blocks/GrammarParseBlock.java b/JPratt/src/main/java/bjc/pratt/blocks/GrammarParseBlock.java index b714940..6bf5582 100644 --- a/JPratt/src/main/java/bjc/pratt/blocks/GrammarParseBlock.java +++ b/JPratt/src/main/java/bjc/pratt/blocks/GrammarParseBlock.java @@ -2,11 +2,10 @@ 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.pratt.tokens.Token; +import bjc.pratt.tokens.TokenStream; import bjc.utils.data.ITree; import bjc.utils.funcutils.Isomorphism; import bjc.utils.parserutils.ParserException; -- cgit v1.2.3