-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathast.go
More file actions
672 lines (582 loc) · 13.3 KB
/
Copy pathast.go
File metadata and controls
672 lines (582 loc) · 13.3 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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
// Package ast defines the minipy abstract syntax tree: a module of statements
// over scalar expressions, control flow, and functions
// (docs/spec/03-grammar.md). Every node carries the source position of its
// first token.
package ast
import "github.com/siyul-park/minipy/token"
// Node is any AST node; it reports the position of its first token.
type Node interface {
Pos() token.Pos
}
// Stmt is a statement node.
type Stmt interface {
Node
stmtNode()
}
// Expr is an expression node.
type Expr interface {
Node
exprNode()
}
// Base carries a node's source position; embed it in every node.
type Base struct {
Position token.Pos
}
// Module is a whole compilation unit: an ordered list of top-level statements.
type Module struct {
Base
Body []Stmt
}
// AnnAssign is an annotated declaration `target: ann [= value]`. Value is nil
// for a bare declaration.
type AnnAssign struct {
Base
Target *Name
Ann Expr
Value Expr
}
// Assign is a plain assignment `target = value`.
// For chained assignments like `a = b = expr`, Targets holds the additional
// targets beyond the first (which is stored in Target).
type Assign struct {
Base
Target Expr
Targets []Expr
Value Expr
}
// AugAssign is an augmented assignment `target <op>= value`; Op is the base
// binary operator (e.g. token.PLUS for `+=`).
type AugAssign struct {
Base
Target Expr
Op token.Type
Value Expr
}
// ExprStmt is an expression evaluated for effect; its value is discarded.
type ExprStmt struct {
Base
X Expr
}
// If is `if Cond: Body [else: Orelse]`. An `elif` chain is represented as a
// nested If in Orelse, matching the CPython AST shape.
type If struct {
Base
Cond Expr
Body []Stmt
Orelse []Stmt
}
// While is `while Cond: Body [else: Orelse]`. Orelse runs only when the loop
// exits without a break.
type While struct {
Base
Cond Expr
Body []Stmt
Orelse []Stmt
}
// For is `for Target in Iter: Body [else: Orelse]`. Orelse runs only when the
// loop exits without a break. Flat tuple targets are allowed.
type For struct {
Base
Target Expr
Iter Expr
Body []Stmt
Orelse []Stmt
Async bool
}
// ParamKind distinguishes positional-only, normal, and keyword-only parameters.
type ParamKind int
const (
ParamNormal ParamKind = iota
ParamPosOnly
ParamKwOnly
)
// Param is a function parameter with an optional type annotation and default.
type Param struct {
Base
Name *Name
Ann Expr
Default Expr
Kind ParamKind
Vararg bool
Kwarg bool
}
// Function is `def Name(Params) -> Returns: Body`.
type Function struct {
Base
Name *Name
Params []*Param
Returns Expr
Decorators []Expr
Body []Stmt
Async bool
}
// Class is `class Name[(Base)]: Body`.
type Class struct {
Base
Name *Name
Bases []Expr
Keywords []*Keyword
Decorators []Expr
Body []Stmt
}
// Keyword is a keyword argument in a call or class header. Name is empty for
// `**expr`.
type Keyword struct {
Base
Name string
Value Expr
}
// ImportAlias is one imported name, optionally renamed with `as`.
type ImportAlias struct {
Base
Name string
As string
}
// Import is `import a [as b], ...`.
type Import struct {
Base
Names []*ImportAlias
}
// ImportFrom is `from module import name [as alias], ...`.
type ImportFrom struct {
Base
Module string
Names []*ImportAlias
Level int
}
// TypeAlias is Python 3.13's soft-keyword `type Name = expr`.
type TypeAlias struct {
Base
Name *Name
Value Expr
}
// Global is a `global x, y` declaration inside a function.
type Global struct {
Base
Names []string
}
// Nonlocal is a `nonlocal x, y` declaration inside a nested function.
type Nonlocal struct {
Base
Names []string
}
// Return is a `return` statement. Value is nil for bare `return`.
type Return struct {
Base
Value Expr
}
// Yield is a generator suspension statement. Value is nil for bare `yield`.
// From is true for `yield from iter`, which delegates to a child iterator.
type Yield struct {
Base
Value Expr
From bool
}
// Delete is `del target1, target2, ...`. Each target is a Name, Subscript, or
// Attribute lvalue.
type Delete struct {
Base
Targets []Expr
}
// Assert is `assert Test[, Msg]`. Msg is nil when absent.
type Assert struct {
Base
Test Expr
Msg Expr
}
// Match is `match Subject: case ...` structural pattern matching.
type Match struct {
Base
Subject Expr
Cases []*Case
}
// Try is `try: Body [except ...] [else: Orelse] [finally: Finalbody]`.
type Try struct {
Base
Body []Stmt
Handlers []*ExceptHandler
Orelse []Stmt
Finalbody []Stmt
}
// ExceptHandler is one `except [Type] [as Name]: Body` clause.
type ExceptHandler struct {
Base
Type Expr
Name string
Body []Stmt
Star bool
}
// Raise is `raise [Exc]`. Exc is nil for a bare re-raise.
type Raise struct {
Base
Exc Expr
Cause Expr
}
// With is `with Items: Body`.
type With struct {
Base
Items []*WithItem
Body []Stmt
Async bool
}
// WithItem is one context manager item, optionally bound by `as`.
type WithItem struct {
Base
Context Expr
OptionalVars Expr
}
// Case is one `case Pattern [if Guard]: Body` arm of a Match. Guard is nil when
// absent.
type Case struct {
Base
Pattern Pattern
Guard Expr
Body []Stmt
}
// Break is the `break` statement.
type Break struct{ Base }
// Continue is the `continue` statement.
type Continue struct{ Base }
// Pass is the `pass` no-op statement.
type Pass struct{ Base }
// Pattern is a `match` case pattern node.
type Pattern interface {
Node
patternNode()
}
// WildcardPattern is `_`; it matches anything and binds nothing.
type WildcardPattern struct{ Base }
// CapturePattern is a bare name `x`; it matches anything and binds the subject.
type CapturePattern struct {
Base
Name string
}
// ValuePattern matches by equality against a literal or dotted value expression
// (e.g. `3`, `"s"`, `None`, `Color.RED`).
type ValuePattern struct {
Base
Value Expr
}
// SequencePattern is `[p, ...]` or `(p, ...)`. Star is the index of the starred
// element capturing the rest, or -1 when there is none.
type SequencePattern struct {
Base
Elems []Pattern
Star int
}
// StarPattern is `*rest` inside a SequencePattern; Name is "" for `*_`.
type StarPattern struct {
Base
Name string
}
// MappingPattern is `{key: p, ..., **rest}`. Rest is "" when no `**` capture is
// present.
type MappingPattern struct {
Base
Keys []Expr
Values []Pattern
Rest string
}
// ClassPattern is `Class(pos..., name=kw...)`. Class is a Name or dotted
// Attribute; KwNames pairs with Kw by index.
type ClassPattern struct {
Base
Class Expr
Args []Pattern
KwNames []string
Kw []Pattern
}
// OrPattern is `p1 | p2 | ...`; it matches when any alternative matches.
type OrPattern struct {
Base
Alts []Pattern
}
// AsPattern is `Pattern as Name`; it matches Pattern then binds the subject to
// Name.
type AsPattern struct {
Base
Pattern Pattern
Name string
}
// IfExp is the conditional expression `Body if Cond else Orelse`.
type IfExp struct {
Base
Body Expr
Cond Expr
Orelse Expr
}
// LambdaExpr is `lambda params: body`; Params carry inferred annotations when
// a Callable context is available.
type LambdaExpr struct {
Base
Params []*Param
Body Expr
}
// Name is an identifier reference.
type Name struct {
Base
Name string
}
// IntLit is an integer literal (int64).
type IntLit struct {
Base
Value int64
}
// FloatLit is a floating-point literal (float64).
type FloatLit struct {
Base
Value float64
}
// StrLit is a decoded string literal.
type StrLit struct {
Base
Value string
}
// BytesLit is a decoded bytes literal. Value holds the decoded byte payload
// in a Go string, which can carry arbitrary byte values (not necessarily
// valid UTF-8).
type BytesLit struct {
Base
Value string
}
// BoolLit is `True` or `False`.
type BoolLit struct {
Base
Value bool
}
// NoneLit is `None`.
type NoneLit struct {
Base
}
// EllipsisLit is `...`.
type EllipsisLit struct {
Base
}
// UnaryExpr is a prefix operation: `+x`, `-x`, `~x`, or `not x`.
type UnaryExpr struct {
Base
Op token.Type
X Expr
}
// BinaryExpr is an arithmetic, bitwise, or shift operation.
type BinaryExpr struct {
Base
Op token.Type
X, Y Expr
}
// BoolOp is a short-circuiting `and`/`or`.
type BoolOp struct {
Base
Op token.Type
X, Y Expr
}
// Compare is a (possibly chained) comparison `x op y op z ...`.
type Compare struct {
Base
X Expr
Ops []token.Type
Comparators []Expr
}
// CallExpr is a function call `function(args...)`.
type CallExpr struct {
Base
Fn Expr
Args []Expr
Keywords []*Keyword
StarArgs []Expr
Kwargs Expr
}
// Attribute is `x.name`.
type Attribute struct {
Base
X Expr
Name string
}
// Subscript is `x[index]`.
type Subscript struct {
Base
X Expr
Index Expr
}
// Slice is `lower:upper[:step]` inside a subscript.
type Slice struct {
Base
Lower Expr
Upper Expr
Step Expr
}
// Starred is `*expr` in displays, calls, or assignment targets.
type Starred struct {
Base
X Expr
}
// NamedExpr is `(target := value)`.
type NamedExpr struct {
Base
Target *Name
Value Expr
}
// AwaitExpr is `await expr` (parse-only until scheduler/runtime support lands).
type AwaitExpr struct {
Base
X Expr
}
// YieldExpr is `yield expr` in expression position.
type YieldExpr struct {
Base
Value Expr
From bool
}
// GeneratorExp is `(elem for ...)`.
type GeneratorExp struct {
Base
Elem Expr
Clauses []*Comprehension
}
// UnionType is a union annotation `A | B | C`. It appears only in annotation
// position; the checker resolves its members to a types.Union.
type UnionType struct {
Base
Members []Expr
}
// ListLit is `[a, b, c]`.
type ListLit struct {
Base
Elems []Expr
}
// DictLit is `{k: v}`.
type DictLit struct {
Base
Keys []Expr
Values []Expr
}
// SetLit is `{a, b, c}`.
type SetLit struct {
Base
Elems []Expr
}
// Comprehension is one `for target in iter if ...` clause.
type Comprehension struct {
Base
Target *Name
Iter Expr
Ifs []Expr
Async bool
}
// ListComp is `[elem for ...]`.
type ListComp struct {
Base
Elem Expr
Clauses []*Comprehension
}
// DictComp is `{key: value for ...}`.
type DictComp struct {
Base
Key Expr
Value Expr
Clauses []*Comprehension
}
// SetComp is `{elem for ...}`.
type SetComp struct {
Base
Elem Expr
Clauses []*Comprehension
}
// TupleLit is `(a, b)` or a flat tuple target `a, b`.
type TupleLit struct {
Base
Elems []Expr
}
// FString is an f-string split into literal and formatted expression parts.
type FString struct {
Base
Parts []FStringPart
}
// FStringPart is either raw text or a formatted expression.
type FStringPart interface {
Node
fstringPartNode()
}
// FStringText is literal text inside an f-string.
type FStringText struct {
Base
Value string
}
// FStringExpr is a replacement field inside an f-string.
type FStringExpr struct {
Base
Expr Expr
Debug string
Conversion rune
Format []FStringPart
}
// Pos returns the position of the node's first token.
func (b Base) Pos() token.Pos { return b.Position }
func (*AnnAssign) stmtNode() {}
func (*Assign) stmtNode() {}
func (*AugAssign) stmtNode() {}
func (*ExprStmt) stmtNode() {}
func (*If) stmtNode() {}
func (*While) stmtNode() {}
func (*For) stmtNode() {}
func (*Function) stmtNode() {}
func (*Class) stmtNode() {}
func (*Import) stmtNode() {}
func (*ImportFrom) stmtNode() {}
func (*TypeAlias) stmtNode() {}
func (*Global) stmtNode() {}
func (*Nonlocal) stmtNode() {}
func (*Return) stmtNode() {}
func (*Yield) stmtNode() {}
func (*Break) stmtNode() {}
func (*Continue) stmtNode() {}
func (*Pass) stmtNode() {}
func (*Delete) stmtNode() {}
func (*Assert) stmtNode() {}
func (*Match) stmtNode() {}
func (*Try) stmtNode() {}
func (*Raise) stmtNode() {}
func (*With) stmtNode() {}
func (*WildcardPattern) patternNode() {}
func (*CapturePattern) patternNode() {}
func (*ValuePattern) patternNode() {}
func (*SequencePattern) patternNode() {}
func (*StarPattern) patternNode() {}
func (*MappingPattern) patternNode() {}
func (*ClassPattern) patternNode() {}
func (*OrPattern) patternNode() {}
func (*AsPattern) patternNode() {}
func (*Name) exprNode() {}
func (*LambdaExpr) exprNode() {}
func (*IntLit) exprNode() {}
func (*FloatLit) exprNode() {}
func (*StrLit) exprNode() {}
func (*BytesLit) exprNode() {}
func (*BoolLit) exprNode() {}
func (*NoneLit) exprNode() {}
func (*EllipsisLit) exprNode() {}
func (*UnaryExpr) exprNode() {}
func (*BinaryExpr) exprNode() {}
func (*BoolOp) exprNode() {}
func (*Compare) exprNode() {}
func (*CallExpr) exprNode() {}
func (*IfExp) exprNode() {}
func (*Attribute) exprNode() {}
func (*Subscript) exprNode() {}
func (*Slice) exprNode() {}
func (*Starred) exprNode() {}
func (*NamedExpr) exprNode() {}
func (*AwaitExpr) exprNode() {}
func (*YieldExpr) exprNode() {}
func (*GeneratorExp) exprNode() {}
func (*UnionType) exprNode() {}
func (*ListLit) exprNode() {}
func (*DictLit) exprNode() {}
func (*SetLit) exprNode() {}
func (*ListComp) exprNode() {}
func (*DictComp) exprNode() {}
func (*SetComp) exprNode() {}
func (*TupleLit) exprNode() {}
func (*FString) exprNode() {}
func (*FStringText) fstringPartNode() {}
func (*FStringExpr) fstringPartNode() {}