Source file src/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 "errors" 12 "go/ast" 13 "go/token" 14 "io" 15 "io/fs" 16 "os" 17 "path/filepath" 18 "strings" 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 func readSource(filename string, src any) ([]byte, error) { 25 if src != nil { 26 switch s := src.(type) { 27 case string: 28 return []byte(s), nil 29 case []byte: 30 return s, nil 31 case *bytes.Buffer: 32 // is io.Reader, but src is already available in []byte form 33 if s != nil { 34 return s.Bytes(), nil 35 } 36 case io.Reader: 37 return io.ReadAll(s) 38 } 39 return nil, errors.New("invalid source") 40 } 41 return os.ReadFile(filename) 42 } 43 44 // A Mode value is a set of flags (or 0). 45 // They control the amount of source code parsed and other optional 46 // parser functionality. 47 type Mode uint 48 49 const ( 50 PackageClauseOnly Mode = 1 << iota // stop parsing after package clause 51 ImportsOnly // stop parsing after import declarations 52 ParseComments // parse comments and add them to AST 53 Trace // print a trace of parsed productions 54 DeclarationErrors // report declaration errors 55 SpuriousErrors // same as AllErrors, for backward-compatibility 56 SkipObjectResolution // skip deprecated identifier resolution; see ParseFile 57 AllErrors = SpuriousErrors // report all errors (not just the first 10 on different lines) 58 ) 59 60 // ParseFile parses the source code of a single Go source file and returns 61 // the corresponding [ast.File] node. The source code may be provided via 62 // the filename of the source file, or via the src parameter. 63 // 64 // If src != nil, ParseFile parses the source from src and the filename is 65 // only used when recording position information. The type of the argument 66 // for the src parameter must be string, []byte, or [io.Reader]. 67 // If src == nil, ParseFile parses the file specified by filename. 68 // 69 // The mode parameter controls the amount of source text parsed and 70 // other optional parser functionality. If the [SkipObjectResolution] 71 // mode bit is set (recommended), the object resolution phase of 72 // parsing will be skipped, causing File.Scope, File.Unresolved, and 73 // all Ident.Obj fields to be nil. Those fields are deprecated; see 74 // [ast.Object] for details. 75 // 76 // Position information is recorded in the file set fset, which must not be 77 // nil. 78 // 79 // If the source couldn't be read, the returned AST is nil and the error 80 // indicates the specific failure. If the source was read but syntax 81 // errors were found, the result is a partial AST (with [ast.Bad]* nodes 82 // representing the fragments of erroneous source code). Multiple errors 83 // are returned via a scanner.ErrorList which is sorted by source position. 84 func ParseFile(fset *token.FileSet, filename string, src any, mode Mode) (f *ast.File, err error) { 85 if fset == nil { 86 panic("parser.ParseFile: no token.FileSet provided (fset == nil)") 87 } 88 89 // get source 90 text, err := readSource(filename, src) 91 if err != nil { 92 return nil, err 93 } 94 95 file := fset.AddFile(filename, -1, len(text)) 96 97 var p parser 98 defer func() { 99 if e := recover(); e != nil { 100 // resume same panic if it's not a bailout 101 bail, ok := e.(bailout) 102 if !ok { 103 panic(e) 104 } else if bail.msg != "" { 105 p.errors.Add(p.file.Position(bail.pos), bail.msg) 106 } 107 } 108 109 // set result values 110 if f == nil { 111 // source is not a valid Go source file - satisfy 112 // ParseFile API and return a valid (but) empty 113 // *ast.File 114 f = &ast.File{ 115 Name: new(ast.Ident), 116 Scope: ast.NewScope(nil), 117 } 118 } 119 120 // Ensure the start/end are consistent, 121 // whether parsing succeeded or not. 122 f.FileStart = token.Pos(file.Base()) 123 f.FileEnd = token.Pos(file.Base() + file.Size()) 124 125 p.errors.Sort() 126 err = p.errors.Err() 127 }() 128 129 // parse source 130 p.init(file, text, mode) 131 f = p.parseFile() 132 133 return 134 } 135 136 // ParseDir calls [ParseFile] for all files with names ending in ".go" in the 137 // directory specified by path and returns a map of package name -> package 138 // AST with all the packages found. 139 // 140 // If filter != nil, only the files with [fs.FileInfo] entries passing through 141 // the filter (and ending in ".go") are considered. The mode bits are passed 142 // to [ParseFile] unchanged. Position information is recorded in fset, which 143 // must not be nil. 144 // 145 // If the directory couldn't be read, a nil map and the respective error are 146 // returned. If a parse error occurred, a non-nil but incomplete map and the 147 // first error encountered are returned. 148 func ParseDir(fset *token.FileSet, path string, filter func(fs.FileInfo) bool, mode Mode) (pkgs map[string]*ast.Package, first error) { 149 list, err := os.ReadDir(path) 150 if err != nil { 151 return nil, err 152 } 153 154 pkgs = make(map[string]*ast.Package) 155 for _, d := range list { 156 if d.IsDir() || !strings.HasSuffix(d.Name(), ".go") { 157 continue 158 } 159 if filter != nil { 160 info, err := d.Info() 161 if err != nil { 162 return nil, err 163 } 164 if !filter(info) { 165 continue 166 } 167 } 168 filename := filepath.Join(path, d.Name()) 169 if src, err := ParseFile(fset, filename, nil, mode); err == nil { 170 name := src.Name.Name 171 pkg, found := pkgs[name] 172 if !found { 173 pkg = &ast.Package{ 174 Name: name, 175 Files: make(map[string]*ast.File), 176 } 177 pkgs[name] = pkg 178 } 179 pkg.Files[filename] = src 180 } else if first == nil { 181 first = err 182 } 183 } 184 185 return 186 } 187 188 // ParseExprFrom is a convenience function for parsing an expression. 189 // The arguments have the same meaning as for [ParseFile], but the source must 190 // be a valid Go (type or value) expression. Specifically, fset must not 191 // be nil. 192 // 193 // If the source couldn't be read, the returned AST is nil and the error 194 // indicates the specific failure. If the source was read but syntax 195 // errors were found, the result is a partial AST (with [ast.Bad]* nodes 196 // representing the fragments of erroneous source code). Multiple errors 197 // are returned via a scanner.ErrorList which is sorted by source position. 198 func ParseExprFrom(fset *token.FileSet, filename string, src any, mode Mode) (expr ast.Expr, err error) { 199 if fset == nil { 200 panic("parser.ParseExprFrom: no token.FileSet provided (fset == nil)") 201 } 202 203 // get source 204 text, err := readSource(filename, src) 205 if err != nil { 206 return nil, err 207 } 208 209 var p parser 210 defer func() { 211 if e := recover(); e != nil { 212 // resume same panic if it's not a bailout 213 bail, ok := e.(bailout) 214 if !ok { 215 panic(e) 216 } else if bail.msg != "" { 217 p.errors.Add(p.file.Position(bail.pos), bail.msg) 218 } 219 } 220 p.errors.Sort() 221 err = p.errors.Err() 222 }() 223 224 // parse expr 225 file := fset.AddFile(filename, -1, len(text)) 226 p.init(file, text, mode) 227 expr = p.parseRhs() 228 229 // If a semicolon was inserted, consume it; 230 // report an error if there's more tokens. 231 if p.tok == token.SEMICOLON && p.lit == "\n" { 232 p.next() 233 } 234 p.expect(token.EOF) 235 236 return 237 } 238 239 // ParseExpr is a convenience function for obtaining the AST of an expression x. 240 // The position information recorded in the AST is undefined. The filename used 241 // in error messages is the empty string. 242 // 243 // If syntax errors were found, the result is a partial AST (with [ast.Bad]* nodes 244 // representing the fragments of erroneous source code). Multiple errors are 245 // returned via a scanner.ErrorList which is sorted by source position. 246 func ParseExpr(x string) (ast.Expr, error) { 247 return ParseExprFrom(token.NewFileSet(), "", []byte(x), 0) 248 } 249