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 --- .../pratt/commands/impls/ChainCommand.java | 83 ++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 JPratt/src/main/java/com/ashardalon/pratt/commands/impls/ChainCommand.java (limited to 'JPratt/src/main/java/com/ashardalon/pratt/commands/impls/ChainCommand.java') diff --git a/JPratt/src/main/java/com/ashardalon/pratt/commands/impls/ChainCommand.java b/JPratt/src/main/java/com/ashardalon/pratt/commands/impls/ChainCommand.java new file mode 100644 index 0000000..5ba84ef --- /dev/null +++ b/JPratt/src/main/java/com/ashardalon/pratt/commands/impls/ChainCommand.java @@ -0,0 +1,83 @@ +package com.ashardalon.pratt.commands.impls; + +import java.util.Set; + +import com.ashardalon.pratt.ParserContext; +import com.ashardalon.pratt.commands.BinaryPostCommand; +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; + +/** + * Create a new chained operator. + * + * @author bjculkin + * + * @param + * The key type of the tokens. + * + * @param + * The value type of the tokens. + * + * @param + * The state type of the parser. + */ +public class ChainCommand extends BinaryPostCommand { + private final Set chainWith; + + private final Token chain; + + /** + * Create a new chained operator. + * + * @param precedence + * The precedence of this operator. + * + * @param chainSet + * The operators to chain with. + * + * @param chainMarker + * The token to use as the node in the AST. + */ + public ChainCommand(final int precedence, final Set chainSet, final Token chainMarker) { + super(precedence); + + chainWith = chainSet; + chain = chainMarker; + } + + @Override + public CommandResult denote(final Tree> operand, final Token operator, + final ParserContext ctx) throws ParserException { + CommandResult resOuter = ctx.parse.parseExpression(1 + leftBinding(), ctx.tokens, ctx.state, + false); + if (resOuter.status != Status.SUCCESS) return resOuter; + final Tree> tree = resOuter.success(); + + final Tree> res = new SimpleTree<>(operator, operand, tree); + + if(chainWith.contains(ctx.tokens.current().getKey())) { + final Token tok = ctx.tokens.current(); + ctx.tokens.next(); + + CommandResult resOther = denote(tree, tok, + new ParserContext<>(ctx.tokens, ctx.parse, ctx.state)); + if (resOther.status != Status.SUCCESS) return resOther; + + final Tree> other = resOther.success(); + + return CommandResult.success(new SimpleTree<>(chain, res, other)); + } + + return CommandResult.success(res); + } + + @Override + public int nextBinding() { + return leftBinding() - 1; + } +} \ No newline at end of file -- cgit v1.2.3