Involved Source Filesinterface.go Package parser implements a parser for Go source files. Input may be
provided in a variety of forms (see the various Parse* functions); the
output is an abstract syntax tree (AST) representing the Go source. The
parser is invoked through one of the Parse* functions.
The parser accepts a larger language than is syntactically permitted by
the Go spec, for simplicity, and for improved robustness in the presence
of syntax errors. For instance, in method declarations, the receiver is
treated like an ordinary parameter list and thus may contain multiple
entries where the spec permits exactly one. Consequently, the corresponding
field in the AST (ast.FuncDecl.Recv) field is not restricted to one entry.resolver.go
Code Examples
package main
import (
"fmt"
"go/parser"
"go/token"
)
func main() {
fset := token.NewFileSet() // positions are relative to fset
src := `package foo
import (
"fmt"
"time"
)
func bar() {
fmt.Println(time.Now())
}`
// Parse src but stop after processing the imports.
f, err := parser.ParseFile(fset, "", src, parser.ImportsOnly)
if err != nil {
fmt.Println(err)
return
}
// Print the imports from the file's AST.
for _, s := range f.Imports {
fmt.Println(s.Path.Value)
}
}
Package-Level Type Names (total 6, in which 1 is exported)
Package-Level Functions (total 15, in which 4 are exported)
ParseDir calls [ParseFile] for all files with names ending in ".go" in the
directory specified by path and returns a map of package name -> package
AST with all the packages found.
If filter != nil, only the files with [fs.FileInfo] entries passing through
the filter (and ending in ".go") are considered. The mode bits are passed
to [ParseFile] unchanged. Position information is recorded in fset, which
must not be nil.
If the directory couldn't be read, a nil map and the respective error are
returned. If a parse error occurred, a non-nil but incomplete map and the
first error encountered are returned.
ParseExpr is a convenience function for obtaining the AST of an expression x.
The position information recorded in the AST is undefined. The filename used
in error messages is the empty string.
If syntax errors were found, the result is a partial AST (with [ast.Bad]* nodes
representing the fragments of erroneous source code). Multiple errors are
returned via a scanner.ErrorList which is sorted by source position.
ParseExprFrom is a convenience function for parsing an expression.
The arguments have the same meaning as for [ParseFile], but the source must
be a valid Go (type or value) expression. Specifically, fset must not
be nil.
If the source couldn't be read, the returned AST is nil and the error
indicates the specific failure. If the source was read but syntax
errors were found, the result is a partial AST (with [ast.Bad]* nodes
representing the fragments of erroneous source code). Multiple errors
are returned via a scanner.ErrorList which is sorted by source position.
ParseFile parses the source code of a single Go source file and returns
the corresponding [ast.File] node. The source code may be provided via
the filename of the source file, or via the src parameter.
If src != nil, ParseFile parses the source from src and the filename is
only used when recording position information. The type of the argument
for the src parameter must be string, []byte, or [io.Reader].
If src == nil, ParseFile parses the file specified by filename.
The mode parameter controls the amount of source text parsed and
other optional parser functionality. If the [SkipObjectResolution]
mode bit is set (recommended), the object resolution phase of
parsing will be skipped, causing File.Scope, File.Unresolved, and
all Ident.Obj fields to be nil. Those fields are deprecated; see
[ast.Object] for details.
Position information is recorded in the file set fset, which must not be
nil.
If the source couldn't be read, the returned AST is nil and the error
indicates the specific failure. If the source was read but syntax
errors were found, the result is a partial AST (with [ast.Bad]* nodes
representing the fragments of erroneous source code). Multiple errors
are returned via a scanner.ErrorList which is sorted by source position.
decNestLev is used to track nesting depth during parsing to prevent stack exhaustion.
It is used along with incNestLev in a similar fashion to how un and trace are used.
extractName splits the expression x into (name, expr) if syntactically
x can be written as name expr. The split only happens if expr is a type
element (per the isTypeElem predicate) or if force is set.
If x is just a name, the result is (name, nil). If the split succeeds,
the result is (name, expr). Otherwise the result is (nil, x).
Examples:
x force name expr
------------------------------------
P*[]int T/F P *[]int
P*E T P *E
P*E F nil P*E
P([]int) T/F P ([]int)
P(E) T P (E)
P(E) F nil P(E)
P*E|F|~G T/F P *E|F|~G
P*E|F|G T P *E|F|G
P*E|F|G F nil P*E|F|G
isTypeElem reports whether x is a (possibly parenthesized) type element expression.
The result is false if x could be a type element OR an ordinary (value) expression.
packIndexExpr returns an IndexExpr x[expr0] or IndexListExpr x[expr0, ...].
If src != nil, readSource converts src to a []byte if possible;
otherwise it returns an error. If src == nil, readSource returns
the result of reading the file specified by filename.
resolveFile walks the given file to resolve identifiers within the file
scope, updating ast.Ident.Obj fields with declaration information.
If declErr is non-nil, it is used to report declaration errors during
resolution. tok is used to format position in error messages.
The unresolved object is a sentinel to mark identifiers that have been added
to the list of unresolved identifiers. The sentinel is only used for verifying
internal consistency.
Package-Level Constants (total 14, in which 8 are exported)
constAllErrorsMode = 32 // report all errors (not just the first 10 on different lines)