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.
5 // This file contains the exported entry points for invoking the parser.
21 // If src != nil, readSource converts src to a []byte if possible;
22 // otherwise it returns an error. If src == nil, readSource returns
23 // the result of reading the file specified by filename.
25 func readSource(filename string, src interface{}) ([]byte, error) {
27 switch s := src.(type) {
33 // is io.Reader, but src is already available in []byte form
38 return ioutil.ReadAll(s)
40 return nil, errors.New("invalid source")
42 return ioutil.ReadFile(filename)
45 // A Mode value is a set of flags (or 0).
46 // They control the amount of source code parsed and other optional
47 // parser functionality.
52 PackageClauseOnly Mode = 1 << iota // stop parsing after package clause
53 ImportsOnly // stop parsing after import declarations
54 ParseComments // parse comments and add them to AST
55 Trace // print a trace of parsed productions
56 DeclarationErrors // report declaration errors
57 SpuriousErrors // same as AllErrors, for backward-compatibility
58 AllErrors = SpuriousErrors // report all errors (not just the first 10 on different lines)
61 // ParseFile parses the source code of a single Go source file and returns
62 // the corresponding ast.File node. The source code may be provided via
63 // the filename of the source file, or via the src parameter.
65 // If src != nil, ParseFile parses the source from src and the filename is
66 // only used when recording position information. The type of the argument
67 // for the src parameter must be string, []byte, or io.Reader.
68 // If src == nil, ParseFile parses the file specified by filename.
70 // The mode parameter controls the amount of source text parsed and other
71 // optional parser functionality. Position information is recorded in the
72 // file set fset, which must not be nil.
74 // If the source couldn't be read, the returned AST is nil and the error
75 // indicates the specific failure. If the source was read but syntax
76 // errors were found, the result is a partial AST (with ast.Bad* nodes
77 // representing the fragments of erroneous source code). Multiple errors
78 // are returned via a scanner.ErrorList which is sorted by source position.
80 func ParseFile(fset *token.FileSet, filename string, src interface{}, mode Mode) (f *ast.File, err error) {
82 panic("parser.ParseFile: no token.FileSet provided (fset == nil)")
86 text, err := readSource(filename, src)
93 if e := recover(); e != nil {
94 // resume same panic if it's not a bailout
95 if _, ok := e.(bailout); !ok {
102 // source is not a valid Go source file - satisfy
103 // ParseFile API and return a valid (but) empty
106 Name: new(ast.Ident),
107 Scope: ast.NewScope(nil),
116 p.init(fset, filename, text, mode)
122 // ParseDir calls ParseFile for all files with names ending in ".go" in the
123 // directory specified by path and returns a map of package name -> package
124 // AST with all the packages found.
126 // If filter != nil, only the files with os.FileInfo entries passing through
127 // the filter (and ending in ".go") are considered. The mode bits are passed
128 // to ParseFile unchanged. Position information is recorded in fset, which
131 // If the directory couldn't be read, a nil map and the respective error are
132 // returned. If a parse error occurred, a non-nil but incomplete map and the
133 // first error encountered are returned.
135 func ParseDir(fset *token.FileSet, path string, filter func(os.FileInfo) bool, mode Mode) (pkgs map[string]*ast.Package, first error) {
136 fd, err := os.Open(path)
142 list, err := fd.Readdir(-1)
147 pkgs = make(map[string]*ast.Package)
148 for _, d := range list {
149 if strings.HasSuffix(d.Name(), ".go") && (filter == nil || filter(d)) {
150 filename := filepath.Join(path, d.Name())
151 if src, err := ParseFile(fset, filename, nil, mode); err == nil {
152 name := src.Name.Name
153 pkg, found := pkgs[name]
157 Files: make(map[string]*ast.File),
161 pkg.Files[filename] = src
162 } else if first == nil {
171 // ParseExprFrom is a convenience function for parsing an expression.
172 // The arguments have the same meaning as for ParseFile, but the source must
173 // be a valid Go (type or value) expression. Specifically, fset must not
176 // If the source couldn't be read, the returned AST is nil and the error
177 // indicates the specific failure. If the source was read but syntax
178 // errors were found, the result is a partial AST (with ast.Bad* nodes
179 // representing the fragments of erroneous source code). Multiple errors
180 // are returned via a scanner.ErrorList which is sorted by source position.
182 func ParseExprFrom(fset *token.FileSet, filename string, src interface{}, mode Mode) (expr ast.Expr, err error) {
184 panic("parser.ParseExprFrom: no token.FileSet provided (fset == nil)")
188 text, err := readSource(filename, src)
195 if e := recover(); e != nil {
196 // resume same panic if it's not a bailout
197 if _, ok := e.(bailout); !ok {
206 p.init(fset, filename, text, mode)
207 // Set up pkg-level scopes to avoid nil-pointer errors.
208 // This is not needed for a correct expression x as the
209 // parser will be ok with a nil topScope, but be cautious
210 // in case of an erroneous x.
212 p.pkgScope = p.topScope
213 expr = p.parseRhsOrType()
215 assert(p.topScope == nil, "unbalanced scopes")
217 // If a semicolon was inserted, consume it;
218 // report an error if there's more tokens.
219 if p.tok == token.SEMICOLON && p.lit == "\n" {
227 // ParseExpr is a convenience function for obtaining the AST of an expression x.
228 // The position information recorded in the AST is undefined. The filename used
229 // in error messages is the empty string.
231 // If syntax errors were found, the result is a partial AST (with ast.Bad* nodes
232 // representing the fragments of erroneous source code). Multiple errors are
233 // returned via a scanner.ErrorList which is sorted by source position.
235 func ParseExpr(x string) (ast.Expr, error) {
236 return ParseExprFrom(token.NewFileSet(), "", []byte(x), 0)