Skip to content

Generates Tree-Sitter Grammars From TypeScript Definitions

License

Notifications You must be signed in to change notification settings

3p3r/type-sitter

Repository files navigation

type-sitter

The Industrial Parser Generator. If you can write a JSON Schema for it, this can generate a parser for it.

Generates Tree-Sitter Grammars From TypeScript Definitions. Inspired By Rust-Sitter.

usage

  1. Write a base grammar that supports TypeScript primitives:
    any, array, null, number, object, string, bool.

    // use API of GrammarBuilder to define a base grammar with placeholders.
    // placeholders in the returned base grammar is resolved by type-sitter.
    export const baseGrammar: ($$: GrammarBuilder) => string;
  2. Express your DSL that goes on top of your base grammar with types.

    export interface Grammar {
      // ... grammar definition using TypeScript typings
    }
  3. Use type-sitter to generate a Tree-Sitter grammar.

    # outputs grammar.js and grammar.json
    type-sitter -i grammar.ts
  4. Use tree-sitter to make a Tree-Sitter parser.

    # outputs the actual parser
    tree-sitter generate
  5. Optionally use grammar.js to further modify the parser and grammar.json to validate ASTs sent over the wire.

See caveats for limitations.
See type-sitter --help for more information.
See examples directory for sample DSLs.

caveats

  • Not all TypeScript features are supported. See quicktype for details.
  • Duplicated pairs with the same keys but different values do not error out.
  • Generated parsers do not replace a data validation layer. Those are mutex.
  • This project currently focuses on declarative DSLs vs imperative grammars.