|
| 1 | +// Copyright 2009 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 the exported entry points for invoking the parser. |
| 6 | + |
| 7 | +package parser |
| 8 | + |
| 9 | +import ( |
| 10 | + "bytes" |
| 11 | + "errors" |
| 12 | + "io" |
| 13 | + "io/ioutil" |
| 14 | + "os" |
| 15 | + "path/filepath" |
| 16 | + |
| 17 | + "go/ast" |
| 18 | + |
| 19 | + "go/token" |
| 20 | +) |
| 21 | + |
| 22 | +// ImportPathToName is the type of the function that's used |
| 23 | +// to find the package name for an imported package path. |
| 24 | +// The fromDir argument holds the directory that contains the |
| 25 | +// import statement, which may be empty. |
| 26 | +type ImportPathToName func(path string, fromDir string) (string, error) |
| 27 | + |
| 28 | +// If src != nil, readSource converts src to a []byte if possible; |
| 29 | +// otherwise it returns an error. If src == nil, readSource returns |
| 30 | +// the result of reading the file specified by filename. |
| 31 | +// |
| 32 | +func readSource(filename string, src interface{}) ([]byte, error) { |
| 33 | + if src != nil { |
| 34 | + switch s := src.(type) { |
| 35 | + case string: |
| 36 | + return []byte(s), nil |
| 37 | + case []byte: |
| 38 | + return s, nil |
| 39 | + case *bytes.Buffer: |
| 40 | + // is io.Reader, but src is already available in []byte form |
| 41 | + if s != nil { |
| 42 | + return s.Bytes(), nil |
| 43 | + } |
| 44 | + case io.Reader: |
| 45 | + var buf bytes.Buffer |
| 46 | + _, err := io.Copy(&buf, s) |
| 47 | + if err != nil { |
| 48 | + return nil, err |
| 49 | + } |
| 50 | + return buf.Bytes(), nil |
| 51 | + default: |
| 52 | + return nil, errors.New("invalid source") |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + return ioutil.ReadFile(filename) |
| 57 | +} |
| 58 | + |
| 59 | +func (p *parser) parseEOF() error { |
| 60 | + p.expect(token.EOF) |
| 61 | + p.ErrorList.Sort() |
| 62 | + return p.ErrorList.Err() |
| 63 | +} |
| 64 | + |
| 65 | +// ParseExpr parses a Go expression and returns the corresponding |
| 66 | +// AST node. The fset, filename, and src arguments have the same interpretation |
| 67 | +// as for ParseFile. If there is an error, the result expression |
| 68 | +// may be nil or contain a partial AST. |
| 69 | +// |
| 70 | +// if scope is non-nil, it will be used as the scope for the expression. |
| 71 | +// |
| 72 | +func ParseExpr(fset *token.FileSet, filename string, src interface{}, scope *ast.Scope, pathToName ImportPathToName) (ast.Expr, error) { |
| 73 | + data, err := readSource(filename, src) |
| 74 | + if err != nil { |
| 75 | + return nil, err |
| 76 | + } |
| 77 | + |
| 78 | + var p parser |
| 79 | + p.init(fset, filename, data, 0, scope, pathToName) |
| 80 | + x := p.parseExpr() |
| 81 | + if p.tok == token.SEMICOLON { |
| 82 | + p.next() // consume automatically inserted semicolon, if any |
| 83 | + } |
| 84 | + return x, p.parseEOF() |
| 85 | +} |
| 86 | + |
| 87 | +// ParseFile parses the source code of a single Go source file and returns |
| 88 | +// the corresponding ast.File node. The source code may be provided via |
| 89 | +// the filename of the source file, or via the src parameter. |
| 90 | +// |
| 91 | +// If src != nil, ParseFile parses the source from src and the filename is |
| 92 | +// only used when recording position information. The type of the argument |
| 93 | +// for the src parameter must be string, []byte, or io.Reader. |
| 94 | +// |
| 95 | +// If src == nil, ParseFile parses the file specified by filename. |
| 96 | +// |
| 97 | +// The mode parameter controls the amount of source text parsed and other |
| 98 | +// optional parser functionality. Position information is recorded in the |
| 99 | +// file set fset. |
| 100 | +// |
| 101 | +// If the source couldn't be read, the returned AST is nil and the error |
| 102 | +// indicates the specific failure. If the source was read but syntax |
| 103 | +// errors were found, the result is a partial AST (with ast.BadX nodes |
| 104 | +// representing the fragments of erroneous source code). Multiple errors |
| 105 | +// are returned via a scanner.ErrorList which is sorted by file position. |
| 106 | +// |
| 107 | +func ParseFile(fset *token.FileSet, filename string, src interface{}, mode uint, pkgScope *ast.Scope, pathToName ImportPathToName) (*ast.File, error) { |
| 108 | + data, err := readSource(filename, src) |
| 109 | + if err != nil { |
| 110 | + return nil, err |
| 111 | + } |
| 112 | + |
| 113 | + var p parser |
| 114 | + p.init(fset, filename, data, mode, pkgScope, pathToName) |
| 115 | + p.pkgScope = p.topScope |
| 116 | + p.openScope() |
| 117 | + p.fileScope = p.topScope |
| 118 | + p.ErrorList.RemoveMultiples() |
| 119 | + return p.parseFile(), p.ErrorList.Err() // parseFile() reads to EOF |
| 120 | +} |
| 121 | + |
| 122 | +func parseFileInPkg(fset *token.FileSet, pkgs map[string]*ast.Package, filename string, mode uint, pathToName ImportPathToName) (err error) { |
| 123 | + data, err := readSource(filename, nil) |
| 124 | + if err != nil { |
| 125 | + return err |
| 126 | + } |
| 127 | + // first find package name, so we can use the correct package |
| 128 | + // scope when parsing the file. |
| 129 | + src, err := ParseFile(fset, filename, data, PackageClauseOnly, nil, pathToName) |
| 130 | + if err != nil { |
| 131 | + return |
| 132 | + } |
| 133 | + name := src.Name.Name |
| 134 | + pkg := pkgs[name] |
| 135 | + if pkg == nil { |
| 136 | + pkg = &ast.Package{name, ast.NewScope(Universe), nil, make(map[string]*ast.File)} |
| 137 | + pkgs[name] = pkg |
| 138 | + } |
| 139 | + src, err = ParseFile(fset, filename, data, mode, pkg.Scope, pathToName) |
| 140 | + if err != nil { |
| 141 | + return |
| 142 | + } |
| 143 | + pkg.Files[filename] = src |
| 144 | + return |
| 145 | +} |
| 146 | + |
| 147 | +// ParseFiles calls ParseFile for each file in the filenames list and returns |
| 148 | +// a map of package name -> package AST with all the packages found. The mode |
| 149 | +// bits are passed to ParseFile unchanged. Position information is recorded |
| 150 | +// in the file set fset. |
| 151 | +// |
| 152 | +// Files with parse errors are ignored. In this case the map of packages may |
| 153 | +// be incomplete (missing packages and/or incomplete packages) and the first |
| 154 | +// error encountered is returned. |
| 155 | +// |
| 156 | +func ParseFiles(fset *token.FileSet, filenames []string, mode uint, pathToName ImportPathToName) (pkgs map[string]*ast.Package, first error) { |
| 157 | + pkgs = make(map[string]*ast.Package) |
| 158 | + for _, filename := range filenames { |
| 159 | + if err := parseFileInPkg(fset, pkgs, filename, mode, pathToName); err != nil && first == nil { |
| 160 | + first = err |
| 161 | + } |
| 162 | + } |
| 163 | + return |
| 164 | +} |
| 165 | + |
| 166 | +// ParseDir calls ParseFile for the files in the directory specified by path and |
| 167 | +// returns a map of package name -> package AST with all the packages found. If |
| 168 | +// filter != nil, only the files with os.FileInfo entries passing through the filter |
| 169 | +// are considered. The mode bits are passed to ParseFile unchanged. Position |
| 170 | +// information is recorded in the file set fset. |
| 171 | +// |
| 172 | +// If the directory couldn't be read, a nil map and the respective error are |
| 173 | +// returned. If a parse error occurred, a non-nil but incomplete map and the |
| 174 | +// error are returned. |
| 175 | +// |
| 176 | +func ParseDir(fset *token.FileSet, path string, filter func(os.FileInfo) bool, mode uint, pathToName ImportPathToName) (map[string]*ast.Package, error) { |
| 177 | + fd, err := os.Open(path) |
| 178 | + if err != nil { |
| 179 | + return nil, err |
| 180 | + } |
| 181 | + defer fd.Close() |
| 182 | + |
| 183 | + list, err := fd.Readdir(-1) |
| 184 | + if err != nil { |
| 185 | + return nil, err |
| 186 | + } |
| 187 | + |
| 188 | + filenames := make([]string, len(list)) |
| 189 | + n := 0 |
| 190 | + for i := 0; i < len(list); i++ { |
| 191 | + d := list[i] |
| 192 | + if filter == nil || filter(d) { |
| 193 | + filenames[n] = filepath.Join(path, d.Name()) |
| 194 | + n++ |
| 195 | + } |
| 196 | + } |
| 197 | + filenames = filenames[0:n] |
| 198 | + |
| 199 | + return ParseFiles(fset, filenames, mode, pathToName) |
| 200 | +} |
0 commit comments