package bjc.pratt.commands.impls; import bjc.pratt.ParserContext; import bjc.pratt.blocks.ParseBlock; import bjc.pratt.commands.AbstractInitialCommand; import bjc.pratt.tokens.Token; import bjc.utils.data.ITree; import bjc.utils.data.Tree; import bjc.utils.parserutils.ParserException; /** * A prefix ternary operator, like an if/then/else group. * * @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 PreTernaryCommand extends AbstractInitialCommand { private final Token trm; private final ParseBlock condBlock; private final ParseBlock opblock1; private final ParseBlock opblock2; /** * Create a new ternary statement. * * @param cond * The block for handling the condition. * * @param op1 * The block for handling the first operator. * * @param op2 * The block for handling the second operator. * * @param term * The token to use as the node for the AST. */ public PreTernaryCommand(final ParseBlock cond, final ParseBlock op1, final ParseBlock op2, final Token term) { super(); if(cond == null) throw new NullPointerException("Cond block must not be null"); else if(op1 == null) throw new NullPointerException("Op block #1 must not be null"); else if(op2 == null) throw new NullPointerException("Op block #2 must not be null"); condBlock = cond; opblock1 = op1; opblock2 = op2; trm = term; } @Override protected ITree> intNullDenotation(final Token operator, final ParserContext ctx) throws ParserException { final ITree> cond = condBlock.parse(ctx); final ITree> op1 = opblock1.parse(ctx); final ITree> op2 = opblock2.parse(ctx); return new Tree<>(trm, cond, op1, op2); } }