forked from ballerina-platform/nballerina
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsexprParse.bal
257 lines (240 loc) · 9.11 KB
/
sexprParse.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
import wso2/nballerina.comm.err;
import wso2/nballerina.comm.sexpr;
import wso2/nballerina.types.sexpr as ts;
// Name (ts:AtomRef) to s-expression representation map
public type AtomTableSexpr map<ts:Atom>;
// Name (ts:AtomRef) to SemType map
public type AtomTable map<SemType>;
type SexprTypeParseContext record{|
Env env;
AtomTable atoms;
|};
type SexprAtomParseContext record{|
*SexprTypeParseContext;
AtomTableSexpr atomSexprs = {};
map<Definition> started = {};
|};
type SexprParseContext SexprAtomParseContext|SexprTypeParseContext;
// Remove and parse each type sexpr in `atomSexprs`.
public function atomTableFromSexpr(Env env, AtomTableSexpr atomSexprs) returns AtomTable {
AtomTable atoms = {};
SexprAtomParseContext pc = { env, atoms, atomSexprs };
foreach var [name, atom] in atomSexprs.entries() {
if atoms.hasKey(name) {
continue;
}
atoms[name] = fromAtomSexpr(pc, name, atom);
}
return atoms;
}
// Create a SemType from it's s-expression representation. Expects atomTable mapping from names to
// their SemTypes table for atom reference lookup.
public function fromSexpr(Env env, AtomTable atoms, ts:Type tySexpr) returns SemType {
SexprTypeParseContext pc = { env, atoms };
return fromSexprInternal(pc , tySexpr);
}
final AtomTable namedTypes = {
"never" : NEVER,
"nil" : NIL,
"boolean" : BOOLEAN,
"int" : INT,
"float" : FLOAT,
"decimal" : DECIMAL,
"string" : STRING,
"list" : LIST,
"mapping" : MAPPING,
"table" : TABLE,
"xml" : XML,
"error" : ERROR,
"function": FUNCTION,
"typedesc": TYPEDESC,
"handle" : HANDLE,
"object" : OBJECT,
"stream" : STREAM,
"future" : FUTURE,
"any" : ANY,
"readonly": VAL_READONLY,
"char" : STRING_CHAR,
"byte" : BYTE
};
// If non-builtin identifier (ie: ts:AtomRef) return it else nil.
public function atomRefSexpr(ts:Type tySexpr) returns string? {
if tySexpr is string && namedTypes[tySexpr] == () {
return tySexpr;
}
return ();
}
final map<SemType> & readonly xmlBuiltinSubtypeSexprSemType = {
"element": XML_ELEMENT,
"comment": XML_COMMENT,
"text" : XML_TEXT,
"pi" : XML_PI
};
function fromSexprInternal(SexprParseContext pc, ts:Type tySexpr) returns SemType {
if tySexpr is string {
SemType? ty = namedTypes[tySexpr];
if ty != () {
return ty;
}
SemType? existingAtomTy = pc.atoms[tySexpr]; // tySexpr is AtomRef
if existingAtomTy != () {
return existingAtomTy;
}
if pc is SexprAtomParseContext {
Definition? defn = pc.started[tySexpr];
if defn != () {
return defn.getSemType(pc.env);
}
SemType newAtomTy = fromAtomSexpr(pc, tySexpr, pc.atomSexprs.get(tySexpr));
pc.atoms[tySexpr] = newAtomTy;
return newAtomTy;
}
panic error("Atom not available in the table: " + tySexpr);
}
if tySexpr is int {
return intRange(tySexpr, tySexpr);
}
if tySexpr is boolean {
return booleanConst(tySexpr);
}
if tySexpr is sexpr:String {
return stringConst(tySexpr.s);
}
match <sexpr:Data[]>tySexpr { // JBUG: cast
[] => {
return NIL;
}
["int-range", var min, var max] => {
return intRange(<int>min, <int>max);
}
["float", var { s }] => {
return floatConst(checkpanic float:fromString(s));
}
["decimal", var { s }] => {
return decimalConst(checkpanic decimal:fromString(s));
}
["|", ...var types] => {
SemType result = NEVER;
foreach var ty in types {
result = union(result, fromSexprInternal(pc, <ts:Type>ty));
}
return result;
}
["&", ...var types] => {
SemType result = VAL;
foreach var ty in types {
result = intersect(result, fromSexprInternal(pc, <ts:Type>ty));
}
return result;
}
["!", ...var types] => {
SemType result = NEVER;
foreach var ty in types {
result = union(result, fromSexprInternal(pc, <ts:Type>ty));
}
return complement(result);
}
["xml", 0] => {
return xmlSequence(NEVER);
}
["xml", 1, ...var subTys] => {
SemType result = NEVER;
foreach var ty in subTys {
result = union(result, xmlBuiltinSubtypeSexprSemType.get(<ts:XmlBuiltinSubtypeNames>ty));
}
return result;
}
["xml", "*", ...var subTys] => {
SemType result = NEVER;
foreach var ty in subTys {
result = union(result, xmlBuiltinSubtypeSexprSemType.get(<ts:XmlBuiltinSubtypeNames>ty));
}
return xmlSequence(result);
}
["error", var ty] => {
return errorDetail(fromSexprInternal(pc, <ts:Type>ty));
}
["table", var ty] => {
return tableContaining(pc.env, fromSexprInternal(pc, <ts:Type>ty));
}
["object", var ty] => {
return objectContaining(fromSexprInternal(pc, <ts:Type>ty));
}
_ => {
panic err:impossible("unsupported sexpr SemType:" + tySexpr.toString());
}
}
}
// Parse and return atom, add other encountered atoms during to pc.atoms
function fromAtomSexpr(SexprAtomParseContext pc, string name, ts:Atom atomSexpr) returns SemType {
match atomSexpr {
["list", var members] => {
return fromListSexpr(pc, name, <ts:Type[]>members);
}
["list", var members, var rest] => {
return fromListSexpr(pc, name, <ts:Type[]>members, restSexpr = <ts:Type>rest);
}
["min-length-list", var members, var membersCount] => {
return fromListSexpr(pc, name, <ts:Type[]>members, <int>membersCount);
}
["min-length-list", var members, var membersCount, var rest] => {
return fromListSexpr(pc, name, <ts:Type[]>members, <int>membersCount, <ts:Type>rest);
}
["array", var member] => {
return fromListSexpr(pc, name, restSexpr = <ts:Type>member);
}
["array", var member, var len] => {
return fromListSexpr(pc, name, fixedLength = <int>len, initialSexpr = [<ts:Type>member]);
}
["map", var rest] => {
return fromMappingSexpr(pc, name, restSexpr = <ts:Type>rest);
}
["mapping", var fieldsSexpr] => {
return fromMappingSexpr(pc, name, <ts:Field[]>fieldsSexpr);
}
["mapping", var fieldsSexpr, var rest] => {
return fromMappingSexpr(pc, name, <ts:Field[]>fieldsSexpr, <ts:Type>rest);
}
["function", var ret, var params] => {
return fromFunctionSexpr(pc, name, <ts:Type>ret, <ts:Type[]>params);
}
["function", var ret, var params, var rest] => {
return fromFunctionSexpr(pc, name, <ts:Type>ret, <ts:Type[]>params, rest);
}
// Only to be used from *.typetest files
["cell", var ty, "none"] => {
return cellContaining(pc.env, fromSexprInternal(pc, <ts:Type>ty), CELL_MUT_NONE);
}
["cell", var ty, "limited"] => {
return cellContaining(pc.env, fromSexprInternal(pc, <ts:Type>ty), CELL_MUT_LIMITED);
}
["cell", var ty, "unlimited"] => {
return cellContaining(pc.env, fromSexprInternal(pc, <ts:Type>ty), CELL_MUT_UNLIMITED);
}
_ => {
panic err:impossible("impossible sexpr atom:" + atomSexpr.toString());
}
}
}
function fromListSexpr(SexprAtomParseContext pc, string name, ts:Type[] initialSexpr = [], int fixedLength = initialSexpr.length(), ts:Type restSexpr = "never") returns SemType {
ListDefinition d = new;
pc.started[name] = d;
SemType[] initial = from var member in initialSexpr select fromSexprInternal(pc, member);
SemType rest = fromSexprInternal(pc, restSexpr);
return defineListTypeWrapped(d, pc.env, initial, fixedLength, rest);
}
function fromMappingSexpr(SexprAtomParseContext pc, string name, ts:Field[] fieldsSexpr = [], ts:Type restSexpr = "never") returns SemType {
MappingDefinition d = new;
pc.started[name] = d;
Field[] fields = from var [fieldName, fieldTy] in fieldsSexpr select { name: fieldName.s, ty: fromSexprInternal(pc, fieldTy) };
return defineMappingTypeWrapped(d, pc.env, fields, fromSexprInternal(pc, restSexpr));
}
function fromFunctionSexpr(SexprAtomParseContext pc, string name, ts:Type ret, ts:Type[] params, ts:Type? rest=()) returns SemType {
FunctionDefinition d = new;
pc.started[name] = d;
SemType[] paramTypes = from var param in params select fromSexprInternal(pc, param);
SemType returnType = fromSexprInternal(pc, ret);
SemType restParamType = rest != () ? fromSexprInternal(pc, rest) : NEVER;
Env env = pc.env;
return d.define(env, defineListTypeWrapped(new(), env, paramTypes, rest=restParamType, mut=CELL_MUT_NONE), returnType);
}