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.utils.parserutils;
import java.io.InputStream;
import java.util.InputMismatchException;
import java.util.Scanner;
import java.util.Stack;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import bjc.utils.data.IHolder;
import bjc.utils.data.IPair;
import bjc.utils.data.Identity;
import bjc.utils.data.Pair;
import bjc.utils.exceptions.UnknownPragmaException;
import bjc.utils.funcdata.FunctionalMap;
import bjc.utils.funcdata.FunctionalStringTokenizer;
import bjc.utils.funcdata.IMap;
/**
* This class parses a rules based config file, and uses it to drive a
* provided set of actions. It differs from {@link RuleBasedConfigReader}
* in that it has support for subrules
*
* @author ben
*
* @param <State>
* The type of the state object to use
* @param <SubruleState>
* The type of state to use for subrules
*
*/
public class RuleStackBasedConfigReader<State, SubruleState> {
private BiConsumer<FunctionalStringTokenizer, IPair<String, State>> startRule;
private BiConsumer<FunctionalStringTokenizer, State> continueRule;
private Consumer<State> endRule;
private Stack<SubruleState> subruleStack;
private BiFunction<FunctionalStringTokenizer, State, SubruleState> startSubrule;
private BiConsumer<FunctionalStringTokenizer, SubruleState> continueSubrule;
private BiConsumer<State, SubruleState> endSubrule;
private IMap<String, BiConsumer<FunctionalStringTokenizer, State>> pragmas;
/**
* Create a new rule-based config reader
*
* @param startRule
* The action to fire when starting a rule
* @param continueRule
* The action to fire when continuing a rule
* @param endRule
* The action to fire when ending a rule
*/
public RuleStackBasedConfigReader(
BiConsumer<FunctionalStringTokenizer, IPair<String, State>> startRule,
BiConsumer<FunctionalStringTokenizer, State> continueRule,
Consumer<State> endRule) {
this.startRule = startRule;
this.continueRule = continueRule;
this.endRule = endRule;
this.pragmas = new FunctionalMap<>();
}
public RuleStackBasedConfigReader() {
this.pragmas = new FunctionalMap<>();
}
/**
* Add a pragma to this reader
*
* @param pragmaName
* The name of the pragma to add
* @param pragmaAction
* The function to execute when this pragma is read
*/
public void addPragma(String pragmaName,
BiConsumer<FunctionalStringTokenizer, State> pragmaAction) {
if (pragmaName == null) {
throw new NullPointerException("Pragma name must not be null");
} else if (pragmaAction == null) {
throw new NullPointerException(
"Pragma action must not be null");
}
pragmas.put(pragmaName, pragmaAction);
}
private void continueRule(State state, boolean ruleOpen, String line) {
if (ruleOpen == false) {
throw new InputMismatchException(
"Can't continue rule with no rule currently open");
}
if (continueRule == null) {
throw new InputMismatchException(
"Attempted to continue rule with rule continuation disabled."
+ " Check for extraneous tabs");
}
String lineSansInitTab = line.substring(1);
if (lineSansInitTab.startsWith("\t")) {
// Do subrule stuff
int subruleLevel;
for (subruleLevel = 1; lineSansInitTab
.charAt(subruleLevel) != '\t'; subruleLevel++) {
// Count up subrule levels
}
if (subruleStack.size() + 1 < subruleLevel) {
throw new InputMismatchException(
"Attempted to start a nested subrule without starting its parent."
+ " Check indentation, as subrules can only be started one at a time");
} else if (subruleStack.size() + 1 == subruleLevel) {
SubruleState subruleState = startSubrule.apply(
new FunctionalStringTokenizer(lineSansInitTab
.substring(subruleLevel - 1), " "),
state);
subruleStack.push(subruleState);
} else if (subruleStack.size() == subruleLevel) {
continueSubrule.accept(
new FunctionalStringTokenizer(lineSansInitTab
.substring(subruleLevel - 1), " "),
subruleStack.peek());
} else {
while (subruleStack.size() != subruleLevel) {
endSubrule.accept(state, subruleStack.pop());
}
continueSubrule.accept(
new FunctionalStringTokenizer(lineSansInitTab
.substring(subruleLevel - 1), " "),
subruleStack.peek());
}
} else {
if (!subruleStack.empty()) {
while (!subruleStack.empty()) {
endSubrule.accept(state, subruleStack.pop());
}
}
continueRule.accept(
new FunctionalStringTokenizer(lineSansInitTab, " "),
state);
}
}
private boolean endRule(State state, boolean ruleOpen) {
if (ruleOpen == false) {
// Ignore blank line without an open rule
} else {
if (endRule == null) {
// Nothing happens on rule end
ruleOpen = false;
} else {
endRule.accept(state);
}
ruleOpen = false;
}
return ruleOpen;
}
/**
* Run a stream through this reader
*
* @param inputStream
* The stream to get input
* @param initialState
* The initial state of the reader
* @return The final state of the reader
*/
public State fromStream(InputStream inputStream, State initialState) {
if (inputStream == null) {
throw new NullPointerException(
"Input stream must not be null");
}
State state;
try (Scanner inputSource = new Scanner(inputStream, "\n")) {
state = initialState;
IHolder<Boolean> ruleOpen = new Identity<>(false);
inputSource.forEachRemaining((line) -> {
if (line.startsWith("#") || line.startsWith("//")) {
// It's a comment
return;
} else if (line.equals("")) {
ruleOpen.replace(endRule(state, ruleOpen.getValue()));
return;
} else if (line.startsWith("\t")) {
continueRule(state, ruleOpen.getValue(), line);
} else {
ruleOpen.replace(
startRule(state, ruleOpen.getValue(), line));
}
});
}
return state;
}
/**
* Set the action to execute when continuing a rule
*
* @param continueRule
* The action to execute on continuation of a rule
*/
public void setContinueRule(
BiConsumer<FunctionalStringTokenizer, State> continueRule) {
this.continueRule = continueRule;
}
/**
* Set the action to execute when ending a rule
*
* @param endRule
* The action to execute on ending of a rule
*/
public void setEndRule(Consumer<State> endRule) {
this.endRule = endRule;
}
/**
* Set the action to execute when starting a rule
*
* @param startRule
* The action to execute on starting of a rule
*/
public void setStartRule(
BiConsumer<FunctionalStringTokenizer, IPair<String, State>> startRule) {
if (startRule == null) {
throw new NullPointerException(
"Action on rule start must be non-null");
}
this.startRule = startRule;
}
private boolean startRule(State state, boolean ruleOpen, String line) {
FunctionalStringTokenizer tokenizer = new FunctionalStringTokenizer(
line, " ");
String nextToken = tokenizer.nextToken();
if (nextToken.equals("pragma")) {
String token = tokenizer.nextToken();
pragmas.getOrDefault(token, (tokenzer, stat) -> {
throw new UnknownPragmaException(
"Unknown pragma " + token);
}).accept(tokenizer, state);
} else {
if (ruleOpen == true) {
throw new InputMismatchException("Attempted to open a"
+ " rule with a rule already open. Make sure rules are"
+ " seperated by blank lines");
}
startRule.accept(tokenizer, new Pair<>(nextToken, state));
ruleOpen = true;
}
return ruleOpen;
}
/**
* Set the function to use when a subrule starts
*
* @param startSubrule
* the function to use for starting a subrule
*/
public void setStartSubrule(
BiFunction<FunctionalStringTokenizer, State, SubruleState> startSubrule) {
this.startSubrule = startSubrule;
}
/**
* Set the function to use when a subrule continues
*
* @param continueSubrule
* the function to use for continuing a subrule
*/
public void setContinueSubrule(
BiConsumer<FunctionalStringTokenizer, SubruleState> continueSubrule) {
this.continueSubrule = continueSubrule;
}
/**
* Set the function to use when a subrule ends
*
* @param endSubrule
* the function to use for ending a subrule
*/
public void setEndSubrule(BiConsumer<State, SubruleState> endSubrule) {
this.endSubrule = endSubrule;
}
}
|