Source file
src/go/parser/parser.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package parser
17
18 import (
19 "fmt"
20 "go/ast"
21 "go/internal/typeparams"
22 "go/scanner"
23 "go/token"
24 "strconv"
25 "strings"
26 "unicode"
27 )
28
29
30 type parser struct {
31 file *token.File
32 errors scanner.ErrorList
33 scanner scanner.Scanner
34
35
36 mode Mode
37 trace bool
38 indent int
39
40
41 comments []*ast.CommentGroup
42 leadComment *ast.CommentGroup
43 lineComment *ast.CommentGroup
44
45
46 pos token.Pos
47 tok token.Token
48 lit string
49
50
51
52
53
54 syncPos token.Pos
55 syncCnt int
56
57
58 exprLev int
59 inRhs bool
60
61 imports []*ast.ImportSpec
62
63
64
65 nestLev int
66 }
67
68 func (p *parser) init(fset *token.FileSet, filename string, src []byte, mode Mode) {
69 p.file = fset.AddFile(filename, -1, len(src))
70 var m scanner.Mode
71 if mode&ParseComments != 0 {
72 m = scanner.ScanComments
73 }
74 eh := func(pos token.Position, msg string) { p.errors.Add(pos, msg) }
75 p.scanner.Init(p.file, src, eh, m)
76
77 p.mode = mode
78 p.trace = mode&Trace != 0
79 p.next()
80 }
81
82 func (p *parser) allowGenerics() bool { return p.mode&typeparams.DisallowParsing == 0 }
83 func (p *parser) allowTypeSets() bool { return p.mode&typeparams.DisallowTypeSets == 0 }
84
85
86
87
88 func (p *parser) printTrace(a ...any) {
89 const dots = ". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . "
90 const n = len(dots)
91 pos := p.file.Position(p.pos)
92 fmt.Printf("%5d:%3d: ", pos.Line, pos.Column)
93 i := 2 * p.indent
94 for i > n {
95 fmt.Print(dots)
96 i -= n
97 }
98
99 fmt.Print(dots[0:i])
100 fmt.Println(a...)
101 }
102
103 func trace(p *parser, msg string) *parser {
104 p.printTrace(msg, "(")
105 p.indent++
106 return p
107 }
108
109
110 func un(p *parser) {
111 p.indent--
112 p.printTrace(")")
113 }
114
115
116 const maxNestLev int = 1e5
117
118 func incNestLev(p *parser) *parser {
119 p.nestLev++
120 if p.nestLev > maxNestLev {
121 p.error(p.pos, "exceeded max nesting depth")
122 panic(bailout{})
123 }
124 return p
125 }
126
127
128
129 func decNestLev(p *parser) {
130 p.nestLev--
131 }
132
133
134 func (p *parser) next0() {
135
136
137
138
139 if p.trace && p.pos.IsValid() {
140 s := p.tok.String()
141 switch {
142 case p.tok.IsLiteral():
143 p.printTrace(s, p.lit)
144 case p.tok.IsOperator(), p.tok.IsKeyword():
145 p.printTrace("\"" + s + "\"")
146 default:
147 p.printTrace(s)
148 }
149 }
150
151 p.pos, p.tok, p.lit = p.scanner.Scan()
152 }
153
154
155 func (p *parser) consumeComment() (comment *ast.Comment, endline int) {
156
157
158 endline = p.file.Line(p.pos)
159 if p.lit[1] == '*' {
160
161 for i := 0; i < len(p.lit); i++ {
162 if p.lit[i] == '\n' {
163 endline++
164 }
165 }
166 }
167
168 comment = &ast.Comment{Slash: p.pos, Text: p.lit}
169 p.next0()
170
171 return
172 }
173
174
175
176
177
178 func (p *parser) consumeCommentGroup(n int) (comments *ast.CommentGroup, endline int) {
179 var list []*ast.Comment
180 endline = p.file.Line(p.pos)
181 for p.tok == token.COMMENT && p.file.Line(p.pos) <= endline+n {
182 var comment *ast.Comment
183 comment, endline = p.consumeComment()
184 list = append(list, comment)
185 }
186
187
188 comments = &ast.CommentGroup{List: list}
189 p.comments = append(p.comments, comments)
190
191 return
192 }
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208 func (p *parser) next() {
209 p.leadComment = nil
210 p.lineComment = nil
211 prev := p.pos
212 p.next0()
213
214 if p.tok == token.COMMENT {
215 var comment *ast.CommentGroup
216 var endline int
217
218 if p.file.Line(p.pos) == p.file.Line(prev) {
219
220
221 comment, endline = p.consumeCommentGroup(0)
222 if p.file.Line(p.pos) != endline || p.tok == token.EOF {
223
224
225 p.lineComment = comment
226 }
227 }
228
229
230 endline = -1
231 for p.tok == token.COMMENT {
232 comment, endline = p.consumeCommentGroup(1)
233 }
234
235 if endline+1 == p.file.Line(p.pos) {
236
237
238 p.leadComment = comment
239 }
240 }
241 }
242
243
244
245 type bailout struct {
246 pos token.Pos
247 msg string
248 }
249
250 func (p *parser) error(pos token.Pos, msg string) {
251 if p.trace {
252 defer un(trace(p, "error: "+msg))
253 }
254
255 epos := p.file.Position(pos)
256
257
258
259
260 if p.mode&AllErrors == 0 {
261 n := len(p.errors)
262 if n > 0 && p.errors[n-1].Pos.Line == epos.Line {
263 return
264 }
265 if n > 10 {
266 panic(bailout{})
267 }
268 }
269
270 p.errors.Add(epos, msg)
271 }
272
273 func (p *parser) errorExpected(pos token.Pos, msg string) {
274 msg = "expected " + msg
275 if pos == p.pos {
276
277
278 switch {
279 case p.tok == token.SEMICOLON && p.lit == "\n":
280 msg += ", found newline"
281 case p.tok.IsLiteral():
282
283 msg += ", found " + p.lit
284 default:
285 msg += ", found '" + p.tok.String() + "'"
286 }
287 }
288 p.error(pos, msg)
289 }
290
291 func (p *parser) expect(tok token.Token) token.Pos {
292 pos := p.pos
293 if p.tok != tok {
294 p.errorExpected(pos, "'"+tok.String()+"'")
295 }
296 p.next()
297 return pos
298 }
299
300
301
302 func (p *parser) expect2(tok token.Token) (pos token.Pos) {
303 if p.tok == tok {
304 pos = p.pos
305 } else {
306 p.errorExpected(p.pos, "'"+tok.String()+"'")
307 }
308 p.next()
309 return
310 }
311
312
313
314 func (p *parser) expectClosing(tok token.Token, context string) token.Pos {
315 if p.tok != tok && p.tok == token.SEMICOLON && p.lit == "\n" {
316 p.error(p.pos, "missing ',' before newline in "+context)
317 p.next()
318 }
319 return p.expect(tok)
320 }
321
322 func (p *parser) expectSemi() {
323
324 if p.tok != token.RPAREN && p.tok != token.RBRACE {
325 switch p.tok {
326 case token.COMMA:
327
328 p.errorExpected(p.pos, "';'")
329 fallthrough
330 case token.SEMICOLON:
331 p.next()
332 default:
333 p.errorExpected(p.pos, "';'")
334 p.advance(stmtStart)
335 }
336 }
337 }
338
339 func (p *parser) atComma(context string, follow token.Token) bool {
340 if p.tok == token.COMMA {
341 return true
342 }
343 if p.tok != follow {
344 msg := "missing ','"
345 if p.tok == token.SEMICOLON && p.lit == "\n" {
346 msg += " before newline"
347 }
348 p.error(p.pos, msg+" in "+context)
349 return true
350 }
351 return false
352 }
353
354 func assert(cond bool, msg string) {
355 if !cond {
356 panic("go/parser internal error: " + msg)
357 }
358 }
359
360
361
362 func (p *parser) advance(to map[token.Token]bool) {
363 for ; p.tok != token.EOF; p.next() {
364 if to[p.tok] {
365
366
367
368
369
370
371
372 if p.pos == p.syncPos && p.syncCnt < 10 {
373 p.syncCnt++
374 return
375 }
376 if p.pos > p.syncPos {
377 p.syncPos = p.pos
378 p.syncCnt = 0
379 return
380 }
381
382
383
384
385
386 }
387 }
388 }
389
390 var stmtStart = map[token.Token]bool{
391 token.BREAK: true,
392 token.CONST: true,
393 token.CONTINUE: true,
394 token.DEFER: true,
395 token.FALLTHROUGH: true,
396 token.FOR: true,
397 token.GO: true,
398 token.GOTO: true,
399 token.IF: true,
400 token.RETURN: true,
401 token.SELECT: true,
402 token.SWITCH: true,
403 token.TYPE: true,
404 token.VAR: true,
405 }
406
407 var declStart = map[token.Token]bool{
408 token.CONST: true,
409 token.TYPE: true,
410 token.VAR: true,
411 }
412
413 var exprEnd = map[token.Token]bool{
414 token.COMMA: true,
415 token.COLON: true,
416 token.SEMICOLON: true,
417 token.RPAREN: true,
418 token.RBRACK: true,
419 token.RBRACE: true,
420 }
421
422
423
424
425
426
427
428
429
430
431 func (p *parser) safePos(pos token.Pos) (res token.Pos) {
432 defer func() {
433 if recover() != nil {
434 res = token.Pos(p.file.Base() + p.file.Size())
435 }
436 }()
437 _ = p.file.Offset(pos)
438 return pos
439 }
440
441
442
443
444 func (p *parser) parseIdent() *ast.Ident {
445 pos := p.pos
446 name := "_"
447 if p.tok == token.IDENT {
448 name = p.lit
449 p.next()
450 } else {
451 p.expect(token.IDENT)
452 }
453 return &ast.Ident{NamePos: pos, Name: name}
454 }
455
456 func (p *parser) parseIdentList() (list []*ast.Ident) {
457 if p.trace {
458 defer un(trace(p, "IdentList"))
459 }
460
461 list = append(list, p.parseIdent())
462 for p.tok == token.COMMA {
463 p.next()
464 list = append(list, p.parseIdent())
465 }
466
467 return
468 }
469
470
471
472
473
474 func (p *parser) parseExprList() (list []ast.Expr) {
475 if p.trace {
476 defer un(trace(p, "ExpressionList"))
477 }
478
479 list = append(list, p.checkExpr(p.parseExpr()))
480 for p.tok == token.COMMA {
481 p.next()
482 list = append(list, p.checkExpr(p.parseExpr()))
483 }
484
485 return
486 }
487
488 func (p *parser) parseList(inRhs bool) []ast.Expr {
489 old := p.inRhs
490 p.inRhs = inRhs
491 list := p.parseExprList()
492 p.inRhs = old
493 return list
494 }
495
496
497
498
499 func (p *parser) parseType() ast.Expr {
500 if p.trace {
501 defer un(trace(p, "Type"))
502 }
503
504 typ := p.tryIdentOrType()
505
506 if typ == nil {
507 pos := p.pos
508 p.errorExpected(pos, "type")
509 p.advance(exprEnd)
510 return &ast.BadExpr{From: pos, To: p.pos}
511 }
512
513 return typ
514 }
515
516 func (p *parser) parseQualifiedIdent(ident *ast.Ident) ast.Expr {
517 if p.trace {
518 defer un(trace(p, "QualifiedIdent"))
519 }
520
521 typ := p.parseTypeName(ident)
522 if p.tok == token.LBRACK && p.allowGenerics() {
523 typ = p.parseTypeInstance(typ)
524 }
525
526 return typ
527 }
528
529
530 func (p *parser) parseTypeName(ident *ast.Ident) ast.Expr {
531 if p.trace {
532 defer un(trace(p, "TypeName"))
533 }
534
535 if ident == nil {
536 ident = p.parseIdent()
537 }
538
539 if p.tok == token.PERIOD {
540
541 p.next()
542 sel := p.parseIdent()
543 return &ast.SelectorExpr{X: ident, Sel: sel}
544 }
545
546 return ident
547 }
548
549
550
551 func (p *parser) parseArrayType(lbrack token.Pos, len ast.Expr) *ast.ArrayType {
552 if p.trace {
553 defer un(trace(p, "ArrayType"))
554 }
555
556 if len == nil {
557 p.exprLev++
558
559 if p.tok == token.ELLIPSIS {
560 len = &ast.Ellipsis{Ellipsis: p.pos}
561 p.next()
562 } else if p.tok != token.RBRACK {
563 len = p.parseRhs()
564 }
565 p.exprLev--
566 }
567 if p.tok == token.COMMA {
568
569
570
571 p.error(p.pos, "unexpected comma; expecting ]")
572 p.next()
573 }
574 p.expect(token.RBRACK)
575 elt := p.parseType()
576 return &ast.ArrayType{Lbrack: lbrack, Len: len, Elt: elt}
577 }
578
579 func (p *parser) parseArrayFieldOrTypeInstance(x *ast.Ident) (*ast.Ident, ast.Expr) {
580 if p.trace {
581 defer un(trace(p, "ArrayFieldOrTypeInstance"))
582 }
583
584
585
586 lbrack := p.expect(token.LBRACK)
587 var args []ast.Expr
588 var firstComma token.Pos
589
590
591 argparser := p.parseRhsOrType
592 if !p.allowGenerics() {
593 argparser = p.parseRhs
594 }
595 if p.tok != token.RBRACK {
596 p.exprLev++
597 args = append(args, argparser())
598 for p.tok == token.COMMA {
599 if !firstComma.IsValid() {
600 firstComma = p.pos
601 }
602 p.next()
603 args = append(args, argparser())
604 }
605 p.exprLev--
606 }
607 rbrack := p.expect(token.RBRACK)
608
609 if len(args) == 0 {
610
611 elt := p.parseType()
612 return x, &ast.ArrayType{Lbrack: lbrack, Elt: elt}
613 }
614
615
616 if len(args) == 1 {
617 elt := p.tryIdentOrType()
618 if elt != nil {
619
620 return x, &ast.ArrayType{Lbrack: lbrack, Len: args[0], Elt: elt}
621 }
622 if !p.allowGenerics() {
623 p.error(rbrack, "missing element type in array type expression")
624 return nil, &ast.BadExpr{From: args[0].Pos(), To: args[0].End()}
625 }
626 }
627
628 if !p.allowGenerics() {
629 p.error(firstComma, "expected ']', found ','")
630 return x, &ast.BadExpr{From: args[0].Pos(), To: args[len(args)-1].End()}
631 }
632
633
634 return nil, typeparams.PackIndexExpr(x, lbrack, args, rbrack)
635 }
636
637 func (p *parser) parseFieldDecl() *ast.Field {
638 if p.trace {
639 defer un(trace(p, "FieldDecl"))
640 }
641
642 doc := p.leadComment
643
644 var names []*ast.Ident
645 var typ ast.Expr
646 if p.tok == token.IDENT {
647 name := p.parseIdent()
648 if p.tok == token.PERIOD || p.tok == token.STRING || p.tok == token.SEMICOLON || p.tok == token.RBRACE {
649
650 typ = name
651 if p.tok == token.PERIOD {
652 typ = p.parseQualifiedIdent(name)
653 }
654 } else {
655
656 names = []*ast.Ident{name}
657 for p.tok == token.COMMA {
658 p.next()
659 names = append(names, p.parseIdent())
660 }
661
662
663 if len(names) == 1 && p.tok == token.LBRACK {
664 name, typ = p.parseArrayFieldOrTypeInstance(name)
665 if name == nil {
666 names = nil
667 }
668 } else {
669
670 typ = p.parseType()
671 }
672 }
673 } else {
674
675
676
677 typ = p.parseType()
678 }
679
680 var tag *ast.BasicLit
681 if p.tok == token.STRING {
682 tag = &ast.BasicLit{ValuePos: p.pos, Kind: p.tok, Value: p.lit}
683 p.next()
684 }
685
686 p.expectSemi()
687
688 field := &ast.Field{Doc: doc, Names: names, Type: typ, Tag: tag, Comment: p.lineComment}
689 return field
690 }
691
692 func (p *parser) parseStructType() *ast.StructType {
693 if p.trace {
694 defer un(trace(p, "StructType"))
695 }
696
697 pos := p.expect(token.STRUCT)
698 lbrace := p.expect(token.LBRACE)
699 var list []*ast.Field
700 for p.tok == token.IDENT || p.tok == token.MUL || p.tok == token.LPAREN {
701
702
703
704 list = append(list, p.parseFieldDecl())
705 }
706 rbrace := p.expect(token.RBRACE)
707
708 return &ast.StructType{
709 Struct: pos,
710 Fields: &ast.FieldList{
711 Opening: lbrace,
712 List: list,
713 Closing: rbrace,
714 },
715 }
716 }
717
718 func (p *parser) parsePointerType() *ast.StarExpr {
719 if p.trace {
720 defer un(trace(p, "PointerType"))
721 }
722
723 star := p.expect(token.MUL)
724 base := p.parseType()
725
726 return &ast.StarExpr{Star: star, X: base}
727 }
728
729 func (p *parser) parseDotsType() *ast.Ellipsis {
730 if p.trace {
731 defer un(trace(p, "DotsType"))
732 }
733
734 pos := p.expect(token.ELLIPSIS)
735 elt := p.parseType()
736
737 return &ast.Ellipsis{Ellipsis: pos, Elt: elt}
738 }
739
740 type field struct {
741 name *ast.Ident
742 typ ast.Expr
743 }
744
745 func (p *parser) parseParamDecl(name *ast.Ident, typeSetsOK bool) (f field) {
746
747
748 if p.trace {
749 defer un(trace(p, "ParamDeclOrNil"))
750 }
751
752 ptok := p.tok
753 if name != nil {
754 p.tok = token.IDENT
755 } else if typeSetsOK && p.tok == token.TILDE {
756
757 return field{nil, p.embeddedElem(nil)}
758 }
759
760 switch p.tok {
761 case token.IDENT:
762
763 if name != nil {
764 f.name = name
765 p.tok = ptok
766 } else {
767 f.name = p.parseIdent()
768 }
769 switch p.tok {
770 case token.IDENT, token.MUL, token.ARROW, token.FUNC, token.CHAN, token.MAP, token.STRUCT, token.INTERFACE, token.LPAREN:
771
772 f.typ = p.parseType()
773
774 case token.LBRACK:
775
776 f.name, f.typ = p.parseArrayFieldOrTypeInstance(f.name)
777
778 case token.ELLIPSIS:
779
780 f.typ = p.parseDotsType()
781 return
782
783 case token.PERIOD:
784
785 f.typ = p.parseQualifiedIdent(f.name)
786 f.name = nil
787
788 case token.TILDE:
789 if typeSetsOK {
790 f.typ = p.embeddedElem(nil)
791 return
792 }
793
794 case token.OR:
795 if typeSetsOK {
796
797 f.typ = p.embeddedElem(f.name)
798 f.name = nil
799 return
800 }
801 }
802
803 case token.MUL, token.ARROW, token.FUNC, token.LBRACK, token.CHAN, token.MAP, token.STRUCT, token.INTERFACE, token.LPAREN:
804
805 f.typ = p.parseType()
806
807 case token.ELLIPSIS:
808
809
810 f.typ = p.parseDotsType()
811 return
812
813 default:
814
815
816 p.errorExpected(p.pos, "')'")
817 p.advance(exprEnd)
818 }
819
820
821 if typeSetsOK && p.tok == token.OR && f.typ != nil {
822 f.typ = p.embeddedElem(f.typ)
823 }
824
825 return
826 }
827
828 func (p *parser) parseParameterList(name0 *ast.Ident, typ0 ast.Expr, closing token.Token) (params []*ast.Field) {
829 if p.trace {
830 defer un(trace(p, "ParameterList"))
831 }
832
833
834 tparams := closing == token.RBRACK
835
836 typeSetsOK := tparams && p.allowTypeSets()
837
838 pos := p.pos
839 if name0 != nil {
840 pos = name0.Pos()
841 }
842
843 var list []field
844 var named int
845
846 for name0 != nil || p.tok != closing && p.tok != token.EOF {
847 var par field
848 if typ0 != nil {
849 if typeSetsOK {
850 typ0 = p.embeddedElem(typ0)
851 }
852 par = field{name0, typ0}
853 } else {
854 par = p.parseParamDecl(name0, typeSetsOK)
855 }
856 name0 = nil
857 typ0 = nil
858 if par.name != nil || par.typ != nil {
859 list = append(list, par)
860 if par.name != nil && par.typ != nil {
861 named++
862 }
863 }
864 if !p.atComma("parameter list", closing) {
865 break
866 }
867 p.next()
868 }
869
870 if len(list) == 0 {
871 return
872 }
873
874
875
876
877
878 if named == 0 {
879
880 for i := 0; i < len(list); i++ {
881 par := &list[i]
882 if typ := par.name; typ != nil {
883 par.typ = typ
884 par.name = nil
885 }
886 }
887 if tparams {
888 p.error(pos, "all type parameters must be named")
889 }
890 } else if named != len(list) {
891
892 ok := true
893 var typ ast.Expr
894 missingName := pos
895 for i := len(list) - 1; i >= 0; i-- {
896 if par := &list[i]; par.typ != nil {
897 typ = par.typ
898 if par.name == nil {
899 ok = false
900 missingName = par.typ.Pos()
901 n := ast.NewIdent("_")
902 n.NamePos = typ.Pos()
903 par.name = n
904 }
905 } else if typ != nil {
906 par.typ = typ
907 } else {
908
909 ok = false
910 missingName = par.name.Pos()
911 par.typ = &ast.BadExpr{From: par.name.Pos(), To: p.pos}
912 }
913 }
914 if !ok {
915 if tparams {
916 p.error(missingName, "all type parameters must be named")
917 } else {
918 p.error(pos, "mixed named and unnamed parameters")
919 }
920 }
921 }
922
923
924 if named == 0 {
925
926 for _, par := range list {
927 assert(par.typ != nil, "nil type in unnamed parameter list")
928 params = append(params, &ast.Field{Type: par.typ})
929 }
930 return
931 }
932
933
934 var names []*ast.Ident
935 var typ ast.Expr
936 addParams := func() {
937 assert(typ != nil, "nil type in named parameter list")
938 field := &ast.Field{Names: names, Type: typ}
939 params = append(params, field)
940 names = nil
941 }
942 for _, par := range list {
943 if par.typ != typ {
944 if len(names) > 0 {
945 addParams()
946 }
947 typ = par.typ
948 }
949 names = append(names, par.name)
950 }
951 if len(names) > 0 {
952 addParams()
953 }
954 return
955 }
956
957 func (p *parser) parseParameters(acceptTParams bool) (tparams, params *ast.FieldList) {
958 if p.trace {
959 defer un(trace(p, "Parameters"))
960 }
961
962 if p.allowGenerics() && acceptTParams && p.tok == token.LBRACK {
963 opening := p.pos
964 p.next()
965
966 list := p.parseParameterList(nil, nil, token.RBRACK)
967 rbrack := p.expect(token.RBRACK)
968 tparams = &ast.FieldList{Opening: opening, List: list, Closing: rbrack}
969
970 if tparams.NumFields() == 0 {
971 p.error(tparams.Closing, "empty type parameter list")
972 tparams = nil
973 }
974 }
975
976 opening := p.expect(token.LPAREN)
977
978 var fields []*ast.Field
979 if p.tok != token.RPAREN {
980 fields = p.parseParameterList(nil, nil, token.RPAREN)
981 }
982
983 rparen := p.expect(token.RPAREN)
984 params = &ast.FieldList{Opening: opening, List: fields, Closing: rparen}
985
986 return
987 }
988
989 func (p *parser) parseResult() *ast.FieldList {
990 if p.trace {
991 defer un(trace(p, "Result"))
992 }
993
994 if p.tok == token.LPAREN {
995 _, results := p.parseParameters(false)
996 return results
997 }
998
999 typ := p.tryIdentOrType()
1000 if typ != nil {
1001 list := make([]*ast.Field, 1)
1002 list[0] = &ast.Field{Type: typ}
1003 return &ast.FieldList{List: list}
1004 }
1005
1006 return nil
1007 }
1008
1009 func (p *parser) parseFuncType() *ast.FuncType {
1010 if p.trace {
1011 defer un(trace(p, "FuncType"))
1012 }
1013
1014 pos := p.expect(token.FUNC)
1015 tparams, params := p.parseParameters(true)
1016 if tparams != nil {
1017 p.error(tparams.Pos(), "function type must have no type parameters")
1018 }
1019 results := p.parseResult()
1020
1021 return &ast.FuncType{Func: pos, Params: params, Results: results}
1022 }
1023
1024 func (p *parser) parseMethodSpec() *ast.Field {
1025 if p.trace {
1026 defer un(trace(p, "MethodSpec"))
1027 }
1028
1029 doc := p.leadComment
1030 var idents []*ast.Ident
1031 var typ ast.Expr
1032 x := p.parseTypeName(nil)
1033 if ident, _ := x.(*ast.Ident); ident != nil {
1034 switch {
1035 case p.tok == token.LBRACK && p.allowGenerics():
1036
1037 lbrack := p.pos
1038 p.next()
1039 p.exprLev++
1040 x := p.parseExpr()
1041 p.exprLev--
1042 if name0, _ := x.(*ast.Ident); name0 != nil && p.tok != token.COMMA && p.tok != token.RBRACK {
1043
1044
1045
1046
1047 _ = p.parseParameterList(name0, nil, token.RBRACK)
1048 _ = p.expect(token.RBRACK)
1049 p.error(lbrack, "interface method must have no type parameters")
1050
1051
1052 _, params := p.parseParameters(false)
1053 results := p.parseResult()
1054 idents = []*ast.Ident{ident}
1055 typ = &ast.FuncType{
1056 Func: token.NoPos,
1057 Params: params,
1058 Results: results,
1059 }
1060 } else {
1061
1062
1063 list := []ast.Expr{x}
1064 if p.atComma("type argument list", token.RBRACK) {
1065 p.exprLev++
1066 p.next()
1067 for p.tok != token.RBRACK && p.tok != token.EOF {
1068 list = append(list, p.parseType())
1069 if !p.atComma("type argument list", token.RBRACK) {
1070 break
1071 }
1072 p.next()
1073 }
1074 p.exprLev--
1075 }
1076 rbrack := p.expectClosing(token.RBRACK, "type argument list")
1077 typ = typeparams.PackIndexExpr(ident, lbrack, list, rbrack)
1078 }
1079 case p.tok == token.LPAREN:
1080
1081
1082 _, params := p.parseParameters(false)
1083 results := p.parseResult()
1084 idents = []*ast.Ident{ident}
1085 typ = &ast.FuncType{Func: token.NoPos, Params: params, Results: results}
1086 default:
1087
1088 typ = x
1089 }
1090 } else {
1091
1092 typ = x
1093 if p.tok == token.LBRACK && p.allowGenerics() {
1094
1095 typ = p.parseTypeInstance(typ)
1096 }
1097 }
1098
1099
1100
1101
1102
1103 return &ast.Field{Doc: doc, Names: idents, Type: typ}
1104 }
1105
1106 func (p *parser) embeddedElem(x ast.Expr) ast.Expr {
1107 if p.trace {
1108 defer un(trace(p, "EmbeddedElem"))
1109 }
1110 if x == nil {
1111 x = p.embeddedTerm()
1112 }
1113 for p.tok == token.OR {
1114 t := new(ast.BinaryExpr)
1115 t.OpPos = p.pos
1116 t.Op = token.OR
1117 p.next()
1118 t.X = x
1119 t.Y = p.embeddedTerm()
1120 x = t
1121 }
1122 return x
1123 }
1124
1125 func (p *parser) embeddedTerm() ast.Expr {
1126 if p.trace {
1127 defer un(trace(p, "EmbeddedTerm"))
1128 }
1129 if p.tok == token.TILDE {
1130 t := new(ast.UnaryExpr)
1131 t.OpPos = p.pos
1132 t.Op = token.TILDE
1133 p.next()
1134 t.X = p.parseType()
1135 return t
1136 }
1137
1138 t := p.tryIdentOrType()
1139 if t == nil {
1140 pos := p.pos
1141 p.errorExpected(pos, "~ term or type")
1142 p.advance(exprEnd)
1143 return &ast.BadExpr{From: pos, To: p.pos}
1144 }
1145
1146 return t
1147 }
1148
1149 func (p *parser) parseInterfaceType() *ast.InterfaceType {
1150 if p.trace {
1151 defer un(trace(p, "InterfaceType"))
1152 }
1153
1154 pos := p.expect(token.INTERFACE)
1155 lbrace := p.expect(token.LBRACE)
1156
1157 var list []*ast.Field
1158
1159 parseElements:
1160 for {
1161 switch {
1162 case p.tok == token.IDENT:
1163 f := p.parseMethodSpec()
1164 if f.Names == nil && p.allowGenerics() {
1165 f.Type = p.embeddedElem(f.Type)
1166 }
1167 p.expectSemi()
1168 f.Comment = p.lineComment
1169 list = append(list, f)
1170 case p.tok == token.TILDE && p.allowGenerics():
1171 typ := p.embeddedElem(nil)
1172 p.expectSemi()
1173 comment := p.lineComment
1174 list = append(list, &ast.Field{Type: typ, Comment: comment})
1175 case p.allowGenerics():
1176 if t := p.tryIdentOrType(); t != nil {
1177 typ := p.embeddedElem(t)
1178 p.expectSemi()
1179 comment := p.lineComment
1180 list = append(list, &ast.Field{Type: typ, Comment: comment})
1181 } else {
1182 break parseElements
1183 }
1184 default:
1185 break parseElements
1186 }
1187 }
1188
1189
1190
1191 rbrace := p.expect(token.RBRACE)
1192
1193 return &ast.InterfaceType{
1194 Interface: pos,
1195 Methods: &ast.FieldList{
1196 Opening: lbrace,
1197 List: list,
1198 Closing: rbrace,
1199 },
1200 }
1201 }
1202
1203 func (p *parser) parseMapType() *ast.MapType {
1204 if p.trace {
1205 defer un(trace(p, "MapType"))
1206 }
1207
1208 pos := p.expect(token.MAP)
1209 p.expect(token.LBRACK)
1210 key := p.parseType()
1211 p.expect(token.RBRACK)
1212 value := p.parseType()
1213
1214 return &ast.MapType{Map: pos, Key: key, Value: value}
1215 }
1216
1217 func (p *parser) parseChanType() *ast.ChanType {
1218 if p.trace {
1219 defer un(trace(p, "ChanType"))
1220 }
1221
1222 pos := p.pos
1223 dir := ast.SEND | ast.RECV
1224 var arrow token.Pos
1225 if p.tok == token.CHAN {
1226 p.next()
1227 if p.tok == token.ARROW {
1228 arrow = p.pos
1229 p.next()
1230 dir = ast.SEND
1231 }
1232 } else {
1233 arrow = p.expect(token.ARROW)
1234 p.expect(token.CHAN)
1235 dir = ast.RECV
1236 }
1237 value := p.parseType()
1238
1239 return &ast.ChanType{Begin: pos, Arrow: arrow, Dir: dir, Value: value}
1240 }
1241
1242 func (p *parser) parseTypeInstance(typ ast.Expr) ast.Expr {
1243 assert(p.allowGenerics(), "parseTypeInstance while not parsing type params")
1244 if p.trace {
1245 defer un(trace(p, "TypeInstance"))
1246 }
1247
1248 opening := p.expect(token.LBRACK)
1249 p.exprLev++
1250 var list []ast.Expr
1251 for p.tok != token.RBRACK && p.tok != token.EOF {
1252 list = append(list, p.parseType())
1253 if !p.atComma("type argument list", token.RBRACK) {
1254 break
1255 }
1256 p.next()
1257 }
1258 p.exprLev--
1259
1260 closing := p.expectClosing(token.RBRACK, "type argument list")
1261
1262 if len(list) == 0 {
1263 p.errorExpected(closing, "type argument list")
1264 return &ast.IndexExpr{
1265 X: typ,
1266 Lbrack: opening,
1267 Index: &ast.BadExpr{From: opening + 1, To: closing},
1268 Rbrack: closing,
1269 }
1270 }
1271
1272 return typeparams.PackIndexExpr(typ, opening, list, closing)
1273 }
1274
1275 func (p *parser) tryIdentOrType() ast.Expr {
1276 defer decNestLev(incNestLev(p))
1277
1278 switch p.tok {
1279 case token.IDENT:
1280 typ := p.parseTypeName(nil)
1281 if p.tok == token.LBRACK && p.allowGenerics() {
1282 typ = p.parseTypeInstance(typ)
1283 }
1284 return typ
1285 case token.LBRACK:
1286 lbrack := p.expect(token.LBRACK)
1287 return p.parseArrayType(lbrack, nil)
1288 case token.STRUCT:
1289 return p.parseStructType()
1290 case token.MUL:
1291 return p.parsePointerType()
1292 case token.FUNC:
1293 typ := p.parseFuncType()
1294 return typ
1295 case token.INTERFACE:
1296 return p.parseInterfaceType()
1297 case token.MAP:
1298 return p.parseMapType()
1299 case token.CHAN, token.ARROW:
1300 return p.parseChanType()
1301 case token.LPAREN:
1302 lparen := p.pos
1303 p.next()
1304 typ := p.parseType()
1305 rparen := p.expect(token.RPAREN)
1306 return &ast.ParenExpr{Lparen: lparen, X: typ, Rparen: rparen}
1307 }
1308
1309
1310 return nil
1311 }
1312
1313
1314
1315
1316 func (p *parser) parseStmtList() (list []ast.Stmt) {
1317 if p.trace {
1318 defer un(trace(p, "StatementList"))
1319 }
1320
1321 for p.tok != token.CASE && p.tok != token.DEFAULT && p.tok != token.RBRACE && p.tok != token.EOF {
1322 list = append(list, p.parseStmt())
1323 }
1324
1325 return
1326 }
1327
1328 func (p *parser) parseBody() *ast.BlockStmt {
1329 if p.trace {
1330 defer un(trace(p, "Body"))
1331 }
1332
1333 lbrace := p.expect(token.LBRACE)
1334 list := p.parseStmtList()
1335 rbrace := p.expect2(token.RBRACE)
1336
1337 return &ast.BlockStmt{Lbrace: lbrace, List: list, Rbrace: rbrace}
1338 }
1339
1340 func (p *parser) parseBlockStmt() *ast.BlockStmt {
1341 if p.trace {
1342 defer un(trace(p, "BlockStmt"))
1343 }
1344
1345 lbrace := p.expect(token.LBRACE)
1346 list := p.parseStmtList()
1347 rbrace := p.expect2(token.RBRACE)
1348
1349 return &ast.BlockStmt{Lbrace: lbrace, List: list, Rbrace: rbrace}
1350 }
1351
1352
1353
1354
1355 func (p *parser) parseFuncTypeOrLit() ast.Expr {
1356 if p.trace {
1357 defer un(trace(p, "FuncTypeOrLit"))
1358 }
1359
1360 typ := p.parseFuncType()
1361 if p.tok != token.LBRACE {
1362
1363 return typ
1364 }
1365
1366 p.exprLev++
1367 body := p.parseBody()
1368 p.exprLev--
1369
1370 return &ast.FuncLit{Type: typ, Body: body}
1371 }
1372
1373
1374
1375 func (p *parser) parseOperand() ast.Expr {
1376 if p.trace {
1377 defer un(trace(p, "Operand"))
1378 }
1379
1380 switch p.tok {
1381 case token.IDENT:
1382 x := p.parseIdent()
1383 return x
1384
1385 case token.INT, token.FLOAT, token.IMAG, token.CHAR, token.STRING:
1386 x := &ast.BasicLit{ValuePos: p.pos, Kind: p.tok, Value: p.lit}
1387 p.next()
1388 return x
1389
1390 case token.LPAREN:
1391 lparen := p.pos
1392 p.next()
1393 p.exprLev++
1394 x := p.parseRhsOrType()
1395 p.exprLev--
1396 rparen := p.expect(token.RPAREN)
1397 return &ast.ParenExpr{Lparen: lparen, X: x, Rparen: rparen}
1398
1399 case token.FUNC:
1400 return p.parseFuncTypeOrLit()
1401 }
1402
1403 if typ := p.tryIdentOrType(); typ != nil {
1404
1405 _, isIdent := typ.(*ast.Ident)
1406 assert(!isIdent, "type cannot be identifier")
1407 return typ
1408 }
1409
1410
1411 pos := p.pos
1412 p.errorExpected(pos, "operand")
1413 p.advance(stmtStart)
1414 return &ast.BadExpr{From: pos, To: p.pos}
1415 }
1416
1417 func (p *parser) parseSelector(x ast.Expr) ast.Expr {
1418 if p.trace {
1419 defer un(trace(p, "Selector"))
1420 }
1421
1422 sel := p.parseIdent()
1423
1424 return &ast.SelectorExpr{X: x, Sel: sel}
1425 }
1426
1427 func (p *parser) parseTypeAssertion(x ast.Expr) ast.Expr {
1428 if p.trace {
1429 defer un(trace(p, "TypeAssertion"))
1430 }
1431
1432 lparen := p.expect(token.LPAREN)
1433 var typ ast.Expr
1434 if p.tok == token.TYPE {
1435
1436 p.next()
1437 } else {
1438 typ = p.parseType()
1439 }
1440 rparen := p.expect(token.RPAREN)
1441
1442 return &ast.TypeAssertExpr{X: x, Type: typ, Lparen: lparen, Rparen: rparen}
1443 }
1444
1445 func (p *parser) parseIndexOrSliceOrInstance(x ast.Expr) ast.Expr {
1446 if p.trace {
1447 defer un(trace(p, "parseIndexOrSliceOrInstance"))
1448 }
1449
1450 lbrack := p.expect(token.LBRACK)
1451 if p.tok == token.RBRACK {
1452
1453
1454 p.errorExpected(p.pos, "operand")
1455 rbrack := p.pos
1456 p.next()
1457 return &ast.IndexExpr{
1458 X: x,
1459 Lbrack: lbrack,
1460 Index: &ast.BadExpr{From: rbrack, To: rbrack},
1461 Rbrack: rbrack,
1462 }
1463 }
1464 p.exprLev++
1465
1466 const N = 3
1467 var args []ast.Expr
1468 var index [N]ast.Expr
1469 var colons [N - 1]token.Pos
1470 var firstComma token.Pos
1471 if p.tok != token.COLON {
1472
1473
1474 index[0] = p.parseRhsOrType()
1475 }
1476 ncolons := 0
1477 switch p.tok {
1478 case token.COLON:
1479
1480 for p.tok == token.COLON && ncolons < len(colons) {
1481 colons[ncolons] = p.pos
1482 ncolons++
1483 p.next()
1484 if p.tok != token.COLON && p.tok != token.RBRACK && p.tok != token.EOF {
1485 index[ncolons] = p.parseRhs()
1486 }
1487 }
1488 case token.COMMA:
1489 firstComma = p.pos
1490
1491 args = append(args, index[0])
1492 for p.tok == token.COMMA {
1493 p.next()
1494 if p.tok != token.RBRACK && p.tok != token.EOF {
1495 args = append(args, p.parseType())
1496 }
1497 }
1498 }
1499
1500 p.exprLev--
1501 rbrack := p.expect(token.RBRACK)
1502
1503 if ncolons > 0 {
1504
1505 slice3 := false
1506 if ncolons == 2 {
1507 slice3 = true
1508
1509
1510 if index[1] == nil {
1511 p.error(colons[0], "2nd index required in 3-index slice")
1512 index[1] = &ast.BadExpr{From: colons[0] + 1, To: colons[1]}
1513 }
1514 if index[2] == nil {
1515 p.error(colons[1], "3rd index required in 3-index slice")
1516 index[2] = &ast.BadExpr{From: colons[1] + 1, To: rbrack}
1517 }
1518 }
1519 return &ast.SliceExpr{X: x, Lbrack: lbrack, Low: index[0], High: index[1], Max: index[2], Slice3: slice3, Rbrack: rbrack}
1520 }
1521
1522 if len(args) == 0 {
1523
1524 return &ast.IndexExpr{X: x, Lbrack: lbrack, Index: index[0], Rbrack: rbrack}
1525 }
1526
1527 if !p.allowGenerics() {
1528 p.error(firstComma, "expected ']' or ':', found ','")
1529 return &ast.BadExpr{From: args[0].Pos(), To: args[len(args)-1].End()}
1530 }
1531
1532
1533 return typeparams.PackIndexExpr(x, lbrack, args, rbrack)
1534 }
1535
1536 func (p *parser) parseCallOrConversion(fun ast.Expr) *ast.CallExpr {
1537 if p.trace {
1538 defer un(trace(p, "CallOrConversion"))
1539 }
1540
1541 lparen := p.expect(token.LPAREN)
1542 p.exprLev++
1543 var list []ast.Expr
1544 var ellipsis token.Pos
1545 for p.tok != token.RPAREN && p.tok != token.EOF && !ellipsis.IsValid() {
1546 list = append(list, p.parseRhsOrType())
1547 if p.tok == token.ELLIPSIS {
1548 ellipsis = p.pos
1549 p.next()
1550 }
1551 if !p.atComma("argument list", token.RPAREN) {
1552 break
1553 }
1554 p.next()
1555 }
1556 p.exprLev--
1557 rparen := p.expectClosing(token.RPAREN, "argument list")
1558
1559 return &ast.CallExpr{Fun: fun, Lparen: lparen, Args: list, Ellipsis: ellipsis, Rparen: rparen}
1560 }
1561
1562 func (p *parser) parseValue() ast.Expr {
1563 if p.trace {
1564 defer un(trace(p, "Element"))
1565 }
1566
1567 if p.tok == token.LBRACE {
1568 return p.parseLiteralValue(nil)
1569 }
1570
1571 x := p.checkExpr(p.parseExpr())
1572
1573 return x
1574 }
1575
1576 func (p *parser) parseElement() ast.Expr {
1577 if p.trace {
1578 defer un(trace(p, "Element"))
1579 }
1580
1581 x := p.parseValue()
1582 if p.tok == token.COLON {
1583 colon := p.pos
1584 p.next()
1585 x = &ast.KeyValueExpr{Key: x, Colon: colon, Value: p.parseValue()}
1586 }
1587
1588 return x
1589 }
1590
1591 func (p *parser) parseElementList() (list []ast.Expr) {
1592 if p.trace {
1593 defer un(trace(p, "ElementList"))
1594 }
1595
1596 for p.tok != token.RBRACE && p.tok != token.EOF {
1597 list = append(list, p.parseElement())
1598 if !p.atComma("composite literal", token.RBRACE) {
1599 break
1600 }
1601 p.next()
1602 }
1603
1604 return
1605 }
1606
1607 func (p *parser) parseLiteralValue(typ ast.Expr) ast.Expr {
1608 if p.trace {
1609 defer un(trace(p, "LiteralValue"))
1610 }
1611
1612 lbrace := p.expect(token.LBRACE)
1613 var elts []ast.Expr
1614 p.exprLev++
1615 if p.tok != token.RBRACE {
1616 elts = p.parseElementList()
1617 }
1618 p.exprLev--
1619 rbrace := p.expectClosing(token.RBRACE, "composite literal")
1620 return &ast.CompositeLit{Type: typ, Lbrace: lbrace, Elts: elts, Rbrace: rbrace}
1621 }
1622
1623
1624 func (p *parser) checkExpr(x ast.Expr) ast.Expr {
1625 switch unparen(x).(type) {
1626 case *ast.BadExpr:
1627 case *ast.Ident:
1628 case *ast.BasicLit:
1629 case *ast.FuncLit:
1630 case *ast.CompositeLit:
1631 case *ast.ParenExpr:
1632 panic("unreachable")
1633 case *ast.SelectorExpr:
1634 case *ast.IndexExpr:
1635 case *ast.IndexListExpr:
1636 case *ast.SliceExpr:
1637 case *ast.TypeAssertExpr:
1638
1639
1640
1641
1642
1643 case *ast.CallExpr:
1644 case *ast.StarExpr:
1645 case *ast.UnaryExpr:
1646 case *ast.BinaryExpr:
1647 default:
1648
1649 p.errorExpected(x.Pos(), "expression")
1650 x = &ast.BadExpr{From: x.Pos(), To: p.safePos(x.End())}
1651 }
1652 return x
1653 }
1654
1655
1656 func unparen(x ast.Expr) ast.Expr {
1657 if p, isParen := x.(*ast.ParenExpr); isParen {
1658 x = unparen(p.X)
1659 }
1660 return x
1661 }
1662
1663
1664
1665 func (p *parser) checkExprOrType(x ast.Expr) ast.Expr {
1666 switch t := unparen(x).(type) {
1667 case *ast.ParenExpr:
1668 panic("unreachable")
1669 case *ast.ArrayType:
1670 if len, isEllipsis := t.Len.(*ast.Ellipsis); isEllipsis {
1671 p.error(len.Pos(), "expected array length, found '...'")
1672 x = &ast.BadExpr{From: x.Pos(), To: p.safePos(x.End())}
1673 }
1674 }
1675
1676
1677 return x
1678 }
1679
1680 func (p *parser) parsePrimaryExpr(x ast.Expr) ast.Expr {
1681 if p.trace {
1682 defer un(trace(p, "PrimaryExpr"))
1683 }
1684
1685 if x == nil {
1686 x = p.parseOperand()
1687 }
1688
1689
1690
1691 var n int
1692 defer func() { p.nestLev -= n }()
1693 for n = 1; ; n++ {
1694 incNestLev(p)
1695 switch p.tok {
1696 case token.PERIOD:
1697 p.next()
1698 switch p.tok {
1699 case token.IDENT:
1700 x = p.parseSelector(p.checkExprOrType(x))
1701 case token.LPAREN:
1702 x = p.parseTypeAssertion(p.checkExpr(x))
1703 default:
1704 pos := p.pos
1705 p.errorExpected(pos, "selector or type assertion")
1706
1707
1708
1709
1710
1711 if p.tok != token.RBRACE {
1712 p.next()
1713 }
1714 sel := &ast.Ident{NamePos: pos, Name: "_"}
1715 x = &ast.SelectorExpr{X: x, Sel: sel}
1716 }
1717 case token.LBRACK:
1718 x = p.parseIndexOrSliceOrInstance(p.checkExpr(x))
1719 case token.LPAREN:
1720 x = p.parseCallOrConversion(p.checkExprOrType(x))
1721 case token.LBRACE:
1722
1723
1724 t := unparen(x)
1725
1726 switch t.(type) {
1727 case *ast.BadExpr, *ast.Ident, *ast.SelectorExpr:
1728 if p.exprLev < 0 {
1729 return x
1730 }
1731
1732 case *ast.IndexExpr, *ast.IndexListExpr:
1733 if p.exprLev < 0 {
1734 return x
1735 }
1736
1737 case *ast.ArrayType, *ast.StructType, *ast.MapType:
1738
1739 default:
1740 return x
1741 }
1742 if t != x {
1743 p.error(t.Pos(), "cannot parenthesize type in composite literal")
1744
1745 }
1746 x = p.parseLiteralValue(x)
1747 default:
1748 return x
1749 }
1750 }
1751 }
1752
1753 func (p *parser) parseUnaryExpr() ast.Expr {
1754 defer decNestLev(incNestLev(p))
1755
1756 if p.trace {
1757 defer un(trace(p, "UnaryExpr"))
1758 }
1759
1760 switch p.tok {
1761 case token.ADD, token.SUB, token.NOT, token.XOR, token.AND, token.TILDE:
1762 pos, op := p.pos, p.tok
1763 p.next()
1764 x := p.parseUnaryExpr()
1765 return &ast.UnaryExpr{OpPos: pos, Op: op, X: p.checkExpr(x)}
1766
1767 case token.ARROW:
1768
1769 arrow := p.pos
1770 p.next()
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786 x := p.parseUnaryExpr()
1787
1788
1789 if typ, ok := x.(*ast.ChanType); ok {
1790
1791
1792
1793 dir := ast.SEND
1794 for ok && dir == ast.SEND {
1795 if typ.Dir == ast.RECV {
1796
1797 p.errorExpected(typ.Arrow, "'chan'")
1798 }
1799 arrow, typ.Begin, typ.Arrow = typ.Arrow, arrow, arrow
1800 dir, typ.Dir = typ.Dir, ast.RECV
1801 typ, ok = typ.Value.(*ast.ChanType)
1802 }
1803 if dir == ast.SEND {
1804 p.errorExpected(arrow, "channel type")
1805 }
1806
1807 return x
1808 }
1809
1810
1811 return &ast.UnaryExpr{OpPos: arrow, Op: token.ARROW, X: p.checkExpr(x)}
1812
1813 case token.MUL:
1814
1815 pos := p.pos
1816 p.next()
1817 x := p.parseUnaryExpr()
1818 return &ast.StarExpr{Star: pos, X: p.checkExprOrType(x)}
1819 }
1820
1821 return p.parsePrimaryExpr(nil)
1822 }
1823
1824 func (p *parser) tokPrec() (token.Token, int) {
1825 tok := p.tok
1826 if p.inRhs && tok == token.ASSIGN {
1827 tok = token.EQL
1828 }
1829 return tok, tok.Precedence()
1830 }
1831
1832
1833
1834
1835
1836
1837 func (p *parser) parseBinaryExpr(x ast.Expr, prec1 int, check bool) ast.Expr {
1838 if p.trace {
1839 defer un(trace(p, "BinaryExpr"))
1840 }
1841
1842 if x == nil {
1843 x = p.parseUnaryExpr()
1844 }
1845
1846
1847
1848 var n int
1849 defer func() { p.nestLev -= n }()
1850 for n = 1; ; n++ {
1851 incNestLev(p)
1852 op, oprec := p.tokPrec()
1853 if oprec < prec1 {
1854 return x
1855 }
1856 pos := p.expect(op)
1857 y := p.parseBinaryExpr(nil, oprec+1, check)
1858 if check {
1859 x = p.checkExpr(x)
1860 y = p.checkExpr(y)
1861 }
1862 x = &ast.BinaryExpr{X: x, OpPos: pos, Op: op, Y: y}
1863 }
1864 }
1865
1866
1867
1868
1869 func (p *parser) parseExpr() ast.Expr {
1870 if p.trace {
1871 defer un(trace(p, "Expression"))
1872 }
1873
1874 return p.parseBinaryExpr(nil, token.LowestPrec+1, true)
1875 }
1876
1877 func (p *parser) parseRhs() ast.Expr {
1878 old := p.inRhs
1879 p.inRhs = true
1880 x := p.checkExpr(p.parseExpr())
1881 p.inRhs = old
1882 return x
1883 }
1884
1885 func (p *parser) parseRhsOrType() ast.Expr {
1886 old := p.inRhs
1887 p.inRhs = true
1888 x := p.checkExprOrType(p.parseExpr())
1889 p.inRhs = old
1890 return x
1891 }
1892
1893
1894
1895
1896
1897 const (
1898 basic = iota
1899 labelOk
1900 rangeOk
1901 )
1902
1903
1904
1905
1906
1907 func (p *parser) parseSimpleStmt(mode int) (ast.Stmt, bool) {
1908 if p.trace {
1909 defer un(trace(p, "SimpleStmt"))
1910 }
1911
1912 x := p.parseList(false)
1913
1914 switch p.tok {
1915 case
1916 token.DEFINE, token.ASSIGN, token.ADD_ASSIGN,
1917 token.SUB_ASSIGN, token.MUL_ASSIGN, token.QUO_ASSIGN,
1918 token.REM_ASSIGN, token.AND_ASSIGN, token.OR_ASSIGN,
1919 token.XOR_ASSIGN, token.SHL_ASSIGN, token.SHR_ASSIGN, token.AND_NOT_ASSIGN:
1920
1921 pos, tok := p.pos, p.tok
1922 p.next()
1923 var y []ast.Expr
1924 isRange := false
1925 if mode == rangeOk && p.tok == token.RANGE && (tok == token.DEFINE || tok == token.ASSIGN) {
1926 pos := p.pos
1927 p.next()
1928 y = []ast.Expr{&ast.UnaryExpr{OpPos: pos, Op: token.RANGE, X: p.parseRhs()}}
1929 isRange = true
1930 } else {
1931 y = p.parseList(true)
1932 }
1933 as := &ast.AssignStmt{Lhs: x, TokPos: pos, Tok: tok, Rhs: y}
1934 if tok == token.DEFINE {
1935 p.checkAssignStmt(as)
1936 }
1937 return as, isRange
1938 }
1939
1940 if len(x) > 1 {
1941 p.errorExpected(x[0].Pos(), "1 expression")
1942
1943 }
1944
1945 switch p.tok {
1946 case token.COLON:
1947
1948 colon := p.pos
1949 p.next()
1950 if label, isIdent := x[0].(*ast.Ident); mode == labelOk && isIdent {
1951
1952
1953
1954 stmt := &ast.LabeledStmt{Label: label, Colon: colon, Stmt: p.parseStmt()}
1955 return stmt, false
1956 }
1957
1958
1959
1960
1961
1962
1963 p.error(colon, "illegal label declaration")
1964 return &ast.BadStmt{From: x[0].Pos(), To: colon + 1}, false
1965
1966 case token.ARROW:
1967
1968 arrow := p.pos
1969 p.next()
1970 y := p.parseRhs()
1971 return &ast.SendStmt{Chan: x[0], Arrow: arrow, Value: y}, false
1972
1973 case token.INC, token.DEC:
1974
1975 s := &ast.IncDecStmt{X: x[0], TokPos: p.pos, Tok: p.tok}
1976 p.next()
1977 return s, false
1978 }
1979
1980
1981 return &ast.ExprStmt{X: x[0]}, false
1982 }
1983
1984 func (p *parser) checkAssignStmt(as *ast.AssignStmt) {
1985 for _, x := range as.Lhs {
1986 if _, isIdent := x.(*ast.Ident); !isIdent {
1987 p.errorExpected(x.Pos(), "identifier on left side of :=")
1988 }
1989 }
1990 }
1991
1992 func (p *parser) parseCallExpr(callType string) *ast.CallExpr {
1993 x := p.parseRhsOrType()
1994 if call, isCall := x.(*ast.CallExpr); isCall {
1995 return call
1996 }
1997 if _, isBad := x.(*ast.BadExpr); !isBad {
1998
1999 p.error(p.safePos(x.End()), fmt.Sprintf("function must be invoked in %s statement", callType))
2000 }
2001 return nil
2002 }
2003
2004 func (p *parser) parseGoStmt() ast.Stmt {
2005 if p.trace {
2006 defer un(trace(p, "GoStmt"))
2007 }
2008
2009 pos := p.expect(token.GO)
2010 call := p.parseCallExpr("go")
2011 p.expectSemi()
2012 if call == nil {
2013 return &ast.BadStmt{From: pos, To: pos + 2}
2014 }
2015
2016 return &ast.GoStmt{Go: pos, Call: call}
2017 }
2018
2019 func (p *parser) parseDeferStmt() ast.Stmt {
2020 if p.trace {
2021 defer un(trace(p, "DeferStmt"))
2022 }
2023
2024 pos := p.expect(token.DEFER)
2025 call := p.parseCallExpr("defer")
2026 p.expectSemi()
2027 if call == nil {
2028 return &ast.BadStmt{From: pos, To: pos + 5}
2029 }
2030
2031 return &ast.DeferStmt{Defer: pos, Call: call}
2032 }
2033
2034 func (p *parser) parseReturnStmt() *ast.ReturnStmt {
2035 if p.trace {
2036 defer un(trace(p, "ReturnStmt"))
2037 }
2038
2039 pos := p.pos
2040 p.expect(token.RETURN)
2041 var x []ast.Expr
2042 if p.tok != token.SEMICOLON && p.tok != token.RBRACE {
2043 x = p.parseList(true)
2044 }
2045 p.expectSemi()
2046
2047 return &ast.ReturnStmt{Return: pos, Results: x}
2048 }
2049
2050 func (p *parser) parseBranchStmt(tok token.Token) *ast.BranchStmt {
2051 if p.trace {
2052 defer un(trace(p, "BranchStmt"))
2053 }
2054
2055 pos := p.expect(tok)
2056 var label *ast.Ident
2057 if tok != token.FALLTHROUGH && p.tok == token.IDENT {
2058 label = p.parseIdent()
2059 }
2060 p.expectSemi()
2061
2062 return &ast.BranchStmt{TokPos: pos, Tok: tok, Label: label}
2063 }
2064
2065 func (p *parser) makeExpr(s ast.Stmt, want string) ast.Expr {
2066 if s == nil {
2067 return nil
2068 }
2069 if es, isExpr := s.(*ast.ExprStmt); isExpr {
2070 return p.checkExpr(es.X)
2071 }
2072 found := "simple statement"
2073 if _, isAss := s.(*ast.AssignStmt); isAss {
2074 found = "assignment"
2075 }
2076 p.error(s.Pos(), fmt.Sprintf("expected %s, found %s (missing parentheses around composite literal?)", want, found))
2077 return &ast.BadExpr{From: s.Pos(), To: p.safePos(s.End())}
2078 }
2079
2080
2081
2082
2083 func (p *parser) parseIfHeader() (init ast.Stmt, cond ast.Expr) {
2084 if p.tok == token.LBRACE {
2085 p.error(p.pos, "missing condition in if statement")
2086 cond = &ast.BadExpr{From: p.pos, To: p.pos}
2087 return
2088 }
2089
2090
2091 prevLev := p.exprLev
2092 p.exprLev = -1
2093
2094 if p.tok != token.SEMICOLON {
2095
2096 if p.tok == token.VAR {
2097 p.next()
2098 p.error(p.pos, "var declaration not allowed in 'IF' initializer")
2099 }
2100 init, _ = p.parseSimpleStmt(basic)
2101 }
2102
2103 var condStmt ast.Stmt
2104 var semi struct {
2105 pos token.Pos
2106 lit string
2107 }
2108 if p.tok != token.LBRACE {
2109 if p.tok == token.SEMICOLON {
2110 semi.pos = p.pos
2111 semi.lit = p.lit
2112 p.next()
2113 } else {
2114 p.expect(token.SEMICOLON)
2115 }
2116 if p.tok != token.LBRACE {
2117 condStmt, _ = p.parseSimpleStmt(basic)
2118 }
2119 } else {
2120 condStmt = init
2121 init = nil
2122 }
2123
2124 if condStmt != nil {
2125 cond = p.makeExpr(condStmt, "boolean expression")
2126 } else if semi.pos.IsValid() {
2127 if semi.lit == "\n" {
2128 p.error(semi.pos, "unexpected newline, expecting { after if clause")
2129 } else {
2130 p.error(semi.pos, "missing condition in if statement")
2131 }
2132 }
2133
2134
2135 if cond == nil {
2136 cond = &ast.BadExpr{From: p.pos, To: p.pos}
2137 }
2138
2139 p.exprLev = prevLev
2140 return
2141 }
2142
2143 func (p *parser) parseIfStmt() *ast.IfStmt {
2144 defer decNestLev(incNestLev(p))
2145
2146 if p.trace {
2147 defer un(trace(p, "IfStmt"))
2148 }
2149
2150 pos := p.expect(token.IF)
2151
2152 init, cond := p.parseIfHeader()
2153 body := p.parseBlockStmt()
2154
2155 var else_ ast.Stmt
2156 if p.tok == token.ELSE {
2157 p.next()
2158 switch p.tok {
2159 case token.IF:
2160 else_ = p.parseIfStmt()
2161 case token.LBRACE:
2162 else_ = p.parseBlockStmt()
2163 p.expectSemi()
2164 default:
2165 p.errorExpected(p.pos, "if statement or block")
2166 else_ = &ast.BadStmt{From: p.pos, To: p.pos}
2167 }
2168 } else {
2169 p.expectSemi()
2170 }
2171
2172 return &ast.IfStmt{If: pos, Init: init, Cond: cond, Body: body, Else: else_}
2173 }
2174
2175 func (p *parser) parseTypeList() (list []ast.Expr) {
2176 if p.trace {
2177 defer un(trace(p, "TypeList"))
2178 }
2179
2180 list = append(list, p.parseType())
2181 for p.tok == token.COMMA {
2182 p.next()
2183 list = append(list, p.parseType())
2184 }
2185
2186 return
2187 }
2188
2189 func (p *parser) parseCaseClause(typeSwitch bool) *ast.CaseClause {
2190 if p.trace {
2191 defer un(trace(p, "CaseClause"))
2192 }
2193
2194 pos := p.pos
2195 var list []ast.Expr
2196 if p.tok == token.CASE {
2197 p.next()
2198 if typeSwitch {
2199 list = p.parseTypeList()
2200 } else {
2201 list = p.parseList(true)
2202 }
2203 } else {
2204 p.expect(token.DEFAULT)
2205 }
2206
2207 colon := p.expect(token.COLON)
2208 body := p.parseStmtList()
2209
2210 return &ast.CaseClause{Case: pos, List: list, Colon: colon, Body: body}
2211 }
2212
2213 func isTypeSwitchAssert(x ast.Expr) bool {
2214 a, ok := x.(*ast.TypeAssertExpr)
2215 return ok && a.Type == nil
2216 }
2217
2218 func (p *parser) isTypeSwitchGuard(s ast.Stmt) bool {
2219 switch t := s.(type) {
2220 case *ast.ExprStmt:
2221
2222 return isTypeSwitchAssert(t.X)
2223 case *ast.AssignStmt:
2224
2225 if len(t.Lhs) == 1 && len(t.Rhs) == 1 && isTypeSwitchAssert(t.Rhs[0]) {
2226 switch t.Tok {
2227 case token.ASSIGN:
2228
2229 p.error(t.TokPos, "expected ':=', found '='")
2230 fallthrough
2231 case token.DEFINE:
2232 return true
2233 }
2234 }
2235 }
2236 return false
2237 }
2238
2239 func (p *parser) parseSwitchStmt() ast.Stmt {
2240 if p.trace {
2241 defer un(trace(p, "SwitchStmt"))
2242 }
2243
2244 pos := p.expect(token.SWITCH)
2245
2246 var s1, s2 ast.Stmt
2247 if p.tok != token.LBRACE {
2248 prevLev := p.exprLev
2249 p.exprLev = -1
2250 if p.tok != token.SEMICOLON {
2251 s2, _ = p.parseSimpleStmt(basic)
2252 }
2253 if p.tok == token.SEMICOLON {
2254 p.next()
2255 s1 = s2
2256 s2 = nil
2257 if p.tok != token.LBRACE {
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270 s2, _ = p.parseSimpleStmt(basic)
2271 }
2272 }
2273 p.exprLev = prevLev
2274 }
2275
2276 typeSwitch := p.isTypeSwitchGuard(s2)
2277 lbrace := p.expect(token.LBRACE)
2278 var list []ast.Stmt
2279 for p.tok == token.CASE || p.tok == token.DEFAULT {
2280 list = append(list, p.parseCaseClause(typeSwitch))
2281 }
2282 rbrace := p.expect(token.RBRACE)
2283 p.expectSemi()
2284 body := &ast.BlockStmt{Lbrace: lbrace, List: list, Rbrace: rbrace}
2285
2286 if typeSwitch {
2287 return &ast.TypeSwitchStmt{Switch: pos, Init: s1, Assign: s2, Body: body}
2288 }
2289
2290 return &ast.SwitchStmt{Switch: pos, Init: s1, Tag: p.makeExpr(s2, "switch expression"), Body: body}
2291 }
2292
2293 func (p *parser) parseCommClause() *ast.CommClause {
2294 if p.trace {
2295 defer un(trace(p, "CommClause"))
2296 }
2297
2298 pos := p.pos
2299 var comm ast.Stmt
2300 if p.tok == token.CASE {
2301 p.next()
2302 lhs := p.parseList(false)
2303 if p.tok == token.ARROW {
2304
2305 if len(lhs) > 1 {
2306 p.errorExpected(lhs[0].Pos(), "1 expression")
2307
2308 }
2309 arrow := p.pos
2310 p.next()
2311 rhs := p.parseRhs()
2312 comm = &ast.SendStmt{Chan: lhs[0], Arrow: arrow, Value: rhs}
2313 } else {
2314
2315 if tok := p.tok; tok == token.ASSIGN || tok == token.DEFINE {
2316
2317 if len(lhs) > 2 {
2318 p.errorExpected(lhs[0].Pos(), "1 or 2 expressions")
2319
2320 lhs = lhs[0:2]
2321 }
2322 pos := p.pos
2323 p.next()
2324 rhs := p.parseRhs()
2325 as := &ast.AssignStmt{Lhs: lhs, TokPos: pos, Tok: tok, Rhs: []ast.Expr{rhs}}
2326 if tok == token.DEFINE {
2327 p.checkAssignStmt(as)
2328 }
2329 comm = as
2330 } else {
2331
2332 if len(lhs) > 1 {
2333 p.errorExpected(lhs[0].Pos(), "1 expression")
2334
2335 }
2336 comm = &ast.ExprStmt{X: lhs[0]}
2337 }
2338 }
2339 } else {
2340 p.expect(token.DEFAULT)
2341 }
2342
2343 colon := p.expect(token.COLON)
2344 body := p.parseStmtList()
2345
2346 return &ast.CommClause{Case: pos, Comm: comm, Colon: colon, Body: body}
2347 }
2348
2349 func (p *parser) parseSelectStmt() *ast.SelectStmt {
2350 if p.trace {
2351 defer un(trace(p, "SelectStmt"))
2352 }
2353
2354 pos := p.expect(token.SELECT)
2355 lbrace := p.expect(token.LBRACE)
2356 var list []ast.Stmt
2357 for p.tok == token.CASE || p.tok == token.DEFAULT {
2358 list = append(list, p.parseCommClause())
2359 }
2360 rbrace := p.expect(token.RBRACE)
2361 p.expectSemi()
2362 body := &ast.BlockStmt{Lbrace: lbrace, List: list, Rbrace: rbrace}
2363
2364 return &ast.SelectStmt{Select: pos, Body: body}
2365 }
2366
2367 func (p *parser) parseForStmt() ast.Stmt {
2368 if p.trace {
2369 defer un(trace(p, "ForStmt"))
2370 }
2371
2372 pos := p.expect(token.FOR)
2373
2374 var s1, s2, s3 ast.Stmt
2375 var isRange bool
2376 if p.tok != token.LBRACE {
2377 prevLev := p.exprLev
2378 p.exprLev = -1
2379 if p.tok != token.SEMICOLON {
2380 if p.tok == token.RANGE {
2381
2382 pos := p.pos
2383 p.next()
2384 y := []ast.Expr{&ast.UnaryExpr{OpPos: pos, Op: token.RANGE, X: p.parseRhs()}}
2385 s2 = &ast.AssignStmt{Rhs: y}
2386 isRange = true
2387 } else {
2388 s2, isRange = p.parseSimpleStmt(rangeOk)
2389 }
2390 }
2391 if !isRange && p.tok == token.SEMICOLON {
2392 p.next()
2393 s1 = s2
2394 s2 = nil
2395 if p.tok != token.SEMICOLON {
2396 s2, _ = p.parseSimpleStmt(basic)
2397 }
2398 p.expectSemi()
2399 if p.tok != token.LBRACE {
2400 s3, _ = p.parseSimpleStmt(basic)
2401 }
2402 }
2403 p.exprLev = prevLev
2404 }
2405
2406 body := p.parseBlockStmt()
2407 p.expectSemi()
2408
2409 if isRange {
2410 as := s2.(*ast.AssignStmt)
2411
2412 var key, value ast.Expr
2413 switch len(as.Lhs) {
2414 case 0:
2415
2416 case 1:
2417 key = as.Lhs[0]
2418 case 2:
2419 key, value = as.Lhs[0], as.Lhs[1]
2420 default:
2421 p.errorExpected(as.Lhs[len(as.Lhs)-1].Pos(), "at most 2 expressions")
2422 return &ast.BadStmt{From: pos, To: p.safePos(body.End())}
2423 }
2424
2425
2426 x := as.Rhs[0].(*ast.UnaryExpr).X
2427 return &ast.RangeStmt{
2428 For: pos,
2429 Key: key,
2430 Value: value,
2431 TokPos: as.TokPos,
2432 Tok: as.Tok,
2433 X: x,
2434 Body: body,
2435 }
2436 }
2437
2438
2439 return &ast.ForStmt{
2440 For: pos,
2441 Init: s1,
2442 Cond: p.makeExpr(s2, "boolean or range expression"),
2443 Post: s3,
2444 Body: body,
2445 }
2446 }
2447
2448 func (p *parser) parseStmt() (s ast.Stmt) {
2449 defer decNestLev(incNestLev(p))
2450
2451 if p.trace {
2452 defer un(trace(p, "Statement"))
2453 }
2454
2455 switch p.tok {
2456 case token.CONST, token.TYPE, token.VAR:
2457 s = &ast.DeclStmt{Decl: p.parseDecl(stmtStart)}
2458 case
2459
2460 token.IDENT, token.INT, token.FLOAT, token.IMAG, token.CHAR, token.STRING, token.FUNC, token.LPAREN,
2461 token.LBRACK, token.STRUCT, token.MAP, token.CHAN, token.INTERFACE,
2462 token.ADD, token.SUB, token.MUL, token.AND, token.XOR, token.ARROW, token.NOT:
2463 s, _ = p.parseSimpleStmt(labelOk)
2464
2465
2466
2467 if _, isLabeledStmt := s.(*ast.LabeledStmt); !isLabeledStmt {
2468 p.expectSemi()
2469 }
2470 case token.GO:
2471 s = p.parseGoStmt()
2472 case token.DEFER:
2473 s = p.parseDeferStmt()
2474 case token.RETURN:
2475 s = p.parseReturnStmt()
2476 case token.BREAK, token.CONTINUE, token.GOTO, token.FALLTHROUGH:
2477 s = p.parseBranchStmt(p.tok)
2478 case token.LBRACE:
2479 s = p.parseBlockStmt()
2480 p.expectSemi()
2481 case token.IF:
2482 s = p.parseIfStmt()
2483 case token.SWITCH:
2484 s = p.parseSwitchStmt()
2485 case token.SELECT:
2486 s = p.parseSelectStmt()
2487 case token.FOR:
2488 s = p.parseForStmt()
2489 case token.SEMICOLON:
2490
2491
2492
2493 s = &ast.EmptyStmt{Semicolon: p.pos, Implicit: p.lit == "\n"}
2494 p.next()
2495 case token.RBRACE:
2496
2497 s = &ast.EmptyStmt{Semicolon: p.pos, Implicit: true}
2498 default:
2499
2500 pos := p.pos
2501 p.errorExpected(pos, "statement")
2502 p.advance(stmtStart)
2503 s = &ast.BadStmt{From: pos, To: p.pos}
2504 }
2505
2506 return
2507 }
2508
2509
2510
2511
2512 type parseSpecFunction func(doc *ast.CommentGroup, pos token.Pos, keyword token.Token, iota int) ast.Spec
2513
2514 func isValidImport(lit string) bool {
2515 const illegalChars = `!"#$%&'()*,:;<=>?[\]^{|}` + "`\uFFFD"
2516 s, _ := strconv.Unquote(lit)
2517 for _, r := range s {
2518 if !unicode.IsGraphic(r) || unicode.IsSpace(r) || strings.ContainsRune(illegalChars, r) {
2519 return false
2520 }
2521 }
2522 return s != ""
2523 }
2524
2525 func (p *parser) parseImportSpec(doc *ast.CommentGroup, _ token.Pos, _ token.Token, _ int) ast.Spec {
2526 if p.trace {
2527 defer un(trace(p, "ImportSpec"))
2528 }
2529
2530 var ident *ast.Ident
2531 switch p.tok {
2532 case token.PERIOD:
2533 ident = &ast.Ident{NamePos: p.pos, Name: "."}
2534 p.next()
2535 case token.IDENT:
2536 ident = p.parseIdent()
2537 }
2538
2539 pos := p.pos
2540 var path string
2541 if p.tok == token.STRING {
2542 path = p.lit
2543 if !isValidImport(path) {
2544 p.error(pos, "invalid import path: "+path)
2545 }
2546 p.next()
2547 } else {
2548 p.expect(token.STRING)
2549 }
2550 p.expectSemi()
2551
2552
2553 spec := &ast.ImportSpec{
2554 Doc: doc,
2555 Name: ident,
2556 Path: &ast.BasicLit{ValuePos: pos, Kind: token.STRING, Value: path},
2557 Comment: p.lineComment,
2558 }
2559 p.imports = append(p.imports, spec)
2560
2561 return spec
2562 }
2563
2564 func (p *parser) parseValueSpec(doc *ast.CommentGroup, _ token.Pos, keyword token.Token, iota int) ast.Spec {
2565 if p.trace {
2566 defer un(trace(p, keyword.String()+"Spec"))
2567 }
2568
2569 pos := p.pos
2570 idents := p.parseIdentList()
2571 typ := p.tryIdentOrType()
2572 var values []ast.Expr
2573
2574 if p.tok == token.ASSIGN {
2575 p.next()
2576 values = p.parseList(true)
2577 }
2578 p.expectSemi()
2579
2580 switch keyword {
2581 case token.VAR:
2582 if typ == nil && values == nil {
2583 p.error(pos, "missing variable type or initialization")
2584 }
2585 case token.CONST:
2586 if values == nil && (iota == 0 || typ != nil) {
2587 p.error(pos, "missing constant value")
2588 }
2589 }
2590
2591 spec := &ast.ValueSpec{
2592 Doc: doc,
2593 Names: idents,
2594 Type: typ,
2595 Values: values,
2596 Comment: p.lineComment,
2597 }
2598 return spec
2599 }
2600
2601 func (p *parser) parseGenericType(spec *ast.TypeSpec, openPos token.Pos, name0 *ast.Ident, typ0 ast.Expr) {
2602 if p.trace {
2603 defer un(trace(p, "parseGenericType"))
2604 }
2605
2606 list := p.parseParameterList(name0, typ0, token.RBRACK)
2607 closePos := p.expect(token.RBRACK)
2608 spec.TypeParams = &ast.FieldList{Opening: openPos, List: list, Closing: closePos}
2609
2610
2611 if p.tok == token.ASSIGN {
2612
2613 spec.Assign = p.pos
2614 p.next()
2615 }
2616 spec.Type = p.parseType()
2617 }
2618
2619 func (p *parser) parseTypeSpec(doc *ast.CommentGroup, _ token.Pos, _ token.Token, _ int) ast.Spec {
2620 if p.trace {
2621 defer un(trace(p, "TypeSpec"))
2622 }
2623
2624 name := p.parseIdent()
2625 spec := &ast.TypeSpec{Doc: doc, Name: name}
2626
2627 if p.tok == token.LBRACK && p.allowGenerics() {
2628
2629
2630 lbrack := p.pos
2631 p.next()
2632 if p.tok == token.IDENT {
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648 var x ast.Expr = p.parseIdent()
2649 if p.tok != token.LBRACK {
2650
2651
2652
2653 p.exprLev++
2654 lhs := p.parsePrimaryExpr(x)
2655 x = p.parseBinaryExpr(lhs, token.LowestPrec+1, false)
2656 p.exprLev--
2657 }
2658
2659
2660
2661
2662
2663
2664
2665 if pname, ptype := extractName(x, p.tok == token.COMMA); pname != nil && (ptype != nil || p.tok != token.RBRACK) {
2666
2667
2668
2669 p.parseGenericType(spec, lbrack, pname, ptype)
2670 } else {
2671
2672
2673 spec.Type = p.parseArrayType(lbrack, x)
2674 }
2675 } else {
2676
2677 spec.Type = p.parseArrayType(lbrack, nil)
2678 }
2679 } else {
2680
2681 if p.tok == token.ASSIGN {
2682
2683 spec.Assign = p.pos
2684 p.next()
2685 }
2686 spec.Type = p.parseType()
2687 }
2688
2689 p.expectSemi()
2690 spec.Comment = p.lineComment
2691
2692 return spec
2693 }
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713 func extractName(x ast.Expr, force bool) (*ast.Ident, ast.Expr) {
2714 switch x := x.(type) {
2715 case *ast.Ident:
2716 return x, nil
2717 case *ast.BinaryExpr:
2718 switch x.Op {
2719 case token.MUL:
2720 if name, _ := x.X.(*ast.Ident); name != nil && (force || isTypeElem(x.Y)) {
2721
2722 return name, &ast.StarExpr{Star: x.OpPos, X: x.Y}
2723 }
2724 case token.OR:
2725 if name, lhs := extractName(x.X, force || isTypeElem(x.Y)); name != nil && lhs != nil {
2726
2727 op := *x
2728 op.X = lhs
2729 return name, &op
2730 }
2731 }
2732 case *ast.CallExpr:
2733 if name, _ := x.Fun.(*ast.Ident); name != nil {
2734 if len(x.Args) == 1 && x.Ellipsis == token.NoPos && (force || isTypeElem(x.Args[0])) {
2735
2736 return name, x.Args[0]
2737 }
2738 }
2739 }
2740 return nil, x
2741 }
2742
2743
2744
2745 func isTypeElem(x ast.Expr) bool {
2746 switch x := x.(type) {
2747 case *ast.ArrayType, *ast.StructType, *ast.FuncType, *ast.InterfaceType, *ast.MapType, *ast.ChanType:
2748 return true
2749 case *ast.BinaryExpr:
2750 return isTypeElem(x.X) || isTypeElem(x.Y)
2751 case *ast.UnaryExpr:
2752 return x.Op == token.TILDE
2753 case *ast.ParenExpr:
2754 return isTypeElem(x.X)
2755 }
2756 return false
2757 }
2758
2759 func (p *parser) parseGenDecl(keyword token.Token, f parseSpecFunction) *ast.GenDecl {
2760 if p.trace {
2761 defer un(trace(p, "GenDecl("+keyword.String()+")"))
2762 }
2763
2764 doc := p.leadComment
2765 pos := p.expect(keyword)
2766 var lparen, rparen token.Pos
2767 var list []ast.Spec
2768 if p.tok == token.LPAREN {
2769 lparen = p.pos
2770 p.next()
2771 for iota := 0; p.tok != token.RPAREN && p.tok != token.EOF; iota++ {
2772 list = append(list, f(p.leadComment, pos, keyword, iota))
2773 }
2774 rparen = p.expect(token.RPAREN)
2775 p.expectSemi()
2776 } else {
2777 list = append(list, f(nil, pos, keyword, 0))
2778 }
2779
2780 return &ast.GenDecl{
2781 Doc: doc,
2782 TokPos: pos,
2783 Tok: keyword,
2784 Lparen: lparen,
2785 Specs: list,
2786 Rparen: rparen,
2787 }
2788 }
2789
2790 func (p *parser) parseFuncDecl() *ast.FuncDecl {
2791 if p.trace {
2792 defer un(trace(p, "FunctionDecl"))
2793 }
2794
2795 doc := p.leadComment
2796 pos := p.expect(token.FUNC)
2797
2798 var recv *ast.FieldList
2799 if p.tok == token.LPAREN {
2800 _, recv = p.parseParameters(false)
2801 }
2802
2803 ident := p.parseIdent()
2804
2805 tparams, params := p.parseParameters(true)
2806 if recv != nil && tparams != nil {
2807
2808
2809 p.error(tparams.Opening, "method must have no type parameters")
2810 tparams = nil
2811 }
2812 results := p.parseResult()
2813
2814 var body *ast.BlockStmt
2815 switch p.tok {
2816 case token.LBRACE:
2817 body = p.parseBody()
2818 p.expectSemi()
2819 case token.SEMICOLON:
2820 p.next()
2821 if p.tok == token.LBRACE {
2822
2823 p.error(p.pos, "unexpected semicolon or newline before {")
2824 body = p.parseBody()
2825 p.expectSemi()
2826 }
2827 default:
2828 p.expectSemi()
2829 }
2830
2831 decl := &ast.FuncDecl{
2832 Doc: doc,
2833 Recv: recv,
2834 Name: ident,
2835 Type: &ast.FuncType{
2836 Func: pos,
2837 TypeParams: tparams,
2838 Params: params,
2839 Results: results,
2840 },
2841 Body: body,
2842 }
2843 return decl
2844 }
2845
2846 func (p *parser) parseDecl(sync map[token.Token]bool) ast.Decl {
2847 if p.trace {
2848 defer un(trace(p, "Declaration"))
2849 }
2850
2851 var f parseSpecFunction
2852 switch p.tok {
2853 case token.CONST, token.VAR:
2854 f = p.parseValueSpec
2855
2856 case token.TYPE:
2857 f = p.parseTypeSpec
2858
2859 case token.FUNC:
2860 return p.parseFuncDecl()
2861
2862 default:
2863 pos := p.pos
2864 p.errorExpected(pos, "declaration")
2865 p.advance(sync)
2866 return &ast.BadDecl{From: pos, To: p.pos}
2867 }
2868
2869 return p.parseGenDecl(p.tok, f)
2870 }
2871
2872
2873
2874
2875 func (p *parser) parseFile() *ast.File {
2876 if p.trace {
2877 defer un(trace(p, "File"))
2878 }
2879
2880
2881
2882 if p.errors.Len() != 0 {
2883 return nil
2884 }
2885
2886
2887 doc := p.leadComment
2888 pos := p.expect(token.PACKAGE)
2889
2890
2891 ident := p.parseIdent()
2892 if ident.Name == "_" && p.mode&DeclarationErrors != 0 {
2893 p.error(p.pos, "invalid package name _")
2894 }
2895 p.expectSemi()
2896
2897
2898
2899 if p.errors.Len() != 0 {
2900 return nil
2901 }
2902
2903 var decls []ast.Decl
2904 if p.mode&PackageClauseOnly == 0 {
2905
2906 for p.tok == token.IMPORT {
2907 decls = append(decls, p.parseGenDecl(token.IMPORT, p.parseImportSpec))
2908 }
2909
2910 if p.mode&ImportsOnly == 0 {
2911
2912 for p.tok != token.EOF {
2913 decls = append(decls, p.parseDecl(declStart))
2914 }
2915 }
2916 }
2917
2918 f := &ast.File{
2919 Doc: doc,
2920 Package: pos,
2921 Name: ident,
2922 Decls: decls,
2923 Imports: p.imports,
2924 Comments: p.comments,
2925 }
2926 var declErr func(token.Pos, string)
2927 if p.mode&DeclarationErrors != 0 {
2928 declErr = p.error
2929 }
2930 if p.mode&SkipObjectResolution == 0 {
2931 resolveFile(f, p.file, declErr)
2932 }
2933
2934 return f
2935 }
2936
View as plain text