forked from ballerina-platform/nballerina
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparseTypeTest.bal
56 lines (50 loc) · 1.51 KB
/
parseTypeTest.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
public type TypeProjection record {
Identifier identifier;
int|Identifier index;
};
public type TypeTest record {
SubtypeTestOp op;
Identifier|TypeProjection left;
Identifier|TypeProjection right;
};
public type SubtypeTestOp "<" | "=" | "<>";
public type Identifier string;
public function parseTypeTest(string str) returns TypeTest|error {
SourceFile file = createSourceFile([str], { filename: "<internal>" });
Tokenizer tok = new(file);
check tok.advance();
Identifier|TypeProjection left = check parseTypeProjection(tok);
Token? t = tok.current();
if t is "<"|"=" {
check tok.advance();
SubtypeTestOp op;
if tok.current() is ">" {
check tok.advance();
op = "<>";
}
else {
op = t;
}
Identifier|TypeProjection right = check parseTypeProjection(tok);
return { op, left, right };
}
return parseError(tok);
}
function parseTypeProjection(Tokenizer tok) returns Identifier|TypeProjection|error {
Identifier identifier = check tok.expectIdentifier();
if tok.current() is "[" {
check tok.advance();
Token? t = tok.current();
int|Identifier index;
if t is [DECIMAL_NUMBER, string] {
index = check int:fromString(t[1]);
check tok.advance();
}
else {
index = check tok.expectIdentifier();
}
check tok.expect("]");
return { identifier, index};
}
return identifier;
}