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, os.Error) {
27 switch s := src.(type) {
33 // is io.Reader, but src is already available in []byte form
39 _, err := io.Copy(&buf, s)
43 return buf.Bytes(), nil
45 return nil, os.ErrorString("invalid source")
49 return ioutil.ReadFile(filename)
53 func (p *parser) parseEOF() os.Error {
55 return p.GetError(scanner.Sorted)
59 // ParseExpr parses a Go expression and returns the corresponding
60 // AST node. The fset, filename, and src arguments have the same interpretation
61 // as for ParseFile. If there is an error, the result expression
62 // may be nil or contain a partial AST.
64 func ParseExpr(fset *token.FileSet, filename string, src interface{}) (ast.Expr, os.Error) {
65 data, err := readSource(filename, src)
71 p.init(fset, filename, data, 0)
73 if p.tok == token.SEMICOLON {
74 p.next() // consume automatically inserted semicolon, if any
76 return x, p.parseEOF()
80 // ParseStmtList parses a list of Go statements and returns the list
81 // of corresponding AST nodes. The fset, filename, and src arguments have the same
82 // interpretation as for ParseFile. If there is an error, the node
83 // list may be nil or contain partial ASTs.
85 func ParseStmtList(fset *token.FileSet, filename string, src interface{}) ([]ast.Stmt, os.Error) {
86 data, err := readSource(filename, src)
92 p.init(fset, filename, data, 0)
93 return p.parseStmtList(), p.parseEOF()
97 // ParseDeclList parses a list of Go declarations and returns the list
98 // of corresponding AST nodes. The fset, filename, and src arguments have the same
99 // interpretation as for ParseFile. If there is an error, the node
100 // list may be nil or contain partial ASTs.
102 func ParseDeclList(fset *token.FileSet, filename string, src interface{}) ([]ast.Decl, os.Error) {
103 data, err := readSource(filename, src)
109 p.init(fset, filename, data, 0)
110 return p.parseDeclList(), p.parseEOF()
114 // ParseFile parses the source code of a single Go source file and returns
115 // the corresponding ast.File node. The source code may be provided via
116 // the filename of the source file, or via the src parameter.
118 // If src != nil, ParseFile parses the source from src and the filename is
119 // only used when recording position information. The type of the argument
120 // for the src parameter must be string, []byte, or io.Reader.
122 // If src == nil, ParseFile parses the file specified by filename.
124 // The mode parameter controls the amount of source text parsed and other
125 // optional parser functionality. Position information is recorded in the
128 // If the source couldn't be read, the returned AST is nil and the error
129 // indicates the specific failure. If the source was read but syntax
130 // errors were found, the result is a partial AST (with ast.BadX nodes
131 // representing the fragments of erroneous source code). Multiple errors
132 // are returned via a scanner.ErrorList which is sorted by file position.
134 func ParseFile(fset *token.FileSet, filename string, src interface{}, mode uint) (*ast.File, os.Error) {
135 data, err := readSource(filename, src)
141 p.init(fset, filename, data, mode)
142 return p.parseFile(), p.GetError(scanner.NoMultiples) // parseFile() reads to EOF
146 // ParseFiles calls ParseFile for each file in the filenames list and returns
147 // a map of package name -> package AST with all the packages found. The mode
148 // bits are passed to ParseFile unchanged. Position information is recorded
149 // in the file set fset.
151 // Files with parse errors are ignored. In this case the map of packages may
152 // be incomplete (missing packages and/or incomplete packages) and the first
153 // error encountered is returned.
155 func ParseFiles(fset *token.FileSet, filenames []string, mode uint) (pkgs map[string]*ast.Package, first os.Error) {
156 pkgs = make(map[string]*ast.Package)
157 for _, filename := range filenames {
158 if src, err := ParseFile(fset, filename, nil, mode); err == nil {
159 name := src.Name.Name
160 pkg, found := pkgs[name]
162 // TODO(gri) Use NewPackage here; reconsider ParseFiles API.
163 pkg = &ast.Package{name, nil, nil, make(map[string]*ast.File)}
166 pkg.Files[filename] = src
167 } else if first == nil {
175 // ParseDir calls ParseFile for the files in the directory specified by path and
176 // returns a map of package name -> package AST with all the packages found. If
177 // filter != nil, only the files with os.FileInfo entries passing through the filter
178 // are considered. The mode bits are passed to ParseFile unchanged. Position
179 // information is recorded in the file set fset.
181 // If the directory couldn't be read, a nil map and the respective error are
182 // returned. If a parse error occurred, a non-nil but incomplete map and the
183 // error are returned.
185 func ParseDir(fset *token.FileSet, path string, filter func(*os.FileInfo) bool, mode uint) (map[string]*ast.Package, os.Error) {
186 fd, err := os.Open(path, os.O_RDONLY, 0)
192 list, err := fd.Readdir(-1)
197 filenames := make([]string, len(list))
199 for i := 0; i < len(list); i++ {
201 if filter == nil || filter(d) {
202 filenames[n] = filepath.Join(path, d.Name)
206 filenames = filenames[0:n]
208 return ParseFiles(fset, filenames, mode)