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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
|
package bjc.rgens.parser.elements.vars;
import java.util.ArrayList;
import java.util.List;
import bjc.rgens.parser.GenerationState;
import bjc.rgens.parser.GrammarException;
import bjc.utils.ioutils.LevelSplitter;
/**
* Case element which references a variable.
*
* @author Ben Culkin
*
*/
public abstract class VariableElement {
/**
* Whether or not to forbid spaces in this element.
*/
public boolean forbidSpaces;
/**
* Create a new variable element.
*
* @param forbidSpacing
* Whether spacing should be forbidden in this element.
*/
protected VariableElement(boolean forbidSpacing) {
forbidSpaces = forbidSpacing;
}
/**
* Generate this element.
*
* @param state
* The state of generation.
*/
public abstract void generate(GenerationState state);
/**
* Parse a variable element from a string.
*
* @param varElm
* The string to parse.
*
* @return The variable elements which make up the string.
*/
public static List<VariableElement> parseElementString(String varElm) {
boolean forbidSpaces = LevelSplitter.def.levelContains(varElm, "-", "+");
String[] parts;
if (forbidSpaces) {
parts = LevelSplitter.def.levelSplit(varElm, true, "-", "+")
.toArray(new String[0]);
} else {
parts = new String[] {
varElm
};
}
return parseElementString(forbidSpaces, parts);
}
/**
* Parse a string of variable elements.
*
* @param forbidSpaces
* Whether or not to forbid spacing in this variable.
* @param parts
* The parts to parse into variable elements.
*
* @return The variable elements from the string.
*/
public static List<VariableElement> parseElementString(boolean forbidSpaces,
String... parts) {
List<VariableElement> elms = new ArrayList<>(parts.length);
VariableElement prevElement = null;
for (String npart : parts) {
// @HACK
//
// This is so that inline refs to hypenized rule names
// work. Not sure this is a good impl. strategy
String part = npart.replaceAll("\\(|\\)", "");
VariableElement elm = null;
if (part.startsWith("$")) {
elm = new VRefVariableElement(forbidSpaces, part.substring(1));
} else if (part.startsWith("@")) {
if (forbidSpaces)
throw new GrammarException(
"Arrays references aren't allowed in rule names");
elm = new ARefVariableElement(part.substring(1));
} else if (part.startsWith("%")) {
elm = new RRefVariableElement(forbidSpaces,
String.format("[%s]", part.substring(1)));
} else if (part.startsWith("/")) {
throw new GrammarException("Template variables aren't implemented yet");
} else {
if (prevElement != null
&& prevElement instanceof LiteralVariableElement) {
/* Aggregate chain literals together */
((LiteralVariableElement) prevElement).val += part;
} else {
if (part.contains(" ")) {
elm = new LiteralVariableElement(false, part);
} else {
elm = new LiteralVariableElement(true, part);
}
}
}
if (elm != null) {
elms.add(elm);
prevElement = elm;
}
}
return elms;
}
}
|