Commit 312bd7a1 authored by Robert Griesemer's avatar Robert Griesemer

parser:

- Changed filter function for parser.ParsePackage to
  take an *os.Dir instead of a filename for more
  powerful filters

- Removed TODO in ast.PackageInterface: Now collect
  package comments from all package files

- Cleanups in godoc: Use the new ParsePackage and
  PackageInterface functions; as a result computing
  package information is much simpler now.

R=rsc
DELTA=285  (80 added, 110 deleted, 95 changed)
OCL=32473
CL=32486
parent f9057c7b
...@@ -111,14 +111,12 @@ func init() { ...@@ -111,14 +111,12 @@ func init() {
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// Support // Support
func isDir(name string) bool {
d, err := os.Stat(name);
return err == nil && d.IsDirectory();
}
func isGoFile(dir *os.Dir) bool { func isGoFile(dir *os.Dir) bool {
return dir.IsRegular() && pathutil.Ext(dir.Name) == ".go"; return
dir.IsRegular() &&
!strings.HasPrefix(dir.Name, ".") && // ignore .files
pathutil.Ext(dir.Name) == ".go" &&
!strings.HasSuffix(dir.Name, "_test.go"); // ignore test files
} }
...@@ -374,14 +372,6 @@ func serveFile(c *http.Conn, req *http.Request) { ...@@ -374,14 +372,6 @@ func serveFile(c *http.Conn, req *http.Request) {
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// Packages // Packages
type pakDesc struct {
dirname string; // relative to goroot
pakname string; // same as last component of importpath
importpath string; // import "___"
filenames map[string] bool; // set of file (names) belonging to this package
}
// TODO if we don't plan to use the directory information, simplify to []string // TODO if we don't plan to use the directory information, simplify to []string
type dirList []*os.Dir type dirList []*os.Dir
...@@ -390,146 +380,97 @@ func (d dirList) Less(i, j int) bool { return d[i].Name < d[j].Name } ...@@ -390,146 +380,97 @@ func (d dirList) Less(i, j int) bool { return d[i].Name < d[j].Name }
func (d dirList) Swap(i, j int) { d[i], d[j] = d[j], d[i] } func (d dirList) Swap(i, j int) { d[i], d[j] = d[j], d[i] }
func isPackageFile(dirname, filename, pakname string) bool { func pkgName(filename string) string {
// ignore test files file, err := parse(filename, parser.PackageClauseOnly);
if strings.HasSuffix(filename, "_test.go") { if err != nil || file == nil {
return false; return "";
} }
return file.Name.Value;
// determine package name
prog, errors := parse(dirname + "/" + filename, parser.PackageClauseOnly);
if prog == nil {
return false;
}
return prog != nil && prog.Name.Value == pakname;
} }
// Returns the canonical URL path, the package denoted by path, and type PageInfo struct {
// the list of sub-directories in the corresponding package directory. PDoc *doc.PackageDoc; // nil if no package found
// If there is no such package, the package descriptor pd is nil. Dirs dirList; // nil if no subdirectories found
// If there are no sub-directories, the dirs list is nil. }
func findPackage(path string) (canonical string, pd *pakDesc, dirs dirList) {
canonical = pathutil.Clean(Pkg + path) + "/";
// get directory contents, if possible
importpath := pathutil.Clean(path); // no trailing '/'
dirname := pathutil.Join(*pkgroot, importpath);
if !isDir(dirname) {
return;
}
fd, err1 := os.Open(dirname, os.O_RDONLY, 0);
if err1 != nil {
log.Stderrf("open %s: %v", dirname, err1);
return;
}
list, err2 := fd.Readdir(-1); // getPageInfo returns the PageInfo for a given package directory.
if err2 != nil { // If there is no corresponding package in the directory,
log.Stderrf("readdir %s: %v", dirname, err2); // PageInfo.PDoc is nil. If there are no subdirectories,
return; // PageInfo.Dirs is nil.
} //
func getPageInfo(path string) PageInfo {
// the path is relative to *pkgroot
dirname := pathutil.Join(*pkgroot, path);
// the package name is the directory name within its parent // the package name is the directory name within its parent
_, pakname := pathutil.Split(dirname); _, pkgname := pathutil.Split(dirname);
// collect all files belonging to the package and count the // filter function to select the desired .go files and
// number of sub-directories // collect subdirectories
filenames := make(map[string]bool); var subdirlist vector.Vector;
nsub := 0; subdirlist.Init(0);
for i, entry := range list { filter := func(d *os.Dir) bool {
switch { if isGoFile(d) {
case isGoFile(&entry) && isPackageFile(dirname, entry.Name, pakname): // Some directories contain main packages: Only accept
// add file to package desc // files that belong to the expected package so that
if tmp, found := filenames[entry.Name]; found { // parser.ParsePackage doesn't return "multiple packages
panic("internal error: same file added more than once: " + entry.Name); // found" errors.
} return pkgName(dirname + "/" + d.Name) == pkgname;
filenames[entry.Name] = true; }
case isPkgDir(&entry): if isPkgDir(d) {
nsub++; subdirlist.Push(d);
} }
return false;
};
// get package AST
pkg, err := parser.ParsePackage(dirname, filter, parser.ParseComments);
if err != nil {
log.Stderr(err);
} }
// make the list of sub-directories, if any // convert and sort subdirectory list, if any
var subdirs dirList; var subdirs dirList;
if nsub > 0 { if subdirlist.Len() > 0 {
subdirs = make(dirList, nsub); subdirs = make(dirList, subdirlist.Len());
nsub = 0; for i := 0; i < subdirlist.Len(); i++ {
for i, entry := range list { subdirs[i] = subdirlist.At(i).(*os.Dir);
if isPkgDir(&entry) {
// make a copy here so sorting (and other code) doesn't
// have to make one every time an entry is moved
copy := new(os.Dir);
*copy = entry;
subdirs[nsub] = copy;
nsub++;
}
} }
sort.Sort(subdirs); sort.Sort(subdirs);
} }
// if there are no package files, then there is no package // compute package documentation
if len(filenames) == 0 { var pdoc *doc.PackageDoc;
return canonical, nil, subdirs; if pkg != nil {
} // TODO(gri) Simplify DocReader interface: no need anymore to add
// more than one file because of ast.PackageInterface.
return canonical, &pakDesc{dirname, pakname, importpath, filenames}, subdirs;
}
func (p *pakDesc) doc() (*doc.PackageDoc, *parseErrors) {
if p == nil {
return nil, nil;
}
// compute documentation
// TODO(gri) change doc to work on entire ast.Package at once
var r doc.DocReader; var r doc.DocReader;
i := 0; r.Init(pkg.Name, pathutil.Clean(path)); // no trailing '/' in importpath
for filename := range p.filenames { r.AddFile(ast.PackageExports(pkg));
src, err := parse(p.dirname + "/" + filename, parser.ParseComments); pdoc = r.Doc();
if err != nil {
return nil, err;
}
if i == 0 {
// first file - initialize doc
r.Init(src.Name.Value, p.importpath);
}
i++;
ast.FilterExports(src); // we only care about exports
r.AddFile(src);
} }
return r.Doc(), nil; return PageInfo{pdoc, subdirs};
} }
type PageInfo struct {
PDoc *doc.PackageDoc;
Dirs dirList;
}
func servePkg(c *http.Conn, r *http.Request) { func servePkg(c *http.Conn, r *http.Request) {
path := r.Url.Path; path := r.Url.Path;
path = path[len(Pkg) : len(path)]; path = path[len(Pkg) : len(path)];
canonical, desc, dirs := findPackage(path);
if r.Url.Path != canonical { // canonicalize URL path and redirect if necessary
if canonical := pathutil.Clean(Pkg + path) + "/"; r.Url.Path != canonical {
http.Redirect(c, canonical, http.StatusMovedPermanently); http.Redirect(c, canonical, http.StatusMovedPermanently);
return; return;
} }
pdoc, errors := desc.doc(); info := getPageInfo(path);
if errors != nil {
serveParseErrors(c, errors);
return;
}
var buf bytes.Buffer; var buf bytes.Buffer;
if false { // TODO req.Params["format"] == "text" if false { // TODO req.Params["format"] == "text"
err := packageText.Execute(PageInfo{pdoc, dirs}, &buf); err := packageText.Execute(info, &buf);
if err != nil { if err != nil {
log.Stderrf("packageText.Execute: %s", err); log.Stderrf("packageText.Execute: %s", err);
} }
...@@ -537,7 +478,7 @@ func servePkg(c *http.Conn, r *http.Request) { ...@@ -537,7 +478,7 @@ func servePkg(c *http.Conn, r *http.Request) {
return; return;
} }
err := packageHtml.Execute(PageInfo{pdoc, dirs}, &buf); err := packageHtml.Execute(info, &buf);
if err != nil { if err != nil {
log.Stderrf("packageHtml.Execute: %s", err); log.Stderrf("packageHtml.Execute: %s", err);
} }
...@@ -697,20 +638,12 @@ func main() { ...@@ -697,20 +638,12 @@ func main() {
parseerrorText = parseerrorHtml; parseerrorText = parseerrorHtml;
} }
_, desc, dirs := findPackage(flag.Arg(0)); info := getPageInfo(flag.Arg(0));
pdoc, errors := desc.doc();
if errors != nil {
err := parseerrorText.Execute(errors, os.Stderr);
if err != nil {
log.Stderrf("parseerrorText.Execute: %s", err);
}
os.Exit(1);
}
if pdoc != nil && flag.NArg() > 1 { if info.PDoc != nil && flag.NArg() > 1 {
args := flag.Args(); args := flag.Args();
pdoc.Filter(args[1 : len(args)]); info.PDoc.Filter(args[1 : len(args)]);
} }
packageText.Execute(PageInfo{pdoc, dirs}, os.Stdout); packageText.Execute(info, os.Stdout);
} }
...@@ -60,14 +60,14 @@ func parserMode() uint { ...@@ -60,14 +60,14 @@ func parserMode() uint {
} }
func isPkgFile(filename string) bool { func isPkgFile(d *os.Dir) bool {
// ignore non-Go files // ignore non-Go files
if strings.HasPrefix(filename, ".") || !strings.HasSuffix(filename, ".go") { if !d.IsRegular() || strings.HasPrefix(d.Name, ".") || !strings.HasSuffix(d.Name, ".go") {
return false; return false;
} }
// ignore test files unless explicitly included // ignore test files unless explicitly included
return *allgo || !strings.HasSuffix(filename, "_test.go"); return *allgo || !strings.HasSuffix(d.Name, "_test.go");
} }
...@@ -146,7 +146,7 @@ func main() { ...@@ -146,7 +146,7 @@ func main() {
if !*silent { if !*silent {
w := makeTabwriter(os.Stdout); w := makeTabwriter(os.Stdout);
if *exports { if *exports {
src := ast.PackageInterface(pkg); src := ast.PackageExports(pkg);
printer.Fprint(w, src, printerMode()); // ignore errors printer.Fprint(w, src, printerMode()); // ignore errors
} else { } else {
for _, src := range pkg.Files { for _, src := range pkg.Files {
......
...@@ -190,40 +190,71 @@ func FilterExports(src *File) bool { ...@@ -190,40 +190,71 @@ func FilterExports(src *File) bool {
} }
// PackageInterface returns an AST containing only the exported declarations // separator is an empty //-style comment that is interspersed between
// of the package pkg. The pkg AST is modified by PackageInterface. // different comment groups when they are concatenated into a single group
// //
func PackageInterface(pkg *Package) *File { var separator = &Comment{noPos, []byte{'/', '/'}};
// filter each package file
for filename, s := range pkg.Files {
if !FilterExports(s) { // PackageExports returns an AST containing only the exported declarations
pkg.Files[filename] = nil, false; // of the package pkg. PackageExports modifies the pkg AST.
//
func PackageExports(pkg *Package) *File {
// Collect all source files with exported declarations and count
// the number of package comments and declarations in all files.
files := make([]*File, len(pkg.Files));
ncomments := 0;
ndecls := 0;
i := 0;
for _, f := range pkg.Files {
if f.Doc != nil {
ncomments += len(f.Doc.List) + 1; // +1 for separator
}
if FilterExports(f) {
ndecls += len(f.Decls);
files[i] = f;
i++;
} }
} }
files = files[0 : i];
// compute total number of top-level declarations in all source files // Collect package comments from all package files into a single
// CommentGroup - the collected package documentation. The order
// is unspecified. In general there should be only one file with
// a package comment; but it's better to collect extra comments
// than drop them on the floor.
var doc *CommentGroup; var doc *CommentGroup;
n := 0; if ncomments > 0 {
for _, src := range pkg.Files { list := make([]*Comment, ncomments - 1); // -1: no separator before first group
if doc == nil && src.Doc != nil { i := 0;
// TODO(gri) what to do with multiple package comments? for _, f := range pkg.Files {
doc = src.Doc; if f.Doc != nil {
if i > 0 {
// not the first group - add separator
list[i] = separator;
i++;
}
for _, c := range f.Doc.List {
list[i] = c;
i++
}
} }
n += len(src.Decls); }
doc = &CommentGroup{list, nil};
} }
// collect top-level declarations of all source files // Collect exported declarations from all package files.
decls := make([]Decl, n); var decls []Decl;
if ndecls > 0 {
decls = make([]Decl, ndecls);
i := 0; i := 0;
for _, src := range pkg.Files { for _, f := range files {
for _, d := range src.Decls { for _, d := range f.Decls {
decls[i] = d; decls[i] = d;
i++; i++;
} }
} }
}
// TODO(gri) should also collect comments so that this function
// can be used by godoc.
var noPos token.Position;
return &File{doc, noPos, &Ident{noPos, pkg.Name}, decls, nil}; return &File{doc, noPos, &Ident{noPos, pkg.Name}, decls, nil};
} }
...@@ -148,30 +148,31 @@ func ParsePkgFile(pkgname, filename string, mode uint) (*ast.File, os.Error) { ...@@ -148,30 +148,31 @@ func ParsePkgFile(pkgname, filename string, mode uint) (*ast.File, os.Error) {
// ParsePackage parses all files in the directory specified by path and // ParsePackage parses all files in the directory specified by path and
// returns an AST representing the package found. The set of files may be // returns an AST representing the package found. The set of files may be
// restricted by providing a non-nil filter function; only the files with // restricted by providing a non-nil filter function; only the files with
// (path-local) filenames passing through the filter are considered. If // os.Dir entries passing through the filter are considered.
// zero or more then one package is found, an error is returned. Mode // If ParsePackage does not find exactly one package, it returns an error.
// flags that control the amount of source text parsed are ignored. // Mode flags that control the amount of source text parsed are ignored.
// //
func ParsePackage(path string, filter func(string) bool, mode uint) (*ast.Package, os.Error) { func ParsePackage(path string, filter func(*os.Dir) bool, mode uint) (*ast.Package, os.Error) {
fd, err := os.Open(path, os.O_RDONLY, 0); fd, err := os.Open(path, os.O_RDONLY, 0);
if err != nil { if err != nil {
return nil, err; return nil, err;
} }
list, err := fd.Readdirnames(-1); list, err := fd.Readdir(-1);
if err != nil { if err != nil {
return nil, err; return nil, err;
} }
name := ""; name := "";
files := make(map[string]*ast.File); files := make(map[string]*ast.File);
for _, filename := range list { for i := 0; i < len(list); i++ {
if filter == nil || filter(filename) { entry := &list[i];
src, err := ParsePkgFile(name, pathutil.Join(path, filename), mode); if filter == nil || filter(entry) {
src, err := ParsePkgFile(name, pathutil.Join(path, entry.Name), mode);
if err != nil { if err != nil {
return nil, err; return nil, err;
} }
files[filename] = src; files[entry.Name] = src;
if name == "" { if name == "" {
name = src.Name.Value; name = src.Name.Value;
} }
......
...@@ -62,7 +62,7 @@ func TestParse3(t *testing.T) { ...@@ -62,7 +62,7 @@ func TestParse3(t *testing.T) {
} }
func filter(filename string) bool { func nameFilter(filename string) bool {
switch filename { switch filename {
case "parser.go": case "parser.go":
case "interface.go": case "interface.go":
...@@ -74,9 +74,14 @@ func filter(filename string) bool { ...@@ -74,9 +74,14 @@ func filter(filename string) bool {
} }
func dirFilter(d *os.Dir) bool {
return nameFilter(d.Name);
}
func TestParse4(t *testing.T) { func TestParse4(t *testing.T) {
path := "."; path := ".";
pkg, err := ParsePackage(path, filter, 0); pkg, err := ParsePackage(path, dirFilter, 0);
if err != nil { if err != nil {
t.Fatalf("ParsePackage(%s): %v", path, err); t.Fatalf("ParsePackage(%s): %v", path, err);
} }
...@@ -84,7 +89,7 @@ func TestParse4(t *testing.T) { ...@@ -84,7 +89,7 @@ func TestParse4(t *testing.T) {
t.Errorf("incorrect package name: %s", pkg.Name); t.Errorf("incorrect package name: %s", pkg.Name);
} }
for filename, _ := range pkg.Files { for filename, _ := range pkg.Files {
if !filter(filename) { if !nameFilter(filename) {
t.Errorf("unexpected package file: %s", filename); t.Errorf("unexpected package file: %s", filename);
} }
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment