blob: 78b3d140e89c4218a72b4fde8e7662f8665874ca (
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
|
package bjc.rgens.parser.elements;
import bjc.rgens.parser.GenerationState;
import bjc.rgens.parser.GrammarException;
import bjc.rgens.parser.RGrammar;
import bjc.rgens.parser.Rule;
/**
* Case element that defines a expanded-rule variable.
* @author Ben Culkin
*
*/
public class ExpVariableCaseElement extends VariableDefCaseElement {
/**
* Create a new variable-expanding element.
* @param name The name of the variable.
* @param def The definition of the variable.
*/
public ExpVariableCaseElement(String name, String def) {
super(name, def);
}
@Override
public void generate(GenerationState state) {
GenerationState newState = state.newBuf();
Rule rl = state.findRule(varDef, true);
if(rl != null) {
RGrammar destGrammar = rl.belongsTo;
newState.swapGrammar(destGrammar);
/*
* Don't post-process the string, we should only do that
* once.
*/
String res = destGrammar.generate(varDef, state, false);
state.defineVar(varName, res);
} else {
String msg = String.format("No rule '%s' defined", varDef);
throw new GrammarException(msg);
}
}
}
|