parser

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Nov 8, 2026 License: MIT Imports: 19 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

Index

Constants

View Source
const (
	FallbackTimeout   = "timeout"
	FallbackLarge     = "large"
	FallbackGenerated = "generated"
	FallbackMinified  = "minified"
)

Fallback reasons recorded on FileInfo.Fallback and in the scan report.

Variables

This section is empty.

Functions

func IsDataLanguage added in v0.5.0

func IsDataLanguage(lang string) bool

IsDataLanguage reports whether lang is config or build data, which should not decide a project's or module's primary language.

func LeafHash added in v0.5.0

func LeafHash(path string, content []byte) []byte

LeafHash is the hash used both as Merkle leaf and cache key.

Types

type Cache added in v0.5.0

type Cache interface {
	Get(key string) (*FileInfo, bool)
	Put(key string, info *FileInfo)
}

Cache stores parse results keyed by a content-and-path hash so unchanged files skip parsing on the next run.

type FileInfo

type FileInfo struct {
	Path         string            `json:"path"`
	Language     string            `json:"language"`
	Imports      []string          `json:"imports,omitempty"`
	Exports      []string          `json:"exports,omitempty"`
	TypeDefs     map[string]string `json:"type_defs,omitempty"` // type name -> brief definition
	LineCount    int               `json:"line_count"`
	IsEntrypoint bool              `json:"is_entrypoint,omitempty"`
	Fallback     string            `json:"fallback,omitempty"` // why tree-sitter was skipped, see limits.go
}

FileInfo holds the result of parsing a source file.

func ParseAll

func ParseAll(paths []string) ([]*FileInfo, []error)

ParseAll parses multiple files and returns all results. Errors from individual files are collected and non-fatal.

func ParseContent added in v0.5.0

func ParseContent(path string, content []byte) (info *FileInfo, err error)

ParseContent runs the registered parsers on in-memory content. A panic in any parser is converted into an error so one bad file cannot kill a scan.

func ParseContentWith added in v0.5.0

func ParseContentWith(path string, content []byte, lim Limits) (info *FileInfo, err error)

ParseContentWith is ParseContent under cost limits.

func ParseFile

func ParseFile(path string) (*FileInfo, error)

ParseFile reads a file from disk and dispatches to the appropriate parser.

type GenericParser

type GenericParser struct{}

GenericParser is a fallback parser that identifies languages by file extension but does not perform import/export analysis.

func (*GenericParser) CanParse

func (g *GenericParser) CanParse(_ string) bool

CanParse always returns true — the generic parser handles any file.

func (*GenericParser) Parse

func (g *GenericParser) Parse(path string, content []byte) (*FileInfo, error)

Parse returns path, language (from extension), and line count only.

type GoParser

type GoParser struct{}

GoParser parses Go source files using the stdlib go/parser + go/ast.

func (*GoParser) CanParse

func (g *GoParser) CanParse(filename string) bool

CanParse returns true for .go files that are NOT test files.

func (*GoParser) Parse

func (g *GoParser) Parse(path string, content []byte) (*FileInfo, error)

Parse extracts imports and exported names from a Go source file.

type Limits added in v0.5.0

type Limits struct {
	ParseTimeout time.Duration // wall-clock budget per file
	MaxBytes     int64         // largest file given to tree-sitter
	MaxLineLen   int           // longest line before a file counts as minified or generated
	Generated    bool          // route files marked as generated code to the lightweight path
}

Limits bound the cost of structural (tree-sitter) parsing. Files that exceed them are indexed with the lightweight generic parser instead, so one huge or pathological file cannot stall or bloat a whole scan. Zero disables a limit.

type PanicError added in v0.5.0

type PanicError struct {
	Path  string
	Value any
	Stack string
}

PanicError reports a recovered parser panic.

func (*PanicError) Error added in v0.5.0

func (e *PanicError) Error() string

type Parser

type Parser interface {
	CanParse(filename string) bool
	Parse(path string, content []byte) (*FileInfo, error)
}

Parser extracts structured information from a source file.

type PoolOptions added in v0.5.0

type PoolOptions struct {
	Root        string // files are read from Root/<rel>
	Workers     int    // pool size; values below 1 mean 1
	MaxFileSize int64  // content larger than this is rejected (race with growth); 0 disables
	Cache       Cache  // optional
	// Visit sees each file's bytes once, inside the worker, so callers can
	// extract extra data without keeping every file in memory. It must be
	// safe for concurrent use.
	Visit func(rel string, content []byte)
	// MemoryBudget caps the estimated working set of concurrent tree-sitter
	// parses, in bytes. One file larger than the budget still runs, alone.
	// 0 disables the cap.
	MemoryBudget int64
	// Limits routes expensive files to the lightweight parser.
	Limits Limits
	// contains filtered or unexported fields
}

PoolOptions configures ScanFiles.

type Scanned added in v0.5.0

type Scanned struct {
	Path     string
	Info     *FileInfo
	Leaf     []byte // sha256(path || content), the Merkle leaf and cache key
	Err      error
	Cached   bool
	Panicked bool
	Dur      time.Duration // parse time, zero for cache hits
}

Scanned is the per-file outcome of ScanFiles.

func ScanFiles added in v0.5.0

func ScanFiles(files []string, o PoolOptions) []Scanned

ScanFiles parses files with a bounded worker pool. At most Workers file bodies are in memory at once. Results are returned in input order, so output does not depend on scheduling.

type TreeSitterParser added in v0.2.0

type TreeSitterParser struct{}

TreeSitterParser uses tree-sitter ASTs to extract structured info from source files. Handles all languages that have tree-sitter grammars, except Go (which uses stdlib AST).

func (*TreeSitterParser) CanParse added in v0.2.0

func (p *TreeSitterParser) CanParse(filename string) bool

func (*TreeSitterParser) Parse added in v0.2.0

func (p *TreeSitterParser) Parse(path string, content []byte) (*FileInfo, error)

func (*TreeSitterParser) ParseWithTimeout added in v0.5.0

func (p *TreeSitterParser) ParseWithTimeout(path string, content []byte, timeout time.Duration) (*FileInfo, error)

ParseWithTimeout is Parse with a wall-clock budget. When the budget runs out the partial tree is discarded and the file gets the generic parser's result, marked with FallbackTimeout.

Jump to

Keyboard shortcuts

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