parser

package
v1.1.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 3, 2020 License: BSD-3-Clause Imports: 12 Imported by: 70

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

View Source
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

View Source
var Universe = ast.NewScope(nil)

Functions

func ParseDeclList

func ParseDeclList(fset *token.FileSet, filename string, src interface{}, scope *ast.Scope, pathToName ImportPathToName) ([]ast.Decl, error)

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.

If scope is non-nil, it will be used for declarations.

func ParseDir

func ParseDir(fset *token.FileSet, path string, filter func(os.FileInfo) bool, mode uint, pathToName ImportPathToName) (map[string]*ast.Package, 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

func ParseExpr(fset *token.FileSet, filename string, src interface{}, scope *ast.Scope, pathToName ImportPathToName) (ast.Expr, error)

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.

if scope is non-nil, it will be used as the scope for the expression.

func ParseFile

func ParseFile(fset *token.FileSet, filename string, src interface{}, mode uint, pkgScope *ast.Scope, pathToName ImportPathToName) (*ast.File, 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, pathToName ImportPathToName) (pkgs map[string]*ast.Package, first 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

func ParseStmtList(fset *token.FileSet, filename string, src interface{}, scope *ast.Scope, pathToName ImportPathToName) ([]ast.Stmt, error)

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.

if scope is non-nil, it will be used as the scope for the statements.

Types

type ImportPathToName

type ImportPathToName func(path string, fromDir string) (string, error)

ImportPathToName is the type of the function that's used to find the package name for an imported package path. The fromDir argument holds the directory that contains the import statement, which may be empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL