type-sitter
TypeScript icon, indicating that this package has built-in type declarations

0.1.6 • Public • Published

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.

Package Sidebar

Install

npm i type-sitter

Weekly Downloads

0

Version

0.1.6

License

MIT

Unpacked Size

13.4 kB

Total Files

5

Last publish

Collaborators

  • sepehr-laal