-
Notifications
You must be signed in to change notification settings - Fork 1
/
ast.go
467 lines (410 loc) · 8.15 KB
/
ast.go
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
463
464
465
466
467
package pythonast
// See https://hg.python.org/cpython/file/tip/Parser/Python.asdl
type Module struct {
Body []Stmt
}
type Stmt interface {
stmt()
}
func (FunctionDef) stmt() {}
func (AsyncFunctionDef) stmt() {}
func (ClassDef) stmt() {}
func (Return) stmt() {}
func (Delete) stmt() {}
func (Assign) stmt() {}
func (AugAssign) stmt() {}
func (AnnAssign) stmt() {}
func (For) stmt() {}
func (AsyncFor) stmt() {}
func (While) stmt() {}
func (If) stmt() {}
func (With) stmt() {}
func (AsyncWith) stmt() {}
func (Raise) stmt() {}
func (Try) stmt() {}
func (Assert) stmt() {}
func (Import) stmt() {}
func (ImportFrom) stmt() {}
func (Global) stmt() {}
func (Nonlocal) stmt() {}
func (ExprStmt) stmt() {}
func (Pass) stmt() {}
func (Break) stmt() {}
func (Continue) stmt() {}
func (Comment) stmt() {}
func (DocString) stmt() {}
type Comment struct {
Text string
}
type DocString struct{ Lines []string }
type Expr interface {
Precedence() int
}
type FunctionDef struct {
Name Identifier
Args Arguments
Body []Stmt
DecoratorList []Expr
Returns Expr
}
type AsyncFunctionDef struct {
Name Identifier
Args Arguments
Body []Stmt
DecoratorList []Expr
Returns Expr
}
type ClassDef struct {
Name Identifier
Bases []Expr
Keywords []Keyword
Body []Stmt
DecoratorList []Expr
}
type Return struct{ Value Expr }
type Delete struct{ Targets []Expr }
type Assign struct {
Targets []Expr
Value Expr
}
type AugAssign struct {
Target Expr
Op Operator
Value Expr
}
// 'simple' indicates that we annotate simple name without parens
type AnnAssign struct {
Target Expr
Annotation Expr
Value Expr
Simple bool
}
// use 'orelse' because else is a keyword in target languages
type For struct {
Target Expr
Iter Expr
Body []Stmt
Orelse []Stmt
}
type AsyncFor struct {
Target Expr
Iter Expr
Body []Stmt
Orelse []Stmt
}
type While struct {
Test Expr
Body []Stmt
Orelse []Stmt
}
type If struct {
Test Expr
Body []Stmt
Orelse []Stmt
}
type With struct {
Items []WithItem
Body []Stmt
}
type AsyncWith struct {
Items []WithItem
Body []Stmt
}
type Raise struct {
Exc Expr
Cause Expr
}
type Try struct {
Body []Stmt
Handlers []ExceptHandler
Orelse []Stmt
Finalbody []Stmt
}
type Assert struct {
Test Expr
Msg Expr
}
type Import struct{ Names []Alias }
type ImportFrom struct {
Module *Identifier
Names []Alias
Level *int
}
type Global struct{ Names []Identifier }
type Nonlocal struct{ Names []Identifier }
type ExprStmt struct{ Value Expr }
type Pass struct{}
type Break struct{}
type Continue struct{}
func (Lambda) Precedence() int { return 1 }
func (IfExp) Precedence() int { return 2 }
func (e BoolOpExpr) Precedence() int { return e.Op.Precedence() }
func (o BoolOp) Precedence() int {
switch o {
case Or:
return 3
case And:
return 4
}
panic("unknown BoolOp")
}
func (Compare) Precedence() int { return 6 }
func (e BinOp) Precedence() int { return e.Op.Precedence() }
func (o Operator) Precedence() int {
switch o {
case BitOr:
return 7
case BitXor:
return 8
case BitAnd:
return 9
case LShift, RShift:
return 10
case Add, Sub:
return 11
case Mult, MatMult, FloorDiv, Div, Mod:
return 12
case Pow:
return 14
}
panic("unknown Operator")
}
func (e UnaryOpExpr) Precedence() int { return e.Op.Precedence() }
func (o UnaryOp) Precedence() int {
switch o {
case Not:
return 5
case Invert, UAdd, USub:
return 13
}
panic("unknown UnaryOp")
}
func (Await) Precedence() int { return 15 }
func (Subscript) Precedence() int { return 16 }
func (Call) Precedence() int { return 16 }
func (Attribute) Precedence() int { return 16 }
func (Dict) Precedence() int { return 17 }
func (Set) Precedence() int { return 17 }
func (List) Precedence() int { return 17 }
func (Tuple) Precedence() int { return 0 }
func (ListComp) Precedence() int { return 17 }
func (SetComp) Precedence() int { return 17 }
func (DictComp) Precedence() int { return 17 }
func (GeneratorExp) Precedence() int { return 17 }
func (Yield) Precedence() int { return 0 } // dubious
func (YieldFrom) Precedence() int { return 0 }
func (Num) Precedence() int { return 100 }
func (Str) Precedence() int { return 100 }
func (FormattedValue) Precedence() int { return 0 }
func (JoinedStr) Precedence() int { return 100 }
func (Bytes) Precedence() int { return 100 }
func (NameConstant) Precedence() int { return 100 }
func (Ellipsis) Precedence() int { return 0 }
func (ConstantExpr) Precedence() int { return 100 }
func (Starred) Precedence() int { return 100 }
func (Name) Precedence() int { return 100 }
type BoolOpExpr struct {
Op BoolOp
Values []Expr
}
type BinOp struct {
Left Expr
Op Operator
Right Expr
}
type UnaryOpExpr struct {
Op UnaryOp
Operand Expr
}
type Lambda struct {
Args Arguments
Body Expr
}
type IfExp struct {
Test Expr
Body Expr
Orelse Expr
}
type Dict struct {
Keys []Expr
Values []Expr
}
type Set struct{ Elts []Expr }
type ListComp struct {
Elt Expr
Generators []Comprehension
}
type SetComp struct {
Elt Expr
Generators []Comprehension
}
type DictComp struct {
Key Expr
Value Expr
Generators []Comprehension
}
type GeneratorExp struct {
Elt Expr
Generators []Comprehension
}
// the grammar constrains where yield expressions can occur
type Await struct{ Value Expr }
type Yield struct{ Value Expr }
type YieldFrom struct{ Value Expr }
// need sequences for compare to distinguish between
// x < 4 < 3 and struct {< x 4} < 3
type Compare struct {
Left Expr
Ops []CmpOp
Comparators []Expr
}
type Call struct {
Func Expr
Args []Expr
Keywords []Keyword
}
type Num struct{ N string } // a number as a PyObject.
type Str struct{ S string } // need to specify raw; unicode; *etc
type FormattedValue struct {
Value Expr
Conversion *int
FormatSpec Expr
}
type JoinedStr struct{ Values []Expr }
type Bytes struct{ S []byte }
type NameConstant struct{ Value Singleton }
type Ellipsis struct{}
type ConstantExpr struct{ Value interface{} }
// the following expression can appear in assignment context
type Attribute struct {
Value Expr
Attr Identifier
Ctx ExprContext
}
type Subscript struct {
Value Expr
Slice Slice
Ctx ExprContext
}
type Starred struct {
Value Expr
Ctx ExprContext
}
type Name struct {
Id Identifier
Ctx ExprContext
}
type List struct {
Elts []Expr
Ctx ExprContext
}
type Tuple struct {
Elts []Expr
Ctx ExprContext
}
type ExprContext int
const (
LoadStore ExprContext = iota
Del
AugLoad
AugStore
Param
)
type Slice interface {
slice()
}
type RangeSlice struct {
Lower Expr
Upper Expr
Step Expr
}
type ExtSlice struct{ Dims *Slice }
type Index struct{ Value Expr }
func (RangeSlice) slice() {}
func (ExtSlice) slice() {}
func (Index) slice() {}
type BoolOp int
const (
And BoolOp = iota
Or
)
type Operator int
const (
Add Operator = iota
Sub
Mult
MatMult
Div
Mod
Pow
LShift
RShift
BitOr
BitXor
BitAnd
FloorDiv
)
type UnaryOp int
const (
Invert UnaryOp = iota
Not
UAdd
USub
)
type CmpOp int
const (
Eq CmpOp = iota
NotEq
Lt
LtE
Gt
GtE
Is
IsNot
In
NotIn
)
type Comprehension struct {
Target Expr
Iter Expr
Ifs []Expr
IsAsync int
}
type ExceptHandler struct {
Typ Expr
Name Identifier
Body []Stmt
}
type Arguments struct {
Args []Arg
Vararg *Arg
Kwonlyargs []Arg
KwDefaults []Expr
Kwarg *Arg
Defaults []Expr
}
type Arg struct {
Arg Identifier
Annotation Expr
}
// keyword arguments supplied to call (NULL identifier for **kwargs)
type Keyword struct {
Arg *Identifier
Value Expr
}
// import name with optional 'as' alias.
type Alias struct {
Name Identifier
Asname *Identifier
}
type WithItem struct {
ContextExpr Expr
OptionalVars Expr
}
type Identifier string
type Singleton int
const (
None Singleton = iota
True
False
)