-
Notifications
You must be signed in to change notification settings - Fork 46
/
preparse.bal
93 lines (85 loc) · 2.94 KB
/
preparse.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
import wso2/nballerina.comm.err;
const PREPARSE_TYPE_DESC = true;
const PREPARSE_EXPR = false;
type CLOSE_BRACKET ")" | "]" | "}" | "|}";
final readonly & map<CLOSE_BRACKET> closeBracketMap = {
"(": ")",
"[": "]",
"{" : "}",
"{|": "|}"
};
// Returns `true` if a statement whose first token was `(` begins a type descriptor rather than an expression
// (implying that the statement is a local variable declaration rather than a method call).
// This is a preparse: the statement will be parsed again according to the value returned.
function preparseParenTypeDesc(Tokenizer tok) returns boolean|err:Syntax {
return preparseBracketedTypeDesc(tok, ")");
}
function preparseTupleTypeDesc(Tokenizer tok) returns boolean|err:Syntax {
return preparseBracketedTypeDesc(tok, "]");
}
function preparseBracketedTypeDesc(Tokenizer tok, CLOSE_BRACKET close) returns boolean|err:Syntax {
check tok.advance();
boolean? parenResult = check preparseBracketed(tok, close);
if parenResult != () {
return parenResult;
}
Token? t = tok.current();
while t == "[" {
check tok.advance();
boolean? squareResult = check preparseBracketed(tok, "]");
if squareResult != () {
return squareResult;
}
t = tok.current();
}
if t == () {
return tok.err("incomplete statement");
}
return t != "." && t !is AssignOp;
}
function preparseBracketed(Tokenizer tok, CLOSE_BRACKET close) returns err:Syntax|boolean? {
while true {
Token? t = tok.current();
match t {
() => {
return tok.err(`missing ${close}`);
}
"." | "check" | "checkpanic" | "is" => {
return PREPARSE_EXPR;
}
";" => {
if close != "|}" && close != "}" {
return tok.err(`missing ${close}`);
}
check tok.advance();
}
"(" | "[" | "{" | "{|" => {
check tok.advance();
boolean? result = check preparseBracketed(tok, closeBracketMap.get(<string>t));
if result != () {
return result;
}
}
")" | "]" | "}" | "|}" => {
if t == close {
check tok.advance();
break;
}
return tok.err(`mismatched close bracket: expected ${close}`);
}
_ => {
check tok.advance();
}
}
}
return ();
}
// Returns `true` if a statement that starts with an unqualified identifier followed by `[` begins a type descriptor rather than an expression
function preparseArrayTypeDesc(Tokenizer tok) returns boolean|err:Syntax {
check tok.advance();
if tok.currentIsNoSpaceColon() {
check tok.advance();
_ = check tok.expectIdentifier();
}
return preparseBracketedTypeDesc(tok, "]");
}