Parser combinator is the method that use the principles of functional programming to compose the primitive parsers into larger ones.
To demo how is the composing works, let's build a parser that accepts either a digit or a letter.
We can start by building primitive parsers for digits and letter:
// primitive parser const digit = (c) => !isNaN(+c) && +c >= 0 && +c <= 9; const letter = (c) => c.match(/[a-zA-Z]/) !== null;
The combinator we need to use is an
// combinator const or = (left, right) => c => left(c) || right(c);
Finally, combine them together:
// combined parser const isDigitOrLetter = or(digit, letter);
It's important to note that, the above example is not the way a parser actually works, as it's lack of the mechanism to handle error, and the way to traversal to next chunk of input.
For a more detailed example, check: Parser Combinator Demo on Gist.
Markdown parser using Nom
#rust #nom #parser Source: https://github.com/HGHimself/prose/blob/master/src/markdown.rs This is a source code of a Markdown Parser…
#rust #parser #nom Nom is a parser library written by VLC developers to help them speed-up and improve the reliable of their media file…
Welcome! Look like you've found my personal notebook. This is the place where you can take a peek into my mind to see what I've been…
If you think this note resonated, be it positive or negative, please feel free to send me an email and we can talk.