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