summaryrefslogtreecommitdiff
path: root/JPratt/src/main/java/bjc/pratt/commands/impls/ChainCommand.java
blob: 2a9bf35c3c10e7c4c10174931216fa2eee477554 (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.impls;

import java.util.Set;

import bjc.pratt.ParserContext;
import bjc.pratt.commands.BinaryPostCommand;
import bjc.pratt.tokens.Token;
import bjc.utils.data.ITree;
import bjc.utils.data.Tree;
import bjc.utils.parserutils.ParserException;

/**
 * Create a new chained operator.
 *
 * @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 ChainCommand<K, V, C> extends BinaryPostCommand<K, V, C> {
	private final Set<K> chainWith;

	private final Token<K, V> 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<K> chainSet, final Token<K, V> chainMarker) {
		super(precedence);

		chainWith = chainSet;
		chain = chainMarker;
	}

	@Override
	public ITree<Token<K, V>> denote(final ITree<Token<K, V>> operand, final Token<K, V> operator,
			final ParserContext<K, V, C> ctx) throws ParserException {
		final ITree<Token<K, V>> tree = ctx.parse.parseExpression(1 + leftBinding(), ctx.tokens, ctx.state,
				false);

		final ITree<Token<K, V>> res = new Tree<>(operator, operand, tree);

		if (chainWith.contains(ctx.tokens.current().getKey())) {
			final Token<K, V> tok = ctx.tokens.current();
			ctx.tokens.next();

			final ITree<Token<K, V>> other = denote(tree, tok,
					new ParserContext<>(ctx.tokens, ctx.parse, ctx.state));

			return new Tree<>(chain, res, other);
		}

		return res;
	}

	@Override
	public int nextBinding() {
		return leftBinding() - 1;
	}
}