@@ -75,7 +75,7 @@ const (
75
75
// indicates the specific failure. If the source was read but syntax
76
76
// errors were found, the result is a partial AST (with ast.Bad* nodes
77
77
// representing the fragments of erroneous source code). Multiple errors
78
- // are returned via a scanner.ErrorList which is sorted by file position.
78
+ // are returned via a scanner.ErrorList which is sorted by source position.
79
79
//
80
80
func ParseFile (fset * token.FileSet , filename string , src interface {}, mode Mode ) (f * ast.File , err error ) {
81
81
if fset == nil {
@@ -173,6 +173,12 @@ func ParseDir(fset *token.FileSet, path string, filter func(os.FileInfo) bool, m
173
173
// be a valid Go (type or value) expression. Specifically, fset must not
174
174
// be nil.
175
175
//
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.
181
+ //
176
182
func ParseExprFrom (fset * token.FileSet , filename string , src interface {}, mode Mode ) (expr ast.Expr , err error ) {
177
183
if fset == nil {
178
184
panic ("parser.ParseExprFrom: no token.FileSet provided (fset == nil)" )
@@ -204,7 +210,7 @@ func ParseExprFrom(fset *token.FileSet, filename string, src interface{}, mode M
204
210
// in case of an erroneous x.
205
211
p .openScope ()
206
212
p .pkgScope = p .topScope
207
- e : = p .parseRhsOrType ()
213
+ expr = p .parseRhsOrType ()
208
214
p .closeScope ()
209
215
assert (p .topScope == nil , "unbalanced scopes" )
210
216
@@ -215,18 +221,17 @@ func ParseExprFrom(fset *token.FileSet, filename string, src interface{}, mode M
215
221
}
216
222
p .expect (token .EOF )
217
223
218
- if p .errors .Len () > 0 {
219
- p .errors .Sort ()
220
- return nil , p .errors .Err ()
221
- }
222
-
223
- return e , nil
224
+ return
224
225
}
225
226
226
227
// ParseExpr is a convenience function for obtaining the AST of an expression x.
227
228
// The position information recorded in the AST is undefined. The filename used
228
229
// in error messages is the empty string.
229
230
//
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.
234
+ //
230
235
func ParseExpr (x string ) (ast.Expr , error ) {
231
236
return ParseExprFrom (token .NewFileSet (), "" , []byte (x ), 0 )
232
237
}
0 commit comments