summaryrefslogtreecommitdiff
path: root/JPratt/src/main/java/bjc/pratt/commands/PreTernaryCommand.java
blob: efa7872a66bcf6b91aa730d6ea4a633cedd48afd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package bjc.pratt.commands;

import bjc.pratt.ParseBlock;
import bjc.pratt.ParserContext;
import bjc.pratt.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 <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 Token<K, V> term;

	private ParseBlock<K, V, C> condBlock;

	private ParseBlock<K, V, C>	opblock1;
	private ParseBlock<K, V, C>	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(ParseBlock<K, V, C> cond, ParseBlock<K, V, C> op1, ParseBlock<K, V, C> op2,
			Token<K, V> 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");

		this.condBlock = cond;
		this.opblock1 = op1;
		this.opblock2 = op2;

		this.term = term;
	}

	@Override
	protected ITree<Token<K, V>> intNullDenotation(Token<K, V> operator, ParserContext<K, V, C> ctx)
			throws ParserException {
		ITree<Token<K, V>> cond = condBlock.parse(ctx);

		ITree<Token<K, V>> op1 = opblock1.parse(ctx);

		ITree<Token<K, V>> op2 = opblock2.parse(ctx);

		return new Tree<>(term, cond, op1, op2);
	}
}