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