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