{"library":"parser-ts","title":"TypeScript Parser Combinators (parser-ts)","description":"`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.","language":"javascript","status":"active","last_verified":"Tue Apr 21","install":{"commands":["npm install parser-ts"],"cli":null},"imports":["import { string } from 'parser-ts/string'","import { Parser, run } from 'parser-ts/Parser'","import { pipe } from 'fp-ts/function'"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import { pipe } from 'fp-ts/function';\nimport * as P from 'parser-ts/Parser';\nimport * as S from 'parser-ts/string';\nimport * as C from 'parser-ts/char';\nimport * as E from 'fp-ts/Either';\n\n// Define a parser for one or more digits, then convert to an integer\nconst integerParser = pipe(\n  S.recognize(C.many1(C.digit)), // `recognize` captures the matched string\n  P.map(parseInt)              // `map` transforms the parsed string to a number\n);\n\n// Define a parser for a literal comma character\nconst commaParser = C.char(',');\n\n// Define a parser for a list of integers separated by commas\n// `sepBy` applies `integerParser`, consuming `commaParser` in between\nconst commaSeparatedIntegersParser = pipe(\n  integerParser,\n  P.sepBy(commaParser)\n);\n\n// Helper function to run the parser and log results\nconst parseNumbers = (input: string) => {\n  console.log(`\\nAttempting to parse: \"${input}\"`);\n  const result = P.run(commaSeparatedIntegersParser, input);\n  E.match(\n    (error) => console.error('Parse Error:', error.message, 'at position', error.offset),\n    ([value, remaining]) => console.log('Parsed Value:', value, '| Remaining Input:', remaining.length === 0 ? 'None' : `\"${remaining}\"`) \n  )(result);\n};\n\n// Example Usage\nparseNumbers('1,2,3,4');\nparseNumbers('100');\nparseNumbers('1, 2, 3'); // Fails due to unexpected space\nparseNumbers('abc');\nparseNumbers('1,2,abc');","lang":"typescript","description":"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.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}