Transpiled version (22797L) is out of date.
1 | sclass SimpleLeftToRightParser > Meta { |
2 | S text; |
3 | LS tok; |
4 | gettable TokPtr ptr; |
5 | TokPtr mainLoopPtr; |
6 | S currentToken; |
7 | bool caseInsensitive; |
8 | new L warnings; |
9 | |
10 | *() {} |
11 | *(S *text) {} |
12 | *(LS *tok) {} |
13 | |
14 | swappable LS tokenize(S text) { ret javaTok(text); } |
15 | |
16 | S t aka token() { ret currentToken; } |
17 | S token(int i) { ret get(tok, ptr.idx()+i*2); } |
18 | S next aka consume aka tpp() { var t = t(); next(1); ret t; } |
19 | S lastSpace aka prevSpace() { ret get(tok, ptr.idx()-1); } |
20 | S nextSpace() { ret get(tok, ptr.idx()+1); } |
21 | S space(int i) { ret get(tok, ptr.idx()+i*2+1); } |
22 | |
23 | void unconsume() { next(-1); } |
24 | |
25 | bool tokEq aka eqTok(S a, S b) { ret eqOrEqic(caseInsensitive, a, b); } |
26 | bool tokEqOneOf(S a, S... l) { ret any(l, b -> tokEq(a, b)); } |
27 | |
28 | bool is(int i, S t) { ret tokEq(token(i), t); } |
29 | bool is(S t) { ret tokEq(currentToken, t); } |
30 | bool was(S t) { ret tokEq(token(-1), t); } |
31 | |
32 | bool isOneOf(S... tokens) { ret tokEqOneOf(currentToken, tokens); } |
33 | |
34 | S[] consumeArray(int n) { |
35 | S[] array = new[n]; |
36 | for i to n: array[i] = consume(); |
37 | ret array; |
38 | } |
39 | |
40 | meta-for isInteger also as isIdentifier { |
41 | bool isInteger() { ret isInteger(t()); } |
42 | bool isInteger(S s) { ret main isInteger(s); } |
43 | } |
44 | |
45 | S consumeIdentifier() { ret assertIdentifier(consume()); } |
46 | S consumeIdentifierOpt() { ret isIdentifier(t()) ? consume() : null; } |
47 | |
48 | int consumeInteger() { ret parseInt(assertInteger(consume())); } |
49 | |
50 | bool consumeOpt(S token) { |
51 | if (!is(token)) false; |
52 | consume(); |
53 | true; |
54 | } |
55 | |
56 | void consume(S token) { |
57 | if (!is(token)) |
58 | fail("Expected " + quote(token) + ", got " + describeToken(token()); |
59 | consume(); |
60 | } |
61 | |
62 | S describeToken(S token) { |
63 | ret token == null ? "EOF" : quote(token); |
64 | } |
65 | |
66 | S consumeOneOf(S... tokens) { |
67 | if (!isOneOf(tokens)) |
68 | fail("Expected one of " + asList(tokens)); |
69 | ret consume(); |
70 | } |
71 | |
72 | void ptr(TokPtr ptr) { this.ptr = ptr; fetch(); } |
73 | int idx aka tokIdx() { ret ptr.idx(); } |
74 | int lTok() { ret l(tok); } |
75 | int nRemainingTokens() { ret (lTok()-idx())/2; } |
76 | |
77 | bool atEnd aka endOfText() { ret ptr.atEnd(); } |
78 | |
79 | void fetch { currentToken = ptr!; } |
80 | |
81 | bool lineBreak() { ret containsLineBreak(get(tok, ptr.idx()-1)); } |
82 | bool atEndOrLineBreak() { ret atEnd() || lineBreak(); } |
83 | |
84 | void init { |
85 | tok if null = tokenize(text); |
86 | if (ptr == null) ptr(ListAndIndex(tok, 1)); |
87 | } |
88 | |
89 | bool mainLoop() { |
90 | init(); |
91 | if (atEnd()) false; |
92 | if (eq(mainLoopPtr, ptr)) |
93 | fail("main loop didn't advance (current token: " + quote(token()) + ")"); |
94 | mainLoopPtr = ptr; |
95 | true; |
96 | } |
97 | |
98 | class AssureAdvance { |
99 | TokPtr cur; |
100 | |
101 | { init(); } |
102 | |
103 | bool get() { |
104 | if (atEnd()) false; |
105 | if (eq(cur, ptr)) |
106 | fail("Parse loop didn't advance (current token: " + quote(token()) + ")"); |
107 | cur = ptr; |
108 | true; |
109 | } |
110 | } |
111 | |
112 | void unknownToken { |
113 | warn("Unknown token: " + t()); |
114 | } |
115 | |
116 | void warn(S msg) { |
117 | warnings.add(print(msg)); |
118 | } |
119 | |
120 | // can also move backwards for negative n |
121 | void next aka skip aka consume(int n) { |
122 | ptr(min(lTok(), ptr.idx()+n*2)); |
123 | } |
124 | |
125 | // if i points to an N token, it is incremented |
126 | void ptr(int i) { |
127 | ptr(ListAndIndex(tok, min(i | 1, l(tok)))); |
128 | } |
129 | |
130 | LineAndColumn lineAndColumn() { |
131 | ret tokenToLineAndColumn(ptr); |
132 | } |
133 | |
134 | LineAndColumn lineAndColumn(int idx) { |
135 | ret tokenToLineAndColumn(ptr.plus(idx*2)); |
136 | } |
137 | |
138 | S consumeUntilSpaceOr(IF0<Bool> pred) { |
139 | int i = idx(); |
140 | do next(); while (!atEnd() && empty(lastSpace()) && !pred!); |
141 | ret joinSubList(tok, i, idx()-1); |
142 | } |
143 | |
144 | void setText(S text) { |
145 | this.text = text; |
146 | tok = null; |
147 | ptr = null; |
148 | } |
149 | |
150 | int relativeIndexOf(S token) { |
151 | int n = nRemainingTokens(); |
152 | for i to n: |
153 | if (eqTok(token(i), token)) |
154 | ret i; |
155 | ret -1; |
156 | } |
157 | } |
Began life as a copy of #1033717
download show line numbers debug dex old transpilations
Travelled to 4 computer(s): bhatertpkbcr, ekrmjmnbrukm, mowyntqkapby, mqqgnosmbjvj
No comments. add comment
Snippet ID: | #1033967 |
Snippet name: | SimpleLeftToRightParser |
Eternal ID of this version: | #1033967/67 |
Text MD5: | f156875dc22fc01d5711be93dc6141dd |
Author: | stefan |
Category: | javax / parsing |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2022-12-16 00:22:21 |
Source code size: | 3972 bytes / 157 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 285 / 712 |
Version history: | 66 change(s) |
Referenced in: | [show references] |