package bjc.pratt.commands.impls; import bjc.data.SimpleTree; import bjc.data.Tree; import bjc.pratt.ParserContext; import bjc.pratt.commands.CommandResult; import bjc.pratt.commands.InitialCommand; import bjc.pratt.commands.CommandResult.Status; import bjc.pratt.tokens.Token; import bjc.utils.parserutils.ParserException; public final class PanfixCommand implements InitialCommand { private final Token marker; private final K term; private final int precedence; public PanfixCommand(Token marker, K term, int precedence) { this.marker = marker; this.term = term; this.precedence = precedence; } @Override public CommandResult denote(Token operator, ParserContext ctx) throws ParserException { CommandResult resLeftSide = ctx.parse.parseExpression(precedence + 1, ctx.tokens, ctx.state, false); if (resLeftSide.status != Status.SUCCESS) return resLeftSide; Tree> leftSide = resLeftSide.success(); ctx.tokens.expect(term); ctx.tokens.next(); CommandResult resRightSide = ctx.parse.parseExpression(precedence + 1, ctx.tokens, ctx.state, false); if (resLeftSide.status != Status.SUCCESS) return resRightSide; Tree> rightSide = resRightSide.success(); ctx.tokens.expect(term); ctx.tokens.next(); return CommandResult.success(new SimpleTree<>(marker, leftSide, rightSide)); } }