Documentation
¶
Overview ¶
Package goparser implements a structured parser for Go.
Index ¶
- Variables
- func IsExported(name string) bool
- func MatchFileName(name string, ctx *buildContext) bool
- func MatchFileNameFor(name, goos, goarch string) bool
- func PackageName(importPath string) string
- func QualifyName(pkg, name string) string
- func RegisterGenericShim(pkg, source string, nativeRefs []string)
- type DeferredDecl
- type ErrUndefined
- type PackageSource
- type Parser
- func (p *Parser) ImportPackageValues(m map[string]map[string]reflect.Value)
- func (p *Parser) LoadPackageSources(importPath string, includeTests bool) ([]PackageSource, error)
- func (p *Parser) ParseAll(name, src string) (out []DeferredDecl, err error)
- func (p *Parser) ParseDecl(toks Tokens) (handled bool, err error)
- func (p *Parser) ParseOneStmt(toks Tokens) (Tokens, error)
- func (p *Parser) SetBuildContext(goos, goarch string)
- func (p *Parser) SetIncludeTests(b bool)
- func (p *Parser) SetPkgfs(pkgPath string)
- func (p *Parser) SetRemoteFS(fsys fs.FS)
- func (p *Parser) SetStdlibFS(fsys fs.FS)
- func (p *Parser) SymAdd(i int, name string, v vm.Value, k symbol.Kind, t *vm.Type)
- func (p *Parser) SymSet(key string, sym *symbol.Symbol)
- func (p *Parser) WithImportingPkg(pkg string) func()
- type SelectCaseDesc
- type Token
- type Tokens
Constants ¶
This section is empty.
Variables ¶
var ( ErrEllipsisArray = errors.New("[...] array") ErrFuncType = errors.New("invalid function type") ErrInvalidType = errors.New("invalid type") ErrMissingType = errors.New("missing type") ErrSize = errors.New("invalid size") ErrSyntax = errors.New("syntax error") ErrNotImplemented = errors.New("not implemented") )
Type parsing error definitions.
Functions ¶
func IsExported ¶
IsExported reports whether the given name starts with an upper-case letter.
func MatchFileName ¶
MatchFileName reports whether name (a .go file basename) matches the given build context's GOOS/GOARCH constraints encoded in the file name.
func MatchFileNameFor ¶
MatchFileNameFor reports whether name matches the given GOOS/GOARCH constraints encoded in the file name. It is like MatchFileName but for an explicit platform.
func PackageName ¶
PackageName returns the identifier used to reference the package given its import path: the last segment, or the second-to-last when the last matches "v[0-9]*" (module versioning suffix).
func QualifyName ¶ added in v0.2.0
QualifyName composes the canonical pkg-qualified symbol-table key for a top-level name. For pointer-receiver method names ("*Tag.M"), the '*' moves to the very front of the key ("*<pkg>.Tag.M") so the standard pointer- method composition `"*"+typeKey+"."+method` still produces the same key. Exported so the comp package (qualifyLabel) shares the exact composition.
func RegisterGenericShim ¶ added in v0.2.0
RegisterGenericShim queues an interpreted-source shim to install into pkg's symbol table the next time a Parser populates that package (typically from ImportPackageValues). source must start with `package <pkg>`. nativeRefs lists names referenced bare by source from pkg's exports: those are pre-seeded into p.Symbols at qualified keys so symGet's importingPkg / CompilingPkg fallback resolves them at signature parse and body instantiation time.
Safe to call from package init; the registry persists for the process lifetime and is consulted by every parser created thereafter.
Types ¶
type DeferredDecl ¶ added in v0.2.0
DeferredDecl is a top-level declaration (func body, var initializer) whose code generation is deferred to Phase 2, tagged with the import path of the package it came from ("" for the main package / REPL). The tag lets Phase 2 resolve unqualified identifiers against the originating package's symbols, which matters when a sibling import shadowed a bare name in the symbol table.
type ErrUndefined ¶
ErrUndefined is returned during parsing when a referenced symbol is not yet defined. It is retryable: the lazy fixpoint loop in interp.Eval defers the declaration and retries after other declarations have been processed.
func (ErrUndefined) Error ¶
func (e ErrUndefined) Error() string
type PackageSource ¶ added in v0.2.0
PackageSource is a single .go file's basename and content as loaded by LoadPackageSources.
type Parser ¶
type Parser struct {
*scan.Scanner
Symbols symbol.SymMap
Packages map[string]*symbol.Package
CompilingPkg string // while a deferred decl is being parsed/compiled in Phase 2: its origin package's import path ("" = main/REPL); makes unqualified type/name lookups prefer that package's symbols (see symGet, comp.Compiler.symAt)
InitFuncs []string // ordered list of init function internal names
// contains filtered or unexported fields
}
Parser represents the state of a parser.
func (*Parser) ImportPackageValues ¶
ImportPackageValues populates packages with values.
func (*Parser) LoadPackageSources ¶ added in v0.2.0
func (p *Parser) LoadPackageSources(importPath string, includeTests bool) ([]PackageSource, error)
LoadPackageSources returns the .go files of the given package import path (a directory in the FS chain pkgfs -> stdlibfs -> remotefs), filtered by build tags (file-name and //go:build directives). When includeTests is false, _test.go files are skipped (matching `import "X"` resolution); pass true to include them (used by `mvm test <importpath>`).
Result order matches fs.ReadDir, which is sorted by filename.
func (*Parser) ParseAll ¶
func (p *Parser) ParseAll(name, src string) (out []DeferredDecl, err error)
ParseAll parses code and its dependencies, and returns the still-to-be- code-generated declarations (func bodies, var initializers), each tagged with its originating package path, or an error. When src == "" the source is loaded from the package directory `name`, so its decls are tagged with `name`; otherwise (main package / REPL) they are tagged with "".
func (*Parser) ParseDecl ¶
ParseDecl resolves a declaration's symbols (Phase 1) without emitting code. Returns handled=true if fully resolved, false if code generation is needed.
func (*Parser) ParseOneStmt ¶
ParseOneStmt parses a single pre-scanned statement token slice.
func (*Parser) SetBuildContext ¶
SetBuildContext overrides the parser's target GOOS/GOARCH for build constraint filtering.
func (*Parser) SetIncludeTests ¶ added in v0.2.0
SetIncludeTests toggles whether ParseAll's directory-mode load includes _test.go files. Off by default (matching `import "X"` resolution); turn on for `mvm test <importpath>` so test functions become callable.
func (*Parser) SetRemoteFS ¶ added in v0.2.0
SetRemoteFS installs a last-resort filesystem consulted when neither pkgfs nor stdlibfs contain the requested import path. Typical use is a modfs.FS that fetches modules from a proxy on demand.
func (*Parser) SetStdlibFS ¶
SetStdlibFS installs a fallback filesystem for resolving imported source packages that are not present in the primary pkgfs. This is used to resolve generics-first stdlib packages (cmp, slices, maps, ...) whose sources are embedded in the interpreter binary.
func (*Parser) SymSet ¶
SymSet inserts sym at key in the symbol table, recording the key for potential rollback.
func (*Parser) WithImportingPkg ¶ added in v0.2.0
WithImportingPkg sets p.importingPkg to pkg and returns a function that restores the previous value. Callers loading a package's source directly (e.g. `mvm test <importpath>`) use this to mirror the canonical-key setup that importSrc performs for transitive imports, so the target's top-level Type/Func/Method/Var/Const symbols land at `<pkg>.<name>` keys rather than bare keys (which would mismatch every subsequent qualified lookup in the target's own deferred bodies). See pkgKey, symGet, and the Phase 2 Path B memory notes.
type SelectCaseDesc ¶
type SelectCaseDesc struct {
Dir reflect.SelectDir
ValName string // scoped name of recv value var ("" if none)
OkName string // scoped name of recv ok var ("" if none)
}
SelectCaseDesc describes one case of a select statement for the compiler.
type Token ¶
Token represents a parser token.
func (Token) FieldKeyName ¶ added in v0.2.0
FieldKeyName returns the field name of a struct-composite-literal key Colon token (emitted by newFieldColon), and false otherwise.
func (*Token) MarkNoFnew ¶ added in v0.2.0
func (t *Token) MarkNoFnew()
MarkNoFnew tags this Token (intended for Type Idents) so the compiler will not emit a speculative Fnew for it.
type Tokens ¶
type Tokens []Token
Tokens represents slice of tokens.
func (Tokens) SplitStart ¶
SplitStart is similar to Split, except the first token in toks is skipped.