blob: 392a6c858a3449243f37eef4be1a2eb856863378 (
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
|
package bjc.utils.parserutils;
import java.util.function.BiConsumer;
import bjc.utils.exceptions.PragmaFormatException;
import bjc.utils.funcdata.FunctionalStringTokenizer;
import bjc.utils.funcutils.ListUtils;
/**
* Contains factory methods for common pragma types
*
* @author ben
*
*/
public class RuleBasedReaderPragmas {
/**
* Creates a pragma that takes a single integer argument
*
* @param <StateType>
* The type of state that goes along with this pragma
* @param name
* The name of this pragma, for error message purpose
* @param consumer
* The function to invoke with the parsed integer
* @return A pragma that functions as described above.
*/
public static <StateType> BiConsumer<FunctionalStringTokenizer,
StateType> buildInteger(String name,
BiConsumer<Integer, StateType> consumer) {
return (tokenizer, state) -> {
if (!tokenizer.hasMoreTokens()) {
throw new PragmaFormatException("Pragma " + name
+ " requires one integer argument");
}
String token = tokenizer.nextToken();
try {
consumer.accept(Integer.parseInt(token), state);
} catch (NumberFormatException nfex) {
PragmaFormatException pfex = new PragmaFormatException(
"Argument " + token
+ " to version pragma isn't a valid integer. "
+ "This pragma requires a integer argument");
pfex.initCause(nfex);
throw pfex;
}
};
}
/**
* Creates a pragma that takes any number of arguments and collapses
* them all into a single string
*
* @param <StateType>
* The type of state that goes along with this pragma
* @param name
* The name of this pragma, for error message purpose
* @param consumer
* The function to invoke with the parsed string
* @return A pragma that functions as described above.
*/
public static <StateType> BiConsumer<FunctionalStringTokenizer,
StateType> buildStringCollapser(String name,
BiConsumer<String, StateType> consumer) {
return (tokenizer, state) -> {
if (!tokenizer.hasMoreTokens()) {
throw new PragmaFormatException("Pragma " + name
+ " requires one string argument");
}
consumer.accept(ListUtils.collapseTokens(
tokenizer.toList((strang) -> strang)), state);
};
}
}
|