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.CounterOperation; |
21 | |
import net.sf.jmatchparser.template.engine.operation.IncrementCounterOperation; |
22 | |
import net.sf.jmatchparser.template.engine.operation.JumpOperation; |
23 | |
import net.sf.jmatchparser.template.engine.operation.Label; |
24 | |
import net.sf.jmatchparser.template.engine.operation.LoopForkOperation; |
25 | |
import net.sf.jmatchparser.template.engine.parameter.Parameter; |
26 | |
import net.sf.jmatchparser.template.engine.template.PlainBlockCommandState; |
27 | |
import net.sf.jmatchparser.template.engine.template.MatchTemplateImpl; |
28 | |
|
29 | 1 | public class LoopCommand extends Command { |
30 | |
|
31 | |
@Override |
32 | |
public PlainBlockCommandState parse(MatchTemplateImpl template, String parameters) { |
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | 22 | Parameter[] params = Parameter.parseParameters(parameters, 0, 2, 1, template.getSpecialTags()); |
38 | 22 | if (params[0] == null) |
39 | 12 | params[0] = Parameter.getLiteralParameter("0"); |
40 | 22 | Label startLabel = new Label(), endLabel = new Label(), forkLabel = new Label(); |
41 | 22 | CounterOperation co = new CounterOperation(template.getCurrentTemplatePosition()); |
42 | 22 | template.appendOperation(co); |
43 | 22 | startLabel.setDestinationToNextCommand(template); |
44 | 22 | template.appendOperation(new LoopForkOperation(template.getCurrentTemplatePosition(), forkLabel, co, params[0], params[1])); |
45 | 22 | template.appendOperation(new JumpOperation(template.getCurrentTemplatePosition(), endLabel)); |
46 | 22 | forkLabel.setDestinationToNextCommand(template); |
47 | 22 | return new LoopCommandState(template, co, startLabel, endLabel); |
48 | |
} |
49 | |
|
50 | 1 | private class LoopCommandState extends PlainBlockCommandState { |
51 | |
|
52 | |
private final CounterOperation co; |
53 | |
private final Label startLabel; |
54 | |
private final Label endLabel; |
55 | |
|
56 | 22 | public LoopCommandState(MatchTemplateImpl template, CounterOperation co, Label startLabel, Label endLabel) { |
57 | 22 | super(template); |
58 | 22 | this.co = co; |
59 | 22 | this.startLabel = startLabel; |
60 | 22 | this.endLabel = endLabel; |
61 | 22 | } |
62 | |
|
63 | |
@Override |
64 | |
public boolean canParse(String commandName) { |
65 | 20 | return commandName.equals("endloop"); |
66 | |
} |
67 | |
|
68 | |
@Override |
69 | |
public PlainBlockCommandState parseCommand(MatchTemplateImpl template, |
70 | |
String commandName, String parameters) { |
71 | 20 | template.appendOperation(new IncrementCounterOperation(template.getCurrentTemplatePosition(), co)); |
72 | 20 | template.appendOperation(new JumpOperation(template.getCurrentTemplatePosition(), startLabel)); |
73 | 20 | endLabel.setDestinationToNextCommand(template); |
74 | 20 | return null; |
75 | |
} |
76 | |
} |
77 | |
} |