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