-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathParser.java
More file actions
672 lines (619 loc) · 25.6 KB
/
Copy pathParser.java
File metadata and controls
672 lines (619 loc) · 25.6 KB
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
package cwp.parser;
import clojure.lang.Util;
import cwp.ast.*;
import cwp.lexer.LexerReader;
import cwp.lexer.Token;
import java.util.*;
public class Parser {
// Precedence
final static int LOWEST = 0;
final static int PIPE = 1; // |> , |>>
final static int OR = 2;
final static int AND = 3;
final static int NOT = 4;
final static int COMPARISON = 5; // >, <, >= ,<=, =, ==, !=,
final static int SUM = 6; // +, -
final static int PRODUCT = 7; // *, /
static HashMap<String, Integer> precedence = new HashMap<>();
static {
precedence.put("|>", PIPE);
precedence.put("|>>", PIPE);
precedence.put("or", OR);
precedence.put("and", AND);
// precedence.put("not", NOT);
precedence.put("=", COMPARISON);
precedence.put("==", COMPARISON);
precedence.put("!=", COMPARISON);
precedence.put(">", COMPARISON);
precedence.put("<", COMPARISON);
precedence.put(">=", COMPARISON);
precedence.put("<=", COMPARISON);
precedence.put("+", SUM);
precedence.put("-", SUM);
precedence.put("*", PRODUCT);
precedence.put("/", PRODUCT);
}
public static int getPrecedence(String s) {
return precedence.getOrDefault(s, -1);
}
LexerReader lexerReader;
ArrayList<Integer> indentation = new ArrayList<Integer>();
int curLine = 1;
public int lastIndentation() {
int lastIndex = indentation.size() - 1;
if (lastIndex < 0) {
return -1;
}
return indentation.get(lastIndex);
}
public void addIndentation(int i) {
indentation.add(i);
/*if (i > lastIndentation())
indentation.add(i);
else {
Util.sneakyThrow(new ParserException("Bad indentation level: " + indentation + " " + i));
}*/
}
public void popIndentation() {
indentation.remove(indentation.size() - 1);
}
public boolean checkIndentation(int indent) {
return indentation.contains(indent);
}
public Parser(String s) {
lexerReader = new LexerReader(s);
}
public void unreadToken(Token t) {
lexerReader.unread(t);
}
public Token nextTokenWithSep() {
return lexerReader.read();
}
public Token nextToken() {
Token t = lexerReader.read();
while (t.type == Token.Type.COMMA || t.type == Token.Type.TO) {
t = lexerReader.read();
}
return t;
}
public Expr readExpr() {
return readExpr(Token.Type.EOF, EofExpr.EOF_EXPR);
}
Stack<Expr> stackExpr = new Stack<>();
// Dangerous to use, because when read nextToken(), after unreadExpr() somewhere,
// nextToken() return token after unreaded Expressions - because tokens aren't from unread expr aren't unread
// public void unreadExpr(Expr e) {
// stackExpr.push(e);
// }
public Expr readExpr(Token.Type delim, Expr delimReturn) {
if (!stackExpr.empty()) {
return stackExpr.pop();
}
Token t = nextToken();
if (t.type == Token.Type.EOF) {
return EofExpr.EOF_EXPR;
}
if (t.type == delim) {
delimReturn.initTok = t;
return delimReturn;
}
if (t.type == Token.Type.SYMBOL && t.str.equals("def") && !t.callable) {
return readDef(t);
}
if (t.type == Token.Type.SYMBOL && t.str.equals("if") && !t.callable) {
return readIfElse(t);
}
if (t.type == Token.Type.SYMBOL && t.str.equals("try") && !t.callable) {
return readTryCatchFinally(t);
}
if (t.type == Token.Type.SYMBOL && (t.str.equals("fn") || t.str.equals("lambda")) && !t.callable) {
return readFn(t);
}
if (t.type == Token.Type.SYMBOL && t.str.equals("ns") && !t.callable) {
return readNS(t);
}
if (t.type == Token.Type.SYMBOL && Controls.isFlat(t.str)) {
return readFlatCotrol(t, Controls.Type.FLAT);
}
if (t.type == Token.Type.SYMBOL && Controls.isVec(t.str)) {
return readFlatCotrol(t, Controls.Type.VEC);
}
if (t.type == Token.Type.SYMBOL && Controls.isMap(t.str)) {
return readFlatCotrol(t, Controls.Type.MAP);
}
Expr e = readInfixExpr(t, LOWEST);
if (e == null) {
throw Util.sneakyThrow(new ParserException("Unexpected token: " + t.str, t));
}
return e;
}
public Expr readInfixExpr(Token t, int prevPrecedence) {
Expr firstExpr = readUnaryExpr(t);
if (firstExpr.initTok.type == Token.Type.EOF) throw Util.runtimeException("EOF while reading");
Token opToken = nextTokenWithSep();
MultiInfixExpr mExpr = new MultiInfixExpr(firstExpr.initTok, opToken, firstExpr);
for (; ; ) {
int curPrecedence = getPrecedence(opToken.str);
if (curPrecedence == -1 || prevPrecedence >= curPrecedence) {
unreadToken(opToken);
break;
}
Expr rightExpr = readInfixExpr(nextToken(), curPrecedence);
mExpr.add(rightExpr);
opToken = nextTokenWithSep();
if (!opToken.str.equals(mExpr.opToken.str)) {
mExpr = new MultiInfixExpr(mExpr.initTok, opToken, mExpr);
}
}
return mExpr.getExpr();
}
public Expr readUnaryExpr(Token t) {
if (t.type == Token.Type.META) {
Expr meta = readExpr();
if (meta.initTok.type == Token.Type.EOF) throw Util.runtimeException("EOF while reading");
Expr expr = readExpr();
if (expr.initTok.type == Token.Type.EOF) throw Util.runtimeException("EOF while reading");
return new WithMetaExpr(t, meta, expr);
}
if (t.type == Token.Type.DEREF ||
t.type == Token.Type.QUOTE ||
t.type == Token.Type.UNQUOTE ||
t.type == Token.Type.SYNTAX_QUOTE ||
t.type == Token.Type.VAR_QUOTE) {
Expr nextExpr = readExpr();
if (nextExpr.initTok.type == Token.Type.EOF) throw Util.runtimeException("EOF while reading");
return new UnaryExpr(t, nextExpr);
}
if (t.type == Token.Type.SYMBOL &&
(t.str.equals("not")) ||
(t.str.equals("throw"))) {
Expr nextExpr = readExpr();
if (nextExpr.initTok.type == Token.Type.EOF) throw Util.runtimeException("EOF while reading");
return new UnaryExpr(t, nextExpr, true);
}
return readFunctionCallExpr(t);
}
public Expr readFunctionCallExpr(Token t) {
Expr e = readBaseExpr(t);
while (e != null && e.callable) {
Token nextTok = nextToken();
if (nextTok.type != Token.Type.LPAREN) {
throw Util.sneakyThrow(new ParserException("After callable should be '('", nextTok));
}
DelimitedListResult d = readDelimitedList(Token.Type.RPAREN);
e = new FunctionCallExpr(e, d.a, d.last.initTok.callable);
}
return e;
}
public Expr readBaseExpr(Token t) {
if (t.type == Token.Type.REGEX
|| t.type == Token.Type.NUMBER
|| t.type == Token.Type.STRING
|| t.type == Token.Type.KEYWORD
|| t.type == Token.Type.SYMBOL
|| t.type == Token.Type.BOOL
|| t.type == Token.Type.NULL
|| t.type == Token.Type.CHAR
|| t.type == Token.Type.SYMBOLIC_VALUE
|| t.type == Token.Type.RAW
) {
return new SimpleExpr(t, t.callable);
}
if (t.type == Token.Type.LCURLY) {
DelimitedListResult res = readDelimitedList(Token.Type.RCURLY);
if (res.a.size() % 2 != 0) {
throw Util.sneakyThrow(new ParserException("Map literal must have even number of forms", t));
}
return new MapExpr(t, res.a, res.last.initTok.callable);
}
if (t.type == Token.Type.NAMESPACE_MAP) {
DelimitedListResult res = readDelimitedList(Token.Type.RCURLY);
if (res.a.size() % 2 != 0) {
throw Util.sneakyThrow(new ParserException("Map literal must have even number of forms", t));
}
return new MapExpr(t, res.a, res.last.initTok.callable);
}
if (t.type == Token.Type.CONDITIONAL) {
DelimitedListResult res = readDelimitedList(Token.Type.RPAREN);
if (res.a.size() % 2 != 0) {
throw Util.sneakyThrow(new ParserException("Conditional must have even number of forms", t));
}
return new ConditionalExpr(t, res.a, res.last.initTok.callable);
}
if (t.type == Token.Type.LBRACE) {
DelimitedListResult res = readDelimitedList(Token.Type.RBRACE);
return new VectorExpr(t, res.a, res.last.initTok.callable);
}
if (t.type == Token.Type.SET) {
DelimitedListResult res = readDelimitedList(Token.Type.RCURLY);
return new SetExpr(t, res.a, res.last.initTok.callable);
}
if (t.type == Token.Type.LPAREN) {
DelimitedListResult res = readDelimitedList(Token.Type.RPAREN);
return new ParensExpr(t, res.a, res.last.initTok.callable);
}
if (t.type == Token.Type.EOF) {
throw Util.runtimeException("EOF while reading");
}
Util.sneakyThrow(new ParserException("Unexpected token: " + t.str, t));
return null;
}
public Expr readIfElse(Token initToken) {
ArrayList<Expr> ifExprs;
ArrayList<Expr> elseExprs = null;
Expr condExpr = readExpr();
if (condExpr == EofExpr.EOF_EXPR) {
throw Util.runtimeException("EOF while reading");
}
// reading if clause
Token nextToken = nextToken();
if (nextToken.type == Token.Type.EOF) {
throw Util.runtimeException("EOF while reading");
}
if (nextToken.type == Token.Type.COLON) {
ifExprs = readBlock(initToken);
} else {
unreadToken(nextToken);
Expr e = readExpr();
if (e == EofExpr.EOF_EXPR) throw Util.runtimeException("EOF while reading");
ifExprs = new ArrayList<>();
ifExprs.add(e);
}
// reading else clause
// FIXED: error when block , because block return unreadExpr() but here is nextToken
nextToken = nextToken();
// System.out.println(">>> nextToken, expect else: " + nextToken);
if (nextToken.type == Token.Type.SYMBOL && nextToken.str.equals("else")) {
nextToken = nextToken();
if (nextToken.type == Token.Type.COLON) {
elseExprs = readBlock(initToken);
} else {
unreadToken(nextToken);
Expr e = readExpr();
if (e == EofExpr.EOF_EXPR) throw Util.runtimeException("EOF while reading");
elseExprs = new ArrayList<>();
elseExprs.add(e);
}
} else {
unreadToken(nextToken);
}
return new IfElseExpr(initToken, condExpr, ifExprs, elseExprs);
}
public DefExpr readDef(Token initTok) {
boolean isFunction = false;
ArrayList<Expr> metas = new ArrayList<>();
ArrayList<Expr> args = new ArrayList<>();
ArrayList<Expr> body = new ArrayList<>();
Token nameTok;
for (; ; ) {
nameTok = nextToken();
if (nameTok.type != Token.Type.META) {
break;
}
Expr expr = readExpr();
metas.add(expr);
}
if (nameTok.type != Token.Type.SYMBOL) {
throw Util.sneakyThrow(new ParserException("Bad 'def' declaration, expected symbol, not: " + nameTok.str,
nameTok));
}
Token colonOrLBrace = nextToken();
if (colonOrLBrace.type != Token.Type.LPAREN && colonOrLBrace.type != Token.Type.COLON) {
throw Util.sneakyThrow(new ParserException("Bad 'def' declaration, expected `(` or `:`, not: " + colonOrLBrace.str,
colonOrLBrace));
}
if (colonOrLBrace.type == Token.Type.LPAREN) {
isFunction = true;
DelimitedListResult d = readDelimitedList(Token.Type.RPAREN);
args = d.a;
colonOrLBrace = nextToken();
}
if (colonOrLBrace.type != Token.Type.COLON) {
throw Util.sneakyThrow(new ParserException("Bad 'def' declaration, expected `:`, not: " + colonOrLBrace.str,
colonOrLBrace));
}
body = readBlock(initTok);
DefExpr defExpr = new DefExpr(initTok, nameTok, isFunction, metas, args, body);
// System.out.println("<END DefExpr> : " + defExpr.gen());
return defExpr;
}
public TryCatchFinallyExpr readTryCatchFinally(Token initTok) {
Token nextTok = nextToken();
if (nextTok.type != Token.Type.COLON) {
throw Util.sneakyThrow(new ParserException("Expected ':', but got: " + nextTok.str, nextTok));
}
ArrayList<Expr> body = readBlock(initTok);
ArrayList<CatchExpr> catches = new ArrayList<CatchExpr>();
for (; ; ) {
nextTok = nextToken();
if (nextTok.str.equals("catch") && nextTok.column == initTok.column) {
CatchExpr e = readCatch(nextTok);
catches.add(e);
} else {
break;
}
}
ArrayList<Expr> finallyExpr = new ArrayList<Expr>();
if (nextTok.str.equals("finally") && nextTok.column == initTok.column) {
/*if (nextTok.column != initTok.column) {
throw Util.sneakyThrow(new ParserException("Finally keyword have to be at same column as try exception, at: "
+ nextTok.line + ", " + nextTok.column));
}*/
Token finallyTok = nextTok;
nextTok = nextToken();
if (nextTok.type != Token.Type.COLON) {
throw Util.sneakyThrow(new ParserException("Expected ':', but got:" + nextTok.str, nextTok));
}
finallyExpr = readBlock(finallyTok);
} else {
unreadToken(nextTok);
}
TryCatchFinallyExpr tcfExpr = new TryCatchFinallyExpr(initTok, body, catches, finallyExpr);
// System.out.println("<END TryCatchFinallyExpr> : " + tcfExpr.gen());
return tcfExpr;
}
public CatchExpr readCatch(Token initTok) {
Token exceptionType = nextToken();
if (exceptionType.type != Token.Type.SYMBOL) {
throw Util.sneakyThrow(new ParserException("Expected exception type, but got:" + exceptionType.str,
exceptionType));
}
Token exceptionName = nextToken();
//System.out.println(">>> Catch exType: " + exceptionType.str + " " + exceptionType.type);
//System.out.println(">>> Catch exName: " + exceptionName.str + " " + exceptionName.type);
if (exceptionName.type != Token.Type.SYMBOL) {
throw Util.sneakyThrow(new ParserException("Expected exception name, but got:" + exceptionName.str,
exceptionName));
}
Token nextToken = nextToken();
if (nextToken.type != Token.Type.COLON) {
throw Util.sneakyThrow(new ParserException("Expected ':', but got:" + nextToken.str, nextToken));
}
ArrayList<Expr> body = readBlock(initTok);
return new CatchExpr(initTok, exceptionType, exceptionName, body);
}
public FnExpr readFn(Token initTok) {
ArrayList<Expr> args = new ArrayList<>();
ArrayList<Expr> body = new ArrayList<>();
for (; ; ) {
Token nextToken = nextToken();
if (nextToken.type == Token.Type.EOF) {
throw Util.runtimeException("EOF while reading");
}
if (nextToken.type == Token.Type.COLON) {
break;
}
unreadToken(nextToken);
Expr e = readExpr();
if (e == EofExpr.EOF_EXPR) {
throw Util.runtimeException("EOF while reading");
}
args.add(e);
}
body = readBlock(initTok);
FnExpr fnExpr = new FnExpr(initTok, args, body);
return fnExpr;
}
public ControlExpr readFlatCotrol(Token initTok, Controls.Type type) {
//ArrayList<Expr> args = readBlock(initTok);
ArrayList<Expr> args = new ArrayList<Expr>();
for (; ; ) {
Token nextToken = nextToken();
if (nextToken.type == Token.Type.COLON) {
break;
}
if (nextToken.type == Token.Type.EOF) {
throw Util.runtimeException("EOF while reading");
}
unreadToken(nextToken);
Expr e = readExpr();
args.add(e);
}
ArrayList<Expr> body = readBlock(initTok);
return new ControlExpr(initTok, args, body, type);
}
public NsExpr readNS(Token initTok) {
ArrayList<Expr> requires = null;
ArrayList<Expr> imports = null;
ArrayList<Expr> flat = new ArrayList<>();
ArrayList<Expr> map = new ArrayList<>();
ArrayList<Expr> vec = new ArrayList<>();
Expr nameExpr = readExpr();
if (nameExpr == EofExpr.EOF_EXPR) {
throw Util.runtimeException("EOF while reading");
}
Token nextToken = nextToken();
ArrayList<String> names = new ArrayList<>(List.of("require", "import", "map", "flat", "vec"));
Controls.reset();
while (names.contains(nextToken.str)) {
Token colonToken = nextToken();
if (colonToken.type != Token.Type.COLON) {
throw Util.sneakyThrow(new ParserException("Expected ':', but got: " + colonToken.str, colonToken));
}
switch (nextToken.str) {
case "require":
requires = readBlock(nextToken);
break;
case "import":
imports = readBlock(nextToken);
break;
case "vec":
vec = readBlock(nextToken);
break;
case "flat":
flat = readBlock(nextToken);
break;
case "map":
map = readBlock(nextToken);
break;
}
nextToken = nextToken();
}
unreadToken(nextToken);
// System.out.println(map);
// System.out.println(vec);
// System.out.println(flat);
for (var e : map) Controls.addMap(e.gen());
for (var e : vec) Controls.addVec(e.gen());
for (var e : flat) Controls.addFlat(e.gen());
// Token nextToken = nextToken();
// if (nextToken.str.equals("require")) {
// Token colonToken = nextToken();
// if (colonToken.type != Token.Type.COLON) {
// throw Util.sneakyThrow(new ParserException("Expected ':', but got: " + colonToken.str +
// " , at: " + colonToken.line + ", " + colonToken.column));
// }
// requires = readBlock(nextToken);
// } else {
// unreadToken(nextToken);
// }
// nextToken = nextToken();
// if (nextToken.str.equals("import")) {
// Token colonToken = nextToken();
// if (colonToken.type != Token.Type.COLON) {
// throw Util.sneakyThrow(new ParserException("Expected ':', but got: " + colonToken.str +
// " , at: " + colonToken.line + ", " + colonToken.column));
// }
// imports = readBlock(nextToken);
// } else {
// unreadToken(nextToken);
// }
NsExpr nsExpr = new NsExpr(initTok, nameExpr, requires, imports);
// System.out.println(nsExpr);
return nsExpr;
}
public ArrayList<Expr> readBlock(Token initTok) {
ArrayList<Expr> body = new ArrayList<>();
addIndentation(initTok.column);
// System.out.println(">>> add indentation def: " + indentation + " initTok.column: " + initTok.column);
Expr firstExpr = readExpr();
if (firstExpr.initTok.type == Token.Type.EOF) {
throw Util.runtimeException("EOF while reading");
}
/*if (firstExpr.initTok.line == initTok.line) {
body.add(firstExpr);
return body;
}*/
if (firstExpr.initTok.column <= initTok.column) {
throw Util.sneakyThrow(new ParserException("Bad indentation, for '"
+ firstExpr.initTok.str + "', should be nested to: " + initTok.str, firstExpr.initTok));
}
body.add(firstExpr);
addIndentation(firstExpr.initTok.column);
//System.out.println(">>> add indentation first-tok: " + indentation + " initTok.column: " + firstExpr.initTok.column
// + ", '" + firstExpr.gen() + "'");
//System.out.println(">>> indentation: " + indentation + " " + firstExpr.initTok.column + " " + firstExpr.gen());
Expr prevExpr = firstExpr;
boolean second = true;
for (; ; ) {
Token nextToken = nextToken();
//System.out.println("NextExpr: " + nextToken.column + " " + initTok.column + " " + nextToken.toString() + "'");
if (nextToken.type == Token.Type.EOF ||
nextToken.type == Token.Type.COLON ||
nextToken.type == Token.Type.RPAREN ||
nextToken.type == Token.Type.RBRACE ||
nextToken.type == Token.Type.RCURLY
) {
//System.out.println("NextExpr EOF: " + nextToken);
unreadToken(nextToken);
break;
} else if (nextToken.column == firstExpr.initTok.column ||
(nextToken.line == prevExpr.initTok.line && nextToken.line != initTok.line)
// || ((nextToken.line == prevExpr.initTok.line) && !second)
) {
unreadToken(nextToken);
Expr nextExpr = readExpr();
//System.out.println("NextExpr add: " + nextExpr.gen());
body.add(nextExpr);
prevExpr = nextExpr;
} else if (nextToken.column > firstExpr.initTok.column && nextToken.line != initTok.line) {
//System.out.println("NextExpr throw: " + nextToken);
Util.sneakyThrow(new ParserException("Bad indentation, greater than "
+ firstExpr.initTok.column + ": " + nextToken.str, nextToken));
} else /*if (!checkIndentation(nextToken.column)) {
// System.out.println(">>: checkIndentation: " + indentation);
// popIndentation();
Util.sneakyThrow(new ParserException("Bad indentation for: '" + nextToken.str
+ "', available: " + indentation.toString() + ", but get: " + nextToken.column + " "
+ ", at line: " + nextToken.line
+ ", column: " + nextToken.column));
break;
} else */ {
//System.out.println(">>block unread: " + nextToken);
unreadToken(nextToken);
break;
}
second = false;
}
popIndentation();
popIndentation();
// System.out.println(">>> return Block 2: " + body);
return body;
}
public class DelimitedListResult {
public ArrayList<Expr> a;
public Expr last;
public DelimitedListResult(ArrayList<Expr> a, Expr last) {
this.a = a;
this.last = last;
}
}
public DelimitedListResult readDelimitedList(Token.Type tokDelim) {
Expr onReturnExpr = new Expr();
ArrayList<Expr> a = new ArrayList<Expr>();
for (; ; ) {
Expr form = readExpr(tokDelim, onReturnExpr);
if (form == EofExpr.EOF_EXPR) {
throw Util.runtimeException("EOF while reading");
} else if (form == onReturnExpr) {
return new DelimitedListResult(a, form);
}
a.add(form);
}
}
public ArrayList<Token> readAll() {
ArrayList<Token> arr = new ArrayList<Token>();
//Token t = LexerReader.read(charReader);
Token t = lexerReader.read();
curLine = t.line;
if (t.column != lastIndentation()) {
Util.runtimeException("EOF while reading character");
}
while (t.type != Token.Type.EOF) {
arr.add(t);
// t = LexerReader.read(charReader);
t = lexerReader.read();
if (t.type == Token.Type.COLON) {
}
}
arr.add(t);
return arr;
}
public static ArrayList<Expr> readString(String s) {
ArrayList<Expr> arr = new ArrayList<Expr>();
Parser p = new Parser(s);
for (; ; ) {
Expr e = p.readExpr();
if (e == EofExpr.EOF_EXPR) break;
if (e.initTok.column != 1) {
Util.sneakyThrow(new ParserException("Top level expression should start at column 1, but got: '"
+ e.initTok.str, e.initTok));
}
arr.add(e);
}
return arr;
}
public static String genStr(String s) {
ArrayList<Expr> arr = readString(s);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < arr.size() - 1; i++) {
Expr e = arr.get(i);
sb.append(e.gen());
sb.append("\n\n");
}
if (!arr.isEmpty()) {
sb.append(arr.get(arr.size() - 1).gen());
}
return sb.toString();
}
}