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 ++++++++++++++++++++++ .../main/java/bjc/pratt/blocks/ParseBlocks.java | 16 +++-- .../java/bjc/pratt/blocks/SimpleParseBlock.java | 15 ++-- .../bjc/pratt/commands/BlockInitialCommand.java | 41 +++++++++++ .../bjc/pratt/commands/BlockNonInitialCommand.java | 74 ++++++++++++++++++++ .../java/bjc/pratt/commands/InitialCommands.java | 2 +- .../bjc/pratt/commands/NonInitialCommands.java | 4 +- 7 files changed, 215 insertions(+), 18 deletions(-) create mode 100644 JPratt/src/main/java/bjc/pratt/blocks/GrammarParseBlock.java create mode 100644 JPratt/src/main/java/bjc/pratt/commands/BlockInitialCommand.java create mode 100644 JPratt/src/main/java/bjc/pratt/commands/BlockNonInitialCommand.java (limited to 'JPratt/src/main/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 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 ParseBlock simple(final int precedence, final K terminator, final Predicate>> 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 implements ParseBlock { * * @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>> validator) { + public SimpleParseBlock(final int precedence, final Predicate>> validator, + final K terminator) { if (precedence < 0) throw new IllegalArgumentException("Precedence must be non-negative"); pow = precedence; @@ -92,9 +90,4 @@ public class SimpleParseBlock implements ParseBlock { 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/commands/BlockInitialCommand.java b/JPratt/src/main/java/bjc/pratt/commands/BlockInitialCommand.java new file mode 100644 index 0000000..f0448f7 --- /dev/null +++ b/JPratt/src/main/java/bjc/pratt/commands/BlockInitialCommand.java @@ -0,0 +1,41 @@ +package bjc.pratt.commands; + +import bjc.pratt.ParseBlock; +import bjc.pratt.ParserContext; +import bjc.pratt.Token; +import bjc.utils.data.ITree; +import bjc.utils.parserutils.ParserException; + +/** + * An initial command that delegates all the work to a {@link ParseBlock} + * + * @author bjculkin + * @param + * The token key type. + * + * @param + * The token value type. + * + * @param + * The parser state type. + * + */ +public class BlockInitialCommand extends AbstractInitialCommand { + private ParseBlock blck; + + /** + * Create a new block initial command. + * + * @param block + * The block to delegate to. + */ + public BlockInitialCommand(ParseBlock block) { + blck = block; + } + + @Override + protected ITree> intNullDenotation(Token operator, ParserContext ctx) + throws ParserException { + return blck.parse(ctx); + } +} \ No newline at end of file diff --git a/JPratt/src/main/java/bjc/pratt/commands/BlockNonInitialCommand.java b/JPratt/src/main/java/bjc/pratt/commands/BlockNonInitialCommand.java new file mode 100644 index 0000000..9a5ffc9 --- /dev/null +++ b/JPratt/src/main/java/bjc/pratt/commands/BlockNonInitialCommand.java @@ -0,0 +1,74 @@ +package bjc.pratt.commands; + +import bjc.pratt.NonInitialCommand; +import bjc.pratt.ParseBlock; +import bjc.pratt.ParserContext; +import bjc.pratt.Token; +import bjc.utils.data.ITree; +import bjc.utils.data.Tree; +import bjc.utils.parserutils.ParserException; + +/** + * A non-initial command that delegates all of the work to a {@link ParseBlock} + * + * @author bjculkin + * + * @param + * The token key type. + * + * @param + * The token value type. + * + * @param + * The parser state type. + */ +public class BlockNonInitialCommand extends NonInitialCommand { + private ParseBlock innr; + + private int lftBind; + private int nxtBind; + + private Token trm; + + /** + * Create a new non-initial command that delegates to a parse block. + * + * @param inner + * The parse block to delegate to. + * + * @param leftBind + * The left binding power (precedence). + * + * @param rightBind + * The right binding power (associativity control). + * + * @param term + * The token to use as the node in the AST. + */ + public BlockNonInitialCommand(ParseBlock inner, int leftBind, int rightBind, Token term) { + innr = inner; + + lftBind = leftBind; + nxtBind = rightBind; + + trm = term; + } + + @Override + public ITree> denote(ITree> operand, Token operator, ParserContext ctx) + throws ParserException { + ITree> expression = innr.parse(ctx); + + return new Tree<>(trm, expression); + } + + @Override + public int leftBinding() { + return lftBind; + } + + @Override + public int nextBinding() { + return nxtBind; + } +} \ No newline at end of file diff --git a/JPratt/src/main/java/bjc/pratt/commands/InitialCommands.java b/JPratt/src/main/java/bjc/pratt/commands/InitialCommands.java index 5710277..b373a7c 100644 --- a/JPratt/src/main/java/bjc/pratt/commands/InitialCommands.java +++ b/JPratt/src/main/java/bjc/pratt/commands/InitialCommands.java @@ -168,4 +168,4 @@ public class InitialCommands { public static InitialCommand denest(final InitialCommand comm) { return new DenestingCommand<>(comm); } -} +} \ No newline at end of file diff --git a/JPratt/src/main/java/bjc/pratt/commands/NonInitialCommands.java b/JPratt/src/main/java/bjc/pratt/commands/NonInitialCommands.java index 48922b7..39baf1f 100644 --- a/JPratt/src/main/java/bjc/pratt/commands/NonInitialCommands.java +++ b/JPratt/src/main/java/bjc/pratt/commands/NonInitialCommands.java @@ -104,7 +104,7 @@ public class NonInitialCommands { */ public static NonInitialCommand postCircumfix(final int precedence, final int insidePrecedence, final K closer, final Token marker) { - final ParseBlock innerBlock = new SimpleParseBlock<>(insidePrecedence, closer, null); + final ParseBlock innerBlock = new SimpleParseBlock<>(insidePrecedence, null, closer); return new PostCircumfixCommand<>(precedence, innerBlock, marker); } @@ -134,7 +134,7 @@ public class NonInitialCommands { */ public static NonInitialCommand ternary(final int precedence, final int insidePrecedence, final K closer, final Token marker, final boolean nonassoc) { - final ParseBlock innerBlock = new SimpleParseBlock<>(insidePrecedence, closer, null); + final ParseBlock innerBlock = new SimpleParseBlock<>(insidePrecedence, null, closer); return new TernaryCommand<>(precedence, innerBlock, marker, nonassoc); } -- cgit v1.2.3