1 // Copyright 2011 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // This file contains support functions for parsing .go files
6 // accessed via godoc's file system fs.
17 "golang.org/x/tools/godoc/vfs"
20 var linePrefix = []byte("//line ")
22 // This function replaces source lines starting with "//line " with a blank line.
23 // It does this irrespective of whether the line is truly a line comment or not;
24 // e.g., the line may be inside a string, or a /*-style comment; however that is
25 // rather unlikely (proper testing would require a full Go scan which we want to
26 // avoid for performance).
27 func replaceLinePrefixCommentsWithBlankLine(src []byte) {
29 i := bytes.Index(src, linePrefix)
33 // 0 <= i && i+len(linePrefix) <= len(src)
34 if i == 0 || src[i-1] == '\n' {
35 // at beginning of line: blank out line
36 for i < len(src) && src[i] != '\n' {
41 // not at beginning of line: skip over prefix
49 func (c *Corpus) parseFile(fset *token.FileSet, filename string, mode parser.Mode) (*ast.File, error) {
50 src, err := vfs.ReadFile(c.fs, filename)
55 // Temporary ad-hoc fix for issue 5247.
56 // TODO(gri,dmitshur) Remove this in favor of a better fix, eventually (see issue 32092).
57 replaceLinePrefixCommentsWithBlankLine(src)
59 return parser.ParseFile(fset, filename, src, mode)
62 func (c *Corpus) parseFiles(fset *token.FileSet, relpath string, abspath string, localnames []string) (map[string]*ast.File, error) {
63 files := make(map[string]*ast.File)
64 for _, f := range localnames {
65 absname := pathpkg.Join(abspath, f)
66 file, err := c.parseFile(fset, absname, parser.ParseComments)
70 files[pathpkg.Join(relpath, f)] = file