ParseBox is a TypeScript library that introduces parser combinators for seamless parsing in both Static and Runtime environments. By leveraging declarative structures, it ensures consistent parsing across domains, leading to cleaner and more maintainable code. Whether for experimentation or enhancing high-performance DSL parsing, ParseBox brings clarity and efficiency to your TypeScript projects.
ParseBox is an innovative TypeScript library designed for creating parser combinators that facilitate the construction of domain-specific languages (DSLs). It integrates seamlessly into the TypeScript type system, providing a rich set of declarative parsing utilities to ensure a consistent parsing experience across both static and runtime environments.
Key Features
- Symmetric Parsing: Ensures that parsing rules are consistent across different execution contexts, allowing you to write parsers once and utilize them both at runtime and for type checking.
- Declarative Combinators: Offers primary combinators such as
Const
,Tuple
, andUnion
that enable you to define complex parsing structures with ease, mimicking the expressive power of Backus-Naur Form (BNF).
Quick Example
To illustrate how you can use ParseBox effectively, here’s a brief example:
import { Runtime, Static } from '@sinclair/parsebox';
// Runtime Parser Example
const T = Runtime.Tuple([
Runtime.Const('X'),
Runtime.Const('Y'),
Runtime.Const('Z')
]);
const R = Runtime.Parse(T, 'X Y Z W'); // Outputs: [['X', 'Y', 'Z'], ' W']
// Static Parser Example
type T = Static.Tuple<[
Static.Const<'X'>,
Static.Const<'Y'>,
Static.Const<'Z'>
]>
type R = Static.Parse<T, 'X Y Z W'>; // Outputs: [['X', 'Y', 'Z'], ' W']
Comprehensive API
ParseBox consists of a wide variety of parsing utilities, including:
- Terminals: Parse basic data types such as numbers, strings, and identifiers.
- Mapping Capabilities: Implement semantic actions that transform parsed structures into more complex representations like Abstract Syntax Trees (ASTs).
- Contextual Parsing: Pass context values to manage dynamic parsing requirements informed by external states.
- Advanced Parsing Techniques: Supports LL(1) parsing strategies to avoid infinite left recursion while handling complex grammars.
ParseBox is meticulously crafted for high-performance parsing, making it suitable for research and real-world applications in TypeScript. It is designed with compatibility in mind, supporting TypeScript versions as low as 4.9.5 to provide a seamless developer experience.
Join the community and start leveraging the power of ParseBox to streamline your parsing tasks in TypeScript!