parser

package
v1.5.3 Latest Latest
Warning

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

Go to latest
Published: Dec 2, 2025 License: Apache-2.0 Imports: 18 Imported by: 3

Documentation

Overview

Package parser implements a parser for XGo 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 XGo 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)

	// ParseGoAsGoPlus - parse Go files by gop/parser
	ParseGoAsGoPlus Mode = 1 << 16
	// ParserGoPlusClass - parse XGo 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

func Parse(fset *token.FileSet, filename string, src any, mode Mode) (pkgs map[string]*ast.Package, err error)

Parse parses a single XGo source file. The filename specifies the XGo 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

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 ParseEntries

func ParseEntries(fset *token.FileSet, files []string, conf Config) (map[string]*ast.Package, error)

ParseEntries parses XGo source files and returns the corresponding packages. Compared to ParseFiles, ParseEntries detects fileKind by its filename.

func ParseEntry

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

ParseEntry calls ParseFSEntry by passing a local filesystem.

func ParseExpr

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 ParseExprEx

func ParseExprEx(file *token.File, src []byte, offset int, mode Mode) (expr ast.Expr, err scanner.ErrorList)

ParseExprEx 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/XGo (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 ParseExprFrom

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/XGo (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 XGo 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 XGo files with fs.FileInfo entries passing through the filter 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 ParseFSEntries

func ParseFSEntries(fset *token.FileSet, fs FileSystem, files []string, conf Config) (map[string]*ast.Package, error)

ParseFSEntries parses XGo source files and returns the corresponding packages. Compared to ParseFSFiles, ParseFSEntries detects fileKind by its filename.

func ParseFSEntry

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

ParseFSEntry parses the source code of a single XGo 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 any, mode Mode) (f *ast.File, err error)

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

func ParseFSFiles

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

ParseFSFiles parses XGo source files and returns the corresponding packages.

func ParseFile

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

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

func ParseFiles

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

ParseFiles parses XGo source files and returns the corresponding packages.

func SetDebug

func SetDebug(dbgFlags int)

Types

type Config

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