parser

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2026 License: Apache-2.0 Imports: 13 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

View Source
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.

View Source
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.

View Source
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.

View Source
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

View Source
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

View Source
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

View Source
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

func AssignTargets(node *sitter.Node, src []byte) []string

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

func AssignValue(node *sitter.Node, src []byte) *sitter.Node

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

func DefName(id language.ID, node *sitter.Node, src []byte) string

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

func HasImplicitReceiver(id language.ID, def *sitter.Node, src []byte) bool

HasImplicitReceiver reports whether Python binds the first parameter as a method receiver. Static methods and nested plain functions return false.

func LastSegment

func LastSegment(text string) string

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

func LiteralValue(n *sitter.Node, src []byte) (string, bool)

LiteralValue decodes fixed strings, including Rust raw strings and Python's implicit concatenation, with optional parentheses; runtime expressions remain unresolved

func ParamBindings

func ParamBindings(id language.ID, n *sitter.Node, src []byte) []string

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 ParamList

func ParamList(node *sitter.Node) *sitter.Node

func ParseTree

func ParseTree(ctx context.Context, adapter *language.Adapter, src []byte) (*sitter.Tree, error)

ParseTree returns a raw tree under ParseTimeout and context cancellation. The caller owns a non-nil returned tree and must Close it.

func Resolve

func Resolve(g *graph.Graph)

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

func SplitTopLevel(inner string) []string

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

func SplitTopLevelSep(inner string, sep byte) []string

SplitTopLevelSep is SplitTopLevel on another separator: int | None splits on '|'

func TemplateWithSubstitutions added in v1.2.0

func TemplateWithSubstitutions(body, repl string) (string, bool)

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

func UnquoteString(text string) (value string, ok bool)

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.

type Receiver

type Receiver struct {
	Text     string
	CallText string
	CallArgs []graph.Arg
}

Receiver is the callee's own receiver as either plain text or a nested call with arguments. Preserving the shape lets taint analysis inspect a call-shaped receiver without guessing.

func CalleeAndArgs

func CalleeAndArgs(id language.ID, call *sitter.Node, src []byte) (calleeText string, receiver Receiver, args []graph.Arg)

CalleeAndArgs pulls the callee expression text, the callee's own receiver and the call's arguments out of a call-shaped node

Jump to

Keyboard shortcuts

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