parser

package
v1.2.0-test.4 Latest Latest
Warning

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

Go to latest
Published: Jan 13, 2024 License: Apache-2.0 Imports: 17 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 (
	// PackageClauseOnly - stop parsing after package clause
	PackageClauseOnly = Mode(goparser.PackageClauseOnly)
	// ImportsOnly - stop parsing after import declarations
	ImportsOnly = Mode(goparser.ImportsOnly)
	// ParseComments - parse comments and add them to AST
	ParseComments = Mode(goparser.ParseComments)
	// Trace - print a trace of parsed productions
	Trace = Mode(goparser.Trace)
	// DeclarationErrors - report declaration errors
	DeclarationErrors = Mode(goparser.DeclarationErrors)
	// AllErrors - report all errors (not just the first 10 on different lines)
	AllErrors = Mode(goparser.AllErrors)
	// SkipObjectResolution - don't resolve identifiers to objects - see ParseFile
	SkipObjectResolution = Mode(goparser.SkipObjectResolution)
	// ParseGoAsGoPlus - parse Go files by gop/parser
	ParseGoAsGoPlus Mode = 1 << 16
	// ParserGoPlusClass - parse Go+ classfile by gop/parser
	ParseGoPlusClass Mode = 1 << 17
	// SaveAbsFile - parse and save absolute path to pkg.Files
	SaveAbsFile Mode = 1 << 18
)
View Source
const (
	DbgFlagParseOutput = 1 << iota
	DbgFlagParseError
	DbgFlagAll = DbgFlagParseOutput | DbgFlagParseError
)

Variables

View Source
var (
	ErrUnknownFileKind = errors.New("unknown file kind")
)

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(fs.FileInfo) bool, mode Mode) (pkgs map[string]*ast.Package, first error)

ParseDir calls ParseFSDir by passing a local filesystem.

func ParseDirEx added in v1.1.0

func ParseDirEx(fset *token.FileSet, path string, conf Config) (pkgs map[string]*ast.Package, first error)

ParseDirEx calls ParseFSDir by passing a local filesystem.

func ParseEntry added in v1.1.10

func ParseEntry(fset *token.FileSet, filename string, src interface{}, conf Config) (f *ast.File, err error)

ParseEntry calls ParseFSEntry by passing a local filesystem.

func ParseExpr added in v1.1.9

func ParseExpr(x string) (ast.Expr, error)

ParseExpr is a convenience function for obtaining the AST of an expression x. The position information recorded in the AST is undefined. The filename used in error messages is the empty string.

If syntax errors were found, the result is a partial AST (with ast.Bad* nodes representing the fragments of erroneous source code). Multiple errors are returned via a scanner.ErrorList which is sorted by source position.

func ParseExprFrom added in v1.1.9

func ParseExprFrom(fset *token.FileSet, filename string, src any, mode Mode) (expr ast.Expr, err error)

ParseExprFrom is a convenience function for parsing an expression. The arguments have the same meaning as for ParseFile, but the source must be a valid Go (type or value) expression. Specifically, fset must not be nil.

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.Bad* nodes representing the fragments of erroneous source code). Multiple errors are returned via a scanner.ErrorList which is sorted by source position.

func ParseFSDir

func ParseFSDir(fset *token.FileSet, fs FileSystem, dir string, conf Config) (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 fs.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 ParseFSEntry added in v1.1.10

func ParseFSEntry(fset *token.FileSet, fs FileSystem, filename string, src interface{}, conf Config) (f *ast.File, err error)

ParseFSEntry parses the source code of a single Go+ source file and returns the corresponding ast.File node. Compared to ParseFSFile, ParseFSEntry detects fileKind by its filename.

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 ParseFSFiles added in v1.0.30

func ParseFSFiles(fset *token.FileSet, fs FileSystem, files []string, mode Mode) (map[string]*ast.Package, error)

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 ParseFiles added in v1.0.30

func ParseFiles(fset *token.FileSet, files []string, mode Mode) (map[string]*ast.Package, error)

func SetDebug added in v0.9.9

func SetDebug(dbgFlags int)

Types

type Config added in v1.1.0

type Config struct {
	ClassKind func(fname string) (isProj, ok bool)
	Filter    func(fs.FileInfo) bool
	Mode      Mode
}

type FileSystem

type FileSystem = fsx.FileSystem

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.

Directories

Path Synopsis
fsx

Jump to

Keyboard shortcuts

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