Help us shape the future of Go! Take the 2026 Go Developer Survey

parse

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 18, 2026 License: MIT Imports: 35 Imported by: 0

Documentation

Overview

Package parse turns source files into language-neutral model.Facts using tree-sitter grammars and per-language extraction queries.

Languages self-register (see Register) from lang_<x>.go files that embed a queries/<x>.scm query. The query follows a capture convention documented in queries/go.scm; the generic extractor in extract.go interprets it, and optional hooks (hooks.go) refine the result per language.

Index

Constants

View Source
const CSharpPartialQual = "<partial>"

CSharpPartialQual is the qualifier of the synthetic embeds edge that links every part of a partial type to its sibling parts (see postProcess). It is not a valid C# identifier, so it never collides with a written qualifier.

Variables

View Source
var ErrClosed = errors.New("parse: closed")

ErrClosed is returned when using a closed Engine or Pool.

View Source
var ErrCorruptFacts = errors.New("parse: corrupt facts encoding")

ErrCorruptFacts is wrapped by every DecodeFacts error.

Functions

func AppendFacts

func AppendFacts(dst []byte, f *model.Facts) []byte

AppendFacts appends the binary encoding of f to dst.

func CSharpIsPartialSig

func CSharpIsPartialSig(sig string) bool

CSharpIsPartialSig reports whether a type signature carries the partial modifier ("public partial class X").

func DecodeFacts

func DecodeFacts(data []byte) (f *model.Facts, err error)

DecodeFacts decodes data produced by EncodeFacts. It never panics on malformed input; errors wrap ErrCorruptFacts. The result is not validated semantically (see model.Facts.Validate).

func DumpFacts

func DumpFacts(f *model.Facts) string

DumpFacts renders facts as stable, human-readable text for golden tests and debugging. Definitions are shown with their qualified name; references and inheritance edges name their enclosing definition.

func EncodeFacts

func EncodeFacts(f *model.Facts) []byte

EncodeFacts returns the binary encoding of f.

func ForExt

func ForExt(ext string) (model.LanguageSpec, bool)

ForExt returns the spec claiming ext (with leading dot). Exact case is tried first, then lowercase, mirroring model.LangFromExt.

func ForPath

func ForPath(p string) (model.LanguageSpec, bool)

ForPath returns the spec for a file path by its extension.

func IsTestPath

func IsTestPath(p string) bool

IsTestPath reports whether path is a test file by the naming convention of its language (see TestPathMatcher). Unknown languages return false.

func JavaExternalName

func JavaExternalName(name string) bool

JavaExternalName reports whether a qualified name belongs to the Java platform (java.*, javax.*, ...).

func JavaPackageOf

func JavaPackageOf(src []byte) string

JavaPackageOf scans the package declaration at the top of a Java source file without parsing it. It returns "" for the default package.

func Langs

func Langs() []model.Lang

Langs returns the registered languages in ascending order.

func Lookup

func Lookup(l model.Lang) (model.LanguageSpec, bool)

Lookup returns the spec registered for l.

func PHPNamespaceModule

func PHPNamespaceModule(ns string) string

PHPNamespaceModule converts a namespace ("App\Model", optionally with a leading backslash) to the dotted module identity ("App.Model").

func PHPSplitFQN

func PHPSplitFQN(fqn string) (qualifier, name string)

