1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
package net.sf.jmatchparser.template.engine.operation; |
19 | |
|
20 | |
import net.sf.jmatchparser.template.engine.ParserState; |
21 | |
import net.sf.jmatchparser.template.engine.parameter.Parameter; |
22 | |
|
23 | |
public class LoopForkOperation extends Operation { |
24 | |
|
25 | |
private final Label label; |
26 | |
private final CounterOperation co; |
27 | |
private final Parameter mincount; |
28 | |
private final Parameter maxcount; |
29 | |
|
30 | |
public LoopForkOperation(String templatePosition, Label label, |
31 | |
CounterOperation co, Parameter mincount, Parameter maxcount) { |
32 | 22 | super(templatePosition); |
33 | 22 | this.label = label; |
34 | 22 | this.co = co; |
35 | 22 | this.mincount = mincount; |
36 | 22 | this.maxcount = maxcount; |
37 | 22 | } |
38 | |
|
39 | |
@Override |
40 | |
public OperationResult execute(ParserState state) { |
41 | 256 | int min = 0, max = Integer.MAX_VALUE; |
42 | |
try { |
43 | 256 | min = Integer.parseInt(mincount.getString(state)); |
44 | 0 | } catch (NumberFormatException ex) { |
45 | 256 | } |
46 | 256 | if (maxcount != null) { |
47 | |
try { |
48 | 29 | max = Integer.parseInt(maxcount.getString(state)); |
49 | 0 | } catch (NumberFormatException ex) { |
50 | 29 | } |
51 | |
} |
52 | 256 | if (min > max) { |
53 | 0 | int tmp = max; |
54 | 0 | max = min; |
55 | 0 | min = tmp; |
56 | |
} |
57 | 256 | int counter = co.getCounter(); |
58 | 256 | if (counter < max) { |
59 | |
|
60 | 254 | state.getParser().enqueueState(new ParserState(state, label.getDestination())); |
61 | |
} |
62 | 256 | if (counter < min) { |
63 | 30 | return OperationResult.DIE; |
64 | |
} |
65 | 226 | return OperationResult.CONTINUE; |
66 | |
} |
67 | |
} |