parser

package
v1.0.0-rc3 Latest Latest
Warning

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

Go to latest
Published: Sep 12, 2021 License: Apache-2.0 Imports: 14 Imported by: 8

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.

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.

Index

Constants

View Source
const (
	DbgFlagParseOutput = 1 << iota
	DbgFlagParseError
	DbgFlagAll = DbgFlagParseOutput | DbgFlagParseError
)

Variables

This section is empty.

Functions

func Parse added in v0.7.3

func Parse(fset *token.FileSet, target string, src interface{}, mode Mode) (pkgs map[string]*ast.Package, err error)

Parse parses a single Go+ source file. The target specifies the Go+ source file. If the file couldn't be read, a nil map and the respective error are returned.

func ParseDir

func ParseDir(fset *token.FileSet, path string, filter func(os.FileInfo) bool, mode Mode) (pkgs map[string]*ast.Package, first error)

ParseDir calls ParseFSDir by passing a local filesystem.

func ParseFSDir

func ParseFSDir(fset *token.FileSet, fs FileSystem, path string, filter func(os.FileInfo) bool, mode Mode) (pkgs map[string]*ast.Package, first error)

ParseFSDir calls ParseFile for all files with names ending in ".gop" 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 (and ending in ".gop") 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.

func ParseFSFile

func ParseFSFile(fset *token.FileSet, fs FileSystem, filename string, src interface{}, mode Mode) (f *ast.File, err error)

ParseFSFile parses the source code of a single Go+ source file and returns the corresponding ast.File node.

func ParseFile

func ParseFile(fset *token.FileSet, filename string, src interface{}, mode Mode) (f *ast.File, err error)

ParseFile parses the source code of a single Go+ source file and returns the corresponding ast.File node.

func SetDebug added in v0.9.9

func SetDebug(dbgFlags int)

Types

type FileSystem

type FileSystem interface {
	ReadDir(dirname string) ([]os.FileInfo, error)
	ReadFile(filename string) ([]byte, error)
	Join(elem ...string) string
}

FileSystem represents a file system.

type Mode

type Mode uint

A Mode value is a set of flags (or 0). They control the amount of source code parsed and other optional parser functionality.

const (
	// PackageClauseOnly - stop parsing after package clause
	PackageClauseOnly Mode = 1 << iota
	// ImportsOnly - stop parsing after import declarations
	ImportsOnly
	// ParseComments - parse comments and add them to AST
	ParseComments
	// Trace - print a trace of parsed productions
	Trace
	// DeclarationErrors - report declaration errors
	DeclarationErrors
	// AllErrors - report all errors (not just the first 10 on different lines)
	AllErrors
	// ParseGoFiles - parse *.go files
	ParseGoFiles
)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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