OSDN Git Service

Update to current Go library.
[pf3gnuchains/gcc-fork.git] / libgo / go / go / parser / interface.go
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         "go/ast"
12         "go/scanner"
13         "go/token"
14         "io"
15         "io/ioutil"
16         "os"
17         "path/filepath"
18 )
19
20
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.
24 //
25 func readSource(filename string, src interface{}) ([]byte, os.Error) {
26         if src != nil {
27                 switch s := src.(type) {
28                 case string:
29                         return []byte(s), nil
30                 case []byte:
31                         return s, nil
32                 case *bytes.Buffer:
33                         // is io.Reader, but src is already available in []byte form
34                         if s != nil {
35                                 return s.Bytes(), nil
36                         }
37                 case io.Reader:
38                         var buf bytes.Buffer
39                         _, err := io.Copy(&buf, s)
40                         if err != nil {
41                                 return nil, err
42                         }
43                         return buf.Bytes(), nil
44                 default:
45                         return nil, os.ErrorString("invalid source")
46                 }
47         }
48
49         return ioutil.ReadFile(filename)
50 }
51
52
53 func (p *parser) parseEOF() os.Error {
54         p.expect(token.EOF)
55         return p.GetError(scanner.Sorted)
56 }
57
58
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.
63 //
64 func ParseExpr(fset *token.FileSet, filename string, src interface{}) (ast.Expr, os.Error) {
65         data, err := readSource(filename, src)
66         if err != nil {
67                 return nil, err
68         }
69
70         var p parser
71         p.init(fset, filename, data, 0)
72         x := p.parseRhs()
73         if p.tok == token.SEMICOLON {
74                 p.next() // consume automatically inserted semicolon, if any
75         }
76         return x, p.parseEOF()
77 }
78
79
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.
84 //
85 func ParseStmtList(fset *token.FileSet, filename string, src interface{}) ([]ast.Stmt, os.Error) {
86         data, err := readSource(filename, src)
87         if err != nil {
88                 return nil, err
89         }
90
91         var p parser
92         p.init(fset, filename, data, 0)
93         return p.parseStmtList(), p.parseEOF()
94 }
95
96
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.
101 //
102 func ParseDeclList(fset *token.FileSet, filename string, src interface{}) ([]ast.Decl, os.Error) {
103         data, err := readSource(filename, src)
104         if err != nil {
105                 return nil, err
106         }
107
108         var p parser
109         p.init(fset, filename, data, 0)
110         return p.parseDeclList(), p.parseEOF()
111 }
112
113
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.
117 //
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.
121 //
122 // If src == nil, ParseFile parses the file specified by filename.
123 //
124 // The mode parameter controls the amount of source text parsed and other
125 // optional parser functionality. Position information is recorded in the
126 // file set fset.
127 //
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.
133 //
134 func ParseFile(fset *token.FileSet, filename string, src interface{}, mode uint) (*ast.File, os.Error) {
135         data, err := readSource(filename, src)
136         if err != nil {
137                 return nil, err
138         }
139
140         var p parser
141         p.init(fset, filename, data, mode)
142         return p.parseFile(), p.GetError(scanner.NoMultiples) // parseFile() reads to EOF
143 }
144
145
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.
150 //
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.
154 //
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]
161                         if !found {
162                                 // TODO(gri) Use NewPackage here; reconsider ParseFiles API.
163                                 pkg = &ast.Package{name, nil, nil, make(map[string]*ast.File)}
164                                 pkgs[name] = pkg
165                         }
166                         pkg.Files[filename] = src
167                 } else if first == nil {
168                         first = err
169                 }
170         }
171         return
172 }
173
174
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.
180 //
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.
184 //
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)
187         if err != nil {
188                 return nil, err
189         }
190         defer fd.Close()
191
192         list, err := fd.Readdir(-1)
193         if err != nil {
194                 return nil, err
195         }
196
197         filenames := make([]string, len(list))
198         n := 0
199         for i := 0; i < len(list); i++ {
200                 d := &list[i]
201                 if filter == nil || filter(d) {
202                         filenames[n] = filepath.Join(path, d.Name)
203                         n++
204                 }
205         }
206         filenames = filenames[0:n]
207
208         return ParseFiles(fset, filenames, mode)
209 }