2013-07-16 22:02:35 -06:00
|
|
|
// Copyright 2011 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
// This file contains support functions for parsing .go files
|
|
|
|
// accessed via godoc's file system fs.
|
|
|
|
|
2013-07-17 01:09:54 -06:00
|
|
|
package godoc
|
2013-07-16 22:02:35 -06:00
|
|
|
|
|
|
|
import (
|
|
|
|
"go/ast"
|
|
|
|
"go/parser"
|
|
|
|
"go/token"
|
|
|
|
pathpkg "path"
|
2013-07-17 01:09:54 -06:00
|
|
|
|
|
|
|
"code.google.com/p/go.tools/godoc/vfs"
|
2013-07-16 22:02:35 -06:00
|
|
|
)
|
|
|
|
|
2013-07-17 21:14:09 -06:00
|
|
|
func (c *Corpus) parseFile(fset *token.FileSet, filename string, mode parser.Mode) (*ast.File, error) {
|
|
|
|
src, err := vfs.ReadFile(c.fs, filename)
|
2013-07-16 22:02:35 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return parser.ParseFile(fset, filename, src, mode)
|
|
|
|
}
|
|
|
|
|
2014-01-30 04:28:19 -07:00
|
|
|
func (c *Corpus) parseFiles(fset *token.FileSet, relpath string, abspath string, localnames []string) (map[string]*ast.File, error) {
|
2013-07-16 22:02:35 -06:00
|
|
|
files := make(map[string]*ast.File)
|
|
|
|
for _, f := range localnames {
|
|
|
|
absname := pathpkg.Join(abspath, f)
|
2013-07-17 21:14:09 -06:00
|
|
|
file, err := c.parseFile(fset, absname, parser.ParseComments)
|
2013-07-16 22:02:35 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-01-30 04:28:19 -07:00
|
|
|
files[pathpkg.Join(relpath, f)] = file
|
2013-07-16 22:02:35 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return files, nil
|
|
|
|
}
|