github.com/mattn/go@v0.0.0-20171011075504-07f7db3ea99f/src/cmd/compile/internal/syntax/parser.go (about)

     1  // Copyright 2016 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 syntax
     6  
     7  import (
     8  	"cmd/internal/src"
     9  	"fmt"
    10  	"io"
    11  	"strconv"
    12  	"strings"
    13  )
    14  
    15  const debug = false
    16  const trace = false
    17  
    18  type parser struct {
    19  	base  *src.PosBase
    20  	errh  ErrorHandler
    21  	fileh FilenameHandler
    22  	mode  Mode
    23  	scanner
    24  
    25  	first  error  // first error encountered
    26  	errcnt int    // number of errors encountered
    27  	pragma Pragma // pragma flags
    28  
    29  	fnest  int    // function nesting level (for error handling)
    30  	xnest  int    // expression nesting level (for complit ambiguity resolution)
    31  	indent []byte // tracing support
    32  }
    33  
    34  func (p *parser) init(base *src.PosBase, r io.Reader, errh ErrorHandler, pragh PragmaHandler, fileh FilenameHandler, mode Mode) {
    35  	p.base = base
    36  	p.errh = errh
    37  	p.fileh = fileh
    38  	p.mode = mode
    39  	p.scanner.init(
    40  		r,
    41  		// Error and pragma handlers for scanner.
    42  		// Because the (line, col) positions passed to these
    43  		// handlers are always at or after the current reading
    44  		// position, it is save to use the most recent position
    45  		// base to compute the corresponding Pos value.
    46  		func(line, col uint, msg string) {
    47  			p.error_at(p.pos_at(line, col), msg)
    48  		},
    49  		func(line, col uint, text string) {
    50  			if strings.HasPrefix(text, "line ") {
    51  				p.updateBase(line, col+5, text[5:])
    52  				return
    53  			}
    54  			if pragh != nil {
    55  				p.pragma |= pragh(p.pos_at(line, col), text)
    56  			}
    57  		},
    58  	)
    59  
    60  	p.first = nil
    61  	p.errcnt = 0
    62  	p.pragma = 0
    63  
    64  	p.fnest = 0
    65  	p.xnest = 0
    66  	p.indent = nil
    67  }
    68  
    69  const lineMax = 1<<24 - 1 // TODO(gri) this limit is defined for src.Pos - fix
    70  
    71  func (p *parser) updateBase(line, col uint, text string) {
    72  	// Want to use LastIndexByte below but it's not defined in Go1.4 and bootstrap fails.
    73  	i := strings.LastIndex(text, ":") // look from right (Windows filenames may contain ':')
    74  	if i < 0 {
    75  		return // ignore (not a line directive)
    76  	}
    77  	nstr := text[i+1:]
    78  	n, err := strconv.Atoi(nstr)
    79  	if err != nil || n <= 0 || n > lineMax {
    80  		p.error_at(p.pos_at(line, col+uint(i+1)), "invalid line number: "+nstr)
    81  		return
    82  	}
    83  	absFile := text[:i]
    84  	if p.fileh != nil {
    85  		absFile = p.fileh(absFile)
    86  	}
    87  	p.base = src.NewLinePragmaBase(src.MakePos(p.base.Pos().Base(), line, col), absFile, uint(n))
    88  }
    89  
    90  func (p *parser) got(tok token) bool {
    91  	if p.tok == tok {
    92  		p.next()
    93  		return true
    94  	}
    95  	return false
    96  }
    97  
    98  func (p *parser) want(tok token) {
    99  	if !p.got(tok) {
   100  		p.syntax_error("expecting " + tok.String())
   101  		p.advance()
   102  	}
   103  }
   104  
   105  // ----------------------------------------------------------------------------
   106  // Error handling
   107  
   108  // pos_at returns the Pos value for (line, col) and the current position base.
   109  func (p *parser) pos_at(line, col uint) src.Pos {
   110  	return src.MakePos(p.base, line, col)
   111  }
   112  
   113  // error reports an error at the given position.
   114  func (p *parser) error_at(pos src.Pos, msg string) {
   115  	err := Error{pos, msg}
   116  	if p.first == nil {
   117  		p.first = err
   118  	}
   119  	p.errcnt++
   120  	if p.errh == nil {
   121  		panic(p.first)
   122  	}
   123  	p.errh(err)
   124  }
   125  
   126  // syntax_error_at reports a syntax error at the given position.
   127  func (p *parser) syntax_error_at(pos src.Pos, msg string) {
   128  	if trace {
   129  		defer p.trace("syntax_error (" + msg + ")")()
   130  	}
   131  
   132  	if p.tok == _EOF && p.first != nil {
   133  		return // avoid meaningless follow-up errors
   134  	}
   135  
   136  	// add punctuation etc. as needed to msg
   137  	switch {
   138  	case msg == "":
   139  		// nothing to do
   140  	case strings.HasPrefix(msg, "in"), strings.HasPrefix(msg, "at"), strings.HasPrefix(msg, "after"):
   141  		msg = " " + msg
   142  	case strings.HasPrefix(msg, "expecting"):
   143  		msg = ", " + msg
   144  	default:
   145  		// plain error - we don't care about current token
   146  		p.error_at(pos, "syntax error: "+msg)
   147  		return
   148  	}
   149  
   150  	// determine token string
   151  	var tok string
   152  	switch p.tok {
   153  	case _Name, _Semi:
   154  		tok = p.lit
   155  	case _Literal:
   156  		tok = "literal " + p.lit
   157  	case _Operator:
   158  		tok = p.op.String()
   159  	case _AssignOp:
   160  		tok = p.op.String() + "="
   161  	case _IncOp:
   162  		tok = p.op.String()
   163  		tok += tok
   164  	default:
   165  		tok = tokstring(p.tok)
   166  	}
   167  
   168  	p.error_at(pos, "syntax error: unexpected "+tok+msg)
   169  }
   170  
   171  // Convenience methods using the current token position.
   172  func (p *parser) pos() src.Pos            { return p.pos_at(p.line, p.col) }
   173  func (p *parser) error(msg string)        { p.error_at(p.pos(), msg) }
   174  func (p *parser) syntax_error(msg string) { p.syntax_error_at(p.pos(), msg) }
   175  
   176  // The stopset contains keywords that start a statement.
   177  // They are good synchronization points in case of syntax
   178  // errors and (usually) shouldn't be skipped over.
   179  const stopset uint64 = 1<<_Break |
   180  	1<<_Const |
   181  	1<<_Continue |
   182  	1<<_Defer |
   183  	1<<_Fallthrough |
   184  	1<<_For |
   185  	1<<_Go |
   186  	1<<_Goto |
   187  	1<<_If |
   188  	1<<_Return |
   189  	1<<_Select |
   190  	1<<_Switch |
   191  	1<<_Type |
   192  	1<<_Var
   193  
   194  // Advance consumes tokens until it finds a token of the stopset or followlist.
   195  // The stopset is only considered if we are inside a function (p.fnest > 0).
   196  // The followlist is the list of valid tokens that can follow a production;
   197  // if it is empty, exactly one token is consumed to ensure progress.
   198  func (p *parser) advance(followlist ...token) {
   199  	if len(followlist) == 0 {
   200  		p.next()
   201  		return
   202  	}
   203  
   204  	// compute follow set
   205  	// (not speed critical, advance is only called in error situations)
   206  	var followset uint64 = 1 << _EOF // never skip over EOF
   207  	for _, tok := range followlist {
   208  		followset |= 1 << tok
   209  	}
   210  
   211  	for !(contains(followset, p.tok) || p.fnest > 0 && contains(stopset, p.tok)) {
   212  		p.next()
   213  	}
   214  }
   215  
   216  func tokstring(tok token) string {
   217  	switch tok {
   218  	case _EOF:
   219  		return "EOF"
   220  	case _Comma:
   221  		return "comma"
   222  	case _Semi:
   223  		return "semicolon"
   224  	}
   225  	return tok.String()
   226  }
   227  
   228  // usage: defer p.trace(msg)()
   229  func (p *parser) trace(msg string) func() {
   230  	fmt.Printf("%5d: %s%s (\n", p.line, p.indent, msg)
   231  	const tab = ". "
   232  	p.indent = append(p.indent, tab...)
   233  	return func() {
   234  		p.indent = p.indent[:len(p.indent)-len(tab)]
   235  		if x := recover(); x != nil {
   236  			panic(x) // skip print_trace
   237  		}
   238  		fmt.Printf("%5d: %s)\n", p.line, p.indent)
   239  	}
   240  }
   241  
   242  // ----------------------------------------------------------------------------
   243  // Package files
   244  //
   245  // Parse methods are annotated with matching Go productions as appropriate.
   246  // The annotations are intended as guidelines only since a single Go grammar
   247  // rule may be covered by multiple parse methods and vice versa.
   248  //
   249  // Excluding methods returning slices, parse methods named xOrNil may return
   250  // nil; all others are expected to return a valid non-nil node.
   251  
   252  // SourceFile = PackageClause ";" { ImportDecl ";" } { TopLevelDecl ";" } .
   253  func (p *parser) fileOrNil() *File {
   254  	if trace {
   255  		defer p.trace("file")()
   256  	}
   257  
   258  	f := new(File)
   259  	f.pos = p.pos()
   260  
   261  	// PackageClause
   262  	if !p.got(_Package) {
   263  		p.syntax_error("package statement must be first")
   264  		return nil
   265  	}
   266  	f.PkgName = p.name()
   267  	p.want(_Semi)
   268  
   269  	// don't bother continuing if package clause has errors
   270  	if p.first != nil {
   271  		return nil
   272  	}
   273  
   274  	// { ImportDecl ";" }
   275  	for p.got(_Import) {
   276  		f.DeclList = p.appendGroup(f.DeclList, p.importDecl)
   277  		p.want(_Semi)
   278  	}
   279  
   280  	// { TopLevelDecl ";" }
   281  	for p.tok != _EOF {
   282  		switch p.tok {
   283  		case _Const:
   284  			p.next()
   285  			f.DeclList = p.appendGroup(f.DeclList, p.constDecl)
   286  
   287  		case _Type:
   288  			p.next()
   289  			f.DeclList = p.appendGroup(f.DeclList, p.typeDecl)
   290  
   291  		case _Var:
   292  			p.next()
   293  			f.DeclList = p.appendGroup(f.DeclList, p.varDecl)
   294  
   295  		case _Func:
   296  			p.next()
   297  			if d := p.funcDeclOrNil(); d != nil {
   298  				f.DeclList = append(f.DeclList, d)
   299  			}
   300  
   301  		default:
   302  			if p.tok == _Lbrace && len(f.DeclList) > 0 && isEmptyFuncDecl(f.DeclList[len(f.DeclList)-1]) {
   303  				// opening { of function declaration on next line
   304  				p.syntax_error("unexpected semicolon or newline before {")
   305  			} else {
   306  				p.syntax_error("non-declaration statement outside function body")
   307  			}
   308  			p.advance(_Const, _Type, _Var, _Func)
   309  			continue
   310  		}
   311  
   312  		// Reset p.pragma BEFORE advancing to the next token (consuming ';')
   313  		// since comments before may set pragmas for the next function decl.
   314  		p.pragma = 0
   315  
   316  		if p.tok != _EOF && !p.got(_Semi) {
   317  			p.syntax_error("after top level declaration")
   318  			p.advance(_Const, _Type, _Var, _Func)
   319  		}
   320  	}
   321  	// p.tok == _EOF
   322  
   323  	f.Lines = p.source.line
   324  
   325  	return f
   326  }
   327  
   328  func isEmptyFuncDecl(dcl Decl) bool {
   329  	f, ok := dcl.(*FuncDecl)
   330  	return ok && f.Body == nil
   331  }
   332  
   333  // ----------------------------------------------------------------------------
   334  // Declarations
   335  
   336  // appendGroup(f) = f | "(" { f ";" } ")" .
   337  func (p *parser) appendGroup(list []Decl, f func(*Group) Decl) []Decl {
   338  	if p.got(_Lparen) {
   339  		g := new(Group)
   340  		for p.tok != _EOF && p.tok != _Rparen {
   341  			list = append(list, f(g))
   342  			if !p.osemi(_Rparen) {
   343  				break
   344  			}
   345  		}
   346  		p.want(_Rparen)
   347  	} else {
   348  		list = append(list, f(nil))
   349  	}
   350  
   351  	if debug {
   352  		for _, d := range list {
   353  			if d == nil {
   354  				panic("nil list entry")
   355  			}
   356  		}
   357  	}
   358  
   359  	return list
   360  }
   361  
   362  // ImportSpec = [ "." | PackageName ] ImportPath .
   363  // ImportPath = string_lit .
   364  func (p *parser) importDecl(group *Group) Decl {
   365  	if trace {
   366  		defer p.trace("importDecl")()
   367  	}
   368  
   369  	d := new(ImportDecl)
   370  	d.pos = p.pos()
   371  
   372  	switch p.tok {
   373  	case _Name:
   374  		d.LocalPkgName = p.name()
   375  	case _Dot:
   376  		d.LocalPkgName = p.newName(".")
   377  		p.next()
   378  	}
   379  	d.Path = p.oliteral()
   380  	if d.Path == nil {
   381  		p.syntax_error("missing import path")
   382  		p.advance(_Semi, _Rparen)
   383  		return nil
   384  	}
   385  	d.Group = group
   386  
   387  	return d
   388  }
   389  
   390  // ConstSpec = IdentifierList [ [ Type ] "=" ExpressionList ] .
   391  func (p *parser) constDecl(group *Group) Decl {
   392  	if trace {
   393  		defer p.trace("constDecl")()
   394  	}
   395  
   396  	d := new(ConstDecl)
   397  	d.pos = p.pos()
   398  
   399  	d.NameList = p.nameList(p.name())
   400  	if p.tok != _EOF && p.tok != _Semi && p.tok != _Rparen {
   401  		d.Type = p.typeOrNil()
   402  		if p.got(_Assign) {
   403  			d.Values = p.exprList()
   404  		}
   405  	}
   406  	d.Group = group
   407  
   408  	return d
   409  }
   410  
   411  // TypeSpec = identifier [ "=" ] Type .
   412  func (p *parser) typeDecl(group *Group) Decl {
   413  	if trace {
   414  		defer p.trace("typeDecl")()
   415  	}
   416  
   417  	d := new(TypeDecl)
   418  	d.pos = p.pos()
   419  
   420  	d.Name = p.name()
   421  	d.Alias = p.got(_Assign)
   422  	d.Type = p.typeOrNil()
   423  	if d.Type == nil {
   424  		d.Type = p.bad()
   425  		p.syntax_error("in type declaration")
   426  		p.advance(_Semi, _Rparen)
   427  	}
   428  	d.Group = group
   429  	d.Pragma = p.pragma
   430  
   431  	return d
   432  }
   433  
   434  // VarSpec = IdentifierList ( Type [ "=" ExpressionList ] | "=" ExpressionList ) .
   435  func (p *parser) varDecl(group *Group) Decl {
   436  	if trace {
   437  		defer p.trace("varDecl")()
   438  	}
   439  
   440  	d := new(VarDecl)
   441  	d.pos = p.pos()
   442  
   443  	d.NameList = p.nameList(p.name())
   444  	if p.got(_Assign) {
   445  		d.Values = p.exprList()
   446  	} else {
   447  		d.Type = p.type_()
   448  		if p.got(_Assign) {
   449  			d.Values = p.exprList()
   450  		}
   451  	}
   452  	d.Group = group
   453  
   454  	return d
   455  }
   456  
   457  // FunctionDecl = "func" FunctionName ( Function | Signature ) .
   458  // FunctionName = identifier .
   459  // Function     = Signature FunctionBody .
   460  // MethodDecl   = "func" Receiver MethodName ( Function | Signature ) .
   461  // Receiver     = Parameters .
   462  func (p *parser) funcDeclOrNil() *FuncDecl {
   463  	if trace {
   464  		defer p.trace("funcDecl")()
   465  	}
   466  
   467  	f := new(FuncDecl)
   468  	f.pos = p.pos()
   469  
   470  	if p.tok == _Lparen {
   471  		rcvr := p.paramList()
   472  		switch len(rcvr) {
   473  		case 0:
   474  			p.error("method has no receiver")
   475  		default:
   476  			p.error("method has multiple receivers")
   477  			fallthrough
   478  		case 1:
   479  			f.Recv = rcvr[0]
   480  		}
   481  	}
   482  
   483  	if p.tok != _Name {
   484  		p.syntax_error("expecting name or (")
   485  		p.advance(_Lbrace, _Semi)
   486  		return nil
   487  	}
   488  
   489  	f.Name = p.name()
   490  	f.Type = p.funcType()
   491  	if p.tok == _Lbrace {
   492  		f.Body = p.funcBody()
   493  	}
   494  	f.Pragma = p.pragma
   495  
   496  	return f
   497  }
   498  
   499  func (p *parser) funcBody() *BlockStmt {
   500  	p.fnest++
   501  	errcnt := p.errcnt
   502  	body := p.blockStmt("")
   503  	p.fnest--
   504  
   505  	// Don't check branches if there were syntax errors in the function
   506  	// as it may lead to spurious errors (e.g., see test/switch2.go) or
   507  	// possibly crashes due to incomplete syntax trees.
   508  	if p.mode&CheckBranches != 0 && errcnt == p.errcnt {
   509  		checkBranches(body, p.errh)
   510  	}
   511  
   512  	return body
   513  }
   514  
   515  // ----------------------------------------------------------------------------
   516  // Expressions
   517  
   518  func (p *parser) expr() Expr {
   519  	if trace {
   520  		defer p.trace("expr")()
   521  	}
   522  
   523  	return p.binaryExpr(0)
   524  }
   525  
   526  // Expression = UnaryExpr | Expression binary_op Expression .
   527  func (p *parser) binaryExpr(prec int) Expr {
   528  	// don't trace binaryExpr - only leads to overly nested trace output
   529  
   530  	x := p.unaryExpr()
   531  	for (p.tok == _Operator || p.tok == _Star) && p.prec > prec {
   532  		t := new(Operation)
   533  		t.pos = p.pos()
   534  		t.Op = p.op
   535  		t.X = x
   536  		tprec := p.prec
   537  		p.next()
   538  		t.Y = p.binaryExpr(tprec)
   539  		x = t
   540  	}
   541  	return x
   542  }
   543  
   544  // UnaryExpr = PrimaryExpr | unary_op UnaryExpr .
   545  func (p *parser) unaryExpr() Expr {
   546  	if trace {
   547  		defer p.trace("unaryExpr")()
   548  	}
   549  
   550  	switch p.tok {
   551  	case _Operator, _Star:
   552  		switch p.op {
   553  		case Mul, Add, Sub, Not, Xor:
   554  			x := new(Operation)
   555  			x.pos = p.pos()
   556  			x.Op = p.op
   557  			p.next()
   558  			x.X = p.unaryExpr()
   559  			return x
   560  
   561  		case And:
   562  			x := new(Operation)
   563  			x.pos = p.pos()
   564  			x.Op = And
   565  			p.next()
   566  			// unaryExpr may have returned a parenthesized composite literal
   567  			// (see comment in operand) - remove parentheses if any
   568  			x.X = unparen(p.unaryExpr())
   569  			return x
   570  		}
   571  
   572  	case _Arrow:
   573  		// receive op (<-x) or receive-only channel (<-chan E)
   574  		pos := p.pos()
   575  		p.next()
   576  
   577  		// If the next token is _Chan we still don't know if it is
   578  		// a channel (<-chan int) or a receive op (<-chan int(ch)).
   579  		// We only know once we have found the end of the unaryExpr.
   580  
   581  		x := p.unaryExpr()
   582  
   583  		// There are two cases:
   584  		//
   585  		//   <-chan...  => <-x is a channel type
   586  		//   <-x        => <-x is a receive operation
   587  		//
   588  		// In the first case, <- must be re-associated with
   589  		// the channel type parsed already:
   590  		//
   591  		//   <-(chan E)   =>  (<-chan E)
   592  		//   <-(chan<-E)  =>  (<-chan (<-E))
   593  
   594  		if _, ok := x.(*ChanType); ok {
   595  			// x is a channel type => re-associate <-
   596  			dir := SendOnly
   597  			t := x
   598  			for dir == SendOnly {
   599  				c, ok := t.(*ChanType)
   600  				if !ok {
   601  					break
   602  				}
   603  				dir = c.Dir
   604  				if dir == RecvOnly {
   605  					// t is type <-chan E but <-<-chan E is not permitted
   606  					// (report same error as for "type _ <-<-chan E")
   607  					p.syntax_error("unexpected <-, expecting chan")
   608  					// already progressed, no need to advance
   609  				}
   610  				c.Dir = RecvOnly
   611  				t = c.Elem
   612  			}
   613  			if dir == SendOnly {
   614  				// channel dir is <- but channel element E is not a channel
   615  				// (report same error as for "type _ <-chan<-E")
   616  				p.syntax_error(fmt.Sprintf("unexpected %s, expecting chan", String(t)))
   617  				// already progressed, no need to advance
   618  			}
   619  			return x
   620  		}
   621  
   622  		// x is not a channel type => we have a receive op
   623  		o := new(Operation)
   624  		o.pos = pos
   625  		o.Op = Recv
   626  		o.X = x
   627  		return o
   628  	}
   629  
   630  	// TODO(mdempsky): We need parens here so we can report an
   631  	// error for "(x) := true". It should be possible to detect
   632  	// and reject that more efficiently though.
   633  	return p.pexpr(true)
   634  }
   635  
   636  // callStmt parses call-like statements that can be preceded by 'defer' and 'go'.
   637  func (p *parser) callStmt() *CallStmt {
   638  	if trace {
   639  		defer p.trace("callStmt")()
   640  	}
   641  
   642  	s := new(CallStmt)
   643  	s.pos = p.pos()
   644  	s.Tok = p.tok // _Defer or _Go
   645  	p.next()
   646  
   647  	x := p.pexpr(p.tok == _Lparen) // keep_parens so we can report error below
   648  	if t := unparen(x); t != x {
   649  		p.error(fmt.Sprintf("expression in %s must not be parenthesized", s.Tok))
   650  		// already progressed, no need to advance
   651  		x = t
   652  	}
   653  
   654  	cx, ok := x.(*CallExpr)
   655  	if !ok {
   656  		p.error(fmt.Sprintf("expression in %s must be function call", s.Tok))
   657  		// already progressed, no need to advance
   658  		cx = new(CallExpr)
   659  		cx.pos = x.Pos()
   660  		cx.Fun = p.bad()
   661  	}
   662  
   663  	s.Call = cx
   664  	return s
   665  }
   666  
   667  // Operand     = Literal | OperandName | MethodExpr | "(" Expression ")" .
   668  // Literal     = BasicLit | CompositeLit | FunctionLit .
   669  // BasicLit    = int_lit | float_lit | imaginary_lit | rune_lit | string_lit .
   670  // OperandName = identifier | QualifiedIdent.
   671  func (p *parser) operand(keep_parens bool) Expr {
   672  	if trace {
   673  		defer p.trace("operand " + p.tok.String())()
   674  	}
   675  
   676  	switch p.tok {
   677  	case _Name:
   678  		return p.name()
   679  
   680  	case _Literal:
   681  		return p.oliteral()
   682  
   683  	case _Lparen:
   684  		pos := p.pos()
   685  		p.next()
   686  		p.xnest++
   687  		x := p.expr()
   688  		p.xnest--
   689  		p.want(_Rparen)
   690  
   691  		// Optimization: Record presence of ()'s only where needed
   692  		// for error reporting. Don't bother in other cases; it is
   693  		// just a waste of memory and time.
   694  
   695  		// Parentheses are not permitted on lhs of := .
   696  		// switch x.Op {
   697  		// case ONAME, ONONAME, OPACK, OTYPE, OLITERAL, OTYPESW:
   698  		// 	keep_parens = true
   699  		// }
   700  
   701  		// Parentheses are not permitted around T in a composite
   702  		// literal T{}. If the next token is a {, assume x is a
   703  		// composite literal type T (it may not be, { could be
   704  		// the opening brace of a block, but we don't know yet).
   705  		if p.tok == _Lbrace {
   706  			keep_parens = true
   707  		}
   708  
   709  		// Parentheses are also not permitted around the expression
   710  		// in a go/defer statement. In that case, operand is called
   711  		// with keep_parens set.
   712  		if keep_parens {
   713  			px := new(ParenExpr)
   714  			px.pos = pos
   715  			px.X = x
   716  			x = px
   717  		}
   718  		return x
   719  
   720  	case _Func:
   721  		pos := p.pos()
   722  		p.next()
   723  		t := p.funcType()
   724  		if p.tok == _Lbrace {
   725  			p.xnest++
   726  
   727  			f := new(FuncLit)
   728  			f.pos = pos
   729  			f.Type = t
   730  			f.Body = p.funcBody()
   731  
   732  			p.xnest--
   733  			return f
   734  		}
   735  		return t
   736  
   737  	case _Lbrack, _Chan, _Map, _Struct, _Interface:
   738  		return p.type_() // othertype
   739  
   740  	default:
   741  		x := p.bad()
   742  		p.syntax_error("expecting expression")
   743  		p.advance()
   744  		return x
   745  	}
   746  
   747  	// Syntactically, composite literals are operands. Because a complit
   748  	// type may be a qualified identifier which is handled by pexpr
   749  	// (together with selector expressions), complits are parsed there
   750  	// as well (operand is only called from pexpr).
   751  }
   752  
   753  // PrimaryExpr =
   754  // 	Operand |
   755  // 	Conversion |
   756  // 	PrimaryExpr Selector |
   757  // 	PrimaryExpr Index |
   758  // 	PrimaryExpr Slice |
   759  // 	PrimaryExpr TypeAssertion |
   760  // 	PrimaryExpr Arguments .
   761  //
   762  // Selector       = "." identifier .
   763  // Index          = "[" Expression "]" .
   764  // Slice          = "[" ( [ Expression ] ":" [ Expression ] ) |
   765  //                      ( [ Expression ] ":" Expression ":" Expression )
   766  //                  "]" .
   767  // TypeAssertion  = "." "(" Type ")" .
   768  // Arguments      = "(" [ ( ExpressionList | Type [ "," ExpressionList ] ) [ "..." ] [ "," ] ] ")" .
   769  func (p *parser) pexpr(keep_parens bool) Expr {
   770  	if trace {
   771  		defer p.trace("pexpr")()
   772  	}
   773  
   774  	x := p.operand(keep_parens)
   775  
   776  loop:
   777  	for {
   778  		pos := p.pos()
   779  		switch p.tok {
   780  		case _Dot:
   781  			p.next()
   782  			switch p.tok {
   783  			case _Name:
   784  				// pexpr '.' sym
   785  				t := new(SelectorExpr)
   786  				t.pos = pos
   787  				t.X = x
   788  				t.Sel = p.name()
   789  				x = t
   790  
   791  			case _Lparen:
   792  				p.next()
   793  				if p.got(_Type) {
   794  					t := new(TypeSwitchGuard)
   795  					t.pos = pos
   796  					t.X = x
   797  					x = t
   798  				} else {
   799  					t := new(AssertExpr)
   800  					t.pos = pos
   801  					t.X = x
   802  					t.Type = p.expr()
   803  					x = t
   804  				}
   805  				p.want(_Rparen)
   806  
   807  			default:
   808  				p.syntax_error("expecting name or (")
   809  				p.advance(_Semi, _Rparen)
   810  			}
   811  
   812  		case _Lbrack:
   813  			p.next()
   814  			p.xnest++
   815  
   816  			var i Expr
   817  			if p.tok != _Colon {
   818  				i = p.expr()
   819  				if p.got(_Rbrack) {
   820  					// x[i]
   821  					t := new(IndexExpr)
   822  					t.pos = pos
   823  					t.X = x
   824  					t.Index = i
   825  					x = t
   826  					p.xnest--
   827  					break
   828  				}
   829  			}
   830  
   831  			// x[i:...
   832  			t := new(SliceExpr)
   833  			t.pos = pos
   834  			t.X = x
   835  			t.Index[0] = i
   836  			p.want(_Colon)
   837  			if p.tok != _Colon && p.tok != _Rbrack {
   838  				// x[i:j...
   839  				t.Index[1] = p.expr()
   840  			}
   841  			if p.got(_Colon) {
   842  				t.Full = true
   843  				// x[i:j:...]
   844  				if t.Index[1] == nil {
   845  					p.error("middle index required in 3-index slice")
   846  				}
   847  				if p.tok != _Rbrack {
   848  					// x[i:j:k...
   849  					t.Index[2] = p.expr()
   850  				} else {
   851  					p.error("final index required in 3-index slice")
   852  				}
   853  			}
   854  			p.want(_Rbrack)
   855  
   856  			x = t
   857  			p.xnest--
   858  
   859  		case _Lparen:
   860  			x = p.call(x)
   861  
   862  		case _Lbrace:
   863  			// operand may have returned a parenthesized complit
   864  			// type; accept it but complain if we have a complit
   865  			t := unparen(x)
   866  			// determine if '{' belongs to a composite literal or a block statement
   867  			complit_ok := false
   868  			switch t.(type) {
   869  			case *Name, *SelectorExpr:
   870  				if p.xnest >= 0 {
   871  					// x is considered a composite literal type
   872  					complit_ok = true
   873  				}
   874  			case *ArrayType, *SliceType, *StructType, *MapType:
   875  				// x is a comptype
   876  				complit_ok = true
   877  			}
   878  			if !complit_ok {
   879  				break loop
   880  			}
   881  			if t != x {
   882  				p.syntax_error("cannot parenthesize type in composite literal")
   883  				// already progressed, no need to advance
   884  			}
   885  			n := p.complitexpr()
   886  			n.Type = x
   887  			x = n
   888  
   889  		default:
   890  			break loop
   891  		}
   892  	}
   893  
   894  	return x
   895  }
   896  
   897  // Element = Expression | LiteralValue .
   898  func (p *parser) bare_complitexpr() Expr {
   899  	if trace {
   900  		defer p.trace("bare_complitexpr")()
   901  	}
   902  
   903  	if p.tok == _Lbrace {
   904  		// '{' start_complit braced_keyval_list '}'
   905  		return p.complitexpr()
   906  	}
   907  
   908  	return p.expr()
   909  }
   910  
   911  // LiteralValue = "{" [ ElementList [ "," ] ] "}" .
   912  func (p *parser) complitexpr() *CompositeLit {
   913  	if trace {
   914  		defer p.trace("complitexpr")()
   915  	}
   916  
   917  	x := new(CompositeLit)
   918  	x.pos = p.pos()
   919  
   920  	p.want(_Lbrace)
   921  	p.xnest++
   922  
   923  	for p.tok != _EOF && p.tok != _Rbrace {
   924  		// value
   925  		e := p.bare_complitexpr()
   926  		if p.tok == _Colon {
   927  			// key ':' value
   928  			l := new(KeyValueExpr)
   929  			l.pos = p.pos()
   930  			p.next()
   931  			l.Key = e
   932  			l.Value = p.bare_complitexpr()
   933  			e = l
   934  			x.NKeys++
   935  		}
   936  		x.ElemList = append(x.ElemList, e)
   937  		if !p.ocomma(_Rbrace) {
   938  			break
   939  		}
   940  	}
   941  
   942  	x.Rbrace = p.pos()
   943  	p.xnest--
   944  	p.want(_Rbrace)
   945  
   946  	return x
   947  }
   948  
   949  // ----------------------------------------------------------------------------
   950  // Types
   951  
   952  func (p *parser) type_() Expr {
   953  	if trace {
   954  		defer p.trace("type_")()
   955  	}
   956  
   957  	typ := p.typeOrNil()
   958  	if typ == nil {
   959  		typ = p.bad()
   960  		p.syntax_error("expecting type")
   961  		p.advance()
   962  	}
   963  
   964  	return typ
   965  }
   966  
   967  func newIndirect(pos src.Pos, typ Expr) Expr {
   968  	o := new(Operation)
   969  	o.pos = pos
   970  	o.Op = Mul
   971  	o.X = typ
   972  	return o
   973  }
   974  
   975  // typeOrNil is like type_ but it returns nil if there was no type
   976  // instead of reporting an error.
   977  //
   978  // Type     = TypeName | TypeLit | "(" Type ")" .
   979  // TypeName = identifier | QualifiedIdent .
   980  // TypeLit  = ArrayType | StructType | PointerType | FunctionType | InterfaceType |
   981  // 	      SliceType | MapType | Channel_Type .
   982  func (p *parser) typeOrNil() Expr {
   983  	if trace {
   984  		defer p.trace("typeOrNil")()
   985  	}
   986  
   987  	pos := p.pos()
   988  	switch p.tok {
   989  	case _Star:
   990  		// ptrtype
   991  		p.next()
   992  		return newIndirect(pos, p.type_())
   993  
   994  	case _Arrow:
   995  		// recvchantype
   996  		p.next()
   997  		p.want(_Chan)
   998  		t := new(ChanType)
   999  		t.pos = pos
  1000  		t.Dir = RecvOnly
  1001  		t.Elem = p.chanElem()
  1002  		return t
  1003  
  1004  	case _Func:
  1005  		// fntype
  1006  		p.next()
  1007  		return p.funcType()
  1008  
  1009  	case _Lbrack:
  1010  		// '[' oexpr ']' ntype
  1011  		// '[' _DotDotDot ']' ntype
  1012  		p.next()
  1013  		p.xnest++
  1014  		if p.got(_Rbrack) {
  1015  			// []T
  1016  			p.xnest--
  1017  			t := new(SliceType)
  1018  			t.pos = pos
  1019  			t.Elem = p.type_()
  1020  			return t
  1021  		}
  1022  
  1023  		// [n]T
  1024  		t := new(ArrayType)
  1025  		t.pos = pos
  1026  		if !p.got(_DotDotDot) {
  1027  			t.Len = p.expr()
  1028  		}
  1029  		p.want(_Rbrack)
  1030  		p.xnest--
  1031  		t.Elem = p.type_()
  1032  		return t
  1033  
  1034  	case _Chan:
  1035  		// _Chan non_recvchantype
  1036  		// _Chan _Comm ntype
  1037  		p.next()
  1038  		t := new(ChanType)
  1039  		t.pos = pos
  1040  		if p.got(_Arrow) {
  1041  			t.Dir = SendOnly
  1042  		}
  1043  		t.Elem = p.chanElem()
  1044  		return t
  1045  
  1046  	case _Map:
  1047  		// _Map '[' ntype ']' ntype
  1048  		p.next()
  1049  		p.want(_Lbrack)
  1050  		t := new(MapType)
  1051  		t.pos = pos
  1052  		t.Key = p.type_()
  1053  		p.want(_Rbrack)
  1054  		t.Value = p.type_()
  1055  		return t
  1056  
  1057  	case _Struct:
  1058  		return p.structType()
  1059  
  1060  	case _Interface:
  1061  		return p.interfaceType()
  1062  
  1063  	case _Name:
  1064  		return p.dotname(p.name())
  1065  
  1066  	case _Lparen:
  1067  		p.next()
  1068  		t := p.type_()
  1069  		p.want(_Rparen)
  1070  		return t
  1071  	}
  1072  
  1073  	return nil
  1074  }
  1075  
  1076  func (p *parser) funcType() *FuncType {
  1077  	if trace {
  1078  		defer p.trace("funcType")()
  1079  	}
  1080  
  1081  	typ := new(FuncType)
  1082  	typ.pos = p.pos()
  1083  	typ.ParamList = p.paramList()
  1084  	typ.ResultList = p.funcResult()
  1085  
  1086  	return typ
  1087  }
  1088  
  1089  func (p *parser) chanElem() Expr {
  1090  	if trace {
  1091  		defer p.trace("chanElem")()
  1092  	}
  1093  
  1094  	typ := p.typeOrNil()
  1095  	if typ == nil {
  1096  		typ = p.bad()
  1097  		p.syntax_error("missing channel element type")
  1098  		// assume element type is simply absent - don't advance
  1099  	}
  1100  
  1101  	return typ
  1102  }
  1103  
  1104  func (p *parser) dotname(name *Name) Expr {
  1105  	if trace {
  1106  		defer p.trace("dotname")()
  1107  	}
  1108  
  1109  	if p.tok == _Dot {
  1110  		s := new(SelectorExpr)
  1111  		s.pos = p.pos()
  1112  		p.next()
  1113  		s.X = name
  1114  		s.Sel = p.name()
  1115  		return s
  1116  	}
  1117  	return name
  1118  }
  1119  
  1120  // StructType = "struct" "{" { FieldDecl ";" } "}" .
  1121  func (p *parser) structType() *StructType {
  1122  	if trace {
  1123  		defer p.trace("structType")()
  1124  	}
  1125  
  1126  	typ := new(StructType)
  1127  	typ.pos = p.pos()
  1128  
  1129  	p.want(_Struct)
  1130  	p.want(_Lbrace)
  1131  	for p.tok != _EOF && p.tok != _Rbrace {
  1132  		p.fieldDecl(typ)
  1133  		if !p.osemi(_Rbrace) {
  1134  			break
  1135  		}
  1136  	}
  1137  	p.want(_Rbrace)
  1138  
  1139  	return typ
  1140  }
  1141  
  1142  // InterfaceType = "interface" "{" { MethodSpec ";" } "}" .
  1143  func (p *parser) interfaceType() *InterfaceType {
  1144  	if trace {
  1145  		defer p.trace("interfaceType")()
  1146  	}
  1147  
  1148  	typ := new(InterfaceType)
  1149  	typ.pos = p.pos()
  1150  
  1151  	p.want(_Interface)
  1152  	p.want(_Lbrace)
  1153  	for p.tok != _EOF && p.tok != _Rbrace {
  1154  		if m := p.methodDecl(); m != nil {
  1155  			typ.MethodList = append(typ.MethodList, m)
  1156  		}
  1157  		if !p.osemi(_Rbrace) {
  1158  			break
  1159  		}
  1160  	}
  1161  	p.want(_Rbrace)
  1162  
  1163  	return typ
  1164  }
  1165  
  1166  // Result = Parameters | Type .
  1167  func (p *parser) funcResult() []*Field {
  1168  	if trace {
  1169  		defer p.trace("funcResult")()
  1170  	}
  1171  
  1172  	if p.tok == _Lparen {
  1173  		return p.paramList()
  1174  	}
  1175  
  1176  	pos := p.pos()
  1177  	if typ := p.typeOrNil(); typ != nil {
  1178  		f := new(Field)
  1179  		f.pos = pos
  1180  		f.Type = typ
  1181  		return []*Field{f}
  1182  	}
  1183  
  1184  	return nil
  1185  }
  1186  
  1187  func (p *parser) addField(styp *StructType, pos src.Pos, name *Name, typ Expr, tag *BasicLit) {
  1188  	if tag != nil {
  1189  		for i := len(styp.FieldList) - len(styp.TagList); i > 0; i-- {
  1190  			styp.TagList = append(styp.TagList, nil)
  1191  		}
  1192  		styp.TagList = append(styp.TagList, tag)
  1193  	}
  1194  
  1195  	f := new(Field)
  1196  	f.pos = pos
  1197  	f.Name = name
  1198  	f.Type = typ
  1199  	styp.FieldList = append(styp.FieldList, f)
  1200  
  1201  	if debug && tag != nil && len(styp.FieldList) != len(styp.TagList) {
  1202  		panic("inconsistent struct field list")
  1203  	}
  1204  }
  1205  
  1206  // FieldDecl      = (IdentifierList Type | AnonymousField) [ Tag ] .
  1207  // AnonymousField = [ "*" ] TypeName .
  1208  // Tag            = string_lit .
  1209  func (p *parser) fieldDecl(styp *StructType) {
  1210  	if trace {
  1211  		defer p.trace("fieldDecl")()
  1212  	}
  1213  
  1214  	pos := p.pos()
  1215  	switch p.tok {
  1216  	case _Name:
  1217  		name := p.name()
  1218  		if p.tok == _Dot || p.tok == _Literal || p.tok == _Semi || p.tok == _Rbrace {
  1219  			// embed oliteral
  1220  			typ := p.qualifiedName(name)
  1221  			tag := p.oliteral()
  1222  			p.addField(styp, pos, nil, typ, tag)
  1223  			return
  1224  		}
  1225  
  1226  		// new_name_list ntype oliteral
  1227  		names := p.nameList(name)
  1228  		typ := p.type_()
  1229  		tag := p.oliteral()
  1230  
  1231  		for _, name := range names {
  1232  			p.addField(styp, name.Pos(), name, typ, tag)
  1233  		}
  1234  
  1235  	case _Lparen:
  1236  		p.next()
  1237  		if p.tok == _Star {
  1238  			// '(' '*' embed ')' oliteral
  1239  			pos := p.pos()
  1240  			p.next()
  1241  			typ := newIndirect(pos, p.qualifiedName(nil))
  1242  			p.want(_Rparen)
  1243  			tag := p.oliteral()
  1244  			p.addField(styp, pos, nil, typ, tag)
  1245  			p.syntax_error("cannot parenthesize embedded type")
  1246  
  1247  		} else {
  1248  			// '(' embed ')' oliteral
  1249  			typ := p.qualifiedName(nil)
  1250  			p.want(_Rparen)
  1251  			tag := p.oliteral()
  1252  			p.addField(styp, pos, nil, typ, tag)
  1253  			p.syntax_error("cannot parenthesize embedded type")
  1254  		}
  1255  
  1256  	case _Star:
  1257  		p.next()
  1258  		if p.got(_Lparen) {
  1259  			// '*' '(' embed ')' oliteral
  1260  			typ := newIndirect(pos, p.qualifiedName(nil))
  1261  			p.want(_Rparen)
  1262  			tag := p.oliteral()
  1263  			p.addField(styp, pos, nil, typ, tag)
  1264  			p.syntax_error("cannot parenthesize embedded type")
  1265  
  1266  		} else {
  1267  			// '*' embed oliteral
  1268  			typ := newIndirect(pos, p.qualifiedName(nil))
  1269  			tag := p.oliteral()
  1270  			p.addField(styp, pos, nil, typ, tag)
  1271  		}
  1272  
  1273  	default:
  1274  		p.syntax_error("expecting field name or embedded type")
  1275  		p.advance(_Semi, _Rbrace)
  1276  	}
  1277  }
  1278  
  1279  func (p *parser) oliteral() *BasicLit {
  1280  	if p.tok == _Literal {
  1281  		b := new(BasicLit)
  1282  		b.pos = p.pos()
  1283  		b.Value = p.lit
  1284  		b.Kind = p.kind
  1285  		p.next()
  1286  		return b
  1287  	}
  1288  	return nil
  1289  }
  1290  
  1291  // MethodSpec        = MethodName Signature | InterfaceTypeName .
  1292  // MethodName        = identifier .
  1293  // InterfaceTypeName = TypeName .
  1294  func (p *parser) methodDecl() *Field {
  1295  	if trace {
  1296  		defer p.trace("methodDecl")()
  1297  	}
  1298  
  1299  	switch p.tok {
  1300  	case _Name:
  1301  		name := p.name()
  1302  
  1303  		// accept potential name list but complain
  1304  		hasNameList := false
  1305  		for p.got(_Comma) {
  1306  			p.name()
  1307  			hasNameList = true
  1308  		}
  1309  		if hasNameList {
  1310  			p.syntax_error("name list not allowed in interface type")
  1311  			// already progressed, no need to advance
  1312  		}
  1313  
  1314  		f := new(Field)
  1315  		f.pos = name.Pos()
  1316  		if p.tok != _Lparen {
  1317  			// packname
  1318  			f.Type = p.qualifiedName(name)
  1319  			return f
  1320  		}
  1321  
  1322  		f.Name = name
  1323  		f.Type = p.funcType()
  1324  		return f
  1325  
  1326  	case _Lparen:
  1327  		p.syntax_error("cannot parenthesize embedded type")
  1328  		f := new(Field)
  1329  		f.pos = p.pos()
  1330  		p.next()
  1331  		f.Type = p.qualifiedName(nil)
  1332  		p.want(_Rparen)
  1333  		return f
  1334  
  1335  	default:
  1336  		p.syntax_error("expecting method or interface name")
  1337  		p.advance(_Semi, _Rbrace)
  1338  		return nil
  1339  	}
  1340  }
  1341  
  1342  // ParameterDecl = [ IdentifierList ] [ "..." ] Type .
  1343  func (p *parser) paramDeclOrNil() *Field {
  1344  	if trace {
  1345  		defer p.trace("paramDecl")()
  1346  	}
  1347  
  1348  	f := new(Field)
  1349  	f.pos = p.pos()
  1350  
  1351  	switch p.tok {
  1352  	case _Name:
  1353  		f.Name = p.name()
  1354  		switch p.tok {
  1355  		case _Name, _Star, _Arrow, _Func, _Lbrack, _Chan, _Map, _Struct, _Interface, _Lparen:
  1356  			// sym name_or_type
  1357  			f.Type = p.type_()
  1358  
  1359  		case _DotDotDot:
  1360  			// sym dotdotdot
  1361  			f.Type = p.dotsType()
  1362  
  1363  		case _Dot:
  1364  			// name_or_type
  1365  			// from dotname
  1366  			f.Type = p.dotname(f.Name)
  1367  			f.Name = nil
  1368  		}
  1369  
  1370  	case _Arrow, _Star, _Func, _Lbrack, _Chan, _Map, _Struct, _Interface, _Lparen:
  1371  		// name_or_type
  1372  		f.Type = p.type_()
  1373  
  1374  	case _DotDotDot:
  1375  		// dotdotdot
  1376  		f.Type = p.dotsType()
  1377  
  1378  	default:
  1379  		p.syntax_error("expecting )")
  1380  		p.advance(_Comma, _Rparen)
  1381  		return nil
  1382  	}
  1383  
  1384  	return f
  1385  }
  1386  
  1387  // ...Type
  1388  func (p *parser) dotsType() *DotsType {
  1389  	if trace {
  1390  		defer p.trace("dotsType")()
  1391  	}
  1392  
  1393  	t := new(DotsType)
  1394  	t.pos = p.pos()
  1395  
  1396  	p.want(_DotDotDot)
  1397  	t.Elem = p.typeOrNil()
  1398  	if t.Elem == nil {
  1399  		t.Elem = p.bad()
  1400  		p.syntax_error("final argument in variadic function missing type")
  1401  	}
  1402  
  1403  	return t
  1404  }
  1405  
  1406  // Parameters    = "(" [ ParameterList [ "," ] ] ")" .
  1407  // ParameterList = ParameterDecl { "," ParameterDecl } .
  1408  func (p *parser) paramList() (list []*Field) {
  1409  	if trace {
  1410  		defer p.trace("paramList")()
  1411  	}
  1412  
  1413  	pos := p.pos()
  1414  	p.want(_Lparen)
  1415  
  1416  	var named int // number of parameters that have an explicit name and type
  1417  	for p.tok != _EOF && p.tok != _Rparen {
  1418  		if par := p.paramDeclOrNil(); par != nil {
  1419  			if debug && par.Name == nil && par.Type == nil {
  1420  				panic("parameter without name or type")
  1421  			}
  1422  			if par.Name != nil && par.Type != nil {
  1423  				named++
  1424  			}
  1425  			list = append(list, par)
  1426  		}
  1427  		if !p.ocomma(_Rparen) {
  1428  			break
  1429  		}
  1430  	}
  1431  
  1432  	// distribute parameter types
  1433  	if named == 0 {
  1434  		// all unnamed => found names are named types
  1435  		for _, par := range list {
  1436  			if typ := par.Name; typ != nil {
  1437  				par.Type = typ
  1438  				par.Name = nil
  1439  			}
  1440  		}
  1441  	} else if named != len(list) {
  1442  		// some named => all must be named
  1443  		ok := true
  1444  		var typ Expr
  1445  		for i := len(list) - 1; i >= 0; i-- {
  1446  			if par := list[i]; par.Type != nil {
  1447  				typ = par.Type
  1448  				if par.Name == nil {
  1449  					ok = false
  1450  					n := p.newName("_")
  1451  					n.pos = typ.Pos() // correct position
  1452  					par.Name = n
  1453  				}
  1454  			} else if typ != nil {
  1455  				par.Type = typ
  1456  			} else {
  1457  				// par.Type == nil && typ == nil => we only have a par.Name
  1458  				ok = false
  1459  				t := p.bad()
  1460  				t.pos = par.Name.Pos() // correct position
  1461  				par.Type = t
  1462  			}
  1463  		}
  1464  		if !ok {
  1465  			p.syntax_error_at(pos, "mixed named and unnamed function parameters")
  1466  		}
  1467  	}
  1468  
  1469  	p.want(_Rparen)
  1470  	return
  1471  }
  1472  
  1473  func (p *parser) bad() *BadExpr {
  1474  	b := new(BadExpr)
  1475  	b.pos = p.pos()
  1476  	return b
  1477  }
  1478  
  1479  // ----------------------------------------------------------------------------
  1480  // Statements
  1481  
  1482  // We represent x++, x-- as assignments x += ImplicitOne, x -= ImplicitOne.
  1483  // ImplicitOne should not be used elsewhere.
  1484  var ImplicitOne = &BasicLit{Value: "1"}
  1485  
  1486  // SimpleStmt = EmptyStmt | ExpressionStmt | SendStmt | IncDecStmt | Assignment | ShortVarDecl .
  1487  func (p *parser) simpleStmt(lhs Expr, rangeOk bool) SimpleStmt {
  1488  	if trace {
  1489  		defer p.trace("simpleStmt")()
  1490  	}
  1491  
  1492  	if rangeOk && p.tok == _Range {
  1493  		// _Range expr
  1494  		if debug && lhs != nil {
  1495  			panic("invalid call of simpleStmt")
  1496  		}
  1497  		return p.newRangeClause(nil, false)
  1498  	}
  1499  
  1500  	if lhs == nil {
  1501  		lhs = p.exprList()
  1502  	}
  1503  
  1504  	if _, ok := lhs.(*ListExpr); !ok && p.tok != _Assign && p.tok != _Define {
  1505  		// expr
  1506  		pos := p.pos()
  1507  		switch p.tok {
  1508  		case _AssignOp:
  1509  			// lhs op= rhs
  1510  			op := p.op
  1511  			p.next()
  1512  			return p.newAssignStmt(pos, op, lhs, p.expr())
  1513  
  1514  		case _IncOp:
  1515  			// lhs++ or lhs--
  1516  			op := p.op
  1517  			p.next()
  1518  			return p.newAssignStmt(pos, op, lhs, ImplicitOne)
  1519  
  1520  		case _Arrow:
  1521  			// lhs <- rhs
  1522  			s := new(SendStmt)
  1523  			s.pos = pos
  1524  			p.next()
  1525  			s.Chan = lhs
  1526  			s.Value = p.expr()
  1527  			return s
  1528  
  1529  		default:
  1530  			// expr
  1531  			s := new(ExprStmt)
  1532  			s.pos = lhs.Pos()
  1533  			s.X = lhs
  1534  			return s
  1535  		}
  1536  	}
  1537  
  1538  	// expr_list
  1539  	pos := p.pos()
  1540  	switch p.tok {
  1541  	case _Assign:
  1542  		p.next()
  1543  
  1544  		if rangeOk && p.tok == _Range {
  1545  			// expr_list '=' _Range expr
  1546  			return p.newRangeClause(lhs, false)
  1547  		}
  1548  
  1549  		// expr_list '=' expr_list
  1550  		return p.newAssignStmt(pos, 0, lhs, p.exprList())
  1551  
  1552  	case _Define:
  1553  		p.next()
  1554  
  1555  		if rangeOk && p.tok == _Range {
  1556  			// expr_list ':=' range expr
  1557  			return p.newRangeClause(lhs, true)
  1558  		}
  1559  
  1560  		// expr_list ':=' expr_list
  1561  		rhs := p.exprList()
  1562  
  1563  		if x, ok := rhs.(*TypeSwitchGuard); ok {
  1564  			switch lhs := lhs.(type) {
  1565  			case *Name:
  1566  				x.Lhs = lhs
  1567  			case *ListExpr:
  1568  				p.error_at(lhs.Pos(), fmt.Sprintf("cannot assign 1 value to %d variables", len(lhs.ElemList)))
  1569  				// make the best of what we have
  1570  				if lhs, ok := lhs.ElemList[0].(*Name); ok {
  1571  					x.Lhs = lhs
  1572  				}
  1573  			default:
  1574  				p.error_at(lhs.Pos(), fmt.Sprintf("invalid variable name %s in type switch", String(lhs)))
  1575  			}
  1576  			s := new(ExprStmt)
  1577  			s.pos = x.Pos()
  1578  			s.X = x
  1579  			return s
  1580  		}
  1581  
  1582  		as := p.newAssignStmt(pos, Def, lhs, rhs)
  1583  		return as
  1584  
  1585  	default:
  1586  		p.syntax_error("expecting := or = or comma")
  1587  		p.advance(_Semi, _Rbrace)
  1588  		// make the best of what we have
  1589  		if x, ok := lhs.(*ListExpr); ok {
  1590  			lhs = x.ElemList[0]
  1591  		}
  1592  		s := new(ExprStmt)
  1593  		s.pos = lhs.Pos()
  1594  		s.X = lhs
  1595  		return s
  1596  	}
  1597  }
  1598  
  1599  func (p *parser) newRangeClause(lhs Expr, def bool) *RangeClause {
  1600  	r := new(RangeClause)
  1601  	r.pos = p.pos()
  1602  	p.next() // consume _Range
  1603  	r.Lhs = lhs
  1604  	r.Def = def
  1605  	r.X = p.expr()
  1606  	return r
  1607  }
  1608  
  1609  func (p *parser) newAssignStmt(pos src.Pos, op Operator, lhs, rhs Expr) *AssignStmt {
  1610  	a := new(AssignStmt)
  1611  	a.pos = pos
  1612  	a.Op = op
  1613  	a.Lhs = lhs
  1614  	a.Rhs = rhs
  1615  	return a
  1616  }
  1617  
  1618  func (p *parser) labeledStmtOrNil(label *Name) Stmt {
  1619  	if trace {
  1620  		defer p.trace("labeledStmt")()
  1621  	}
  1622  
  1623  	s := new(LabeledStmt)
  1624  	s.pos = p.pos()
  1625  	s.Label = label
  1626  
  1627  	p.want(_Colon)
  1628  
  1629  	if p.tok == _Rbrace {
  1630  		// We expect a statement (incl. an empty statement), which must be
  1631  		// terminated by a semicolon. Because semicolons may be omitted before
  1632  		// an _Rbrace, seeing an _Rbrace implies an empty statement.
  1633  		e := new(EmptyStmt)
  1634  		e.pos = p.pos()
  1635  		s.Stmt = e
  1636  		return s
  1637  	}
  1638  
  1639  	s.Stmt = p.stmtOrNil()
  1640  	if s.Stmt != nil {
  1641  		return s
  1642  	}
  1643  
  1644  	// report error at line of ':' token
  1645  	p.syntax_error_at(s.pos, "missing statement after label")
  1646  	// we are already at the end of the labeled statement - no need to advance
  1647  	return nil // avoids follow-on errors (see e.g., fixedbugs/bug274.go)
  1648  }
  1649  
  1650  // context must be a non-empty string unless we know that p.tok == _Lbrace.
  1651  func (p *parser) blockStmt(context string) *BlockStmt {
  1652  	if trace {
  1653  		defer p.trace("blockStmt")()
  1654  	}
  1655  
  1656  	s := new(BlockStmt)
  1657  	s.pos = p.pos()
  1658  
  1659  	if !p.got(_Lbrace) {
  1660  		p.syntax_error("expecting { after " + context)
  1661  		p.advance(_Name, _Rbrace)
  1662  		// TODO(gri) may be better to return here than to continue (#19663)
  1663  	}
  1664  
  1665  	s.List = p.stmtList()
  1666  	s.Rbrace = p.pos()
  1667  	p.want(_Rbrace)
  1668  
  1669  	return s
  1670  }
  1671  
  1672  func (p *parser) declStmt(f func(*Group) Decl) *DeclStmt {
  1673  	if trace {
  1674  		defer p.trace("declStmt")()
  1675  	}
  1676  
  1677  	s := new(DeclStmt)
  1678  	s.pos = p.pos()
  1679  
  1680  	p.next() // _Const, _Type, or _Var
  1681  	s.DeclList = p.appendGroup(nil, f)
  1682  
  1683  	return s
  1684  }
  1685  
  1686  func (p *parser) forStmt() Stmt {
  1687  	if trace {
  1688  		defer p.trace("forStmt")()
  1689  	}
  1690  
  1691  	s := new(ForStmt)
  1692  	s.pos = p.pos()
  1693  
  1694  	s.Init, s.Cond, s.Post = p.header(_For)
  1695  	s.Body = p.blockStmt("for clause")
  1696  
  1697  	return s
  1698  }
  1699  
  1700  func (p *parser) header(keyword token) (init SimpleStmt, cond Expr, post SimpleStmt) {
  1701  	p.want(keyword)
  1702  
  1703  	if p.tok == _Lbrace {
  1704  		if keyword == _If {
  1705  			p.syntax_error("missing condition in if statement")
  1706  		}
  1707  		return
  1708  	}
  1709  	// p.tok != _Lbrace
  1710  
  1711  	outer := p.xnest
  1712  	p.xnest = -1
  1713  
  1714  	if p.tok != _Semi {
  1715  		// accept potential varDecl but complain
  1716  		if p.got(_Var) {
  1717  			p.syntax_error(fmt.Sprintf("var declaration not allowed in %s initializer", keyword.String()))
  1718  		}
  1719  		init = p.simpleStmt(nil, keyword == _For)
  1720  		// If we have a range clause, we are done (can only happen for keyword == _For).
  1721  		if _, ok := init.(*RangeClause); ok {
  1722  			p.xnest = outer
  1723  			return
  1724  		}
  1725  	}
  1726  
  1727  	var condStmt SimpleStmt
  1728  	var semi struct {
  1729  		pos src.Pos
  1730  		lit string // valid if pos.IsKnown()
  1731  	}
  1732  	if p.tok == _Semi {
  1733  		semi.pos = p.pos()
  1734  		semi.lit = p.lit
  1735  		p.next()
  1736  		if keyword == _For {
  1737  			if p.tok != _Semi {
  1738  				if p.tok == _Lbrace {
  1739  					p.syntax_error("expecting for loop condition")
  1740  					goto done
  1741  				}
  1742  				condStmt = p.simpleStmt(nil, false)
  1743  			}
  1744  			p.want(_Semi)
  1745  			if p.tok != _Lbrace {
  1746  				post = p.simpleStmt(nil, false)
  1747  				if a, _ := post.(*AssignStmt); a != nil && a.Op == Def {
  1748  					p.syntax_error_at(a.Pos(), "cannot declare in post statement of for loop")
  1749  				}
  1750  			}
  1751  		} else if p.tok != _Lbrace {
  1752  			condStmt = p.simpleStmt(nil, false)
  1753  		}
  1754  	} else {
  1755  		condStmt = init
  1756  		init = nil
  1757  	}
  1758  
  1759  done:
  1760  	// unpack condStmt
  1761  	switch s := condStmt.(type) {
  1762  	case nil:
  1763  		if keyword == _If && semi.pos.IsKnown() {
  1764  			if semi.lit != "semicolon" {
  1765  				p.syntax_error_at(semi.pos, fmt.Sprintf("unexpected %s, expecting { after if clause", semi.lit))
  1766  			} else {
  1767  				p.syntax_error_at(semi.pos, "missing condition in if statement")
  1768  			}
  1769  		}
  1770  	case *ExprStmt:
  1771  		cond = s.X
  1772  	default:
  1773  		p.syntax_error(fmt.Sprintf("%s used as value", String(s)))
  1774  	}
  1775  
  1776  	p.xnest = outer
  1777  	return
  1778  }
  1779  
  1780  func (p *parser) ifStmt() *IfStmt {
  1781  	if trace {
  1782  		defer p.trace("ifStmt")()
  1783  	}
  1784  
  1785  	s := new(IfStmt)
  1786  	s.pos = p.pos()
  1787  
  1788  	s.Init, s.Cond, _ = p.header(_If)
  1789  	s.Then = p.blockStmt("if clause")
  1790  
  1791  	if p.got(_Else) {
  1792  		switch p.tok {
  1793  		case _If:
  1794  			s.Else = p.ifStmt()
  1795  		case _Lbrace:
  1796  			s.Else = p.blockStmt("")
  1797  		default:
  1798  			p.syntax_error("else must be followed by if or statement block")
  1799  			p.advance(_Name, _Rbrace)
  1800  		}
  1801  	}
  1802  
  1803  	return s
  1804  }
  1805  
  1806  func (p *parser) switchStmt() *SwitchStmt {
  1807  	if trace {
  1808  		defer p.trace("switchStmt")()
  1809  	}
  1810  
  1811  	s := new(SwitchStmt)
  1812  	s.pos = p.pos()
  1813  
  1814  	s.Init, s.Tag, _ = p.header(_Switch)
  1815  
  1816  	if !p.got(_Lbrace) {
  1817  		p.syntax_error("missing { after switch clause")
  1818  		p.advance(_Case, _Default, _Rbrace)
  1819  	}
  1820  	for p.tok != _EOF && p.tok != _Rbrace {
  1821  		s.Body = append(s.Body, p.caseClause())
  1822  	}
  1823  	s.Rbrace = p.pos()
  1824  	p.want(_Rbrace)
  1825  
  1826  	return s
  1827  }
  1828  
  1829  func (p *parser) selectStmt() *SelectStmt {
  1830  	if trace {
  1831  		defer p.trace("selectStmt")()
  1832  	}
  1833  
  1834  	s := new(SelectStmt)
  1835  	s.pos = p.pos()
  1836  
  1837  	p.want(_Select)
  1838  	if !p.got(_Lbrace) {
  1839  		p.syntax_error("missing { after select clause")
  1840  		p.advance(_Case, _Default, _Rbrace)
  1841  	}
  1842  	for p.tok != _EOF && p.tok != _Rbrace {
  1843  		s.Body = append(s.Body, p.commClause())
  1844  	}
  1845  	s.Rbrace = p.pos()
  1846  	p.want(_Rbrace)
  1847  
  1848  	return s
  1849  }
  1850  
  1851  func (p *parser) caseClause() *CaseClause {
  1852  	if trace {
  1853  		defer p.trace("caseClause")()
  1854  	}
  1855  
  1856  	c := new(CaseClause)
  1857  	c.pos = p.pos()
  1858  
  1859  	switch p.tok {
  1860  	case _Case:
  1861  		p.next()
  1862  		c.Cases = p.exprList()
  1863  
  1864  	case _Default:
  1865  		p.next()
  1866  
  1867  	default:
  1868  		p.syntax_error("expecting case or default or }")
  1869  		p.advance(_Colon, _Case, _Default, _Rbrace)
  1870  	}
  1871  
  1872  	c.Colon = p.pos()
  1873  	p.want(_Colon)
  1874  	c.Body = p.stmtList()
  1875  
  1876  	return c
  1877  }
  1878  
  1879  func (p *parser) commClause() *CommClause {
  1880  	if trace {
  1881  		defer p.trace("commClause")()
  1882  	}
  1883  
  1884  	c := new(CommClause)
  1885  	c.pos = p.pos()
  1886  
  1887  	switch p.tok {
  1888  	case _Case:
  1889  		p.next()
  1890  		c.Comm = p.simpleStmt(nil, false)
  1891  
  1892  		// The syntax restricts the possible simple statements here to:
  1893  		//
  1894  		//     lhs <- x (send statement)
  1895  		//     <-x
  1896  		//     lhs = <-x
  1897  		//     lhs := <-x
  1898  		//
  1899  		// All these (and more) are recognized by simpleStmt and invalid
  1900  		// syntax trees are flagged later, during type checking.
  1901  		// TODO(gri) eventually may want to restrict valid syntax trees
  1902  		// here.
  1903  
  1904  	case _Default:
  1905  		p.next()
  1906  
  1907  	default:
  1908  		p.syntax_error("expecting case or default or }")
  1909  		p.advance(_Colon, _Case, _Default, _Rbrace)
  1910  	}
  1911  
  1912  	c.Colon = p.pos()
  1913  	p.want(_Colon)
  1914  	c.Body = p.stmtList()
  1915  
  1916  	return c
  1917  }
  1918  
  1919  // Statement =
  1920  // 	Declaration | LabeledStmt | SimpleStmt |
  1921  // 	GoStmt | ReturnStmt | BreakStmt | ContinueStmt | GotoStmt |
  1922  // 	FallthroughStmt | Block | IfStmt | SwitchStmt | SelectStmt | ForStmt |
  1923  // 	DeferStmt .
  1924  func (p *parser) stmtOrNil() Stmt {
  1925  	if trace {
  1926  		defer p.trace("stmt " + p.tok.String())()
  1927  	}
  1928  
  1929  	// Most statements (assignments) start with an identifier;
  1930  	// look for it first before doing anything more expensive.
  1931  	if p.tok == _Name {
  1932  		lhs := p.exprList()
  1933  		if label, ok := lhs.(*Name); ok && p.tok == _Colon {
  1934  			return p.labeledStmtOrNil(label)
  1935  		}
  1936  		return p.simpleStmt(lhs, false)
  1937  	}
  1938  
  1939  	switch p.tok {
  1940  	case _Lbrace:
  1941  		return p.blockStmt("")
  1942  
  1943  	case _Var:
  1944  		return p.declStmt(p.varDecl)
  1945  
  1946  	case _Const:
  1947  		return p.declStmt(p.constDecl)
  1948  
  1949  	case _Type:
  1950  		return p.declStmt(p.typeDecl)
  1951  
  1952  	case _Operator, _Star:
  1953  		switch p.op {
  1954  		case Add, Sub, Mul, And, Xor, Not:
  1955  			return p.simpleStmt(nil, false) // unary operators
  1956  		}
  1957  
  1958  	case _Literal, _Func, _Lparen, // operands
  1959  		_Lbrack, _Struct, _Map, _Chan, _Interface, // composite types
  1960  		_Arrow: // receive operator
  1961  		return p.simpleStmt(nil, false)
  1962  
  1963  	case _For:
  1964  		return p.forStmt()
  1965  
  1966  	case _Switch:
  1967  		return p.switchStmt()
  1968  
  1969  	case _Select:
  1970  		return p.selectStmt()
  1971  
  1972  	case _If:
  1973  		return p.ifStmt()
  1974  
  1975  	case _Fallthrough:
  1976  		s := new(BranchStmt)
  1977  		s.pos = p.pos()
  1978  		p.next()
  1979  		s.Tok = _Fallthrough
  1980  		return s
  1981  
  1982  	case _Break, _Continue:
  1983  		s := new(BranchStmt)
  1984  		s.pos = p.pos()
  1985  		s.Tok = p.tok
  1986  		p.next()
  1987  		if p.tok == _Name {
  1988  			s.Label = p.name()
  1989  		}
  1990  		return s
  1991  
  1992  	case _Go, _Defer:
  1993  		return p.callStmt()
  1994  
  1995  	case _Goto:
  1996  		s := new(BranchStmt)
  1997  		s.pos = p.pos()
  1998  		s.Tok = _Goto
  1999  		p.next()
  2000  		s.Label = p.name()
  2001  		return s
  2002  
  2003  	case _Return:
  2004  		s := new(ReturnStmt)
  2005  		s.pos = p.pos()
  2006  		p.next()
  2007  		if p.tok != _Semi && p.tok != _Rbrace {
  2008  			s.Results = p.exprList()
  2009  		}
  2010  		return s
  2011  
  2012  	case _Semi:
  2013  		s := new(EmptyStmt)
  2014  		s.pos = p.pos()
  2015  		return s
  2016  	}
  2017  
  2018  	return nil
  2019  }
  2020  
  2021  // StatementList = { Statement ";" } .
  2022  func (p *parser) stmtList() (l []Stmt) {
  2023  	if trace {
  2024  		defer p.trace("stmtList")()
  2025  	}
  2026  
  2027  	for p.tok != _EOF && p.tok != _Rbrace && p.tok != _Case && p.tok != _Default {
  2028  		s := p.stmtOrNil()
  2029  		if s == nil {
  2030  			break
  2031  		}
  2032  		l = append(l, s)
  2033  		// customized version of osemi:
  2034  		// ';' is optional before a closing ')' or '}'
  2035  		if p.tok == _Rparen || p.tok == _Rbrace {
  2036  			continue
  2037  		}
  2038  		if !p.got(_Semi) {
  2039  			p.syntax_error("at end of statement")
  2040  			p.advance(_Semi, _Rbrace)
  2041  		}
  2042  	}
  2043  	return
  2044  }
  2045  
  2046  // Arguments = "(" [ ( ExpressionList | Type [ "," ExpressionList ] ) [ "..." ] [ "," ] ] ")" .
  2047  func (p *parser) call(fun Expr) *CallExpr {
  2048  	if trace {
  2049  		defer p.trace("call")()
  2050  	}
  2051  
  2052  	// call or conversion
  2053  	// convtype '(' expr ocomma ')'
  2054  	c := new(CallExpr)
  2055  	c.pos = p.pos()
  2056  	c.Fun = fun
  2057  
  2058  	p.want(_Lparen)
  2059  	p.xnest++
  2060  
  2061  	for p.tok != _EOF && p.tok != _Rparen {
  2062  		c.ArgList = append(c.ArgList, p.expr())
  2063  		c.HasDots = p.got(_DotDotDot)
  2064  		if !p.ocomma(_Rparen) || c.HasDots {
  2065  			break
  2066  		}
  2067  	}
  2068  
  2069  	p.xnest--
  2070  	p.want(_Rparen)
  2071  
  2072  	return c
  2073  }
  2074  
  2075  // ----------------------------------------------------------------------------
  2076  // Common productions
  2077  
  2078  func (p *parser) newName(value string) *Name {
  2079  	n := new(Name)
  2080  	n.pos = p.pos()
  2081  	n.Value = value
  2082  	return n
  2083  }
  2084  
  2085  func (p *parser) name() *Name {
  2086  	// no tracing to avoid overly verbose output
  2087  
  2088  	if p.tok == _Name {
  2089  		n := p.newName(p.lit)
  2090  		p.next()
  2091  		return n
  2092  	}
  2093  
  2094  	n := p.newName("_")
  2095  	p.syntax_error("expecting name")
  2096  	p.advance()
  2097  	return n
  2098  }
  2099  
  2100  // IdentifierList = identifier { "," identifier } .
  2101  // The first name must be provided.
  2102  func (p *parser) nameList(first *Name) []*Name {
  2103  	if trace {
  2104  		defer p.trace("nameList")()
  2105  	}
  2106  
  2107  	if debug && first == nil {
  2108  		panic("first name not provided")
  2109  	}
  2110  
  2111  	l := []*Name{first}
  2112  	for p.got(_Comma) {
  2113  		l = append(l, p.name())
  2114  	}
  2115  
  2116  	return l
  2117  }
  2118  
  2119  // The first name may be provided, or nil.
  2120  func (p *parser) qualifiedName(name *Name) Expr {
  2121  	if trace {
  2122  		defer p.trace("qualifiedName")()
  2123  	}
  2124  
  2125  	switch {
  2126  	case name != nil:
  2127  		// name is provided
  2128  	case p.tok == _Name:
  2129  		name = p.name()
  2130  	default:
  2131  		name = p.newName("_")
  2132  		p.syntax_error("expecting name")
  2133  		p.advance(_Dot, _Semi, _Rbrace)
  2134  	}
  2135  
  2136  	return p.dotname(name)
  2137  }
  2138  
  2139  // ExpressionList = Expression { "," Expression } .
  2140  func (p *parser) exprList() Expr {
  2141  	if trace {
  2142  		defer p.trace("exprList")()
  2143  	}
  2144  
  2145  	x := p.expr()
  2146  	if p.got(_Comma) {
  2147  		list := []Expr{x, p.expr()}
  2148  		for p.got(_Comma) {
  2149  			list = append(list, p.expr())
  2150  		}
  2151  		t := new(ListExpr)
  2152  		t.pos = x.Pos()
  2153  		t.ElemList = list
  2154  		x = t
  2155  	}
  2156  	return x
  2157  }
  2158  
  2159  // osemi parses an optional semicolon.
  2160  func (p *parser) osemi(follow token) bool {
  2161  	switch p.tok {
  2162  	case _Semi:
  2163  		p.next()
  2164  		return true
  2165  
  2166  	case _Rparen, _Rbrace:
  2167  		// semicolon is optional before ) or }
  2168  		return true
  2169  	}
  2170  
  2171  	p.syntax_error("expecting semicolon, newline, or " + tokstring(follow))
  2172  	p.advance(follow)
  2173  	return false
  2174  }
  2175  
  2176  // ocomma parses an optional comma.
  2177  func (p *parser) ocomma(follow token) bool {
  2178  	switch p.tok {
  2179  	case _Comma:
  2180  		p.next()
  2181  		return true
  2182  
  2183  	case _Rparen, _Rbrace:
  2184  		// comma is optional before ) or }
  2185  		return true
  2186  	}
  2187  
  2188  	p.syntax_error("expecting comma or " + tokstring(follow))
  2189  	p.advance(follow)
  2190  	return false
  2191  }
  2192  
  2193  // unparen removes all parentheses around an expression.
  2194  func unparen(x Expr) Expr {
  2195  	for {
  2196  		p, ok := x.(*ParenExpr)
  2197  		if !ok {
  2198  			break
  2199  		}
  2200  		x = p.X
  2201  	}
  2202  	return x
  2203  }