OLD | NEW |
1 // Copyright 2009 The Go Authors. All rights reserved. | 1 // Copyright 2009 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 // A parser for Go source files. Input may be provided in a variety of | 5 // A parser for Go source files. Input may be provided in a variety of |
6 // forms (see the various Parse* functions); the output is an abstract | 6 // forms (see the various Parse* functions); the output is an abstract |
7 // syntax tree (AST) representing the Go source. The parser is invoked | 7 // syntax tree (AST) representing the Go source. The parser is invoked |
8 // through one of the Parse* functions. | 8 // through one of the Parse* functions. |
9 // | 9 // |
10 package parser | 10 package parser |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
48 | 48 |
49 // Next token | 49 // Next token |
50 pos token.Pos // token position | 50 pos token.Pos // token position |
51 tok token.Token // one token look-ahead | 51 tok token.Token // one token look-ahead |
52 lit_ []byte // token literal (slice into original source, don't hol
d on to it) | 52 lit_ []byte // token literal (slice into original source, don't hol
d on to it) |
53 | 53 |
54 // Non-syntactic parser control | 54 // Non-syntactic parser control |
55 exprLev int // < 0: in control clause, >= 0: in expression | 55 exprLev int // < 0: in control clause, >= 0: in expression |
56 | 56 |
57 // Ordinary identifer scopes | 57 // Ordinary identifer scopes |
58 » pkgScope *ast.Scope // pkgScope.Outer == nil | 58 » pkgScope *ast.Scope // pkgScope.Outer == nil |
59 » topScope *ast.Scope // top-most scope; may be pkgScope | 59 » topScope *ast.Scope // top-most scope; may be pkgScope |
60 » unresolved []*ast.Ident // unresolved global identifiers | 60 » unresolved []*ast.Ident // unresolved identifiers |
| 61 » imports []*ast.ImportSpec // list of imports |
61 | 62 |
62 // Label scope | 63 // Label scope |
63 // (maintained by open/close LabelScope) | 64 // (maintained by open/close LabelScope) |
64 labelScope *ast.Scope // label scope for current function | 65 labelScope *ast.Scope // label scope for current function |
65 targetStack [][]*ast.Ident // stack of unresolved labels | 66 targetStack [][]*ast.Ident // stack of unresolved labels |
66 } | 67 } |
67 | 68 |
68 | 69 |
69 // scannerMode returns the scanner mode bits given the parser's mode bits. | 70 // scannerMode returns the scanner mode bits given the parser's mode bits. |
70 func scannerMode(mode uint) uint { | 71 func scannerMode(mode uint) uint { |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
134 } | 135 } |
135 } | 136 } |
136 // pop label scope | 137 // pop label scope |
137 p.targetStack = p.targetStack[0:n] | 138 p.targetStack = p.targetStack[0:n] |
138 p.labelScope = p.labelScope.Outer | 139 p.labelScope = p.labelScope.Outer |
139 } | 140 } |
140 | 141 |
141 | 142 |
142 func (p *parser) declare(decl interface{}, scope *ast.Scope, kind ast.ObjKind, i
dents ...*ast.Ident) { | 143 func (p *parser) declare(decl interface{}, scope *ast.Scope, kind ast.ObjKind, i
dents ...*ast.Ident) { |
143 for _, ident := range idents { | 144 for _, ident := range idents { |
| 145 assert(ident.Obj == nil, "identifier already declared or resolve
d") |
144 if ident.Name != "_" { | 146 if ident.Name != "_" { |
145 obj := ast.NewObj(kind, ident.Name) | 147 obj := ast.NewObj(kind, ident.Name) |
146 // remember the corresponding declaration for redeclarat
ion | 148 // remember the corresponding declaration for redeclarat
ion |
147 // errors and global variable resolution/typechecking ph
ase | 149 // errors and global variable resolution/typechecking ph
ase |
148 obj.Decl = decl | 150 obj.Decl = decl |
149 alt := scope.Insert(obj) | 151 alt := scope.Insert(obj) |
150 if alt != obj && p.mode&DeclarationErrors != 0 { | 152 if alt != obj && p.mode&DeclarationErrors != 0 { |
151 prevDecl := "" | 153 prevDecl := "" |
152 if pos := alt.Pos(); pos.IsValid() { | 154 if pos := alt.Pos(); pos.IsValid() { |
153 prevDecl = fmt.Sprintf("\n\tprevious dec
laration at %s", p.file.Position(pos)) | 155 prevDecl = fmt.Sprintf("\n\tprevious dec
laration at %s", p.file.Position(pos)) |
154 } | 156 } |
155 p.error(ident.Pos(), fmt.Sprintf("%s redeclared
in this block%s", ident.Name, prevDecl)) | 157 p.error(ident.Pos(), fmt.Sprintf("%s redeclared
in this block%s", ident.Name, prevDecl)) |
156 } | 158 } |
157 ident.Obj = obj | 159 ident.Obj = obj |
158 } | 160 } |
159 } | 161 } |
160 } | 162 } |
161 | 163 |
162 | 164 |
163 func (p *parser) shortVarDecl(idents []*ast.Ident) { | 165 func (p *parser) shortVarDecl(idents []*ast.Ident) { |
164 // Go spec: A short variable declaration may redeclare variables | 166 // Go spec: A short variable declaration may redeclare variables |
165 // provided they were originally declared in the same block with | 167 // provided they were originally declared in the same block with |
166 // the same type, and at least one of the non-blank variables is new. | 168 // the same type, and at least one of the non-blank variables is new. |
167 n := 0 // number of new variables | 169 n := 0 // number of new variables |
168 for _, ident := range idents { | 170 for _, ident := range idents { |
| 171 assert(ident.Obj == nil, "identifier already declared or resolve
d") |
169 if ident.Name != "_" { | 172 if ident.Name != "_" { |
170 obj := ast.NewObj(ast.Var, ident.Name) | 173 obj := ast.NewObj(ast.Var, ident.Name) |
171 // short var declarations cannot have redeclaration erro
rs | 174 // short var declarations cannot have redeclaration erro
rs |
172 // and are not global => no need to remember the respect
ive | 175 // and are not global => no need to remember the respect
ive |
173 // declaration | 176 // declaration |
174 alt := p.topScope.Insert(obj) | 177 alt := p.topScope.Insert(obj) |
175 if alt == obj { | 178 if alt == obj { |
176 n++ // new declaration | 179 n++ // new declaration |
177 } | 180 } |
178 ident.Obj = alt | 181 ident.Obj = alt |
179 } | 182 } |
180 } | 183 } |
181 if n == 0 && p.mode&DeclarationErrors != 0 { | 184 if n == 0 && p.mode&DeclarationErrors != 0 { |
182 p.error(idents[0].Pos(), "no new variables on left side of :=") | 185 p.error(idents[0].Pos(), "no new variables on left side of :=") |
183 } | 186 } |
184 } | 187 } |
185 | 188 |
186 | 189 |
187 func (p *parser) resolve(ident *ast.Ident) { | 190 // The unresolved object is a sentinel to mark identifiers that have been added |
| 191 // to the list of unresolved identifiers. The sentinel is only used for verifyin
g |
| 192 // internal consistency. |
| 193 var unresolved = new(ast.Object) |
| 194 |
| 195 |
| 196 func (p *parser) resolve(x ast.Expr) { |
| 197 » // nothing to do if x is not an identifier or the blank identifier |
| 198 » ident, _ := x.(*ast.Ident) |
| 199 » if ident == nil { |
| 200 » » return |
| 201 » } |
| 202 » assert(ident.Obj == nil, "identifier already declared or resolved") |
188 if ident.Name == "_" { | 203 if ident.Name == "_" { |
189 return | 204 return |
190 } | 205 } |
191 // try to resolve the identifier | 206 // try to resolve the identifier |
192 for s := p.topScope; s != nil; s = s.Outer { | 207 for s := p.topScope; s != nil; s = s.Outer { |
193 if obj := s.Lookup(ident.Name); obj != nil { | 208 if obj := s.Lookup(ident.Name); obj != nil { |
194 ident.Obj = obj | 209 ident.Obj = obj |
195 return | 210 return |
196 } | 211 } |
197 } | 212 } |
198 » // collect unresolved global identifiers; ignore the others | 213 » // all local scopes are known, so any unresolved identifier |
199 » if p.topScope == p.pkgScope { | 214 » // must be found either in the file scope, package scope |
200 » » p.unresolved = append(p.unresolved, ident) | 215 » // (perhaps in another file), or universe scope --- collect |
201 » } | 216 » // them so that they can be resolved later |
| 217 » ident.Obj = unresolved |
| 218 » p.unresolved = append(p.unresolved, ident) |
202 } | 219 } |
203 | 220 |
204 | 221 |
205 // ---------------------------------------------------------------------------- | 222 // ---------------------------------------------------------------------------- |
206 // Parsing support | 223 // Parsing support |
207 | 224 |
208 func (p *parser) printTrace(a ...interface{}) { | 225 func (p *parser) printTrace(a ...interface{}) { |
209 const dots = ". . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . " + | 226 const dots = ". . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . " + |
210 ". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
" | 227 ". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
" |
211 const n = uint(len(dots)) | 228 const n = uint(len(dots)) |
(...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
381 } | 398 } |
382 | 399 |
383 | 400 |
384 func (p *parser) expectSemi() { | 401 func (p *parser) expectSemi() { |
385 if p.tok != token.RPAREN && p.tok != token.RBRACE { | 402 if p.tok != token.RPAREN && p.tok != token.RBRACE { |
386 p.expect(token.SEMICOLON) | 403 p.expect(token.SEMICOLON) |
387 } | 404 } |
388 } | 405 } |
389 | 406 |
390 | 407 |
| 408 func assert(cond bool, msg string) { |
| 409 if !cond { |
| 410 panic("go/parser internal error: " + msg) |
| 411 } |
| 412 } |
| 413 |
| 414 |
391 // ---------------------------------------------------------------------------- | 415 // ---------------------------------------------------------------------------- |
392 // Identifiers | 416 // Identifiers |
393 | 417 |
394 func (p *parser) parseIdent() *ast.Ident { | 418 func (p *parser) parseIdent() *ast.Ident { |
395 pos := p.pos | 419 pos := p.pos |
396 name := "_" | 420 name := "_" |
397 if p.tok == token.IDENT { | 421 if p.tok == token.IDENT { |
398 name = string(p.lit_) | 422 name = string(p.lit_) |
399 p.next() | 423 p.next() |
400 } else { | 424 } else { |
(...skipping 14 matching lines...) Expand all Loading... |
415 list = append(list, p.parseIdent()) | 439 list = append(list, p.parseIdent()) |
416 } | 440 } |
417 | 441 |
418 return | 442 return |
419 } | 443 } |
420 | 444 |
421 | 445 |
422 // ---------------------------------------------------------------------------- | 446 // ---------------------------------------------------------------------------- |
423 // Common productions | 447 // Common productions |
424 | 448 |
425 func (p *parser) parseExprList() (list []ast.Expr) { | 449 // If lhs is set, result list elements which are identifiers are not resolved. |
| 450 func (p *parser) parseExprList(lhs bool) (list []ast.Expr) { |
426 if p.trace { | 451 if p.trace { |
427 defer un(trace(p, "ExpressionList")) | 452 defer un(trace(p, "ExpressionList")) |
428 } | 453 } |
429 | 454 |
430 » list = append(list, p.parseExpr()) | 455 » list = append(list, p.parseExpr(lhs)) |
431 for p.tok == token.COMMA { | 456 for p.tok == token.COMMA { |
432 p.next() | 457 p.next() |
433 » » list = append(list, p.parseExpr()) | 458 » » list = append(list, p.parseExpr(lhs)) |
434 } | 459 } |
435 | 460 |
436 return | 461 return |
437 } | 462 } |
438 | 463 |
439 | 464 |
| 465 func (p *parser) parseLhsList() []ast.Expr { |
| 466 list := p.parseExprList(true) |
| 467 switch p.tok { |
| 468 case token.DEFINE: |
| 469 // lhs of a short variable declaration |
| 470 p.shortVarDecl(p.makeIdentList(list)) |
| 471 case token.COLON: |
| 472 // lhs of a label declaration or a communication clause of a sel
ect |
| 473 // statement (parseLhsList is not called when parsing the case c
lause |
| 474 // of a switch statement): |
| 475 // - labels are declared by the caller of parseLhsList |
| 476 // - for communication clauses, if there is a stand-alone identi
fier |
| 477 // followed by a colon, we have a syntax error; there is no ne
ed |
| 478 // to resolve the identifier in that case |
| 479 default: |
| 480 // identifiers must be declared elsewhere |
| 481 for _, x := range list { |
| 482 p.resolve(x) |
| 483 } |
| 484 } |
| 485 return list |
| 486 } |
| 487 |
| 488 |
| 489 func (p *parser) parseRhsList() []ast.Expr { |
| 490 return p.parseExprList(false) |
| 491 } |
| 492 |
| 493 |
440 // ---------------------------------------------------------------------------- | 494 // ---------------------------------------------------------------------------- |
441 // Types | 495 // Types |
442 | 496 |
443 func (p *parser) parseType() ast.Expr { | 497 func (p *parser) parseType() ast.Expr { |
444 if p.trace { | 498 if p.trace { |
445 defer un(trace(p, "Type")) | 499 defer un(trace(p, "Type")) |
446 } | 500 } |
447 | 501 |
448 typ := p.tryType() | 502 typ := p.tryType() |
449 | 503 |
450 if typ == nil { | 504 if typ == nil { |
451 pos := p.pos | 505 pos := p.pos |
452 p.errorExpected(pos, "type") | 506 p.errorExpected(pos, "type") |
453 p.next() // make progress | 507 p.next() // make progress |
454 return &ast.BadExpr{pos, p.pos} | 508 return &ast.BadExpr{pos, p.pos} |
455 } | 509 } |
456 | 510 |
457 return typ | 511 return typ |
458 } | 512 } |
459 | 513 |
460 | 514 |
461 func (p *parser) parseQualifiedIdent() ast.Expr { | 515 // If the result is an identifier, it is not resolved. |
462 » if p.trace { | |
463 » » defer un(trace(p, "QualifiedIdent")) | |
464 » } | |
465 | |
466 » ident := p.parseIdent() | |
467 » p.resolve(ident) | |
468 » var x ast.Expr = ident | |
469 » if p.tok == token.PERIOD { | |
470 » » // first identifier is a package identifier | |
471 » » p.next() | |
472 » » sel := p.parseIdent() | |
473 » » x = &ast.SelectorExpr{x, sel} | |
474 » } | |
475 | |
476 » return x | |
477 } | |
478 | |
479 | |
480 func (p *parser) parseTypeName() ast.Expr { | 516 func (p *parser) parseTypeName() ast.Expr { |
481 if p.trace { | 517 if p.trace { |
482 defer un(trace(p, "TypeName")) | 518 defer un(trace(p, "TypeName")) |
483 } | 519 } |
484 | 520 |
485 » return p.parseQualifiedIdent() | 521 » ident := p.parseIdent() |
| 522 » // don't resolve ident yet - it may be a parameter or field name |
| 523 |
| 524 » if p.tok == token.PERIOD { |
| 525 » » // ident is a package name |
| 526 » » p.next() |
| 527 » » p.resolve(ident) |
| 528 » » sel := p.parseIdent() |
| 529 » » return &ast.SelectorExpr{ident, sel} |
| 530 » } |
| 531 |
| 532 » return ident |
486 } | 533 } |
487 | 534 |
488 | 535 |
489 func (p *parser) parseArrayType(ellipsisOk bool) ast.Expr { | 536 func (p *parser) parseArrayType(ellipsisOk bool) ast.Expr { |
490 if p.trace { | 537 if p.trace { |
491 defer un(trace(p, "ArrayType")) | 538 defer un(trace(p, "ArrayType")) |
492 } | 539 } |
493 | 540 |
494 lbrack := p.expect(token.LBRACK) | 541 lbrack := p.expect(token.LBRACK) |
495 var len ast.Expr | 542 var len ast.Expr |
496 if ellipsisOk && p.tok == token.ELLIPSIS { | 543 if ellipsisOk && p.tok == token.ELLIPSIS { |
497 len = &ast.Ellipsis{p.pos, nil} | 544 len = &ast.Ellipsis{p.pos, nil} |
498 p.next() | 545 p.next() |
499 } else if p.tok != token.RBRACK { | 546 } else if p.tok != token.RBRACK { |
500 » » len = p.parseExpr() | 547 » » len = p.parseRhs() |
501 } | 548 } |
502 p.expect(token.RBRACK) | 549 p.expect(token.RBRACK) |
503 elt := p.parseType() | 550 elt := p.parseType() |
504 | 551 |
505 return &ast.ArrayType{lbrack, len, elt} | 552 return &ast.ArrayType{lbrack, len, elt} |
506 } | 553 } |
507 | 554 |
508 | 555 |
509 func (p *parser) makeIdentList(list []ast.Expr) []*ast.Ident { | 556 func (p *parser) makeIdentList(list []ast.Expr) []*ast.Ident { |
510 idents := make([]*ast.Ident, len(list)) | 557 idents := make([]*ast.Ident, len(list)) |
511 for i, x := range list { | 558 for i, x := range list { |
512 ident, isIdent := x.(*ast.Ident) | 559 ident, isIdent := x.(*ast.Ident) |
513 if !isIdent { | 560 if !isIdent { |
514 pos := x.(ast.Expr).Pos() | 561 pos := x.(ast.Expr).Pos() |
515 p.errorExpected(pos, "identifier") | 562 p.errorExpected(pos, "identifier") |
516 ident = &ast.Ident{pos, "_", nil} | 563 ident = &ast.Ident{pos, "_", nil} |
517 } | 564 } |
518 idents[i] = ident | 565 idents[i] = ident |
519 } | 566 } |
520 return idents | 567 return idents |
521 } | 568 } |
522 | 569 |
523 | 570 |
524 func (p *parser) parseFieldDecl() *ast.Field { | 571 func (p *parser) parseFieldDecl(scope *ast.Scope) *ast.Field { |
525 if p.trace { | 572 if p.trace { |
526 defer un(trace(p, "FieldDecl")) | 573 defer un(trace(p, "FieldDecl")) |
527 } | 574 } |
528 | 575 |
529 doc := p.leadComment | 576 doc := p.leadComment |
530 | 577 |
531 // fields | 578 // fields |
532 list, typ := p.parseVarList(false) | 579 list, typ := p.parseVarList(false) |
533 | 580 |
534 // optional tag | 581 // optional tag |
535 var tag *ast.BasicLit | 582 var tag *ast.BasicLit |
536 if p.tok == token.STRING { | 583 if p.tok == token.STRING { |
537 tag = &ast.BasicLit{p.pos, p.tok, p.lit()} | 584 tag = &ast.BasicLit{p.pos, p.tok, p.lit()} |
538 p.next() | 585 p.next() |
539 } | 586 } |
540 | 587 |
541 // analyze case | 588 // analyze case |
542 var idents []*ast.Ident | 589 var idents []*ast.Ident |
543 if typ != nil { | 590 if typ != nil { |
544 // IdentifierList Type | 591 // IdentifierList Type |
545 idents = p.makeIdentList(list) | 592 idents = p.makeIdentList(list) |
546 } else { | 593 } else { |
547 // ["*"] TypeName (AnonymousField) | 594 // ["*"] TypeName (AnonymousField) |
548 typ = list[0] // we always have at least one element | 595 typ = list[0] // we always have at least one element |
| 596 p.resolve(typ) |
549 if n := len(list); n > 1 || !isTypeName(deref(typ)) { | 597 if n := len(list); n > 1 || !isTypeName(deref(typ)) { |
550 pos := typ.Pos() | 598 pos := typ.Pos() |
551 p.errorExpected(pos, "anonymous field") | 599 p.errorExpected(pos, "anonymous field") |
552 typ = &ast.BadExpr{pos, list[n-1].End()} | 600 typ = &ast.BadExpr{pos, list[n-1].End()} |
553 } | 601 } |
554 } | 602 } |
555 | 603 |
556 p.expectSemi() // call before accessing p.linecomment | 604 p.expectSemi() // call before accessing p.linecomment |
557 | 605 |
558 » return &ast.Field{doc, idents, typ, tag, p.lineComment} | 606 » field := &ast.Field{doc, idents, typ, tag, p.lineComment} |
| 607 » p.declare(field, scope, ast.Var, idents...) |
| 608 |
| 609 » return field |
559 } | 610 } |
560 | 611 |
561 | 612 |
562 func (p *parser) parseStructType() *ast.StructType { | 613 func (p *parser) parseStructType() *ast.StructType { |
563 if p.trace { | 614 if p.trace { |
564 defer un(trace(p, "StructType")) | 615 defer un(trace(p, "StructType")) |
565 } | 616 } |
566 | 617 |
567 pos := p.expect(token.STRUCT) | 618 pos := p.expect(token.STRUCT) |
568 lbrace := p.expect(token.LBRACE) | 619 lbrace := p.expect(token.LBRACE) |
| 620 scope := ast.NewScope(nil) // struct scope |
569 var list []*ast.Field | 621 var list []*ast.Field |
570 for p.tok == token.IDENT || p.tok == token.MUL || p.tok == token.LPAREN
{ | 622 for p.tok == token.IDENT || p.tok == token.MUL || p.tok == token.LPAREN
{ |
571 // a field declaration cannot start with a '(' but we accept | 623 // a field declaration cannot start with a '(' but we accept |
572 // it here for more robust parsing and better error messages | 624 // it here for more robust parsing and better error messages |
573 // (parseFieldDecl will check and complain if necessary) | 625 // (parseFieldDecl will check and complain if necessary) |
574 » » list = append(list, p.parseFieldDecl()) | 626 » » list = append(list, p.parseFieldDecl(scope)) |
575 } | 627 } |
576 rbrace := p.expect(token.RBRACE) | 628 rbrace := p.expect(token.RBRACE) |
577 | 629 |
| 630 // TODO(gri): store struct scope in AST |
578 return &ast.StructType{pos, &ast.FieldList{lbrace, list, rbrace}, false} | 631 return &ast.StructType{pos, &ast.FieldList{lbrace, list, rbrace}, false} |
579 } | 632 } |
580 | 633 |
581 | 634 |
582 func (p *parser) parsePointerType() *ast.StarExpr { | 635 func (p *parser) parsePointerType() *ast.StarExpr { |
583 if p.trace { | 636 if p.trace { |
584 defer un(trace(p, "PointerType")) | 637 defer un(trace(p, "PointerType")) |
585 } | 638 } |
586 | 639 |
587 star := p.expect(token.MUL) | 640 star := p.expect(token.MUL) |
588 base := p.parseType() | 641 base := p.parseType() |
589 | 642 |
590 return &ast.StarExpr{star, base} | 643 return &ast.StarExpr{star, base} |
591 } | 644 } |
592 | 645 |
593 | 646 |
594 func (p *parser) tryVarType(isParam bool) ast.Expr { | 647 func (p *parser) tryVarType(isParam bool) ast.Expr { |
595 if isParam && p.tok == token.ELLIPSIS { | 648 if isParam && p.tok == token.ELLIPSIS { |
596 pos := p.pos | 649 pos := p.pos |
597 p.next() | 650 p.next() |
598 » » typ := p.tryType() // don't use parseType so we can provide bett
er error message | 651 » » typ := p.tryIdentOrType(isParam) // don't use parseType so we ca
n provide better error message |
599 if typ == nil { | 652 if typ == nil { |
600 p.error(pos, "'...' parameter is missing type") | 653 p.error(pos, "'...' parameter is missing type") |
601 typ = &ast.BadExpr{pos, p.pos} | 654 typ = &ast.BadExpr{pos, p.pos} |
602 } | 655 } |
603 if p.tok != token.RPAREN { | 656 if p.tok != token.RPAREN { |
604 p.error(pos, "can use '...' with last parameter type onl
y") | 657 p.error(pos, "can use '...' with last parameter type onl
y") |
605 } | 658 } |
606 return &ast.Ellipsis{pos, typ} | 659 return &ast.Ellipsis{pos, typ} |
607 } | 660 } |
608 » return p.tryType() | 661 » return p.tryIdentOrType(false) |
609 } | 662 } |
610 | 663 |
611 | 664 |
612 func (p *parser) parseVarType(isParam bool) ast.Expr { | 665 func (p *parser) parseVarType(isParam bool) ast.Expr { |
613 typ := p.tryVarType(isParam) | 666 typ := p.tryVarType(isParam) |
614 if typ == nil { | 667 if typ == nil { |
615 pos := p.pos | 668 pos := p.pos |
616 p.errorExpected(pos, "type") | 669 p.errorExpected(pos, "type") |
617 p.next() // make progress | 670 p.next() // make progress |
618 typ = &ast.BadExpr{pos, p.pos} | 671 typ = &ast.BadExpr{pos, p.pos} |
(...skipping 15 matching lines...) Expand all Loading... |
634 // afterwards | 687 // afterwards |
635 list = append(list, p.parseVarType(isParam)) | 688 list = append(list, p.parseVarType(isParam)) |
636 if p.tok != token.COMMA { | 689 if p.tok != token.COMMA { |
637 break | 690 break |
638 } | 691 } |
639 p.next() | 692 p.next() |
640 } | 693 } |
641 | 694 |
642 // if we had a list of identifiers, it must be followed by a type | 695 // if we had a list of identifiers, it must be followed by a type |
643 typ = p.tryVarType(isParam) | 696 typ = p.tryVarType(isParam) |
| 697 if typ != nil { |
| 698 p.resolve(typ) |
| 699 } |
644 | 700 |
645 return | 701 return |
646 } | 702 } |
647 | 703 |
648 | 704 |
649 func (p *parser) parseParameterList(scope *ast.Scope, ellipsisOk bool) (params [
]*ast.Field) { | 705 func (p *parser) parseParameterList(scope *ast.Scope, ellipsisOk bool) (params [
]*ast.Field) { |
650 if p.trace { | 706 if p.trace { |
651 defer un(trace(p, "ParameterList")) | 707 defer un(trace(p, "ParameterList")) |
652 } | 708 } |
653 | 709 |
(...skipping 21 matching lines...) Expand all Loading... |
675 if p.tok != token.COMMA { | 731 if p.tok != token.COMMA { |
676 break | 732 break |
677 } | 733 } |
678 p.next() | 734 p.next() |
679 } | 735 } |
680 | 736 |
681 } else { | 737 } else { |
682 // Type { "," Type } (anonymous parameters) | 738 // Type { "," Type } (anonymous parameters) |
683 params = make([]*ast.Field, len(list)) | 739 params = make([]*ast.Field, len(list)) |
684 for i, x := range list { | 740 for i, x := range list { |
| 741 p.resolve(x) |
685 params[i] = &ast.Field{Type: x} | 742 params[i] = &ast.Field{Type: x} |
686 } | 743 } |
687 } | 744 } |
688 | 745 |
689 return | 746 return |
690 } | 747 } |
691 | 748 |
692 | 749 |
693 func (p *parser) parseParameters(scope *ast.Scope, ellipsisOk bool) *ast.FieldLi
st { | 750 func (p *parser) parseParameters(scope *ast.Scope, ellipsisOk bool) *ast.FieldLi
st { |
694 if p.trace { | 751 if p.trace { |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
744 } | 801 } |
745 | 802 |
746 pos := p.expect(token.FUNC) | 803 pos := p.expect(token.FUNC) |
747 scope := ast.NewScope(p.topScope) // function scope | 804 scope := ast.NewScope(p.topScope) // function scope |
748 params, results := p.parseSignature(scope) | 805 params, results := p.parseSignature(scope) |
749 | 806 |
750 return &ast.FuncType{pos, params, results}, scope | 807 return &ast.FuncType{pos, params, results}, scope |
751 } | 808 } |
752 | 809 |
753 | 810 |
754 func (p *parser) parseMethodSpec() *ast.Field { | 811 func (p *parser) parseMethodSpec(scope *ast.Scope) *ast.Field { |
755 if p.trace { | 812 if p.trace { |
756 defer un(trace(p, "MethodSpec")) | 813 defer un(trace(p, "MethodSpec")) |
757 } | 814 } |
758 | 815 |
759 doc := p.leadComment | 816 doc := p.leadComment |
760 var idents []*ast.Ident | 817 var idents []*ast.Ident |
761 var typ ast.Expr | 818 var typ ast.Expr |
762 » x := p.parseQualifiedIdent() | 819 » x := p.parseTypeName() |
763 if ident, isIdent := x.(*ast.Ident); isIdent && p.tok == token.LPAREN { | 820 if ident, isIdent := x.(*ast.Ident); isIdent && p.tok == token.LPAREN { |
764 // method | 821 // method |
765 idents = []*ast.Ident{ident} | 822 idents = []*ast.Ident{ident} |
766 scope := ast.NewScope(nil) // method scope | 823 scope := ast.NewScope(nil) // method scope |
767 params, results := p.parseSignature(scope) | 824 params, results := p.parseSignature(scope) |
768 typ = &ast.FuncType{token.NoPos, params, results} | 825 typ = &ast.FuncType{token.NoPos, params, results} |
769 } else { | 826 } else { |
770 // embedded interface | 827 // embedded interface |
771 typ = x | 828 typ = x |
772 } | 829 } |
773 p.expectSemi() // call before accessing p.linecomment | 830 p.expectSemi() // call before accessing p.linecomment |
774 | 831 |
775 » return &ast.Field{doc, idents, typ, nil, p.lineComment} | 832 » spec := &ast.Field{doc, idents, typ, nil, p.lineComment} |
| 833 » p.declare(spec, scope, ast.Fun, idents...) |
| 834 |
| 835 » return spec |
776 } | 836 } |
777 | 837 |
778 | 838 |
779 func (p *parser) parseInterfaceType() *ast.InterfaceType { | 839 func (p *parser) parseInterfaceType() *ast.InterfaceType { |
780 if p.trace { | 840 if p.trace { |
781 defer un(trace(p, "InterfaceType")) | 841 defer un(trace(p, "InterfaceType")) |
782 } | 842 } |
783 | 843 |
784 pos := p.expect(token.INTERFACE) | 844 pos := p.expect(token.INTERFACE) |
785 lbrace := p.expect(token.LBRACE) | 845 lbrace := p.expect(token.LBRACE) |
| 846 scope := ast.NewScope(nil) // interface scope |
786 var list []*ast.Field | 847 var list []*ast.Field |
787 for p.tok == token.IDENT { | 848 for p.tok == token.IDENT { |
788 » » list = append(list, p.parseMethodSpec()) | 849 » » list = append(list, p.parseMethodSpec(scope)) |
789 } | 850 } |
790 rbrace := p.expect(token.RBRACE) | 851 rbrace := p.expect(token.RBRACE) |
791 | 852 |
| 853 // TODO(gri): store interface scope in AST |
792 return &ast.InterfaceType{pos, &ast.FieldList{lbrace, list, rbrace}, fal
se} | 854 return &ast.InterfaceType{pos, &ast.FieldList{lbrace, list, rbrace}, fal
se} |
793 } | 855 } |
794 | 856 |
795 | 857 |
796 func (p *parser) parseMapType() *ast.MapType { | 858 func (p *parser) parseMapType() *ast.MapType { |
797 if p.trace { | 859 if p.trace { |
798 defer un(trace(p, "MapType")) | 860 defer un(trace(p, "MapType")) |
799 } | 861 } |
800 | 862 |
801 pos := p.expect(token.MAP) | 863 pos := p.expect(token.MAP) |
(...skipping 23 matching lines...) Expand all Loading... |
825 p.expect(token.ARROW) | 887 p.expect(token.ARROW) |
826 p.expect(token.CHAN) | 888 p.expect(token.CHAN) |
827 dir = ast.RECV | 889 dir = ast.RECV |
828 } | 890 } |
829 value := p.parseType() | 891 value := p.parseType() |
830 | 892 |
831 return &ast.ChanType{pos, dir, value} | 893 return &ast.ChanType{pos, dir, value} |
832 } | 894 } |
833 | 895 |
834 | 896 |
835 func (p *parser) tryRawType(ellipsisOk bool) ast.Expr { | 897 // If the result is an identifier, it is not resolved. |
| 898 func (p *parser) tryIdentOrType(ellipsisOk bool) ast.Expr { |
836 switch p.tok { | 899 switch p.tok { |
837 case token.IDENT: | 900 case token.IDENT: |
838 return p.parseTypeName() | 901 return p.parseTypeName() |
839 case token.LBRACK: | 902 case token.LBRACK: |
840 return p.parseArrayType(ellipsisOk) | 903 return p.parseArrayType(ellipsisOk) |
841 case token.STRUCT: | 904 case token.STRUCT: |
842 return p.parseStructType() | 905 return p.parseStructType() |
843 case token.MUL: | 906 case token.MUL: |
844 return p.parsePointerType() | 907 return p.parsePointerType() |
845 case token.FUNC: | 908 case token.FUNC: |
(...skipping 11 matching lines...) Expand all Loading... |
857 typ := p.parseType() | 920 typ := p.parseType() |
858 rparen := p.expect(token.RPAREN) | 921 rparen := p.expect(token.RPAREN) |
859 return &ast.ParenExpr{lparen, typ, rparen} | 922 return &ast.ParenExpr{lparen, typ, rparen} |
860 } | 923 } |
861 | 924 |
862 // no type found | 925 // no type found |
863 return nil | 926 return nil |
864 } | 927 } |
865 | 928 |
866 | 929 |
867 func (p *parser) tryType() ast.Expr { return p.tryRawType(false) } | 930 func (p *parser) tryType() ast.Expr { |
| 931 » typ := p.tryIdentOrType(false) |
| 932 » if typ != nil { |
| 933 » » p.resolve(typ) |
| 934 » } |
| 935 » return typ |
| 936 } |
868 | 937 |
869 | 938 |
870 // ---------------------------------------------------------------------------- | 939 // ---------------------------------------------------------------------------- |
871 // Blocks | 940 // Blocks |
872 | 941 |
873 func (p *parser) parseStmtList() (list []ast.Stmt) { | 942 func (p *parser) parseStmtList() (list []ast.Stmt) { |
874 if p.trace { | 943 if p.trace { |
875 defer un(trace(p, "StatementList")) | 944 defer un(trace(p, "StatementList")) |
876 } | 945 } |
877 | 946 |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
932 p.exprLev++ | 1001 p.exprLev++ |
933 body := p.parseBody(scope) | 1002 body := p.parseBody(scope) |
934 p.exprLev-- | 1003 p.exprLev-- |
935 | 1004 |
936 return &ast.FuncLit{typ, body} | 1005 return &ast.FuncLit{typ, body} |
937 } | 1006 } |
938 | 1007 |
939 | 1008 |
940 // parseOperand may return an expression or a raw type (incl. array | 1009 // parseOperand may return an expression or a raw type (incl. array |
941 // types of the form [...]T. Callers must verify the result. | 1010 // types of the form [...]T. Callers must verify the result. |
| 1011 // If lhs is set and the result is an identifier, it is not resolved. |
942 // | 1012 // |
943 func (p *parser) parseOperand() ast.Expr { | 1013 func (p *parser) parseOperand(lhs bool) ast.Expr { |
944 if p.trace { | 1014 if p.trace { |
945 defer un(trace(p, "Operand")) | 1015 defer un(trace(p, "Operand")) |
946 } | 1016 } |
947 | 1017 |
948 switch p.tok { | 1018 switch p.tok { |
949 case token.IDENT: | 1019 case token.IDENT: |
950 » » ident := p.parseIdent() | 1020 » » x := p.parseIdent() |
951 » » p.resolve(ident) | 1021 » » if !lhs { |
952 » » return ident | 1022 » » » p.resolve(x) |
| 1023 » » } |
| 1024 » » return x |
953 | 1025 |
954 case token.INT, token.FLOAT, token.IMAG, token.CHAR, token.STRING: | 1026 case token.INT, token.FLOAT, token.IMAG, token.CHAR, token.STRING: |
955 x := &ast.BasicLit{p.pos, p.tok, p.lit()} | 1027 x := &ast.BasicLit{p.pos, p.tok, p.lit()} |
956 p.next() | 1028 p.next() |
957 return x | 1029 return x |
958 | 1030 |
959 case token.LPAREN: | 1031 case token.LPAREN: |
960 lparen := p.pos | 1032 lparen := p.pos |
961 p.next() | 1033 p.next() |
962 p.exprLev++ | 1034 p.exprLev++ |
963 » » x := p.parseExpr() | 1035 » » x := p.parseRhs() |
964 p.exprLev-- | 1036 p.exprLev-- |
965 rparen := p.expect(token.RPAREN) | 1037 rparen := p.expect(token.RPAREN) |
966 return &ast.ParenExpr{lparen, x, rparen} | 1038 return &ast.ParenExpr{lparen, x, rparen} |
967 | 1039 |
968 case token.FUNC: | 1040 case token.FUNC: |
969 return p.parseFuncTypeOrLit() | 1041 return p.parseFuncTypeOrLit() |
970 | 1042 |
971 default: | 1043 default: |
972 » » t := p.tryRawType(true) // could be type for composite literal o
r conversion | 1044 » » if typ := p.tryIdentOrType(true); typ != nil { |
973 » » if t != nil { | 1045 » » » // could be type for composite literal or conversion |
974 » » » return t | 1046 » » » _, isIdent := typ.(*ast.Ident) |
| 1047 » » » assert(!isIdent, "type cannot be identifier") |
| 1048 » » » return typ |
975 } | 1049 } |
976 } | 1050 } |
977 | 1051 |
978 pos := p.pos | 1052 pos := p.pos |
979 p.errorExpected(pos, "operand") | 1053 p.errorExpected(pos, "operand") |
980 p.next() // make progress | 1054 p.next() // make progress |
981 return &ast.BadExpr{pos, p.pos} | 1055 return &ast.BadExpr{pos, p.pos} |
982 } | 1056 } |
983 | 1057 |
984 | 1058 |
985 func (p *parser) parseSelectorOrTypeAssertion(x ast.Expr) ast.Expr { | 1059 func (p *parser) parseSelector(x ast.Expr) ast.Expr { |
986 if p.trace { | 1060 if p.trace { |
987 » » defer un(trace(p, "SelectorOrTypeAssertion")) | 1061 » » defer un(trace(p, "Selector")) |
988 } | 1062 } |
989 | 1063 |
990 » p.expect(token.PERIOD) | 1064 » sel := p.parseIdent() |
991 » if p.tok == token.IDENT { | 1065 |
992 » » // selector | 1066 » return &ast.SelectorExpr{x, sel} |
993 » » sel := p.parseIdent() | 1067 } |
994 » » return &ast.SelectorExpr{x, sel} | 1068 |
| 1069 |
| 1070 func (p *parser) parseTypeAssertion(x ast.Expr) ast.Expr { |
| 1071 » if p.trace { |
| 1072 » » defer un(trace(p, "TypeAssertion")) |
995 } | 1073 } |
996 | 1074 |
997 // type assertion | |
998 p.expect(token.LPAREN) | 1075 p.expect(token.LPAREN) |
999 var typ ast.Expr | 1076 var typ ast.Expr |
1000 if p.tok == token.TYPE { | 1077 if p.tok == token.TYPE { |
1001 // type switch: typ == nil | 1078 // type switch: typ == nil |
1002 p.next() | 1079 p.next() |
1003 } else { | 1080 } else { |
1004 typ = p.parseType() | 1081 typ = p.parseType() |
1005 } | 1082 } |
1006 p.expect(token.RPAREN) | 1083 p.expect(token.RPAREN) |
1007 | 1084 |
1008 return &ast.TypeAssertExpr{x, typ} | 1085 return &ast.TypeAssertExpr{x, typ} |
1009 } | 1086 } |
1010 | 1087 |
1011 | 1088 |
1012 func (p *parser) parseIndexOrSlice(x ast.Expr) ast.Expr { | 1089 func (p *parser) parseIndexOrSlice(x ast.Expr) ast.Expr { |
1013 if p.trace { | 1090 if p.trace { |
1014 defer un(trace(p, "IndexOrSlice")) | 1091 defer un(trace(p, "IndexOrSlice")) |
1015 } | 1092 } |
1016 | 1093 |
1017 lbrack := p.expect(token.LBRACK) | 1094 lbrack := p.expect(token.LBRACK) |
1018 p.exprLev++ | 1095 p.exprLev++ |
1019 var low, high ast.Expr | 1096 var low, high ast.Expr |
1020 isSlice := false | 1097 isSlice := false |
1021 if p.tok != token.COLON { | 1098 if p.tok != token.COLON { |
1022 » » low = p.parseExpr() | 1099 » » low = p.parseRhs() |
1023 } | 1100 } |
1024 if p.tok == token.COLON { | 1101 if p.tok == token.COLON { |
1025 isSlice = true | 1102 isSlice = true |
1026 p.next() | 1103 p.next() |
1027 if p.tok != token.RBRACK { | 1104 if p.tok != token.RBRACK { |
1028 » » » high = p.parseExpr() | 1105 » » » high = p.parseRhs() |
1029 } | 1106 } |
1030 } | 1107 } |
1031 p.exprLev-- | 1108 p.exprLev-- |
1032 rbrack := p.expect(token.RBRACK) | 1109 rbrack := p.expect(token.RBRACK) |
1033 | 1110 |
1034 if isSlice { | 1111 if isSlice { |
1035 return &ast.SliceExpr{x, lbrack, low, high, rbrack} | 1112 return &ast.SliceExpr{x, lbrack, low, high, rbrack} |
1036 } | 1113 } |
1037 return &ast.IndexExpr{x, lbrack, low, rbrack} | 1114 return &ast.IndexExpr{x, lbrack, low, rbrack} |
1038 } | 1115 } |
1039 | 1116 |
1040 | 1117 |
1041 func (p *parser) parseCallOrConversion(fun ast.Expr) *ast.CallExpr { | 1118 func (p *parser) parseCallOrConversion(fun ast.Expr) *ast.CallExpr { |
1042 if p.trace { | 1119 if p.trace { |
1043 defer un(trace(p, "CallOrConversion")) | 1120 defer un(trace(p, "CallOrConversion")) |
1044 } | 1121 } |
1045 | 1122 |
1046 lparen := p.expect(token.LPAREN) | 1123 lparen := p.expect(token.LPAREN) |
1047 p.exprLev++ | 1124 p.exprLev++ |
1048 var list []ast.Expr | 1125 var list []ast.Expr |
1049 var ellipsis token.Pos | 1126 var ellipsis token.Pos |
1050 for p.tok != token.RPAREN && p.tok != token.EOF && !ellipsis.IsValid() { | 1127 for p.tok != token.RPAREN && p.tok != token.EOF && !ellipsis.IsValid() { |
1051 » » list = append(list, p.parseExpr()) | 1128 » » list = append(list, p.parseRhs()) |
1052 if p.tok == token.ELLIPSIS { | 1129 if p.tok == token.ELLIPSIS { |
1053 ellipsis = p.pos | 1130 ellipsis = p.pos |
1054 p.next() | 1131 p.next() |
1055 } | 1132 } |
1056 if p.tok != token.COMMA { | 1133 if p.tok != token.COMMA { |
1057 break | 1134 break |
1058 } | 1135 } |
1059 p.next() | 1136 p.next() |
1060 } | 1137 } |
1061 p.exprLev-- | 1138 p.exprLev-- |
1062 rparen := p.expect(token.RPAREN) | 1139 rparen := p.expect(token.RPAREN) |
1063 | 1140 |
1064 return &ast.CallExpr{fun, lparen, list, ellipsis, rparen} | 1141 return &ast.CallExpr{fun, lparen, list, ellipsis, rparen} |
1065 } | 1142 } |
1066 | 1143 |
1067 | 1144 |
1068 func (p *parser) parseElement(keyOk bool) ast.Expr { | 1145 func (p *parser) parseElement(keyOk bool) ast.Expr { |
1069 if p.trace { | 1146 if p.trace { |
1070 defer un(trace(p, "Element")) | 1147 defer un(trace(p, "Element")) |
1071 } | 1148 } |
1072 | 1149 |
1073 if p.tok == token.LBRACE { | 1150 if p.tok == token.LBRACE { |
1074 return p.parseLiteralValue(nil) | 1151 return p.parseLiteralValue(nil) |
1075 } | 1152 } |
1076 | 1153 |
1077 » x := p.parseExpr() | 1154 » x := p.parseRhs() |
1078 if keyOk && p.tok == token.COLON { | 1155 if keyOk && p.tok == token.COLON { |
1079 colon := p.pos | 1156 colon := p.pos |
1080 p.next() | 1157 p.next() |
1081 x = &ast.KeyValueExpr{x, colon, p.parseElement(false)} | 1158 x = &ast.KeyValueExpr{x, colon, p.parseElement(false)} |
1082 } | 1159 } |
1083 return x | 1160 return x |
1084 } | 1161 } |
1085 | 1162 |
1086 | 1163 |
1087 func (p *parser) parseElementList() (list []ast.Expr) { | 1164 func (p *parser) parseElementList() (list []ast.Expr) { |
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1224 p.error(len.Pos(), "expected array length, found '...'") | 1301 p.error(len.Pos(), "expected array length, found '...'") |
1225 x = &ast.BadExpr{x.Pos(), x.End()} | 1302 x = &ast.BadExpr{x.Pos(), x.End()} |
1226 } | 1303 } |
1227 } | 1304 } |
1228 | 1305 |
1229 // all other nodes are expressions or types | 1306 // all other nodes are expressions or types |
1230 return x | 1307 return x |
1231 } | 1308 } |
1232 | 1309 |
1233 | 1310 |
1234 func (p *parser) parsePrimaryExpr() ast.Expr { | 1311 // If lhs is set and the result is an identifier, it is not resolved. |
| 1312 func (p *parser) parsePrimaryExpr(lhs bool) ast.Expr { |
1235 if p.trace { | 1313 if p.trace { |
1236 defer un(trace(p, "PrimaryExpr")) | 1314 defer un(trace(p, "PrimaryExpr")) |
1237 } | 1315 } |
1238 | 1316 |
1239 » x := p.parseOperand() | 1317 » x := p.parseOperand(lhs) |
1240 L: | 1318 L: |
1241 for { | 1319 for { |
1242 switch p.tok { | 1320 switch p.tok { |
1243 case token.PERIOD: | 1321 case token.PERIOD: |
1244 » » » x = p.parseSelectorOrTypeAssertion(p.checkExpr(x)) | 1322 » » » p.next() |
| 1323 » » » if lhs { |
| 1324 » » » » p.resolve(x) |
| 1325 » » » } |
| 1326 » » » switch p.tok { |
| 1327 » » » case token.IDENT: |
| 1328 » » » » x = p.parseSelector(p.checkExpr(x)) |
| 1329 » » » case token.LPAREN: |
| 1330 » » » » x = p.parseTypeAssertion(p.checkExpr(x)) |
| 1331 » » » default: |
| 1332 » » » » pos := p.pos |
| 1333 » » » » p.next() // make progress |
| 1334 » » » » p.errorExpected(pos, "selector or type assertion
") |
| 1335 » » » » x = &ast.BadExpr{pos, p.pos} |
| 1336 » » » } |
1245 case token.LBRACK: | 1337 case token.LBRACK: |
| 1338 if lhs { |
| 1339 p.resolve(x) |
| 1340 } |
1246 x = p.parseIndexOrSlice(p.checkExpr(x)) | 1341 x = p.parseIndexOrSlice(p.checkExpr(x)) |
1247 case token.LPAREN: | 1342 case token.LPAREN: |
| 1343 if lhs { |
| 1344 p.resolve(x) |
| 1345 } |
1248 x = p.parseCallOrConversion(p.checkExprOrType(x)) | 1346 x = p.parseCallOrConversion(p.checkExprOrType(x)) |
1249 case token.LBRACE: | 1347 case token.LBRACE: |
1250 if isLiteralType(x) && (p.exprLev >= 0 || !isTypeName(x)
) { | 1348 if isLiteralType(x) && (p.exprLev >= 0 || !isTypeName(x)
) { |
| 1349 if lhs { |
| 1350 p.resolve(x) |
| 1351 } |
1251 x = p.parseLiteralValue(x) | 1352 x = p.parseLiteralValue(x) |
1252 } else { | 1353 } else { |
1253 break L | 1354 break L |
1254 } | 1355 } |
1255 default: | 1356 default: |
1256 break L | 1357 break L |
1257 } | 1358 } |
| 1359 lhs = false // no need to try to resolve again |
1258 } | 1360 } |
1259 | 1361 |
1260 return x | 1362 return x |
1261 } | 1363 } |
1262 | 1364 |
1263 | 1365 |
1264 func (p *parser) parseUnaryExpr() ast.Expr { | 1366 // If lhs is set and the result is an identifier, it is not resolved. |
| 1367 func (p *parser) parseUnaryExpr(lhs bool) ast.Expr { |
1265 if p.trace { | 1368 if p.trace { |
1266 defer un(trace(p, "UnaryExpr")) | 1369 defer un(trace(p, "UnaryExpr")) |
1267 } | 1370 } |
1268 | 1371 |
1269 switch p.tok { | 1372 switch p.tok { |
1270 case token.ADD, token.SUB, token.NOT, token.XOR, token.AND, token.RANGE: | 1373 case token.ADD, token.SUB, token.NOT, token.XOR, token.AND, token.RANGE: |
1271 pos, op := p.pos, p.tok | 1374 pos, op := p.pos, p.tok |
1272 p.next() | 1375 p.next() |
1273 » » x := p.parseUnaryExpr() | 1376 » » x := p.parseUnaryExpr(false) |
1274 return &ast.UnaryExpr{pos, op, p.checkExpr(x)} | 1377 return &ast.UnaryExpr{pos, op, p.checkExpr(x)} |
1275 | 1378 |
1276 case token.ARROW: | 1379 case token.ARROW: |
1277 // channel type or receive expression | 1380 // channel type or receive expression |
1278 pos := p.pos | 1381 pos := p.pos |
1279 p.next() | 1382 p.next() |
1280 if p.tok == token.CHAN { | 1383 if p.tok == token.CHAN { |
1281 p.next() | 1384 p.next() |
1282 value := p.parseType() | 1385 value := p.parseType() |
1283 return &ast.ChanType{pos, ast.RECV, value} | 1386 return &ast.ChanType{pos, ast.RECV, value} |
1284 } | 1387 } |
1285 | 1388 |
1286 » » x := p.parseUnaryExpr() | 1389 » » x := p.parseUnaryExpr(false) |
1287 return &ast.UnaryExpr{pos, token.ARROW, p.checkExpr(x)} | 1390 return &ast.UnaryExpr{pos, token.ARROW, p.checkExpr(x)} |
1288 | 1391 |
1289 case token.MUL: | 1392 case token.MUL: |
1290 // pointer type or unary "*" expression | 1393 // pointer type or unary "*" expression |
1291 pos := p.pos | 1394 pos := p.pos |
1292 p.next() | 1395 p.next() |
1293 » » x := p.parseUnaryExpr() | 1396 » » x := p.parseUnaryExpr(false) |
1294 return &ast.StarExpr{pos, p.checkExprOrType(x)} | 1397 return &ast.StarExpr{pos, p.checkExprOrType(x)} |
1295 } | 1398 } |
1296 | 1399 |
1297 » return p.parsePrimaryExpr() | 1400 » return p.parsePrimaryExpr(lhs) |
1298 } | 1401 } |
1299 | 1402 |
1300 | 1403 |
1301 func (p *parser) parseBinaryExpr(prec1 int) ast.Expr { | 1404 // If lhs is set and the result is an identifier, it is not resolved. |
| 1405 func (p *parser) parseBinaryExpr(lhs bool, prec1 int) ast.Expr { |
1302 if p.trace { | 1406 if p.trace { |
1303 defer un(trace(p, "BinaryExpr")) | 1407 defer un(trace(p, "BinaryExpr")) |
1304 } | 1408 } |
1305 | 1409 |
1306 » x := p.parseUnaryExpr() | 1410 » x := p.parseUnaryExpr(lhs) |
1307 for prec := p.tok.Precedence(); prec >= prec1; prec-- { | 1411 for prec := p.tok.Precedence(); prec >= prec1; prec-- { |
1308 for p.tok.Precedence() == prec { | 1412 for p.tok.Precedence() == prec { |
1309 pos, op := p.pos, p.tok | 1413 pos, op := p.pos, p.tok |
1310 p.next() | 1414 p.next() |
1311 » » » y := p.parseBinaryExpr(prec + 1) | 1415 » » » if lhs { |
| 1416 » » » » p.resolve(x) |
| 1417 » » » » lhs = false |
| 1418 » » » } |
| 1419 » » » y := p.parseBinaryExpr(false, prec+1) |
1312 x = &ast.BinaryExpr{p.checkExpr(x), pos, op, p.checkExpr
(y)} | 1420 x = &ast.BinaryExpr{p.checkExpr(x), pos, op, p.checkExpr
(y)} |
1313 } | 1421 } |
1314 } | 1422 } |
1315 | 1423 |
1316 return x | 1424 return x |
1317 } | 1425 } |
1318 | 1426 |
1319 | 1427 |
| 1428 // If lhs is set and the result is an identifier, it is not resolved. |
1320 // TODO(gri): parseExpr may return a type or even a raw type ([..]int) - | 1429 // TODO(gri): parseExpr may return a type or even a raw type ([..]int) - |
1321 // should reject when a type/raw type is obviously not allowed | 1430 // should reject when a type/raw type is obviously not allowed |
1322 func (p *parser) parseExpr() ast.Expr { | 1431 func (p *parser) parseExpr(lhs bool) ast.Expr { |
1323 if p.trace { | 1432 if p.trace { |
1324 defer un(trace(p, "Expression")) | 1433 defer un(trace(p, "Expression")) |
1325 } | 1434 } |
1326 | 1435 |
1327 » return p.parseBinaryExpr(token.LowestPrec + 1) | 1436 » return p.parseBinaryExpr(lhs, token.LowestPrec+1) |
1328 } | 1437 } |
1329 | 1438 |
1330 | 1439 |
| 1440 func (p *parser) parseRhs() ast.Expr { |
| 1441 return p.parseExpr(false) |
| 1442 } |
| 1443 |
| 1444 |
1331 // ---------------------------------------------------------------------------- | 1445 // ---------------------------------------------------------------------------- |
1332 // Statements | 1446 // Statements |
1333 | 1447 |
1334 func (p *parser) parseSimpleStmt(labelOk bool) ast.Stmt { | 1448 func (p *parser) parseSimpleStmt(labelOk bool) ast.Stmt { |
1335 if p.trace { | 1449 if p.trace { |
1336 defer un(trace(p, "SimpleStmt")) | 1450 defer un(trace(p, "SimpleStmt")) |
1337 } | 1451 } |
1338 | 1452 |
1339 » x := p.parseExprList() | 1453 » x := p.parseLhsList() |
1340 | 1454 |
1341 switch p.tok { | 1455 switch p.tok { |
1342 case | 1456 case |
1343 token.DEFINE, token.ASSIGN, token.ADD_ASSIGN, | 1457 token.DEFINE, token.ASSIGN, token.ADD_ASSIGN, |
1344 token.SUB_ASSIGN, token.MUL_ASSIGN, token.QUO_ASSIGN, | 1458 token.SUB_ASSIGN, token.MUL_ASSIGN, token.QUO_ASSIGN, |
1345 token.REM_ASSIGN, token.AND_ASSIGN, token.OR_ASSIGN, | 1459 token.REM_ASSIGN, token.AND_ASSIGN, token.OR_ASSIGN, |
1346 token.XOR_ASSIGN, token.SHL_ASSIGN, token.SHR_ASSIGN, token.AND_
NOT_ASSIGN: | 1460 token.XOR_ASSIGN, token.SHL_ASSIGN, token.SHR_ASSIGN, token.AND_
NOT_ASSIGN: |
1347 // assignment statement | 1461 // assignment statement |
1348 pos, tok := p.pos, p.tok | 1462 pos, tok := p.pos, p.tok |
1349 p.next() | 1463 p.next() |
1350 » » y := p.parseExprList() | 1464 » » y := p.parseRhsList() |
1351 » » if tok == token.DEFINE { | |
1352 » » » p.shortVarDecl(p.makeIdentList(x)) | |
1353 » » } | |
1354 return &ast.AssignStmt{x, pos, tok, y} | 1465 return &ast.AssignStmt{x, pos, tok, y} |
1355 } | 1466 } |
1356 | 1467 |
1357 if len(x) > 1 { | 1468 if len(x) > 1 { |
1358 p.errorExpected(x[0].Pos(), "1 expression") | 1469 p.errorExpected(x[0].Pos(), "1 expression") |
1359 // continue with first expression | 1470 // continue with first expression |
1360 } | 1471 } |
1361 | 1472 |
1362 switch p.tok { | 1473 switch p.tok { |
1363 case token.COLON: | 1474 case token.COLON: |
1364 // labeled statement | 1475 // labeled statement |
1365 colon := p.pos | 1476 colon := p.pos |
1366 p.next() | 1477 p.next() |
1367 if label, isIdent := x[0].(*ast.Ident); labelOk && isIdent { | 1478 if label, isIdent := x[0].(*ast.Ident); labelOk && isIdent { |
1368 // Go spec: The scope of a label is the body of the func
tion | 1479 // Go spec: The scope of a label is the body of the func
tion |
1369 // in which it is declared and excludes the body of any
nested | 1480 // in which it is declared and excludes the body of any
nested |
1370 // function. | 1481 // function. |
1371 stmt := &ast.LabeledStmt{label, colon, p.parseStmt()} | 1482 stmt := &ast.LabeledStmt{label, colon, p.parseStmt()} |
1372 p.declare(stmt, p.labelScope, ast.Lbl, label) | 1483 p.declare(stmt, p.labelScope, ast.Lbl, label) |
1373 return stmt | 1484 return stmt |
1374 } | 1485 } |
1375 p.error(x[0].Pos(), "illegal label declaration") | 1486 p.error(x[0].Pos(), "illegal label declaration") |
1376 return &ast.BadStmt{x[0].Pos(), colon + 1} | 1487 return &ast.BadStmt{x[0].Pos(), colon + 1} |
1377 | 1488 |
1378 case token.ARROW: | 1489 case token.ARROW: |
1379 // send statement | 1490 // send statement |
1380 arrow := p.pos | 1491 arrow := p.pos |
1381 p.next() // consume "<-" | 1492 p.next() // consume "<-" |
1382 » » y := p.parseExpr() | 1493 » » y := p.parseRhs() |
1383 return &ast.SendStmt{x[0], arrow, y} | 1494 return &ast.SendStmt{x[0], arrow, y} |
1384 | 1495 |
1385 case token.INC, token.DEC: | 1496 case token.INC, token.DEC: |
1386 // increment or decrement | 1497 // increment or decrement |
1387 s := &ast.IncDecStmt{x[0], p.pos, p.tok} | 1498 s := &ast.IncDecStmt{x[0], p.pos, p.tok} |
1388 p.next() // consume "++" or "--" | 1499 p.next() // consume "++" or "--" |
1389 return s | 1500 return s |
1390 } | 1501 } |
1391 | 1502 |
1392 // expression | 1503 // expression |
1393 return &ast.ExprStmt{x[0]} | 1504 return &ast.ExprStmt{x[0]} |
1394 } | 1505 } |
1395 | 1506 |
1396 | 1507 |
1397 func (p *parser) parseCallExpr() *ast.CallExpr { | 1508 func (p *parser) parseCallExpr() *ast.CallExpr { |
1398 » x := p.parseExpr() | 1509 » x := p.parseRhs() |
1399 if call, isCall := x.(*ast.CallExpr); isCall { | 1510 if call, isCall := x.(*ast.CallExpr); isCall { |
1400 return call | 1511 return call |
1401 } | 1512 } |
1402 p.errorExpected(x.Pos(), "function/method call") | 1513 p.errorExpected(x.Pos(), "function/method call") |
1403 return nil | 1514 return nil |
1404 } | 1515 } |
1405 | 1516 |
1406 | 1517 |
1407 func (p *parser) parseGoStmt() ast.Stmt { | 1518 func (p *parser) parseGoStmt() ast.Stmt { |
1408 if p.trace { | 1519 if p.trace { |
(...skipping 29 matching lines...) Expand all Loading... |
1438 | 1549 |
1439 func (p *parser) parseReturnStmt() *ast.ReturnStmt { | 1550 func (p *parser) parseReturnStmt() *ast.ReturnStmt { |
1440 if p.trace { | 1551 if p.trace { |
1441 defer un(trace(p, "ReturnStmt")) | 1552 defer un(trace(p, "ReturnStmt")) |
1442 } | 1553 } |
1443 | 1554 |
1444 pos := p.pos | 1555 pos := p.pos |
1445 p.expect(token.RETURN) | 1556 p.expect(token.RETURN) |
1446 var x []ast.Expr | 1557 var x []ast.Expr |
1447 if p.tok != token.SEMICOLON && p.tok != token.RBRACE { | 1558 if p.tok != token.SEMICOLON && p.tok != token.RBRACE { |
1448 » » x = p.parseExprList() | 1559 » » x = p.parseRhsList() |
1449 } | 1560 } |
1450 p.expectSemi() | 1561 p.expectSemi() |
1451 | 1562 |
1452 return &ast.ReturnStmt{pos, x} | 1563 return &ast.ReturnStmt{pos, x} |
1453 } | 1564 } |
1454 | 1565 |
1455 | 1566 |
1456 func (p *parser) parseBranchStmt(tok token.Token) *ast.BranchStmt { | 1567 func (p *parser) parseBranchStmt(tok token.Token) *ast.BranchStmt { |
1457 if p.trace { | 1568 if p.trace { |
1458 defer un(trace(p, "BranchStmt")) | 1569 defer un(trace(p, "BranchStmt")) |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1493 p.openScope() | 1604 p.openScope() |
1494 defer p.closeScope() | 1605 defer p.closeScope() |
1495 | 1606 |
1496 var s ast.Stmt | 1607 var s ast.Stmt |
1497 var x ast.Expr | 1608 var x ast.Expr |
1498 { | 1609 { |
1499 prevLev := p.exprLev | 1610 prevLev := p.exprLev |
1500 p.exprLev = -1 | 1611 p.exprLev = -1 |
1501 if p.tok == token.SEMICOLON { | 1612 if p.tok == token.SEMICOLON { |
1502 p.next() | 1613 p.next() |
1503 » » » x = p.parseExpr() | 1614 » » » x = p.parseRhs() |
1504 } else { | 1615 } else { |
1505 s = p.parseSimpleStmt(false) | 1616 s = p.parseSimpleStmt(false) |
1506 if p.tok == token.SEMICOLON { | 1617 if p.tok == token.SEMICOLON { |
1507 p.next() | 1618 p.next() |
1508 » » » » x = p.parseExpr() | 1619 » » » » x = p.parseRhs() |
1509 } else { | 1620 } else { |
1510 x = p.makeExpr(s) | 1621 x = p.makeExpr(s) |
1511 s = nil | 1622 s = nil |
1512 } | 1623 } |
1513 } | 1624 } |
1514 p.exprLev = prevLev | 1625 p.exprLev = prevLev |
1515 } | 1626 } |
1516 | 1627 |
1517 body := p.parseBlockStmt() | 1628 body := p.parseBlockStmt() |
1518 var else_ ast.Stmt | 1629 var else_ ast.Stmt |
(...skipping 26 matching lines...) Expand all Loading... |
1545 func (p *parser) parseCaseClause(exprSwitch bool) *ast.CaseClause { | 1656 func (p *parser) parseCaseClause(exprSwitch bool) *ast.CaseClause { |
1546 if p.trace { | 1657 if p.trace { |
1547 defer un(trace(p, "CaseClause")) | 1658 defer un(trace(p, "CaseClause")) |
1548 } | 1659 } |
1549 | 1660 |
1550 pos := p.pos | 1661 pos := p.pos |
1551 var list []ast.Expr | 1662 var list []ast.Expr |
1552 if p.tok == token.CASE { | 1663 if p.tok == token.CASE { |
1553 p.next() | 1664 p.next() |
1554 if exprSwitch { | 1665 if exprSwitch { |
1555 » » » list = p.parseExprList() | 1666 » » » list = p.parseRhsList() |
1556 } else { | 1667 } else { |
1557 list = p.parseTypeList() | 1668 list = p.parseTypeList() |
1558 } | 1669 } |
1559 } else { | 1670 } else { |
1560 p.expect(token.DEFAULT) | 1671 p.expect(token.DEFAULT) |
1561 } | 1672 } |
1562 | 1673 |
1563 colon := p.expect(token.COLON) | 1674 colon := p.expect(token.COLON) |
1564 p.openScope() | 1675 p.openScope() |
1565 body := p.parseStmtList() | 1676 body := p.parseStmtList() |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1632 func (p *parser) parseCommClause() *ast.CommClause { | 1743 func (p *parser) parseCommClause() *ast.CommClause { |
1633 if p.trace { | 1744 if p.trace { |
1634 defer un(trace(p, "CommClause")) | 1745 defer un(trace(p, "CommClause")) |
1635 } | 1746 } |
1636 | 1747 |
1637 p.openScope() | 1748 p.openScope() |
1638 pos := p.pos | 1749 pos := p.pos |
1639 var comm ast.Stmt | 1750 var comm ast.Stmt |
1640 if p.tok == token.CASE { | 1751 if p.tok == token.CASE { |
1641 p.next() | 1752 p.next() |
1642 » » lhs := p.parseExprList() | 1753 » » lhs := p.parseLhsList() |
1643 if p.tok == token.ARROW { | 1754 if p.tok == token.ARROW { |
1644 // SendStmt | 1755 // SendStmt |
1645 if len(lhs) > 1 { | 1756 if len(lhs) > 1 { |
1646 p.errorExpected(lhs[0].Pos(), "1 expression") | 1757 p.errorExpected(lhs[0].Pos(), "1 expression") |
1647 // continue with first expression | 1758 // continue with first expression |
1648 } | 1759 } |
1649 arrow := p.pos | 1760 arrow := p.pos |
1650 p.next() | 1761 p.next() |
1651 » » » rhs := p.parseExpr() | 1762 » » » rhs := p.parseRhs() |
1652 comm = &ast.SendStmt{lhs[0], arrow, rhs} | 1763 comm = &ast.SendStmt{lhs[0], arrow, rhs} |
1653 } else { | 1764 } else { |
1654 // RecvStmt | 1765 // RecvStmt |
1655 pos := p.pos | 1766 pos := p.pos |
1656 tok := p.tok | 1767 tok := p.tok |
1657 var rhs ast.Expr | 1768 var rhs ast.Expr |
1658 if tok == token.ASSIGN || tok == token.DEFINE { | 1769 if tok == token.ASSIGN || tok == token.DEFINE { |
1659 // RecvStmt with assignment | 1770 // RecvStmt with assignment |
1660 if len(lhs) > 2 { | 1771 if len(lhs) > 2 { |
1661 p.errorExpected(lhs[0].Pos(), "1 or 2 ex
pressions") | 1772 p.errorExpected(lhs[0].Pos(), "1 or 2 ex
pressions") |
1662 // continue with first two expressions | 1773 // continue with first two expressions |
1663 lhs = lhs[0:2] | 1774 lhs = lhs[0:2] |
1664 } | 1775 } |
1665 p.next() | 1776 p.next() |
1666 » » » » rhs = p.parseExpr() | 1777 » » » » rhs = p.parseRhs() |
1667 » » » » if tok == token.DEFINE { | |
1668 » » » » » p.shortVarDecl(p.makeIdentList(lhs)) | |
1669 » » » » } | |
1670 } else { | 1778 } else { |
1671 // rhs must be single receive operation | 1779 // rhs must be single receive operation |
1672 if len(lhs) > 1 { | 1780 if len(lhs) > 1 { |
1673 p.errorExpected(lhs[0].Pos(), "1 express
ion") | 1781 p.errorExpected(lhs[0].Pos(), "1 express
ion") |
1674 // continue with first expression | 1782 // continue with first expression |
1675 } | 1783 } |
1676 rhs = lhs[0] | 1784 rhs = lhs[0] |
1677 lhs = nil // there is no lhs | 1785 lhs = nil // there is no lhs |
1678 } | 1786 } |
1679 if x, isUnary := rhs.(*ast.UnaryExpr); !isUnary || x.Op
!= token.ARROW { | 1787 if x, isUnary := rhs.(*ast.UnaryExpr); !isUnary || x.Op
!= token.ARROW { |
(...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1866 | 1974 |
1867 var path *ast.BasicLit | 1975 var path *ast.BasicLit |
1868 if p.tok == token.STRING { | 1976 if p.tok == token.STRING { |
1869 path = &ast.BasicLit{p.pos, p.tok, p.lit()} | 1977 path = &ast.BasicLit{p.pos, p.tok, p.lit()} |
1870 p.next() | 1978 p.next() |
1871 } else { | 1979 } else { |
1872 p.expect(token.STRING) // use expect() error handling | 1980 p.expect(token.STRING) // use expect() error handling |
1873 } | 1981 } |
1874 p.expectSemi() // call before accessing p.linecomment | 1982 p.expectSemi() // call before accessing p.linecomment |
1875 | 1983 |
1876 » return &ast.ImportSpec{doc, ident, path, p.lineComment} | 1984 » // collect imports |
| 1985 » spec := &ast.ImportSpec{doc, ident, path, p.lineComment} |
| 1986 » p.imports = append(p.imports, spec) |
| 1987 |
| 1988 » return spec |
1877 } | 1989 } |
1878 | 1990 |
1879 | 1991 |
1880 func parseConstSpec(p *parser, doc *ast.CommentGroup, iota int) ast.Spec { | 1992 func parseConstSpec(p *parser, doc *ast.CommentGroup, iota int) ast.Spec { |
1881 if p.trace { | 1993 if p.trace { |
1882 defer un(trace(p, "ConstSpec")) | 1994 defer un(trace(p, "ConstSpec")) |
1883 } | 1995 } |
1884 | 1996 |
1885 idents := p.parseIdentList() | 1997 idents := p.parseIdentList() |
1886 typ := p.tryType() | 1998 typ := p.tryType() |
1887 var values []ast.Expr | 1999 var values []ast.Expr |
1888 if typ != nil || p.tok == token.ASSIGN || iota == 0 { | 2000 if typ != nil || p.tok == token.ASSIGN || iota == 0 { |
1889 p.expect(token.ASSIGN) | 2001 p.expect(token.ASSIGN) |
1890 » » values = p.parseExprList() | 2002 » » values = p.parseRhsList() |
1891 } | 2003 } |
1892 p.expectSemi() // call before accessing p.linecomment | 2004 p.expectSemi() // call before accessing p.linecomment |
1893 | 2005 |
1894 // Go spec: The scope of a constant or variable identifier declared insi
de | 2006 // Go spec: The scope of a constant or variable identifier declared insi
de |
1895 // a function begins at the end of the ConstSpec or VarSpec and ends at | 2007 // a function begins at the end of the ConstSpec or VarSpec and ends at |
1896 // the end of the innermost containing block. | 2008 // the end of the innermost containing block. |
1897 // (Global identifiers are resolved in a separate phase after parsing.) | 2009 // (Global identifiers are resolved in a separate phase after parsing.) |
1898 spec := &ast.ValueSpec{doc, idents, typ, values, p.lineComment} | 2010 spec := &ast.ValueSpec{doc, idents, typ, values, p.lineComment} |
1899 p.declare(spec, p.topScope, ast.Con, idents...) | 2011 p.declare(spec, p.topScope, ast.Con, idents...) |
1900 | 2012 |
(...skipping 24 matching lines...) Expand all Loading... |
1925 func parseVarSpec(p *parser, doc *ast.CommentGroup, _ int) ast.Spec { | 2037 func parseVarSpec(p *parser, doc *ast.CommentGroup, _ int) ast.Spec { |
1926 if p.trace { | 2038 if p.trace { |
1927 defer un(trace(p, "VarSpec")) | 2039 defer un(trace(p, "VarSpec")) |
1928 } | 2040 } |
1929 | 2041 |
1930 idents := p.parseIdentList() | 2042 idents := p.parseIdentList() |
1931 typ := p.tryType() | 2043 typ := p.tryType() |
1932 var values []ast.Expr | 2044 var values []ast.Expr |
1933 if typ == nil || p.tok == token.ASSIGN { | 2045 if typ == nil || p.tok == token.ASSIGN { |
1934 p.expect(token.ASSIGN) | 2046 p.expect(token.ASSIGN) |
1935 » » values = p.parseExprList() | 2047 » » values = p.parseRhsList() |
1936 } | 2048 } |
1937 p.expectSemi() // call before accessing p.linecomment | 2049 p.expectSemi() // call before accessing p.linecomment |
1938 | 2050 |
1939 // Go spec: The scope of a constant or variable identifier declared insi
de | 2051 // Go spec: The scope of a constant or variable identifier declared insi
de |
1940 // a function begins at the end of the ConstSpec or VarSpec and ends at | 2052 // a function begins at the end of the ConstSpec or VarSpec and ends at |
1941 // the end of the innermost containing block. | 2053 // the end of the innermost containing block. |
1942 // (Global identifiers are resolved in a separate phase after parsing.) | 2054 // (Global identifiers are resolved in a separate phase after parsing.) |
1943 spec := &ast.ValueSpec{doc, idents, typ, values, p.lineComment} | 2055 spec := &ast.ValueSpec{doc, idents, typ, values, p.lineComment} |
1944 p.declare(spec, p.topScope, ast.Var, idents...) | 2056 p.declare(spec, p.topScope, ast.Var, idents...) |
1945 | 2057 |
(...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2113 } | 2225 } |
2114 | 2226 |
2115 if p.mode&ImportsOnly == 0 { | 2227 if p.mode&ImportsOnly == 0 { |
2116 // rest of package body | 2228 // rest of package body |
2117 for p.tok != token.EOF { | 2229 for p.tok != token.EOF { |
2118 decls = append(decls, p.parseDecl()) | 2230 decls = append(decls, p.parseDecl()) |
2119 } | 2231 } |
2120 } | 2232 } |
2121 } | 2233 } |
2122 | 2234 |
2123 » if p.topScope != p.pkgScope { | 2235 » assert(p.topScope == p.pkgScope, "imbalanced scopes") |
2124 » » panic("internal error: imbalanced scopes") | |
2125 » } | |
2126 | 2236 |
2127 // resolve global identifiers within the same file | 2237 // resolve global identifiers within the same file |
2128 i := 0 | 2238 i := 0 |
2129 for _, ident := range p.unresolved { | 2239 for _, ident := range p.unresolved { |
2130 // i <= index for current ident | 2240 // i <= index for current ident |
2131 » » ident.Obj = p.pkgScope.Lookup(ident.Name) | 2241 » » assert(ident.Obj == unresolved, "object already resolved") |
| 2242 » » ident.Obj = p.pkgScope.Lookup(ident.Name) // also removes unreso
lved sentinel |
2132 if ident.Obj == nil { | 2243 if ident.Obj == nil { |
2133 p.unresolved[i] = ident | 2244 p.unresolved[i] = ident |
2134 i++ | 2245 i++ |
2135 } | 2246 } |
2136 } | 2247 } |
2137 | 2248 |
| 2249 // TODO(gri): store p.imports in AST |
2138 return &ast.File{doc, pos, ident, decls, p.pkgScope, p.unresolved[0:i],
p.comments} | 2250 return &ast.File{doc, pos, ident, decls, p.pkgScope, p.unresolved[0:i],
p.comments} |
2139 } | 2251 } |
OLD | NEW |