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