-
Notifications
You must be signed in to change notification settings - Fork 46
/
parseExpr.bal
614 lines (581 loc) · 20.5 KB
/
parseExpr.bal
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
import wso2/nballerina.comm.err;
type SpecialMethodName "map";
function parseExpr(Tokenizer tok) returns Expr|err:Syntax {
return parseLogicalOrExpr(tok);
}
function parseLogicalOrExpr(Tokenizer tok) returns Expr|err:Syntax {
Position startPos = tok.currentStartPos();
Expr expr = check parseLogicalAndExpr(tok);
while true {
Token? t = tok.current();
if t == "||" {
Position opPos = tok.currentStartPos();
check tok.advance();
Expr right = check parseLogicalAndExpr(tok);
Position endPos = tok.previousEndPos();
BinaryLogicalExpr bin = { startPos, endPos, opPos, logicalOp: "||", left: expr, right };
expr = bin;
}
else {
break;
}
}
return expr;
}
function parseLogicalAndExpr(Tokenizer tok) returns Expr|err:Syntax {
Position startPos = tok.currentStartPos();
Expr expr = check parseBitwiseOrExpr(tok);
while true {
Token? t = tok.current();
if t == "&&" {
Position opPos = tok.currentStartPos();
check tok.advance();
Expr right = check parseBitwiseOrExpr(tok);
Position endPos = tok.previousEndPos();
BinaryLogicalExpr bin = { startPos, endPos, opPos, logicalOp: "&&", left: expr, right };
expr = bin;
}
else {
break;
}
}
return expr;
}
function parseBitwiseOrExpr(Tokenizer tok) returns Expr|err:Syntax {
Position startPos = tok.currentStartPos();
Expr expr = check parseBitwiseXorExpr(tok);
while true {
Token? t = tok.current();
if t == "|" {
Position opPos = tok.currentStartPos();
check tok.advance();
Expr right = check parseBitwiseXorExpr(tok);
Position endPos = tok.previousEndPos();
BinaryBitwiseExpr bin = { startPos, endPos, opPos, bitwiseOp: t, left: expr, right };
expr = bin;
}
else {
break;
}
}
return expr;
}
function parseBitwiseXorExpr(Tokenizer tok) returns Expr|err:Syntax {
Position startPos = tok.currentStartPos();
Expr expr = check parseBitwiseAndExpr(tok);
while true {
Token? t = tok.current();
if t == "^" {
Position opPos = tok.currentStartPos();
check tok.advance();
Expr right = check parseBitwiseAndExpr(tok);
Position endPos = tok.previousEndPos();
BinaryBitwiseExpr bin = { startPos, endPos, opPos, bitwiseOp: t, left: expr, right };
expr = bin;
}
else {
break;
}
}
return expr;
}
function parseBitwiseAndExpr(Tokenizer tok) returns Expr|err:Syntax {
Position startPos = tok.currentStartPos();
Expr expr = check parseEqualityExpr(tok);
while true {
Token? t = tok.current();
if t == "&" {
Position opPos = tok.currentStartPos();
check tok.advance();
Expr right = check parseEqualityExpr(tok);
Position endPos = tok.previousEndPos();
BinaryBitwiseExpr bin = { startPos, endPos, opPos, bitwiseOp: t, left: expr, right };
expr = bin;
}
else {
break;
}
}
return expr;
}
function parseEqualityExpr(Tokenizer tok) returns Expr|err:Syntax {
Position startPos = tok.currentStartPos();
Expr expr = check parseRelationalExpr(tok);
while true {
Token? t = tok.current();
if t is BinaryEqualityOp {
Position opPos = tok.currentStartPos();
check tok.advance();
Expr right = check parseRelationalExpr(tok);
Position endPos = tok.previousEndPos();
BinaryEqualityExpr bin = { startPos, endPos, opPos, equalityOp: t, left: expr, right };
expr = bin;
}
else {
break;
}
}
return expr;
}
function parseRelationalExpr(Tokenizer tok) returns Expr|err:Syntax {
Position startPos = tok.currentStartPos();
Expr expr = check parseShiftExpr(tok);
Token? t = tok.current();
if t is BinaryRelationalOp {
Position opPos = tok.currentStartPos();
check tok.advance();
Expr right = check parseShiftExpr(tok);
Position endPos = tok.previousEndPos();
BinaryRelationalExpr bin = { startPos, endPos, opPos, relationalOp: t, left: expr, right };
return bin;
}
else if t == "is" {
return finishTypeTestExpr(tok, expr, false, startPos);
}
if t == "!" {
check tok.advance();
Token? t2 = tok.current();
if t2 is "is" {
return finishTypeTestExpr(tok, expr, true, startPos);
}
return tok.err("invalid operator");
}
else {
return expr;
}
}
function finishTypeTestExpr(Tokenizer tok, Expr expr, boolean negated, Position startPos) returns TypeTestExpr|err:Syntax {
Position kwPos = tok.currentStartPos();
tok.setMode(MODE_TYPE_DESC);
check tok.advance();
TypeDesc td = check parseTypeDesc(tok);
Position endPos = tok.previousEndPos();
tok.setMode(MODE_NORMAL);
return { startPos, endPos, td, left: expr, negated, kwPos };
}
function parseRangeExpr(Tokenizer tok) returns RangeExpr|err:Syntax {
Position startPos = tok.currentStartPos();
Expr lower = check parseAdditiveExpr(tok);
Position opPos = check tok.expectStart("..<");
Expr upper = check parseAdditiveExpr(tok);
Position endPos = tok.previousEndPos();
return { startPos, endPos, opPos, lower, upper };
}
function parseShiftExpr(Tokenizer tok) returns Expr|err:Syntax {
Position startPos = tok.currentStartPos();
Expr expr = check parseAdditiveExpr(tok);
while true {
Token? t = tok.current();
if t is BitwiseShiftOp {
Position opPos = tok.currentStartPos();
check tok.advance();
Expr right = check parseAdditiveExpr(tok);
Position endPos = tok.previousEndPos();
BinaryBitwiseExpr shift = { startPos, endPos, opPos, bitwiseOp: t, left: expr, right };
expr = shift;
}
else {
break;
}
}
return expr;
}
function parseAdditiveExpr(Tokenizer tok) returns Expr|err:Syntax {
Position startPos = tok.currentStartPos();
Expr expr = check parseMultiplicativeExpr(tok);
while true {
Token? t = tok.current();
if t is ("+"|"-") {
Position opPos = tok.currentStartPos();
check tok.advance();
Expr right = check parseMultiplicativeExpr(tok);
Position endPos = tok.previousEndPos();
BinaryArithmeticExpr bin = { startPos, endPos, opPos, arithmeticOp: t, left: expr, right };
expr = bin;
}
else {
break;
}
}
return expr;
}
function parseMultiplicativeExpr(Tokenizer tok) returns Expr|err:Syntax {
Position startPos = tok.currentStartPos();
Expr expr = check parseUnaryExpr(tok);
while true {
Token? t = tok.current();
if t is ("*"|"/"|"%") {
Position opPos = tok.currentStartPos();
check tok.advance();
Expr right = check parseUnaryExpr(tok);
Position endPos = tok.previousEndPos();
BinaryArithmeticExpr bin = { startPos, endPos, opPos, arithmeticOp: t, left: expr, right };
expr = bin;
}
else {
break;
}
}
return expr;
}
function parseUnaryExpr(Tokenizer tok) returns Expr|err:Syntax {
Token? t = tok.current();
Position startPos = tok.currentStartPos();
if t is "-"|"!"|"~" {
Position opPos = tok.currentStartPos();
check tok.advance();
Expr operand = check parseUnaryExpr(tok);
Position endPos = tok.previousEndPos();
UnaryExpr expr = { startPos, endPos, opPos, op: t, operand };
return expr;
}
else if t is CheckingKeyword {
Position kwPos = tok.currentStartPos();
check tok.advance();
Expr operand = check parseUnaryExpr(tok);
Position endPos = tok.previousEndPos();
CheckingExpr expr = { startPos, endPos, kwPos, checkingKeyword: t, operand };
return expr;
}
else if t is "<" {
return parseTypeCastExpr(tok, startPos);
}
return parsePrimaryExpr(tok);
}
function parseTypeCastExpr(Tokenizer tok, Position startPos) returns Expr|err:Syntax {
tok.setMode(MODE_TYPE_DESC);
Position opPos = tok.currentStartPos();
check tok.advance();
TypeDesc td = check parseTypeDesc(tok);
check tok.expect(">");
tok.setMode(MODE_NORMAL);
Expr operand = check parseUnaryExpr(tok);
Position endPos = tok.previousEndPos();
TypeCastExpr expr = { startPos, endPos, opPos, td, operand };
return expr;
}
function parsePrimaryExpr(Tokenizer tok) returns Expr|err:Syntax {
Position startPos = tok.currentStartPos();
return finishPrimaryExpr(tok, check startPrimaryExpr(tok), startPos);
}
function startPrimaryExpr(Tokenizer tok) returns Expr|err:Syntax {
Token? t = tok.current();
Position startPos = tok.currentStartPos();
Position endPos = tok.currentEndPos();
if t is [IDENTIFIER, string] {
string? prefix;
string name;
check tok.advance();
[prefix, name] = check parseOptQualIdentifier(tok, t[1]);
if tok.current() == "(" {
return finishFunctionCallExpr(tok, prefix, name, startPos);
}
endPos = tok.previousEndPos();
return { startPos, endPos, prefix, name, qNamePos: startPos };
}
else if t is [DECIMAL_NUMBER, string] {
IntLiteralExpr expr = { startPos, endPos, base: 10, digits: t[1] };
check tok.advance();
return expr;
}
else if t is [DECIMAL_FP_NUMBER, string, FpTypeSuffix?] {
FpLiteralExpr expr = { startPos, endPos, untypedLiteral: t[1], typeSuffix: t[2] };
check tok.advance();
return expr;
}
else if t is [HEX_INT_LITERAL, string] {
IntLiteralExpr expr = { startPos, endPos, base: 16, digits: t[1] };
check tok.advance();
return expr;
}
else if t is [STRING_LITERAL, string] {
LiteralExpr expr = { startPos, endPos, value: t[1] };
check tok.advance();
return expr;
}
else if t == "(" {
check tok.advance();
if tok.current() == ")" {
endPos = tok.currentEndPos();
check tok.advance();
LiteralExpr expr = { startPos, endPos, value: () };
return expr;
}
Expr expr = check parseExpr(tok);
endPos = check tok.expectEnd(")");
return { startPos, endPos, expr };
}
else if t is "true"|"false" {
check tok.advance();
LiteralExpr expr = { startPos, endPos, value: t == "true" };
return expr;
}
else if t is "null" {
check tok.advance();
LiteralExpr expr = { startPos, endPos, value: () };
return expr;
}
else if t is "error" {
Position kwPos = tok.currentStartPos();
check tok.advance();
check tok.expect("(");
Expr message = check parseExpr(tok);
endPos = check tok.expectEnd(")");
return { startPos, endPos, message, kwPos };
}
else if t == "[" {
check tok.advance();
var [members, _] = check parseExprList(tok, "]");
endPos = tok.previousEndPos();
return { startPos, endPos, opPos: startPos, members };
}
else if t == "{" {
check tok.advance();
Field[] fields = check parseFields(tok);
endPos = tok.previousEndPos();
return { startPos, endPos, opPos: startPos, fields };
}
else if t == "function" {
check tok.advance();
return parseExplicitAnonymousFunctionExpr(tok, startPos);
}
else {
return parseError(tok);
}
}
function parseExplicitAnonymousFunctionExpr(Tokenizer tok, Position startPos) returns ExplicitAnonymousFunctionExpr|err:Syntax {
FunctionParam [] params = [];
FunctionTypeDesc typeDesc = check parseFunctionTypeDesc(tok, params);
StmtBlock body = check parseStmtBlock(tok);
Position endPos = tok.previousEndPos();
AnonFunction func = { startPos, endPos, params, body, typeDesc };
return { startPos, endPos, func };
}
function finishPrimaryExpr(Tokenizer tok, Expr expr, Position startPos) returns Expr|err:Syntax {
Token? t = tok.current();
Position opPos = tok.currentStartPos();
if t == "[" {
check tok.advance();
Expr index = check parseExpr(tok);
Position accessEndPos = check tok.expectEnd("]");
MemberAccessExpr accessExpr = { startPos, endPos: accessEndPos, opPos, container: expr, index };
return finishPrimaryExpr(tok, accessExpr, startPos);
}
else if t == "." {
opPos = tok.currentStartPos();
check tok.advance();
Position qnamePos = tok.currentStartPos();
string name = check parseIdentifierOrMethodName(tok);
if tok.current() == "(" {
return finishPrimaryExpr(tok, check finishMethodCallExpr(tok, expr, name, startPos, qnamePos, opPos), startPos);
}
else {
Position endPos = tok.previousEndPos();
FieldAccessExpr fieldAccessExpr = { startPos, endPos, opPos, container: expr, fieldName: name };
return finishPrimaryExpr(tok, fieldAccessExpr, startPos);
}
}
else {
return expr;
}
}
function parseIdentifierOrMethodName(Tokenizer tok) returns string|err:Syntax {
Token? t = tok.current();
if t is SpecialMethodName {
check tok.advance();
if tok.current() == "(" {
return t;
}
return parseError(tok, "expected open parenthesis");
}
return tok.expectIdentifier();
}
// Called with current token as "("
function finishMethodCallExpr(Tokenizer tok, Expr target, string methodName, Position startPos, Position namePos, Position opPos) returns MethodCallExpr|err:Syntax {
Position openParenPos = tok.currentStartPos();
check tok.advance();
var [args, closeParenPos] = check parseExprList(tok, ")");
Position endPos = tok.previousEndPos();
return { startPos, endPos, opPos, namePos, openParenPos, closeParenPos, target, methodName, args };
}
function finishFunctionCallExpr(Tokenizer tok, string? prefix, string funcName, Position startPos) returns FunctionCallExpr|err:Syntax {
Position openParenPos = tok.currentStartPos();
check tok.advance();
var [args, closeParenPos] = check parseExprList(tok, ")");
Position endPos = tok.previousEndPos();
return { startPos, endPos, openParenPos, closeParenPos, qNamePos: startPos, funcName, args, prefix };
}
function parseExprList(Tokenizer tok, "]"|")" terminator) returns [Expr[], Position]|err:Syntax {
Expr[] exprs = [];
if tok.current() != terminator {
while true {
Expr expr = check parseExpr(tok);
exprs.push(expr);
Token? t = tok.current();
if t == "," {
check tok.advance();
}
else if t == terminator {
break;
}
else {
return parseError(tok, "invalid expression list");
}
}
}
Position closeTerminatorPos = tok.currentStartPos();
check tok.advance();
return [exprs, closeTerminatorPos];
}
function parseFields(Tokenizer tok) returns Field[]|err:Syntax {
Field[] fields = [];
if tok.current() != "}" {
while true {
Field f = check parseField(tok);
fields.push(f);
Token? t = tok.current();
if t == "," {
check tok.advance();
}
else if t == "}" {
break;
}
else {
return parseError(tok, "invalid field list");
}
}
}
check tok.advance();
return fields;
}
function parseField(Tokenizer tok) returns Field|err:Syntax {
Token? t = tok.current();
Position startPos = tok.currentStartPos();
if t is [IDENTIFIER|STRING_LITERAL, string] {
boolean isIdentifier = t[0] == IDENTIFIER;
string name = t[1];
// Don't report an error for duplicates here
// (it's not a syntax error)
// Instead save the position and report during codeGen
check tok.advance();
Position colonPos = tok.currentStartPos();
check tok.expect(":");
Expr value = check parseExpr(tok);
Position endPos = tok.previousEndPos();
Field f = { startPos, endPos, colonPos, name, value, isIdentifier };
return f;
}
return tok.err("expected field name");
}
// This is simple-const-expr in the spec
// This is used for match patterns
// Will also be used for type descriptors
function parseSimpleConstExpr(Tokenizer tok) returns SimpleConstExpr|err:Syntax {
Token? t = tok.current();
Position startPos = tok.currentStartPos();
if t == "-" {
Position opPos = tok.currentStartPos();
check tok.advance();
NumericLiteralExpr operand = check parseNumericLiteralExpr(tok);
Position endPos = tok.previousEndPos();
SimpleConstNegateExpr expr = { startPos, endPos, opPos, operand };
return expr;
}
match t {
[STRING_LITERAL, var value] => {
Position endPos = tok.currentEndPos();
LiteralExpr expr = { startPos, endPos, value };
check tok.advance();
return expr;
}
"null" => {
Position endPos = tok.currentEndPos();
LiteralExpr expr = { startPos, endPos, value: () };
return expr;
}
"(" => {
check tok.advance();
Position endPos = check tok.expectEnd(")");
LiteralExpr expr = { startPos, endPos, value: () };
return expr;
}
"true"|"false" => {
Position endPos = tok.currentEndPos();
check tok.advance();
LiteralExpr expr = { startPos, endPos, value: t == "true" };
return expr;
}
[DECIMAL_FP_NUMBER, _, _] => {
return parseNumericLiteralExpr(tok);
}
}
return parseArrayLengthExpr(tok);
}
function parseArrayLengthExpr(Tokenizer tok) returns SimpleConstExpr|err:Syntax {
Token? t = tok.current();
Position startPos = tok.currentStartPos();
match t {
[IDENTIFIER, var identifier] => {
Position endPos = tok.currentEndPos();
check tok.advance();
var [prefix, name] = check parseOptQualIdentifier(tok, identifier);
if prefix != () {
endPos = tok.previousEndPos();
}
return { startPos, endPos, prefix, name, qNamePos: startPos };
}
[DECIMAL_NUMBER, _]
| [HEX_INT_LITERAL, _] => {
return parseNumericLiteralExpr(tok);
}
}
return parseError(tok);
}
function parseOptQualIdentifier(Tokenizer tok, string identifier) returns [string?, string]|err:Syntax {
if tok.currentIsNoSpaceColon() {
check tok.advance();
return [identifier, check tok.expectIdentifier()];
}
else {
return [(), identifier];
}
}
function parseNumericLiteralExpr(Tokenizer tok) returns NumericLiteralExpr|err:Syntax {
Token? t = tok.current();
Position startPos = tok.currentStartPos();
match t {
[DECIMAL_NUMBER, _]
| [HEX_INT_LITERAL, _] => {
return parseIntLiteralExpr(tok);
}
[DECIMAL_FP_NUMBER, var untypedLiteral, var typeSuffix] => {
Position endPos = tok.currentEndPos();
check tok.advance();
return { startPos, endPos, untypedLiteral, typeSuffix };
}
}
return parseError(tok, "expected numeric literal");
}
// XXX This can merged into parseNumericLiteralExpr when we add float support
// outside types
function parseIntLiteralExpr(Tokenizer tok) returns IntLiteralExpr|err:Syntax {
Token? t = tok.current();
Position startPos = tok.currentStartPos();
Position endPos = tok.currentEndPos();
match t {
[DECIMAL_NUMBER, var digits] => {
check tok.advance();
IntLiteralExpr expr = { startPos, endPos, base: 10, digits };
return expr;
}
[HEX_INT_LITERAL, var digits] => {
check tok.advance();
IntLiteralExpr expr = { startPos, endPos, base: 16, digits };
return expr;
}
}
return parseError(tok, "expected integer literal");
}
public function intFromIntLiteral(IntLiteralBase base, string digits) returns int|error {
return base == 10 ? int:fromString(digits) : int:fromHexString(digits);
}