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.
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.
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.
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
32 // The parser structure holds the parser's internal state.
36 scanner scanner.Scanner
39 mode uint // parsing mode
40 trace bool // == (mode & Trace != 0)
41 indent uint // indentation used for tracing output
44 comments []*ast.CommentGroup
45 leadComment *ast.CommentGroup // last lead comment
46 lineComment *ast.CommentGroup // last line comment
49 pos token.Pos // token position
50 tok token.Token // one token look-ahead
51 lit string // token literal
53 // Non-syntactic parser control
54 exprLev int // < 0: in control clause, >= 0: in expression
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
63 // (maintained by open/close LabelScope)
64 labelScope *ast.Scope // label scope for current function
65 targetStack [][]*ast.Ident // stack of unresolved labels
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
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))
82 p.trace = mode&Trace != 0 // for convenience (p.trace is used frequently)
86 // set up the pkgScope here (as opposed to in parseFile) because
87 // there are other parser entry points (ParseExpr, etc.)
89 p.pkgScope = p.topScope
91 // for the same reason, set up a label scope
95 // ----------------------------------------------------------------------------
98 func (p *parser) openScope() {
99 p.topScope = ast.NewScope(p.topScope)
102 func (p *parser) closeScope() {
103 p.topScope = p.topScope.Outer
106 func (p *parser) openLabelScope() {
107 p.labelScope = ast.NewScope(p.labelScope)
108 p.targetStack = append(p.targetStack, nil)
111 func (p *parser) closeLabelScope() {
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))
122 p.targetStack = p.targetStack[0:n]
123 p.labelScope = p.labelScope.Outer
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
135 if ident.Name != "_" {
136 if alt := scope.Insert(obj); alt != nil && p.mode&DeclarationErrors != 0 {
138 if pos := alt.Pos(); pos.IsValid() {
139 prevDecl = fmt.Sprintf("\n\tprevious declaration at %s", p.file.Position(pos))
141 p.error(ident.Pos(), fmt.Sprintf("%s redeclared in this block%s", ident.Name, prevDecl))
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
159 if ident.Name != "_" {
160 if alt := p.topScope.Insert(obj); alt != nil {
161 ident.Obj = alt // redeclaration
163 n++ // new declaration
167 if n == 0 && p.mode&DeclarationErrors != 0 {
168 p.error(idents[0].Pos(), "no new variables on left side of :=")
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)
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)
183 assert(ident.Obj == nil, "identifier already declared or resolved")
184 if ident.Name == "_" {
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 {
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)
202 // ----------------------------------------------------------------------------
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)
212 for ; i > n; i -= n {
219 func trace(p *parser, msg string) *parser {
220 p.printTrace(msg, "(")
225 // Usage pattern: defer un(trace(p, "..."));
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() {
240 case p.tok.IsLiteral():
241 p.printTrace(s, p.lit)
242 case p.tok.IsOperator(), p.tok.IsKeyword():
243 p.printTrace("\"" + s + "\"")
249 p.pos, p.tok, p.lit = p.scanner.Scan()
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)
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' {
266 comment = &ast.Comment{p.pos, p.lit}
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.
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)
286 // add comment group to the comments list
287 comments = &ast.CommentGroup{list}
288 p.comments = append(p.comments, comments)
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.
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.
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
305 // Lead and line comments may be considered documentation that is
306 // stored in the AST.
308 func (p *parser) next() {
311 line := p.file.Line(p.pos) // current line
314 if p.tok == token.COMMENT {
315 var comment *ast.CommentGroup
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
329 // consume successor comments, if any
331 for p.tok == token.COMMENT {
332 comment, endline = p.consumeCommentGroup()
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
343 func (p *parser) error(pos token.Pos, msg string) {
344 p.Error(p.file.Position(pos), msg)
347 func (p *parser) errorExpected(pos token.Pos, msg string) {
348 msg = "expected " + msg
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"
355 msg += ", found '" + p.tok.String() + "'"
356 if p.tok.IsLiteral() {
364 func (p *parser) expect(tok token.Token) token.Pos {
367 p.errorExpected(pos, "'"+tok.String()+"'")
369 p.next() // make progress
373 func (p *parser) expectSemi() {
374 if p.tok != token.RPAREN && p.tok != token.RBRACE {
375 p.expect(token.SEMICOLON)
379 func assert(cond bool, msg string) {
381 panic("go/parser internal error: " + msg)
385 // ----------------------------------------------------------------------------
388 func (p *parser) parseIdent() *ast.Ident {
391 if p.tok == token.IDENT {
395 p.expect(token.IDENT) // use expect() error handling
397 return &ast.Ident{pos, name, nil}
400 func (p *parser) parseIdentList() (list []*ast.Ident) {
402 defer un(trace(p, "IdentList"))
405 list = append(list, p.parseIdent())
406 for p.tok == token.COMMA {
408 list = append(list, p.parseIdent())
414 // ----------------------------------------------------------------------------
415 // Common productions
417 // If lhs is set, result list elements which are identifiers are not resolved.
418 func (p *parser) parseExprList(lhs bool) (list []ast.Expr) {
420 defer un(trace(p, "ExpressionList"))
423 list = append(list, p.checkExpr(p.parseExpr(lhs)))
424 for p.tok == token.COMMA {
426 list = append(list, p.checkExpr(p.parseExpr(lhs)))
432 func (p *parser) parseLhsList() []ast.Expr {
433 list := p.parseExprList(true)
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.
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
449 // identifiers must be declared elsewhere
450 for _, x := range list {
457 func (p *parser) parseRhsList() []ast.Expr {
458 return p.parseExprList(false)
461 // ----------------------------------------------------------------------------
464 func (p *parser) parseType() ast.Expr {
466 defer un(trace(p, "Type"))
473 p.errorExpected(pos, "type")
474 p.next() // make progress
475 return &ast.BadExpr{pos, p.pos}
481 // If the result is an identifier, it is not resolved.
482 func (p *parser) parseTypeName() ast.Expr {
484 defer un(trace(p, "TypeName"))
487 ident := p.parseIdent()
488 // don't resolve ident yet - it may be a parameter or field name
490 if p.tok == token.PERIOD {
491 // ident is a package name
494 sel := p.parseIdent()
495 return &ast.SelectorExpr{ident, sel}
501 func (p *parser) parseArrayType(ellipsisOk bool) ast.Expr {
503 defer un(trace(p, "ArrayType"))
506 lbrack := p.expect(token.LBRACK)
508 if ellipsisOk && p.tok == token.ELLIPSIS {
509 len = &ast.Ellipsis{p.pos, nil}
511 } else if p.tok != token.RBRACK {
514 p.expect(token.RBRACK)
517 return &ast.ArrayType{lbrack, len, elt}
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)
525 pos := x.(ast.Expr).Pos()
526 p.errorExpected(pos, "identifier")
527 ident = &ast.Ident{pos, "_", nil}
534 func (p *parser) parseFieldDecl(scope *ast.Scope) *ast.Field {
536 defer un(trace(p, "FieldDecl"))
542 list, typ := p.parseVarList(false)
545 var tag *ast.BasicLit
546 if p.tok == token.STRING {
547 tag = &ast.BasicLit{p.pos, p.tok, p.lit}
552 var idents []*ast.Ident
554 // IdentifierList Type
555 idents = p.makeIdentList(list)
557 // ["*"] TypeName (AnonymousField)
558 typ = list[0] // we always have at least one element
560 if n := len(list); n > 1 || !isTypeName(deref(typ)) {
562 p.errorExpected(pos, "anonymous field")
563 typ = &ast.BadExpr{pos, list[n-1].End()}
567 p.expectSemi() // call before accessing p.linecomment
569 field := &ast.Field{doc, idents, typ, tag, p.lineComment}
570 p.declare(field, nil, scope, ast.Var, idents...)
575 func (p *parser) parseStructType() *ast.StructType {
577 defer un(trace(p, "StructType"))
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))
590 rbrace := p.expect(token.RBRACE)
592 return &ast.StructType{pos, &ast.FieldList{lbrace, list, rbrace}, false}
595 func (p *parser) parsePointerType() *ast.StarExpr {
597 defer un(trace(p, "PointerType"))
600 star := p.expect(token.MUL)
601 base := p.parseType()
603 return &ast.StarExpr{star, base}
606 func (p *parser) tryVarType(isParam bool) ast.Expr {
607 if isParam && p.tok == token.ELLIPSIS {
610 typ := p.tryIdentOrType(isParam) // don't use parseType so we can provide better error message
612 p.error(pos, "'...' parameter is missing type")
613 typ = &ast.BadExpr{pos, p.pos}
615 return &ast.Ellipsis{pos, typ}
617 return p.tryIdentOrType(false)
620 func (p *parser) parseVarType(isParam bool) ast.Expr {
621 typ := p.tryVarType(isParam)
624 p.errorExpected(pos, "type")
625 p.next() // make progress
626 typ = &ast.BadExpr{pos, p.pos}
631 func (p *parser) parseVarList(isParam bool) (list []ast.Expr, typ ast.Expr) {
633 defer un(trace(p, "VarList"))
636 // a list of identifiers looks like a list of type names
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 {
647 typ = p.tryVarType(isParam) // maybe nil as in: func f(int,) {}
650 // if we had a list of identifiers, it must be followed by a type
651 if typ = p.tryVarType(isParam); typ != nil {
658 func (p *parser) parseParameterList(scope *ast.Scope, ellipsisOk bool) (params []*ast.Field) {
660 defer un(trace(p, "ParameterList"))
663 list, typ := p.parseVarList(ellipsisOk)
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 {
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 {
691 // Type { "," Type } (anonymous parameters)
692 params = make([]*ast.Field, len(list))
693 for i, x := range list {
695 params[i] = &ast.Field{Type: x}
702 func (p *parser) parseParameters(scope *ast.Scope, ellipsisOk bool) *ast.FieldList {
704 defer un(trace(p, "Parameters"))
707 var params []*ast.Field
708 lparen := p.expect(token.LPAREN)
709 if p.tok != token.RPAREN {
710 params = p.parseParameterList(scope, ellipsisOk)
712 rparen := p.expect(token.RPAREN)
714 return &ast.FieldList{lparen, params, rparen}
717 func (p *parser) parseResult(scope *ast.Scope) *ast.FieldList {
719 defer un(trace(p, "Result"))
722 if p.tok == token.LPAREN {
723 return p.parseParameters(scope, false)
728 list := make([]*ast.Field, 1)
729 list[0] = &ast.Field{Type: typ}
730 return &ast.FieldList{List: list}
736 func (p *parser) parseSignature(scope *ast.Scope) (params, results *ast.FieldList) {
738 defer un(trace(p, "Signature"))
741 params = p.parseParameters(scope, true)
742 results = p.parseResult(scope)
747 func (p *parser) parseFuncType() (*ast.FuncType, *ast.Scope) {
749 defer un(trace(p, "FuncType"))
752 pos := p.expect(token.FUNC)
753 scope := ast.NewScope(p.topScope) // function scope
754 params, results := p.parseSignature(scope)
756 return &ast.FuncType{pos, params, results}, scope
759 func (p *parser) parseMethodSpec(scope *ast.Scope) *ast.Field {
761 defer un(trace(p, "MethodSpec"))
765 var idents []*ast.Ident
767 x := p.parseTypeName()
768 if ident, isIdent := x.(*ast.Ident); isIdent && p.tok == token.LPAREN {
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}
775 // embedded interface
779 p.expectSemi() // call before accessing p.linecomment
781 spec := &ast.Field{doc, idents, typ, nil, p.lineComment}
782 p.declare(spec, nil, scope, ast.Fun, idents...)
787 func (p *parser) parseInterfaceType() *ast.InterfaceType {
789 defer un(trace(p, "InterfaceType"))
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))
799 rbrace := p.expect(token.RBRACE)
801 return &ast.InterfaceType{pos, &ast.FieldList{lbrace, list, rbrace}, false}
804 func (p *parser) parseMapType() *ast.MapType {
806 defer un(trace(p, "MapType"))
809 pos := p.expect(token.MAP)
810 p.expect(token.LBRACK)
812 p.expect(token.RBRACK)
813 value := p.parseType()
815 return &ast.MapType{pos, key, value}
818 func (p *parser) parseChanType() *ast.ChanType {
820 defer un(trace(p, "ChanType"))
824 dir := ast.SEND | ast.RECV
825 if p.tok == token.CHAN {
827 if p.tok == token.ARROW {
832 p.expect(token.ARROW)
836 value := p.parseType()
838 return &ast.ChanType{pos, dir, value}
841 // If the result is an identifier, it is not resolved.
842 func (p *parser) tryIdentOrType(ellipsisOk bool) ast.Expr {
845 return p.parseTypeName()
847 return p.parseArrayType(ellipsisOk)
849 return p.parseStructType()
851 return p.parsePointerType()
853 typ, _ := p.parseFuncType()
855 case token.INTERFACE:
856 return p.parseInterfaceType()
858 return p.parseMapType()
859 case token.CHAN, token.ARROW:
860 return p.parseChanType()
865 rparen := p.expect(token.RPAREN)
866 return &ast.ParenExpr{lparen, typ, rparen}
873 func (p *parser) tryType() ast.Expr {
874 typ := p.tryIdentOrType(false)
881 // ----------------------------------------------------------------------------
884 func (p *parser) parseStmtList() (list []ast.Stmt) {
886 defer un(trace(p, "StatementList"))
889 for p.tok != token.CASE && p.tok != token.DEFAULT && p.tok != token.RBRACE && p.tok != token.EOF {
890 list = append(list, p.parseStmt())
896 func (p *parser) parseBody(scope *ast.Scope) *ast.BlockStmt {
898 defer un(trace(p, "Body"))
901 lbrace := p.expect(token.LBRACE)
902 p.topScope = scope // open function scope
904 list := p.parseStmtList()
907 rbrace := p.expect(token.RBRACE)
909 return &ast.BlockStmt{lbrace, list, rbrace}
912 func (p *parser) parseBlockStmt() *ast.BlockStmt {
914 defer un(trace(p, "BlockStmt"))
917 lbrace := p.expect(token.LBRACE)
919 list := p.parseStmtList()
921 rbrace := p.expect(token.RBRACE)
923 return &ast.BlockStmt{lbrace, list, rbrace}
926 // ----------------------------------------------------------------------------
929 func (p *parser) parseFuncTypeOrLit() ast.Expr {
931 defer un(trace(p, "FuncTypeOrLit"))
934 typ, scope := p.parseFuncType()
935 if p.tok != token.LBRACE {
936 // function type only
941 body := p.parseBody(scope)
944 return &ast.FuncLit{typ, body}
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.
951 func (p *parser) parseOperand(lhs bool) ast.Expr {
953 defer un(trace(p, "Operand"))
964 case token.INT, token.FLOAT, token.IMAG, token.CHAR, token.STRING:
965 x := &ast.BasicLit{p.pos, p.tok, p.lit}
973 x := p.parseRhsOrType() // types may be parenthesized: (some type)
975 rparen := p.expect(token.RPAREN)
976 return &ast.ParenExpr{lparen, x, rparen}
979 return p.parseFuncTypeOrLit()
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")
991 p.errorExpected(pos, "operand")
992 p.next() // make progress
993 return &ast.BadExpr{pos, p.pos}
996 func (p *parser) parseSelector(x ast.Expr) ast.Expr {
998 defer un(trace(p, "Selector"))
1001 sel := p.parseIdent()
1003 return &ast.SelectorExpr{x, sel}
1006 func (p *parser) parseTypeAssertion(x ast.Expr) ast.Expr {
1008 defer un(trace(p, "TypeAssertion"))
1011 p.expect(token.LPAREN)
1013 if p.tok == token.TYPE {
1014 // type switch: typ == nil
1019 p.expect(token.RPAREN)
1021 return &ast.TypeAssertExpr{x, typ}
1024 func (p *parser) parseIndexOrSlice(x ast.Expr) ast.Expr {
1026 defer un(trace(p, "IndexOrSlice"))
1029 lbrack := p.expect(token.LBRACK)
1031 var low, high ast.Expr
1033 if p.tok != token.COLON {
1036 if p.tok == token.COLON {
1039 if p.tok != token.RBRACK {
1044 rbrack := p.expect(token.RBRACK)
1047 return &ast.SliceExpr{x, lbrack, low, high, rbrack}
1049 return &ast.IndexExpr{x, lbrack, low, rbrack}
1052 func (p *parser) parseCallOrConversion(fun ast.Expr) *ast.CallExpr {
1054 defer un(trace(p, "CallOrConversion"))
1057 lparen := p.expect(token.LPAREN)
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 {
1067 if p.tok != token.COMMA {
1073 rparen := p.expect(token.RPAREN)
1075 return &ast.CallExpr{fun, lparen, list, ellipsis, rparen}
1078 func (p *parser) parseElement(keyOk bool) ast.Expr {
1080 defer un(trace(p, "Element"))
1083 if p.tok == token.LBRACE {
1084 return p.parseLiteralValue(nil)
1087 x := p.checkExpr(p.parseExpr(keyOk)) // don't resolve if map key
1089 if p.tok == token.COLON {
1092 return &ast.KeyValueExpr{x, colon, p.parseElement(false)}
1094 p.resolve(x) // not a map key
1100 func (p *parser) parseElementList() (list []ast.Expr) {
1102 defer un(trace(p, "ElementList"))
1105 for p.tok != token.RBRACE && p.tok != token.EOF {
1106 list = append(list, p.parseElement(true))
1107 if p.tok != token.COMMA {
1116 func (p *parser) parseLiteralValue(typ ast.Expr) ast.Expr {
1118 defer un(trace(p, "LiteralValue"))
1121 lbrace := p.expect(token.LBRACE)
1124 if p.tok != token.RBRACE {
1125 elts = p.parseElementList()
1128 rbrace := p.expect(token.RBRACE)
1129 return &ast.CompositeLit{typ, lbrace, elts, rbrace}
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) {
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
1153 case *ast.UnaryExpr:
1154 case *ast.BinaryExpr:
1156 // all other nodes are not proper expressions
1157 p.errorExpected(x.Pos(), "expression")
1158 x = &ast.BadExpr{x.Pos(), x.End()}
1163 // isTypeName returns true iff x is a (qualified) TypeName.
1164 func isTypeName(x ast.Expr) bool {
1165 switch t := x.(type) {
1168 case *ast.SelectorExpr:
1169 _, isIdent := t.X.(*ast.Ident)
1172 return false // all other nodes are not type names
1177 // isLiteralType returns true iff x is a legal composite literal type.
1178 func isLiteralType(x ast.Expr) bool {
1179 switch t := x.(type) {
1182 case *ast.SelectorExpr:
1183 _, isIdent := t.X.(*ast.Ident)
1185 case *ast.ArrayType:
1186 case *ast.StructType:
1189 return false // all other nodes are not legal composite literal types
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 {
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 {
1210 // checkExprOrType checks that x is an expression or a type
1211 // (and not a raw type such as [...]T).
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()}
1225 // all other nodes are expressions or types
1229 // If lhs is set and the result is an identifier, it is not resolved.
1230 func (p *parser) parsePrimaryExpr(lhs bool) ast.Expr {
1232 defer un(trace(p, "PrimaryExpr"))
1235 x := p.parseOperand(lhs)
1246 x = p.parseSelector(p.checkExpr(x))
1248 x = p.parseTypeAssertion(p.checkExpr(x))
1251 p.next() // make progress
1252 p.errorExpected(pos, "selector or type assertion")
1253 x = &ast.BadExpr{pos, p.pos}
1259 x = p.parseIndexOrSlice(p.checkExpr(x))
1264 x = p.parseCallOrConversion(p.checkExprOrType(x))
1266 if isLiteralType(x) && (p.exprLev >= 0 || !isTypeName(x)) {
1270 x = p.parseLiteralValue(x)
1277 lhs = false // no need to try to resolve again
1283 // If lhs is set and the result is an identifier, it is not resolved.
1284 func (p *parser) parseUnaryExpr(lhs bool) ast.Expr {
1286 defer un(trace(p, "UnaryExpr"))
1290 case token.ADD, token.SUB, token.NOT, token.XOR, token.AND:
1291 pos, op := p.pos, p.tok
1293 x := p.parseUnaryExpr(false)
1294 return &ast.UnaryExpr{pos, op, p.checkExpr(x)}
1297 // channel type or receive expression
1300 if p.tok == token.CHAN {
1302 value := p.parseType()
1303 return &ast.ChanType{pos, ast.RECV, value}
1306 x := p.parseUnaryExpr(false)
1307 return &ast.UnaryExpr{pos, token.ARROW, p.checkExpr(x)}
1310 // pointer type or unary "*" expression
1313 x := p.parseUnaryExpr(false)
1314 return &ast.StarExpr{pos, p.checkExprOrType(x)}
1317 return p.parsePrimaryExpr(lhs)
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 {
1323 defer un(trace(p, "BinaryExpr"))
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
1335 y := p.parseBinaryExpr(false, prec+1)
1336 x = &ast.BinaryExpr{p.checkExpr(x), pos, op, p.checkExpr(y)}
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
1347 func (p *parser) parseExpr(lhs bool) ast.Expr {
1349 defer un(trace(p, "Expression"))
1352 return p.parseBinaryExpr(lhs, token.LowestPrec+1)
1355 func (p *parser) parseRhs() ast.Expr {
1356 return p.checkExpr(p.parseExpr(false))
1359 func (p *parser) parseRhsOrType() ast.Expr {
1360 return p.checkExprOrType(p.parseExpr(false))
1363 // ----------------------------------------------------------------------------
1366 // Parsing modes for parseSimpleStmt.
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) {
1379 defer un(trace(p, "SimpleStmt"))
1382 x := p.parseLhsList()
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
1395 if mode == rangeOk && p.tok == token.RANGE && (tok == token.DEFINE || tok == token.ASSIGN) {
1398 y = []ast.Expr{&ast.UnaryExpr{pos, token.RANGE, p.parseRhs()}}
1401 y = p.parseRhsList()
1403 if tok == token.DEFINE {
1404 p.shortVarDecl(p.makeIdentList(x))
1406 return &ast.AssignStmt{x, pos, tok, y}, isRange
1410 p.errorExpected(x[0].Pos(), "1 expression")
1411 // continue with first expression
1416 // labeled statement
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
1423 stmt := &ast.LabeledStmt{label, colon, p.parseStmt()}
1424 p.declare(stmt, nil, p.labelScope, ast.Lbl, label)
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
1441 return &ast.SendStmt{x[0], arrow, y}, false
1443 case token.INC, token.DEC:
1444 // increment or decrement
1445 s := &ast.IncDecStmt{x[0], p.pos, p.tok}
1451 return &ast.ExprStmt{x[0]}, false
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 {
1459 p.errorExpected(x.Pos(), "function/method call")
1463 func (p *parser) parseGoStmt() ast.Stmt {
1465 defer un(trace(p, "GoStmt"))
1468 pos := p.expect(token.GO)
1469 call := p.parseCallExpr()
1472 return &ast.BadStmt{pos, pos + 2} // len("go")
1475 return &ast.GoStmt{pos, call}
1478 func (p *parser) parseDeferStmt() ast.Stmt {
1480 defer un(trace(p, "DeferStmt"))
1483 pos := p.expect(token.DEFER)
1484 call := p.parseCallExpr()
1487 return &ast.BadStmt{pos, pos + 5} // len("defer")
1490 return &ast.DeferStmt{pos, call}
1493 func (p *parser) parseReturnStmt() *ast.ReturnStmt {
1495 defer un(trace(p, "ReturnStmt"))
1499 p.expect(token.RETURN)
1501 if p.tok != token.SEMICOLON && p.tok != token.RBRACE {
1502 x = p.parseRhsList()
1506 return &ast.ReturnStmt{pos, x}
1509 func (p *parser) parseBranchStmt(tok token.Token) *ast.BranchStmt {
1511 defer un(trace(p, "BranchStmt"))
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)
1524 return &ast.BranchStmt{pos, tok, label}
1527 func (p *parser) makeExpr(s ast.Stmt) ast.Expr {
1531 if es, isExpr := s.(*ast.ExprStmt); isExpr {
1532 return p.checkExpr(es.X)
1534 p.error(s.Pos(), "expected condition, found simple statement")
1535 return &ast.BadExpr{s.Pos(), s.End()}
1538 func (p *parser) parseIfStmt() *ast.IfStmt {
1540 defer un(trace(p, "IfStmt"))
1543 pos := p.expect(token.IF)
1545 defer p.closeScope()
1550 prevLev := p.exprLev
1552 if p.tok == token.SEMICOLON {
1556 s, _ = p.parseSimpleStmt(basic)
1557 if p.tok == token.SEMICOLON {
1568 body := p.parseBlockStmt()
1570 if p.tok == token.ELSE {
1572 else_ = p.parseStmt()
1577 return &ast.IfStmt{pos, s, x, body, else_}
1580 func (p *parser) parseTypeList() (list []ast.Expr) {
1582 defer un(trace(p, "TypeList"))
1585 list = append(list, p.parseType())
1586 for p.tok == token.COMMA {
1588 list = append(list, p.parseType())
1594 func (p *parser) parseCaseClause(typeSwitch bool) *ast.CaseClause {
1596 defer un(trace(p, "CaseClause"))
1601 if p.tok == token.CASE {
1604 list = p.parseTypeList()
1606 list = p.parseRhsList()
1609 p.expect(token.DEFAULT)
1612 colon := p.expect(token.COLON)
1614 body := p.parseStmtList()
1617 return &ast.CaseClause{pos, list, colon, body}
1620 func isTypeSwitchAssert(x ast.Expr) bool {
1621 a, ok := x.(*ast.TypeAssertExpr)
1622 return ok && a.Type == nil
1625 func isTypeSwitchGuard(s ast.Stmt) bool {
1626 switch t := s.(type) {
1629 return isTypeSwitchAssert(t.X)
1630 case *ast.AssignStmt:
1632 return len(t.Lhs) == 1 && t.Tok == token.DEFINE && len(t.Rhs) == 1 && isTypeSwitchAssert(t.Rhs[0])
1637 func (p *parser) parseSwitchStmt() ast.Stmt {
1639 defer un(trace(p, "SwitchStmt"))
1642 pos := p.expect(token.SWITCH)
1644 defer p.closeScope()
1647 if p.tok != token.LBRACE {
1648 prevLev := p.exprLev
1650 if p.tok != token.SEMICOLON {
1651 s2, _ = p.parseSimpleStmt(basic)
1653 if p.tok == token.SEMICOLON {
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:
1662 // switch t := 0; t := x.(T) { ... }
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).
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.
1671 defer p.closeScope()
1672 s2, _ = p.parseSimpleStmt(basic)
1678 typeSwitch := isTypeSwitchGuard(s2)
1679 lbrace := p.expect(token.LBRACE)
1681 for p.tok == token.CASE || p.tok == token.DEFAULT {
1682 list = append(list, p.parseCaseClause(typeSwitch))
1684 rbrace := p.expect(token.RBRACE)
1686 body := &ast.BlockStmt{lbrace, list, rbrace}
1689 return &ast.TypeSwitchStmt{pos, s1, s2, body}
1692 return &ast.SwitchStmt{pos, s1, p.makeExpr(s2), body}
1695 func (p *parser) parseCommClause() *ast.CommClause {
1697 defer un(trace(p, "CommClause"))
1703 if p.tok == token.CASE {
1705 lhs := p.parseLhsList()
1706 if p.tok == token.ARROW {
1709 p.errorExpected(lhs[0].Pos(), "1 expression")
1710 // continue with first expression
1715 comm = &ast.SendStmt{lhs[0], arrow, rhs}
1721 if tok == token.ASSIGN || tok == token.DEFINE {
1722 // RecvStmt with assignment
1724 p.errorExpected(lhs[0].Pos(), "1 or 2 expressions")
1725 // continue with first two expressions
1730 if tok == token.DEFINE && lhs != nil {
1731 p.shortVarDecl(p.makeIdentList(lhs))
1734 // rhs must be single receive operation
1736 p.errorExpected(lhs[0].Pos(), "1 expression")
1737 // continue with first expression
1740 lhs = nil // there is no lhs
1743 comm = &ast.AssignStmt{lhs, pos, tok, []ast.Expr{rhs}}
1745 comm = &ast.ExprStmt{rhs}
1749 p.expect(token.DEFAULT)
1752 colon := p.expect(token.COLON)
1753 body := p.parseStmtList()
1756 return &ast.CommClause{pos, comm, colon, body}
1759 func (p *parser) parseSelectStmt() *ast.SelectStmt {
1761 defer un(trace(p, "SelectStmt"))
1764 pos := p.expect(token.SELECT)
1765 lbrace := p.expect(token.LBRACE)
1767 for p.tok == token.CASE || p.tok == token.DEFAULT {
1768 list = append(list, p.parseCommClause())
1770 rbrace := p.expect(token.RBRACE)
1772 body := &ast.BlockStmt{lbrace, list, rbrace}
1774 return &ast.SelectStmt{pos, body}
1777 func (p *parser) parseForStmt() ast.Stmt {
1779 defer un(trace(p, "ForStmt"))
1782 pos := p.expect(token.FOR)
1784 defer p.closeScope()
1786 var s1, s2, s3 ast.Stmt
1788 if p.tok != token.LBRACE {
1789 prevLev := p.exprLev
1791 if p.tok != token.SEMICOLON {
1792 s2, isRange = p.parseSimpleStmt(rangeOk)
1794 if !isRange && p.tok == token.SEMICOLON {
1798 if p.tok != token.SEMICOLON {
1799 s2, _ = p.parseSimpleStmt(basic)
1802 if p.tok != token.LBRACE {
1803 s3, _ = p.parseSimpleStmt(basic)
1809 body := p.parseBlockStmt()
1813 as := s2.(*ast.AssignStmt)
1815 var key, value ast.Expr
1816 switch len(as.Lhs) {
1818 key, value = as.Lhs[0], as.Lhs[1]
1822 p.errorExpected(as.Lhs[0].Pos(), "1 or 2 expressions")
1823 return &ast.BadStmt{pos, body.End()}
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}
1831 // regular for statement
1832 return &ast.ForStmt{pos, s1, p.makeExpr(s2), s3, body}
1835 func (p *parser) parseStmt() (s ast.Stmt) {
1837 defer un(trace(p, "Statement"))
1841 case token.CONST, token.TYPE, token.VAR:
1842 s = &ast.DeclStmt{p.parseDecl()}
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
1852 if _, isLabeledStmt := s.(*ast.LabeledStmt); !isLabeledStmt {
1858 s = p.parseDeferStmt()
1860 s = p.parseReturnStmt()
1861 case token.BREAK, token.CONTINUE, token.GOTO, token.FALLTHROUGH:
1862 s = p.parseBranchStmt(p.tok)
1864 s = p.parseBlockStmt()
1869 s = p.parseSwitchStmt()
1871 s = p.parseSelectStmt()
1873 s = p.parseForStmt()
1874 case token.SEMICOLON:
1875 s = &ast.EmptyStmt{p.pos}
1878 // a semicolon may be omitted before a closing "}"
1879 s = &ast.EmptyStmt{p.pos}
1881 // no statement found
1883 p.errorExpected(pos, "statement")
1884 p.next() // make progress
1885 s = &ast.BadStmt{pos, p.pos}
1891 // ----------------------------------------------------------------------------
1894 type parseSpecFunction func(p *parser, doc *ast.CommentGroup, iota int) ast.Spec
1896 func parseImportSpec(p *parser, doc *ast.CommentGroup, _ int) ast.Spec {
1898 defer un(trace(p, "ImportSpec"))
1901 var ident *ast.Ident
1904 ident = &ast.Ident{p.pos, ".", nil}
1907 ident = p.parseIdent()
1910 var path *ast.BasicLit
1911 if p.tok == token.STRING {
1912 path = &ast.BasicLit{p.pos, p.tok, p.lit}
1915 p.expect(token.STRING) // use expect() error handling
1917 p.expectSemi() // call before accessing p.linecomment
1920 spec := &ast.ImportSpec{doc, ident, path, p.lineComment, token.NoPos}
1921 p.imports = append(p.imports, spec)
1926 func parseConstSpec(p *parser, doc *ast.CommentGroup, iota int) ast.Spec {
1928 defer un(trace(p, "ConstSpec"))
1931 idents := p.parseIdentList()
1933 var values []ast.Expr
1934 if typ != nil || p.tok == token.ASSIGN || iota == 0 {
1935 p.expect(token.ASSIGN)
1936 values = p.parseRhsList()
1938 p.expectSemi() // call before accessing p.linecomment
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...)
1950 func parseTypeSpec(p *parser, doc *ast.CommentGroup, _ int) ast.Spec {
1952 defer un(trace(p, "TypeSpec"))
1955 ident := p.parseIdent()
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)
1964 spec.Type = p.parseType()
1965 p.expectSemi() // call before accessing p.linecomment
1966 spec.Comment = p.lineComment
1971 func parseVarSpec(p *parser, doc *ast.CommentGroup, _ int) ast.Spec {
1973 defer un(trace(p, "VarSpec"))
1976 idents := p.parseIdentList()
1978 var values []ast.Expr
1979 if typ == nil || p.tok == token.ASSIGN {
1980 p.expect(token.ASSIGN)
1981 values = p.parseRhsList()
1983 p.expectSemi() // call before accessing p.linecomment
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...)
1995 func (p *parser) parseGenDecl(keyword token.Token, f parseSpecFunction) *ast.GenDecl {
1997 defer un(trace(p, "GenDecl("+keyword.String()+")"))
2000 doc := p.leadComment
2001 pos := p.expect(keyword)
2002 var lparen, rparen token.Pos
2004 if p.tok == token.LPAREN {
2007 for iota := 0; p.tok != token.RPAREN && p.tok != token.EOF; iota++ {
2008 list = append(list, f(p, p.leadComment, iota))
2010 rparen = p.expect(token.RPAREN)
2013 list = append(list, f(p, nil, 0))
2016 return &ast.GenDecl{doc, pos, keyword, lparen, list, rparen}
2019 func (p *parser) parseReceiver(scope *ast.Scope) *ast.FieldList {
2021 defer un(trace(p, "Receiver"))
2024 par := p.parseParameters(scope, false)
2026 // must have exactly one receiver
2027 if par.NumFields() != 1 {
2028 p.errorExpected(par.Opening, "exactly one receiver")
2029 par.List = []*ast.Field{{Type: &ast.BadExpr{par.Opening, par.Closing + 1}}}
2033 // recv type must be of the form ["*"] identifier
2035 base := deref(recv.Type)
2036 if _, isIdent := base.(*ast.Ident); !isIdent {
2037 p.errorExpected(base.Pos(), "(unqualified) identifier")
2038 par.List = []*ast.Field{{Type: &ast.BadExpr{recv.Pos(), recv.End()}}}
2044 func (p *parser) parseFuncDecl() *ast.FuncDecl {
2046 defer un(trace(p, "FunctionDecl"))
2049 doc := p.leadComment
2050 pos := p.expect(token.FUNC)
2051 scope := ast.NewScope(p.topScope) // function scope
2053 var recv *ast.FieldList
2054 if p.tok == token.LPAREN {
2055 recv = p.parseReceiver(scope)
2058 ident := p.parseIdent()
2060 params, results := p.parseSignature(scope)
2062 var body *ast.BlockStmt
2063 if p.tok == token.LBRACE {
2064 body = p.parseBody(scope)
2068 decl := &ast.FuncDecl{doc, recv, ident, &ast.FuncType{pos, params, results}, body}
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.
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)
2084 func (p *parser) parseDecl() ast.Decl {
2086 defer un(trace(p, "Declaration"))
2089 var f parseSpecFunction
2101 return p.parseFuncDecl()
2105 p.errorExpected(pos, "declaration")
2106 p.next() // make progress
2107 decl := &ast.BadDecl{pos, p.pos}
2111 return p.parseGenDecl(p.tok, f)
2114 func (p *parser) parseDeclList() (list []ast.Decl) {
2116 defer un(trace(p, "DeclList"))
2119 for p.tok != token.EOF {
2120 list = append(list, p.parseDecl())
2126 // ----------------------------------------------------------------------------
2129 func (p *parser) parseFile() *ast.File {
2131 defer un(trace(p, "File"))
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 _")
2145 var decls []ast.Decl
2147 // Don't bother parsing the rest if we had errors already.
2148 // Likely not a Go source file at all.
2150 if p.ErrorCount() == 0 && p.mode&PackageClauseOnly == 0 {
2152 for p.tok == token.IMPORT {
2153 decls = append(decls, p.parseGenDecl(token.IMPORT, parseImportSpec))
2156 if p.mode&ImportsOnly == 0 {
2157 // rest of package body
2158 for p.tok != token.EOF {
2159 decls = append(decls, p.parseDecl())
2164 assert(p.topScope == p.pkgScope, "imbalanced scopes")
2166 // resolve global identifiers within the same file
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
2178 return &ast.File{doc, pos, ident, decls, p.pkgScope, p.imports, p.unresolved[0:i], p.comments}