PHPSplitFQN splits a fully qualified name into the qualifier form stored in facts ("\" + namespace, "\" for the global namespace) and the name.

func PythonBuiltin

func PythonBuiltin(name string) bool

PythonBuiltin reports whether name is a Python builtin (function, type, exception or constant); unshadowed references to it never resolve to repository code.

func Register

func Register(spec model.LanguageSpec)

Register adds spec to the registry. It panics on an invalid or duplicate language, a duplicate extension, or an extension without a leading dot, since all of these are programming errors caught at init time.

Types

type Engine

type Engine struct {
	// contains filtered or unexported fields
}

Engine parses files. It owns one tree-sitter Parser and QueryCursor per language, created lazily, while the compiled Query is shared process-wide. An Engine is not safe for concurrent use; use a Pool for parallelism. Close must be called to release native memory.

func NewEngine

func NewEngine() *Engine

NewEngine returns an empty Engine.

func (*Engine) Close

func (e *Engine) Close()

Close releases all parsers and cursors. It is idempotent.

func (*Engine) Parse

func (e *Engine) Parse(ctx context.Context, spec model.LanguageSpec, src []byte) (Result, error)

Parse extracts facts from src using spec. ctx cancels a long parse.

func (*Engine) ParseLang

func (e *Engine) ParseLang(ctx context.Context, l model.Lang, src []byte) (Result, error)

ParseLang is Parse for a registered language.

type Job

type Job struct {
	Spec model.LanguageSpec
	Src  []byte
	Tag  any // caller data passed through to Output (e.g. a file id)
}

Job is one file to parse.

type Output

type Output struct {
	Tag    any
	Result Result
	Err    error
}

Output is the result of a Job.

type PHPNameScope

type PHPNameScope struct {
	// Namespace is the current namespace without a leading backslash.
	Namespace string
	// contains filtered or unexported fields
}

PHPNameScope resolves PHP class and function names textually, as the language does: fully qualified names stand, a leading segment matching a use import is replaced by it, everything else is relative to the current namespace. Unqualified function names without a use import are left unresolved because PHP falls back to the global namespace at runtime. The zero value is the global namespace without imports.

func (*PHPNameScope) AddUse

func (s *PHPNameScope) AddUse(kind, fqn, alias string)

AddUse records an import of fqn (leading backslash optional) under its alias or last segment. kind is "", "function" or "const".

func (*PHPNameScope) Class

func (s *PHPNameScope) Class(name string) (string, bool)

Class returns the fully qualified name (without leading backslash) of a class-like name. ok is false for self, static and parent.

func (*PHPNameScope) Function

func (s *PHPNameScope) Function(name string) (string, bool)

Function returns the fully qualified name of a function or constant name; ok is false for an unqualified name without a use import.

type Pool

type Pool struct {
	// contains filtered or unexported fields
}

Pool is a fixed set of Engines, one per worker. Each Engine owns its parsers and query cursors, so workers never share mutable tree-sitter state; compiled queries are shared read-only. A Pool is safe for concurrent use.

func NewPool

func NewPool(n int) *Pool

NewPool creates a pool of n engines; n <= 0 means GOMAXPROCS.

func (*Pool) Close

func (p *Pool) Close()

Close waits for borrowed engines to be returned and releases all native resources. Further use returns ErrClosed. It is idempotent.

func (*Pool) Parse

func (p *Pool) Parse(ctx context.Context, spec model.LanguageSpec, src []byte) (Result, error)

Parse parses one file on a borrowed engine.

func (*Pool) Run

func (p *Pool) Run(ctx context.Context, jobs <-chan Job, out chan<- Output) error

Run parses jobs with up to Size() workers until jobs is closed or ctx is done, sending one Output per job to out. It does not close out. A worker keeps its engine for the whole run. It returns ctx.Err() if cancelled.

func (*Pool) Size

func (p *Pool) Size() int

Size returns the number of engines (the useful parallelism).

type Result

type Result struct {
	Facts *model.Facts
	// SyntaxErrors is set when the tree contains ERROR or MISSING nodes.
	// Facts are still extracted best-effort (model.StatusParseErr).
	SyntaxErrors bool
}

Result is the outcome of parsing one file.

type TestPathMatcher

type TestPathMatcher interface {
	IsTestPath(path string) bool
}

TestPathMatcher is implemented by specs whose test files are identified by path (e.g. Go's *_test.go). Facts are content-addressed and cached without the path, so the indexer combines this with Facts.TestFile.

Jump to

Keyboard shortcuts

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