Documentation
¶
Overview ¶
Package parser turns one source file into symbols, imports and calls using the language's tree-sitter grammar and queries. It does not resolve calls across files or discover MCP registrations; see Resolve and the registration package for those, which both operate on the whole graph after every file has been parsed.
Index ¶
- Constants
- Variables
- func AssignTargets(node *sitter.Node, src []byte) []string
- func AssignValue(node *sitter.Node, src []byte) *sitter.Node
- func DefName(id language.ID, node *sitter.Node, src []byte) string
- func HasImplicitReceiver(id language.ID, def *sitter.Node, src []byte) bool
- func LastSegment(text string) string
- func LiteralValue(n *sitter.Node, src []byte) (string, bool)
- func ParamBindings(id language.ID, n *sitter.Node, src []byte) []string
- func ParamList(node *sitter.Node) *sitter.Node
- func ParseTree(ctx context.Context, adapter *language.Adapter, src []byte) (*sitter.Tree, error)
- func Resolve(g *graph.Graph)
- func SplitTopLevel(inner string) []string
- func SplitTopLevelSep(inner string, sep byte) []string
- func TemplateWithSubstitutions(body, repl string) (string, bool)
- func UnquoteString(text string) (value string, ok bool)
- type FileResult
- type Receiver
Constants ¶
const CopiedTextFactor = 4
CopiedTextFactor sets the size-dependent budget for copied call text; collectCalls adds a fixed allowance. Source spans share the file buffer, but decoded literals and reconstructed callees allocate text that can grow faster than the input on deeply nested files.
const MaxTreeDepth = 1 << 15
MaxTreeDepth rejects deeply nested syntax trees before queries run. Tree-sitter stores query match depths in 16 bits and can silently lose matches beyond that range. This limit leaves a margin below the overflow boundary.
const ParseTimeout = 5 * time.Second
ParseTimeout is the per-file CPU-time budget shared by parsing and extraction: the CPU time of the thread doing the work, not the time on the clock. It bounds work on hostile syntax trees.
const ParseWallSafety = 60 * time.Second
ParseWallSafety is the wall-clock limit behind the CPU budget, far above it, for a host where even the CPU budget would take too long; it also bounds tree-sitter queries
Variables ¶
var ErrDepthLimit = errors.New("syntax tree exceeded the per-file depth limit")
ErrDepthLimit means the syntax tree exceeded MaxTreeDepth and is reported by Build as a resource limit in Coverage.LimitsHit
var ErrParseTimeout = errors.New("parse exceeded its per-file CPU-time budget")
ErrParseTimeout means parsing or extraction exceeded the per-file deadline and is reported by Build as a resource limit in Coverage.LimitsHit
var ErrTextLimit = errors.New("call text exceeded its per-file limit")
ErrTextLimit means extracted call text exceeded the budget in collectCalls and is reported by Build as a resource limit in Coverage.LimitsHit
Functions ¶
func AssignTargets ¶
AssignTargets returns the variable name(s) an assignment-shaped node writes to. The field is called "left" on every grammar's assignment expression, "name" on a declarator with an initializer (Go's var_spec, JS/TS/Java's variable_declarator) and "pattern" on Rust's let_declaration - trying all three in order needs no per-language switch.
func AssignValue ¶
AssignValue returns the expression an assignment-shaped node reads from: field "value" (a declarator with an initializer) or "right" (an assignment expression), unwrapping Go's expression_list to its first element for the common single-value case
func DefName ¶
defName returns a definition node's own name, falling back to the name of the variable it is assigned to for an anonymous function or arrow function: `const h = (args) => {...}` is findable as "h"
func HasImplicitReceiver ¶
HasImplicitReceiver reports whether Python binds the first parameter as a method receiver. Static methods and nested plain functions return false.
func LastSegment ¶
LastSegment reduces a dotted or path-scoped callee expression to its final identifier: "srv.AddTool" and "mcp::server::add_tool" both become their method name. Name-based resolution, registration discovery and the taint catalogue all match on that name, since receiver types are not tracked.
func LiteralValue ¶
LiteralValue decodes fixed strings, including Rust raw strings and Python's implicit concatenation, with optional parentheses; runtime expressions remain unresolved
func ParamBindings ¶
ParamBindings returns every name one parameter node actually binds. Go, Java, Rust and a plain JS/TS identifier parameter usually bind exactly one name (Go's compact `a, b string` binds two - see paramNamesOf, which this defers to).
func ParseTree ¶
ParseTree returns a raw tree under ParseTimeout and context cancellation. The caller owns a non-nil returned tree and must Close it.
func Resolve ¶
Resolve matches a call's final identifier without receiver-type information. A unique candidate wins; among several, one same-file definition wins. Other ambiguous calls stay unresolved.
func SplitTopLevel ¶
SplitTopLevel splits inner on commas that are not nested inside a bracket, brace, paren or quoted string, so a comma belonging to a nested literal never breaks up its enclosing element
func SplitTopLevelSep ¶ added in v1.2.0
SplitTopLevelSep is SplitTopLevel on another separator: int | None splits on '|'
func TemplateWithSubstitutions ¶ added in v1.2.0
TemplateWithSubstitutions returns a JavaScript template literal's body with every ${...} substitution replaced by repl, or false when a substitution is not closed. Escapes are decoded to the character they stand for.
func UnquoteString ¶
UnquoteString strips the quote syntax off a string-literal node's raw source text and reports whether the result is a fixed value. A template or f-string with an interpolation in it is not a fixed value: ok is false so callers do not mistake the template for the literal name it produces.
Types ¶
type FileResult ¶
type FileResult struct {
Path string
Language language.ID
Imports []graph.Import
Symbols []*graph.Symbol
Calls []graph.Call
// HasSyntaxErrors means tree-sitter produced a tree but part of it did
// not parse cleanly; the result below is still the best effort from the
// rest of the file
HasSyntaxErrors bool
// Constants holds unambiguous same-file string constants from Python, Java, Rust,
// JavaScript/TypeScript and Go
Constants map[string]string
// ObjectConstants holds the source text of unambiguous top-level JavaScript/TypeScript
// `const NAME = { ... }` object literals, for callers that read fields such as description
ObjectConstants map[string]string
// TemplateConstants holds the source text of unambiguous top-level JavaScript/TypeScript
// `const NAME = \`...${x}...\“ template literals that are not fixed strings
TemplateConstants map[string]string
}
FileResult is what parsing one file produces, before cross-file resolution
func ParseFile ¶
func ParseFile(ctx context.Context, adapter *language.Adapter, path string, src []byte) (*FileResult, error)
ParseFile parses one file and returns its extracted FileResult. Total failure, timeout, or cancellation returns an error; local syntax damage may still return a result whose syntax-error flag becomes a coverage gap.