...

Source file src/go/parser/parser.go

Documentation: go/parser

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

View as plain text