blob: 455fce65f090966f17b965f3940f3047d799ebce (
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
48
49
50
51
52
53
54
55
|
package bjc.rgens.parser.elements;
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 ExpVariableCaseElement extends VariableCaseElement {
public ExpVariableCaseElement(String name, String def) {
super(name, def, VariableType.EXPAND);
}
@Override
public void generate(GenerationState state) {
GenerationState newState = state.newBuf();
if (state.rules.containsKey(varDef)) {
Rule rl = state.rules.get(varDef);
if(rl.doRecur()) {
RuleCase destCase = state.rules.get(varDef).getCase();
System.err.printf("\tFINE: Generating %s (from %s)\n", destCase, varDef);
state.gram.generateCase(destCase, newState);
rl.endRecur();
} else {
throw new RecurLimitException("Rule recurrence limit exceeded");
}
} else if (state.importRules.containsKey(varDef)) {
RGrammar destGrammar = state.importRules.get(varDef);
newState.swapGrammar(destGrammar);
String res = destGrammar.generate(varDef, state);
/*
* @NOTE
*
* :Postprocessing
*
* This is because generate() returns a processed
* string, but modifies the passed in StringBuilder.
*/
newState.contents = new StringBuilder(res);
} else {
String msg = String.format("No rule '%s' defined", varDef);
throw new GrammarException(msg);
}
state.vars.put(varName, newState.contents.toString());
}
}
|