scanner.mx raw
1 package main
2
3
4 const runeError = '�'
5 const runeSelf = 0x80
6 const maxRune = '\U0010FFFF'
7 const utfMax = 4
8
9 func decodeRune(p []byte) (r rune, size int32) {
10 n := int32(len(p))
11 if n < 1 { return runeError, 0 }
12 p0 := p[0]
13 if p0 < runeSelf { return rune(p0), 1 }
14 if p0 < 0xC0 { return runeError, 1 }
15 if n < 2 { return runeError, 1 }
16 p1 := p[1]
17 if p1 < 0x80 || p1 >= 0xC0 { return runeError, 1 }
18 if p0 < 0xE0 {
19 r = rune(p0&0x1F)<<6 | rune(p1&0x3F)
20 if r < 0x80 { return runeError, 1 }
21 return r, 2
22 }
23 if n < 3 { return runeError, 1 }
24 p2 := p[2]
25 if p2 < 0x80 || p2 >= 0xC0 { return runeError, 1 }
26 if p0 < 0xF0 {
27 r = rune(p0&0x0F)<<12 | rune(p1&0x3F)<<6 | rune(p2&0x3F)
28 if r < 0x800 { return runeError, 1 }
29 if r >= 0xD800 && r <= 0xDFFF { return runeError, 1 }
30 return r, 3
31 }
32 if n < 4 { return runeError, 1 }
33 p3 := p[3]
34 if p3 < 0x80 || p3 >= 0xC0 { return runeError, 1 }
35 if p0 < 0xF8 {
36 r = rune(p0&0x07)<<18 | rune(p1&0x3F)<<12 | rune(p2&0x3F)<<6 | rune(p3&0x3F)
37 if r < 0x10000 || r > maxRune { return runeError, 1 }
38 return r, 4
39 }
40 return runeError, 1
41 }
42
43 func fullRune(p []byte) (ok bool) {
44 n := int32(len(p))
45 if n == 0 { return false }
46 if p[0] < runeSelf { return true }
47 if p[0] < 0xC0 { return true }
48 if n < 2 { return false }
49 if p[0] < 0xE0 { return true }
50 if n < 3 { return false }
51 if p[0] < 0xF0 { return true }
52 return n >= 4
53 }
54
55 func uIsLetter(ch rune) (ok bool) {
56 if ch < runeSelf { return 'a' <= (ch|0x20) && (ch|0x20) <= 'z' || ch == '_' }
57 return true
58 }
59
60 func uIsDigit(ch rune) (ok bool) {
61 if ch < runeSelf { return '0' <= ch && ch <= '9' }
62 return false
63 }
64
65
66 type srcDone struct{}
67
68 func (srcDone) Error() (s string) { return "EOF" }
69
70 type Source struct {
71 in interface{}
72 errh func(line, col uint32, msg string)
73
74 buf []byte
75 ioerr error
76 noFill bool
77 b, r, e int32
78 line, col uint32
79 ch rune
80 chw int32
81 }
82
83 const sentinel = 0x80
84
85 func (s *Source) release() {
86 s.buf = nil
87 s.in = nil
88 s.errh = nil
89 s.ioerr = nil
90 }
91
92 func (s *Source) init(in interface{}, errh func(line, col uint32, msg string)) {
93 s.in = in
94 s.errh = errh
95
96 if s.buf == nil {
97 s.buf = []byte{:nextSize(0)}
98 }
99 s.buf[0] = sentinel
100 s.ioerr = nil
101 s.noFill = false
102 s.b, s.r, s.e = -1, 0, 0
103 s.line, s.col = 0, 0
104 s.ch = ' '
105 s.chw = 0
106 }
107
108 func (s *Source) initBytes(src []byte, errh func(line, col uint32, msg string)) {
109 s.in = nil
110 s.errh = errh
111 s.noFill = true
112 s.buf = []byte{:len(src) + 1}
113 copy(s.buf, src)
114 s.buf[len(src)] = sentinel
115 s.ioerr = nil
116 s.b, s.r, s.e = -1, 0, int32(len(src))
117 s.line, s.col = 0, 0
118 s.ch = ' '
119 s.chw = 0
120 }
121
122 func (s *Source) pos() (line, col uint32) {
123 return Linebase + s.line, Colbase + s.col
124 }
125
126 func (s *Source) Debugpos() (line, col uint32) {
127 return Linebase + s.line, Colbase + s.col
128 }
129
130 func (s *Source) error(msg string) {
131 line, col := s.pos()
132 s.errh(line, col, msg)
133 }
134
135 func (s *Source) start() { s.b = s.r - s.chw }
136 func (s *Source) stop() { s.b = -1 }
137 func (s *Source) segment() (buf []byte) { return s.buf[s.b : s.r-s.chw] }
138
139 // segmentCopy returns a copy of the current segment that survives buffer
140 // reallocation in fill(). In Moxie string=[]byte so string(segment()) does
141 // NOT copy - the returned slice still aliases s.buf.
142 func (s *Source) segmentCopy() (buf []byte) {
143 b := s.buf[s.b : s.r-s.chw]
144 c := []byte{:len(b)}
145 copy(c, b)
146 return c
147 }
148
149 func (s *Source) rewind() {
150 if s.b < 0 {
151 panic("no active segment")
152 }
153 s.col -= uint32(s.r - s.b)
154 s.r = s.b
155 s.nextch()
156 }
157
158 func (s *Source) nextch() {
159 redo:
160 s.col += uint32(s.chw)
161 if s.ch == '\n' {
162 s.line++
163 s.col = 0
164 }
165
166 if s.ch = rune(s.buf[s.r]); s.ch < sentinel {
167 s.r++
168 s.chw = 1
169 if s.ch == 0 {
170 s.error("invalid NUL character")
171 goto redo
172 }
173 return
174 }
175
176 for s.e-s.r < utfMax && !fullRune(s.buf[s.r:s.e]) && s.ioerr == nil {
177 s.fill()
178 }
179
180 if s.r == s.e {
181 if (s.ioerr == nil || s.ioerr == nil) && !s.noFill {
182 s.error("I/O error: " | s.ioerr.Error())
183 s.ioerr = nil
184 }
185 s.ch = -1
186 s.chw = 0
187 return
188 }
189
190 var w int32
191 s.ch, w = decodeRune(s.buf[s.r:s.e])
192 s.chw = int32(w)
193 s.r += s.chw
194
195 if s.ch == runeError && s.chw == 1 {
196 s.error("invalid UTF-8 encoding")
197 goto redo
198 }
199
200 const BOM = 0xfeff
201 if s.ch == BOM {
202 if s.line > 0 || s.col > 0 {
203 s.error("invalid BOM in the middle of the file")
204 }
205 goto redo
206 }
207 }
208
209 func (s *Source) fill() {
210 if s.noFill {
211 s.ioerr = srcDone{}
212 return
213 }
214 b := s.r
215 if s.b >= 0 {
216 b = s.b
217 s.b = 0
218 }
219 content := s.buf[b:s.e]
220
221 if len(content)*2 > len(s.buf) {
222 s.buf = []byte{:nextSize(int32(len(s.buf)))}
223 copy(s.buf, content)
224 } else if b > 0 {
225 copy(s.buf, content)
226 }
227 s.r -= b
228 s.e -= b
229
230 for i := 0; i < 10; i++ {
231 var n int32
232 var nn int32
233 panic("stream read not supported")
234 n = int32(nn)
235 if n < 0 {
236 panic("negative read")
237 }
238 if n > 0 || s.ioerr != nil {
239 s.e += n
240 s.buf[s.e] = sentinel
241 return
242 }
243 }
244
245 s.buf[s.e] = sentinel
246
247 }
248
249 func nextSize(size int32) (n int32) {
250 const min = 4 << 10
251 const max = 1 << 20
252 if size < min {
253 return min
254 }
255 if size <= max {
256 return size << 1
257 }
258 return size + max
259 }
260
261
262 const (
263 comments uint32 = 1 << iota
264 directives
265 )
266
267
268
269
270 type Scanner struct {
271 Source
272 mode uint32
273 nlsemi bool
274
275 Line, Col uint32
276 Blank bool
277 Tok Token
278 Lit string
279 Bad bool
280 Kind LitKind
281 Op Operator
282 Prec int32
283
284 keywordMap [1 << 6]Token
285 keywordsReady bool
286 }
287
288
289 func (s *Scanner) InitBytes(src []byte, errh func(line, col uint32, msg string), mode uint32) {
290 s.Source.initBytes(src, errh)
291 s.mode = 0
292 s.nlsemi = false
293
294 s.initKeywords()
295 }
296
297 func (s *Scanner) Errorf(msg string) {
298 s.error(msg)
299 }
300
301 func (s *Scanner) ErrorAtf(offset int32, msg string) {
302 s.errh(s.line, s.col+uint32(offset), msg)
303 }
304
305 func (s *Scanner) SetLit(kind LitKind, ok bool) {
306 s.nlsemi = true
307 s.Tok = Literal
308 s.Lit = string(s.segmentCopy())
309 s.Bad = !ok
310 s.Kind = kind
311 }
312
313 func (s *Scanner) Next() {
314 nlsemi := s.nlsemi
315 s.nlsemi = false
316
317 redo:
318 s.stop()
319 startLine, startCol := s.pos()
320 for s.ch == ' ' || s.ch == '\t' || s.ch == '\n' && !nlsemi || s.ch == '\r' {
321 s.nextch()
322 }
323
324 s.Line, s.Col = s.pos()
325 s.Blank = s.line > startLine || startCol == Colbase
326 s.start()
327 if IsLetter(s.ch) || s.ch >= runeSelf && s.AtIdentChar(true) {
328 s.nextch()
329 s.Ident()
330 return
331 }
332
333 switch s.ch {
334 case -1:
335 if nlsemi {
336 s.Lit = "EOF"
337 s.Tok = Semi
338 break
339 }
340 s.Tok = EOF
341
342 case '\n':
343 s.nextch()
344 s.Lit = "newline"
345 s.Tok = Semi
346
347 case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
348 s.Number(false)
349
350 case '"':
351 s.stdString()
352
353 case '`':
354 s.rawString()
355
356 case '\'':
357 s.rune()
358
359 case '(':
360 s.nextch()
361 s.Tok = Lparen
362
363 case '[':
364 s.nextch()
365 s.Tok = Lbrack
366
367 case '{':
368 s.nextch()
369 s.Tok = Lbrace
370
371 case ',':
372 s.nextch()
373 s.Tok = Comma
374
375 case ';':
376 s.nextch()
377 s.Lit = "semicolon"
378 s.Tok = Semi
379
380 case ')':
381 s.nextch()
382 s.nlsemi = true
383 s.Tok = Rparen
384
385 case ']':
386 s.nextch()
387 s.nlsemi = true
388 s.Tok = Rbrack
389
390 case '}':
391 s.nextch()
392 s.nlsemi = true
393 s.Tok = Rbrace
394
395 case ':':
396 s.nextch()
397 if s.ch == '=' {
398 s.nextch()
399 s.Tok = Define
400 break
401 }
402 s.Tok = Colon
403
404 case '.':
405 s.nextch()
406 if IsDecimal(s.ch) {
407 s.Number(true)
408 break
409 }
410 if s.ch == '.' {
411 s.nextch()
412 if s.ch == '.' {
413 s.nextch()
414 s.Tok = DotDotDot
415 break
416 }
417 s.rewind()
418 s.nextch()
419 }
420 s.Tok = Dot
421
422 case '+':
423 s.nextch()
424 s.Op, s.Prec = Add, PrecAdd
425 if s.ch != '+' {
426 s.assignOp()
427 return
428 }
429 s.nextch()
430 s.nlsemi = true
431 s.Tok = IncOp
432
433 case '-':
434 s.nextch()
435 s.Op, s.Prec = Sub, PrecAdd
436 if s.ch != '-' {
437 s.assignOp()
438 return
439 }
440 s.nextch()
441 s.nlsemi = true
442 s.Tok = IncOp
443
444 case '*':
445 s.nextch()
446 s.Op, s.Prec = Mul, PrecMul
447 if s.ch == '=' {
448 s.nextch()
449 s.Tok = AssignOp
450 break
451 }
452 s.Tok = Star
453
454 case '/':
455 s.nextch()
456 if s.ch == '/' {
457 s.nextch()
458 s.lineComment()
459 goto redo
460 }
461 if s.ch == '*' {
462 s.nextch()
463 s.fullComment()
464 if line, _ := s.pos(); line > s.Line && nlsemi {
465 s.Lit = "newline"
466 s.Tok = Semi
467 break
468 }
469 goto redo
470 }
471 s.Op, s.Prec = Div, PrecMul
472 if s.ch == '=' {
473 s.nextch()
474 s.Tok = AssignOp
475 } else {
476 s.Tok = OperatorType
477 }
478
479 case '%':
480 s.nextch()
481 s.Op, s.Prec = Rem, PrecMul
482 if s.ch == '=' {
483 s.nextch()
484 s.Tok = AssignOp
485 } else {
486 s.Tok = OperatorType
487 }
488
489 case '&':
490 s.nextch()
491 if s.ch == '&' {
492 s.nextch()
493 s.Op, s.Prec = AndAnd, PrecAndAnd
494 s.Tok = OperatorType
495 break
496 }
497 s.Op, s.Prec = And, PrecMul
498 if s.ch == '^' {
499 s.nextch()
500 s.Op = AndNot
501 }
502 if s.ch == '=' {
503 s.nextch()
504 s.Tok = AssignOp
505 } else {
506 s.Tok = OperatorType
507 }
508
509 case '|':
510 s.nextch()
511 if s.ch == '|' {
512 s.nextch()
513 s.Op, s.Prec = OrOr, PrecOrOr
514 s.Tok = OperatorType
515 break
516 }
517 s.Op, s.Prec = Or, PrecAdd
518 if s.ch == '=' {
519 s.nextch()
520 s.Tok = AssignOp
521 } else {
522 s.Tok = OperatorType
523 }
524
525 case '^':
526 s.nextch()
527 s.Op, s.Prec = Xor, PrecAdd
528 if s.ch == '=' {
529 s.nextch()
530 s.Tok = AssignOp
531 } else {
532 s.Tok = OperatorType
533 }
534
535 case '<':
536 s.nextch()
537 if s.ch == '=' {
538 s.nextch()
539 s.Op, s.Prec = Leq, PrecCmp
540 s.Tok = OperatorType
541 break
542 }
543 if s.ch == '<' {
544 s.nextch()
545 s.Op, s.Prec = Shl, PrecMul
546 s.assignOp()
547 return
548 }
549 if s.ch == '-' {
550 s.nextch()
551 s.Tok = Arrow
552 break
553 }
554 s.Op, s.Prec = Lss, PrecCmp
555 s.Tok = OperatorType
556
557 case '>':
558 s.nextch()
559 if s.ch == '=' {
560 s.nextch()
561 s.Op, s.Prec = Geq, PrecCmp
562 s.Tok = OperatorType
563 break
564 }
565 if s.ch == '>' {
566 s.nextch()
567 s.Op, s.Prec = Shr, PrecMul
568 s.assignOp()
569 return
570 }
571 s.Op, s.Prec = Gtr, PrecCmp
572 s.Tok = OperatorType
573
574 case '=':
575 s.nextch()
576 if s.ch == '=' {
577 s.nextch()
578 s.Op, s.Prec = Eql, PrecCmp
579 s.Tok = OperatorType
580 break
581 }
582 s.Tok = Assign
583
584 case '!':
585 s.nextch()
586 if s.ch == '=' {
587 s.nextch()
588 s.Op, s.Prec = Neq, PrecCmp
589 s.Tok = OperatorType
590 break
591 }
592 s.Op, s.Prec = Not, 0
593 s.Tok = OperatorType
594
595 case '~':
596 s.nextch()
597 s.Op, s.Prec = Tilde, 0
598 s.Tok = OperatorType
599
600 default:
601 s.Errorf("invalid character " | fmtRuneU(s.ch))
602 s.nextch()
603 goto redo
604 }
605
606 return
607 }
608
609 func (s *Scanner) assignOp() {
610 if s.ch == '=' {
611 s.nextch()
612 s.Tok = AssignOp
613 return
614 }
615 s.Tok = OperatorType
616 }
617
618 func (s *Scanner) Ident() {
619 for IsLetter(s.ch) || IsDecimal(s.ch) {
620 s.nextch()
621 }
622
623 if s.ch >= runeSelf {
624 for s.AtIdentChar(false) {
625 s.nextch()
626 }
627 }
628
629 lit := s.segment()
630 if len(lit) >= 2 {
631 h := (uint32(lit[0])<<4 ^ uint32(lit[1]) + uint32(len(lit))) & 63
632 if tok := s.keywordMap[h]; tok != 0 && tokStrFast(tok) == string(lit) {
633 s.nlsemi = Contains(1<<Break|1<<Continue|1<<Fallthrough|1<<Return, tok)
634 s.Tok = tok
635 return
636 }
637 }
638
639 s.nlsemi = true
640 c := []byte{:len(lit)}
641 copy(c, lit)
642 s.Lit = string(c)
643 s.Tok = NameType
644 }
645
646 func tokStrFast(tok Token) (s string) {
647 idx := [48]uint8{0, 3, 7, 14, 16, 19, 23, 24, 26, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 42, 47, 51, 55, 60, 68, 75, 80, 84, 95, 98, 102, 104, 108, 110, 116, 125, 128, 135, 140, 146, 152, 158, 164, 168, 171, 171}
648 return token_name[idx[tok-1]:idx[tok]]
649 }
650
651 func (s *Scanner) AtIdentChar(first bool) (ok bool) {
652 switch {
653 case uIsLetter(s.ch) || s.ch == '_':
654 case uIsDigit(s.ch):
655 if first {
656 s.Errorf("identifier cannot begin with digit " | fmtRuneU(s.ch))
657 }
658 case s.ch >= runeSelf:
659 s.Errorf("invalid character " | fmtRuneU(s.ch) | " in identifier")
660 default:
661 return false
662 }
663 return true
664 }
665
666 func (s *Scanner) initKeywords() {
667 if s.keywordsReady {
668 return
669 }
670 s.keywordsReady = true
671 for tok := Break; tok <= Var; tok++ {
672 b := []byte(tok.String())
673 h := (uint32(b[0])<<4 ^ uint32(b[1]) + uint32(len(b))) & 63
674 if s.keywordMap[h] != 0 {
675 panic("imperfect hash")
676 }
677 s.keywordMap[h] = tok
678 }
679 }
680
681 func Lower(ch rune) (r rune) { return ('a' - 'A') | ch }
682 func IsLetter(ch rune) (ok bool) { return 'a' <= Lower(ch) && Lower(ch) <= 'z' || ch == '_' }
683 func IsDecimal(ch rune) (ok bool) { return '0' <= ch && ch <= '9' }
684 func IsHex(ch rune) (ok bool) { return '0' <= ch && ch <= '9' || 'a' <= Lower(ch) && Lower(ch) <= 'f' }
685
686 func (s *Scanner) Digits(base int32, invalid *int32) (digsep int32) {
687 if base <= 10 {
688 max := rune('0' + base)
689 for IsDecimal(s.ch) || s.ch == '_' {
690 ds := int32(1)
691 if s.ch == '_' {
692 ds = 2
693 } else if s.ch >= max && *invalid < 0 {
694 _, col := s.pos()
695 *invalid = int32(col - s.col)
696 }
697 digsep |= ds
698 s.nextch()
699 }
700 } else {
701 for IsHex(s.ch) || s.ch == '_' {
702 ds := int32(1)
703 if s.ch == '_' {
704 ds = 2
705 }
706 digsep |= ds
707 s.nextch()
708 }
709 }
710 return
711 }
712
713 func (s *Scanner) Number(seenPoint bool) {
714 ok := true
715 kind := IntLit
716 base := int32(10)
717 prefix := rune(0)
718 digsep := int32(0)
719 invalid := int32(-1)
720
721 if !seenPoint {
722 if s.ch == '0' {
723 s.nextch()
724 switch Lower(s.ch) {
725 case 'x':
726 s.nextch()
727 base, prefix = 16, 'x'
728 case 'o':
729 s.nextch()
730 base, prefix = 8, 'o'
731 case 'b':
732 s.nextch()
733 base, prefix = 2, 'b'
734 default:
735 base, prefix = 8, '0'
736 digsep = 1
737 }
738 }
739 digsep |= s.Digits(base, &invalid)
740 if s.ch == '.' {
741 if prefix == 'o' || prefix == 'b' {
742 s.Errorf("invalid radix point in " | baseName(base) | " literal")
743 ok = false
744 }
745 s.nextch()
746 seenPoint = true
747 }
748 }
749
750 if seenPoint {
751 kind = FloatLit
752 digsep |= s.Digits(base, &invalid)
753 }
754
755 if digsep&1 == 0 && ok {
756 s.Errorf(baseName(base) | " literal has no digits")
757 ok = false
758 }
759
760 if e := Lower(s.ch); e == 'e' || e == 'p' {
761 if ok {
762 switch {
763 case e == 'e' && prefix != 0 && prefix != '0':
764 s.Errorf(fmtQuoteRune(s.ch) | " exponent requires decimal mantissa")
765 ok = false
766 case e == 'p' && prefix != 'x':
767 s.Errorf(fmtQuoteRune(s.ch) | " exponent requires hexadecimal mantissa")
768 ok = false
769 }
770 }
771 s.nextch()
772 kind = FloatLit
773 if s.ch == '+' || s.ch == '-' {
774 s.nextch()
775 }
776 digsep = s.Digits(10, nil) | digsep&2
777 if digsep&1 == 0 && ok {
778 s.Errorf("exponent has no digits")
779 ok = false
780 }
781 } else if prefix == 'x' && kind == FloatLit && ok {
782 s.Errorf("hexadecimal mantissa requires a 'p' exponent")
783 ok = false
784 }
785
786 if s.ch == 'i' {
787 kind = ImagLit
788 s.nextch()
789 }
790
791 s.SetLit(kind, ok)
792
793 if kind == IntLit && invalid >= 0 && ok {
794 s.ErrorAtf(invalid, "invalid digit " | fmtQuoteRune(rune(s.Lit[invalid])) | " in " | baseName(base) | " literal")
795 ok = false
796 }
797
798 if digsep&2 != 0 && ok {
799 if i := invalidSep(s.Lit); i >= 0 {
800 s.ErrorAtf(i, "'_' must separate successive digits")
801 ok = false
802 }
803 }
804
805 s.Bad = !ok
806 }
807
808 func baseName(base int32) (s string) {
809 switch base {
810 case 2:
811 return "binary"
812 case 8:
813 return "octal"
814 case 10:
815 return "decimal"
816 case 16:
817 return "hexadecimal"
818 }
819 panic("invalid base")
820 }
821
822 func invalidSep(x string) (n int32) {
823 x1 := ' '
824 d := '.'
825 i := int32(0)
826
827 if len(x) >= 2 && x[0] == '0' {
828 x1 = Lower(rune(x[1]))
829 if x1 == 'x' || x1 == 'o' || x1 == 'b' {
830 d = '0'
831 i = 2
832 }
833 }
834
835 for ; i < int32(len(x)); i++ {
836 p := d
837 d = rune(x[i])
838 switch {
839 case d == '_':
840 if p != '0' {
841 return i
842 }
843 case IsDecimal(d) || x1 == 'x' && IsHex(d):
844 d = '0'
845 default:
846 if p == '_' {
847 return i - 1
848 }
849 d = '.'
850 }
851 }
852 if d == '_' {
853 return int32(len(x)) - 1
854 }
855
856 return -1
857 }
858
859 func (s *Scanner) rune() {
860 ok := true
861 s.nextch()
862
863 n := 0
864 for ; ; n++ {
865 if s.ch == '\'' {
866 if ok {
867 if n == 0 {
868 s.Errorf("empty rune literal or unescaped '")
869 ok = false
870 } else if n != 1 {
871 s.ErrorAtf(0, "more than one character in rune literal")
872 ok = false
873 }
874 }
875 s.nextch()
876 break
877 }
878 if s.ch == '\\' {
879 s.nextch()
880 if !s.escape('\'') {
881 ok = false
882 }
883 continue
884 }
885 if s.ch == '\n' {
886 if ok {
887 s.Errorf("newline in rune literal")
888 ok = false
889 }
890 break
891 }
892 if s.ch < 0 {
893 if ok {
894 s.ErrorAtf(0, "rune literal not terminated")
895 ok = false
896 }
897 break
898 }
899 s.nextch()
900 }
901
902 s.SetLit(RuneLit, ok)
903 }
904
905 func (s *Scanner) stdString() {
906 ok := true
907 s.nextch()
908
909 for {
910 if s.ch == '"' {
911 s.nextch()
912 break
913 }
914 if s.ch == '\\' {
915 s.nextch()
916 if !s.escape('"') {
917 ok = false
918 }
919 continue
920 }
921 if s.ch == '\n' {
922 s.Errorf("newline in string")
923 ok = false
924 break
925 }
926 if s.ch < 0 {
927 s.ErrorAtf(0, "string not terminated")
928 ok = false
929 break
930 }
931 s.nextch()
932 }
933
934 s.SetLit(StringLit, ok)
935 }
936
937 func (s *Scanner) rawString() {
938 ok := true
939 s.nextch()
940
941 for {
942 if s.ch == '`' {
943 s.nextch()
944 break
945 }
946 if s.ch < 0 {
947 s.ErrorAtf(0, "string not terminated")
948 ok = false
949 break
950 }
951 s.nextch()
952 }
953
954 s.SetLit(StringLit, ok)
955 }
956
957 func (s *Scanner) comment(text string) {
958 s.ErrorAtf(0, text)
959 }
960
961 func (s *Scanner) skipLine() {
962 for s.ch >= 0 && s.ch != '\n' {
963 s.nextch()
964 }
965 }
966
967 func (s *Scanner) lineComment() {
968 if s.mode&comments != 0 {
969 s.skipLine()
970 s.comment(string(s.segment()))
971 return
972 }
973
974 if s.mode&directives == 0 || (s.ch != 'g' && s.ch != 'l') {
975 s.stop()
976 s.skipLine()
977 return
978 }
979
980 prefix := "go:"
981 if s.ch == 'l' {
982 prefix = "line "
983 }
984
985 for _, r := range prefix {
986 if s.ch != rune(r) {
987 s.stop()
988 s.skipLine()
989 return
990 }
991 s.nextch()
992 }
993 s.skipLine()
994 s.comment(string(s.segment()))
995 }
996
997 func (s *Scanner) skipComment() (ok bool) {
998 for s.ch >= 0 {
999 for s.ch == '*' {
1000 s.nextch()
1001 if s.ch == '/' {
1002 s.nextch()
1003 return true
1004 }
1005 }
1006 s.nextch()
1007 }
1008 s.ErrorAtf(0, "comment not terminated")
1009 return false
1010 }
1011
1012 func (s *Scanner) fullComment() {
1013 if s.mode&comments != 0 {
1014 if s.skipComment() {
1015 s.comment(string(s.segment()))
1016 }
1017 return
1018 }
1019
1020 if s.mode&directives == 0 || s.ch != 'l' {
1021 s.stop()
1022 s.skipComment()
1023 return
1024 }
1025
1026 const prefix = "line "
1027
1028 for _, r := range prefix {
1029 if s.ch != rune(r) {
1030 s.stop()
1031 s.skipComment()
1032 return
1033 }
1034 s.nextch()
1035 }
1036 if s.skipComment() {
1037 s.comment(string(s.segment()))
1038 }
1039 }
1040
1041 func (s *Scanner) escape(quote rune) (ok bool) {
1042 var n int32
1043 var base, max uint32
1044
1045 switch s.ch {
1046 case quote, 'a', 'b', 'f', 'n', 'r', 't', 'v', '\\':
1047 s.nextch()
1048 return true
1049 case '0', '1', '2', '3', '4', '5', '6', '7':
1050 n, base, max = 3, 8, 255
1051 case 'x':
1052 s.nextch()
1053 n, base, max = 2, 16, 255
1054 case 'u':
1055 s.nextch()
1056 n, base, max = 4, 16, maxRune
1057 case 'U':
1058 s.nextch()
1059 n, base, max = 8, 16, maxRune
1060 default:
1061 if s.ch < 0 {
1062 return true
1063 }
1064 s.Errorf("unknown escape")
1065 return false
1066 }
1067
1068 var x uint32
1069 for i := n; i > 0; i-- {
1070 if s.ch < 0 {
1071 return true
1072 }
1073 d := base
1074 if IsDecimal(s.ch) {
1075 d = uint32(s.ch) - '0'
1076 } else if 'a' <= Lower(s.ch) && Lower(s.ch) <= 'f' {
1077 d = uint32(Lower(s.ch)) - 'a' + 10
1078 }
1079 if d >= base {
1080 s.Errorf("invalid character " | fmtQuoteRune(s.ch) | " in " | baseName(int32(base)) | " escape")
1081 return false
1082 }
1083 x = x*base + d
1084 s.nextch()
1085 }
1086
1087 if x > max && base == 8 {
1088 s.Errorf("octal escape value " | ItoaU32(x) | " > 255")
1089 return false
1090 }
1091
1092 if x > max || 0xD800 <= x && x < 0xE000 {
1093 s.Errorf("escape is invalid Unicode code point " | fmtRuneU(rune(x)))
1094 return false
1095 }
1096
1097 return true
1098 }
1099
1100
1101
1102 // fmtRuneU formats a rune as "U+XXXX 'c'" (like %#U).
1103 func fmtRuneU(r rune) (s string) {
1104 hex := fmtHex32(uint32(r))
1105 s = "U+" | hex
1106 if r >= 0x20 && r < 0x7f {
1107 s = s | " '" | string([]byte{byte(r)}) | "'"
1108 }
1109 return s
1110 }
1111
1112 // fmtQuoteRune formats a rune as 'c' (like %q for rune).
1113 func fmtQuoteRune(r rune) (s string) {
1114 if r >= 0x20 && r < 0x7f {
1115 return "'" | string([]byte{byte(r)}) | "'"
1116 }
1117 return "U+" | fmtHex32(uint32(r))
1118 }
1119
1120 // fmtHex32 formats a uint32 as uppercase hex with at least 4 digits.
1121 func fmtHex32(v uint32) (s string) {
1122 const digits = "0123456789ABCDEF"
1123 buf := []byte{:0:8}
1124 if v == 0 {
1125 return "0000"
1126 }
1127 for v > 0 {
1128 buf = append(buf, digits[v&0xf])
1129 v >>= 4
1130 }
1131 for len(buf) < 4 {
1132 buf = append(buf, '0')
1133 }
1134 for i, j := int32(0), len(buf)-1; i < j; i, j = i+1, j-1 {
1135 buf[i], buf[j] = buf[j], buf[i]
1136 }
1137 return string(buf)
1138 }
1139
1140
1141 const PosMax = 1 << 30
1142 const Linebase = 1
1143 const Colbase = 1
1144
1145 type Pos struct {
1146 base *PosBase
1147 line, col uint32
1148 }
1149
1150 func MakePos(base *PosBase, line, col uint32) (p Pos) { return Pos{base, sat32(line), sat32(col)} }
1151
1152 func (pos Pos) Pos() (p Pos) { return pos }
1153 func (pos Pos) IsKnown() (ok bool) { return pos.line > 0 }
1154 func (pos Pos) Base() (p *PosBase) { return pos.base }
1155 func (pos Pos) Line() (n uint32) { return pos.line }
1156 func (pos Pos) Col() (n uint32) { return pos.col }
1157
1158 func (pos Pos) FileBase() (p *PosBase) {
1159 b := pos.base
1160 for b != nil && b != b.pos.base {
1161 b = b.pos.base
1162 }
1163 return b
1164 }
1165
1166 func (pos Pos) RelFilename() (s string) { return pos.base.Filename() }
1167
1168 func (pos Pos) RelLine() (n uint32) {
1169 b := pos.base
1170 if b.Line() == 0 {
1171 return 0
1172 }
1173 return b.Line() + (pos.Line() - b.Pos().Line())
1174 }
1175
1176 func (pos Pos) RelCol() (n uint32) {
1177 b := pos.base
1178 if b.Col() == 0 {
1179 return 0
1180 }
1181 if pos.Line() == b.Pos().Line() {
1182 return b.Col() + (pos.Col() - b.Pos().Col())
1183 }
1184 return pos.Col()
1185 }
1186
1187 func (p Pos) Cmp(q Pos) (n int32) {
1188 pname := p.RelFilename()
1189 qname := q.RelFilename()
1190 switch {
1191 case pname < qname:
1192 return -1
1193 case pname > qname:
1194 return +1
1195 }
1196
1197 pline := p.Line()
1198 qline := q.Line()
1199 switch {
1200 case pline < qline:
1201 return -1
1202 case pline > qline:
1203 return +1
1204 }
1205
1206 pcol := p.Col()
1207 qcol := q.Col()
1208 switch {
1209 case pcol < qcol:
1210 return -1
1211 case pcol > qcol:
1212 return +1
1213 }
1214
1215 return 0
1216 }
1217
1218 func (pos Pos) String() (s string) {
1219 rel := position_{pos.RelFilename(), pos.RelLine(), pos.RelCol()}
1220 abs := position_{pos.Base().Pos().RelFilename(), pos.Line(), pos.Col()}
1221 s = rel.String()
1222 if string(rel.filename) != string(abs.filename) || rel.line != abs.line || rel.col != abs.col {
1223 s = s | "[" | abs.String() | "]"
1224 }
1225 return s
1226 }
1227
1228 type position_ struct {
1229 filename string
1230 line, col uint32
1231 }
1232
1233 func (p position_) String() (s string) {
1234 if p.line == 0 {
1235 if p.filename == "" {
1236 return "<unknown position>"
1237 }
1238 return p.filename
1239 }
1240 if p.col == 0 {
1241 return p.filename | ":" | ItoaU32(p.line)
1242 }
1243 return p.filename | ":" | ItoaU32(p.line) | ":" | ItoaU32(p.col)
1244 }
1245
1246 func ItoaU32(n uint32) (s string) {
1247 if n == 0 {
1248 return "0"
1249 }
1250 buf := []byte{:0:10}
1251 for n > 0 {
1252 buf = append(buf, byte('0'+n%10))
1253 n /= 10
1254 }
1255 for i, j := int32(0), len(buf)-1; i < j; i, j = i+1, j-1 {
1256 buf[i], buf[j] = buf[j], buf[i]
1257 }
1258 return string(buf)
1259 }
1260
1261 type PosBase struct {
1262 pos Pos
1263 filename string
1264 line, col uint32
1265 trimmed bool
1266 }
1267
1268 func NewFileBase(filename string) (p *PosBase) {
1269 return NewTrimmedFileBase(filename, false)
1270 }
1271
1272 func NewTrimmedFileBase(filename string, trimmed bool) (p *PosBase) {
1273 base := &PosBase{MakePos(nil, Linebase, Colbase), filename, Linebase, Colbase, trimmed}
1274 base.pos.base = base
1275 return base
1276 }
1277
1278 func NewLineBase(pos Pos, filename string, trimmed bool, line, col uint32) (p *PosBase) {
1279 return &PosBase{pos, filename, sat32(line), sat32(col), trimmed}
1280 }
1281
1282 func (base *PosBase) IsFileBase() (ok bool) {
1283 if base == nil {
1284 return false
1285 }
1286 return base.pos.base == base
1287 }
1288
1289 func (base *PosBase) Pos() (_ Pos) {
1290 if base == nil {
1291 return
1292 }
1293 return base.pos
1294 }
1295
1296 func (base *PosBase) Filename() (s string) {
1297 if base == nil {
1298 return ""
1299 }
1300 return base.filename
1301 }
1302
1303 func (base *PosBase) Line() (n uint32) {
1304 if base == nil {
1305 return 0
1306 }
1307 return base.line
1308 }
1309
1310 func (base *PosBase) Col() (n uint32) {
1311 if base == nil {
1312 return 0
1313 }
1314 return base.col
1315 }
1316
1317 func (base *PosBase) Trimmed() (ok bool) {
1318 if base == nil {
1319 return false
1320 }
1321 return base.trimmed
1322 }
1323
1324 func sat32(x uint32) (n uint32) {
1325 if x > PosMax {
1326 return PosMax
1327 }
1328 return uint32(x)
1329 }
1330
1331 func Itoa(n int32) (s string) {
1332 if n == 0 {
1333 return "0"
1334 }
1335 neg := false
1336 if n < 0 {
1337 neg = true
1338 n = -n
1339 }
1340 buf := []byte{:0:12}
1341 for n > 0 {
1342 buf = append(buf, byte('0'+n%10))
1343 n /= 10
1344 }
1345 if neg {
1346 buf = append(buf, '-')
1347 }
1348 for i, j := int32(0), len(buf)-1; i < j; i, j = i+1, j-1 {
1349 buf[i], buf[j] = buf[j], buf[i]
1350 }
1351 return string(buf)
1352 }
1353
1354 func Itoa64(n int64) (s string) {
1355 if n == 0 {
1356 return "0"
1357 }
1358 neg := false
1359 if n < 0 {
1360 neg = true
1361 n = -n
1362 }
1363 buf := []byte{:0:20}
1364 for n > 0 {
1365 buf = append(buf, byte('0'+n%10))
1366 n /= 10
1367 }
1368 if neg {
1369 buf = append(buf, '-')
1370 }
1371 for i, j := int32(0), len(buf)-1; i < j; i, j = i+1, j-1 {
1372 buf[i], buf[j] = buf[j], buf[i]
1373 }
1374 return string(buf)
1375 }
1376
1377 func Contains(tokset uint64, tok Token) (ok bool) {
1378 var bit uint64 = 1
1379 bit = bit << tok
1380 return tokset&bit != 0
1381 }
1382
1383
1384 type Token uint32
1385
1386 const (
1387 _ Token = iota
1388 EOF
1389
1390 NameType
1391 Literal
1392
1393 OperatorType
1394 AssignOp
1395 IncOp
1396 Assign
1397 Define
1398 Arrow
1399 Star
1400
1401 Lparen
1402 Lbrack
1403 Lbrace
1404 Rparen
1405 Rbrack
1406 Rbrace
1407 Comma
1408 Semi
1409 Colon
1410 Dot
1411 DotDotDot
1412
1413 Break
1414 Case
1415 Chan
1416 Const
1417 Continue
1418 Default
1419 Defer
1420 Else
1421 Fallthrough
1422 For
1423 Func
1424 Go
1425 Goto
1426 If
1427 Import
1428 Interface
1429 Map
1430 Package
1431 Range
1432 Return
1433 Select
1434 Struct
1435 Switch
1436 TypeType
1437 Var
1438
1439 tokenCount
1440 )
1441
1442 const _ uint64 = 1 << (tokenCount - 1)
1443
1444 type LitKind uint8
1445
1446 const (
1447 IntLit LitKind = iota
1448 FloatLit
1449 ImagLit
1450 RuneLit
1451 StringLit
1452 )
1453
1454 type Operator uint32
1455
1456 const (
1457 _ Operator = iota
1458
1459 Def
1460 Not
1461 Recv
1462 Tilde
1463
1464 OrOr
1465
1466 AndAnd
1467
1468 Eql
1469 Neq
1470 Lss
1471 Leq
1472 Gtr
1473 Geq
1474
1475 Add
1476 Sub
1477 Or
1478 Xor
1479
1480 Mul
1481 Div
1482 Rem
1483 And
1484 AndNot
1485 Shl
1486 Shr
1487 )
1488
1489 const (
1490 _ = iota
1491 PrecOrOr
1492 PrecAndAnd
1493 PrecCmp
1494 PrecAdd
1495 PrecMul
1496 )
1497
1498 const token_name = "EOFnameliteralopop=opop=:=<-*([{)]},;:....breakcasechanconstcontinuedefaultdeferelsefallthroughforfuncgogotoifimportinterfacemappackagerangereturnselectstructswitchtypevar"
1499
1500 func (i Token) String() (s string) {
1501 idx := [48]uint8{0, 3, 7, 14, 16, 19, 23, 24, 26, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 42, 47, 51, 55, 60, 68, 75, 80, 84, 95, 98, 102, 104, 108, 110, 116, 125, 128, 135, 140, 146, 152, 158, 164, 168, 171, 171}
1502 i -= 1
1503 if i >= Token(len(idx)-1) {
1504 return "token(" | Itoa64(int64(i+1)) | ")"
1505 }
1506 return token_name[idx[i]:idx[i+1]]
1507 }
1508
1509 const _Operator_name = ":!<-~||&&==!=<<=>>=+-|^*/%&&^<<>>"
1510
1511 func (i Operator) String() (s string) {
1512 idx := [24]uint8{0, 1, 2, 4, 5, 7, 9, 11, 13, 14, 16, 17, 19, 20, 21, 22, 23, 24, 25, 26, 27, 29, 31, 33}
1513 i -= 1
1514 if i >= Operator(len(idx)-1) {
1515 return "Operator(" | Itoa64(int64(i+1)) | ")"
1516 }
1517 return _Operator_name[idx[i]:idx[i+1]]
1518 }
1519
1520 // --- AST nodes ---
1521
1522 type File struct {
1523 Pragma Pragma
1524 PkgName *Name
1525 DeclList []Decl
1526 EOF Pos
1527 GoVersion string
1528 node
1529 }
1530
1531 type node struct {
1532 pos Pos
1533 }
1534
1535 func (n *node) Pos() (p Pos) { return n.pos }
1536 func (n *node) SetPos(pos Pos) { n.pos = pos }
1537 func (*node) aNode() {}
1538
1539 type Node interface {
1540 Pos() Pos
1541 aNode()
1542 }
1543
1544 type Decl interface {
1545 Node
1546 aDecl()
1547 }
1548
1549 type decl struct{ node }
1550
1551 func (*decl) aDecl() {}
1552
1553 type (
1554 ImportDecl struct {
1555 Group *Group
1556 Pragma Pragma
1557 LocalPkgName *Name
1558 Path *BasicLit
1559 decl
1560 }
1561
1562 ConstDecl struct {
1563 Group *Group
1564 Pragma Pragma
1565 NameList []*Name
1566 Type Expr
1567 Values Expr
1568 decl
1569 }
1570
1571 TypeDecl struct {
1572 Group *Group
1573 Pragma Pragma
1574 Name *Name
1575 TParamList []*Field
1576 Alias bool
1577 Type Expr
1578 decl
1579 }
1580
1581 VarDecl struct {
1582 Group *Group
1583 Pragma Pragma
1584 NameList []*Name
1585 Type Expr
1586 Values Expr
1587 decl
1588 }
1589
1590 FuncDecl struct {
1591 Pragma Pragma
1592 Recv *Field
1593 Name *Name
1594 TParamList []*Field
1595 Type *FuncType
1596 Body *BlockStmt
1597 decl
1598 }
1599 )
1600
1601 type Group struct {
1602 _ int32
1603 }
1604
1605 func NewName(pos Pos, value string) (n *Name) {
1606 n = &Name{}
1607 n.pos = pos
1608 n.Value = value
1609 return n
1610 }
1611
1612 type Error struct {
1613 Pos Pos
1614 Msg string
1615 }
1616
1617 func (err Error) Error() (s string) {
1618 return err.Msg
1619 }
1620
1621 func String(n Node) (s string) {
1622 return "node"
1623 }
1624
1625 func StartPos(n Node) (p Pos) {
1626 return n.Pos()
1627 }
1628
1629 func dbg(s string) {
1630 if debug {
1631 out(s)
1632 }
1633 }
1634
1635 type (
1636 Expr interface {
1637 Node
1638 typeInfo
1639 aExpr()
1640 }
1641
1642 BadExpr struct {
1643 expr
1644 }
1645
1646 Name struct {
1647 Value string
1648 expr
1649 }
1650
1651 BasicLit struct {
1652 Value string
1653 Kind LitKind
1654 Bad bool
1655 expr
1656 }
1657
1658 CompositeLit struct {
1659 Type Expr
1660 ElemList []Expr
1661 NKeys int32
1662 Rbrace Pos
1663 expr
1664 }
1665
1666 KeyValueExpr struct {
1667 Key, Value Expr
1668 expr
1669 }
1670
1671 FuncLit struct {
1672 Type *FuncType
1673 Body *BlockStmt
1674 expr
1675 }
1676
1677 ParenExpr struct {
1678 X Expr
1679 expr
1680 }
1681
1682 SelectorExpr struct {
1683 X Expr
1684 Sel *Name
1685 expr
1686 }
1687
1688 IndexExpr struct {
1689 X Expr
1690 Index Expr
1691 expr
1692 }
1693
1694 SliceExpr struct {
1695 X Expr
1696 Index [3]Expr
1697 Full bool
1698 expr
1699 }
1700
1701 AssertExpr struct {
1702 X Expr
1703 Type Expr
1704 expr
1705 }
1706
1707 TypeSwitchGuard struct {
1708 Lhs *Name
1709 X Expr
1710 expr
1711 }
1712
1713 Operation struct {
1714 Op Operator
1715 X, Y Expr
1716 expr
1717 }
1718
1719 CallExpr struct {
1720 Fun Expr
1721 ArgList []Expr
1722 HasDots bool
1723 expr
1724 }
1725
1726 ListExpr struct {
1727 ElemList []Expr
1728 expr
1729 }
1730
1731 ArrayType struct {
1732 Len Expr
1733 Elem Expr
1734 expr
1735 }
1736
1737 SliceType struct {
1738 Elem Expr
1739 expr
1740 }
1741
1742 DotsType struct {
1743 Elem Expr
1744 expr
1745 }
1746
1747 StructType struct {
1748 FieldList []*Field
1749 TagList []*BasicLit
1750 expr
1751 }
1752
1753 Field struct {
1754 Name *Name
1755 Type Expr
1756 node
1757 }
1758
1759 InterfaceType struct {
1760 MethodList []*Field
1761 expr
1762 }
1763
1764 FuncType struct {
1765 ParamList []*Field
1766 ResultList []*Field
1767 expr
1768 }
1769
1770 MapType struct {
1771 Key, Value Expr
1772 expr
1773 }
1774
1775 ChanType struct {
1776 Dir ChanDir
1777 Elem Expr
1778 expr
1779 }
1780 )
1781
1782 type Type interface {
1783 Underlying() Type
1784 String() string
1785 }
1786
1787 type typeInfo interface {
1788 SetTypeInfo(TypeAndValue)
1789 GetTypeInfo() TypeAndValue
1790 }
1791
1792 type ConstVal interface{ String() string }
1793
1794 type TypeAndValue struct {
1795 Type Type
1796 Value ConstVal
1797 exprFlags
1798 }
1799
1800 type exprFlags uint16
1801
1802 func (f exprFlags) IsVoid() (ok bool) { return f&1 != 0 }
1803 func (f exprFlags) IsType() (ok bool) { return f&2 != 0 }
1804 func (f exprFlags) IsBuiltin() (ok bool) { return f&4 != 0 }
1805 func (f exprFlags) IsValue() (ok bool) { return f&8 != 0 }
1806 func (f exprFlags) IsNil() (ok bool) { return f&16 != 0 }
1807 func (f exprFlags) Addressable() (ok bool) { return f&32 != 0 }
1808 func (f exprFlags) Assignable() (ok bool) { return f&64 != 0 }
1809 func (f exprFlags) HasOk() (ok bool) { return f&128 != 0 }
1810 func (f exprFlags) IsRuntimeHelper() (ok bool) { return f&256 != 0 }
1811
1812 func (f *exprFlags) SetIsVoid() { *f |= 1 }
1813 func (f *exprFlags) SetIsType() { *f |= 2 }
1814 func (f *exprFlags) SetIsBuiltin() { *f |= 4 }
1815 func (f *exprFlags) SetIsValue() { *f |= 8 }
1816 func (f *exprFlags) SetIsNil() { *f |= 16 }
1817 func (f *exprFlags) SetAddressable() { *f |= 32 }
1818 func (f *exprFlags) SetAssignable() { *f |= 64 }
1819 func (f *exprFlags) SetHasOk() { *f |= 128 }
1820 func (f *exprFlags) SetIsRuntimeHelper() { *f |= 256 }
1821
1822 type typeAndValue struct {
1823 tv TypeAndValue
1824 }
1825
1826 func (x *typeAndValue) SetTypeInfo(tv TypeAndValue) {
1827 x.tv = tv
1828 }
1829 func (x *typeAndValue) GetTypeInfo() (t TypeAndValue) {
1830 return x.tv
1831 }
1832
1833 type expr struct {
1834 node
1835 typeAndValue
1836 }
1837
1838 func (*expr) aExpr() {}
1839
1840 type ChanDir uint32
1841
1842 const (
1843 _ ChanDir = iota
1844 SendOnly
1845 RecvOnly
1846 )
1847
1848 type (
1849 Stmt interface {
1850 Node
1851 aStmt()
1852 }
1853
1854 SimpleStmt interface {
1855 Stmt
1856 aSimpleStmt()
1857 }
1858
1859 EmptyStmt struct {
1860 simpleStmt
1861 }
1862
1863 LabeledStmt struct {
1864 Label *Name
1865 Stmt Stmt
1866 stmt
1867 }
1868
1869 BlockStmt struct {
1870 List []Stmt
1871 Rbrace Pos
1872 stmt
1873 }
1874
1875 ExprStmt struct {
1876 X Expr
1877 simpleStmt
1878 }
1879
1880 SendStmt struct {
1881 Chan, Value Expr
1882 simpleStmt
1883 }
1884
1885 DeclStmt struct {
1886 DeclList []Decl
1887 stmt
1888 }
1889
1890 AssignStmt struct {
1891 Op Operator
1892 Lhs, Rhs Expr
1893 simpleStmt
1894 }
1895
1896 BranchStmt struct {
1897 Tok Token
1898 Label *Name
1899 Target Stmt
1900 stmt
1901 }
1902
1903 CallStmt struct {
1904 Tok Token
1905 Call Expr
1906 DeferAt Expr
1907 stmt
1908 }
1909
1910 ReturnStmt struct {
1911 Results Expr
1912 stmt
1913 }
1914
1915 IfStmt struct {
1916 Init SimpleStmt
1917 Cond Expr
1918 Then *BlockStmt
1919 Else Stmt
1920 stmt
1921 }
1922
1923 ForStmt struct {
1924 Init SimpleStmt
1925 Cond Expr
1926 Post SimpleStmt
1927 Body *BlockStmt
1928 stmt
1929 }
1930
1931 SwitchStmt struct {
1932 Init SimpleStmt
1933 Tag Expr
1934 Body []*CaseClause
1935 Rbrace Pos
1936 stmt
1937 }
1938
1939 SelectStmt struct {
1940 Body []*CommClause
1941 Rbrace Pos
1942 stmt
1943 }
1944 )
1945
1946 type (
1947 RangeClause struct {
1948 Lhs Expr
1949 Def bool
1950 X Expr
1951 simpleStmt
1952 }
1953
1954 CaseClause struct {
1955 Cases Expr
1956 Body []Stmt
1957 Colon Pos
1958 node
1959 }
1960
1961 CommClause struct {
1962 Comm SimpleStmt
1963 Body []Stmt
1964 Colon Pos
1965 node
1966 }
1967 )
1968
1969 type stmt struct{ node }
1970
1971 func (stmt) aStmt() {}
1972
1973 type simpleStmt struct {
1974 stmt
1975 }
1976
1977 func (simpleStmt) aSimpleStmt() {}
1978
1979 type CommentKind uint32
1980
1981 const (
1982 Above CommentKind = iota
1983 Below
1984 Left
1985 Right
1986 )
1987
1988 type Comment struct {
1989 Kind CommentKind
1990 Text string
1991 Next *Comment
1992 }
1993
1994 // --- parser ---
1995
1996 const debug = false
1997 const trace = false
1998
1999 type ErrorHandler func(err error)
2000 type PragmaHandler func(pos Pos, blank bool, text string, current Pragma)
2001 type Pragma interface{}
2002 type Mode uint32
2003
2004 type Parser struct {
2005 File *PosBase
2006 Errh ErrorHandler
2007 Mode Mode
2008 Pragh PragmaHandler
2009 Scanner
2010
2011 Base *PosBase // current position base
2012 First error // first error encountered
2013 Errcnt int32 // number of errors encountered
2014 Pragma Pragma // pragmas
2015 GoVersion string // Go version from //go:build line
2016
2017 Top bool // in top of file (before package clause)
2018 Fnest int32 // function nesting level (for error handling)
2019 Xnest int32 // expression nesting level (for complit ambiguity resolution)
2020 Indent []byte // tracing support
2021 }
2022
2023 func (p *Parser) initBytes(File *PosBase, src []byte, Errh ErrorHandler, Pragh PragmaHandler, Mode Mode) {
2024 p.Top = true
2025 p.File = File
2026 p.Errh = Errh
2027 p.Mode = Mode
2028 p.Pragh = Pragh
2029 p.Scanner.InitBytes(
2030 src,
2031 func(line, col uint32, msg string) {
2032 if msg[0] != '/' {
2033 p.errorAt(p.posAt(line, col), msg)
2034 return
2035 }
2036 text := commentText(msg)
2037 if (col == Colbase || msg[1] == '*') && bytesHasPrefix(text, "line ") {
2038 var pos Pos
2039 if msg[1] == '/' {
2040 pos = MakePos(p.File, line+1, Colbase)
2041 } else {
2042 pos = MakePos(p.File, line, col+uint32(len(msg)))
2043 }
2044 p.updateBase(pos, line, col+2+5, text[5:])
2045 return
2046 }
2047 if bytesHasPrefix(text, "go:") || bytesHasPrefix(text, ":") {
2048 if Pragh != nil {
2049 Pragh(p.posAt(line, col+2), p.Scanner.Blank, text, p.Pragma)
2050 }
2051 } else if bytesHasPrefix(text, "export ") {
2052 if Pragh != nil {
2053 Pragh(p.posAt(line, col+2), p.Scanner.Blank, text, p.Pragma)
2054 }
2055 }
2056 },
2057 comments,
2058 )
2059
2060 p.Base = File
2061 p.First = nil
2062 p.Errcnt = 0
2063 p.Pragma = nil
2064
2065 p.Fnest = 0
2066 p.Xnest = 0
2067 p.Indent = nil
2068 }
2069
2070 // takePragma returns the current parsed pragmas
2071 // and clears them from the parser state.
2072 func (p *Parser) takePragma() (pv Pragma) {
2073 prag := p.Pragma
2074 p.Pragma = nil
2075 return prag
2076 }
2077
2078 // clearPragma is called at the end of a statement or
2079 // other Go form that does NOT accept a pragma.
2080 // It sends the pragma back to the pragma handler
2081 // to be reported as unused.
2082 func (p *Parser) clearPragma() {
2083 if p.Pragma != nil {
2084 p.Pragh(p.pos(), p.Scanner.Blank, "", p.Pragma)
2085 p.Pragma = nil
2086 }
2087 }
2088
2089 // updateBase sets the current position base to a new line base at pos.
2090 // The base's filename, line, and column values are extracted from text
2091 // which is positioned at (tline, tcol) (only needed for error messages).
2092 func (p *Parser) updateBase(pos Pos, tline, tcol uint32, text string) {
2093 i, n, ok := trailingDigits(text)
2094 if i == 0 {
2095 return // ignore (not a line directive)
2096 }
2097 // i > 0
2098
2099 if !ok {
2100 // text has a suffix :xxx but xxx is not a number
2101 p.errorAt(p.posAt(tline, tcol+i), "invalid line number: " | text[i:])
2102 return
2103 }
2104
2105 var line, col uint32
2106 i2, n2, ok2 := trailingDigits(text[:i-1])
2107 if ok2 {
2108 //line filename:line:col
2109 i, i2 = i2, i
2110 line, col = n2, n
2111 if col == 0 || col > PosMax {
2112 p.errorAt(p.posAt(tline, tcol+i2), "invalid column number: " | text[i2:])
2113 return
2114 }
2115 text = text[:i2-1] // lop off ":col"
2116 } else {
2117 //line filename:line
2118 line = n
2119 }
2120
2121 if line == 0 || line > PosMax {
2122 p.errorAt(p.posAt(tline, tcol+i), "invalid line number: " | text[i:])
2123 return
2124 }
2125
2126 // If we have a column (//line filename:line:col form),
2127 // an empty filename means to use the previous filename.
2128 filename := text[:i-1] // lop off ":line"
2129 trimmed := false
2130 if filename == "" && ok2 {
2131 filename = p.Base.Filename()
2132 trimmed = p.Base.Trimmed()
2133 }
2134
2135 p.Base = NewLineBase(pos, filename, trimmed, line, col)
2136 }
2137
2138 func commentText(s string) (sv string) {
2139 if s[:2] == "/*" {
2140 return s[2 : len(s)-2] // lop off /* and */
2141 }
2142
2143 // line comment (does not include newline)
2144 // (on Windows, the line comment may end in \r\n)
2145 i := len(s)
2146 if s[i-1] == '\r' {
2147 i--
2148 }
2149 return s[2:i] // lop off //, and \r at end, if any
2150 }
2151
2152 func trailingDigits(text string) (uint32, uint32, bool) {
2153 i := bytesLastIndexByte(text, ':')
2154 if i < 0 {
2155 return 0, 0, false
2156 }
2157 s := text[i+1:]
2158 if len(s) == 0 {
2159 return 0, 0, false
2160 }
2161 var n uint32
2162 for j := 0; j < len(s); j++ {
2163 c := s[j]
2164 if c < '0' || c > '9' {
2165 return 0, 0, false
2166 }
2167 n = n*10 + uint32(c-'0')
2168 }
2169 return uint32(i | 1), n, true
2170 }
2171
2172 func (p *Parser) got(tok Token) (ok bool) {
2173 if p.Tok == tok {
2174 p.Next()
2175 return true
2176 }
2177 return false
2178 }
2179
2180 func (p *Parser) want(tok Token) {
2181 if !p.got(tok) {
2182 p.syntaxError("expected " | tokstring(tok))
2183 p.advance()
2184 }
2185 }
2186
2187 // gotAssign is like got(_Assign) but it also accepts ":="
2188 // (and reports an error) for better parser error recovery.
2189 func (p *Parser) gotAssign() (ok bool) {
2190 switch p.Tok {
2191 case Define:
2192 p.syntaxError("expected =")
2193 p.Next()
2194 return true
2195 case Assign:
2196 p.Next()
2197 return true
2198 }
2199
2200 return false
2201 }
2202
2203 // ----------------------------------------------------------------------------
2204 // Error handling
2205
2206 // posAt returns the Pos value for (line, col) and the current position base.
2207 func (p *Parser) posAt(line, col uint32) (pv Pos) {
2208 return MakePos(p.Base, line, col)
2209 }
2210
2211 // errorAt reports an error at the given position.
2212 func (p *Parser) errorAt(pos Pos, msg string) {
2213 if len(msg) == 0 {
2214 return
2215 }
2216 err := Error{pos, msg}
2217 if p.First == nil {
2218 p.First = err
2219 }
2220 p.Errcnt++
2221 if p.Errh == nil {
2222 panic(p.First)
2223 }
2224 p.Errh(err)
2225 }
2226
2227 // syntaxErrorAt reports a syntax error at the given position.
2228 func (p *Parser) syntaxErrorAt(pos Pos, msg string) {
2229 if trace {
2230 p.print("syntax error: " | msg)
2231 }
2232
2233 if p.Tok == EOF && p.First != nil {
2234 return // avoid meaningless follow-up errors
2235 }
2236
2237 // add punctuation etc. as needed to msg
2238 switch {
2239 case msg == "":
2240 // nothing
2241 case bytesHasPrefix(msg, "in "), bytesHasPrefix(msg, "at "), bytesHasPrefix(msg, "after "):
2242 msg = " " | msg
2243 case bytesHasPrefix(msg, "expected "):
2244 msg = ", " | msg
2245 default:
2246 p.errorAt(pos, "syntax error: " | msg)
2247 return
2248 }
2249
2250 // determine token string
2251 var tok string
2252 switch p.Tok {
2253 case NameType:
2254 tok = "name " | p.Lit
2255 case Semi:
2256 tok = p.Lit
2257 case Literal:
2258 tok = "literal " | p.Lit
2259 case OperatorType:
2260 tok = p.Op.String()
2261 case AssignOp:
2262 tok = p.Op.String() | "="
2263 case IncOp:
2264 tok = p.Op.String()
2265 tok = tok | tok
2266 default:
2267 tok = tokstring(p.Tok)
2268 }
2269
2270 p.errorAt(pos, "syntax error: unexpected " | tok | msg)
2271 }
2272
2273 // tokstring returns the English word for selected punctuation tokens
2274 // for more readable error messages. Use tokstring (not tok.String())
2275 // for user-facing (error) messages; use tok.String() for debugging
2276 // output.
2277 func tokstring(tok Token) (s string) {
2278 switch tok {
2279 case Comma:
2280 return "comma"
2281 case Semi:
2282 return "semicolon or newline"
2283 }
2284 s = tok.String()
2285 if Break <= tok && tok <= Var {
2286 return "keyword " | s
2287 }
2288 return s
2289 }
2290
2291 // Convenience methods using the current token position.
2292 func (p *Parser) pos() (pv Pos) { return p.posAt(p.Line-Linebase, p.Col-Colbase) }
2293 func (p *Parser) error(msg string) { p.errorAt(p.pos(), msg) }
2294 func (p *Parser) syntaxError(msg string) { p.syntaxErrorAt(p.pos(), msg) }
2295
2296 // The stopset contains keywords that start a statement.
2297 // They are good synchronization points in case of syntax
2298 // errors and (usually) shouldn't be skipped over.
2299 const stopset uint64 = 1<<Break |
2300 1<<Const |
2301 1<<Continue |
2302 1<<Defer |
2303 1<<Fallthrough |
2304 1<<For |
2305 1<<Go |
2306 1<<Goto |
2307 1<<If |
2308 1<<Return |
2309 1<<Select |
2310 1<<Switch |
2311 1<<TypeType |
2312 1<<Var
2313
2314 // advance consumes tokens until it finds a token of the stopset or followlist.
2315 // The stopset is only considered if we are inside a function (p.fnest > 0).
2316 // The followlist is the list of valid tokens that can follow a production;
2317 // if it is empty, exactly one (non-EOF) token is consumed to ensure progress.
2318 func (p *Parser) advance(followlist ...Token) {
2319 if trace {
2320 p.print("advance")
2321 }
2322
2323 // compute follow set
2324 // (not speed critical, advance is only called in error situations)
2325 var followset uint64 = 1 << EOF // don't skip over EOF
2326 if len(followlist) > 0 {
2327 if p.Fnest > 0 {
2328 followset |= stopset
2329 }
2330 for _, tok := range followlist {
2331 var bit uint64 = 1
2332 followset |= bit << tok
2333 }
2334 }
2335
2336 for !Contains(followset, p.Tok) {
2337 if trace {
2338 p.print("skip " | p.Tok.String())
2339 }
2340 p.Next()
2341 if len(followlist) == 0 {
2342 break
2343 }
2344 }
2345
2346 if trace {
2347 p.print("next " | p.Tok.String())
2348 }
2349 }
2350
2351 // usage: defer p.trace(msg)()
2352 func (p *Parser) trace(msg string) (fn func()) {
2353 p.print(msg | " (")
2354 const tab = ". "
2355 p.Indent = append(p.Indent, tab...)
2356 return func() {
2357 p.Indent = p.Indent[:len(p.Indent)-len(tab)]
2358 if x := recover(); x != nil {
2359 panic(x) // skip print_trace
2360 }
2361 p.print(")")
2362 }
2363 }
2364
2365 func (p *Parser) print(msg string) {
2366 // trace is const false; this is dead code but must type-check
2367 _ = Itoa(int32(p.line)) | ": " | p.Indent | msg | "\n"
2368 }
2369
2370 // ----------------------------------------------------------------------------
2371 // Package files
2372 //
2373 // Parse methods are annotated with matching Go productions as appropriate.
2374 // The annotations are intended as guidelines only since a single Go grammar
2375 // rule may be covered by multiple parse methods and vice versa.
2376 //
2377 // Excluding methods returning slices, parse methods named xOrNil may return
2378 // nil; all others are expected to return a valid non-nil node.
2379
2380 // SourceFile = PackageClause ";" { ImportDecl ";" } { TopLevelDecl ";" } .
2381 func (p *Parser) fileOrNil() (f *File) {
2382 if trace {
2383 defer p.trace("file")()
2384 }
2385
2386 f = &File{}
2387 f.pos = p.pos()
2388
2389 // PackageClause
2390 f.GoVersion = p.GoVersion
2391 p.Top = false
2392 if !p.got(Package) {
2393 p.syntaxError("package statement must be first")
2394 return nil
2395 }
2396 f.Pragma = p.takePragma()
2397 f.PkgName = p.name()
2398 p.want(Semi)
2399
2400 // don't bother continuing if package clause has errors
2401 if p.First != nil {
2402 return nil
2403 }
2404
2405 // Accept import declarations anywhere for error tolerance, but complain.
2406 // { ( ImportDecl | TopLevelDecl ) ";" }
2407 prev := Import
2408 for p.Tok != EOF {
2409 if p.Tok == Import && prev != Import {
2410 p.syntaxError("imports must appear before other declarations")
2411 }
2412 prev = p.Tok
2413
2414 switch p.Tok {
2415 case Import:
2416 p.Next()
2417 f.DeclList = p.appendGroup(f.DeclList, p.importDecl)
2418
2419 case Const:
2420 p.Next()
2421 f.DeclList = p.appendGroup(f.DeclList, p.constDecl)
2422
2423 case TypeType:
2424 p.Next()
2425 f.DeclList = p.appendGroup(f.DeclList, p.typeDecl)
2426
2427 case Var:
2428 p.Next()
2429 f.DeclList = p.appendGroup(f.DeclList, p.varDecl)
2430
2431 case Func:
2432 p.Next()
2433 fd := &FuncDecl{}
2434 fd.pos = p.pos()
2435 if p.got(Lparen) {
2436 rcvr := p.paramList(nil, nil, Rparen, false, false)
2437 if len(rcvr) > 0 {
2438 fd.Recv = rcvr[0]
2439 }
2440 }
2441 if p.Tok == NameType {
2442 fd.Name = p.name()
2443 fd.TParamList, fd.Type = p.funcType("")
2444 }
2445 if p.Tok == Lbrace {
2446 fd.Body = p.funcBody()
2447 }
2448 f.DeclList = append(f.DeclList, fd)
2449
2450 default:
2451 if p.Tok == Lbrace && len(f.DeclList) > 0 && isEmptyFuncDecl(f.DeclList[len(f.DeclList)-1]) {
2452 // opening { of function declaration on next line
2453 p.syntaxError("unexpected semicolon or newline before {")
2454 } else {
2455 p.syntaxError("non-declaration statement outside function body")
2456 }
2457 p.advance(Import, Const, TypeType, Var, Func)
2458 continue
2459 }
2460
2461 // Reset p.pragma BEFORE advancing to the next token (consuming ';')
2462 // since comments before may set pragmas for the next function decl.
2463 p.clearPragma()
2464
2465 if p.Tok != EOF && !p.got(Semi) {
2466 p.syntaxError("after top level declaration")
2467 p.advance(Import, Const, TypeType, Var, Func)
2468 }
2469 }
2470 // p.tok == _EOF
2471
2472 p.clearPragma()
2473 f.EOF = p.pos()
2474
2475 return f
2476 }
2477
2478 func isEmptyFuncDecl(dcl Decl) (ok bool) {
2479 f, ok := dcl.(*FuncDecl)
2480 return ok && f.Body == nil
2481 }
2482
2483 // ----------------------------------------------------------------------------
2484 // Declarations
2485
2486 // list parses a possibly empty, sep-separated list of elements, optionally
2487 // followed by sep, and closed by close (or EOF). sep must be one of _Comma
2488 // or _Semi, and close must be one of _Rparen, _Rbrace, or _Rbrack.
2489 //
2490 // For each list element, f is called. Specifically, unless we're at close
2491 // (or EOF), f is called at least once. After f returns true, no more list
2492 // elements are accepted. list returns the position of the closing
2493 //
2494 // list = [ f { sep f } [sep] ] close .
2495 func (p *Parser) list(context string, sep, close Token, f func() bool) (pv Pos) {
2496 if debug && (sep != Comma && sep != Semi || close != Rparen && close != Rbrace && close != Rbrack) {
2497 panic("invalid sep or close argument for list")
2498 }
2499
2500 done := false
2501 for p.Tok != EOF && p.Tok != close && !done {
2502 done = f()
2503 // sep is optional before close
2504 if !p.got(sep) && p.Tok != close {
2505 p.syntaxError("in " | context | "; possibly missing " | tokstring(sep) | " or " | tokstring(close))
2506 p.advance(Rparen, Rbrack, Rbrace)
2507 if p.Tok != close {
2508 // position could be better but we had an error so we don't care
2509 return p.pos()
2510 }
2511 }
2512 }
2513
2514 pos := p.pos()
2515 p.want(close)
2516 return pos
2517 }
2518
2519 // appendGroup(f) = f + "(" { f ";" } ")" . // ";" is optional before ")"
2520 func (p *Parser) appendGroup(list []Decl, f func(*Group) Decl) (ds []Decl) {
2521 if p.Tok == Lparen {
2522 g := &Group{}
2523 p.clearPragma()
2524 p.Next() // must consume "(" after calling clearPragma!
2525 p.list("grouped declaration", Semi, Rparen, func() bool {
2526 if x := f(g); x != nil {
2527 list = append(list, x)
2528 }
2529 return false
2530 })
2531 } else {
2532 if x := f(nil); x != nil {
2533 list = append(list, x)
2534 }
2535 }
2536 return list
2537 }
2538
2539 // ImportSpec = [ "." + PackageName ] ImportPath .
2540 // ImportPath = string_lit .
2541 func (p *Parser) importDecl(group *Group) Decl {
2542 if trace {
2543 defer p.trace("importDecl")()
2544 }
2545
2546 d := &ImportDecl{}
2547 d.pos = p.pos()
2548 d.Group = group
2549 d.Pragma = p.takePragma()
2550
2551 switch p.Tok {
2552 case NameType:
2553 n := p.name()
2554 if n.Value == "_" {
2555 p.syntaxErrorAt(n.Pos(), "blank imports are not allowed in Moxie")
2556 }
2557 d.LocalPkgName = n
2558 case Dot:
2559 d.LocalPkgName = NewName(p.pos(), ".")
2560 p.Next()
2561 }
2562 d.Path = p.oliteral()
2563 if d.Path == nil {
2564 p.syntaxError("missing import path")
2565 p.advance(Semi, Rparen)
2566 return d
2567 }
2568 if !d.Path.Bad && d.Path.Kind != StringLit {
2569 p.syntaxErrorAt(d.Path.Pos(), "import path must be a string")
2570 d.Path.Bad = true
2571 }
2572 // d.Path.Bad || d.Path.Kind == StringLit
2573
2574 return d
2575 }
2576
2577 // ConstSpec = IdentifierList [ [ Type ] "=" ExpressionList ] .
2578 func (p *Parser) constDecl(group *Group) Decl {
2579 if trace {
2580 defer p.trace("constDecl")()
2581 }
2582
2583 d := &ConstDecl{}
2584 d.pos = p.pos()
2585 d.Group = group
2586 d.Pragma = p.takePragma()
2587
2588 d.NameList = p.nameList(p.name())
2589 if p.Tok != EOF && p.Tok != Semi && p.Tok != Rparen {
2590 d.Type = p.typeOrNil()
2591 if p.gotAssign() {
2592 d.Values = p.exprList()
2593 }
2594 }
2595
2596 return d
2597 }
2598
2599 // TypeSpec = identifier [ TypeParams ] [ "=" ] Type .
2600 func (p *Parser) typeDecl(group *Group) Decl {
2601 if trace {
2602 defer p.trace("typeDecl")()
2603 }
2604
2605 d := &TypeDecl{}
2606 d.pos = p.pos()
2607 d.Group = group
2608 d.Pragma = p.takePragma()
2609
2610 d.Name = p.name()
2611 if p.Tok == Lbrack {
2612 // d.Name "[" ...
2613 // array/slice type or type parameter list
2614 pos := p.pos()
2615 p.Next()
2616 switch p.Tok {
2617 case NameType:
2618 // We may have an array type or a type parameter list.
2619 // In either case we expect an expression x (which may
2620 // just be a name, or a more complex expression) which
2621 // we can analyze further.
2622 //
2623 // A type parameter list may have a type bound starting
2624 // with a "[" as in: P []E. In that case, simply parsing
2625 // an expression would lead to an error: P[] is invalid.
2626 // But since index or slice expressions are never constant
2627 // and thus invalid array length expressions, if the name
2628 // is followed by "[" it must be the start of an array or
2629 // slice constraint. Only if we don't see a "[" do we
2630 // need to parse a full expression. Notably, name <- x
2631 // is not a concern because name <- x is a statement and
2632 // not an expression.
2633 var x Expr = p.name()
2634 if p.Tok != Lbrack {
2635 // To parse the expression starting with name, expand
2636 // the call sequence we would get by passing in name
2637 // to parser.expr, and pass in name to parser.pexpr.
2638 p.Xnest++
2639 x = p.binaryExpr(p.pexpr(x, false), 0)
2640 p.Xnest--
2641 }
2642 // Analyze expression x. If we can split x into a type parameter
2643 // name, possibly followed by a type parameter type, we consider
2644 // this the start of a type parameter list, with some caveats:
2645 // a single name followed by "]" tilts the decision towards an
2646 // array declaration; a type parameter type that could also be
2647 // an ordinary expression but which is followed by a comma tilts
2648 // the decision towards a type parameter list.
2649 if pname, ptype := extractName(x, p.Tok == Comma); pname != nil && (ptype != nil || p.Tok != Rbrack) {
2650 // d.Name "[" pname ...
2651 // d.Name "[" pname ptype ...
2652 // d.Name "[" pname ptype "," ...
2653 d.TParamList = p.paramList(pname, ptype, Rbrack, true, false) // ptype may be nil
2654 d.Alias = p.gotAssign()
2655 d.Type = p.typeOrNil()
2656 } else {
2657 // d.Name "[" pname "]" ...
2658 // d.Name "[" x ...
2659 d.Type = p.arrayType(pos, x)
2660 }
2661 case Rbrack:
2662 // d.Name "[" "]" ...
2663 p.Next()
2664 d.Type = p.sliceType(pos)
2665 default:
2666 // d.Name "[" ...
2667 d.Type = p.arrayType(pos, nil)
2668 }
2669 } else {
2670 d.Alias = p.gotAssign()
2671 d.Type = p.typeOrNil()
2672 }
2673
2674 if d.Type == nil {
2675 d.Type = p.badExpr()
2676 p.syntaxError("in type declaration")
2677 p.advance(Semi, Rparen)
2678 }
2679
2680 return d
2681 }
2682
2683 // extractName splits the expression x into (name, expr) if syntactically
2684 // x can be written as name expr. The split only happens if expr is a type
2685 // element (per the isTypeElem predicate) or if force is set.
2686 // If x is just a name, the result is (name, nil). If the split succeeds,
2687 // the result is (name, expr). Otherwise the result is (nil, x).
2688 // Examples:
2689 //
2690 // x force name expr
2691 // ------------------------------------
2692 // P*[]int32 T/F P *[]int32
2693 // P*E T P *E
2694 // P*E F nil P*E
2695 // P([]int32) T/F P []int32
2696 // P(E) T P E
2697 // P(E) F nil P(E)
2698 // P*E|F|~G T/F P *E|F|~G
2699 // P*E|F|G T P *E|F|G
2700 // P*E|F|G F nil P*E|F|G
2701 func extractName(x Expr, force bool) (*Name, Expr) {
2702 switch x := x.(type) {
2703 case *Name:
2704 return x, nil
2705 case *Operation:
2706 if x.Y == nil {
2707 break // unary expr
2708 }
2709 switch x.Op {
2710 case Mul:
2711 if name, _ := x.X.(*Name); name != nil && (force || isTypeElem(x.Y)) {
2712 // x = name *x.Y
2713 op := *x
2714 op.X, op.Y = op.Y, nil // change op into unary *op.Y
2715 return name, &op
2716 }
2717 case Or:
2718 if name, lhs := extractName(x.X, force || isTypeElem(x.Y)); name != nil && lhs != nil {
2719 // x = name lhs|x.Y
2720 op := *x
2721 op.X = lhs
2722 return name, &op
2723 }
2724 }
2725 case *CallExpr:
2726 if name, _ := x.Fun.(*Name); name != nil {
2727 if len(x.ArgList) == 1 && !x.HasDots && (force || isTypeElem(x.ArgList[0])) {
2728 // The parser doesn't keep unnecessary parentheses.
2729 // Set the flag below to keep them, for testing
2730 // (see go.dev/issues/69206).
2731 const keep_parens = false
2732 if keep_parens {
2733 // x = name (x.ArgList[0])
2734 px := &ParenExpr{}
2735 px.pos = x.pos // position of "(" in call
2736 px.X = x.ArgList[0]
2737 return name, px
2738 } else {
2739 // x = name x.ArgList[0]
2740 return name, Unparen(x.ArgList[0])
2741 }
2742 }
2743 }
2744 }
2745 return nil, x
2746 }
2747
2748 // isTypeElem reports whether x is a (possibly parenthesized) type element expression.
2749 // The result is false if x could be a type element OR an ordinary (value) expression.
2750 func isTypeElem(x Expr) (ok bool) {
2751 switch x := x.(type) {
2752 case *ArrayType, *StructType, *FuncType, *InterfaceType, *SliceType, *MapType, *ChanType:
2753 return true
2754 case *Operation:
2755 return isTypeElem(x.X) || (x.Y != nil && isTypeElem(x.Y)) || x.Op == Tilde
2756 case *ParenExpr:
2757 return isTypeElem(x.X)
2758 }
2759 return false
2760 }
2761
2762 // VarSpec = IdentifierList ( Type [ "=" ExpressionList ] + "=" ExpressionList ) .
2763 func (p *Parser) varDecl(group *Group) Decl {
2764 if trace {
2765 defer p.trace("varDecl")()
2766 }
2767
2768 d := &VarDecl{}
2769 d.pos = p.pos()
2770 d.Group = group
2771 d.Pragma = p.takePragma()
2772
2773 d.NameList = p.nameList(p.name())
2774 if p.gotAssign() {
2775 d.Values = p.exprList()
2776 } else {
2777 d.Type = p.type_()
2778 if p.gotAssign() {
2779 d.Values = p.exprList()
2780 }
2781 }
2782
2783 return d
2784 }
2785
2786 // FunctionDecl = "func" FunctionName [ TypeParams ] ( Function | Signature ) .
2787 // FunctionName = identifier .
2788 // Function = Signature FunctionBody .
2789 // MethodDecl = "func" Receiver MethodName ( Function | Signature ) .
2790 // Receiver = Parameters .
2791 func (p *Parser) funcDeclOrNil() (f *FuncDecl) {
2792 if trace {
2793 defer p.trace("funcDecl")()
2794 }
2795
2796 f = &FuncDecl{}
2797 f.pos = p.pos()
2798 f.Pragma = p.takePragma()
2799
2800 var context string
2801 if p.got(Lparen) {
2802 context = "method"
2803 rcvr := p.paramList(nil, nil, Rparen, false, false)
2804 switch len(rcvr) {
2805 case 0:
2806 p.error("method has no receiver")
2807 default:
2808 p.error("method has multiple receivers")
2809 f.Recv = rcvr[0]
2810 case 1:
2811 f.Recv = rcvr[0]
2812 }
2813 }
2814
2815 if p.Tok == NameType {
2816 f.Name = p.name()
2817 f.TParamList, f.Type = p.funcType(context)
2818 } else {
2819 dbg("funcDecl: not a name, falling to error\n")
2820 f.Name = NewName(p.pos(), "_")
2821 f.Type = &FuncType{}
2822 f.Type.pos = p.pos()
2823 msg := "expected name or ("
2824 if context != "" {
2825 msg = "expected name"
2826 }
2827 p.syntaxError(msg)
2828 p.advance(Lbrace, Semi)
2829 }
2830
2831 if p.Tok == Lbrace {
2832 f.Body = p.funcBody()
2833 }
2834
2835 return f
2836 }
2837
2838 func (p *Parser) funcBody() (b *BlockStmt) {
2839 p.Fnest++
2840 body := p.blockStmt("")
2841 p.Fnest--
2842
2843 return body
2844 }
2845
2846 // ----------------------------------------------------------------------------
2847 // Expressions
2848
2849 func (p *Parser) expr() (e Expr) {
2850 if trace {
2851 defer p.trace("expr")()
2852 }
2853
2854 return p.binaryExpr(nil, 0)
2855 }
2856
2857 // Expression = UnaryExpr | Expression binary_op Expression .
2858 func (p *Parser) binaryExpr(x Expr, prec int32) (e Expr) {
2859 // don't trace binaryExpr - only leads to overly nested trace output
2860
2861 if x == nil {
2862 x = p.unaryExpr()
2863 }
2864 for (p.Tok == OperatorType || p.Tok == Star) && p.Prec > prec {
2865 t := &Operation{}
2866 t.pos = p.pos()
2867 t.Op = p.Op
2868 tprec := p.Prec
2869 p.Next()
2870 t.X = x
2871 t.Y = p.binaryExpr(nil, tprec)
2872 x = t
2873 }
2874 return x
2875 }
2876
2877 // UnaryExpr = PrimaryExpr | unary_op UnaryExpr .
2878 func (p *Parser) unaryExpr() (e Expr) {
2879 if trace {
2880 defer p.trace("unaryExpr")()
2881 }
2882
2883 switch p.Tok {
2884 case OperatorType, Star:
2885 switch p.Op {
2886 case Mul, Add, Sub, Not, Xor, Tilde:
2887 x := &Operation{}
2888 x.pos = p.pos()
2889 x.Op = p.Op
2890 p.Next()
2891 x.X = p.unaryExpr()
2892 return x
2893
2894 case And:
2895 x := &Operation{}
2896 x.pos = p.pos()
2897 x.Op = And
2898 p.Next()
2899 // unaryExpr may have returned a parenthesized composite literal
2900 // (see comment in operand) - remove parentheses if any
2901 x.X = Unparen(p.unaryExpr())
2902 return x
2903 }
2904
2905 case Arrow:
2906 // receive op (<-x) or receive-only channel (<-chan E)
2907 pos := p.pos()
2908 p.Next()
2909
2910 // If the next token is _Chan we still don't know if it is
2911 // a channel (<-chan int32) or a receive op (<-chan int32(ch)).
2912 // We only know once we have found the end of the unaryExpr.
2913
2914 x := p.unaryExpr()
2915
2916 // There are two cases:
2917 //
2918 // <-chan... => <-x is a channel type
2919 // <-x => <-x is a receive operation
2920 //
2921 // In the first case, <- must be re-associated with
2922 // the channel type parsed already:
2923 //
2924 // <-(chan E) => (<-chan E)
2925 // <-(chan<-E) => (<-chan (<-E))
2926
2927 if _, ok := x.(*ChanType); ok {
2928 // x is a channel type => re-associate <-
2929 dir := SendOnly
2930 t := x
2931 for dir == SendOnly {
2932 c, ok := t.(*ChanType)
2933 if !ok {
2934 break
2935 }
2936 dir = c.Dir
2937 if dir == RecvOnly {
2938 // t is type <-chan E but <-<-chan E is not permitted
2939 // (report same error as for "type _ <-<-chan E")
2940 p.syntaxError("unexpected <-, expected chan")
2941 // already progressed, no need to advance
2942 }
2943 c.Dir = RecvOnly
2944 t = c.Elem
2945 }
2946 if dir == SendOnly {
2947 // channel dir is <- but channel element E is not a channel
2948 // (report same error as for "type _ <-chan<-E")
2949 p.syntaxError("unexpected " | String(t) | ", expected chan")
2950 // already progressed, no need to advance
2951 }
2952 return x
2953 }
2954
2955 // x is not a channel type => we have a receive op
2956 o := &Operation{}
2957 o.pos = pos
2958 o.Op = Recv
2959 o.X = x
2960 return o
2961 }
2962
2963 // TODO(mdempsky): We need parens here so we can report an
2964 // error for "(x) := true". It should be possible to detect
2965 // and reject that more efficiently though.
2966 return p.pexpr(nil, true)
2967 }
2968
2969 // callStmt parses call-like statements that can be preceded by 'defer' and 'go'.
2970 func (p *Parser) callStmt() (c *CallStmt) {
2971 if trace {
2972 defer p.trace("callStmt")()
2973 }
2974
2975 s := &CallStmt{}
2976 s.pos = p.pos()
2977 s.Tok = p.Tok // _Defer or _Go
2978 p.Next()
2979
2980 x := p.pexpr(nil, p.Tok == Lparen) // keep_parens so we can report error below
2981 if t := Unparen(x); t != x {
2982 p.errorAt(x.Pos(), "expression in " | s.Tok.String() | " must not be parenthesized")
2983 // already progressed, no need to advance
2984 x = t
2985 }
2986
2987 s.Call = x
2988 return s
2989 }
2990
2991 // Operand = Literal | OperandName | MethodExpr + "(" Expression ")" .
2992 // Literal = BasicLit | CompositeLit | FunctionLit .
2993 // BasicLit = int_lit | float_lit | imaginary_lit | rune_lit | string_lit .
2994 // OperandName = identifier | QualifiedIdent.
2995 func (p *Parser) operand(keep_parens bool) (e Expr) {
2996 if trace {
2997 defer p.trace("operand " | p.Tok.String())()
2998 }
2999
3000 switch p.Tok {
3001 case NameType:
3002 return p.name()
3003
3004 case Literal:
3005 return p.oliteral()
3006
3007 case Lparen:
3008 pos := p.pos()
3009 p.Next()
3010 p.Xnest++
3011 x := p.expr()
3012 p.Xnest--
3013 p.want(Rparen)
3014
3015 // Optimization: Record presence of ()'s only where needed
3016 // for error reporting. Don't bother in other cases; it is
3017 // just a waste of memory and time.
3018 //
3019 // Parentheses are not permitted around T in a composite
3020 // literal T{}. If the next token is a {, assume x is a
3021 // composite literal type T (it may not be, { could be
3022 // the opening brace of a block, but we don't know yet).
3023 if p.Tok == Lbrace {
3024 keep_parens = true
3025 }
3026
3027 // Parentheses are also not permitted around the expression
3028 // in a go/defer statement. In that case, operand is called
3029 // with keep_parens set.
3030 if keep_parens {
3031 px := &ParenExpr{}
3032 px.pos = pos
3033 px.X = x
3034 x = px
3035 }
3036 return x
3037
3038 case Func:
3039 pos := p.pos()
3040 p.Next()
3041 _, ftyp := p.funcType("function type")
3042 if p.Tok == Lbrace {
3043 p.Xnest++
3044
3045 f := &FuncLit{}
3046 f.pos = pos
3047 f.Type = ftyp
3048 f.Body = p.funcBody()
3049
3050 p.Xnest--
3051 return f
3052 }
3053 return ftyp
3054
3055 case Lbrack, Chan, Map, Struct, Interface:
3056 return p.type_() // othertype
3057
3058 default:
3059 x := p.badExpr()
3060 p.syntaxError("expected expression")
3061 p.advance(Rparen, Rbrack, Rbrace)
3062 return x
3063 }
3064
3065 // Syntactically, composite literals are operands. Because a complit
3066 // type may be a qualified identifier which is handled by pexpr
3067 // (together with selector expressions), complits are parsed there
3068 // as well (operand is only called from pexpr).
3069 }
3070
3071 // pexpr parses a PrimaryExpr.
3072 //
3073 // PrimaryExpr =
3074 // Operand |
3075 // Conversion |
3076 // PrimaryExpr Selector |
3077 // PrimaryExpr Index |
3078 // PrimaryExpr Slice |
3079 // PrimaryExpr TypeAssertion |
3080 // PrimaryExpr Arguments .
3081 //
3082 // Selector = "." identifier .
3083 // Index = "[" Expression "]" .
3084 // Slice = "[" ( [ Expression ] ":" [ Expression ] ) |
3085 // ( [ Expression ] ":" Expression ":" Expression )
3086 // "]" .
3087 // TypeAssertion = "." "(" Type ")" .
3088 // Arguments = "(" [ ( ExpressionList | Type [ "," ExpressionList ] ) [ "..." ] [ "," ] ] ")" .
3089 func (p *Parser) pexpr(x Expr, keep_parens bool) (e Expr) {
3090 if trace {
3091 defer p.trace("pexpr")()
3092 }
3093
3094 if x == nil {
3095 x = p.operand(keep_parens)
3096 }
3097
3098 loop:
3099 for {
3100 pos := p.pos()
3101 switch p.Tok {
3102 case Dot:
3103 p.Next()
3104 switch p.Tok {
3105 case NameType:
3106 // pexpr '.' sym
3107 t := &SelectorExpr{}
3108 t.pos = pos
3109 t.X = x
3110 t.Sel = p.name()
3111 x = t
3112
3113 case Lparen:
3114 p.Next()
3115 if p.got(TypeType) {
3116 t := &TypeSwitchGuard{}
3117 // t.Lhs is filled in by parser.simpleStmt
3118 t.pos = pos
3119 t.X = x
3120 x = t
3121 } else {
3122 t := &AssertExpr{}
3123 t.pos = pos
3124 t.X = x
3125 t.Type = p.type_()
3126 x = t
3127 }
3128 p.want(Rparen)
3129
3130 default:
3131 p.syntaxError("expected name or (")
3132 p.advance(Semi, Rparen)
3133 }
3134
3135 case Lbrack:
3136 p.Next()
3137
3138 var i Expr
3139 if p.Tok != Colon {
3140 var comma bool
3141 if p.Tok == Rbrack {
3142 // invalid empty instance, slice or index expression; accept but complain
3143 p.syntaxError("expected operand")
3144 i = p.badExpr()
3145 } else {
3146 i, comma = p.typeList(false)
3147 }
3148 if comma || p.Tok == Rbrack {
3149 p.want(Rbrack)
3150 // x[], x[i,] or x[i, j, ...]
3151 t := &IndexExpr{}
3152 t.pos = pos
3153 t.X = x
3154 t.Index = i
3155 x = t
3156 break
3157 }
3158 }
3159
3160 // x[i:...
3161 // For better error message, don't simply use p.want(_Colon) here (go.dev/issue/47704).
3162 if !p.got(Colon) {
3163 p.syntaxError("expected comma, : or ]")
3164 p.advance(Comma, Colon, Rbrack)
3165 }
3166 p.Xnest++
3167 t := &SliceExpr{}
3168 t.pos = pos
3169 t.X = x
3170 t.Index[0] = i
3171 if p.Tok != Colon && p.Tok != Rbrack {
3172 // x[i:j...
3173 t.Index[1] = p.expr()
3174 }
3175 if p.Tok == Colon {
3176 t.Full = true
3177 // x[i:j:...]
3178 if t.Index[1] == nil {
3179 p.error("middle index required in 3-index slice")
3180 t.Index[1] = p.badExpr()
3181 }
3182 p.Next()
3183 if p.Tok != Rbrack {
3184 // x[i:j:k...
3185 t.Index[2] = p.expr()
3186 } else {
3187 p.error("final index required in 3-index slice")
3188 t.Index[2] = p.badExpr()
3189 }
3190 }
3191 p.Xnest--
3192 p.want(Rbrack)
3193 x = t
3194
3195 case Lparen:
3196 t := &CallExpr{}
3197 t.pos = pos
3198 p.Next()
3199 t.Fun = x
3200 t.ArgList, t.HasDots = p.argList()
3201 x = t
3202
3203 case Lbrace:
3204 // operand may have returned a parenthesized complit
3205 // type; accept it but complain if we have a complit
3206 t := Unparen(x)
3207 // determine if '{' belongs to a composite literal or a block statement
3208 complit_ok := false
3209 switch t.(type) {
3210 case *Name, *SelectorExpr:
3211 if p.Xnest >= 0 {
3212 // x is possibly a composite literal type
3213 complit_ok = true
3214 }
3215 case *IndexExpr:
3216 if p.Xnest >= 0 && !isValue(t) {
3217 // x is possibly a composite literal type
3218 complit_ok = true
3219 }
3220 case *ArrayType, *SliceType, *StructType, *MapType:
3221 // x is a comptype
3222 complit_ok = true
3223 }
3224 if !complit_ok {
3225 break loop
3226 }
3227 if t != x {
3228 p.syntaxError("cannot parenthesize type in composite literal")
3229 // already progressed, no need to advance
3230 }
3231 n := p.complitexpr()
3232 n.Type = x
3233 x = n
3234
3235 default:
3236 break loop
3237 }
3238 }
3239
3240 return x
3241 }
3242
3243 // isValue reports whether x syntactically must be a value (and not a type) expression.
3244 func isValue(x Expr) (ok bool) {
3245 switch x := x.(type) {
3246 case *BasicLit, *CompositeLit, *FuncLit, *SliceExpr, *AssertExpr, *TypeSwitchGuard, *CallExpr:
3247 return true
3248 case *Operation:
3249 return x.Op != Mul || x.Y != nil // *T may be a type
3250 case *ParenExpr:
3251 return isValue(x.X)
3252 case *IndexExpr:
3253 return isValue(x.X) || isValue(x.Index)
3254 }
3255 return false
3256 }
3257
3258 // Element = Expression | LiteralValue .
3259 func (p *Parser) bare_complitexpr() (e Expr) {
3260 if trace {
3261 defer p.trace("bare_complitexpr")()
3262 }
3263
3264 if p.Tok == Lbrace {
3265 // '{' start_complit braced_keyval_list '}'
3266 return p.complitexpr()
3267 }
3268
3269 return p.expr()
3270 }
3271
3272 // LiteralValue = "{" [ ElementList [ "," ] ] "}" .
3273 func (p *Parser) complitexpr() (c *CompositeLit) {
3274 if trace {
3275 defer p.trace("complitexpr")()
3276 }
3277
3278 x := &CompositeLit{}
3279 x.pos = p.pos()
3280
3281 p.Xnest++
3282 p.want(Lbrace)
3283 x.Rbrace = p.list("composite literal", Comma, Rbrace, func() bool {
3284 // value
3285 e := p.bare_complitexpr()
3286 if p.Tok == Colon {
3287 // key ':' value
3288 l := &KeyValueExpr{}
3289 l.pos = p.pos()
3290 p.Next()
3291 l.Key = e
3292 l.Value = p.bare_complitexpr()
3293 e = l
3294 x.NKeys++
3295 }
3296 x.ElemList = append(x.ElemList, e)
3297 return false
3298 })
3299 p.Xnest--
3300
3301 return x
3302 }
3303
3304 // ----------------------------------------------------------------------------
3305 // Types
3306
3307 func (p *Parser) type_() (e Expr) {
3308 if trace {
3309 defer p.trace("type_")()
3310 }
3311
3312 typ := p.typeOrNil()
3313 if typ == nil {
3314 typ = p.badExpr()
3315 p.syntaxError("expected type")
3316 p.advance(Comma, Colon, Semi, Rparen, Rbrack, Rbrace)
3317 }
3318
3319 return typ
3320 }
3321
3322 func newIndirect(pos Pos, typ Expr) (e Expr) {
3323 o := &Operation{}
3324 o.pos = pos
3325 o.Op = Mul
3326 o.X = typ
3327 return o
3328 }
3329
3330 // typeOrNil is like type_ but it returns nil if there was no type
3331 // instead of reporting an error.
3332 //
3333 // Type = TypeName | TypeLit + "(" Type ")" .
3334 // TypeName = identifier | QualifiedIdent .
3335 // TypeLit = ArrayType | StructType | PointerType | FunctionType | InterfaceType |
3336 // SliceType | MapType | Channel_Type .
3337 func (p *Parser) typeOrNil() (e Expr) {
3338 if trace {
3339 defer p.trace("typeOrNil")()
3340 }
3341
3342 pos := p.pos()
3343 switch p.Tok {
3344 case Star:
3345 // ptrtype
3346 p.Next()
3347 return newIndirect(pos, p.type_())
3348
3349 case Arrow:
3350 // recvchantype
3351 p.Next()
3352 p.want(Chan)
3353 t := &ChanType{}
3354 t.pos = pos
3355 t.Dir = RecvOnly
3356 t.Elem = p.chanElem()
3357 return t
3358
3359 case Func:
3360 // fntype
3361 p.Next()
3362 _, t := p.funcType("function type")
3363 return t
3364
3365 case Lbrack:
3366 // '[' oexpr ']' ntype
3367 // '[' _DotDotDot ']' ntype
3368 p.Next()
3369 if p.got(Rbrack) {
3370 return p.sliceType(pos)
3371 }
3372 return p.arrayType(pos, nil)
3373
3374 case Chan:
3375 // _Chan non_recvchantype
3376 // _Chan _Comm ntype
3377 p.Next()
3378 t := &ChanType{}
3379 t.pos = pos
3380 if p.got(Arrow) {
3381 t.Dir = SendOnly
3382 }
3383 t.Elem = p.chanElem()
3384 return t
3385
3386 case Map:
3387 // _Map '[' ntype ']' ntype
3388 p.Next()
3389 p.want(Lbrack)
3390 t := &MapType{}
3391 t.pos = pos
3392 t.Key = p.type_()
3393 p.want(Rbrack)
3394 t.Value = p.type_()
3395 return t
3396
3397 case Struct:
3398 return p.structType()
3399
3400 case Interface:
3401 return p.interfaceType()
3402
3403 case NameType:
3404 return p.qualifiedName(nil)
3405
3406 case Lparen:
3407 p.Next()
3408 t := p.type_()
3409 p.want(Rparen)
3410 // The parser doesn't keep unnecessary parentheses.
3411 // Set the flag below to keep them, for testing
3412 // (see e.g. tests for go.dev/issue/68639).
3413 const keep_parens = false
3414 if keep_parens {
3415 px := &ParenExpr{}
3416 px.pos = pos
3417 px.X = t
3418 t = px
3419 }
3420 return t
3421 }
3422
3423 return nil
3424 }
3425
3426 func (p *Parser) typeInstance(typ Expr) (e Expr) {
3427 if trace {
3428 defer p.trace("typeInstance")()
3429 }
3430
3431 pos := p.pos()
3432 p.want(Lbrack)
3433 x := &IndexExpr{}
3434 x.pos = pos
3435 x.X = typ
3436 if p.Tok == Rbrack {
3437 p.syntaxError("expected type argument list")
3438 x.Index = p.badExpr()
3439 } else {
3440 x.Index, _ = p.typeList(true)
3441 }
3442 p.want(Rbrack)
3443 return x
3444 }
3445
3446 // If context != "", type parameters are not permitted.
3447 func (p *Parser) funcType(context string) ([]*Field, *FuncType) {
3448 if trace {
3449 defer p.trace("funcType")()
3450 }
3451
3452 typ := &FuncType{}
3453 typ.pos = p.pos()
3454
3455 var tparamList []*Field
3456 if p.got(Lbrack) {
3457 if context != "" {
3458 // accept but complain
3459 p.syntaxErrorAt(typ.pos, context | " must have no type parameters")
3460 }
3461 if p.Tok == Rbrack {
3462 p.syntaxError("empty type parameter list")
3463 p.Next()
3464 } else {
3465 tparamList = p.paramList(nil, nil, Rbrack, true, false)
3466 }
3467 }
3468
3469 p.want(Lparen)
3470 typ.ParamList = p.paramList(nil, nil, Rparen, false, true)
3471 typ.ResultList = p.funcResult()
3472
3473 return tparamList, typ
3474 }
3475
3476 // "[" has already been consumed, and pos is its position.
3477 // If len != nil it is the already consumed array length.
3478 func (p *Parser) arrayType(pos Pos, len Expr) (e Expr) {
3479 if trace {
3480 defer p.trace("arrayType")()
3481 }
3482
3483 if len == nil && !p.got(DotDotDot) {
3484 p.Xnest++
3485 len = p.expr()
3486 p.Xnest--
3487 }
3488 if p.Tok == Comma {
3489 // Trailing commas are accepted in type parameter
3490 // lists but not in array type declarations.
3491 // Accept for better error handling but complain.
3492 p.syntaxError("unexpected comma; expected ]")
3493 p.Next()
3494 }
3495 p.want(Rbrack)
3496 t := &ArrayType{}
3497 t.pos = pos
3498 t.Len = len
3499 t.Elem = p.type_()
3500 return t
3501 }
3502
3503 // "[" and "]" have already been consumed, and pos is the position of "[".
3504 func (p *Parser) sliceType(pos Pos) (e Expr) {
3505 t := &SliceType{}
3506 t.pos = pos
3507 t.Elem = p.type_()
3508 return t
3509 }
3510
3511 func (p *Parser) chanElem() (e Expr) {
3512 if trace {
3513 defer p.trace("chanElem")()
3514 }
3515
3516 typ := p.typeOrNil()
3517 if typ == nil {
3518 typ = p.badExpr()
3519 p.syntaxError("missing channel element type")
3520 // assume element type is simply absent - don't advance
3521 }
3522
3523 return typ
3524 }
3525
3526 // StructType = "struct" "{" { FieldDecl ";" } "}" .
3527 func (p *Parser) structType() (s *StructType) {
3528 if trace {
3529 defer p.trace("structType")()
3530 }
3531
3532 typ := &StructType{}
3533 typ.pos = p.pos()
3534
3535 p.want(Struct)
3536 p.want(Lbrace)
3537 p.list("struct type", Semi, Rbrace, func() bool {
3538 p.fieldDecl(typ)
3539 return false
3540 })
3541
3542 return typ
3543 }
3544
3545 // InterfaceType = "interface" "{" { ( MethodDecl | EmbeddedElem ) ";" } "}" .
3546 func (p *Parser) interfaceType() (i *InterfaceType) {
3547 if trace {
3548 defer p.trace("interfaceType")()
3549 }
3550
3551 typ := &InterfaceType{}
3552 typ.pos = p.pos()
3553
3554 p.want(Interface)
3555 p.want(Lbrace)
3556 p.list("interface type", Semi, Rbrace, func() bool {
3557 var f *Field
3558 if p.Tok == NameType {
3559 f = p.methodDecl()
3560 }
3561 if f == nil || f.Name == nil {
3562 f = p.embeddedElem(f)
3563 }
3564 typ.MethodList = append(typ.MethodList, f)
3565 return false
3566 })
3567
3568 return typ
3569 }
3570
3571 // Result = Parameters | Type .
3572 func (p *Parser) funcResult() (fs []*Field) {
3573 if trace {
3574 defer p.trace("funcResult")()
3575 }
3576
3577 if p.got(Lparen) {
3578 return p.paramList(nil, nil, Rparen, false, false)
3579 }
3580
3581 pos := p.pos()
3582 if typ := p.typeOrNil(); typ != nil {
3583 f := &Field{}
3584 f.pos = pos
3585 f.Type = typ
3586 return []*Field{f}
3587 }
3588
3589 return nil
3590 }
3591
3592 func (p *Parser) addField(styp *StructType, pos Pos, name *Name, typ Expr, tag *BasicLit) {
3593 if tag != nil {
3594 for i := len(styp.FieldList) - len(styp.TagList); i > 0; i-- {
3595 styp.TagList = append(styp.TagList, nil)
3596 }
3597 styp.TagList = append(styp.TagList, tag)
3598 }
3599
3600 f := &Field{}
3601 f.pos = pos
3602 f.Name = name
3603 f.Type = typ
3604 styp.FieldList = append(styp.FieldList, f)
3605
3606 if debug && tag != nil && len(styp.FieldList) != len(styp.TagList) {
3607 panic("inconsistent struct field list")
3608 }
3609 }
3610
3611 // FieldDecl = (IdentifierList Type | AnonymousField) [ Tag ] .
3612 // AnonymousField = [ "*" ] TypeName .
3613 // Tag = string_lit .
3614 func (p *Parser) fieldDecl(styp *StructType) {
3615 if trace {
3616 defer p.trace("fieldDecl")()
3617 }
3618
3619 pos := p.pos()
3620 switch p.Tok {
3621 case NameType:
3622 name := p.name()
3623 if p.Tok == Dot || p.Tok == Literal || p.Tok == Semi || p.Tok == Rbrace {
3624 // embedded type
3625 typ := p.qualifiedName(name)
3626 tag := p.oliteral()
3627 p.addField(styp, pos, nil, typ, tag)
3628 break
3629 }
3630
3631 // name1, name2, ... Type [ tag ]
3632 names := p.nameList(name)
3633 var typ Expr
3634
3635 // Careful dance: We don't know if we have an embedded instantiated
3636 // type T[P1, P2, ...] or a field T of array/slice type [P]E or []E.
3637 if len(names) == 1 && p.Tok == Lbrack {
3638 typ = p.arrayOrTArgs()
3639 if typ, ok := typ.(*IndexExpr); ok {
3640 // embedded type T[P1, P2, ...]
3641 typ.X = name // name == names[0]
3642 tag := p.oliteral()
3643 p.addField(styp, pos, nil, typ, tag)
3644 break
3645 }
3646 } else {
3647 // T P
3648 typ = p.type_()
3649 }
3650
3651 tag := p.oliteral()
3652
3653 for _, name := range names {
3654 p.addField(styp, name.Pos(), name, typ, tag)
3655 }
3656
3657 case Star:
3658 p.Next()
3659 var typ Expr
3660 if p.Tok == Lparen {
3661 // *(T)
3662 p.syntaxError("cannot parenthesize embedded type")
3663 p.Next()
3664 typ = p.qualifiedName(nil)
3665 p.got(Rparen) // no need to complain if missing
3666 } else {
3667 // *T
3668 typ = p.qualifiedName(nil)
3669 }
3670 tag := p.oliteral()
3671 p.addField(styp, pos, nil, newIndirect(pos, typ), tag)
3672
3673 case Lparen:
3674 p.syntaxError("cannot parenthesize embedded type")
3675 p.Next()
3676 var typ Expr
3677 if p.Tok == Star {
3678 // (*T)
3679 pos := p.pos()
3680 p.Next()
3681 typ = newIndirect(pos, p.qualifiedName(nil))
3682 } else {
3683 // (T)
3684 typ = p.qualifiedName(nil)
3685 }
3686 p.got(Rparen) // no need to complain if missing
3687 tag := p.oliteral()
3688 p.addField(styp, pos, nil, typ, tag)
3689
3690 default:
3691 p.syntaxError("expected field name or embedded type")
3692 p.advance(Semi, Rbrace)
3693 }
3694 }
3695
3696 func (p *Parser) arrayOrTArgs() (e Expr) {
3697 if trace {
3698 defer p.trace("arrayOrTArgs")()
3699 }
3700
3701 pos := p.pos()
3702 p.want(Lbrack)
3703 if p.got(Rbrack) {
3704 return p.sliceType(pos)
3705 }
3706
3707 // x [n]E or x[n,], x[n1, n2], ...
3708 n, comma := p.typeList(false)
3709 p.want(Rbrack)
3710 if !comma {
3711 if elem := p.typeOrNil(); elem != nil {
3712 // x [n]E
3713 t := &ArrayType{}
3714 t.pos = pos
3715 t.Len = n
3716 t.Elem = elem
3717 return t
3718 }
3719 }
3720
3721 // x[n,], x[n1, n2], ...
3722 t := &IndexExpr{}
3723 t.pos = pos
3724 // t.X will be filled in by caller
3725 t.Index = n
3726 return t
3727 }
3728
3729 func (p *Parser) oliteral() (b *BasicLit) {
3730 if p.Tok == Literal {
3731 b = &BasicLit{}
3732 b.pos = p.pos()
3733 b.Value = p.Lit
3734 b.Kind = p.Kind
3735 b.Bad = p.Bad
3736 p.Next()
3737 return b
3738 }
3739 return nil
3740 }
3741
3742 // MethodSpec = MethodName Signature | InterfaceTypeName .
3743 // MethodName = identifier .
3744 // InterfaceTypeName = TypeName .
3745 func (p *Parser) methodDecl() (f *Field) {
3746 if trace {
3747 defer p.trace("methodDecl")()
3748 }
3749
3750 f = &Field{}
3751 f.pos = p.pos()
3752 name := p.name()
3753
3754 const context = "interface method"
3755
3756 switch p.Tok {
3757 case Lparen:
3758 // method
3759 f.Name = name
3760 _, f.Type = p.funcType(context)
3761
3762 case Lbrack:
3763 // Careful dance: We don't know if we have a generic method m[T C](x T)
3764 // or an embedded instantiated type T[P1, P2] (we accept generic methods
3765 // for generality and robustness of parsing but complain with an error).
3766 pos := p.pos()
3767 p.Next()
3768
3769 // Empty type parameter or argument lists are not permitted.
3770 // Treat as if [] were absent.
3771 if p.Tok == Rbrack {
3772 // name[]
3773 pos := p.pos()
3774 p.Next()
3775 if p.Tok == Lparen {
3776 // name[](
3777 p.errorAt(pos, "empty type parameter list")
3778 f.Name = name
3779 _, f.Type = p.funcType(context)
3780 } else {
3781 p.errorAt(pos, "empty type argument list")
3782 f.Type = name
3783 }
3784 break
3785 }
3786
3787 // A type argument list looks like a parameter list with only
3788 // types. Parse a parameter list and decide afterwards.
3789 list := p.paramList(nil, nil, Rbrack, false, false)
3790 if len(list) == 0 {
3791 // The type parameter list is not [] but we got nothing
3792 // due to other errors (reported by paramList). Treat
3793 // as if [] were absent.
3794 if p.Tok == Lparen {
3795 f.Name = name
3796 _, f.Type = p.funcType(context)
3797 } else {
3798 f.Type = name
3799 }
3800 break
3801 }
3802
3803 // len(list) > 0
3804 if list[0].Name != nil {
3805 // generic method
3806 f.Name = name
3807 _, f.Type = p.funcType(context)
3808 p.errorAt(pos, "interface method must have no type parameters")
3809 break
3810 }
3811
3812 // embedded instantiated type
3813 t := &IndexExpr{}
3814 t.pos = pos
3815 t.X = name
3816 if len(list) == 1 {
3817 t.Index = list[0].Type
3818 } else {
3819 // len(list) > 1
3820 l := &ListExpr{}
3821 l.pos = list[0].Pos()
3822 l.ElemList = []Expr{:len(list)}
3823 for i := range list {
3824 l.ElemList[i] = list[i].Type
3825 }
3826 t.Index = l
3827 }
3828 f.Type = t
3829
3830 default:
3831 // embedded type
3832 f.Type = p.qualifiedName(name)
3833 }
3834
3835 return f
3836 }
3837
3838 // EmbeddedElem = MethodSpec | EmbeddedTerm { "|" EmbeddedTerm } .
3839 func (p *Parser) embeddedElem(f *Field) (fv *Field) {
3840 if trace {
3841 defer p.trace("embeddedElem")()
3842 }
3843
3844 if f == nil {
3845 f = &Field{}
3846 f.pos = p.pos()
3847 f.Type = p.embeddedTerm()
3848 }
3849
3850 for p.Tok == OperatorType && p.Op == Or {
3851 t := &Operation{}
3852 t.pos = p.pos()
3853 t.Op = Or
3854 p.Next()
3855 t.X = f.Type
3856 t.Y = p.embeddedTerm()
3857 f.Type = t
3858 }
3859
3860 return f
3861 }
3862
3863 // EmbeddedTerm = [ "~" ] Type .
3864 func (p *Parser) embeddedTerm() (e Expr) {
3865 if trace {
3866 defer p.trace("embeddedTerm")()
3867 }
3868
3869 if p.Tok == OperatorType && p.Op == Tilde {
3870 t := &Operation{}
3871 t.pos = p.pos()
3872 t.Op = Tilde
3873 p.Next()
3874 t.X = p.type_()
3875 return t
3876 }
3877
3878 t := p.typeOrNil()
3879 if t == nil {
3880 t = p.badExpr()
3881 p.syntaxError("expected ~ term or type")
3882 p.advance(OperatorType, Semi, Rparen, Rbrack, Rbrace)
3883 }
3884
3885 return t
3886 }
3887
3888 // ParameterDecl = [ IdentifierList ] [ "..." ] Type .
3889 func (p *Parser) paramDeclOrNil(name *Name, follow Token) (f *Field) {
3890 if trace {
3891 defer p.trace("paramDeclOrNil")()
3892 }
3893
3894 // type set notation is ok in type parameter lists
3895 typeSetsOk := follow == Rbrack
3896
3897 pos := p.pos()
3898 if name != nil {
3899 pos = name.pos
3900 } else if typeSetsOk && p.Tok == OperatorType && p.Op == Tilde {
3901 // "~" ...
3902 return p.embeddedElem(nil)
3903 }
3904
3905 f = &Field{}
3906 f.pos = pos
3907
3908 if p.Tok == NameType || name != nil {
3909 // name
3910 if name == nil {
3911 name = p.name()
3912 }
3913
3914 if p.Tok == Lbrack {
3915 // name "[" ...
3916 f.Type = p.arrayOrTArgs()
3917 if typ, ok := f.Type.(*IndexExpr); ok {
3918 // name "[" ... "]"
3919 typ.X = name
3920 } else {
3921 // name "[" n "]" E
3922 f.Name = name
3923 }
3924 if typeSetsOk && p.Tok == OperatorType && p.Op == Or {
3925 // name "[" ... "]" "|" ...
3926 // name "[" n "]" E "|" ...
3927 f = p.embeddedElem(f)
3928 }
3929 return f
3930 }
3931
3932 if p.Tok == Dot {
3933 // name "." ...
3934 f.Type = p.qualifiedName(name)
3935 if typeSetsOk && p.Tok == OperatorType && p.Op == Or {
3936 // name "." name "|" ...
3937 f = p.embeddedElem(f)
3938 }
3939 return f
3940 }
3941
3942 if typeSetsOk && p.Tok == OperatorType && p.Op == Or {
3943 // name "|" ...
3944 f.Type = name
3945 return p.embeddedElem(f)
3946 }
3947
3948 f.Name = name
3949 }
3950
3951 if p.Tok == DotDotDot {
3952 // [name] "..." ...
3953 t := &DotsType{}
3954 t.pos = p.pos()
3955 p.Next()
3956 t.Elem = p.typeOrNil()
3957 if t.Elem == nil {
3958 f.Type = p.badExpr()
3959 p.syntaxError("... is missing type")
3960 } else {
3961 f.Type = t
3962 }
3963 return f
3964 }
3965
3966 if typeSetsOk && p.Tok == OperatorType && p.Op == Tilde {
3967 // [name] "~" ...
3968 f.Type = p.embeddedElem(nil).Type
3969 return f
3970 }
3971
3972 f.Type = p.typeOrNil()
3973 if typeSetsOk && p.Tok == OperatorType && p.Op == Or && f.Type != nil {
3974 // [name] type "|"
3975 f = p.embeddedElem(f)
3976 }
3977 if f.Name != nil || f.Type != nil {
3978 return f
3979 }
3980
3981 p.syntaxError("expected " | tokstring(follow))
3982 p.advance(Comma, follow)
3983 return nil
3984 }
3985
3986 // Parameters = "(" [ ParameterList [ "," ] ] ")" .
3987 // ParameterList = ParameterDecl { "," ParameterDecl } .
3988 // "(" or "[" has already been consumed.
3989 // If name != nil, it is the first name after "(" or "[".
3990 // If typ != nil, name must be != nil, and (name, typ) is the first field in the list.
3991 // In the result list, either all fields have a name, or no field has a name.
3992 func (p *Parser) paramList(name *Name, typ Expr, close Token, requireNames, dddok bool) (list []*Field) {
3993 if trace {
3994 defer p.trace("paramList")()
3995 }
3996
3997 // p.list won't invoke its function argument if we're at the end of the
3998 // parameter list. If we have a complete field, handle this case here.
3999 if name != nil && typ != nil && p.Tok == close {
4000 p.Next()
4001 par := &Field{}
4002 par.pos = name.pos
4003 par.Name = name
4004 par.Type = typ
4005 return []*Field{par}
4006 }
4007
4008 var named int32 // number of parameters that have an explicit name and type
4009 var typed int32 // number of parameters that have an explicit type
4010 end := p.list("parameter list", Comma, close, func() bool {
4011 var par *Field
4012 if typ != nil {
4013 if debug && name == nil {
4014 panic("initial type provided without name")
4015 }
4016 par = &Field{}
4017 par.pos = name.pos
4018 par.Name = name
4019 par.Type = typ
4020 } else {
4021 par = p.paramDeclOrNil(name, close)
4022 }
4023 name = nil // 1st name was consumed if present
4024 typ = nil // 1st type was consumed if present
4025 if par != nil {
4026 if debug && par.Name == nil && par.Type == nil {
4027 panic("parameter without name or type")
4028 }
4029 if par.Name != nil && par.Type != nil {
4030 named++
4031 }
4032 if par.Type != nil {
4033 typed++
4034 }
4035 list = append(list, par)
4036 }
4037 return false
4038 })
4039
4040 if len(list) == 0 {
4041 return
4042 }
4043
4044 // distribute parameter types (len(list) > 0)
4045 if named == 0 && !requireNames {
4046 // all unnamed and we're not in a type parameter list => found names are named types
4047 for _, par := range list {
4048 if typ := par.Name; typ != nil {
4049 par.Type = typ
4050 par.Name = nil
4051 }
4052 }
4053 } else if named != len(list) {
4054 // some named or we're in a type parameter list => all must be named
4055 var errPos Pos // left-most error position (or unknown)
4056 var typ Expr // current type (from right to left)
4057 for i := len(list) - 1; i >= 0; i-- {
4058 par := list[i]
4059 if par.Type != nil {
4060 typ = par.Type
4061 if par.Name == nil {
4062 errPos = StartPos(typ)
4063 par.Name = NewName(errPos, "_")
4064 }
4065 } else if typ != nil {
4066 par.Type = typ
4067 } else {
4068 // par.Type == nil && typ == nil => we only have a par.Name
4069 errPos = par.Name.Pos()
4070 t := p.badExpr()
4071 t.pos = errPos // correct position
4072 par.Type = t
4073 }
4074 }
4075 if errPos.IsKnown() {
4076 // Not all parameters are named because named != len(list).
4077 // If named == typed, there must be parameters that have no types.
4078 // They must be at the end of the parameter list, otherwise types
4079 // would have been filled in by the right-to-left sweep above and
4080 // there would be no error.
4081 // If requireNames is set, the parameter list is a type parameter
4082 // list.
4083 var msg string
4084 if named == typed {
4085 errPos = end // position error at closing token ) or ]
4086 if requireNames {
4087 msg = "missing type constraint"
4088 } else {
4089 msg = "missing parameter type"
4090 }
4091 } else {
4092 if requireNames {
4093 msg = "missing type parameter name"
4094 // go.dev/issue/60812
4095 if len(list) == 1 {
4096 msg = msg | " or invalid array length"
4097 }
4098 } else {
4099 msg = "missing parameter name"
4100 }
4101 }
4102 p.syntaxErrorAt(errPos, msg)
4103 }
4104 }
4105
4106 // check use of ... - DISABLED for gen1 debugging
4107
4108 return
4109 }
4110
4111 func (p *Parser) badExpr() (b *BadExpr) {
4112 b = &BadExpr{}
4113 b.pos = p.pos()
4114 return b
4115 }
4116
4117 // ----------------------------------------------------------------------------
4118 // Statements
4119
4120 // SimpleStmt = EmptyStmt | ExpressionStmt | SendStmt | IncDecStmt | Assignment | ShortVarDecl .
4121 func (p *Parser) simpleStmt(lhs Expr, keyword Token) (s SimpleStmt) {
4122 if trace {
4123 defer p.trace("simpleStmt")()
4124 }
4125
4126 if keyword == For && p.Tok == Range {
4127 // _Range expr
4128 if debug && lhs != nil {
4129 panic("invalid call of simpleStmt")
4130 }
4131 return p.newRangeClause(nil, false)
4132 }
4133
4134 if lhs == nil {
4135 lhs = p.exprList()
4136 }
4137
4138 if _, ok := lhs.(*ListExpr); !ok && p.Tok != Assign && p.Tok != Define {
4139 // expr
4140 pos := p.pos()
4141 switch p.Tok {
4142 case AssignOp:
4143 // lhs op= rhs
4144 op := p.Op
4145 p.Next()
4146 return p.newAssignStmt(pos, op, lhs, p.expr())
4147
4148 case IncOp:
4149 // lhs++ or lhs--
4150 op := p.Op
4151 p.Next()
4152 return p.newAssignStmt(pos, op, lhs, nil)
4153
4154 case Arrow:
4155 // lhs <- rhs
4156 ss := &SendStmt{}
4157 ss.pos = pos
4158 p.Next()
4159 ss.Chan = lhs
4160 ss.Value = p.expr()
4161 return ss
4162
4163 default:
4164 // expr
4165 es := &ExprStmt{}
4166 es.pos = lhs.Pos()
4167 es.X = lhs
4168 return es
4169 }
4170 }
4171
4172 // expr_list
4173 switch p.Tok {
4174 case Assign, Define:
4175 pos := p.pos()
4176 var op Operator
4177 if p.Tok == Define {
4178 op = Def
4179 }
4180 p.Next()
4181
4182 if keyword == For && p.Tok == Range {
4183 // expr_list op= _Range expr
4184 return p.newRangeClause(lhs, op == Def)
4185 }
4186
4187 // expr_list op= expr_list
4188 rhs := p.exprList()
4189
4190 if x, ok := rhs.(*TypeSwitchGuard); ok && keyword == Switch && op == Def {
4191 if lhs, ok := lhs.(*Name); ok {
4192 // switch … lhs := rhs.(type)
4193 x.Lhs = lhs
4194 es := &ExprStmt{}
4195 es.pos = x.Pos()
4196 es.X = x
4197 return es
4198 }
4199 }
4200
4201 return p.newAssignStmt(pos, op, lhs, rhs)
4202
4203 default:
4204 p.syntaxError("expected := or = or comma")
4205 p.advance(Semi, Rbrace)
4206 if x, ok := lhs.(*ListExpr); ok {
4207 lhs = x.ElemList[0]
4208 }
4209 es := &ExprStmt{}
4210 es.pos = lhs.Pos()
4211 es.X = lhs
4212 return es
4213 }
4214 }
4215
4216 func (p *Parser) newRangeClause(lhs Expr, def bool) (r *RangeClause) {
4217 r = &RangeClause{}
4218 r.pos = p.pos()
4219 p.Next() // consume _Range
4220 r.Lhs = lhs
4221 r.Def = def
4222 r.X = p.expr()
4223 return r
4224 }
4225
4226 func (p *Parser) newAssignStmt(pos Pos, op Operator, lhs, rhs Expr) (a *AssignStmt) {
4227 a = &AssignStmt{}
4228 a.pos = pos
4229 a.Op = op
4230 a.Lhs = lhs
4231 a.Rhs = rhs
4232 return a
4233 }
4234
4235 func (p *Parser) labeledStmtOrNil(label *Name) Stmt {
4236 if trace {
4237 defer p.trace("labeledStmt")()
4238 }
4239
4240 s := &LabeledStmt{}
4241 s.pos = p.pos()
4242 s.Label = label
4243
4244 p.want(Colon)
4245
4246 if p.Tok == Rbrace {
4247 e := &EmptyStmt{}
4248 e.pos = p.pos()
4249 s.Stmt = e
4250 return s
4251 }
4252
4253 s.Stmt = p.stmtOrNil()
4254 if s.Stmt != nil {
4255 return s
4256 }
4257
4258 p.syntaxErrorAt(s.pos, "missing statement after label")
4259 return nil
4260 }
4261
4262 // context must be a non-empty string unless we know that p.tok == _Lbrace.
4263 func (p *Parser) blockStmt(context string) (b *BlockStmt) {
4264 if trace {
4265 defer p.trace("blockStmt")()
4266 }
4267
4268 s := &BlockStmt{}
4269 s.pos = p.pos()
4270
4271 // people coming from C may forget that braces are mandatory in Go
4272 if !p.got(Lbrace) {
4273 p.syntaxError("expected { after " | context)
4274 p.advance(NameType, Rbrace)
4275 s.Rbrace = p.pos() // in case we found "}"
4276 if p.got(Rbrace) {
4277 return s
4278 }
4279 }
4280
4281 s.List = p.stmtList()
4282 s.Rbrace = p.pos()
4283 p.want(Rbrace)
4284
4285 return s
4286 }
4287
4288 func (p *Parser) declStmt(f func(*Group) Decl) (d *DeclStmt) {
4289 if trace {
4290 defer p.trace("declStmt")()
4291 }
4292
4293 s := &DeclStmt{}
4294 s.pos = p.pos()
4295
4296 p.Next() // _Const, _Type, or _Var
4297 s.DeclList = p.appendGroup(nil, f)
4298
4299 return s
4300 }
4301
4302 func (p *Parser) forStmt() Stmt {
4303 if trace {
4304 defer p.trace("forStmt")()
4305 }
4306
4307 s := &ForStmt{}
4308 s.pos = p.pos()
4309
4310 s.Init, s.Cond, s.Post = p.header(For)
4311 s.Body = p.blockStmt("for clause")
4312
4313 return s
4314 }
4315
4316 func (p *Parser) header(keyword Token) (init SimpleStmt, cond Expr, post SimpleStmt) {
4317 p.want(keyword)
4318
4319 if p.Tok == Lbrace {
4320 if keyword == If {
4321 p.syntaxError("missing condition in if statement")
4322 cond = p.badExpr()
4323 }
4324 return
4325 }
4326 // p.tok != _Lbrace
4327
4328 outer := p.Xnest
4329 p.Xnest = -1
4330
4331 if p.Tok != Semi {
4332 // accept potential varDecl but complain
4333 if p.got(Var) {
4334 p.syntaxError("var declaration not allowed in " | keyword.String() | " initializer")
4335 }
4336 init = p.simpleStmt(nil, keyword)
4337 // If we have a range clause, we are done (can only happen for keyword == _For).
4338 if _, ok := init.(*RangeClause); ok {
4339 p.Xnest = outer
4340 return
4341 }
4342 }
4343
4344 var condStmt SimpleStmt
4345 var semi struct {
4346 pos Pos
4347 lit string // valid if pos.IsKnown()
4348 }
4349 if p.Tok != Lbrace {
4350 if p.Tok == Semi {
4351 semi.pos = p.pos()
4352 semi.lit = p.Lit
4353 p.Next()
4354 } else {
4355 // asking for a '{' rather than a ';' here leads to a better error message
4356 p.want(Lbrace)
4357 if p.Tok != Lbrace {
4358 p.advance(Lbrace, Rbrace) // for better synchronization (e.g., go.dev/issue/22581)
4359 }
4360 }
4361 if keyword == For {
4362 if p.Tok != Semi {
4363 if p.Tok == Lbrace {
4364 p.syntaxError("expected for loop condition")
4365 goto done
4366 }
4367 condStmt = p.simpleStmt(nil, 0 /* range not permitted */)
4368 }
4369 p.want(Semi)
4370 if p.Tok != Lbrace {
4371 post = p.simpleStmt(nil, 0 /* range not permitted */)
4372 if a, _ := post.(*AssignStmt); a != nil && a.Op == Def {
4373 p.syntaxErrorAt(a.Pos(), "cannot declare in post statement of for loop")
4374 }
4375 }
4376 } else if p.Tok != Lbrace {
4377 condStmt = p.simpleStmt(nil, keyword)
4378 }
4379 } else {
4380 condStmt = init
4381 init = nil
4382 }
4383
4384 done:
4385 // unpack condStmt
4386 switch s := condStmt.(type) {
4387 case nil:
4388 if keyword == If && semi.pos.IsKnown() {
4389 if semi.lit != "semicolon" {
4390 p.syntaxErrorAt(semi.pos, "unexpected " | semi.lit | ", expected { after if clause")
4391 } else {
4392 p.syntaxErrorAt(semi.pos, "missing condition in if statement")
4393 }
4394 b := &BadExpr{}
4395 b.pos = semi.pos
4396 cond = b
4397 }
4398 case *ExprStmt:
4399 cond = s.X
4400 default:
4401 // A common syntax error is to write '=' instead of '==',
4402 // which turns an expression into an assignment. Provide
4403 // a more explicit error message in that case to prevent
4404 // further confusion.
4405 var str string
4406 if as, ok := s.(*AssignStmt); ok && as.Op == 0 {
4407 // Emphasize complex Lhs and Rhs of assignment with parentheses to highlight '='.
4408 str = "assignment " | emphasize(as.Lhs) | " = " | emphasize(as.Rhs)
4409 } else {
4410 str = String(s)
4411 }
4412 p.syntaxErrorAt(s.Pos(), "cannot use " | str | " as value")
4413 }
4414
4415 p.Xnest = outer
4416 return
4417 }
4418
4419 // emphasize returns a string representation of x, with (top-level)
4420 // binary expressions emphasized by enclosing them in parentheses.
4421 func emphasize(x Expr) (s string) {
4422 return "<expr>"
4423 }
4424
4425 func (p *Parser) ifStmt() (i *IfStmt) {
4426 if trace {
4427 defer p.trace("ifStmt")()
4428 }
4429
4430 s := &IfStmt{}
4431 s.pos = p.pos()
4432
4433 s.Init, s.Cond, _ = p.header(If)
4434 s.Then = p.blockStmt("if clause")
4435
4436 if p.got(Else) {
4437 switch p.Tok {
4438 case If:
4439 s.Else = p.ifStmt()
4440 case Lbrace:
4441 s.Else = p.blockStmt("")
4442 default:
4443 p.syntaxError("else must be followed by if or statement block")
4444 p.advance(NameType, Rbrace)
4445 }
4446 }
4447
4448 return s
4449 }
4450
4451 func (p *Parser) switchStmt() (s *SwitchStmt) {
4452 if trace {
4453 defer p.trace("switchStmt")()
4454 }
4455
4456 s = &SwitchStmt{}
4457 s.pos = p.pos()
4458
4459 s.Init, s.Tag, _ = p.header(Switch)
4460
4461 if !p.got(Lbrace) {
4462 p.syntaxError("missing { after switch clause")
4463 p.advance(Case, Default, Rbrace)
4464 }
4465 for p.Tok != EOF && p.Tok != Rbrace {
4466 s.Body = append(s.Body, p.caseClause())
4467 }
4468 s.Rbrace = p.pos()
4469 p.want(Rbrace)
4470
4471 return s
4472 }
4473
4474 func (p *Parser) selectStmt() (s *SelectStmt) {
4475 if trace {
4476 defer p.trace("selectStmt")()
4477 }
4478
4479 s = &SelectStmt{}
4480 s.pos = p.pos()
4481
4482 p.want(Select)
4483 if !p.got(Lbrace) {
4484 p.syntaxError("missing { after select clause")
4485 p.advance(Case, Default, Rbrace)
4486 }
4487 for p.Tok != EOF && p.Tok != Rbrace {
4488 s.Body = append(s.Body, p.commClause())
4489 }
4490 s.Rbrace = p.pos()
4491 p.want(Rbrace)
4492
4493 return s
4494 }
4495
4496 func (p *Parser) caseClause() (c *CaseClause) {
4497 if trace {
4498 defer p.trace("caseClause")()
4499 }
4500
4501 c = &CaseClause{}
4502 c.pos = p.pos()
4503
4504 switch p.Tok {
4505 case Case:
4506 p.Next()
4507 c.Cases = p.exprList()
4508
4509 case Default:
4510 p.Next()
4511
4512 default:
4513 p.syntaxError("expected case or default or }")
4514 p.advance(Colon, Case, Default, Rbrace)
4515 }
4516
4517 c.Colon = p.pos()
4518 p.want(Colon)
4519 c.Body = p.stmtList()
4520
4521 return c
4522 }
4523
4524 func (p *Parser) commClause() (c *CommClause) {
4525 if trace {
4526 defer p.trace("commClause")()
4527 }
4528
4529 c = &CommClause{}
4530 c.pos = p.pos()
4531
4532 switch p.Tok {
4533 case Case:
4534 p.Next()
4535 c.Comm = p.simpleStmt(nil, 0)
4536
4537 // The syntax restricts the possible simple statements here to:
4538 //
4539 // lhs <- x (send statement)
4540 // <-x
4541 // lhs = <-x
4542 // lhs := <-x
4543 //
4544 // All these (and more) are recognized by simpleStmt and invalid
4545 // syntax trees are flagged later, during type checking.
4546
4547 case Default:
4548 p.Next()
4549
4550 default:
4551 p.syntaxError("expected case or default or }")
4552 p.advance(Colon, Case, Default, Rbrace)
4553 }
4554
4555 c.Colon = p.pos()
4556 p.want(Colon)
4557 c.Body = p.stmtList()
4558
4559 return c
4560 }
4561
4562 // stmtOrNil parses a statement if one is present, or else returns nil.
4563 //
4564 // Statement =
4565 // Declaration | LabeledStmt | SimpleStmt |
4566 // GoStmt | ReturnStmt | BreakStmt | ContinueStmt | GotoStmt |
4567 // FallthroughStmt | Block | IfStmt | SwitchStmt | SelectStmt | ForStmt |
4568 // DeferStmt .
4569 func (p *Parser) stmtOrNil() (s Stmt) {
4570 if trace {
4571 defer p.trace("stmt " | p.Tok.String())()
4572 }
4573
4574 // Most statements (assignments) start with an identifier;
4575 // look for it first before doing anything more expensive.
4576 if p.Tok == NameType {
4577 p.clearPragma()
4578 lhs := p.exprList()
4579 if label, ok := lhs.(*Name); ok && p.Tok == Colon {
4580 return p.labeledStmtOrNil(label)
4581 }
4582 return p.simpleStmt(lhs, 0)
4583 }
4584
4585 switch p.Tok {
4586 case Var:
4587 return p.declStmt(p.varDecl)
4588
4589 case Const:
4590 return p.declStmt(p.constDecl)
4591
4592 case TypeType:
4593 return p.declStmt(p.typeDecl)
4594 }
4595
4596 p.clearPragma()
4597
4598 switch p.Tok {
4599 case Lbrace:
4600 return p.blockStmt("")
4601
4602 case OperatorType, Star:
4603 switch p.Op {
4604 case Add, Sub, Mul, And, Xor, Not:
4605 return p.simpleStmt(nil, 0) // unary operators
4606 }
4607
4608 case Literal, Func, Lparen, // operands
4609 Lbrack, Struct, Map, Chan, Interface, // composite types
4610 Arrow: // receive operator
4611 return p.simpleStmt(nil, 0)
4612
4613 case For:
4614 return p.forStmt()
4615
4616 case Switch:
4617 return p.switchStmt()
4618
4619 case Select:
4620 return p.selectStmt()
4621
4622 case If:
4623 return p.ifStmt()
4624
4625 case Fallthrough:
4626 b := &BranchStmt{}
4627 b.pos = p.pos()
4628 p.Next()
4629 b.Tok = Fallthrough
4630 return b
4631
4632 case Break, Continue:
4633 b := &BranchStmt{}
4634 b.pos = p.pos()
4635 b.Tok = p.Tok
4636 p.Next()
4637 if p.Tok == NameType {
4638 b.Label = p.name()
4639 }
4640 return b
4641
4642 case Go, Defer:
4643 return p.callStmt()
4644
4645 case Goto:
4646 b := &BranchStmt{}
4647 b.pos = p.pos()
4648 b.Tok = Goto
4649 p.Next()
4650 b.Label = p.name()
4651 return b
4652
4653 case Return:
4654 r := &ReturnStmt{}
4655 r.pos = p.pos()
4656 p.Next()
4657 if p.Tok != Semi && p.Tok != Rbrace {
4658 r.Results = p.exprList()
4659 }
4660 return r
4661
4662 case Semi:
4663 e := &EmptyStmt{}
4664 e.pos = p.pos()
4665 return e
4666 }
4667
4668 return nil
4669 }
4670
4671 // StatementList = { Statement ";" } .
4672 func (p *Parser) stmtList() (l []Stmt) {
4673 if trace {
4674 defer p.trace("stmtList")()
4675 }
4676
4677 for p.Tok != EOF && p.Tok != Rbrace && p.Tok != Case && p.Tok != Default {
4678 s := p.stmtOrNil()
4679 p.clearPragma()
4680 if s == nil {
4681 break
4682 }
4683 l = append(l, s)
4684 // ";" is optional before "}"
4685 if !p.got(Semi) && p.Tok != Rbrace {
4686 p.syntaxError("at end of statement")
4687 p.advance(Semi, Rbrace, Case, Default)
4688 p.got(Semi) // avoid spurious empty statement
4689 }
4690 }
4691 return
4692 }
4693
4694 // argList parses a possibly empty, comma-separated list of arguments,
4695 // optionally followed by a comma (if not empty), and closed by ")".
4696 // The last argument may be followed by "...".
4697 //
4698 // argList = [ arg { "," arg } [ "..." ] [ "," ] ] ")" .
4699 func (p *Parser) argList() (list []Expr, hasDots bool) {
4700 if trace {
4701 defer p.trace("argList")()
4702 }
4703
4704 p.Xnest++
4705 p.list("argument list", Comma, Rparen, func() bool {
4706 list = append(list, p.expr())
4707 hasDots = p.got(DotDotDot)
4708 return hasDots
4709 })
4710 p.Xnest--
4711
4712 return
4713 }
4714
4715 // ----------------------------------------------------------------------------
4716 // Common productions
4717
4718 func (p *Parser) name() (n *Name) {
4719 // no tracing to avoid overly verbose output
4720
4721 if p.Tok == NameType {
4722 n = NewName(p.pos(), p.Lit)
4723 p.Next()
4724 return n
4725 }
4726
4727 n = NewName(p.pos(), "_")
4728 p.syntaxError("expected name")
4729 p.advance()
4730 return n
4731 }
4732
4733 // IdentifierList = identifier { "," identifier } .
4734 // The first name must be provided.
4735 func (p *Parser) nameList(First *Name) (ns []*Name) {
4736 if trace {
4737 defer p.trace("nameList")()
4738 }
4739
4740 if debug && First == nil {
4741 panic("first name not provided")
4742 }
4743
4744 l := []*Name{First}
4745 for p.got(Comma) {
4746 l = append(l, p.name())
4747 }
4748
4749 return l
4750 }
4751
4752 // The first name may be provided, or nil.
4753 func (p *Parser) qualifiedName(name *Name) (e Expr) {
4754 if trace {
4755 defer p.trace("qualifiedName")()
4756 }
4757
4758 var x Expr
4759 switch {
4760 case name != nil:
4761 x = name
4762 case p.Tok == NameType:
4763 x = p.name()
4764 default:
4765 x = NewName(p.pos(), "_")
4766 p.syntaxError("expected name")
4767 p.advance(Dot, Semi, Rbrace)
4768 }
4769
4770 if p.Tok == Dot {
4771 s := &SelectorExpr{}
4772 s.pos = p.pos()
4773 p.Next()
4774 s.X = x
4775 s.Sel = p.name()
4776 x = s
4777 }
4778
4779 if p.Tok == Lbrack {
4780 x = p.typeInstance(x)
4781 }
4782
4783 return x
4784 }
4785
4786 // ExpressionList = Expression { "," Expression } .
4787 func (p *Parser) exprList() (e Expr) {
4788 if trace {
4789 defer p.trace("exprList")()
4790 }
4791
4792 x := p.expr()
4793 if p.got(Comma) {
4794 list := []Expr{x, p.expr()}
4795 for p.got(Comma) {
4796 list = append(list, p.expr())
4797 }
4798 t := &ListExpr{}
4799 t.pos = x.Pos()
4800 t.ElemList = list
4801 x = t
4802 }
4803 return x
4804 }
4805
4806 // typeList parses a non-empty, comma-separated list of types,
4807 // optionally followed by a comma. If strict is set to false,
4808 // the first element may also be a (non-type) expression.
4809 // If there is more than one argument, the result is a *ListExpr.
4810 // The comma result indicates whether there was a (separating or
4811 // trailing) comma.
4812 //
4813 // typeList = arg { "," arg } [ "," ] .
4814 func (p *Parser) typeList(strict bool) (x Expr, comma bool) {
4815 if trace {
4816 defer p.trace("typeList")()
4817 }
4818
4819 p.Xnest++
4820 if strict {
4821 x = p.type_()
4822 } else {
4823 x = p.expr()
4824 }
4825 if p.got(Comma) {
4826 comma = true
4827 if t := p.typeOrNil(); t != nil {
4828 list := []Expr{x, t}
4829 for p.got(Comma) {
4830 if t = p.typeOrNil(); t == nil {
4831 break
4832 }
4833 list = append(list, t)
4834 }
4835 l := &ListExpr{}
4836 l.pos = x.Pos() // == list[0].Pos()
4837 l.ElemList = list
4838 x = l
4839 }
4840 }
4841 p.Xnest--
4842 return
4843 }
4844
4845 // Unparen returns e with any enclosing parentheses stripped.
4846 func Unparen(x Expr) (e Expr) {
4847 for {
4848 p, ok := x.(*ParenExpr)
4849 if !ok {
4850 break
4851 }
4852 x = p.X
4853 }
4854 return x
4855 }
4856
4857 // UnpackListExpr unpacks a *ListExpr into a []Expr.
4858 func UnpackListExpr(x Expr) (es []Expr) {
4859 switch x := x.(type) {
4860 case nil:
4861 return nil
4862 case *ListExpr:
4863 return x.ElemList
4864 default:
4865 return []Expr{x}
4866 }
4867 }
4868
4869
4870 func parseSource(name string, src []byte) (f *File) {
4871 errh := func(err error) {}
4872 var p Parser
4873 p.initBytes(NewFileBase(name), src, errh, nil, 0)
4874 p.Next()
4875 return p.fileOrNil()
4876 }
4877
4878 func bytesHasPrefix(s, prefix string) (ok bool) {
4879 if len(prefix) > len(s) { return false }
4880 return s[:len(prefix)] == prefix
4881 }
4882
4883 func bytesIndexByte(s string, c byte) (n int32) {
4884 for i := int32(0); i < int32(len(s)); i++ {
4885 if s[i] == c { return i }
4886 }
4887 return -1
4888 }
4889
4890 func bytesLastIndexByte(s string, c byte) (n int32) {
4891 for i := int32(len(s)) - 1; i >= 0; i-- {
4892 if s[i] == c { return i }
4893 }
4894 return -1
4895 }
4896