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