tree-sitter-c-sharp

repository·master·Indexed 43 days ago

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

A high-performance, incremental C# grammar for the tree-sitter parsing engine, optimized for syntax highlighting and fragment parsing. It supports C# versions 1 through 14.0 and includes a Node.js binding with built-in query strings for highlights, injections, locals, and tags. The grammar is based on Roslyn but modified to handle preprocessor blocks and simplify tree structure.

Tokens
640
Snippets
2
Records
4
Agent score
6%

What's inside tree-sitter-c-sharp

  1. Overview of tree-sitter-c-sharp

    master

    tree-sitter-c-sharp is a C# grammar for the tree-sitter incremental parsing library. It is based on the Roslyn grammar but has been modified to:

    • Handle #if, #else, #elif, and #endif preprocessor blocks.
    • Support syntax highlighting and parsing of code fragments.
    • Simplify the output tree structure.
    • Reduce parser state count and complexity.
    • Address differences between parsing technologies and specific grammar bugs.
    • Align with tree-sitter conventions.
  2. C# language version support and limitations

    master

    The grammar provides comprehensive support for C# versions 1 through 14.0. However, users should be aware of the following current limitations:

    • Identifier usage: async, var, and await cannot be used as identifiers in every context where they are valid in standard C#.
    • File-based app directives: Preprocessor directives such as #:property, #:package, #:sdk, and #:project are not yet recognized.
  3. Use the tree-sitter-c-sharp Node.js binding

    master

    The tree-sitter-c-sharp package provides a Node.js binding for the C# grammar. When imported, it automatically loads the native .node binary and attaches several useful properties to the default export:

    1. Query Strings: The following properties are available as getters that lazily load the corresponding .scm query files from the package directory:
      • HIGHLIGHTS_QUERY: Syntax highlighting queries.
      • INJECTIONS_QUERY: Injection queries for embedding other languages.
      • LOCALS_QUERY: Queries for local scope information.
      • TAGS_QUERY: Queries for syntax tagging.
    2. nodeTypeInfo: Contains metadata about node types, loaded from src/node-types.json if available.

    To use it, simply import the default export from the package.

    import cSharpGrammar from 'tree-sitter-c-sharp';
    
    // Accessing query strings
    const highlights = cSharpGrammar.HIGHLIGHTS_QUERY;
    
    // Accessing node type information
    const typeInfo = cSharpGrammar.nodeTypeInfo;
  4. Reference the tree-sitter-c-sharp query properties

    master

    The binding exports the following query properties, which return the raw string content of the corresponding .scm files:

    • HIGHLIGHTS_QUERY (maps to queries/highlights.scm)
    • INJECTIONS_QUERY (maps to queries/injections.scm)
    • LOCALS_QUERY (maps to queries/locals.scm)
    • TAGS_QUERY (maps to queries/tags.scm)
    HIGHLIGHTS_QUERY
    INJECTIONS_QUERY
    LOCALS_QUERY
    TAGS_QUERY