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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
|
package bjc.rgens.parser;
import java.io.IOException;
import java.nio.file.Path;
import java.util.Stack;
import java.util.function.Supplier;
import bjc.utils.funcdata.IList;
import bjc.utils.gen.WeightedGrammar;
/**
* Represents the internal state of reader
*
* @author ben
*
*/
public class ReaderState {
private WeightedGrammar<String> currentGrammar;
private Stack<WeightedGrammar<String>> grammarStack;
private String currentRule;
private boolean isUniform;
private Path currentDirectory;
/**
* Create a new reader state
*
* @param inputPath
* The path to this grammar
*/
public ReaderState(Path inputPath) {
grammarStack = new Stack<>();
currentGrammar = new WeightedGrammar<>();
// Grammars start out uniform
isUniform = true;
currentDirectory = inputPath.getParent();
}
/**
* Get the rule names for the current grammar
*
* @return The rule names for the current grammar
*/
public IList<String> getRuleNames() {
return currentGrammar.getRuleNames();
}
/**
* Check if this reader is currently in uniform mode
*
* @return Whether this grammar is in uniform mode
*/
public boolean isUniform() {
return isUniform;
}
/**
* Set the current grammar to be the specified one
*
* @param newWorkingGrammar
* The new grammar to use
*/
public void setCurrentGrammar(WeightedGrammar<String> newWorkingGrammar) {
currentGrammar = newWorkingGrammar;
}
/**
* Set the rule currently being worked on
*
* @param ruleName
* The rule currently being worked on
*/
public void setCurrentRule(String ruleName) {
currentRule = ruleName;
}
/**
* Set the initial rule of this grammar
*
* @param ruleName
* The initial rule of this grammar
*/
public void setInitialRule(String ruleName) {
currentGrammar.setInitialRule(ruleName);
}
/**
* Toggle this uniformity setting for this grammar
*/
public void toggleUniformity() {
isUniform = !isUniform;
}
/**
* Start work on a new sub-grammar of the previous grammar
*/
public void startNewSubgrammar() {
grammarStack.push(currentGrammar);
currentGrammar = new WeightedGrammar<>();
}
/**
* Move to editing the grammar that is the parent of this current one
*/
public void editParent() {
currentGrammar = grammarStack.pop();
}
/**
* Add a case to the current grammar
*
* @param ruleProbability
* The probability for this case to occur
* @param ruleParts
* The parts that make up this case
*/
public void addCase(int ruleProbability, IList<String> ruleParts) {
currentGrammar.addCase(currentRule, ruleProbability, ruleParts);
}
public void addSpecialRule(String ruleName, Supplier<IList<String>> cse) {
currentGrammar.addSpecialRule(ruleName, cse);
}
/**
* Edit a subgrammar of the current grammar
*
* @param subgrammarName
* The name of the subgrammar to edit
*/
public void editSubgrammar(String subgrammarName) {
WeightedGrammar<String> subgrammar = currentGrammar.getSubgrammar(subgrammarName);
grammarStack.push(currentGrammar);
currentGrammar = subgrammar;
}
/**
* Start editing a new rule in the current grammar
*
* @param ruleName
* The name of the new rule to edit
*/
public void startNewRule(String ruleName) {
currentGrammar.addRule(ruleName);
currentRule = ruleName;
}
/**
* Convert this package of state into a weighted grammar
*
* @return The grammar represented by this state
*/
public WeightedGrammar<String> getGrammar() {
return currentGrammar;
}
/**
* Alias a current subgrammar to a new name
*
* @param subgrammarName
* The name of the subgrammar to alias
* @param subgrammarAlias
* The name of the alias for the subgrammar
*/
public void addGrammarAlias(String subgrammarName, String subgrammarAlias) {
currentGrammar.addGrammarAlias(subgrammarName, subgrammarAlias);
}
/**
* Load a subgrammar into this one.
*
* @param subgrammarName
* The name to assign to the subgrammar
* @param subgrammarPath
* The path to load the subgrammar from
*/
public void loadSubgrammar(String subgrammarName,
String subgrammarPath) {
Path loadPath = currentDirectory.resolve(subgrammarPath);
try {
WeightedGrammar<String> subgrammar = RBGrammarReader
.fromPath(loadPath);
currentGrammar.addSubgrammar(subgrammarName, subgrammar);
} catch (IOException ioex) {
PragmaErrorException peex = new PragmaErrorException(
"Couldn't load subgrammar " + subgrammarName + " from "
+ subgrammarPath);
peex.initCause(ioex);
throw peex;
}
}
/**
* Prefix a token onto all of the cases for the specified rule
*
* @param ruleName
* The rule to do prefixing on
* @param prefixToken
* The token to prefix onto each case
* @param additionalProbability
* The probability modification of the prefixed cases
*/
public void prefixRule(String ruleName, String prefixToken,
int additionalProbability) {
currentGrammar.prefixRule(ruleName, prefixToken,
additionalProbability);
}
/**
* Promote the specified subgrammar above the current grammar
*
* @param subgrammarName
* The name of the subgrammar to promote
* @param subordinateName
* The name to bind this grammar to the subgrammar under
*/
public void promoteGrammar(String subgrammarName,
String subordinateName) {
WeightedGrammar<String> subgrammar = currentGrammar
.getSubgrammar(subgrammarName);
currentGrammar.deleteSubgrammar(subgrammarName);
subgrammar.addSubgrammar(subordinateName, currentGrammar);
currentGrammar = subgrammar;
}
/**
* Delete a rule from the current subgrammar
*
* @param ruleName
* The name of the rule to delete
*/
public void deleteRule(String ruleName) {
currentGrammar.deleteRule(ruleName);
}
/**
* Delete a subgrammar from the current grammar
*
* @param subgrammarName
* The name of the subgrammar to delete
*/
public void deleteSubgrammar(String subgrammarName) {
currentGrammar.deleteSubgrammar(subgrammarName);
}
/**
* Save the current grammar as a subgrammar of the previous one
*
* @param subgrammarName
* The name of the subgrammar to save this under
*/
public void saveSubgrammar(String subgrammarName) {
WeightedGrammar<String> newWorkingGrammar = grammarStack.pop();
newWorkingGrammar.addSubgrammar(subgrammarName, currentGrammar);
currentGrammar = newWorkingGrammar;
}
/**
* Subordinate this grammar as a subgrammar to a new grammar
*
* @param grammarName
* The name for the subgrammar to bind the current grammar
* to
*/
public void subordinateGrammar(String grammarName) {
WeightedGrammar<String> newWorkingGrammar = new WeightedGrammar<>();
newWorkingGrammar.addSubgrammar(grammarName, currentGrammar);
currentGrammar = newWorkingGrammar;
}
/**
* Suffix a token onto all of the cases for the specified rule
*
* @param ruleName
* The rule to do suffixing on
* @param suffixToken
* The token to suffix onto each case
* @param additionalProbability
* The probability modification of the suffixed cases
*/
public void suffixRule(String ruleName, String suffixToken,
int additionalProbability) {
currentGrammar.suffixRule(ruleName, suffixToken,
additionalProbability);
}
}
|