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
-
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;
-
Express your DSL that goes on top of your base grammar with types.
export interface Grammar { // ... grammar definition using TypeScript typings }
-
Use
type-sitter
to generate a Tree-Sitter grammar.# outputs grammar.js and grammar.json type-sitter -i grammar.ts
-
Use
tree-sitter
to make a Tree-Sitter parser.# outputs the actual parser tree-sitter generate
-
Optionally use
grammar.js
to further modify the parser andgrammar.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.