forked from microsoft/pxt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathast.ts
More file actions
462 lines (436 loc) · 11.7 KB
/
ast.ts
File metadata and controls
462 lines (436 loc) · 11.7 KB
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
/// <reference path='../built/pxtlib.d.ts' />
/// <reference path='../built/pxtcompiler.d.ts' />
namespace pxt.py {
export enum VarModifier {
NonLocal,
Global
}
export interface ParameterDesc extends pxtc.ParameterDesc {
pyType?: Type;
}
export interface SymbolInfo extends pxtc.SymbolInfo, VarDescOptions {
pyRetType?: Type;
pySymbolType?: Type;
pyInstanceType?: Type;
parameters: ParameterDesc[];
pyAST?: AST;
isProtected?: boolean;
moduleTypeMarker?: {};
declared?: number; // A reference to the current iteration; used for detecting duplicate functions
}
export interface TypeOptions {
union?: Type;
classType?: SymbolInfo; // instance type
moduleType?: SymbolInfo; // class/static member type
primType?: string;
typeArgs?: Type[];
anyType?: boolean;
}
export interface Type extends TypeOptions {
tid: number;
}
export interface VarDescOptions {
expandsTo?: string;
isImportStar?: boolean;
isPlainImport?: boolean;
isLocal?: boolean;
isParam?: boolean;
isImport?: SymbolInfo;
modifier?: VarModifier;
forVariableEndPos?: number;
/* usage information */
firstRefPos?: number;
lastRefPos?: number;
firstAssignPos?: number;
firstAssignDepth?: number;
}
// based on grammar at https://docs.python.org/3/library/ast.html
export interface AST {
startPos: number;
endPos: number;
kind: string;
}
export interface Stmt extends AST {
_stmtBrand: void;
_comments?: Token[];
}
export interface Symbol extends Stmt {
_symbolBrand: void;
symInfo: SymbolInfo;
}
export interface Expr extends AST {
tsType?: Type;
symbolInfo?: SymbolInfo;
inCalledPosition?: boolean; // it's an f in f(...)
forTargetEndPos?: number; // it's X in "for X in ..." and this is the position of the end of that for
_exprBrand: void;
}
export type expr_context = "Load" | "Store" | "Del" | "AugLoad" | "AugStore" | "Param"
export type boolop = "And" | "Or"
export type operator = "Add" | "Sub" | "Mult" | "MatMult" | "Div" | "Mod" | "Pow"
| "LShift" | "RShift" | "BitOr" | "BitXor" | "BitAnd" | "FloorDiv"
export type unaryop = "Invert" | "Not" | "UAdd" | "USub"
export type cmpop = "Eq" | "NotEq" | "Lt" | "LtE" | "Gt" | "GtE" | "Is" | "IsNot" | "In" | "NotIn"
export type identifier = string
export type int = number
export interface Arg extends AST {
kind: "Arg";
arg: identifier;
annotation?: Expr;
}
export interface Arguments extends AST {
kind: "Arguments";
args: Arg[];
vararg?: Arg;
kwonlyargs: Arg[];
kw_defaults: Expr[];
kwarg?: Arg;
defaults: Expr[];
}
// keyword arguments supplied to call (NULL identifier for **kwargs)
export interface Keyword extends AST {
kind: "Keyword";
arg?: identifier;
value: Expr;
}
export interface Comprehension extends AST {
kind: "Comprehension";
target: Expr;
iter: Expr;
ifs: Expr[];
is_async: int;
}
export interface Module extends Symbol, ScopeDef {
kind: "Module";
body: Stmt[];
name?: string;
source: string;
tsFilename: string;
tsBody?: pxtc.SymbolInfo[];
}
export interface ExceptHandler extends AST {
kind: "ExceptHandler";
type?: Expr;
name?: identifier;
body: Stmt[];
}
// import name with optional 'as' alias.
export interface Alias extends AST {
kind: "Alias";
name: identifier;
asname?: identifier;
}
export interface WithItem extends AST {
kind: "WithItem";
context_expr: Expr;
optional_vars?: Expr;
}
export interface AnySlice extends AST {
_anySliceBrand: void;
}
export interface Slice extends AnySlice {
kind: "Slice";
lower?: Expr;
upper?: Expr;
step?: Expr;
}
export interface ExtSlice extends AnySlice {
kind: "ExtSlice";
dims: AnySlice[];
}
export interface Index extends AnySlice {
kind: "Index";
value: Expr;
}
export interface ScopeDef extends Stmt {
vars: Map<SymbolInfo>;
parent?: ScopeDef;
blockDepth?: number;
}
export interface FunctionDef extends Symbol, ScopeDef {
kind: "FunctionDef";
name: identifier;
args: Arguments;
body: Stmt[];
decorator_list: Expr[];
returns?: Expr;
alwaysThrows?: boolean;
callers?: Expr[];
}
export interface AsyncFunctionDef extends Stmt {
kind: "AsyncFunctionDef";
name: identifier;
args: Arguments;
body: Stmt[];
decorator_list: Expr[];
returns?: Expr;
}
export interface ClassDef extends Symbol, ScopeDef {
kind: "ClassDef";
name: identifier;
bases: Expr[];
keywords: Keyword[];
body: Stmt[];
decorator_list: Expr[];
baseClass?: ClassDef;
isEnum?: boolean;
isNamespace?: boolean;
}
export interface Return extends Stmt {
kind: "Return";
value?: Expr;
}
export interface Delete extends Stmt {
kind: "Delete";
targets: Expr[];
}
export interface Assign extends Symbol {
kind: "Assign";
targets: Expr[];
value: Expr;
}
export interface AugAssign extends Stmt {
kind: "AugAssign";
target: Expr;
op: operator;
value: Expr;
}
export interface AnnAssign extends Stmt {
kind: "AnnAssign";
target: Expr;
annotation: Expr;
value?: Expr;
simple: int; // 'simple' indicates that we annotate simple name without parens
}
export interface For extends Stmt {
kind: "For";
target: Expr;
iter: Expr;
body: Stmt[];
orelse: Stmt[]; // use 'orelse' because else is a keyword in target languages
}
export interface AsyncFor extends Stmt {
kind: "AsyncFor";
target: Expr;
iter: Expr;
body: Stmt[];
orelse: Stmt[];
}
export interface While extends Stmt {
kind: "While";
test: Expr;
body: Stmt[];
orelse: Stmt[];
}
export interface If extends Stmt {
kind: "If";
test: Expr;
body: Stmt[];
orelse: Stmt[];
}
export interface With extends Stmt {
kind: "With";
items: WithItem[];
body: Stmt[];
}
export interface AsyncWith extends Stmt {
kind: "AsyncWith";
items: WithItem[];
body: Stmt[];
}
export interface Raise extends Stmt {
kind: "Raise";
exc?: Expr;
cause?: Expr;
}
export interface Try extends Stmt {
kind: "Try";
body: Stmt[];
handlers: ExceptHandler[];
orelse: Stmt[];
finalbody: Stmt[];
}
export interface Assert extends Stmt {
kind: "Assert";
test: Expr;
msg?: Expr;
}
export interface Import extends Stmt {
kind: "Import";
names: Alias[];
}
export interface ImportFrom extends Stmt {
kind: "ImportFrom";
module?: identifier;
names: Alias[];
level?: int;
}
export interface Global extends Stmt {
kind: "Global";
names: identifier[];
}
export interface Nonlocal extends Stmt {
kind: "Nonlocal";
names: identifier[];
}
export interface ExprStmt extends Stmt {
kind: "ExprStmt";
value: Expr;
}
export interface Pass extends Stmt {
kind: "Pass";
}
export interface Break extends Stmt {
kind: "Break";
}
export interface Continue extends Stmt {
kind: "Continue";
}
export interface BoolOp extends Expr {
kind: "BoolOp";
op: boolop;
values: Expr[];
}
export interface BinOp extends Expr {
kind: "BinOp";
left: Expr;
op: operator;
right: Expr;
}
export interface UnaryOp extends Expr {
kind: "UnaryOp";
op: unaryop;
operand: Expr;
}
export interface Lambda extends Expr {
kind: "Lambda";
args: Arguments;
body: Expr;
}
export interface IfExp extends Expr {
kind: "IfExp";
test: Expr;
body: Expr;
orelse: Expr;
}
export interface Dict extends Expr {
kind: "Dict";
keys: (Expr | undefined)[];
values: Expr[];
}
export interface Set extends Expr {
kind: "Set";
elts: Expr[];
}
export interface ListComp extends Expr {
kind: "ListComp";
elt: Expr;
generators: Comprehension[];
}
export interface SetComp extends Expr {
kind: "SetComp";
elt: Expr;
generators: Comprehension[];
}
export interface DictComp extends Expr {
kind: "DictComp";
key: Expr;
value: Expr;
generators: Comprehension[];
}
export interface GeneratorExp extends Expr {
kind: "GeneratorExp";
elt: Expr;
generators: Comprehension[];
}
export interface Await extends Expr {
kind: "Await";
value: Expr;
}
export interface Yield extends Expr {
kind: "Yield";
value?: Expr;
}
export interface YieldFrom extends Expr {
kind: "YieldFrom";
value: Expr;
}
// need sequences for compare to distinguish between x < 4 < 3 and (x < 4) < 3
export interface Compare extends Expr {
kind: "Compare";
left: Expr;
ops: cmpop[];
comparators: Expr[];
}
export interface Call extends Expr {
kind: "Call";
func: Expr;
args: Expr[];
keywords: Keyword[];
}
export interface Num extends Expr {
kind: "Num";
n: number;
ns: string;
}
export interface Str extends Expr {
kind: "Str";
s: string;
}
export interface FormattedValue extends Expr {
kind: "FormattedValue";
value: Expr;
conversion?: int;
format_spec?: Expr;
}
export interface JoinedStr extends Expr {
kind: "JoinedStr";
values: Expr[];
}
export interface Bytes extends Expr {
kind: "Bytes";
s: number[];
}
export interface NameConstant extends Expr {
kind: "NameConstant";
value: boolean | undefined; // undefined=None, True, False
}
export interface Ellipsis extends Expr {
kind: "Ellipsis";
}
export interface Constant extends Expr {
kind: "Constant";
value: any; // ???
}
// the following expression can appear in assignment context
export interface AssignmentExpr extends Expr {
ctx: expr_context;
}
export interface Attribute extends AssignmentExpr {
kind: "Attribute";
value: Expr;
attr: identifier;
}
export interface Subscript extends AssignmentExpr {
kind: "Subscript";
value: Expr;
slice: AnySlice;
}
export interface Starred extends AssignmentExpr {
kind: "Starred";
value: Expr;
}
export interface Name extends AssignmentExpr {
kind: "Name";
id: identifier;
isdef?: boolean;
}
export interface List extends AssignmentExpr {
kind: "List";
elts: Expr[];
}
export interface Tuple extends AssignmentExpr {
kind: "Tuple";
elts: Expr[];
}
}