OSDN Git Service

55b8998b7d585b8035bb8e615d00b87cfda2e1ec
[pf3gnuchains/gcc-fork.git] / libgo / go / go / parser / parser.go
1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 // Package parser implements a parser for Go source files. Input may be
6 // provided in a variety of forms (see the various Parse* functions); the
7 // output is an abstract syntax tree (AST) representing the Go source. The
8 // parser is invoked through one of the Parse* functions.
9 //
10 package parser
11
12 import (
13         "fmt"
14         "go/ast"
15         "go/scanner"
16         "go/token"
17 )
18
19 // The mode parameter to the Parse* functions is a set of flags (or 0).
20 // They control the amount of source code parsed and other optional
21 // parser functionality.
22 //
23 const (
24         PackageClauseOnly uint = 1 << iota // parsing stops after package clause
25         ImportsOnly                        // parsing stops after import declarations
26         ParseComments                      // parse comments and add them to AST
27         Trace                              // print a trace of parsed productions
28         DeclarationErrors                  // report declaration errors
29         SpuriousErrors                     // report all (not just the first) errors per line
30 )
31
32 // The parser structure holds the parser's internal state.
33 type parser struct {
34         file *token.File
35         scanner.ErrorVector
36         scanner scanner.Scanner
37
38         // Tracing/debugging
39         mode   uint // parsing mode
40         trace  bool // == (mode & Trace != 0)
41         indent uint // indentation used for tracing output
42
43         // Comments
44         comments    []*ast.CommentGroup
45         leadComment *ast.CommentGroup // last lead comment
46         lineComment *ast.CommentGroup // last line comment
47
48         // Next token
49         pos token.Pos   // token position
50         tok token.Token // one token look-ahead
51         lit string      // token literal
52
53         // Non-syntactic parser control
54         exprLev int // < 0: in control clause, >= 0: in expression
55
56         // Ordinary identifier scopes
57         pkgScope   *ast.Scope        // pkgScope.Outer == nil
58         topScope   *ast.Scope        // top-most scope; may be pkgScope
59         unresolved []*ast.Ident      // unresolved identifiers
60         imports    []*ast.ImportSpec // list of imports
61
62         // Label scope
63         // (maintained by open/close LabelScope)
64         labelScope  *ast.Scope     // label scope for current function
65         targetStack [][]*ast.Ident // stack of unresolved labels
66 }
67
68 // scannerMode returns the scanner mode bits given the parser's mode bits.
69 func scannerMode(mode uint) uint {
70         var m uint = scanner.InsertSemis
71         if mode&ParseComments != 0 {
72                 m |= scanner.ScanComments
73         }
74         return m
75 }
76
77 func (p *parser) init(fset *token.FileSet, filename string, src []byte, mode uint) {
78         p.file = fset.AddFile(filename, fset.Base(), len(src))
79         p.scanner.Init(p.file, src, p, scannerMode(mode))
80
81         p.mode = mode
82         p.trace = mode&Trace != 0 // for convenience (p.trace is used frequently)
83
84         p.next()
85
86         // set up the pkgScope here (as opposed to in parseFile) because
87         // there are other parser entry points (ParseExpr, etc.)
88         p.openScope()
89         p.pkgScope = p.topScope
90
91         // for the same reason, set up a label scope
92         p.openLabelScope()
93 }
94
95 // ----------------------------------------------------------------------------
96 // Scoping support
97
98 func (p *parser) openScope() {
99         p.topScope = ast.NewScope(p.topScope)
100 }
101
102 func (p *parser) closeScope() {
103         p.topScope = p.topScope.Outer
104 }
105
106 func (p *parser) openLabelScope() {
107         p.labelScope = ast.NewScope(p.labelScope)
108         p.targetStack = append(p.targetStack, nil)
109 }
110
111 func (p *parser) closeLabelScope() {
112         // resolve labels
113         n := len(p.targetStack) - 1
114         scope := p.labelScope
115         for _, ident := range p.targetStack[n] {
116                 ident.Obj = scope.Lookup(ident.Name)
117                 if ident.Obj == nil && p.mode&DeclarationErrors != 0 {
118                         p.error(ident.Pos(), fmt.Sprintf("label %s undefined", ident.Name))
119                 }
120         }
121         // pop label scope
122         p.targetStack = p.targetStack[0:n]
123         p.labelScope = p.labelScope.Outer
124 }
125
126 func (p *parser) declare(decl, data interface{}, scope *ast.Scope, kind ast.ObjKind, idents ...*ast.Ident) {
127         for _, ident := range idents {
128                 assert(ident.Obj == nil, "identifier already declared or resolved")
129                 obj := ast.NewObj(kind, ident.Name)
130                 // remember the corresponding declaration for redeclaration
131                 // errors and global variable resolution/typechecking phase
132                 obj.Decl = decl
133                 obj.Data = data
134                 ident.Obj = obj
135                 if ident.Name != "_" {
136                         if alt := scope.Insert(obj); alt != nil && p.mode&DeclarationErrors != 0 {
137                                 prevDecl := ""
138                                 if pos := alt.Pos(); pos.IsValid() {
139                                         prevDecl = fmt.Sprintf("\n\tprevious declaration at %s", p.file.Position(pos))
140                                 }
141                                 p.error(ident.Pos(), fmt.Sprintf("%s redeclared in this block%s", ident.Name, prevDecl))
142                         }
143                 }
144         }
145 }
146
147 func (p *parser) shortVarDecl(idents []*ast.Ident) {
148         // Go spec: A short variable declaration may redeclare variables
149         // provided they were originally declared in the same block with
150         // the same type, and at least one of the non-blank variables is new.
151         n := 0 // number of new variables
152         for _, ident := range idents {
153                 assert(ident.Obj == nil, "identifier already declared or resolved")
154                 obj := ast.NewObj(ast.Var, ident.Name)
155                 // short var declarations cannot have redeclaration errors
156                 // and are not global => no need to remember the respective
157                 // declaration
158                 ident.Obj = obj
159                 if ident.Name != "_" {
160                         if alt := p.topScope.Insert(obj); alt != nil {
161                                 ident.Obj = alt // redeclaration
162                         } else {
163                                 n++ // new declaration
164                         }
165                 }
166         }
167         if n == 0 && p.mode&DeclarationErrors != 0 {
168                 p.error(idents[0].Pos(), "no new variables on left side of :=")
169         }
170 }
171
172 // The unresolved object is a sentinel to mark identifiers that have been added
173 // to the list of unresolved identifiers. The sentinel is only used for verifying
174 // internal consistency.
175 var unresolved = new(ast.Object)
176
177 func (p *parser) resolve(x ast.Expr) {
178         // nothing to do if x is not an identifier or the blank identifier
179         ident, _ := x.(*ast.Ident)
180         if ident == nil {
181                 return
182         }
183         assert(ident.Obj == nil, "identifier already declared or resolved")
184         if ident.Name == "_" {
185                 return
186         }
187         // try to resolve the identifier
188         for s := p.topScope; s != nil; s = s.Outer {
189                 if obj := s.Lookup(ident.Name); obj != nil {
190                         ident.Obj = obj
191                         return
192                 }
193         }
194         // all local scopes are known, so any unresolved identifier
195         // must be found either in the file scope, package scope
196         // (perhaps in another file), or universe scope --- collect
197         // them so that they can be resolved later
198         ident.Obj = unresolved
199         p.unresolved = append(p.unresolved, ident)
200 }
201
202 // ----------------------------------------------------------------------------
203 // Parsing support
204
205 func (p *parser) printTrace(a ...interface{}) {
206         const dots = ". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . " +
207                 ". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . "
208         const n = uint(len(dots))
209         pos := p.file.Position(p.pos)
210         fmt.Printf("%5d:%3d: ", pos.Line, pos.Column)
211         i := 2 * p.indent
212         for ; i > n; i -= n {
213                 fmt.Print(dots)
214         }
215         fmt.Print(dots[0:i])
216         fmt.Println(a...)
217 }
218
219 func trace(p *parser, msg string) *parser {
220         p.printTrace(msg, "(")
221         p.indent++
222         return p
223 }
224
225 // Usage pattern: defer un(trace(p, "..."));
226 func un(p *parser) {
227         p.indent--
228         p.printTrace(")")
229 }
230
231 // Advance to the next token.
232 func (p *parser) next0() {
233         // Because of one-token look-ahead, print the previous token
234         // when tracing as it provides a more readable output. The
235         // very first token (!p.pos.IsValid()) is not initialized
236         // (it is token.ILLEGAL), so don't print it .
237         if p.trace && p.pos.IsValid() {
238                 s := p.tok.String()
239                 switch {
240                 case p.tok.IsLiteral():
241                         p.printTrace(s, p.lit)
242                 case p.tok.IsOperator(), p.tok.IsKeyword():
243                         p.printTrace("\"" + s + "\"")
244                 default:
245                         p.printTrace(s)
246                 }
247         }
248
249         p.pos, p.tok, p.lit = p.scanner.Scan()
250 }
251
252 // Consume a comment and return it and the line on which it ends.
253 func (p *parser) consumeComment() (comment *ast.Comment, endline int) {
254         // /*-style comments may end on a different line than where they start.
255         // Scan the comment for '\n' chars and adjust endline accordingly.
256         endline = p.file.Line(p.pos)
257         if p.lit[1] == '*' {
258                 // don't use range here - no need to decode Unicode code points
259                 for i := 0; i < len(p.lit); i++ {
260                         if p.lit[i] == '\n' {
261                                 endline++
262                         }
263                 }
264         }
265
266         comment = &ast.Comment{p.pos, p.lit}
267         p.next0()
268
269         return
270 }
271
272 // Consume a group of adjacent comments, add it to the parser's
273 // comments list, and return it together with the line at which
274 // the last comment in the group ends. An empty line or non-comment
275 // token terminates a comment group.
276 //
277 func (p *parser) consumeCommentGroup() (comments *ast.CommentGroup, endline int) {
278         var list []*ast.Comment
279         endline = p.file.Line(p.pos)
280         for p.tok == token.COMMENT && endline+1 >= p.file.Line(p.pos) {
281                 var comment *ast.Comment
282                 comment, endline = p.consumeComment()
283                 list = append(list, comment)
284         }
285
286         // add comment group to the comments list
287         comments = &ast.CommentGroup{list}
288         p.comments = append(p.comments, comments)
289
290         return
291 }
292
293 // Advance to the next non-comment token. In the process, collect
294 // any comment groups encountered, and remember the last lead and
295 // and line comments.
296 //
297 // A lead comment is a comment group that starts and ends in a
298 // line without any other tokens and that is followed by a non-comment
299 // token on the line immediately after the comment group.
300 //
301 // A line comment is a comment group that follows a non-comment
302 // token on the same line, and that has no tokens after it on the line
303 // where it ends.
304 //
305 // Lead and line comments may be considered documentation that is
306 // stored in the AST.
307 //
308 func (p *parser) next() {
309         p.leadComment = nil
310         p.lineComment = nil
311         line := p.file.Line(p.pos) // current line
312         p.next0()
313
314         if p.tok == token.COMMENT {
315                 var comment *ast.CommentGroup
316                 var endline int
317
318                 if p.file.Line(p.pos) == line {
319                         // The comment is on same line as the previous token; it
320                         // cannot be a lead comment but may be a line comment.
321                         comment, endline = p.consumeCommentGroup()
322                         if p.file.Line(p.pos) != endline {
323                                 // The next token is on a different line, thus
324                                 // the last comment group is a line comment.
325                                 p.lineComment = comment
326                         }
327                 }
328
329                 // consume successor comments, if any
330                 endline = -1
331                 for p.tok == token.COMMENT {
332                         comment, endline = p.consumeCommentGroup()
333                 }
334
335                 if endline+1 == p.file.Line(p.pos) {
336                         // The next token is following on the line immediately after the
337                         // comment group, thus the last comment group is a lead comment.
338                         p.leadComment = comment
339                 }
340         }
341 }
342
343 func (p *parser) error(pos token.Pos, msg string) {
344         p.Error(p.file.Position(pos), msg)
345 }
346
347 func (p *parser) errorExpected(pos token.Pos, msg string) {
348         msg = "expected " + msg
349         if pos == p.pos {
350                 // the error happened at the current position;
351                 // make the error message more specific
352                 if p.tok == token.SEMICOLON && p.lit[0] == '\n' {
353                         msg += ", found newline"
354                 } else {
355                         msg += ", found '" + p.tok.String() + "'"
356                         if p.tok.IsLiteral() {
357                                 msg += " " + p.lit
358                         }
359                 }
360         }
361         p.error(pos, msg)
362 }
363
364 func (p *parser) expect(tok token.Token) token.Pos {
365         pos := p.pos
366         if p.tok != tok {
367                 p.errorExpected(pos, "'"+tok.String()+"'")
368         }
369         p.next() // make progress
370         return pos
371 }
372
373 func (p *parser) expectSemi() {
374         if p.tok != token.RPAREN && p.tok != token.RBRACE {
375                 p.expect(token.SEMICOLON)
376         }
377 }
378
379 func assert(cond bool, msg string) {
380         if !cond {
381                 panic("go/parser internal error: " + msg)
382         }
383 }
384
385 // ----------------------------------------------------------------------------
386 // Identifiers
387
388 func (p *parser) parseIdent() *ast.Ident {
389         pos := p.pos
390         name := "_"
391         if p.tok == token.IDENT {
392                 name = p.lit
393                 p.next()
394         } else {
395                 p.expect(token.IDENT) // use expect() error handling
396         }
397         return &ast.Ident{pos, name, nil}
398 }
399
400 func (p *parser) parseIdentList() (list []*ast.Ident) {
401         if p.trace {
402                 defer un(trace(p, "IdentList"))
403         }
404
405         list = append(list, p.parseIdent())
406         for p.tok == token.COMMA {
407                 p.next()
408                 list = append(list, p.parseIdent())
409         }
410
411         return
412 }
413
414 // ----------------------------------------------------------------------------
415 // Common productions
416
417 // If lhs is set, result list elements which are identifiers are not resolved.
418 func (p *parser) parseExprList(lhs bool) (list []ast.Expr) {
419         if p.trace {
420                 defer un(trace(p, "ExpressionList"))
421         }
422
423         list = append(list, p.checkExpr(p.parseExpr(lhs)))
424         for p.tok == token.COMMA {
425                 p.next()
426                 list = append(list, p.checkExpr(p.parseExpr(lhs)))
427         }
428
429         return
430 }
431
432 func (p *parser) parseLhsList() []ast.Expr {
433         list := p.parseExprList(true)
434         switch p.tok {
435         case token.DEFINE:
436                 // lhs of a short variable declaration
437                 // but doesn't enter scope until later:
438                 // caller must call p.shortVarDecl(p.makeIdentList(list))
439                 // at appropriate time.
440         case token.COLON:
441                 // lhs of a label declaration or a communication clause of a select
442                 // statement (parseLhsList is not called when parsing the case clause
443                 // of a switch statement):
444                 // - labels are declared by the caller of parseLhsList
445                 // - for communication clauses, if there is a stand-alone identifier
446                 //   followed by a colon, we have a syntax error; there is no need
447                 //   to resolve the identifier in that case
448         default:
449                 // identifiers must be declared elsewhere
450                 for _, x := range list {
451                         p.resolve(x)
452                 }
453         }
454         return list
455 }
456
457 func (p *parser) parseRhsList() []ast.Expr {
458         return p.parseExprList(false)
459 }
460
461 // ----------------------------------------------------------------------------
462 // Types
463
464 func (p *parser) parseType() ast.Expr {
465         if p.trace {
466                 defer un(trace(p, "Type"))
467         }
468
469         typ := p.tryType()
470
471         if typ == nil {
472                 pos := p.pos
473                 p.errorExpected(pos, "type")
474                 p.next() // make progress
475                 return &ast.BadExpr{pos, p.pos}
476         }
477
478         return typ
479 }
480
481 // If the result is an identifier, it is not resolved.
482 func (p *parser) parseTypeName() ast.Expr {
483         if p.trace {
484                 defer un(trace(p, "TypeName"))
485         }
486
487         ident := p.parseIdent()
488         // don't resolve ident yet - it may be a parameter or field name
489
490         if p.tok == token.PERIOD {
491                 // ident is a package name
492                 p.next()
493                 p.resolve(ident)
494                 sel := p.parseIdent()
495                 return &ast.SelectorExpr{ident, sel}
496         }
497
498         return ident
499 }
500
501 func (p *parser) parseArrayType(ellipsisOk bool) ast.Expr {
502         if p.trace {
503                 defer un(trace(p, "ArrayType"))
504         }
505
506         lbrack := p.expect(token.LBRACK)
507         var len ast.Expr
508         if ellipsisOk && p.tok == token.ELLIPSIS {
509                 len = &ast.Ellipsis{p.pos, nil}
510                 p.next()
511         } else if p.tok != token.RBRACK {
512                 len = p.parseRhs()
513         }
514         p.expect(token.RBRACK)
515         elt := p.parseType()
516
517         return &ast.ArrayType{lbrack, len, elt}
518 }
519
520 func (p *parser) makeIdentList(list []ast.Expr) []*ast.Ident {
521         idents := make([]*ast.Ident, len(list))
522         for i, x := range list {
523                 ident, isIdent := x.(*ast.Ident)
524                 if !isIdent {
525                         pos := x.(ast.Expr).Pos()
526                         p.errorExpected(pos, "identifier")
527                         ident = &ast.Ident{pos, "_", nil}
528                 }
529                 idents[i] = ident
530         }
531         return idents
532 }
533
534 func (p *parser) parseFieldDecl(scope *ast.Scope) *ast.Field {
535         if p.trace {
536                 defer un(trace(p, "FieldDecl"))
537         }
538
539         doc := p.leadComment
540
541         // fields
542         list, typ := p.parseVarList(false)
543
544         // optional tag
545         var tag *ast.BasicLit
546         if p.tok == token.STRING {
547                 tag = &ast.BasicLit{p.pos, p.tok, p.lit}
548                 p.next()
549         }
550
551         // analyze case
552         var idents []*ast.Ident
553         if typ != nil {
554                 // IdentifierList Type
555                 idents = p.makeIdentList(list)
556         } else {
557                 // ["*"] TypeName (AnonymousField)
558                 typ = list[0] // we always have at least one element
559                 p.resolve(typ)
560                 if n := len(list); n > 1 || !isTypeName(deref(typ)) {
561                         pos := typ.Pos()
562                         p.errorExpected(pos, "anonymous field")
563                         typ = &ast.BadExpr{pos, list[n-1].End()}
564                 }
565         }
566
567         p.expectSemi() // call before accessing p.linecomment
568
569         field := &ast.Field{doc, idents, typ, tag, p.lineComment}
570         p.declare(field, nil, scope, ast.Var, idents...)
571
572         return field
573 }
574
575 func (p *parser) parseStructType() *ast.StructType {
576         if p.trace {
577                 defer un(trace(p, "StructType"))
578         }
579
580         pos := p.expect(token.STRUCT)
581         lbrace := p.expect(token.LBRACE)
582         scope := ast.NewScope(nil) // struct scope
583         var list []*ast.Field
584         for p.tok == token.IDENT || p.tok == token.MUL || p.tok == token.LPAREN {
585                 // a field declaration cannot start with a '(' but we accept
586                 // it here for more robust parsing and better error messages
587                 // (parseFieldDecl will check and complain if necessary)
588                 list = append(list, p.parseFieldDecl(scope))
589         }
590         rbrace := p.expect(token.RBRACE)
591
592         return &ast.StructType{pos, &ast.FieldList{lbrace, list, rbrace}, false}
593 }
594
595 func (p *parser) parsePointerType() *ast.StarExpr {
596         if p.trace {
597                 defer un(trace(p, "PointerType"))
598         }
599
600         star := p.expect(token.MUL)
601         base := p.parseType()
602
603         return &ast.StarExpr{star, base}
604 }
605
606 func (p *parser) tryVarType(isParam bool) ast.Expr {
607         if isParam && p.tok == token.ELLIPSIS {
608                 pos := p.pos
609                 p.next()
610                 typ := p.tryIdentOrType(isParam) // don't use parseType so we can provide better error message
611                 if typ == nil {
612                         p.error(pos, "'...' parameter is missing type")
613                         typ = &ast.BadExpr{pos, p.pos}
614                 }
615                 return &ast.Ellipsis{pos, typ}
616         }
617         return p.tryIdentOrType(false)
618 }
619
620 func (p *parser) parseVarType(isParam bool) ast.Expr {
621         typ := p.tryVarType(isParam)
622         if typ == nil {
623                 pos := p.pos
624                 p.errorExpected(pos, "type")
625                 p.next() // make progress
626                 typ = &ast.BadExpr{pos, p.pos}
627         }
628         return typ
629 }
630
631 func (p *parser) parseVarList(isParam bool) (list []ast.Expr, typ ast.Expr) {
632         if p.trace {
633                 defer un(trace(p, "VarList"))
634         }
635
636         // a list of identifiers looks like a list of type names
637         //
638         // parse/tryVarType accepts any type (including parenthesized
639         // ones) even though the syntax does not permit them here: we
640         // accept them all for more robust parsing and complain later
641         for typ := p.parseVarType(isParam); typ != nil; {
642                 list = append(list, typ)
643                 if p.tok != token.COMMA {
644                         break
645                 }
646                 p.next()
647                 typ = p.tryVarType(isParam) // maybe nil as in: func f(int,) {}
648         }
649
650         // if we had a list of identifiers, it must be followed by a type
651         if typ = p.tryVarType(isParam); typ != nil {
652                 p.resolve(typ)
653         }
654
655         return
656 }
657
658 func (p *parser) parseParameterList(scope *ast.Scope, ellipsisOk bool) (params []*ast.Field) {
659         if p.trace {
660                 defer un(trace(p, "ParameterList"))
661         }
662
663         list, typ := p.parseVarList(ellipsisOk)
664         if typ != nil {
665                 // IdentifierList Type
666                 idents := p.makeIdentList(list)
667                 field := &ast.Field{nil, idents, typ, nil, nil}
668                 params = append(params, field)
669                 // Go spec: The scope of an identifier denoting a function
670                 // parameter or result variable is the function body.
671                 p.declare(field, nil, scope, ast.Var, idents...)
672                 if p.tok == token.COMMA {
673                         p.next()
674                 }
675
676                 for p.tok != token.RPAREN && p.tok != token.EOF {
677                         idents := p.parseIdentList()
678                         typ := p.parseVarType(ellipsisOk)
679                         field := &ast.Field{nil, idents, typ, nil, nil}
680                         params = append(params, field)
681                         // Go spec: The scope of an identifier denoting a function
682                         // parameter or result variable is the function body.
683                         p.declare(field, nil, scope, ast.Var, idents...)
684                         if p.tok != token.COMMA {
685                                 break
686                         }
687                         p.next()
688                 }
689
690         } else {
691                 // Type { "," Type } (anonymous parameters)
692                 params = make([]*ast.Field, len(list))
693                 for i, x := range list {
694                         p.resolve(x)
695                         params[i] = &ast.Field{Type: x}
696                 }
697         }
698
699         return
700 }
701
702 func (p *parser) parseParameters(scope *ast.Scope, ellipsisOk bool) *ast.FieldList {
703         if p.trace {
704                 defer un(trace(p, "Parameters"))
705         }
706
707         var params []*ast.Field
708         lparen := p.expect(token.LPAREN)
709         if p.tok != token.RPAREN {
710                 params = p.parseParameterList(scope, ellipsisOk)
711         }
712         rparen := p.expect(token.RPAREN)
713
714         return &ast.FieldList{lparen, params, rparen}
715 }
716
717 func (p *parser) parseResult(scope *ast.Scope) *ast.FieldList {
718         if p.trace {
719                 defer un(trace(p, "Result"))
720         }
721
722         if p.tok == token.LPAREN {
723                 return p.parseParameters(scope, false)
724         }
725
726         typ := p.tryType()
727         if typ != nil {
728                 list := make([]*ast.Field, 1)
729                 list[0] = &ast.Field{Type: typ}
730                 return &ast.FieldList{List: list}
731         }
732
733         return nil
734 }
735
736 func (p *parser) parseSignature(scope *ast.Scope) (params, results *ast.FieldList) {
737         if p.trace {
738                 defer un(trace(p, "Signature"))
739         }
740
741         params = p.parseParameters(scope, true)
742         results = p.parseResult(scope)
743
744         return
745 }
746
747 func (p *parser) parseFuncType() (*ast.FuncType, *ast.Scope) {
748         if p.trace {
749                 defer un(trace(p, "FuncType"))
750         }
751
752         pos := p.expect(token.FUNC)
753         scope := ast.NewScope(p.topScope) // function scope
754         params, results := p.parseSignature(scope)
755
756         return &ast.FuncType{pos, params, results}, scope
757 }
758
759 func (p *parser) parseMethodSpec(scope *ast.Scope) *ast.Field {
760         if p.trace {
761                 defer un(trace(p, "MethodSpec"))
762         }
763
764         doc := p.leadComment
765         var idents []*ast.Ident
766         var typ ast.Expr
767         x := p.parseTypeName()
768         if ident, isIdent := x.(*ast.Ident); isIdent && p.tok == token.LPAREN {
769                 // method
770                 idents = []*ast.Ident{ident}
771                 scope := ast.NewScope(nil) // method scope
772                 params, results := p.parseSignature(scope)
773                 typ = &ast.FuncType{token.NoPos, params, results}
774         } else {
775                 // embedded interface
776                 typ = x
777                 p.resolve(typ)
778         }
779         p.expectSemi() // call before accessing p.linecomment
780
781         spec := &ast.Field{doc, idents, typ, nil, p.lineComment}
782         p.declare(spec, nil, scope, ast.Fun, idents...)
783
784         return spec
785 }
786
787 func (p *parser) parseInterfaceType() *ast.InterfaceType {
788         if p.trace {
789                 defer un(trace(p, "InterfaceType"))
790         }
791
792         pos := p.expect(token.INTERFACE)
793         lbrace := p.expect(token.LBRACE)
794         scope := ast.NewScope(nil) // interface scope
795         var list []*ast.Field
796         for p.tok == token.IDENT {
797                 list = append(list, p.parseMethodSpec(scope))
798         }
799         rbrace := p.expect(token.RBRACE)
800
801         return &ast.InterfaceType{pos, &ast.FieldList{lbrace, list, rbrace}, false}
802 }
803
804 func (p *parser) parseMapType() *ast.MapType {
805         if p.trace {
806                 defer un(trace(p, "MapType"))
807         }
808
809         pos := p.expect(token.MAP)
810         p.expect(token.LBRACK)
811         key := p.parseType()
812         p.expect(token.RBRACK)
813         value := p.parseType()
814
815         return &ast.MapType{pos, key, value}
816 }
817
818 func (p *parser) parseChanType() *ast.ChanType {
819         if p.trace {
820                 defer un(trace(p, "ChanType"))
821         }
822
823         pos := p.pos
824         dir := ast.SEND | ast.RECV
825         if p.tok == token.CHAN {
826                 p.next()
827                 if p.tok == token.ARROW {
828                         p.next()
829                         dir = ast.SEND
830                 }
831         } else {
832                 p.expect(token.ARROW)
833                 p.expect(token.CHAN)
834                 dir = ast.RECV
835         }
836         value := p.parseType()
837
838         return &ast.ChanType{pos, dir, value}
839 }
840
841 // If the result is an identifier, it is not resolved.
842 func (p *parser) tryIdentOrType(ellipsisOk bool) ast.Expr {
843         switch p.tok {
844         case token.IDENT:
845                 return p.parseTypeName()
846         case token.LBRACK:
847                 return p.parseArrayType(ellipsisOk)
848         case token.STRUCT:
849                 return p.parseStructType()
850         case token.MUL:
851                 return p.parsePointerType()
852         case token.FUNC:
853                 typ, _ := p.parseFuncType()
854                 return typ
855         case token.INTERFACE:
856                 return p.parseInterfaceType()
857         case token.MAP:
858                 return p.parseMapType()
859         case token.CHAN, token.ARROW:
860                 return p.parseChanType()
861         case token.LPAREN:
862                 lparen := p.pos
863                 p.next()
864                 typ := p.parseType()
865                 rparen := p.expect(token.RPAREN)
866                 return &ast.ParenExpr{lparen, typ, rparen}
867         }
868
869         // no type found
870         return nil
871 }
872
873 func (p *parser) tryType() ast.Expr {
874         typ := p.tryIdentOrType(false)
875         if typ != nil {
876                 p.resolve(typ)
877         }
878         return typ
879 }
880
881 // ----------------------------------------------------------------------------
882 // Blocks
883
884 func (p *parser) parseStmtList() (list []ast.Stmt) {
885         if p.trace {
886                 defer un(trace(p, "StatementList"))
887         }
888
889         for p.tok != token.CASE && p.tok != token.DEFAULT && p.tok != token.RBRACE && p.tok != token.EOF {
890                 list = append(list, p.parseStmt())
891         }
892
893         return
894 }
895
896 func (p *parser) parseBody(scope *ast.Scope) *ast.BlockStmt {
897         if p.trace {
898                 defer un(trace(p, "Body"))
899         }
900
901         lbrace := p.expect(token.LBRACE)
902         p.topScope = scope // open function scope
903         p.openLabelScope()
904         list := p.parseStmtList()
905         p.closeLabelScope()
906         p.closeScope()
907         rbrace := p.expect(token.RBRACE)
908
909         return &ast.BlockStmt{lbrace, list, rbrace}
910 }
911
912 func (p *parser) parseBlockStmt() *ast.BlockStmt {
913         if p.trace {
914                 defer un(trace(p, "BlockStmt"))
915         }
916
917         lbrace := p.expect(token.LBRACE)
918         p.openScope()
919         list := p.parseStmtList()
920         p.closeScope()
921         rbrace := p.expect(token.RBRACE)
922
923         return &ast.BlockStmt{lbrace, list, rbrace}
924 }
925
926 // ----------------------------------------------------------------------------
927 // Expressions
928
929 func (p *parser) parseFuncTypeOrLit() ast.Expr {
930         if p.trace {
931                 defer un(trace(p, "FuncTypeOrLit"))
932         }
933
934         typ, scope := p.parseFuncType()
935         if p.tok != token.LBRACE {
936                 // function type only
937                 return typ
938         }
939
940         p.exprLev++
941         body := p.parseBody(scope)
942         p.exprLev--
943
944         return &ast.FuncLit{typ, body}
945 }
946
947 // parseOperand may return an expression or a raw type (incl. array
948 // types of the form [...]T. Callers must verify the result.
949 // If lhs is set and the result is an identifier, it is not resolved.
950 //
951 func (p *parser) parseOperand(lhs bool) ast.Expr {
952         if p.trace {
953                 defer un(trace(p, "Operand"))
954         }
955
956         switch p.tok {
957         case token.IDENT:
958                 x := p.parseIdent()
959                 if !lhs {
960                         p.resolve(x)
961                 }
962                 return x
963
964         case token.INT, token.FLOAT, token.IMAG, token.CHAR, token.STRING:
965                 x := &ast.BasicLit{p.pos, p.tok, p.lit}
966                 p.next()
967                 return x
968
969         case token.LPAREN:
970                 lparen := p.pos
971                 p.next()
972                 p.exprLev++
973                 x := p.parseRhsOrType() // types may be parenthesized: (some type)
974                 p.exprLev--
975                 rparen := p.expect(token.RPAREN)
976                 return &ast.ParenExpr{lparen, x, rparen}
977
978         case token.FUNC:
979                 return p.parseFuncTypeOrLit()
980
981         default:
982                 if typ := p.tryIdentOrType(true); typ != nil {
983                         // could be type for composite literal or conversion
984                         _, isIdent := typ.(*ast.Ident)
985                         assert(!isIdent, "type cannot be identifier")
986                         return typ
987                 }
988         }
989
990         pos := p.pos
991         p.errorExpected(pos, "operand")
992         p.next() // make progress
993         return &ast.BadExpr{pos, p.pos}
994 }
995
996 func (p *parser) parseSelector(x ast.Expr) ast.Expr {
997         if p.trace {
998                 defer un(trace(p, "Selector"))
999         }
1000
1001         sel := p.parseIdent()
1002
1003         return &ast.SelectorExpr{x, sel}
1004 }
1005
1006 func (p *parser) parseTypeAssertion(x ast.Expr) ast.Expr {
1007         if p.trace {
1008                 defer un(trace(p, "TypeAssertion"))
1009         }
1010
1011         p.expect(token.LPAREN)
1012         var typ ast.Expr
1013         if p.tok == token.TYPE {
1014                 // type switch: typ == nil
1015                 p.next()
1016         } else {
1017                 typ = p.parseType()
1018         }
1019         p.expect(token.RPAREN)
1020
1021         return &ast.TypeAssertExpr{x, typ}
1022 }
1023
1024 func (p *parser) parseIndexOrSlice(x ast.Expr) ast.Expr {
1025         if p.trace {
1026                 defer un(trace(p, "IndexOrSlice"))
1027         }
1028
1029         lbrack := p.expect(token.LBRACK)
1030         p.exprLev++
1031         var low, high ast.Expr
1032         isSlice := false
1033         if p.tok != token.COLON {
1034                 low = p.parseRhs()
1035         }
1036         if p.tok == token.COLON {
1037                 isSlice = true
1038                 p.next()
1039                 if p.tok != token.RBRACK {
1040                         high = p.parseRhs()
1041                 }
1042         }
1043         p.exprLev--
1044         rbrack := p.expect(token.RBRACK)
1045
1046         if isSlice {
1047                 return &ast.SliceExpr{x, lbrack, low, high, rbrack}
1048         }
1049         return &ast.IndexExpr{x, lbrack, low, rbrack}
1050 }
1051
1052 func (p *parser) parseCallOrConversion(fun ast.Expr) *ast.CallExpr {
1053         if p.trace {
1054                 defer un(trace(p, "CallOrConversion"))
1055         }
1056
1057         lparen := p.expect(token.LPAREN)
1058         p.exprLev++
1059         var list []ast.Expr
1060         var ellipsis token.Pos
1061         for p.tok != token.RPAREN && p.tok != token.EOF && !ellipsis.IsValid() {
1062                 list = append(list, p.parseRhsOrType()) // builtins may expect a type: make(some type, ...)
1063                 if p.tok == token.ELLIPSIS {
1064                         ellipsis = p.pos
1065                         p.next()
1066                 }
1067                 if p.tok != token.COMMA {
1068                         break
1069                 }
1070                 p.next()
1071         }
1072         p.exprLev--
1073         rparen := p.expect(token.RPAREN)
1074
1075         return &ast.CallExpr{fun, lparen, list, ellipsis, rparen}
1076 }
1077
1078 func (p *parser) parseElement(keyOk bool) ast.Expr {
1079         if p.trace {
1080                 defer un(trace(p, "Element"))
1081         }
1082
1083         if p.tok == token.LBRACE {
1084                 return p.parseLiteralValue(nil)
1085         }
1086
1087         x := p.checkExpr(p.parseExpr(keyOk)) // don't resolve if map key
1088         if keyOk {
1089                 if p.tok == token.COLON {
1090                         colon := p.pos
1091                         p.next()
1092                         return &ast.KeyValueExpr{x, colon, p.parseElement(false)}
1093                 }
1094                 p.resolve(x) // not a map key
1095         }
1096
1097         return x
1098 }
1099
1100 func (p *parser) parseElementList() (list []ast.Expr) {
1101         if p.trace {
1102                 defer un(trace(p, "ElementList"))
1103         }
1104
1105         for p.tok != token.RBRACE && p.tok != token.EOF {
1106                 list = append(list, p.parseElement(true))
1107                 if p.tok != token.COMMA {
1108                         break
1109                 }
1110                 p.next()
1111         }
1112
1113         return
1114 }
1115
1116 func (p *parser) parseLiteralValue(typ ast.Expr) ast.Expr {
1117         if p.trace {
1118                 defer un(trace(p, "LiteralValue"))
1119         }
1120
1121         lbrace := p.expect(token.LBRACE)
1122         var elts []ast.Expr
1123         p.exprLev++
1124         if p.tok != token.RBRACE {
1125                 elts = p.parseElementList()
1126         }
1127         p.exprLev--
1128         rbrace := p.expect(token.RBRACE)
1129         return &ast.CompositeLit{typ, lbrace, elts, rbrace}
1130 }
1131
1132 // checkExpr checks that x is an expression (and not a type).
1133 func (p *parser) checkExpr(x ast.Expr) ast.Expr {
1134         switch unparen(x).(type) {
1135         case *ast.BadExpr:
1136         case *ast.Ident:
1137         case *ast.BasicLit:
1138         case *ast.FuncLit:
1139         case *ast.CompositeLit:
1140         case *ast.ParenExpr:
1141                 panic("unreachable")
1142         case *ast.SelectorExpr:
1143         case *ast.IndexExpr:
1144         case *ast.SliceExpr:
1145         case *ast.TypeAssertExpr:
1146                 // If t.Type == nil we have a type assertion of the form
1147                 // y.(type), which is only allowed in type switch expressions.
1148                 // It's hard to exclude those but for the case where we are in
1149                 // a type switch. Instead be lenient and test this in the type
1150                 // checker.
1151         case *ast.CallExpr:
1152         case *ast.StarExpr:
1153         case *ast.UnaryExpr:
1154         case *ast.BinaryExpr:
1155         default:
1156                 // all other nodes are not proper expressions
1157                 p.errorExpected(x.Pos(), "expression")
1158                 x = &ast.BadExpr{x.Pos(), x.End()}
1159         }
1160         return x
1161 }
1162
1163 // isTypeName returns true iff x is a (qualified) TypeName.
1164 func isTypeName(x ast.Expr) bool {
1165         switch t := x.(type) {
1166         case *ast.BadExpr:
1167         case *ast.Ident:
1168         case *ast.SelectorExpr:
1169                 _, isIdent := t.X.(*ast.Ident)
1170                 return isIdent
1171         default:
1172                 return false // all other nodes are not type names
1173         }
1174         return true
1175 }
1176
1177 // isLiteralType returns true iff x is a legal composite literal type.
1178 func isLiteralType(x ast.Expr) bool {
1179         switch t := x.(type) {
1180         case *ast.BadExpr:
1181         case *ast.Ident:
1182         case *ast.SelectorExpr:
1183                 _, isIdent := t.X.(*ast.Ident)
1184                 return isIdent
1185         case *ast.ArrayType:
1186         case *ast.StructType:
1187         case *ast.MapType:
1188         default:
1189                 return false // all other nodes are not legal composite literal types
1190         }
1191         return true
1192 }
1193
1194 // If x is of the form *T, deref returns T, otherwise it returns x.
1195 func deref(x ast.Expr) ast.Expr {
1196         if p, isPtr := x.(*ast.StarExpr); isPtr {
1197                 x = p.X
1198         }
1199         return x
1200 }
1201
1202 // If x is of the form (T), unparen returns unparen(T), otherwise it returns x.
1203 func unparen(x ast.Expr) ast.Expr {
1204         if p, isParen := x.(*ast.ParenExpr); isParen {
1205                 x = unparen(p.X)
1206         }
1207         return x
1208 }
1209
1210 // checkExprOrType checks that x is an expression or a type
1211 // (and not a raw type such as [...]T).
1212 //
1213 func (p *parser) checkExprOrType(x ast.Expr) ast.Expr {
1214         switch t := unparen(x).(type) {
1215         case *ast.ParenExpr:
1216                 panic("unreachable")
1217         case *ast.UnaryExpr:
1218         case *ast.ArrayType:
1219                 if len, isEllipsis := t.Len.(*ast.Ellipsis); isEllipsis {
1220                         p.error(len.Pos(), "expected array length, found '...'")
1221                         x = &ast.BadExpr{x.Pos(), x.End()}
1222                 }
1223         }
1224
1225         // all other nodes are expressions or types
1226         return x
1227 }
1228
1229 // If lhs is set and the result is an identifier, it is not resolved.
1230 func (p *parser) parsePrimaryExpr(lhs bool) ast.Expr {
1231         if p.trace {
1232                 defer un(trace(p, "PrimaryExpr"))
1233         }
1234
1235         x := p.parseOperand(lhs)
1236 L:
1237         for {
1238                 switch p.tok {
1239                 case token.PERIOD:
1240                         p.next()
1241                         if lhs {
1242                                 p.resolve(x)
1243                         }
1244                         switch p.tok {
1245                         case token.IDENT:
1246                                 x = p.parseSelector(p.checkExpr(x))
1247                         case token.LPAREN:
1248                                 x = p.parseTypeAssertion(p.checkExpr(x))
1249                         default:
1250                                 pos := p.pos
1251                                 p.next() // make progress
1252                                 p.errorExpected(pos, "selector or type assertion")
1253                                 x = &ast.BadExpr{pos, p.pos}
1254                         }
1255                 case token.LBRACK:
1256                         if lhs {
1257                                 p.resolve(x)
1258                         }
1259                         x = p.parseIndexOrSlice(p.checkExpr(x))
1260                 case token.LPAREN:
1261                         if lhs {
1262                                 p.resolve(x)
1263                         }
1264                         x = p.parseCallOrConversion(p.checkExprOrType(x))
1265                 case token.LBRACE:
1266                         if isLiteralType(x) && (p.exprLev >= 0 || !isTypeName(x)) {
1267                                 if lhs {
1268                                         p.resolve(x)
1269                                 }
1270                                 x = p.parseLiteralValue(x)
1271                         } else {
1272                                 break L
1273                         }
1274                 default:
1275                         break L
1276                 }
1277                 lhs = false // no need to try to resolve again
1278         }
1279
1280         return x
1281 }
1282
1283 // If lhs is set and the result is an identifier, it is not resolved.
1284 func (p *parser) parseUnaryExpr(lhs bool) ast.Expr {
1285         if p.trace {
1286                 defer un(trace(p, "UnaryExpr"))
1287         }
1288
1289         switch p.tok {
1290         case token.ADD, token.SUB, token.NOT, token.XOR, token.AND:
1291                 pos, op := p.pos, p.tok
1292                 p.next()
1293                 x := p.parseUnaryExpr(false)
1294                 return &ast.UnaryExpr{pos, op, p.checkExpr(x)}
1295
1296         case token.ARROW:
1297                 // channel type or receive expression
1298                 pos := p.pos
1299                 p.next()
1300                 if p.tok == token.CHAN {
1301                         p.next()
1302                         value := p.parseType()
1303                         return &ast.ChanType{pos, ast.RECV, value}
1304                 }
1305
1306                 x := p.parseUnaryExpr(false)
1307                 return &ast.UnaryExpr{pos, token.ARROW, p.checkExpr(x)}
1308
1309         case token.MUL:
1310                 // pointer type or unary "*" expression
1311                 pos := p.pos
1312                 p.next()
1313                 x := p.parseUnaryExpr(false)
1314                 return &ast.StarExpr{pos, p.checkExprOrType(x)}
1315         }
1316
1317         return p.parsePrimaryExpr(lhs)
1318 }
1319
1320 // If lhs is set and the result is an identifier, it is not resolved.
1321 func (p *parser) parseBinaryExpr(lhs bool, prec1 int) ast.Expr {
1322         if p.trace {
1323                 defer un(trace(p, "BinaryExpr"))
1324         }
1325
1326         x := p.parseUnaryExpr(lhs)
1327         for prec := p.tok.Precedence(); prec >= prec1; prec-- {
1328                 for p.tok.Precedence() == prec {
1329                         pos, op := p.pos, p.tok
1330                         p.next()
1331                         if lhs {
1332                                 p.resolve(x)
1333                                 lhs = false
1334                         }
1335                         y := p.parseBinaryExpr(false, prec+1)
1336                         x = &ast.BinaryExpr{p.checkExpr(x), pos, op, p.checkExpr(y)}
1337                 }
1338         }
1339
1340         return x
1341 }
1342
1343 // If lhs is set and the result is an identifier, it is not resolved.
1344 // The result may be a type or even a raw type ([...]int). Callers must
1345 // check the result (using checkExpr or checkExprOrType), depending on
1346 // context.
1347 func (p *parser) parseExpr(lhs bool) ast.Expr {
1348         if p.trace {
1349                 defer un(trace(p, "Expression"))
1350         }
1351
1352         return p.parseBinaryExpr(lhs, token.LowestPrec+1)
1353 }
1354
1355 func (p *parser) parseRhs() ast.Expr {
1356         return p.checkExpr(p.parseExpr(false))
1357 }
1358
1359 func (p *parser) parseRhsOrType() ast.Expr {
1360         return p.checkExprOrType(p.parseExpr(false))
1361 }
1362
1363 // ----------------------------------------------------------------------------
1364 // Statements
1365
1366 // Parsing modes for parseSimpleStmt.
1367 const (
1368         basic = iota
1369         labelOk
1370         rangeOk
1371 )
1372
1373 // parseSimpleStmt returns true as 2nd result if it parsed the assignment
1374 // of a range clause (with mode == rangeOk). The returned statement is an
1375 // assignment with a right-hand side that is a single unary expression of
1376 // the form "range x". No guarantees are given for the left-hand side.
1377 func (p *parser) parseSimpleStmt(mode int) (ast.Stmt, bool) {
1378         if p.trace {
1379                 defer un(trace(p, "SimpleStmt"))
1380         }
1381
1382         x := p.parseLhsList()
1383
1384         switch p.tok {
1385         case
1386                 token.DEFINE, token.ASSIGN, token.ADD_ASSIGN,
1387                 token.SUB_ASSIGN, token.MUL_ASSIGN, token.QUO_ASSIGN,
1388                 token.REM_ASSIGN, token.AND_ASSIGN, token.OR_ASSIGN,
1389                 token.XOR_ASSIGN, token.SHL_ASSIGN, token.SHR_ASSIGN, token.AND_NOT_ASSIGN:
1390                 // assignment statement, possibly part of a range clause
1391                 pos, tok := p.pos, p.tok
1392                 p.next()
1393                 var y []ast.Expr
1394                 isRange := false
1395                 if mode == rangeOk && p.tok == token.RANGE && (tok == token.DEFINE || tok == token.ASSIGN) {
1396                         pos := p.pos
1397                         p.next()
1398                         y = []ast.Expr{&ast.UnaryExpr{pos, token.RANGE, p.parseRhs()}}
1399                         isRange = true
1400                 } else {
1401                         y = p.parseRhsList()
1402                 }
1403                 if tok == token.DEFINE {
1404                         p.shortVarDecl(p.makeIdentList(x))
1405                 }
1406                 return &ast.AssignStmt{x, pos, tok, y}, isRange
1407         }
1408
1409         if len(x) > 1 {
1410                 p.errorExpected(x[0].Pos(), "1 expression")
1411                 // continue with first expression
1412         }
1413
1414         switch p.tok {
1415         case token.COLON:
1416                 // labeled statement
1417                 colon := p.pos
1418                 p.next()
1419                 if label, isIdent := x[0].(*ast.Ident); mode == labelOk && isIdent {
1420                         // Go spec: The scope of a label is the body of the function
1421                         // in which it is declared and excludes the body of any nested
1422                         // function.
1423                         stmt := &ast.LabeledStmt{label, colon, p.parseStmt()}
1424                         p.declare(stmt, nil, p.labelScope, ast.Lbl, label)
1425                         return stmt, false
1426                 }
1427                 // The label declaration typically starts at x[0].Pos(), but the label
1428                 // declaration may be erroneous due to a token after that position (and
1429                 // before the ':'). If SpuriousErrors is not set, the (only) error re-
1430                 // ported for the line is the illegal label error instead of the token
1431                 // before the ':' that caused the problem. Thus, use the (latest) colon
1432                 // position for error reporting.
1433                 p.error(colon, "illegal label declaration")
1434                 return &ast.BadStmt{x[0].Pos(), colon + 1}, false
1435
1436         case token.ARROW:
1437                 // send statement
1438                 arrow := p.pos
1439                 p.next()
1440                 y := p.parseRhs()
1441                 return &ast.SendStmt{x[0], arrow, y}, false
1442
1443         case token.INC, token.DEC:
1444                 // increment or decrement
1445                 s := &ast.IncDecStmt{x[0], p.pos, p.tok}
1446                 p.next()
1447                 return s, false
1448         }
1449
1450         // expression
1451         return &ast.ExprStmt{x[0]}, false
1452 }
1453
1454 func (p *parser) parseCallExpr() *ast.CallExpr {
1455         x := p.parseRhsOrType() // could be a conversion: (some type)(x)
1456         if call, isCall := x.(*ast.CallExpr); isCall {
1457                 return call
1458         }
1459         p.errorExpected(x.Pos(), "function/method call")
1460         return nil
1461 }
1462
1463 func (p *parser) parseGoStmt() ast.Stmt {
1464         if p.trace {
1465                 defer un(trace(p, "GoStmt"))
1466         }
1467
1468         pos := p.expect(token.GO)
1469         call := p.parseCallExpr()
1470         p.expectSemi()
1471         if call == nil {
1472                 return &ast.BadStmt{pos, pos + 2} // len("go")
1473         }
1474
1475         return &ast.GoStmt{pos, call}
1476 }
1477
1478 func (p *parser) parseDeferStmt() ast.Stmt {
1479         if p.trace {
1480                 defer un(trace(p, "DeferStmt"))
1481         }
1482
1483         pos := p.expect(token.DEFER)
1484         call := p.parseCallExpr()
1485         p.expectSemi()
1486         if call == nil {
1487                 return &ast.BadStmt{pos, pos + 5} // len("defer")
1488         }
1489
1490         return &ast.DeferStmt{pos, call}
1491 }
1492
1493 func (p *parser) parseReturnStmt() *ast.ReturnStmt {
1494         if p.trace {
1495                 defer un(trace(p, "ReturnStmt"))
1496         }
1497
1498         pos := p.pos
1499         p.expect(token.RETURN)
1500         var x []ast.Expr
1501         if p.tok != token.SEMICOLON && p.tok != token.RBRACE {
1502                 x = p.parseRhsList()
1503         }
1504         p.expectSemi()
1505
1506         return &ast.ReturnStmt{pos, x}
1507 }
1508
1509 func (p *parser) parseBranchStmt(tok token.Token) *ast.BranchStmt {
1510         if p.trace {
1511                 defer un(trace(p, "BranchStmt"))
1512         }
1513
1514         pos := p.expect(tok)
1515         var label *ast.Ident
1516         if tok != token.FALLTHROUGH && p.tok == token.IDENT {
1517                 label = p.parseIdent()
1518                 // add to list of unresolved targets
1519                 n := len(p.targetStack) - 1
1520                 p.targetStack[n] = append(p.targetStack[n], label)
1521         }
1522         p.expectSemi()
1523
1524         return &ast.BranchStmt{pos, tok, label}
1525 }
1526
1527 func (p *parser) makeExpr(s ast.Stmt) ast.Expr {
1528         if s == nil {
1529                 return nil
1530         }
1531         if es, isExpr := s.(*ast.ExprStmt); isExpr {
1532                 return p.checkExpr(es.X)
1533         }
1534         p.error(s.Pos(), "expected condition, found simple statement")
1535         return &ast.BadExpr{s.Pos(), s.End()}
1536 }
1537
1538 func (p *parser) parseIfStmt() *ast.IfStmt {
1539         if p.trace {
1540                 defer un(trace(p, "IfStmt"))
1541         }
1542
1543         pos := p.expect(token.IF)
1544         p.openScope()
1545         defer p.closeScope()
1546
1547         var s ast.Stmt
1548         var x ast.Expr
1549         {
1550                 prevLev := p.exprLev
1551                 p.exprLev = -1
1552                 if p.tok == token.SEMICOLON {
1553                         p.next()
1554                         x = p.parseRhs()
1555                 } else {
1556                         s, _ = p.parseSimpleStmt(basic)
1557                         if p.tok == token.SEMICOLON {
1558                                 p.next()
1559                                 x = p.parseRhs()
1560                         } else {
1561                                 x = p.makeExpr(s)
1562                                 s = nil
1563                         }
1564                 }
1565                 p.exprLev = prevLev
1566         }
1567
1568         body := p.parseBlockStmt()
1569         var else_ ast.Stmt
1570         if p.tok == token.ELSE {
1571                 p.next()
1572                 else_ = p.parseStmt()
1573         } else {
1574                 p.expectSemi()
1575         }
1576
1577         return &ast.IfStmt{pos, s, x, body, else_}
1578 }
1579
1580 func (p *parser) parseTypeList() (list []ast.Expr) {
1581         if p.trace {
1582                 defer un(trace(p, "TypeList"))
1583         }
1584
1585         list = append(list, p.parseType())
1586         for p.tok == token.COMMA {
1587                 p.next()
1588                 list = append(list, p.parseType())
1589         }
1590
1591         return
1592 }
1593
1594 func (p *parser) parseCaseClause(typeSwitch bool) *ast.CaseClause {
1595         if p.trace {
1596                 defer un(trace(p, "CaseClause"))
1597         }
1598
1599         pos := p.pos
1600         var list []ast.Expr
1601         if p.tok == token.CASE {
1602                 p.next()
1603                 if typeSwitch {
1604                         list = p.parseTypeList()
1605                 } else {
1606                         list = p.parseRhsList()
1607                 }
1608         } else {
1609                 p.expect(token.DEFAULT)
1610         }
1611
1612         colon := p.expect(token.COLON)
1613         p.openScope()
1614         body := p.parseStmtList()
1615         p.closeScope()
1616
1617         return &ast.CaseClause{pos, list, colon, body}
1618 }
1619
1620 func isTypeSwitchAssert(x ast.Expr) bool {
1621         a, ok := x.(*ast.TypeAssertExpr)
1622         return ok && a.Type == nil
1623 }
1624
1625 func isTypeSwitchGuard(s ast.Stmt) bool {
1626         switch t := s.(type) {
1627         case *ast.ExprStmt:
1628                 // x.(nil)
1629                 return isTypeSwitchAssert(t.X)
1630         case *ast.AssignStmt:
1631                 // v := x.(nil)
1632                 return len(t.Lhs) == 1 && t.Tok == token.DEFINE && len(t.Rhs) == 1 && isTypeSwitchAssert(t.Rhs[0])
1633         }
1634         return false
1635 }
1636
1637 func (p *parser) parseSwitchStmt() ast.Stmt {
1638         if p.trace {
1639                 defer un(trace(p, "SwitchStmt"))
1640         }
1641
1642         pos := p.expect(token.SWITCH)
1643         p.openScope()
1644         defer p.closeScope()
1645
1646         var s1, s2 ast.Stmt
1647         if p.tok != token.LBRACE {
1648                 prevLev := p.exprLev
1649                 p.exprLev = -1
1650                 if p.tok != token.SEMICOLON {
1651                         s2, _ = p.parseSimpleStmt(basic)
1652                 }
1653                 if p.tok == token.SEMICOLON {
1654                         p.next()
1655                         s1 = s2
1656                         s2 = nil
1657                         if p.tok != token.LBRACE {
1658                                 // A TypeSwitchGuard may declare a variable in addition
1659                                 // to the variable declared in the initial SimpleStmt.
1660                                 // Introduce extra scope to avoid redeclaration errors:
1661                                 //
1662                                 //      switch t := 0; t := x.(T) { ... }
1663                                 //
1664                                 // (this code is not valid Go because the first t will
1665                                 // cannot be accessed and thus is never used, the extra
1666                                 // scope is needed for the correct error message).
1667                                 //
1668                                 // If we don't have a type switch, s2 must be an expression.
1669                                 // Having the extra nested but empty scope won't affect it.
1670                                 p.openScope()
1671                                 defer p.closeScope()
1672                                 s2, _ = p.parseSimpleStmt(basic)
1673                         }
1674                 }
1675                 p.exprLev = prevLev
1676         }
1677
1678         typeSwitch := isTypeSwitchGuard(s2)
1679         lbrace := p.expect(token.LBRACE)
1680         var list []ast.Stmt
1681         for p.tok == token.CASE || p.tok == token.DEFAULT {
1682                 list = append(list, p.parseCaseClause(typeSwitch))
1683         }
1684         rbrace := p.expect(token.RBRACE)
1685         p.expectSemi()
1686         body := &ast.BlockStmt{lbrace, list, rbrace}
1687
1688         if typeSwitch {
1689                 return &ast.TypeSwitchStmt{pos, s1, s2, body}
1690         }
1691
1692         return &ast.SwitchStmt{pos, s1, p.makeExpr(s2), body}
1693 }
1694
1695 func (p *parser) parseCommClause() *ast.CommClause {
1696         if p.trace {
1697                 defer un(trace(p, "CommClause"))
1698         }
1699
1700         p.openScope()
1701         pos := p.pos
1702         var comm ast.Stmt
1703         if p.tok == token.CASE {
1704                 p.next()
1705                 lhs := p.parseLhsList()
1706                 if p.tok == token.ARROW {
1707                         // SendStmt
1708                         if len(lhs) > 1 {
1709                                 p.errorExpected(lhs[0].Pos(), "1 expression")
1710                                 // continue with first expression
1711                         }
1712                         arrow := p.pos
1713                         p.next()
1714                         rhs := p.parseRhs()
1715                         comm = &ast.SendStmt{lhs[0], arrow, rhs}
1716                 } else {
1717                         // RecvStmt
1718                         pos := p.pos
1719                         tok := p.tok
1720                         var rhs ast.Expr
1721                         if tok == token.ASSIGN || tok == token.DEFINE {
1722                                 // RecvStmt with assignment
1723                                 if len(lhs) > 2 {
1724                                         p.errorExpected(lhs[0].Pos(), "1 or 2 expressions")
1725                                         // continue with first two expressions
1726                                         lhs = lhs[0:2]
1727                                 }
1728                                 p.next()
1729                                 rhs = p.parseRhs()
1730                                 if tok == token.DEFINE && lhs != nil {
1731                                         p.shortVarDecl(p.makeIdentList(lhs))
1732                                 }
1733                         } else {
1734                                 // rhs must be single receive operation
1735                                 if len(lhs) > 1 {
1736                                         p.errorExpected(lhs[0].Pos(), "1 expression")
1737                                         // continue with first expression
1738                                 }
1739                                 rhs = lhs[0]
1740                                 lhs = nil // there is no lhs
1741                         }
1742                         if lhs != nil {
1743                                 comm = &ast.AssignStmt{lhs, pos, tok, []ast.Expr{rhs}}
1744                         } else {
1745                                 comm = &ast.ExprStmt{rhs}
1746                         }
1747                 }
1748         } else {
1749                 p.expect(token.DEFAULT)
1750         }
1751
1752         colon := p.expect(token.COLON)
1753         body := p.parseStmtList()
1754         p.closeScope()
1755
1756         return &ast.CommClause{pos, comm, colon, body}
1757 }
1758
1759 func (p *parser) parseSelectStmt() *ast.SelectStmt {
1760         if p.trace {
1761                 defer un(trace(p, "SelectStmt"))
1762         }
1763
1764         pos := p.expect(token.SELECT)
1765         lbrace := p.expect(token.LBRACE)
1766         var list []ast.Stmt
1767         for p.tok == token.CASE || p.tok == token.DEFAULT {
1768                 list = append(list, p.parseCommClause())
1769         }
1770         rbrace := p.expect(token.RBRACE)
1771         p.expectSemi()
1772         body := &ast.BlockStmt{lbrace, list, rbrace}
1773
1774         return &ast.SelectStmt{pos, body}
1775 }
1776
1777 func (p *parser) parseForStmt() ast.Stmt {
1778         if p.trace {
1779                 defer un(trace(p, "ForStmt"))
1780         }
1781
1782         pos := p.expect(token.FOR)
1783         p.openScope()
1784         defer p.closeScope()
1785
1786         var s1, s2, s3 ast.Stmt
1787         var isRange bool
1788         if p.tok != token.LBRACE {
1789                 prevLev := p.exprLev
1790                 p.exprLev = -1
1791                 if p.tok != token.SEMICOLON {
1792                         s2, isRange = p.parseSimpleStmt(rangeOk)
1793                 }
1794                 if !isRange && p.tok == token.SEMICOLON {
1795                         p.next()
1796                         s1 = s2
1797                         s2 = nil
1798                         if p.tok != token.SEMICOLON {
1799                                 s2, _ = p.parseSimpleStmt(basic)
1800                         }
1801                         p.expectSemi()
1802                         if p.tok != token.LBRACE {
1803                                 s3, _ = p.parseSimpleStmt(basic)
1804                         }
1805                 }
1806                 p.exprLev = prevLev
1807         }
1808
1809         body := p.parseBlockStmt()
1810         p.expectSemi()
1811
1812         if isRange {
1813                 as := s2.(*ast.AssignStmt)
1814                 // check lhs
1815                 var key, value ast.Expr
1816                 switch len(as.Lhs) {
1817                 case 2:
1818                         key, value = as.Lhs[0], as.Lhs[1]
1819                 case 1:
1820                         key = as.Lhs[0]
1821                 default:
1822                         p.errorExpected(as.Lhs[0].Pos(), "1 or 2 expressions")
1823                         return &ast.BadStmt{pos, body.End()}
1824                 }
1825                 // parseSimpleStmt returned a right-hand side that
1826                 // is a single unary expression of the form "range x"
1827                 x := as.Rhs[0].(*ast.UnaryExpr).X
1828                 return &ast.RangeStmt{pos, key, value, as.TokPos, as.Tok, x, body}
1829         }
1830
1831         // regular for statement
1832         return &ast.ForStmt{pos, s1, p.makeExpr(s2), s3, body}
1833 }
1834
1835 func (p *parser) parseStmt() (s ast.Stmt) {
1836         if p.trace {
1837                 defer un(trace(p, "Statement"))
1838         }
1839
1840         switch p.tok {
1841         case token.CONST, token.TYPE, token.VAR:
1842                 s = &ast.DeclStmt{p.parseDecl()}
1843         case
1844                 // tokens that may start a top-level expression
1845                 token.IDENT, token.INT, token.FLOAT, token.CHAR, token.STRING, token.FUNC, token.LPAREN, // operand
1846                 token.LBRACK, token.STRUCT, // composite type
1847                 token.MUL, token.AND, token.ARROW, token.ADD, token.SUB, token.XOR: // unary operators
1848                 s, _ = p.parseSimpleStmt(labelOk)
1849                 // because of the required look-ahead, labeled statements are
1850                 // parsed by parseSimpleStmt - don't expect a semicolon after
1851                 // them
1852                 if _, isLabeledStmt := s.(*ast.LabeledStmt); !isLabeledStmt {
1853                         p.expectSemi()
1854                 }
1855         case token.GO:
1856                 s = p.parseGoStmt()
1857         case token.DEFER:
1858                 s = p.parseDeferStmt()
1859         case token.RETURN:
1860                 s = p.parseReturnStmt()
1861         case token.BREAK, token.CONTINUE, token.GOTO, token.FALLTHROUGH:
1862                 s = p.parseBranchStmt(p.tok)
1863         case token.LBRACE:
1864                 s = p.parseBlockStmt()
1865                 p.expectSemi()
1866         case token.IF:
1867                 s = p.parseIfStmt()
1868         case token.SWITCH:
1869                 s = p.parseSwitchStmt()
1870         case token.SELECT:
1871                 s = p.parseSelectStmt()
1872         case token.FOR:
1873                 s = p.parseForStmt()
1874         case token.SEMICOLON:
1875                 s = &ast.EmptyStmt{p.pos}
1876                 p.next()
1877         case token.RBRACE:
1878                 // a semicolon may be omitted before a closing "}"
1879                 s = &ast.EmptyStmt{p.pos}
1880         default:
1881                 // no statement found
1882                 pos := p.pos
1883                 p.errorExpected(pos, "statement")
1884                 p.next() // make progress
1885                 s = &ast.BadStmt{pos, p.pos}
1886         }
1887
1888         return
1889 }
1890
1891 // ----------------------------------------------------------------------------
1892 // Declarations
1893
1894 type parseSpecFunction func(p *parser, doc *ast.CommentGroup, iota int) ast.Spec
1895
1896 func parseImportSpec(p *parser, doc *ast.CommentGroup, _ int) ast.Spec {
1897         if p.trace {
1898                 defer un(trace(p, "ImportSpec"))
1899         }
1900
1901         var ident *ast.Ident
1902         switch p.tok {
1903         case token.PERIOD:
1904                 ident = &ast.Ident{p.pos, ".", nil}
1905                 p.next()
1906         case token.IDENT:
1907                 ident = p.parseIdent()
1908         }
1909
1910         var path *ast.BasicLit
1911         if p.tok == token.STRING {
1912                 path = &ast.BasicLit{p.pos, p.tok, p.lit}
1913                 p.next()
1914         } else {
1915                 p.expect(token.STRING) // use expect() error handling
1916         }
1917         p.expectSemi() // call before accessing p.linecomment
1918
1919         // collect imports
1920         spec := &ast.ImportSpec{doc, ident, path, p.lineComment, token.NoPos}
1921         p.imports = append(p.imports, spec)
1922
1923         return spec
1924 }
1925
1926 func parseConstSpec(p *parser, doc *ast.CommentGroup, iota int) ast.Spec {
1927         if p.trace {
1928                 defer un(trace(p, "ConstSpec"))
1929         }
1930
1931         idents := p.parseIdentList()
1932         typ := p.tryType()
1933         var values []ast.Expr
1934         if typ != nil || p.tok == token.ASSIGN || iota == 0 {
1935                 p.expect(token.ASSIGN)
1936                 values = p.parseRhsList()
1937         }
1938         p.expectSemi() // call before accessing p.linecomment
1939
1940         // Go spec: The scope of a constant or variable identifier declared inside
1941         // a function begins at the end of the ConstSpec or VarSpec and ends at
1942         // the end of the innermost containing block.
1943         // (Global identifiers are resolved in a separate phase after parsing.)
1944         spec := &ast.ValueSpec{doc, idents, typ, values, p.lineComment}
1945         p.declare(spec, iota, p.topScope, ast.Con, idents...)
1946
1947         return spec
1948 }
1949
1950 func parseTypeSpec(p *parser, doc *ast.CommentGroup, _ int) ast.Spec {
1951         if p.trace {
1952                 defer un(trace(p, "TypeSpec"))
1953         }
1954
1955         ident := p.parseIdent()
1956
1957         // Go spec: The scope of a type identifier declared inside a function begins
1958         // at the identifier in the TypeSpec and ends at the end of the innermost
1959         // containing block.
1960         // (Global identifiers are resolved in a separate phase after parsing.)
1961         spec := &ast.TypeSpec{doc, ident, nil, nil}
1962         p.declare(spec, nil, p.topScope, ast.Typ, ident)
1963
1964         spec.Type = p.parseType()
1965         p.expectSemi() // call before accessing p.linecomment
1966         spec.Comment = p.lineComment
1967
1968         return spec
1969 }
1970
1971 func parseVarSpec(p *parser, doc *ast.CommentGroup, _ int) ast.Spec {
1972         if p.trace {
1973                 defer un(trace(p, "VarSpec"))
1974         }
1975
1976         idents := p.parseIdentList()
1977         typ := p.tryType()
1978         var values []ast.Expr
1979         if typ == nil || p.tok == token.ASSIGN {
1980                 p.expect(token.ASSIGN)
1981                 values = p.parseRhsList()
1982         }
1983         p.expectSemi() // call before accessing p.linecomment
1984
1985         // Go spec: The scope of a constant or variable identifier declared inside
1986         // a function begins at the end of the ConstSpec or VarSpec and ends at
1987         // the end of the innermost containing block.
1988         // (Global identifiers are resolved in a separate phase after parsing.)
1989         spec := &ast.ValueSpec{doc, idents, typ, values, p.lineComment}
1990         p.declare(spec, nil, p.topScope, ast.Var, idents...)
1991
1992         return spec
1993 }
1994
1995 func (p *parser) parseGenDecl(keyword token.Token, f parseSpecFunction) *ast.GenDecl {
1996         if p.trace {
1997                 defer un(trace(p, "GenDecl("+keyword.String()+")"))
1998         }
1999
2000         doc := p.leadComment
2001         pos := p.expect(keyword)
2002         var lparen, rparen token.Pos
2003         var list []ast.Spec
2004         if p.tok == token.LPAREN {
2005                 lparen = p.pos
2006                 p.next()
2007                 for iota := 0; p.tok != token.RPAREN && p.tok != token.EOF; iota++ {
2008                         list = append(list, f(p, p.leadComment, iota))
2009                 }
2010                 rparen = p.expect(token.RPAREN)
2011                 p.expectSemi()
2012         } else {
2013                 list = append(list, f(p, nil, 0))
2014         }
2015
2016         return &ast.GenDecl{doc, pos, keyword, lparen, list, rparen}
2017 }
2018
2019 func (p *parser) parseReceiver(scope *ast.Scope) *ast.FieldList {
2020         if p.trace {
2021                 defer un(trace(p, "Receiver"))
2022         }
2023
2024         par := p.parseParameters(scope, false)
2025
2026         // must have exactly one receiver
2027         if par.NumFields() != 1 {
2028                 p.errorExpected(par.Opening, "exactly one receiver")
2029                 par.List = []*ast.Field{&ast.Field{Type: &ast.BadExpr{par.Opening, par.Closing + 1}}}
2030                 return par
2031         }
2032
2033         // recv type must be of the form ["*"] identifier
2034         recv := par.List[0]
2035         base := deref(recv.Type)
2036         if _, isIdent := base.(*ast.Ident); !isIdent {
2037                 p.errorExpected(base.Pos(), "(unqualified) identifier")
2038                 par.List = []*ast.Field{&ast.Field{Type: &ast.BadExpr{recv.Pos(), recv.End()}}}
2039         }
2040
2041         return par
2042 }
2043
2044 func (p *parser) parseFuncDecl() *ast.FuncDecl {
2045         if p.trace {
2046                 defer un(trace(p, "FunctionDecl"))
2047         }
2048
2049         doc := p.leadComment
2050         pos := p.expect(token.FUNC)
2051         scope := ast.NewScope(p.topScope) // function scope
2052
2053         var recv *ast.FieldList
2054         if p.tok == token.LPAREN {
2055                 recv = p.parseReceiver(scope)
2056         }
2057
2058         ident := p.parseIdent()
2059
2060         params, results := p.parseSignature(scope)
2061
2062         var body *ast.BlockStmt
2063         if p.tok == token.LBRACE {
2064                 body = p.parseBody(scope)
2065         }
2066         p.expectSemi()
2067
2068         decl := &ast.FuncDecl{doc, recv, ident, &ast.FuncType{pos, params, results}, body}
2069         if recv == nil {
2070                 // Go spec: The scope of an identifier denoting a constant, type,
2071                 // variable, or function (but not method) declared at top level
2072                 // (outside any function) is the package block.
2073                 //
2074                 // init() functions cannot be referred to and there may
2075                 // be more than one - don't put them in the pkgScope
2076                 if ident.Name != "init" {
2077                         p.declare(decl, nil, p.pkgScope, ast.Fun, ident)
2078                 }
2079         }
2080
2081         return decl
2082 }
2083
2084 func (p *parser) parseDecl() ast.Decl {
2085         if p.trace {
2086                 defer un(trace(p, "Declaration"))
2087         }
2088
2089         var f parseSpecFunction
2090         switch p.tok {
2091         case token.CONST:
2092                 f = parseConstSpec
2093
2094         case token.TYPE:
2095                 f = parseTypeSpec
2096
2097         case token.VAR:
2098                 f = parseVarSpec
2099
2100         case token.FUNC:
2101                 return p.parseFuncDecl()
2102
2103         default:
2104                 pos := p.pos
2105                 p.errorExpected(pos, "declaration")
2106                 p.next() // make progress
2107                 decl := &ast.BadDecl{pos, p.pos}
2108                 return decl
2109         }
2110
2111         return p.parseGenDecl(p.tok, f)
2112 }
2113
2114 func (p *parser) parseDeclList() (list []ast.Decl) {
2115         if p.trace {
2116                 defer un(trace(p, "DeclList"))
2117         }
2118
2119         for p.tok != token.EOF {
2120                 list = append(list, p.parseDecl())
2121         }
2122
2123         return
2124 }
2125
2126 // ----------------------------------------------------------------------------
2127 // Source files
2128
2129 func (p *parser) parseFile() *ast.File {
2130         if p.trace {
2131                 defer un(trace(p, "File"))
2132         }
2133
2134         // package clause
2135         doc := p.leadComment
2136         pos := p.expect(token.PACKAGE)
2137         // Go spec: The package clause is not a declaration;
2138         // the package name does not appear in any scope.
2139         ident := p.parseIdent()
2140         if ident.Name == "_" {
2141                 p.error(p.pos, "invalid package name _")
2142         }
2143         p.expectSemi()
2144
2145         var decls []ast.Decl
2146
2147         // Don't bother parsing the rest if we had errors already.
2148         // Likely not a Go source file at all.
2149
2150         if p.ErrorCount() == 0 && p.mode&PackageClauseOnly == 0 {
2151                 // import decls
2152                 for p.tok == token.IMPORT {
2153                         decls = append(decls, p.parseGenDecl(token.IMPORT, parseImportSpec))
2154                 }
2155
2156                 if p.mode&ImportsOnly == 0 {
2157                         // rest of package body
2158                         for p.tok != token.EOF {
2159                                 decls = append(decls, p.parseDecl())
2160                         }
2161                 }
2162         }
2163
2164         assert(p.topScope == p.pkgScope, "imbalanced scopes")
2165
2166         // resolve global identifiers within the same file
2167         i := 0
2168         for _, ident := range p.unresolved {
2169                 // i <= index for current ident
2170                 assert(ident.Obj == unresolved, "object already resolved")
2171                 ident.Obj = p.pkgScope.Lookup(ident.Name) // also removes unresolved sentinel
2172                 if ident.Obj == nil {
2173                         p.unresolved[i] = ident
2174                         i++
2175                 }
2176         }
2177
2178         return &ast.File{doc, pos, ident, decls, p.pkgScope, p.imports, p.unresolved[0:i], p.comments}
2179 }