From 15a2b29e48f134bc93cfd0a3d8512001e9242f3d Mon Sep 17 00:00:00 2001 From: Benjamin Culkin Date: Mon, 3 Jun 2024 17:33:53 -0400 Subject: Rename package to new domain Rename the package to the new domain --- .../ashardalon/pratt/blocks/ChainParseBlock.java | 86 ++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 JPratt/src/main/java/com/ashardalon/pratt/blocks/ChainParseBlock.java (limited to 'JPratt/src/main/java/com/ashardalon/pratt/blocks/ChainParseBlock.java') diff --git a/JPratt/src/main/java/com/ashardalon/pratt/blocks/ChainParseBlock.java b/JPratt/src/main/java/com/ashardalon/pratt/blocks/ChainParseBlock.java new file mode 100644 index 0000000..fe04070 --- /dev/null +++ b/JPratt/src/main/java/com/ashardalon/pratt/blocks/ChainParseBlock.java @@ -0,0 +1,86 @@ +package com.ashardalon.pratt.blocks; + +import java.util.Set; + +import com.ashardalon.pratt.ParserContext; +import com.ashardalon.pratt.commands.CommandResult; +import com.ashardalon.pratt.commands.CommandResult.Status; +import com.ashardalon.pratt.tokens.Token; + +import bjc.data.Tree; +import bjc.data.SimpleTree; +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 CommandResult parse(ParserContext ctx) throws ParserException { + CommandResult resOuter = iner.parse(ctx); + if (resOuter.status != Status.SUCCESS) return resOuter; + + Tree> expression = resOuter.success(); + Token currentToken = ctx.tokens.current(); + if(indicators.contains(currentToken.getKey())) { + Tree> res = new SimpleTree<>(trm); + res.addChild(expression); + + while(indicators.contains(currentToken.getKey())) { + res.addChild(new SimpleTree<>(currentToken)); + ctx.tokens.next(); + + CommandResult resInner = iner.parse(ctx); + if (resInner.status != Status.SUCCESS) return resInner; + + Tree> innerExpression = resInner.success(); + res.addChild(innerExpression); + + currentToken = ctx.tokens.current(); + } + + return CommandResult.success(res); + } + + return resOuter; + } + +} -- cgit v1.2.3