blob: a7be1bb4da991ac4e5a047ddf45a36925470388f (
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
|
package bjc.rgens.parser.elements;
import bjc.utils.data.IPair;
import bjc.rgens.parser.GenerationState;
import bjc.rgens.parser.GrammarException;
import bjc.rgens.parser.RecurLimitException;
import bjc.rgens.parser.RGrammar;
import bjc.rgens.parser.Rule;
import bjc.rgens.parser.RuleCase;
public class RuleVarRefCaseElement extends StringCaseElement {
public RuleVarRefCaseElement(String vl) {
super(vl, false);
}
public void generate(GenerationState state) {
if(!state.rlVars.containsKey(val)) {
throw new GrammarException("No rule variable named " + val);
}
IPair<RGrammar, Rule> par = state.rlVars.get(val);
GenerationState newState = state.newBuf();
newState.swapGrammar(par.getLeft());
if(par.getRight().doRecur()) {
RuleCase cse = par.getRight().getCase(state.rnd);
System.err.printf("\tFINE: Generating %s (from %s)\n", cse, par.getRight().name);
par.getLeft().generateCase(cse, newState);
par.getRight().endRecur();
} else {
throw new RecurLimitException("Rule recurrence limit exceeded");
}
String res = newState.contents.toString();
if (par.getRight().name.contains("+")) {
/* Rule names with pluses in them get space-flattened */
state.contents.append(res.replaceAll("\\s+", ""));
} else {
state.contents.append(res);
}
}
}
|