summaryrefslogtreecommitdiff
path: root/JPratt/src/main/java/bjc/utils/parserutils/pratt/blocks/TriggeredParseBlock.java
blob: fbfc61bbff06a670f588363a72784eb5d4e932e9 (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
package bjc.utils.parserutils.pratt.blocks;

import java.util.function.UnaryOperator;

import bjc.utils.data.ITree;
import bjc.utils.parserutils.ParserException;
import bjc.utils.parserutils.pratt.ParseBlock;
import bjc.utils.parserutils.pratt.ParserContext;
import bjc.utils.parserutils.pratt.Token;

/**
 * A parse block that can adjust the state before handling its context.
 * 
 * @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 TriggeredParseBlock<K, V, C> implements ParseBlock<K, V, C> {
	private UnaryOperator<C>	onEnter;
	private UnaryOperator<C>	onExit;

	private ParseBlock<K, V, C> source;

	/**
	 * Create a new triggered parse block.
	 * 
	 * @param onEnter
	 *                The action to fire before parsing the block.
	 * 
	 * @param onExit
	 *                The action to fire after parsing the block.
	 * 
	 * @param source
	 *                The block to use for parsing.
	 */
	public TriggeredParseBlock(UnaryOperator<C> onEnter, UnaryOperator<C> onExit, ParseBlock<K, V, C> source) {
		super();
		this.onEnter = onEnter;
		this.onExit = onExit;
		this.source = source;
	}

	@Override
	public ITree<Token<K, V>> parse(ParserContext<K, V, C> ctx) throws ParserException {
		C newState = onEnter.apply(ctx.state);

		ParserContext<K, V, C> newCtx = new ParserContext<>(ctx.tokens, ctx.parse, newState);

		ITree<Token<K, V>> res = source.parse(newCtx);

		ctx.state = onExit.apply(newState);

		return res;
	}

}