The chain method takes a parser as an argument, and return a new parser.
the next function
To be able to do further operations on the result of a parser provided that the status of the parser matches the given status, next method have been added to take the resulting state and operates on it.
It simply "chains" a new parser to the current one, which - depending on status value - returns the state that was passed to it as it is, or the state returned from fn.
To simplify working with next, two method have been added.
Well, that that doesn't look very useful right now, but in the next post, basic parsers will be implemented using the parser class, and finally they can be "combined" together to make larger parsers.
You can find the full code on github on main branch
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]