In the previous post, an implementation of the parser class have been introduced, and in this post will be about some basic parsers.
If the parsing process broke down to it's simplest components, a pattern will be found in these components that represent the simplest operations the parser can do, then they can be combined to form larger and more complicated patterns.
First, we need the most basic and the most important of all. a parser to match one character.
the char parser
It matches one character in the input string.
We need to define a parser using our Parser class from before.
you can read the source in src/. it's self documenting and easy to read.
here is a simple overview.
import{char,firstOf,sequence,zeroOrOne,oneOrMore,zeroOrMore}from'pari';// the `char` parser matches one char.// it take a `regex` that matches exactly one char.constdigit=char('[0-9]');// `firstOf` parser returns the first match in a list of parsers.constlowerCase=char('[a-z]');constdigitOrLwcase=firstOf([digit,lowerCase]);// `sequence` parser matches a list of parsers in sequence.consthex=char('[0-9a-fA-F]');constbyteHex=sequence([char('0'),char('x'),hex,hex]