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.pratt.blocks;
import java.util.function.UnaryOperator;
import bjc.pratt.ParseBlock;
import bjc.pratt.ParserContext;
import bjc.pratt.Token;
import bjc.utils.data.ITree;
import bjc.utils.parserutils.ParserException;
/**
* 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;
}
}
|