`parser-ts` is a library providing string parser combinators for TypeScript, heavily influenced by the `purescript-eulalie` library and built upon the foundational `fp-ts` functional programming toolkit. It enables developers to construct complex parsers by combining simpler parsing functions in a declarative manner, leveraging `fp-ts`'s algebraic data types and functional patterns. The current stable version is 0.7.0, with releases occurring periodically to address bugs, introduce new combinators, and align with `fp-ts` peer dependency updates. Its key differentiators include its strong TypeScript typing, functional purity, and close integration with the `fp-ts` ecosystem, making it suitable for applications requiring robust and composable parsing logic within a functional TypeScript codebase. The library primarily focuses on string parsing and is often used for creating DSLs, configuration file parsers, or simple language frontends.
npm install parser-tsVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to define a parser for a comma-separated list of integers using basic combinators like `char`, `many1`, `recognize`, `map`, and `sepBy`, and then run it against various inputs.
Upgrade `fp-ts` to a compatible version (`^2.14.0` or later) in your project: `npm install fp-ts@^2.14.0` or `yarn add fp-ts@^2.14.0`.
Ensure you are using `parser-ts` version 0.6.12 or newer. This version (and subsequent ones) includes fixes that re-implement recursive parsers using `ChainRec` to prevent stack overflows on long inputs.
Familiarize yourself with core `fp-ts` concepts, especially `Either`, `Option`, `pipe`, and basic type classes. The `fp-ts` documentation and examples are excellent resources for understanding the underlying patterns.
Install `fp-ts` directly into your project: `npm install fp-ts@^2.14.0` or `yarn add fp-ts@^2.14.0`. Always check `parser-ts`'s `peerDependencies` for the exact compatible version range.
Use `fp-ts/Either`'s combinators like `match`, `fold`, `isLeft`, or `isRight` to safely access the parsed value. For example: `E.match(() => console.error('Error'), ([value, _]) => console.log('Parsed:', value))(result)`.Debug your parser step-by-step. Use `P.run` with smaller, isolated parts of your input and parser definition to identify where the failure occurs. Pay close attention to optional parsers (`P.optional`) and sequence combinators (`P.apS`, `P.sequenceS`) to ensure they match your input's structure.