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
|
package bjc.utils.ioutils;
import java.io.InputStream;
import java.util.InputMismatchException;
import java.util.Scanner;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import bjc.data.IHolder;
import bjc.data.IPair;
import bjc.data.Identity;
import bjc.data.Pair;
import bjc.utils.exceptions.UnknownPragmaException;
import bjc.funcdata.FunctionalMap;
import bjc.funcdata.FunctionalStringTokenizer;
import bjc.funcdata.IMap;
/**
* This class parses a rules based config file, and uses it to drive a provided
* set of actions
*
* @author ben
*
* @param <E>
* The type of the state object to use
*
*/
public class RuleBasedConfigReader<E> {
/*
* Function to execute when starting a rule.
*
* Takes the tokenizer, and a pair of the read token and application state
*/
private BiConsumer<FunctionalStringTokenizer, IPair<String, E>> start;
/*
* Function to use when continuing a rule.
*
* Takes a tokenizer and application state
*/
private BiConsumer<FunctionalStringTokenizer, E> continueRule;
/*
* Function to use when ending a rule.
*
* Takes an application state
*/
private Consumer<E> end;
/*
* Map of pragma names to pragma actions.
*
* Pragma actions are functions taking a tokenizer and application state
*/
private final IMap<String, BiConsumer<FunctionalStringTokenizer, E>> pragmas;
/**
* Create a new rule-based config reader
*
* @param start
* The action to fire when starting a rule
* @param continueRule
* The action to fire when continuing a rule
* @param end
* The action to fire when ending a rule
*/
public RuleBasedConfigReader(
final BiConsumer<FunctionalStringTokenizer, IPair<String, E>> start,
final BiConsumer<FunctionalStringTokenizer, E> continueRule,
final Consumer<E> end) {
this.start = start;
this.continueRule = continueRule;
this.end = end;
this.pragmas = new FunctionalMap<>();
}
/**
* Add a pragma to this reader
*
* @param name
* The name of the pragma to add
* @param action
* The function to execute when this pragma is read
*/
public void addPragma(final String name,
final BiConsumer<FunctionalStringTokenizer, E> action) {
if (name == null)
throw new NullPointerException("Pragma name must not be null");
else if (action == null)
throw new NullPointerException("Pragma action must not be null");
pragmas.put(name, action);
}
private void continueRule(final E state, final boolean isRuleOpen,
final String line) {
// Make sure our input is correct
if (isRuleOpen == false)
throw new InputMismatchException("Cannot continue rule with no rule open");
else if (continueRule == null)
throw new InputMismatchException(
"Rule continuation not supported for current grammar");
/*
* Accept the rule
*/
continueRule.accept(new FunctionalStringTokenizer(line.substring(1), " "), state);
}
private boolean endRule(final E state, final boolean isRuleOpen) {
/*
* Ignore blank line without an open rule
*/
if (isRuleOpen == false) {
/*
* Do nothing
*/
return false;
}
/*
* Nothing happens on rule end
*/
if (end != null) {
/*
* Process the rule ending
*/
end.accept(state);
}
/*
* Return a closed rule
*/
return false;
}
/**
* Run a stream through this reader
*
* @param input
* The stream to get input
* @param initialState
* The initial state of the reader
* @return The final state of the reader
*/
public E fromStream(final InputStream input, final E initialState) {
if (input == null)
throw new NullPointerException("Input stream must not be null");
/*
* Application state: We're giving this back later
*/
final E state = initialState;
/*
* Prepare our input source
*/
try (Scanner source = new Scanner(input)) {
source.useDelimiter("\n");
/*
* This is true when a rule's open
*/
final IHolder<Boolean> isRuleOpen = new Identity<>(false);
/*
* Do something for every line of the file
*/
source.forEachRemaining(line -> {
/*
* Skip comment lines
*/
if (line.startsWith("#") || line.startsWith("//"))
/*
* It's a comment
*/
return;
else if (line.equals("")) {
/*
* End the rule
*/
isRuleOpen.replace(endRule(state, isRuleOpen.getValue()));
} else if (line.startsWith("\t")) {
/*
* Skip comment lines.
*/
if (line.startsWith("#") || line.startsWith("//"))
/*
* It's a comment.
*/
return;
/*
* Continue the rule
*/
continueRule(state, isRuleOpen.getValue(), line.substring(1));
} else {
/*
* Open a rule
*/
isRuleOpen.replace(startRule(state, isRuleOpen.getValue(), line));
}
});
}
/*
* Return the state that the user has created
*/
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(final BiConsumer<FunctionalStringTokenizer, E> continueRule) {
this.continueRule = continueRule;
}
/**
* Set the action to execute when ending a rule
*
* @param end
* The action to execute on ending of a rule
*/
public void setEndRule(final Consumer<E> end) {
this.end = end;
}
/**
* Set the action to execute when starting a rule
*
* @param start
* The action to execute on starting of a rule
*/
public void setStartRule(
final BiConsumer<FunctionalStringTokenizer, IPair<String, E>> start) {
if (start == null)
throw new NullPointerException("Action on rule start must be non-null");
this.start = start;
}
private boolean startRule(final E state, boolean isRulOpen, final String line) {
boolean isRuleOpen = isRulOpen;
/*
* Create the line tokenizer
*/
final FunctionalStringTokenizer tokenizer
= new FunctionalStringTokenizer(line, " ");
/*
* Get the initial token
*/
final String nextToken = tokenizer.nextToken();
/*
* Handle pragmas
*/
if (nextToken.equals("pragma")) {
/*
* Get the pragma name
*/
final String token = tokenizer.nextToken();
/*
* Handle pragmas
*/
pragmas.getOrDefault(token, (tokenzer, stat) -> {
throw new UnknownPragmaException("Unknown pragma " + token);
}).accept(tokenizer, state);
} else {
/*
* Make sure input is correct
*/
if (isRuleOpen == true)
throw new InputMismatchException(
"Nested rules are currently not supported");
/*
* Start a rule
*/
start.accept(tokenizer, new Pair<>(nextToken, state));
isRuleOpen = true;
}
return isRuleOpen;
}
}
|