grep-ast
grep-ast is a command-line tool designed to search through the Abstract Syntax Tree (AST) of source code files. Unlike traditional `grep` which operates on text, `grep-ast` provides a context-aware output, showing how matching lines fit into the broader code structure (functions, classes, etc.). It leverages `tree-sitter` and `tree-sitter-languages` for parsing various programming languages. The current version is 0.9.0, released in May 2025.
Warnings
- gotcha This `grep-ast` tool is distinct from the `ast-grep` (Rust-based) project, despite similar names and AST-related functionality. `grep-ast` focuses on text-based regex search with AST-aware output, while `ast-grep` (and its Python binding `ast-grep-py`) uses AST patterns for search and rewrite.
- gotcha The search patterns provided to `grep-ast` are regular expressions applied to the *text* of the code, not directly to AST nodes. The AST is used to provide context for the matched text lines, not for pattern matching itself.
- gotcha By default, `grep-ast` recursively searches the current directory and respects `.gitignore`. This might lead to unexpected results if you intend to search files outside the Git repository's scope or want to override `.gitignore` rules.
Install
-
pip install grep-ast
Quickstart
grep-ast "function_name" my_module.py grep-ast "class MyClass" .