Parser combinators are presented as a pragmatic middle ground between brittle regexes and heavyweight parsers like tree-sitter: small, composable functions that consume a prefix of an input string and return a tuple of a parsed value plus the remaining string. A “partial parser” can be any function that follows that contract (it may itself use regex). By chaining partial parsers you stepwise consume input; wrapping those chains produces new parsers, enabling reusable, declarative parsers built from simple pieces. TypeScript can express the necessary variadic types, though getting the types right is fiddly.
Concrete combinators and patterns are explained and demonstrated: pSeq generalizes sequential application of parsers and returns a tuple of results; pMap maps a parser’s tuple result into a richer structured object (so tuples like ["Hello", null, 100] become {greeting, who}); pRepeat applies a parser repeatedly until it fails (the combinatorial analogue of regex repetition). The functional dual of reduce - an unfold - is used to describe splitting input into tokens. Examples include integer, hello, whitespace parsers and a full example of parsing ping output into a PingResult with header, packets and statistics, showing how combinators turn messy text into typed structured data.
Summary generated by AI from the linked article. hn.today is not affiliated with Hacker News or Y Combinator.