Search & Discovery
Structural AST Search & Safe Rewrites (24 Languages)
Match and transform syntax trees across 24 Tree-sitter languages with trigram prefiltering and dry-run diffs.
lancet ast parses syntax trees with 24 compiled-in Tree-sitter grammars. Unlike plain regexes, patterns match true syntactic structure — ignoring whitespace differences, formatting, comments, and string contents.
Metavariables & Equality Constraints#
$VAR(named single node): captures one named AST node. Repeating the same metavariable (e.g.,$X == $Xor$X = $X) enforces structural equality across the two subtrees.$_(anonymous wildcard): matches any single named AST node without allocating or storing a named capture.$$$ARGS(variadic sibling sequence): matches 0 or more sibling AST nodes (function arguments, struct fields, statement blocks).- C-like statement metavariables: in C, C++, Rust, Go, JS/TS/TSX, Java, C#, etc., patterns like
if ($C) $Sand{ $$$B }bind statement and block subtrees.
Cross-Language AST Recipes#
Terminal
# Rust: preview replacing .unwrap() with ?
lancet ast '$X.unwrap()' src --lang rs --rewrite '$X?'
# Rust: find variadic panic! invocations
lancet ast 'panic!($$$ARGS)' src --lang rs
# Python: catch redundant self-comparisons (structural equality on $X)
lancet ast '$X == $X' . --lang py
# Go: rewrite fmt.Errorf wrapping patterns across non-test files
lancet ast 'fmt.Errorf($FMT, $ERR)' pkg/ --lang go --path '!*_test.go' --rewrite 'errors.Wrap($ERR, $FMT)'
# TypeScript / TSX: spot console.log calls
lancet ast 'console.log($$$ARGS)' web/ --lang tsxSafe Rewrites & Atomic --apply#
- Preview first: passing
--rewrite TEMPLATEprints unified diffs (mode: "dry-run") without modifying disk. - Inspect
skipped_overlaps: when one match is nested inside another match in the same file (for example nested$X.unwrap().foo.unwrap()), Lancet keeps the earliest/outermost edit and skips overlapping inner edits cleanly rather than corrupting offsets; checkskipped_overlapsand re-run once for inner leftovers. - Apply atomically: add
--applyto write each file through Lancet's Tree-sitter syntax gate and atomicwrite_atomicwriter.
Important constraints & ast-grep differences
--applyrefuses--limit: Lancet refuses capped partial rewrites so you don't mutate the first N matches accidentally; narrow with--pathglobs instead.- Strict
ifshape:if ($C) $Smatches plainifwithout anelsebranch (unlikeast-grep, which matchesif ... elsetoo). --lang cppincludes.h: C++ AST searches inspect.hheaders in addition to.cpp/.cc/.cxx/.hpp.- Literal-free patterns: patterns without any 3+ byte literal cannot prune via
.lancetand must parse all language files (~0.6–1.0×ast-grepspeed); worst-case$$$backtracking is capped by memoization and a step budget.