Documentation ¶
Overview ¶
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.
Index ¶
- Constants
- func ParseDeclList(filename string, src interface{}, scope *ast.Scope) ([]ast.Decl, os.Error)
- func ParseDir(path string, filter func(*os.Dir) bool, mode uint) (map[string]*ast.Package, os.Error)
- func ParseExpr(filename string, src interface{}, scope *ast.Scope) (ast.Expr, os.Error)
- func ParseFile(filename string, src interface{}, scope *ast.Scope, mode uint) (*ast.File, os.Error)
- func ParseFiles(filenames []string, scope *ast.Scope, mode uint) (map[string]*ast.Package, os.Error)
- func ParseStmtList(filename string, src interface{}, scope *ast.Scope) ([]ast.Stmt, os.Error)
Constants ¶
const ( PackageClauseOnly uint = 1 << iota // parsing stops after package clause ImportsOnly // parsing stops after import declarations ParseComments // parse comments and add them to AST Trace // print a trace of parsed productions )
The mode parameter to the Parse* functions is a set of flags (or 0). They control the amount of source code parsed and other optional parser functionality.
Variables ¶
This section is empty.
Functions ¶
func ParseDeclList ¶
ParseDeclList parses a list of Go declarations and returns the list of corresponding AST nodes. The filename, src, and scope arguments have the same interpretation as for ParseFile. If there is an error, the node list may be nil or contain partial ASTs.
func ParseDir ¶
func ParseDir(path string, filter func(*os.Dir) bool, mode uint) (map[string]*ast.Package, os.Error)
ParseDir calls ParseFile for the files 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 os.Dir entries passing through the filter are considered. The mode bits are passed to ParseFile unchanged.
If the directory couldn't be read, a nil map and the respective error are returned. If a parse error occured, a non-nil but incomplete map and the error are returned.
func ParseExpr ¶
ParseExpr parses a Go expression and returns the corresponding AST node. The filename, src, and scope arguments have the same interpretation as for ParseFile. If there is an error, the result expression may be nil or contain a partial AST.
func ParseFile ¶
ParseFile parses a Go source file and returns a File node.
If src != nil, ParseFile parses the file source from src. src may be provided in a variety of formats. At the moment the following types are supported: string, []byte, and io.Reader. In this case, filename is only used for source position information and error messages.
If src == nil, ParseFile parses the file specified by filename.
If scope != nil, it is the immediately surrounding scope for the file (the package scope) and it is used to lookup and declare identifiers. When parsing multiple files belonging to a package, the same scope should be provided to all files.
The mode parameter controls the amount of source text parsed and other optional parser functionality.
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.BadX nodes representing the fragments of erroneous source code). Multiple errors are returned via a scanner.ErrorList which is sorted by file position.
func ParseFiles ¶
func ParseFiles(filenames []string, scope *ast.Scope, mode uint) (map[string]*ast.Package, os.Error)
ParseFiles calls ParseFile for each file in the filenames list and returns a map of package name -> package AST with all the packages found. The mode bits are passed to ParseFile unchanged.
Files with parse errors are ignored. In this case the map of packages may be incomplete (missing packages and/or incomplete packages) and the last error encountered is returned.
func ParseStmtList ¶
ParseStmtList parses a list of Go statements and returns the list of corresponding AST nodes. The filename, src, and scope arguments have the same interpretation as for ParseFile. If there is an error, the node list may be nil or contain partial ASTs.
Types ¶
This section is empty.