GritQL Documentation

repository·main·Indexed 26 days ago

https://github.com/biomejs/gritql

GritQL is a declarative query language for searching and modifying source code across multiple languages, including JavaScript/TypeScript, Python, JSON, Java, Terraform, Solidity, CSS, Markdown, YAML, Rust, Go, and SQL. It provides a high-performance alternative to regex and AST-based codemods, featuring snippet-based querying, auto-fix capabilities, and a modular pattern system. The project includes the Grit CLI, Python bindings, and a core pattern-matching engine implemented in Rust.

Tokens
28.6K
Snippets
97
Records
273
Agent score
89%

What's inside GritQL

  1. Overview of tree-sitter-solidity

    main

    The tree-sitter-solidity repository provides an efficient, low-dependency parser for Solidity using the tree-sitter framework. It is designed to support most Solidity versions currently in use and is specifically optimized to enable metaprogramming.

    Warning: This grammar is currently in development. The structure of the generated Abstract Syntax Tree (AST) is not yet stable and may change.

  2. Overview of GritQL

    main

    GritQL is a declarative query language designed for searching and modifying source code. It allows users to perform complex code transformations across multiple target languages including JavaScript/TypeScript, Python, JSON, Java, Terraform, Solidity, CSS, Markdown, YAML, Rust, Go, and SQL. Key features include:

    • Snippet-based querying: Any code snippet can be used as a valid GritQL query without needing deep AST knowledge.
    • Scalability: Built with Rust and query optimization to handle repositories with over 10 million lines of code.
    • Modular patterns: Access over 200 standard patterns via a built-in module system or share custom patterns.
    • Auto-fix capabilities: Supports the creation of auto-fix rules for automated code remediation.
  3. Understand the `grit-pattern-matcher` crate architecture

    main

    The grit-pattern-matcher crate provides the core pattern definitions used by the GritQL engine. It defines a Matcher trait that is implemented by various patterns to handle matching logic.

    Crucially, this crate is designed to be independent of TreeSitter, allowing it to be reused by other engines that utilize different parsing infrastructures.

  4. TOML Specification Overview

    main

    TOML (Tom's Obvious, Minimal Language) is a minimal configuration file format designed to map unambiguously to a hash table. It is case-sensitive and must be a valid UTF-8 encoded Unicode document.

    Key syntax rules:

    • Whitespace: Tab (0x09) or space (0x20).
    • Newline: LF (0x0A) or CRLF (0x0D 0x0A).
    • Comments: A hash symbol (#) marks the rest of the line as a comment, except when inside a string. Control characters (other than tab) are not permitted in comments.
  5. Getting started with the Elixir Tree-sitter parser

    main

    The Elixir parser is built using Tree-sitter. Language rules are defined in grammar.js, which generates the parser code located in src/. Custom C code can be used for specialized tokenization in src/scanner.c.

    To resolve grammar conflicts, you can assign precedence and associativity (e.g., prec.left) to rules. For genuine ambiguities that require more than one token of lookahead, add the conflicting rules to the conflicts list in the grammar to utilize the GLR algorithm, or use dynamic precedence (prec.dynamic).