1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
package net.sf.jmatchparser.template.engine.template.command.plain; |
19 | |
|
20 | |
import net.sf.jmatchparser.template.engine.operation.ForkOperation; |
21 | |
import net.sf.jmatchparser.template.engine.operation.JumpOperation; |
22 | |
import net.sf.jmatchparser.template.engine.operation.Label; |
23 | |
import net.sf.jmatchparser.template.engine.template.PlainBlockCommandState; |
24 | |
import net.sf.jmatchparser.template.engine.template.MatchTemplateImpl; |
25 | |
|
26 | |
public class AlternativeCommand extends ParameterlessCommand { |
27 | |
|
28 | |
private final boolean optional; |
29 | |
|
30 | 4 | public AlternativeCommand(boolean optional) { |
31 | 4 | this.optional = optional; |
32 | 4 | } |
33 | |
|
34 | |
@Override |
35 | |
protected PlainBlockCommandState parse(MatchTemplateImpl template) { |
36 | 65 | Label endLabel = new Label(), nextPartLabel = new Label(), forkLabel = new Label(); |
37 | 65 | if (optional) { |
38 | |
|
39 | |
|
40 | 32 | Label dummyEndLabel = new Label(); |
41 | 32 | Label dummyContLabel = new Label(); |
42 | 32 | template.appendOperation(new ForkOperation(template.getCurrentTemplatePosition(), dummyEndLabel, false)); |
43 | 32 | template.appendOperation(new JumpOperation(template.getCurrentTemplatePosition(), dummyContLabel)); |
44 | 32 | dummyEndLabel.setDestinationToNextCommand(template); |
45 | 32 | template.appendOperation(new JumpOperation(template.getCurrentTemplatePosition(), endLabel)); |
46 | 32 | dummyContLabel.setDestinationToNextCommand(template); |
47 | |
} |
48 | 65 | int lastForkPoint = template.getNextOperationIndex(); |
49 | 65 | template.appendOperation(new ForkOperation(template.getCurrentTemplatePosition(), forkLabel, false)); |
50 | 65 | template.appendOperation(new JumpOperation(template.getCurrentTemplatePosition(), nextPartLabel)); |
51 | 65 | forkLabel.setDestinationToNextCommand(template); |
52 | 65 | return new AlternativeCommandState(template, lastForkPoint, nextPartLabel, endLabel); |
53 | |
} |
54 | |
|
55 | |
private class AlternativeCommandState extends PlainBlockCommandState { |
56 | |
|
57 | |
private Label nextPartLabel; |
58 | |
private final Label endLabel; |
59 | |
private int lastForkPoint; |
60 | |
|
61 | |
public AlternativeCommandState(MatchTemplateImpl template, int lastForkPoint, |
62 | 65 | Label nextPartLabel, Label endLabel) { |
63 | 65 | super(template); |
64 | 65 | this.lastForkPoint = lastForkPoint; |
65 | 65 | this.nextPartLabel = nextPartLabel; |
66 | 65 | this.endLabel = endLabel; |
67 | 65 | } |
68 | |
|
69 | |
@Override |
70 | |
public boolean canParse(String commandName) { |
71 | 124 | return commandName.equals("nextalternative") || commandName.equals("nextalt") || commandName.equals("endalternative") || commandName.equals("endalt"); |
72 | |
} |
73 | |
|
74 | |
@Override |
75 | |
public PlainBlockCommandState parseCommand(MatchTemplateImpl template, |
76 | |
String commandName, String parameters) { |
77 | 124 | if (commandName.startsWith("end")) { |
78 | 64 | template.nopOperation(lastForkPoint); |
79 | 64 | template.nopOperation(lastForkPoint + 1); |
80 | 64 | endLabel.setDestinationToNextCommand(template); |
81 | 64 | return null; |
82 | |
} else { |
83 | 60 | template.appendOperation(new JumpOperation(template.getCurrentTemplatePosition(), endLabel)); |
84 | 60 | nextPartLabel.setDestinationToNextCommand(template); |
85 | 60 | nextPartLabel = new Label(); |
86 | 60 | Label forkLabel = new Label(); |
87 | 60 | lastForkPoint = template.getNextOperationIndex(); |
88 | 60 | template.appendOperation(new ForkOperation(template.getCurrentTemplatePosition(), forkLabel, false)); |
89 | 60 | template.appendOperation(new JumpOperation(template.getCurrentTemplatePosition(), nextPartLabel)); |
90 | 60 | forkLabel.setDestinationToNextCommand(template); |
91 | 60 | return this; |
92 | |
} |
93 | |
} |
94 | |
} |
95 | |
} |