tree-sitter-c Documentation

repository·master·Indexed 18 days ago

https://github.com/tree-sitter/tree-sitter-c

A concrete syntax tree parser for the C programming language based on the C99 ISO/IEC 9899:1999 standard. Designed for the tree-sitter framework, it provides high-performance incremental parsing and includes Node.js bindings with query strings for syntax highlighting, injections, locals, and tags.

Tokens
589
Snippets
3
Records
4
Agent score
14%

What's inside tree-sitter-c

  1. Use eslint-config-treesitter for linting

    master

    To apply Tree-sitter based linting rules to your project, import eslint-config-treesitter and spread its configuration into your eslint.config.mjs (or equivalent ESLint flat config file). This provides a pre-configured set of rules designed for Tree-sitter environments.

    import treesitter from 'eslint-config-treesitter';
    
    export default [
      ...treesitter,
    ];
  2. Use the tree-sitter-c Node.js binding

    master

    The tree-sitter-c package provides the Node.js bindings for the C language grammar. When imported, it provides the core binding object which includes access to the C grammar's query files for syntax highlighting, injections, locals, and tags. The binding automatically handles loading the correct prebuilt binary for your platform and architecture.

    import treeSitterC from 'tree-sitter-c';
    
    // The binding object contains the grammar and query strings
    console.log(treeSitterC.HIGHLIGHTS_QUERY);
  3. Access C grammar query strings

    master

    The tree-sitter-c binding exposes several query strings used for advanced syntax processing. These are loaded lazily from the package's internal .scm files when accessed:

    • HIGHLIGHTS_QUERY: Used for syntax highlighting.
    • INJECTIONS_QUERY: Used for language injections.
    • LOCALS_QUERY: Used for identifying local variables/scopes.
    • TAGS_QUERY: Used for semantic tagging.

    Note: These properties are defined as getters and will read the file from disk upon first access.

    // Example of accessing available query properties
    const highlights = treeSitterC.HIGHLIGHTS_QUERY;
    const injections = treeSitterC.INJECTIONS_QUERY;
    const locals = treeSitterC.LOCALS_QUERY;
    const tags = treeSitterC.TAGS_QUERY;