1 | // parser explainer |
2 | static class Explain {
|
3 | static L<S> primitiveClasses = litlist("quoted", "identifier", "any", "int");
|
4 | |
5 | O parseResult; |
6 | L<S> tok; |
7 | L e; |
8 | new L<Explain> subs; |
9 | |
10 | // XXX - applies to full parse always |
11 | L<S> fullMatchClasses() { ret (L<S>) call(parseResult, "fullMatchClasses"); }
|
12 | |
13 | static boolean debug; |
14 | |
15 | *(O *parseResult, L *e) {
|
16 | tok = (L) get(parseResult, "tok"); |
17 | _makeSubExplains(); |
18 | } |
19 | |
20 | void _makeSubExplains() {
|
21 | for (int i = 4; i < l(e); i++) {
|
22 | L sub = cast get(e, i); |
23 | int t1 = (int) get(sub, 0); |
24 | int t2 = (int) get(sub, 1); |
25 | S className = getString(sub, 2); |
26 | L subE = sub; |
27 | if (debug) |
28 | print("subE first = " + sfu(subE));
|
29 | if (!primitiveClasses.contains(className)) |
30 | subE = (L) call(parseResult, "explainFull", t1, t2, className); |
31 | if (debug) |
32 | printF("Explaining for " + className() + ": * * * => *", t1, t2, className, subE);
|
33 | if (subE == null) |
34 | subs.add(null); |
35 | else |
36 | subs.add(new Explain(parseResult, subE)); |
37 | } |
38 | } |
39 | |
40 | S className() {
|
41 | ret getString(e, 2); |
42 | } |
43 | |
44 | int fromToken() {
|
45 | ret (int) get(e, 0); |
46 | } |
47 | |
48 | int toToken() {
|
49 | ret (int) get(e, 1); |
50 | } |
51 | |
52 | // return tokens, padded with empty non-code tokens first and last |
53 | // to make actual CNC |
54 | L<S> tok() {
|
55 | ret concatLists( |
56 | litlist(""),
|
57 | subList(tok, fromToken(), toToken()-1), |
58 | litlist(""));
|
59 | } |
60 | |
61 | S string() {
|
62 | ret join(subList(tok, fromToken(), toToken()-1)); |
63 | } |
64 | |
65 | boolean containsToken(S t) {
|
66 | ret main containsToken(tok(), t); |
67 | } |
68 | |
69 | void findAll(S className, L<S> out) {
|
70 | if (eq(className, className())) |
71 | out.add(string()); |
72 | else // << this is new - don't recurse |
73 | for (Explain e : subs) |
74 | if (e != null) |
75 | e.findAll(className, out); |
76 | } |
77 | |
78 | L<S> findAll(S className) {
|
79 | new L<S> l; |
80 | findAll(className, l); |
81 | ret l; |
82 | } |
83 | |
84 | // short for findFirst |
85 | Explain find(S className) {
|
86 | ret findFirst(className); |
87 | } |
88 | |
89 | Explain findFirst(S className) {
|
90 | if (eq(className, className())) |
91 | ret this; |
92 | ret findFirstSub(className); |
93 | } |
94 | |
95 | // find class, but exclude myself |
96 | Explain findFirstSub(S className) {
|
97 | for (Explain e : subs) |
98 | if (e != null) {
|
99 | Explain result = e.findFirst(className); |
100 | if (result != null) ret result; |
101 | } |
102 | ret null; |
103 | } |
104 | |
105 | boolean is(S className) {
|
106 | ret eq(className(), className); |
107 | } |
108 | |
109 | boolean has(S className) {
|
110 | ret findFirst(className) != null; |
111 | } |
112 | |
113 | boolean hasSub(S className) {
|
114 | ret findFirstSub(className) != null; |
115 | } |
116 | |
117 | void findAll2(S className, L<Explain> out) {
|
118 | if (is(className)) |
119 | out.add(this); |
120 | for (Explain e : subs) |
121 | if (e != null) |
122 | e.findAll2(className, out); |
123 | } |
124 | |
125 | L<Explain> findAll2(S className) {
|
126 | new L<Explain> l; |
127 | findAll2(className, l); |
128 | ret l; |
129 | } |
130 | |
131 | // short for pruneSubs |
132 | Explain prune(S className) {
|
133 | ret pruneSubs(className); |
134 | } |
135 | |
136 | // returns self after pruning |
137 | Explain pruneSubs(S className) {
|
138 | for (int i = 0; i < l(subs); ) {
|
139 | Explain e = sub(i); |
140 | if (e == null) ++i; |
141 | else if (eq(e.className(), className)) |
142 | subs.remove(i); |
143 | else {
|
144 | e.pruneSubs(className); |
145 | ++i; |
146 | } |
147 | } |
148 | ret this; |
149 | } |
150 | |
151 | Explain sub(int i) { ret get(subs, i); }
|
152 | S str(int i) {
|
153 | // ret sub(i).string(); // sub might be null |
154 | L sub = cast get(e, 4+i); |
155 | int t1 = (int) get(sub, 0); |
156 | int t2 = (int) get(sub, 1); |
157 | ret join(subList(tok, t1, t2-1)); |
158 | } |
159 | |
160 | L<Explain> subs() { ret subs; }
|
161 | |
162 | bool singleEqualChild() {
|
163 | if (l(subs) != 1 || first(subs) == null) false; |
164 | Explain e = first(subs); |
165 | ret fromToken() == e.fromToken() && toToken() == e.toToken(); |
166 | } |
167 | |
168 | S prettierAnalysis() {
|
169 | ret (S) call(parseResult, "prettierAnalysis"); |
170 | } |
171 | } |
download show line numbers debug dex old transpilations
Travelled to 14 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt
No comments. add comment
| Snippet ID: | #1002529 |
| Snippet name: | class Explain (parser explainer) |
| Eternal ID of this version: | #1002529/15 |
| Text MD5: | b38407010ad06a76a90040b6842a0e1d |
| Author: | stefan |
| Category: | javax |
| Type: | JavaX fragment (include) |
| Public (visible to everyone): | Yes |
| Archived (hidden from active list): | No |
| Created/modified: | 2021-11-14 21:45:03 |
| Source code size: | 4028 bytes / 171 lines |
| Pitched / IR pitched: | No / No |
| Views / Downloads: | 1035 / 3877 |
| Version history: | 14 change(s) |
| Referenced in: | [show references] |