summaryrefslogtreecommitdiff
path: root/src/main/java/bjc/rgens/parser/elements/RuleVariableCaseElement.java
blob: 5806e39297a6c3611e9a3e50b2700e870e46f9b5 (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
package bjc.rgens.parser.elements;

import static bjc.rgens.parser.RGrammarLogging.fine;

import bjc.rgens.parser.GenerationState;
import bjc.rgens.parser.GrammarException;
import bjc.rgens.parser.Rule;

/**
 * Case element which creates a rule variable.
 * 
 * @author Ben Culkin
 *
 */
public class RuleVariableCaseElement extends VariableDefCaseElement {
	/**
	 * Whether or not this is an exhaustive variable.
	 */
	public final boolean exhaust;

	/**
	 * Create a case element which creates a rule variable.
	 * 
	 * @param varName The name of the variable.
	 * @param varDef The definition of the variable.
	 * @param exhaust Whether or not this is an exhaustive variable.
	 */
	public RuleVariableCaseElement(String varName, String varDef, boolean exhaust) {
		super(varName, varDef);

		this.exhaust = exhaust;
	}

	@Override
	public void generate(GenerationState state) {
		Rule rl = state.findRule(varDef, true);

		if(rl == null) {
			throw new GrammarException("Can't create variable referencing non-existent rule " + varDef);
		}
		
		if(exhaust) {
			rl = rl.exhaust();
		}

		state.defineRuleVar(varName, rl);

		if(exhaust) {
			fine("Defined exhausted rulevar '%s' ('%s')", varName, varDef);
		} else {
			fine("Defined rulevar '%s' ('%s')", varName, varDef);
		}
	}
}