diff options
Diffstat (limited to 'BJC-Utils2/src/main/java/bjc/utils/parserutils/pratt/commands/PreTernaryCommand.java')
| -rw-r--r-- | BJC-Utils2/src/main/java/bjc/utils/parserutils/pratt/commands/PreTernaryCommand.java | 72 |
1 files changed, 51 insertions, 21 deletions
diff --git a/BJC-Utils2/src/main/java/bjc/utils/parserutils/pratt/commands/PreTernaryCommand.java b/BJC-Utils2/src/main/java/bjc/utils/parserutils/pratt/commands/PreTernaryCommand.java index 04604be..42d1a6e 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/parserutils/pratt/commands/PreTernaryCommand.java +++ b/BJC-Utils2/src/main/java/bjc/utils/parserutils/pratt/commands/PreTernaryCommand.java @@ -3,43 +3,73 @@ package bjc.utils.parserutils.pratt.commands; import bjc.utils.data.ITree; import bjc.utils.data.Tree; import bjc.utils.parserutils.ParserException; +import bjc.utils.parserutils.pratt.ParseBlock; import bjc.utils.parserutils.pratt.ParserContext; import bjc.utils.parserutils.pratt.Token; +/** + * A prefix ternary operator, like an if/then/else group. + * + * @author bjculkin + * + * @param <K> + * The key type of the tokens. + * + * @param <V> + * The value type of the tokens. + * + * @param <C> + * The state type of the parser. + */ public class PreTernaryCommand<K, V, C> extends AbstractInitialCommand<K, V, C> { - private int cond1; - private int block1; - private int block2; + private Token<K, V> term; - private K mark1; - private K mark2; + private ParseBlock<K, V, C> condBlock; - private Token<K, V> term; + private ParseBlock<K, V, C> opblock1; + private ParseBlock<K, V, C> opblock2; - public PreTernaryCommand(int cond1, int block1, int block2, K mark1, K mark2, Token<K, V> term) { + /** + * 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(ParseBlock<K, V, C> cond, ParseBlock<K, V, C> op1, ParseBlock<K, V, C> op2, + Token<K, V> term) { super(); - this.cond1 = cond1; - this.block1 = block1; - this.block2 = block2; - this.mark1 = mark1; - this.mark2 = mark2; + + 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"); + + this.condBlock = cond; + this.opblock1 = op1; + this.opblock2 = op2; + this.term = term; } - @SuppressWarnings("unchecked") @Override protected ITree<Token<K, V>> intNullDenotation(Token<K, V> operator, ParserContext<K, V, C> ctx) throws ParserException { - ITree<Token<K, V>> cond = ctx.parse.parseExpression(cond1, ctx.tokens, ctx.state, false); - - ctx.tokens.expect(mark1); - - ITree<Token<K, V>> fstBlock = ctx.parse.parseExpression(block1, ctx.tokens, ctx.state, false); + ITree<Token<K, V>> cond = condBlock.parse(ctx); - ctx.tokens.expect(mark2); + ITree<Token<K, V>> op1 = opblock1.parse(ctx); - ITree<Token<K, V>> sndBlock = ctx.parse.parseExpression(block2, ctx.tokens, ctx.state, false); + ITree<Token<K, V>> op2 = opblock2.parse(ctx); - return new Tree<>(term, cond, fstBlock, sndBlock); + return new Tree<>(term, cond, op1, op2); } }
\ No newline at end of file |
