]> Cypherpunks repositories - gostls13.git/commitdiff
remove assumption that all files belonging to a package are in the same directory:
authorRobert Griesemer <gri@golang.org>
Tue, 16 Feb 2010 19:20:25 +0000 (11:20 -0800)
committerRobert Griesemer <gri@golang.org>
Tue, 16 Feb 2010 19:20:25 +0000 (11:20 -0800)
- adjust ast.Package node and doc.PackageDoc correspondingly
- introduce parser.ParseFiles

R=rsc
CC=golang-dev
https://golang.org/cl/207087

src/pkg/exp/parser/interface.go
src/pkg/go/ast/ast.go
src/pkg/go/doc/doc.go
src/pkg/go/parser/interface.go

index 26b08c2d96028c9c5d291363c4dd2516cca8b018..e04ff188871199c4a647c3e5e50ca472621aa37e 100644 (file)
@@ -183,11 +183,12 @@ func ParsePackage(path string, filter func(*os.Dir) bool, mode uint) (*ast.Packa
        for i := 0; i < len(list); i++ {
                entry := &list[i]
                if filter == nil || filter(entry) {
-                       src, err := ParsePkgFile(name, pathutil.Join(path, entry.Name), mode)
+                       filename := pathutil.Join(path, entry.Name)
+                       src, err := ParsePkgFile(name, filename, mode)
                        if err != nil {
                                return nil, err
                        }
-                       files[entry.Name] = src
+                       files[filename] = src
                        if name == "" {
                                name = src.Name.Name()
                        }
@@ -198,5 +199,5 @@ func ParsePackage(path string, filter func(*os.Dir) bool, mode uint) (*ast.Packa
                return nil, os.NewError(path + ": no package found")
        }
 
-       return &ast.Package{name, path, nil, files}, nil
+       return &ast.Package{name, nil, files}, nil
 }
index ed87039a7e59400584d6f618895e60372ceb4c61..95700cb14dcdce1a30d0631d3bbf084bac181d09 100644 (file)
@@ -722,7 +722,6 @@ type File struct {
 //
 type Package struct {
        Name  string           // package name
-       Path  string           // package path
        Scope *Scope           // package scope
-       Files map[string]*File // path-relative filenames
+       Files map[string]*File // Go source files by filename
 }
index d97548715b363ea3909979b708c86f60f2f281aa..9bd1158de72a9d6bdb1b4c4192705ce748410416 100644 (file)
@@ -293,7 +293,7 @@ func NewFileDoc(file *ast.File) *PackageDoc {
        var r docReader
        r.init(file.Name.Name())
        r.addFile(file)
-       return r.newDoc("", "", nil)
+       return r.newDoc("", nil)
 }
 
 
@@ -307,7 +307,7 @@ func NewPackageDoc(pkg *ast.Package, importpath string) *PackageDoc {
                filenames[i] = filename
                i++
        }
-       return r.newDoc(importpath, pkg.Path, filenames)
+       return r.newDoc(importpath, filenames)
 }
 
 
@@ -511,7 +511,6 @@ func makeBugDocs(v *vector.Vector) []string {
 type PackageDoc struct {
        PackageName string
        ImportPath  string
-       FilePath    string
        Filenames   []string
        Doc         string
        Consts      []*ValueDoc
@@ -524,11 +523,10 @@ type PackageDoc struct {
 
 // newDoc returns the accumulated documentation for the package.
 //
-func (doc *docReader) newDoc(importpath, filepath string, filenames []string) *PackageDoc {
+func (doc *docReader) newDoc(importpath string, filenames []string) *PackageDoc {
        p := new(PackageDoc)
        p.PackageName = doc.pkgName
        p.ImportPath = importpath
-       p.FilePath = filepath
        sort.SortStrings(filenames)
        p.Filenames = filenames
        p.Doc = CommentText(doc.doc)
index 1bd63dd49d67c42c25ca233961e40eebc53a28ad..931f03de671cc7bbfe136068ba7ed700a348520d 100644 (file)
@@ -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)
 }