-
Notifications
You must be signed in to change notification settings - Fork 46
/
parseStmt.bal
398 lines (377 loc) · 14.3 KB
/
parseStmt.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
import wso2/nballerina.comm.err;
function parseStmtBlock(Tokenizer tok) returns StmtBlock|err:Syntax {
if tok.current() == "{" {
Position startPos = tok.currentStartPos();
Stmt[] stmts = [];
check tok.advance();
while tok.current() != "}" {
stmts.push(check parseStmt(tok));
}
Position endPos = tok.currentEndPos();
Position closeBracePos = tok.currentStartPos();
check tok.advance();
return { startPos, endPos, stmts, closeBracePos };
}
return parseError(tok, "unhandled condition in statement block");
}
function parseStmt(Tokenizer tok) returns Stmt|err:Syntax {
Token? cur = tok.current();
Position startPos = tok.currentStartPos();
match cur {
[IDENTIFIER, var identifier] => {
var peeked = tok.peek(skipQualIdent=true);
if peeked is "|" | "?" | "&" | "_" | IDENTIFIER {
TypeDesc td = check parseTypeDesc(tok);
return finishVarDeclStmt(tok, td, startPos);
}
else if peeked == "[" {
boolean isTypeDesc = check savePreparseRestore(tok, preparseArrayTypeDesc);
if isTypeDesc {
return parseVarDeclStmt(tok, startPos);
}
}
check tok.advance();
return finishIdentifierStmt(tok, identifier, startPos, startPos);
}
"_" => {
check tok.advance();
if tok.current() == "=" {
return finishAssignStmt(tok, WILDCARD, "=", startPos);
}
}
"return" => {
check tok.advance();
return parseReturnStmt(tok, startPos);
}
"panic" => {
check tok.advance();
return parsePanicStmt(tok, startPos);
}
"break"|"continue" => {
check tok.advance();
Position endPos = check tok.expectEnd(";");
// JBUG #33341 cast
BreakContinueStmt stmt = { startPos, endPos, breakContinue:<BreakContinue>cur };
return stmt;
}
"if" => {
check tok.advance();
return parseIfElseStmt(tok, startPos);
}
"match" => {
check tok.advance();
return parseMatchStmt(tok, startPos);
}
"while" => {
check tok.advance();
return parseWhileStmt(tok, startPos);
}
"foreach" => {
check tok.advance();
return parseForeachStmt(tok, startPos);
}
"final" => {
check tok.advance();
return parseVarDeclStmt(tok, startPos, true);
}
"error" => {
if tok.peek() == "(" {
return parseMethodCallStmt(tok);
}
else {
return finishVarDeclStmt(tok, check parseTypeDesc(tok), startPos);
}
}
"check"|"checkpanic" => {
check tok.advance();
// JBUG #33341 cast
return finishCheckingCallStmt(tok, <CheckingKeyword>cur, startPos);
}
var td if td is SubsetBuiltinTypeName|"map"|"record" => {
return parseVarDeclStmt(tok, startPos);
}
"(" => {
if check savePreparseRestore(tok, preparseParenTypeDesc) {
return parseVarDeclStmt(tok, startPos);
}
return parseMethodCallStmt(tok);
}
[DECIMAL_NUMBER, _]
| [STRING_LITERAL, _]
| "true"
| "false"
| "null"
| [HEX_INT_LITERAL, _]
| [DECIMAL_FP_NUMBER, _, _] => {
var peeked = tok.peek();
if peeked == "." || (peeked == "[" && !check savePreparseRestore(tok, preparseArrayTypeDesc)) {
return parseMethodCallStmt(tok);
}
return parseVarDeclStmt(tok, startPos);
}
"-" => {
return parseVarDeclStmt(tok, startPos);
}
"[" => {
if check savePreparseRestore(tok, preparseTupleTypeDesc) {
return parseVarDeclStmt(tok, startPos);
}
return parseMethodCallStmt(tok);
}
}
return parseError(tok, "unhandled statement");
}
type AssignOp "="|CompoundAssignOp;
type PreparseFunc function(Tokenizer) returns boolean|err:Syntax;
function savePreparseRestore(Tokenizer tok, PreparseFunc func) returns boolean|err:Syntax {
TokenizerState state = tok.save();
boolean isTypeDesc = check func(tok);
tok.restore(state);
return isTypeDesc;
}
// We only call this when we know (through preparsing) that the statement does not start with a type descriptor.
function finishIdentifierStmt(Tokenizer tok, string name1, Position startPos, Position qNamePos) returns Stmt|err:Syntax {
string name;
string? prefix;
if tok.currentIsNoSpaceColon() {
check tok.advance();
name = check tok.expectIdentifier();
prefix = name1;
}
else {
name = name1;
prefix = ();
}
if tok.current() == "(" {
FunctionCallExpr expr = check finishFunctionCallExpr(tok, prefix, name, startPos);
return finishCallStmt(tok, expr, startPos);
}
Position endPos = tok.previousEndPos();
LExpr lExpr = { startPos, endPos, name, qNamePos: startPos, prefix };
Token? cur = tok.current();
while true {
Position opPos = tok.currentStartPos();
if cur == "." {
check tok.advance();
Position namePos = tok.currentStartPos();
name = check parseIdentifierOrMethodName(tok);
if tok.current() == "(" {
return finishCallStmt(tok, check finishMethodCallExpr(tok, lExpr, name, startPos, namePos, opPos), startPos);
}
lExpr = { startPos, endPos, fieldName: name, container: lExpr, opPos };
}
else if cur == "[" {
check tok.advance();
Expr index = check parseExpr(tok);
endPos = check tok.expectEnd("]");
lExpr = { startPos, endPos, container: lExpr, index, opPos };
}
else {
break;
}
cur = tok.current();
}
if cur is AssignOp {
return finishAssignStmt(tok, lExpr, cur, startPos);
}
return parseError(tok, "invalid statement");
}
function parseMethodCallStmt(Tokenizer tok) returns CallStmt|err:Syntax {
Position startPos = tok.currentStartPos();
Expr callExpr = check startPrimaryExpr(tok);
Token? cur = tok.current();
if cur == "." || cur == "[" {
callExpr = check finishPrimaryExpr(tok, callExpr, startPos);
if callExpr is MethodCallExpr {
Position endPos = check tok.expectEnd(";");
return { startPos, endPos, callExpr };
}
}
return parseError(tok, "expression not allowed as a statement");
}
function finishCallStmt(Tokenizer tok, CallExpr callExpr, Position startPos) returns Stmt|err:Syntax {
Expr primary = check finishPrimaryExpr(tok, callExpr, startPos);
Position endPos = check tok.expectEnd(";");
CallStmt stmt;
if primary === callExpr {
stmt = { startPos, endPos, callExpr };
}
else if primary is MethodCallExpr {
stmt = { startPos, endPos, callExpr: primary };
}
else {
return parseError(tok, "member access expr not allowed as a statement");
}
return stmt;
}
// This function is called with the token after the checkingKeyword as the current token
function finishCheckingCallStmt(Tokenizer tok, CheckingKeyword checkingKeyword, Position kwPos) returns CallStmt|err:Syntax {
Token? t = tok.current();
if t is "check"|"checkpanic" {
// multiple checkingKeywords in the statement call (ex: check check fn();)
Position innerKwPos = tok.currentStartPos();
check tok.advance();
CallStmt operandStmt = check finishCheckingCallStmt(tok, t, innerKwPos);
callStmtAddChecking(kwPos, tok.previousEndPos(), operandStmt, checkingKeyword);
return operandStmt;
}
else if t == "(" {
CallStmt operandStmt = check parseMethodCallStmt(tok);
callStmtAddChecking(kwPos, tok.previousEndPos(), operandStmt, checkingKeyword);
return operandStmt;
}
Expr operand = check parsePrimaryExpr(tok);
if operand is FunctionCallExpr|MethodCallExpr {
CheckingCallExpr callExpr = { startPos: kwPos, endPos: operand.endPos, checkingKeyword, kwPos, operand};
Position endPos = check tok.expectEnd(";");
return { startPos: kwPos, endPos, callExpr };
}
return parseError(tok, "function call, method call or checking expression expected");
}
function callStmtAddChecking(Position kwPos, Position endPos, CallStmt stmt, CheckingKeyword checkingKeyword) {
stmt.callExpr = { startPos: kwPos, endPos: stmt.callExpr.endPos, checkingKeyword, kwPos, operand: stmt.callExpr };
stmt.startPos = kwPos;
}
function finishAssignStmt(Tokenizer tok, LExpr|WILDCARD lValue, AssignOp op, Position startPos) returns AssignStmt|CompoundAssignStmt|err:Syntax {
Position opPos = tok.currentStartPos();
check tok.advance();
Expr expr = check parseExpr(tok);
Position endPos = check tok.expectEnd(";");
// the `||` makes the narrowings work
if op is "=" || lValue == WILDCARD {
AssignStmt stmt = { startPos, endPos, opPos, lValue, expr };
return stmt;
}
else {
string opStr = op;
BinaryArithmeticOp|BinaryBitwiseOp binOp = <BinaryArithmeticOp|BinaryBitwiseOp> opStr.substring(0, opStr.length() - 1);
CompoundAssignStmt stmt = { startPos, endPos, opPos, lValue, expr, op: binOp };
return stmt;
}
}
function parseVarDeclStmt(Tokenizer tok, Position startPos, boolean isFinal = false) returns VarDeclStmt|err:Syntax {
TypeDesc td = check parseTypeDesc(tok);
return finishVarDeclStmt(tok, td, startPos, isFinal);
}
function finishVarDeclStmt(Tokenizer tok, TypeDesc td, Position startPos, boolean isFinal = false) returns VarDeclStmt|err:Syntax {
Token? cur = tok.current();
Position namePos = tok.currentStartPos();
string|WILDCARD name;
if cur == "_" {
name = WILDCARD;
check tok.advance();
}
else {
name = check tok.expectIdentifier();
}
// initExpr is required in the subset
Position opPos = check tok.expectStart("=");
Expr initExpr = check parseExpr(tok);
Position endPos = check tok.expectEnd(";");
return { startPos, endPos, opPos, td, name, namePos, initExpr, isFinal };
}
function parseReturnStmt(Tokenizer tok, Position startPos) returns ReturnStmt|err:Syntax {
Expr? returnExpr;
Position endPos;
if tok.current() == ";" {
endPos = tok.currentEndPos();
returnExpr = ();
check tok.advance();
}
else {
returnExpr = check parseExpr(tok);
endPos = check tok.expectEnd(";");
}
return { startPos, endPos, returnExpr, kwPos: startPos };
}
function parsePanicStmt(Tokenizer tok, Position startPos) returns PanicStmt|err:Syntax {
Expr panicExpr = check parseExpr(tok);
Position endPos = check tok.expectEnd(";");
return { startPos, endPos, panicExpr, kwPos: startPos };
}
function parseIfElseStmt(Tokenizer tok, Position startPos) returns IfElseStmt|err:Syntax {
IfElseStmt|StmtBlock? ifFalse;
Expr condition = check parseExpr(tok);
StmtBlock ifTrue = check parseStmtBlock(tok);
Position endPos = tok.previousEndPos();
Token? cur = tok.current();
if cur == "else" {
check tok.advance();
// if exp1 { } else if exp2 { }
if tok.current() == "if" {
Position ifFalseStartPos = tok.currentStartPos();
check tok.advance();
ifFalse = check parseIfElseStmt(tok, ifFalseStartPos);
}
// if exp1 { } else { }
else if tok.current() == "{" {
ifFalse = check parseStmtBlock(tok);
}
else {
return parseError(tok);
}
endPos = tok.previousEndPos();
}
else {
ifFalse = ();
}
return { startPos, endPos, condition, ifTrue, ifFalse };
}
function parseWhileStmt(Tokenizer tok, Position startPos) returns WhileStmt|err:Syntax {
Expr condition = check parseExpr(tok);
StmtBlock body = check parseStmtBlock(tok);
Position endPos = tok.previousEndPos();
return { startPos, endPos, condition, body };
}
function parseForeachStmt(Tokenizer tok, Position startPos) returns ForeachStmt|err:Syntax {
if tok.current() != "int" {
return parseError(tok, "type of foreach variable must be int");
}
check tok.advance();
Position namePos = tok.currentStartPos();
string name = check tok.expectIdentifier();
Position kwPos = check tok.expectStart("in");
RangeExpr range = check parseRangeExpr(tok);
StmtBlock body = check parseStmtBlock(tok);
Position endPos = tok.previousEndPos();
return { startPos, endPos, namePos, kwPos, name, range, body };
}
function parseMatchStmt(Tokenizer tok, Position startPos) returns MatchStmt|err:Syntax {
Expr expr = check parseExpr(tok);
check tok.expect("{");
MatchClause[] clauses = [];
clauses.push(check parseMatchClause(tok));
while tok.current() != "}" {
clauses.push(check parseMatchClause(tok));
}
Position endPos = tok.currentEndPos();
check tok.advance();
return { startPos, endPos, expr, clauses };
}
function parseMatchClause(Tokenizer tok) returns MatchClause|err:Syntax {
Position startPos = tok.currentStartPos();
MatchPattern[] patterns = check parseMatchPatternList(tok);
Position opPos = check tok.expectStart("=>");
StmtBlock block = check parseStmtBlock(tok);
Position endPos = tok.previousEndPos();
return { startPos, endPos, patterns, block, opPos };
}
function parseMatchPatternList(Tokenizer tok) returns MatchPattern[]|err:Syntax {
MatchPattern[] patterns = [];
patterns.push(check parseMatchPattern(tok));
while tok.current() == "|" {
check tok.advance();
patterns.push(check parseMatchPattern(tok));
}
return patterns;
}
function parseMatchPattern(Tokenizer tok) returns MatchPattern|err:Syntax {
Token? cur = tok.current();
if cur is WILDCARD_MATCH_PATTERN {
Position startPos = tok.currentStartPos();
Position endPos = tok.currentEndPos();
check tok.advance();
return <WildcardMatchPattern> { startPos, endPos };
}
return check parseSimpleConstExpr(tok);
}