百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
T

tree-grepper

> 编程语言
开源

与 grep 类似,但使用树座语法进行搜索

326 stars0 点赞0 次浏览
访问官网GitHub

工具介绍

与 grep 类似,但使用树座语法进行搜索

⚠️ Unmaintained ⚠️

I haven't used this tool in a real way in several years, and don't have the bandwidth to commit to continuing to maintain it. As such, this repo is archived. You're free to continue to using tree-grepper, of course, but what you see here is what you get.

tree-grepper

Works like grep, but uses tree-sitter to search for structure instead of strings. Here's a longer introduction to the tool as a blog post.

Installing

Use nix to install:

  1. if you have cachix installed, cachix use tree-grepper.
  2. nix-env -if https://github.com/BrianHicks/tree-grepper/archive/refs/heads/main.tar.gz

Usage

Use it like grep (or really, more like ack/ag/pt/rg.)

bash
$ tree-grepper -q javascript '(call_expression)'
./tests/cmd/hello-world.js:1:1:query:console.log("Hello, World!")

By default, tree-grepper will output one match per (newline-delimited) line. The columns here are filename, row, column, match name, and match text.

Note, however, that if your query includes a match with newlines in the text they will be included in the output! If this causes problems for your use case, try asking for JSON output (-f json) instead.

tree-grepper uses Tree-sitter's s-expressions to find matches. See the tree-sitter docs on queries for what all you can do there.

We add one important thing on top of the standard query stuff (including #eq? and #match?): if you name a pattern starting with an underscore, it will not be returned in the output. This is primarily useful for filtering out matches you don't really care about. For example, to match JavaScript calls to require but not other functions, you could do this:

scheme
(call_expression (identifier)@_fn (arguments . (string)@import .) (#eq? @_fn require))

In addition to text output, we support JSON output for scripting: just specify -f json. You also get more info (the match's end location and node kind) by asking for JSON output.

Tree View

You can discover the node names your language uses by using --show-tree languagename path/to/file. When you do this, tree-grepper will parse the file and print out an indented tree view.

For a file like this:

javascript
console.log("Hello, World!");

You'll get a tree like this:

bash
$ tree-grepper --show-tree javascript tests/cmd/hello-world.js
program 1:1
  expression_statement 1:1
    call_expression 1:1
      member_expression 1:1
        identifier 1:1: console
        . 1:8: .
        property_identifier 1:9: log
      arguments 1:12
        ( 1:12: (
        string 1:13
          " 1:13: "
          string_fragment 1:14: Hello, World!
          " 1:27: "
        ) 1:28: )
    ; 1:29: ;

Each line takes the format {node name} {location} {source, if present}. Source is only shown for the leaf-most nodes on the tree to avoid printing a huge block. However, tree-grepper can extract text from any of these nodes.

You can use the node names in queries. For example:

  • tree-grepper -q elm (exposed_value) would have matches on average and percentOf.
  • tree-grepper -q elm (module_declaration) would match on the whole declaration, module Math exposing (average, percentOf)

Supported Languages

You can find all the languages in your installed version of tree-grepper by running tree-grepper --languages. The output is the literal language strings you can use in queries or --show-tree. For example, here's the list for the current version of tree-grepper:

bash
$ tree-grepper --languages
c
cpp
cuda
elixir
elm
go
haskell
java
javascript
markdown
nix
php
powershell
python
ruby
rust
sass
typescript

Don't see your favorite? We're open to PRs for adding whatever language you'd like!

For development, there's a nix-shell setup that'll get you everything you need. Set up nix (just Nix, not NixOS) and then run nix-shell in the root of this repository.

After that, you just need to add a tree-sitter grammar to the project. The tree-sitter project keeps an up-to-date list, so you may not even need to write your own!

Note: when you're adding grammars, please keep things in alphabetical order.

  1. Add your grammar as an input in flakes.nix, following the template of the ones already there. You'll need to add an entry in inputs and another in the updateVendor script.
  2. Run direnv reload to make sure you have the latest changes, then update-vendor to get your grammar in the right place. Make sure the repo content under vendor/YOUR-GRAMMAR looks how you expect.
  3. Set up compilation in build.rs by following the pattern there.
  4. Set up a new target in src/language.rs by following the patterns there.
  5. Add the language to the list of supported languages in this readme.

License

See LICENSE in the source.

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Rusthacktoberfest

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言