blob: edd7e80fad13685f967f7a0c48e65b393e495434 (
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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
|
package bjc.rgens.newparser;
import bjc.utils.funcdata.FunctionalList;
import bjc.utils.funcdata.IList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import static bjc.rgens.newparser.RuleCase.CaseType.*;
/**
* Construct randomized grammars piece by piece.
*
* @author EVE
*
*/
public class RGrammarBuilder {
private IList<CaseElement> currentCase;
private Rule currRule;
private Map<String, Rule> rules;
private Set<String> exportedRules;
private String initialRule;
/**
* Create a new randomized grammar builder.
*/
public RGrammarBuilder() {
rules = new HashMap<>();
exportedRules = new HashSet<>();
currentCase = new FunctionalList<>();
}
/**
* Starts a rule with the provided name.
*
* If the rule already exists, it will be opened for adding additional
* cases instead.
*
* @param rName
* The name of the rule currently being built.
*
* @throws IllegalArgumentException
* If the rule name is the empty string.
*/
public void startRule(String rName) {
if(rName == null) {
throw new NullPointerException("Rule name must not be null");
} else if(rName.equals("")) {
throw new IllegalArgumentException("The empty string is not a valid rule name");
}
currRule = new Rule(rName);
}
/**
* Convert this builder into a grammar.
*
* @return The grammar built by this builder
*/
public RGrammar toRGrammar() {
RGrammar grammar = new RGrammar(rules);
grammar.setInitialRule(initialRule);
grammar.setExportedRules(exportedRules);
return grammar;
}
/**
* Adds a case part to this rule.
*
* <h2>Case part formatting</h2>
* <dl>
* <dt>Rule Reference</dt>
* <dd>Rule references are marked by being surrounded with square
* brackets (the square brackets are part of the rule's name)</dd>
* <dt>Literal Strings</dt>
* <dd>Literal strings are the default case part type.</dd>
* </dl>
*
* @param csepart
*/
public void addCasePart(String csepart) {
CaseElement element = CaseElement.createElement(csepart);
currentCase.add(element);
}
/**
* Finalizes editing a rule.
*
* Saves the rule to the rule map.
*
* @throws IllegalStateException
* Must be invoked while a rule is being edited.
*/
public void finishRule() {
if(currRule == null) {
throw new IllegalStateException("Must start a rule before finishing one");
}
rules.put(currRule.ruleName, currRule);
}
/**
* Finishes the current case being edited.
*
* @throws IllegalStateException
* Must be invoked while a rule is being edited.
*/
public void finishCase() {
if(currRule == null) {
throw new IllegalStateException("Must start a rule before finishing a case");
}
currRule.addCase(new RuleCase(NORMAL, currentCase));
currentCase = new FunctionalList<>();
}
/**
* Begins a case for the current rule.
*
* @throws IllegalStateException
* Must be invoked while a rule is being edited.
*/
public void beginCase() {
if(currRule == null) {
throw new IllegalStateException("Must start a rule before adding cases");
}
}
/**
* Set the initial rule of the grammar.
*
* @param init
* The initial rule of the grammar.
*
* @throws IllegalArgumentException
* If the rule is either not valid or not defined in the
* grammar.
*/
public void setInitialRule(String init) {
if(init == null) {
throw new NullPointerException("init must not be null");
} else if(init.equals("")) {
throw new IllegalArgumentException("The empty string is not a valid rule name");
}
initialRule = init;
}
/**
* Add an exported rule to this grammar.
*
* @param export
* The name of the rule to export.
*
* @throws IllegalArgumentException
* If the rule is either not valid or not defined in the
* grammar.
*/
public void addExport(String export) {
if(export == null) {
throw new NullPointerException("Export name must not be null");
} else if(export.equals("")) {
throw new NullPointerException("The empty string is not a valid rule name");
}
exportedRules.add(export);
}
/**
* Suffix a given case element to every case of a specific rule.
*
* @param ruleName
* The rule to suffix.
*
* @param suffix
* The suffix to add.
*
* @throws IllegalArgumentException
* If the rule name is either invalid or not defined by
* this grammar, or if the suffix is invalid.
*/
public void suffixWith(String ruleName, String suffix) {
if(ruleName == null) {
throw new NullPointerException("Rule name must not be null");
} else if(ruleName.equals("")) {
throw new IllegalArgumentException("The empty string is not a valid rule name");
}
CaseElement element = CaseElement.createElement(suffix);
for(RuleCase ruleCase : rules.get(ruleName).getCases()) {
ruleCase.getElements().add(element);
}
}
/**
* Prefix a given case element to every case of a specific rule.
*
* @param ruleName
* The rule to prefix.
*
* @param prefix
* The prefix to add.
*
* @throws IllegalArgumentException
* If the rule name is either invalid or not defined by
* this grammar, or if the prefix is invalid.
*/
public void prefixWith(String ruleName, String prefix) {
if(ruleName == null) {
throw new NullPointerException("Rule name must not be null");
} else if(ruleName.equals("")) {
throw new IllegalArgumentException("The empty string is not a valid rule name");
}
CaseElement element = CaseElement.createElement(prefix);
for(RuleCase ruleCase : rules.get(ruleName).getCases()) {
ruleCase.getElements().add(element);
}
}
}
|