1
2
3
4
5
6
7
8 package ast
9
10 import (
11 "go/token"
12 "unicode"
13 "utf8"
14 )
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 type Node interface {
35 Pos() token.Pos
36 End() token.Pos
37 }
38
39
40 type Expr interface {
41 Node
42 exprNode()
43 }
44
45
46 type Stmt interface {
47 Node
48 stmtNode()
49 }
50
51
52 type Decl interface {
53 Node
54 declNode()
55 }
56
57
58
59
60
61 type Comment struct {
62 Slash token.Pos
63 Text string
64 }
65
66 func (c *Comment) Pos() token.Pos { return c.Slash }
67 func (c *Comment) End() token.Pos { return token.Pos(int(c.Slash) + len(c.Text)) }
68
69
70
71
72 type CommentGroup struct {
73 List []*Comment
74 }
75
76 func (g *CommentGroup) Pos() token.Pos { return g.List[0].Pos() }
77 func (g *CommentGroup) End() token.Pos { return g.List[len(g.List)-1].End() }
78
79
80
81
82
83
84
85
86 type Field struct {
87 Doc *CommentGroup
88 Names []*Ident
89 Type Expr
90 Tag *BasicLit
91 Comment *CommentGroup
92 }
93
94 func (f *Field) Pos() token.Pos {
95 if len(f.Names) > 0 {
96 return f.Names[0].Pos()
97 }
98 return f.Type.Pos()
99 }
100
101 func (f *Field) End() token.Pos {
102 if f.Tag != nil {
103 return f.Tag.End()
104 }
105 return f.Type.End()
106 }
107
108
109 type FieldList struct {
110 Opening token.Pos
111 List []*Field
112 Closing token.Pos
113 }
114
115 func (f *FieldList) Pos() token.Pos {
116 if f.Opening.IsValid() {
117 return f.Opening
118 }
119
120
121 if len(f.List) > 0 {
122 return f.List[0].Pos()
123 }
124 return token.NoPos
125 }
126
127 func (f *FieldList) End() token.Pos {
128 if f.Closing.IsValid() {
129 return f.Closing + 1
130 }
131
132
133 if n := len(f.List); n > 0 {
134 return f.List[n-1].End()
135 }
136 return token.NoPos
137 }
138
139
140 func (f *FieldList) NumFields() int {
141 n := 0
142 if f != nil {
143 for _, g := range f.List {
144 m := len(g.Names)
145 if m == 0 {
146 m = 1
147 }
148 n += m
149 }
150 }
151 return n
152 }
153
154
155
156
157 type (
158
159
160
161
162 BadExpr struct {
163 From, To token.Pos
164 }
165
166
167 Ident struct {
168 NamePos token.Pos
169 Name string
170 Obj *Object
171 }
172
173
174
175
176 Ellipsis struct {
177 Ellipsis token.Pos
178 Elt Expr
179 }
180
181
182 BasicLit struct {
183 ValuePos token.Pos
184 Kind token.Token
185 Value string
186 }
187
188
189 FuncLit struct {
190 Type *FuncType
191 Body *BlockStmt
192 }
193
194
195 CompositeLit struct {
196 Type Expr
197 Lbrace token.Pos
198 Elts []Expr
199 Rbrace token.Pos
200 }
201
202
203 ParenExpr struct {
204 Lparen token.Pos
205 X Expr
206 Rparen token.Pos
207 }
208
209
210 SelectorExpr struct {
211 X Expr
212 Sel *Ident
213 }
214
215
216 IndexExpr struct {
217 X Expr
218 Lbrack token.Pos
219 Index Expr
220 Rbrack token.Pos
221 }
222
223
224 SliceExpr struct {
225 X Expr
226 Lbrack token.Pos
227 Low Expr
228 High Expr
229 Rbrack token.Pos
230 }
231
232
233
234
235 TypeAssertExpr struct {
236 X Expr
237 Type Expr
238 }
239
240
241 CallExpr struct {
242 Fun Expr
243 Lparen token.Pos
244 Args []Expr
245 Ellipsis token.Pos
246 Rparen token.Pos
247 }
248
249
250
251
252 StarExpr struct {
253 Star token.Pos
254 X Expr
255 }
256
257
258
259
260 UnaryExpr struct {
261 OpPos token.Pos
262 Op token.Token
263 X Expr
264 }
265
266
267 BinaryExpr struct {
268 X Expr
269 OpPos token.Pos
270 Op token.Token
271 Y Expr
272 }
273
274
275
276
277 KeyValueExpr struct {
278 Key Expr
279 Colon token.Pos
280 Value Expr
281 }
282 )
283
284
285
286
287 type ChanDir int
288
289 const (
290 SEND ChanDir = 1 << iota
291 RECV
292 )
293
294
295
296
297
298 type (
299
300 ArrayType struct {
301 Lbrack token.Pos
302 Len Expr
303 Elt Expr
304 }
305
306
307 StructType struct {
308 Struct token.Pos
309 Fields *FieldList
310 Incomplete bool
311 }
312
313
314
315
316 FuncType struct {
317 Func token.Pos
318 Params *FieldList
319 Results *FieldList
320 }
321
322
323 InterfaceType struct {
324 Interface token.Pos
325 Methods *FieldList
326 Incomplete bool
327 }
328
329
330 MapType struct {
331 Map token.Pos
332 Key Expr
333 Value Expr
334 }
335
336
337 ChanType struct {
338 Begin token.Pos
339 Dir ChanDir
340 Value Expr
341 }
342 )
343
344
345
346 func (x *BadExpr) Pos() token.Pos { return x.From }
347 func (x *Ident) Pos() token.Pos { return x.NamePos }
348 func (x *Ellipsis) Pos() token.Pos { return x.Ellipsis }
349 func (x *BasicLit) Pos() token.Pos { return x.ValuePos }
350 func (x *FuncLit) Pos() token.Pos { return x.Type.Pos() }
351 func (x *CompositeLit) Pos() token.Pos {
352 if x.Type != nil {
353 return x.Type.Pos()
354 }
355 return x.Lbrace
356 }
357 func (x *ParenExpr) Pos() token.Pos { return x.Lparen }
358 func (x *SelectorExpr) Pos() token.Pos { return x.X.Pos() }
359 func (x *IndexExpr) Pos() token.Pos { return x.X.Pos() }
360 func (x *SliceExpr) Pos() token.Pos { return x.X.Pos() }
361 func (x *TypeAssertExpr) Pos() token.Pos { return x.X.Pos() }
362 func (x *CallExpr) Pos() token.Pos { return x.Fun.Pos() }
363 func (x *StarExpr) Pos() token.Pos { return x.Star }
364 func (x *UnaryExpr) Pos() token.Pos { return x.OpPos }
365 func (x *BinaryExpr) Pos() token.Pos { return x.X.Pos() }
366 func (x *KeyValueExpr) Pos() token.Pos { return x.Key.Pos() }
367 func (x *ArrayType) Pos() token.Pos { return x.Lbrack }
368 func (x *StructType) Pos() token.Pos { return x.Struct }
369 func (x *FuncType) Pos() token.Pos { return x.Func }
370 func (x *InterfaceType) Pos() token.Pos { return x.Interface }
371 func (x *MapType) Pos() token.Pos { return x.Map }
372 func (x *ChanType) Pos() token.Pos { return x.Begin }
373
374 func (x *BadExpr) End() token.Pos { return x.To }
375 func (x *Ident) End() token.Pos { return token.Pos(int(x.NamePos) + len(x.Name)) }
376 func (x *Ellipsis) End() token.Pos {
377 if x.Elt != nil {
378 return x.Elt.End()
379 }
380 return x.Ellipsis + 3
381 }
382 func (x *BasicLit) End() token.Pos { return token.Pos(int(x.ValuePos) + len(x.Value)) }
383 func (x *FuncLit) End() token.Pos { return x.Body.End() }
384 func (x *CompositeLit) End() token.Pos { return x.Rbrace + 1 }
385 func (x *ParenExpr) End() token.Pos { return x.Rparen + 1 }
386 func (x *SelectorExpr) End() token.Pos { return x.Sel.End() }
387 func (x *IndexExpr) End() token.Pos { return x.Rbrack + 1 }
388 func (x *SliceExpr) End() token.Pos { return x.Rbrack + 1 }
389 func (x *TypeAssertExpr) End() token.Pos {
390 if x.Type != nil {
391 return x.Type.End()
392 }
393 return x.X.End()
394 }
395 func (x *CallExpr) End() token.Pos { return x.Rparen + 1 }
396 func (x *StarExpr) End() token.Pos { return x.X.End() }
397 func (x *UnaryExpr) End() token.Pos { return x.X.End() }
398 func (x *BinaryExpr) End() token.Pos { return x.Y.End() }
399 func (x *KeyValueExpr) End() token.Pos { return x.Value.End() }
400 func (x *ArrayType) End() token.Pos { return x.Elt.End() }
401 func (x *StructType) End() token.Pos { return x.Fields.End() }
402 func (x *FuncType) End() token.Pos {
403 if x.Results != nil {
404 return x.Results.End()
405 }
406 return x.Params.End()
407 }
408 func (x *InterfaceType) End() token.Pos { return x.Methods.End() }
409 func (x *MapType) End() token.Pos { return x.Value.End() }
410 func (x *ChanType) End() token.Pos { return x.Value.End() }
411
412
413
414
415 func (x *BadExpr) exprNode() {}
416 func (x *Ident) exprNode() {}
417 func (x *Ellipsis) exprNode() {}
418 func (x *BasicLit) exprNode() {}
419 func (x *FuncLit) exprNode() {}
420 func (x *CompositeLit) exprNode() {}
421 func (x *ParenExpr) exprNode() {}
422 func (x *SelectorExpr) exprNode() {}
423 func (x *IndexExpr) exprNode() {}
424 func (x *SliceExpr) exprNode() {}
425 func (x *TypeAssertExpr) exprNode() {}
426 func (x *CallExpr) exprNode() {}
427 func (x *StarExpr) exprNode() {}
428 func (x *UnaryExpr) exprNode() {}
429 func (x *BinaryExpr) exprNode() {}
430 func (x *KeyValueExpr) exprNode() {}
431
432 func (x *ArrayType) exprNode() {}
433 func (x *StructType) exprNode() {}
434 func (x *FuncType) exprNode() {}
435 func (x *InterfaceType) exprNode() {}
436 func (x *MapType) exprNode() {}
437 func (x *ChanType) exprNode() {}
438
439
440
441
442 var noPos token.Pos
443
444
445
446
447 func NewIdent(name string) *Ident { return &Ident{noPos, name, nil} }
448
449
450
451
452 func IsExported(name string) bool {
453 ch, _ := utf8.DecodeRuneInString(name)
454 return unicode.IsUpper(ch)
455 }
456
457
458
459
460 func (id *Ident) IsExported() bool { return IsExported(id.Name) }
461
462 func (id *Ident) String() string {
463 if id != nil {
464 return id.Name
465 }
466 return "<nil>"
467 }
468
469
470
471
472
473
474
475 type (
476
477
478
479
480 BadStmt struct {
481 From, To token.Pos
482 }
483
484
485 DeclStmt struct {
486 Decl Decl
487 }
488
489
490
491
492
493 EmptyStmt struct {
494 Semicolon token.Pos
495 }
496
497
498 LabeledStmt struct {
499 Label *Ident
500 Colon token.Pos
501 Stmt Stmt
502 }
503
504
505
506
507 ExprStmt struct {
508 X Expr
509 }
510
511
512 SendStmt struct {
513 Chan Expr
514 Arrow token.Pos
515 Value Expr
516 }
517
518
519 IncDecStmt struct {
520 X Expr
521 TokPos token.Pos
522 Tok token.Token
523 }
524
525
526
527
528 AssignStmt struct {
529 Lhs []Expr
530 TokPos token.Pos
531 Tok token.Token
532 Rhs []Expr
533 }
534
535
536 GoStmt struct {
537 Go token.Pos
538 Call *CallExpr
539 }
540
541
542 DeferStmt struct {
543 Defer token.Pos
544 Call *CallExpr
545 }
546
547
548 ReturnStmt struct {
549 Return token.Pos
550 Results []Expr
551 }
552
553
554
555
556 BranchStmt struct {
557 TokPos token.Pos
558 Tok token.Token
559 Label *Ident
560 }
561
562
563 BlockStmt struct {
564 Lbrace token.Pos
565 List []Stmt
566 Rbrace token.Pos
567 }
568
569
570 IfStmt struct {
571 If token.Pos
572 Init Stmt
573 Cond Expr
574 Body *BlockStmt
575 Else Stmt
576 }
577
578
579 CaseClause struct {
580 Case token.Pos
581 List []Expr
582 Colon token.Pos
583 Body []Stmt
584 }
585
586
587 SwitchStmt struct {
588 Switch token.Pos
589 Init Stmt
590 Tag Expr
591 Body *BlockStmt
592 }
593
594
595 TypeSwitchStmt struct {
596 Switch token.Pos
597 Init Stmt
598 Assign Stmt
599 Body *BlockStmt
600 }
601
602
603 CommClause struct {
604 Case token.Pos
605 Comm Stmt
606 Colon token.Pos
607 Body []Stmt
608 }
609
610
611 SelectStmt struct {
612 Select token.Pos
613 Body *BlockStmt
614 }
615
616
617 ForStmt struct {
618 For token.Pos
619 Init Stmt
620 Cond Expr
621 Post Stmt
622 Body *BlockStmt
623 }
624
625
626 RangeStmt struct {
627 For token.Pos
628 Key, Value Expr
629 TokPos token.Pos
630 Tok token.Token
631 X Expr
632 Body *BlockStmt
633 }
634 )
635
636
637
638 func (s *BadStmt) Pos() token.Pos { return s.From }
639 func (s *DeclStmt) Pos() token.Pos { return s.Decl.Pos() }
640 func (s *EmptyStmt) Pos() token.Pos { return s.Semicolon }
641 func (s *LabeledStmt) Pos() token.Pos { return s.Label.Pos() }
642 func (s *ExprStmt) Pos() token.Pos { return s.X.Pos() }
643 func (s *SendStmt) Pos() token.Pos { return s.Chan.Pos() }
644 func (s *IncDecStmt) Pos() token.Pos { return s.X.Pos() }
645 func (s *AssignStmt) Pos() token.Pos { return s.Lhs[0].Pos() }
646 func (s *GoStmt) Pos() token.Pos { return s.Go }
647 func (s *DeferStmt) Pos() token.Pos { return s.Defer }
648 func (s *ReturnStmt) Pos() token.Pos { return s.Return }
649 func (s *BranchStmt) Pos() token.Pos { return s.TokPos }
650 func (s *BlockStmt) Pos() token.Pos { return s.Lbrace }
651 func (s *IfStmt) Pos() token.Pos { return s.If }
652 func (s *CaseClause) Pos() token.Pos { return s.Case }
653 func (s *SwitchStmt) Pos() token.Pos { return s.Switch }
654 func (s *TypeSwitchStmt) Pos() token.Pos { return s.Switch }
655 func (s *CommClause) Pos() token.Pos { return s.Case }
656 func (s *SelectStmt) Pos() token.Pos { return s.Select }
657 func (s *ForStmt) Pos() token.Pos { return s.For }
658 func (s *RangeStmt) Pos() token.Pos { return s.For }
659
660 func (s *BadStmt) End() token.Pos { return s.To }
661 func (s *DeclStmt) End() token.Pos { return s.Decl.End() }
662 func (s *EmptyStmt) End() token.Pos {
663 return s.Semicolon + 1
664 }
665 func (s *LabeledStmt) End() token.Pos { return s.Stmt.End() }
666 func (s *ExprStmt) End() token.Pos { return s.X.End() }
667 func (s *SendStmt) End() token.Pos { return s.Value.End() }
668 func (s *IncDecStmt) End() token.Pos {
669 return s.TokPos + 2
670 }
671 func (s *AssignStmt) End() token.Pos { return s.Rhs[len(s.Rhs)-1].End() }
672 func (s *GoStmt) End() token.Pos { return s.Call.End() }
673 func (s *DeferStmt) End() token.Pos { return s.Call.End() }
674 func (s *ReturnStmt) End() token.Pos {
675 if n := len(s.Results); n > 0 {
676 return s.Results[n-1].End()
677 }
678 return s.Return + 6
679 }
680 func (s *BranchStmt) End() token.Pos {
681 if s.Label != nil {
682 return s.Label.End()
683 }
684 return token.Pos(int(s.TokPos) + len(s.Tok.String()))
685 }
686 func (s *BlockStmt) End() token.Pos { return s.Rbrace + 1 }
687 func (s *IfStmt) End() token.Pos {
688 if s.Else != nil {
689 return s.Else.End()
690 }
691 return s.Body.End()
692 }
693 func (s *CaseClause) End() token.Pos {
694 if n := len(s.Body); n > 0 {
695 return s.Body[n-1].End()
696 }
697 return s.Colon + 1
698 }
699 func (s *SwitchStmt) End() token.Pos { return s.Body.End() }
700 func (s *TypeSwitchStmt) End() token.Pos { return s.Body.End() }
701 func (s *CommClause) End() token.Pos {
702 if n := len(s.Body); n > 0 {
703 return s.Body[n-1].End()
704 }
705 return s.Colon + 1
706 }
707 func (s *SelectStmt) End() token.Pos { return s.Body.End() }
708 func (s *ForStmt) End() token.Pos { return s.Body.End() }
709 func (s *RangeStmt) End() token.Pos { return s.Body.End() }
710
711
712
713
714 func (s *BadStmt) stmtNode() {}
715 func (s *DeclStmt) stmtNode() {}
716 func (s *EmptyStmt) stmtNode() {}
717 func (s *LabeledStmt) stmtNode() {}
718 func (s *ExprStmt) stmtNode() {}
719 func (s *SendStmt) stmtNode() {}
720 func (s *IncDecStmt) stmtNode() {}
721 func (s *AssignStmt) stmtNode() {}
722 func (s *GoStmt) stmtNode() {}
723 func (s *DeferStmt) stmtNode() {}
724 func (s *ReturnStmt) stmtNode() {}
725 func (s *BranchStmt) stmtNode() {}
726 func (s *BlockStmt) stmtNode() {}
727 func (s *IfStmt) stmtNode() {}
728 func (s *CaseClause) stmtNode() {}
729 func (s *SwitchStmt) stmtNode() {}
730 func (s *TypeSwitchStmt) stmtNode() {}
731 func (s *CommClause) stmtNode() {}
732 func (s *SelectStmt) stmtNode() {}
733 func (s *ForStmt) stmtNode() {}
734 func (s *RangeStmt) stmtNode() {}
735
736
737
738
739
740
741
742 type (
743
744 Spec interface {
745 Node
746 specNode()
747 }
748
749
750 ImportSpec struct {
751 Doc *CommentGroup
752 Name *Ident
753 Path *BasicLit
754 Comment *CommentGroup
755 }
756
757
758
759
760 ValueSpec struct {
761 Doc *CommentGroup
762 Names []*Ident
763 Type Expr
764 Values []Expr
765 Comment *CommentGroup
766 }
767
768
769 TypeSpec struct {
770 Doc *CommentGroup
771 Name *Ident
772 Type Expr
773 Comment *CommentGroup
774 }
775 )
776
777
778
779 func (s *ImportSpec) Pos() token.Pos {
780 if s.Name != nil {
781 return s.Name.Pos()
782 }
783 return s.Path.Pos()
784 }
785 func (s *ValueSpec) Pos() token.Pos { return s.Names[0].Pos() }
786 func (s *TypeSpec) Pos() token.Pos { return s.Name.Pos() }
787
788 func (s *ImportSpec) End() token.Pos { return s.Path.End() }
789 func (s *ValueSpec) End() token.Pos {
790 if n := len(s.Values); n > 0 {
791 return s.Values[n-1].End()
792 }
793 if s.Type != nil {
794 return s.Type.End()
795 }
796 return s.Names[len(s.Names)-1].End()
797 }
798 func (s *TypeSpec) End() token.Pos { return s.Type.End() }
799
800
801
802
803 func (s *ImportSpec) specNode() {}
804 func (s *ValueSpec) specNode() {}
805 func (s *TypeSpec) specNode() {}
806
807
808
809 type (
810
811
812
813
814 BadDecl struct {
815 From, To token.Pos
816 }
817
818
819
820
821
822
823
824
825
826
827
828
829 GenDecl struct {
830 Doc *CommentGroup
831 TokPos token.Pos
832 Tok token.Token
833 Lparen token.Pos
834 Specs []Spec
835 Rparen token.Pos
836 }
837
838
839 FuncDecl struct {
840 Doc *CommentGroup
841 Recv *FieldList
842 Name *Ident
843 Type *FuncType
844 Body *BlockStmt
845 }
846 )
847
848
849
850 func (d *BadDecl) Pos() token.Pos { return d.From }
851 func (d *GenDecl) Pos() token.Pos { return d.TokPos }
852 func (d *FuncDecl) Pos() token.Pos { return d.Type.Pos() }
853
854 func (d *BadDecl) End() token.Pos { return d.To }
855 func (d *GenDecl) End() token.Pos {
856 if d.Rparen.IsValid() {
857 return d.Rparen + 1
858 }
859 return d.Specs[0].End()
860 }
861 func (d *FuncDecl) End() token.Pos {
862 if d.Body != nil {
863 return d.Body.End()
864 }
865 return d.Type.End()
866 }
867
868
869
870
871 func (d *BadDecl) declNode() {}
872 func (d *GenDecl) declNode() {}
873 func (d *FuncDecl) declNode() {}
874
875
876
877
878
879
880
881
882
883
884 type File struct {
885 Doc *CommentGroup
886 Package token.Pos
887 Name *Ident
888 Decls []Decl
889 Scope *Scope
890 Imports []*ImportSpec
891 Unresolved []*Ident
892 Comments []*CommentGroup
893 }
894
895 func (f *File) Pos() token.Pos { return f.Package }
896 func (f *File) End() token.Pos {
897 if n := len(f.Decls); n > 0 {
898 return f.Decls[n-1].End()
899 }
900 return f.Name.End()
901 }
902
903
904
905
906 type Package struct {
907 Name string
908 Scope *Scope
909 Imports map[string]*Object
910 Files map[string]*File
911 }
912
913 func (p *Package) Pos() token.Pos { return token.NoPos }
914 func (p *Package) End() token.Pos { return token.NoPos }