This is my take on building a parser combinator library API in JavaScript. This library would have a very low surface area, and would be easy to learn for those who already know already know regex.
Parser combinators often create their own DSL (domain-specific language) to provide a less verbose way of declaring parsers. These DSLs have to support operators for things such as repetition, alternation, optionality, lookahead, and more. Some of them don't create DSLs and provide these as helper functions.
In the following link, there are JSON parser implementations done by several parser combinatiors/generators: https://chevrotain.io/performance/ Some of them are verbose, some of them are concise.
This parser combinator API I'm proposing will not provide a DSL, however it will result in very short parser declarations. Also they're arguably concise if you're comfortable with regular expressions. The idea is this: Regex is already a DSL on its own, and it supports many operators that a parser combinator may need:
- Repetition:
X*
(zero or more),X+
(one or more) - Optionality:
X?
- Alternation:
[XYZ]
,(X|Y|Z)
- Negation:
[^X]
- Positive and negative lookahead...
Therefore, they can be utilized to declare parsing rules. In the following snippet, I've demonstrated a JSON parser design using this technique.