A TypeScript parser combinator library for building type-safe parsers with declarative grammar definitions.
This project acts as a toolkit for quickly defining type-safe grammars, which can be used to parse input text directly into valid ASTs.
Unlike most parser toolchains, this library generates TypeScript types for all AST nodes in the grammar, and automatically constructs a typed AST directly from the input source text.
This is achieved by a combination of the following:
-
Parser Combinators (the foundation): A low-level library of composable parsing primitives
-
BNF-style Syntax (syntactic sugar): A high-level declarative grammar definition DSL for rapidly defining grammars and AST node construction rules
Fun fact: The DSL syntax is itself parsed using a grammar constructed from the underlying parser combinators. Meta!
-
Codegen (type definitions): TypeScript types are automatically generated based on semantic analysis of grammar source files
npm install @timkendrick/syntax-
Define a grammar using the
syntaxfunction:import { syntax } from '@timkendrick/syntax'; const parser = syntax(` NUMBER ::= /\\d+/ PLUS ::= "+" <Expression> ::= { left: <- NUMBER, : PLUS, right: <- NUMBER } `);
-
Use the parser to parse input text into an AST:
const rootNode = parser.parse("42+17"); // rootNode.type === "Expression" // rootNode.properties.left === "42" // rootNode.properties.right === "17" // Manually create an AST node: const node = parser.nodes.Expression("3", "4");
-
Generate TypeScript type definitions for grammar files using the
syntax-codegentool (optional):npx @timkendrick/syntax-codegen ./path/to/file.grammar.js
Ensure the generated TypeScript types are visible within your grammar file:
import './file.grammar.generated.d.ts';
- Declarative Grammar Syntax: Define grammars using an intuitive, BNF-like syntax
- Full Type Safety: Automatically inferred TypeScript types for tokens and AST nodes
- Parser Combinators: Composable parsing primitives for building complex grammars
- Code Generation: Automatic generation of type definitions from grammar files
- Typed Node Factories: Automatic generation of AST node factories from grammar files
- Human-friendly Errors: Parse errors include source snippets with error locations
For full documentation on the grammar definition DSL, see the syntax package README.
See the packages/syntax-examples/ directory for complete working examples:
pnpm install- Install dev dependenciespnpm verify- Run linting and testspnpm test- Run testspnpm test:watch- Run tests in watch mode
For detailed information about the library's design and implementation, see packages/syntax/ARCHITECTURE.md.
MIT