1
2
3
4
5 package syntax
6
7 import "fmt"
8
9
10
11
12 type Node interface {
13
14
15
16
17
18
19
20
21 Pos() Pos
22 SetPos(Pos)
23 aNode()
24 }
25
26 type node struct {
27
28
29 pos Pos
30 }
31
32 func (n *node) Pos() Pos { return n.pos }
33 func (n *node) SetPos(pos Pos) { n.pos = pos }
34 func (*node) aNode() {}
35
36
37
38
39
40 type File struct {
41 Pragma Pragma
42 PkgName *Name
43 DeclList []Decl
44 EOF Pos
45 GoVersion string
46 node
47 }
48
49 func (f *File) String() string {
50 return fmt.Sprintf("File{PkgName:%v, DeclList:%v}", f.PkgName, f.DeclList)
51 }
52
53
54
55
56 type (
57 Decl interface {
58 Node
59 aDecl()
60 }
61
62
63
64 ImportDecl struct {
65 Group *Group
66 Pragma Pragma
67 LocalPkgName *Name
68 Path *BasicLit
69 decl
70 }
71
72
73
74
75 ConstDecl struct {
76 Group *Group
77 Pragma Pragma
78 NameList []*Name
79 Type Expr
80 Values Expr
81 decl
82 }
83
84
85 TypeDecl struct {
86 Group *Group
87 Pragma Pragma
88 Name *Name
89 TParamList []*Field
90 Alias bool
91 Type Expr
92 decl
93 }
94
95
96
97
98 VarDecl struct {
99 Group *Group
100 Pragma Pragma
101 NameList []*Name
102 Type Expr
103 Values Expr
104 decl
105 }
106
107
108
109
110
111 FuncDecl struct {
112 Pragma Pragma
113 Recv *Field
114 Name *Name
115 TParamList []*Field
116 Type *FuncType
117 Body *BlockStmt
118 decl
119 }
120 )
121
122 func (d *FuncDecl) String() string {
123 return fmt.Sprintf("FuncDecl{Name:%v}", d.Name)
124 }
125
126 func (d *TypeDecl) String() string {
127 return fmt.Sprintf("TypeDecl{Name:%v}", d.Name)
128 }
129
130 func (d *VarDecl) String() string {
131 return fmt.Sprintf("FuncDecl{NameList:%v}", d.NameList)
132 }
133
134 type decl struct{ node }
135
136 func (*decl) aDecl() {}
137
138
139 type Group struct {
140 _ int
141 }
142
143
144
145
146 func NewName(pos Pos, value string) *Name {
147 n := new(Name)
148 n.pos = pos
149 n.Value = value
150 return n
151 }
152
153 type (
154 Expr interface {
155 Node
156 typeInfo
157 aExpr()
158 }
159
160
161
162 BadExpr struct {
163 expr
164 }
165
166
167 Name struct {
168 Value string
169 expr
170 }
171
172
173 BasicLit struct {
174 Value string
175 Kind LitKind
176 Bad bool
177 expr
178 }
179
180
181 CompositeLit struct {
182 Type Expr
183 ElemList []Expr
184 NKeys int
185 Rbrace Pos
186 expr
187 }
188
189
190 KeyValueExpr struct {
191 Key, Value Expr
192 expr
193 }
194
195
196 FuncLit struct {
197 Type *FuncType
198 Body *BlockStmt
199 expr
200 }
201
202
203 ParenExpr struct {
204 X Expr
205 expr
206 }
207
208
209 SelectorExpr struct {
210 X Expr
211 Sel *Name
212 expr
213 }
214
215
216
217 IndexExpr struct {
218 X Expr
219 Index Expr
220 expr
221 }
222
223
224 SliceExpr struct {
225 X Expr
226 Index [3]Expr
227
228
229
230
231 Full bool
232 expr
233 }
234
235
236 AssertExpr struct {
237 X Expr
238 Type Expr
239 expr
240 }
241
242
243
244 TypeSwitchGuard struct {
245 Lhs *Name
246 X Expr
247 expr
248 }
249
250 Operation struct {
251 Op Operator
252 X, Y Expr
253 expr
254 }
255
256
257 CallExpr struct {
258 Fun Expr
259 ArgList []Expr
260 HasDots bool
261 expr
262 }
263
264
265 ListExpr struct {
266 ElemList []Expr
267 expr
268 }
269
270
271 ArrayType struct {
272
273 Len Expr
274 Elem Expr
275 expr
276 }
277
278
279 SliceType struct {
280 Elem Expr
281 expr
282 }
283
284
285 DotsType struct {
286 Elem Expr
287 expr
288 }
289
290
291 StructType struct {
292 FieldList []*Field
293 TagList []*BasicLit
294 expr
295 }
296
297
298
299 Field struct {
300 Name *Name
301 Type Expr
302 node
303 }
304
305
306 InterfaceType struct {
307 MethodList []*Field
308 expr
309 }
310
311 FuncType struct {
312 ParamList []*Field
313 ResultList []*Field
314 expr
315 }
316
317
318 MapType struct {
319 Key, Value Expr
320 expr
321 }
322
323
324
325
326 ChanType struct {
327 Dir ChanDir
328 Elem Expr
329 expr
330 }
331 )
332
333 type expr struct {
334 node
335 typeAndValue
336 }
337
338 func (*expr) aExpr() {}
339
340 type ChanDir uint
341
342 const (
343 _ ChanDir = iota
344 SendOnly
345 RecvOnly
346 )
347
348
349
350
351 type (
352 Stmt interface {
353 Node
354 aStmt()
355 }
356
357 SimpleStmt interface {
358 Stmt
359 aSimpleStmt()
360 }
361
362 EmptyStmt struct {
363 simpleStmt
364 }
365
366 LabeledStmt struct {
367 Label *Name
368 Stmt Stmt
369 stmt
370 }
371
372 BlockStmt struct {
373 List []Stmt
374 Rbrace Pos
375 stmt
376 }
377
378 ExprStmt struct {
379 X Expr
380 simpleStmt
381 }
382
383 SendStmt struct {
384 Chan, Value Expr
385 simpleStmt
386 }
387
388 DeclStmt struct {
389 DeclList []Decl
390 stmt
391 }
392
393 AssignStmt struct {
394 Op Operator
395 Lhs, Rhs Expr
396 simpleStmt
397 }
398
399 BranchStmt struct {
400 Tok token
401 Label *Name
402
403
404
405
406
407 Target Stmt
408 stmt
409 }
410
411 CallStmt struct {
412 Tok token
413 Call Expr
414 DeferAt Expr
415 stmt
416 }
417
418 ReturnStmt struct {
419 Results Expr
420 stmt
421 }
422
423 IfStmt struct {
424 Init SimpleStmt
425 Cond Expr
426 Then *BlockStmt
427 Else Stmt
428 stmt
429 }
430
431 ForStmt struct {
432 Init SimpleStmt
433 Cond Expr
434 Post SimpleStmt
435 Body *BlockStmt
436 stmt
437 }
438
439 SwitchStmt struct {
440 Init SimpleStmt
441 Tag Expr
442 Body []*CaseClause
443 Rbrace Pos
444 stmt
445 }
446
447 SelectStmt struct {
448 Body []*CommClause
449 Rbrace Pos
450 stmt
451 }
452 )
453
454 type (
455 RangeClause struct {
456 Lhs Expr
457 Def bool
458 X Expr
459 simpleStmt
460 }
461
462 CaseClause struct {
463 Cases Expr
464 Body []Stmt
465 Colon Pos
466 node
467 }
468
469 CommClause struct {
470 Comm SimpleStmt
471 Body []Stmt
472 Colon Pos
473 node
474 }
475 )
476
477 type stmt struct{ node }
478
479 func (stmt) aStmt() {}
480
481 type simpleStmt struct {
482 stmt
483 }
484
485 func (simpleStmt) aSimpleStmt() {}
486
487
488
489
490
491
492 type CommentKind uint
493
494 const (
495 Above CommentKind = iota
496 Below
497 Left
498 Right
499 )
500
501 type Comment struct {
502 Kind CommentKind
503 Text string
504 Next *Comment
505 }
506
View as plain text