Go Home Page
The Go Programming Language

Package parser

import "go/parser"

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.

Package files

interface.go parser.go

Constants

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.

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
)

func ParseDeclList

func ParseDeclList(filename string, src interface{}) ([]ast.Decl, os.Error)

ParseDeclList parses a list of Go declarations and returns the list of corresponding AST nodes. The 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(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.

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

func ParseExpr(filename string, src interface{}) (ast.Expr, os.Error)

ParseExpr parses a Go expression and returns the corresponding AST node. The 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(filename string, src interface{}, mode uint) (*ast.File, os.Error)

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.

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, 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.

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

func ParseStmtList(filename string, src interface{}) ([]ast.Stmt, os.Error)

ParseStmtList parses a list of Go statements and returns the list of corresponding AST nodes. The 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.