GoPLS Viewer

Home|gopls/godoc/parser.go
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.
4
5// This file contains support functions for parsing .go files
6// accessed via godoc's file system fs.
7
8package godoc
9
10import (
11    "bytes"
12    "go/ast"
13    "go/parser"
14    "go/token"
15    pathpkg "path"
16
17    "golang.org/x/tools/godoc/vfs"
18)
19
20var linePrefix = []byte("//line ")
21
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).
27func replaceLinePrefixCommentsWithBlankLine(src []byte) {
28    for {
29        i := bytes.Index(srclinePrefix)
30        if i < 0 {
31            break // we're done
32        }
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' {
37                src[i] = ' '
38                i++
39            }
40        } else {
41            // not at beginning of line: skip over prefix
42            i += len(linePrefix)
43        }
44        // i <= len(src)
45        src = src[i:]
46    }
47}
48
49func (c *CorpusparseFile(fset *token.FileSetfilename stringmode parser.Mode) (*ast.Fileerror) {
50    srcerr := vfs.ReadFile(c.fsfilename)
51    if err != nil {
52        return nilerr
53    }
54
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)
58
59    return parser.ParseFile(fsetfilenamesrcmode)
60}
61
62func (c *CorpusparseFiles(fset *token.FileSetrelpath stringabspath stringlocalnames []string) (map[string]*ast.Fileerror) {
63    files := make(map[string]*ast.File)
64    for _f := range localnames {
65        absname := pathpkg.Join(abspathf)
66        fileerr := c.parseFile(fsetabsnameparser.ParseComments)
67        if err != nil {
68            return nilerr
69        }
70        files[pathpkg.Join(relpathf)] = file
71    }
72
73    return filesnil
74}
75
MembersX
replaceLinePrefixCommentsWithBlankLine.BlockStmt.i
Corpus.parseFile.mode
Corpus.parseFile.src
Corpus.parseFiles
Corpus.parseFiles.abspath
Corpus.parseFiles.RangeStmt_1824.BlockStmt.err
Corpus.parseFile.c
Corpus.parseFile
Corpus.parseFiles.c
Corpus.parseFiles.fset
Corpus.parseFiles.relpath
Corpus.parseFiles.files
Corpus.parseFiles.localnames
Corpus.parseFiles.RangeStmt_1824.BlockStmt.absname
Corpus.parseFiles.RangeStmt_1824.BlockStmt.file
replaceLinePrefixCommentsWithBlankLine
replaceLinePrefixCommentsWithBlankLine.src
Corpus.parseFile.fset
Corpus.parseFile.filename
Corpus.parseFile.err
Corpus.parseFiles.RangeStmt_1824.f
Members
X