From 6b881e8833596d669fdee9525e064aea0c8946dc Mon Sep 17 00:00:00 2001 From: bculkin2442 Date: Tue, 11 Apr 2017 23:12:34 -0400 Subject: Add exceptions to sample lang. --- .../java/bjc/pratt/blocks/ChainParseBlock.java | 79 ++++++++++++++++++++++ .../bjc/pratt/commands/BlockNonInitialCommand.java | 75 -------------------- 2 files changed, 79 insertions(+), 75 deletions(-) create mode 100644 JPratt/src/main/java/bjc/pratt/blocks/ChainParseBlock.java delete 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/ChainParseBlock.java b/JPratt/src/main/java/bjc/pratt/blocks/ChainParseBlock.java new file mode 100644 index 0000000..1758c17 --- /dev/null +++ b/JPratt/src/main/java/bjc/pratt/blocks/ChainParseBlock.java @@ -0,0 +1,79 @@ +package bjc.pratt.blocks; + +import java.util.Set; + +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 {@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 + * The token key type. + * + * @param + * The token value type. + * + * @param + * The parser state type. + * + */ +public class ChainParseBlock implements ParseBlock { + private ParseBlock iner; + + private Set indicators; + + private Token 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 inner, Set chainIndicators, Token term) { + iner = inner; + indicators = chainIndicators; + trm = term; + } + + @Override + public ITree> parse(ParserContext ctx) throws ParserException { + ITree> expression = iner.parse(ctx); + + Token currentToken = ctx.tokens.current(); + if (indicators.contains(currentToken.getKey())) { + ITree> res = new Tree<>(trm); + res.addChild(expression); + + while (indicators.contains(currentToken.getKey())) { + res.addChild(new Tree<>(currentToken)); + ctx.tokens.next(); + + ITree> innerExpression = iner.parse(ctx); + res.addChild(innerExpression); + + currentToken = ctx.tokens.current(); + } + + return res; + } + + return expression; + } + +} diff --git a/JPratt/src/main/java/bjc/pratt/commands/BlockNonInitialCommand.java b/JPratt/src/main/java/bjc/pratt/commands/BlockNonInitialCommand.java deleted file mode 100644 index c361c73..0000000 --- a/JPratt/src/main/java/bjc/pratt/commands/BlockNonInitialCommand.java +++ /dev/null @@ -1,75 +0,0 @@ -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 final ParseBlock innr; - - private final int lftBind; - private final int nxtBind; - - private final 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(final ParseBlock inner, final int leftBind, final int rightBind, - final Token term) { - innr = inner; - - lftBind = leftBind; - nxtBind = rightBind; - - trm = term; - } - - @Override - public ITree> denote(final ITree> operand, final Token operator, - final ParserContext ctx) throws ParserException { - final 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 -- cgit v1.2.3