Libraryless. Click here for Pure Java version (5383L/31K).
1 | /* |
2 | Input is e.g. "32p 8c". We want to get out the 2 operators. |
3 | Then we apply a "puzzler" which tries to combine the operators in a useful way. |
4 | |
5 | // Operators |
6 | |
7 | <int> p = scaleToHeight : IBWntegralImage -> BWImage |
8 | <int> c = posterize : BWImage -> BWImage |
9 | |
10 | // Juxtaposition of operators |
11 | |
12 | _ _ = juxtapose : Op, Op -> Op [left-assoc] |
13 | */ |
14 | |
15 | sclass GazelleVScriptParser<T> extends SimpleLeftToRightParser { |
16 | CalculatedFieldIndexedList<GAbstractOperator<T>, S> operators = new(op -> op.name); |
17 | new L<ParsedWithTokens> parsed; // contains ints and operators |
18 | |
19 | *() {} |
20 | *(S text) { super(text); } |
21 | |
22 | { set caseInsensitive; } |
23 | |
24 | void addOperators(GAbstractOperator<T>... l) { |
25 | addAll(operators, l); |
26 | } |
27 | |
28 | void addOperators(Iterable<? extends GAbstractOperator<T>> l) { |
29 | addAll(operators, l); |
30 | } |
31 | |
32 | void parse(S text) { this.text = text; parse(); } |
33 | |
34 | void parse { |
35 | while (mainLoop()) { |
36 | if (isInteger()) { |
37 | addToParse(parseInt(tpp())); |
38 | continue; |
39 | } |
40 | |
41 | addToParse(findOp(tpp())); |
42 | } |
43 | } |
44 | |
45 | // assumes you have already called tpp() |
46 | void addToParse(O o) { |
47 | parsed.add(ParsedWithTokens(o, tok, ptr.idx()-2, ptr.idx())); |
48 | } |
49 | |
50 | GAbstractOperator<T> findOp(S name) { |
51 | var ops = operators.getAllByKey(name); |
52 | if (empty(ops)) |
53 | warn("Unknown operator: " + name); |
54 | else if (l(ops) > 1) |
55 | warn("Multiple operator definitions (please use GPolymorphicOperator instead): " + name); |
56 | ret first(ops); |
57 | } |
58 | } |
download show line numbers debug dex old transpilations
Travelled to 4 computer(s): bhatertpkbcr, ekrmjmnbrukm, mowyntqkapby, mqqgnosmbjvj
No comments. add comment
Snippet ID: | #1033966 |
Snippet name: | GazelleVScriptParser - successor of GazelleVScript |
Eternal ID of this version: | #1033966/18 |
Text MD5: | 5b0af4e289f60f88e7dd7dbefa1ee9d5 |
Transpilation MD5: | 13d2c012528423d8b4fafd49ae8fe724 |
Author: | stefan |
Category: | javax |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2022-01-15 02:27:43 |
Source code size: | 1525 bytes / 58 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 221 / 363 |
Version history: | 17 change(s) |
Referenced in: | [show references] |