summaryrefslogtreecommitdiff
path: root/src/pkg/go/parser/interface.go
diff options
context:
space:
mode:
authorRobert Griesemer <gri@golang.org>2010-02-16 11:20:25 -0800
committerRobert Griesemer <gri@golang.org>2010-02-16 11:20:25 -0800
commitf96c03e483aca8036176d436669ea99d25386e4a (patch)
treecbda34f8ea42e33056fef6dd843120092cf501fe /src/pkg/go/parser/interface.go
parent4d5e60f1878767d9f8df494365c034f901a694e3 (diff)
downloadgolang-f96c03e483aca8036176d436669ea99d25386e4a.tar.gz
remove assumption that all files belonging to a package are in the same directory:
- adjust ast.Package node and doc.PackageDoc correspondingly - introduce parser.ParseFiles R=rsc CC=golang-dev http://codereview.appspot.com/207087
Diffstat (limited to 'src/pkg/go/parser/interface.go')
-rw-r--r--src/pkg/go/parser/interface.go54
1 files changed, 38 insertions, 16 deletions
diff --git a/src/pkg/go/parser/interface.go b/src/pkg/go/parser/interface.go
index 1bd63dd49..931f03de6 100644
--- a/src/pkg/go/parser/interface.go
+++ b/src/pkg/go/parser/interface.go
@@ -143,6 +143,35 @@ func ParseFile(filename string, src interface{}, scope *ast.Scope, mode uint) (*
}
+// ParseFiles calls ParseFile for each file in the filenames list and returns
+// a map of package name -> package AST with all the packages found. The mode
+// bits are passed to ParseFile unchanged.
+//
+// Files with parse errors are ignored. In this case the map of packages may
+// be incomplete (missing packages and/or incomplete packages) and the last
+// error encountered is returned.
+//
+func ParseFiles(filenames []string, scope *ast.Scope, mode uint) (map[string]*ast.Package, os.Error) {
+ pkgs := make(map[string]*ast.Package)
+ var err os.Error
+ for _, filename := range filenames {
+ var src *ast.File
+ src, err = ParseFile(filename, nil, scope, mode)
+ if err == nil {
+ name := src.Name.Name()
+ pkg, found := pkgs[name]
+ if !found {
+ pkg = &ast.Package{name, scope, make(map[string]*ast.File)}
+ pkgs[name] = pkg
+ }
+ pkg.Files[filename] = src
+ }
+ }
+
+ return pkgs, err
+}
+
+
// ParseDir calls ParseFile for the files in the directory specified by path and
// returns a map of package name -> package AST with all the packages found. If
// filter != nil, only the files with os.Dir entries passing through the filter
@@ -164,24 +193,17 @@ func ParseDir(path string, filter func(*os.Dir) bool, mode uint) (map[string]*as
return nil, err
}
- var scope *ast.Scope = nil // for now tracking of declarations is disabled
- pkgs := make(map[string]*ast.Package)
+ filenames := make([]string, len(list))
+ n := 0
for i := 0; i < len(list); i++ {
- entry := &list[i]
- if filter == nil || filter(entry) {
- src, err := ParseFile(pathutil.Join(path, entry.Name), nil, scope, mode)
- if err != nil {
- return pkgs, err
- }
- name := src.Name.Name()
- pkg, found := pkgs[name]
- if !found {
- pkg = &ast.Package{name, path, scope, make(map[string]*ast.File)}
- pkgs[name] = pkg
- }
- pkg.Files[entry.Name] = src
+ d := &list[i]
+ if filter == nil || filter(d) {
+ filenames[n] = pathutil.Join(path, d.Name)
+ n++
}
}
+ filenames = filenames[0:n]
- return pkgs, nil
+ var scope *ast.Scope = nil // for now tracking of declarations is disabled
+ return ParseFiles(filenames, scope, mode)
}