Documentation ¶
Overview ¶
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.
Index ¶
- Constants
- func ParseDeclList(fset *token.FileSet, filename string, src interface{}) ([]ast.Decl, os.Error)
- func ParseDir(fset *token.FileSet, path string, filter func(*os.FileInfo) bool, mode uint) (map[string]*ast.Package, os.Error)
- func ParseExpr(fset *token.FileSet, filename string, src interface{}) (ast.Expr, os.Error)
- func ParseFile(fset *token.FileSet, filename string, src interface{}, mode uint) (*ast.File, os.Error)
- func ParseFiles(fset *token.FileSet, filenames []string, mode uint) (pkgs map[string]*ast.Package, first os.Error)
- func ParseStmtList(fset *token.FileSet, filename string, src interface{}) ([]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 DeclarationErrors // report declaration errors )
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 fset, filename, and src 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(fset *token.FileSet, path string, filter func(*os.FileInfo) 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.FileInfo entries passing through the filter are considered. The mode bits are passed to ParseFile unchanged. Position information is recorded in the file set fset.
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 error are returned.
func ParseExpr ¶
ParseExpr parses a Go expression and returns the corresponding AST node. The fset, filename, and src 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 ¶
func ParseFile(fset *token.FileSet, filename string, src interface{}, mode uint) (*ast.File, os.Error)
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. Position information is recorded in the file set fset.
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(fset *token.FileSet, filenames []string, mode uint) (pkgs map[string]*ast.Package, first 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. Position information is recorded in the file set fset.
Files with parse errors are ignored. In this case the map of packages may be incomplete (missing packages and/or incomplete packages) and the first error encountered is returned.
func ParseStmtList ¶
ParseStmtList parses a list of Go statements and returns the list of corresponding AST nodes. The fset, filename, and src 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.