Skip to content

Commit

Permalink
go/parser: minor cleanup
Browse files Browse the repository at this point in the history
- there is no label scope at package level
- open/close all scopes symmetrically now
  that there is only one parse entry point
  (parseFile)

R=golang-dev, r
CC=golang-dev
https://golang.org/cl/6230047
  • Loading branch information
griesemer committed May 23, 2012
1 parent eacc3cc commit 7482822
Showing 1 changed file with 7 additions and 13 deletions.
20 changes: 7 additions & 13 deletions src/pkg/go/parser/parser.go
Expand Up @@ -56,7 +56,7 @@ type parser struct {
unresolved []*ast.Ident // unresolved identifiers
imports []*ast.ImportSpec // list of imports

// Label scope
// Label scopes
// (maintained by open/close LabelScope)
labelScope *ast.Scope // label scope for current function
targetStack [][]*ast.Ident // stack of unresolved labels
Expand All @@ -75,14 +75,6 @@ func (p *parser) init(fset *token.FileSet, filename string, src []byte, mode Mod
p.trace = mode&Trace != 0 // for convenience (p.trace is used frequently)

p.next()

// set up the pkgScope here (as opposed to in parseFile) because
// there are other parser entry points (ParseExpr, etc.)
p.openScope()
p.pkgScope = p.topScope

// for the same reason, set up a label scope
p.openLabelScope()
}

// ----------------------------------------------------------------------------
Expand Down Expand Up @@ -2297,11 +2289,12 @@ func (p *parser) parseFile() *ast.File {
}
p.expectSemi()

var decls []ast.Decl

// Don't bother parsing the rest if we had errors already.
// Likely not a Go source file at all.

p.openScope()
p.pkgScope = p.topScope
var decls []ast.Decl
if p.errors.Len() == 0 && p.mode&PackageClauseOnly == 0 {
// import decls
for p.tok == token.IMPORT {
Expand All @@ -2315,8 +2308,9 @@ func (p *parser) parseFile() *ast.File {
}
}
}

assert(p.topScope == p.pkgScope, "imbalanced scopes")
p.closeScope()
assert(p.topScope == nil, "unbalanced scopes")
assert(p.labelScope == nil, "unbalanced label scopes")

// resolve global identifiers within the same file
i := 0
Expand Down

0 comments on commit 7482822

Please sign in to comment.