Logo

14.2 Parsing operators

PetitParser is based on scannerless parsing. This means that even the most basic of parsers are parsers too. PetitParser offers several of these basic parsers, also called terminals. Here are some examples:

Terminals Description
$a asParser Parses the character $a.
’abc’ asParser Parses the string ’abc’.
#any asParser Parses any character.
#digit asParser Parses the digits 0..9.
#letter asParser Parses the letters a..z and A..Z.
#cr asParser Parses the carriage-return character.
#lf asParser Parses the line-feed character.
#space asParser Parses the space character.
#tab asParser Parses the tabulator character.
nil asParser The empty parser.

More readily available terminals can be found on the class side of the PPPredicateObjectParser class, in the factory-chars protocol.

Combinators Description
p1 , p2 Parses p1 followed by p2 (sequence).
p1 / p2 Parses p1, if that doesn’t work parses p2 (ordered choice).
p star Parses zero or more p.
p plus Parses one or more p.
p optional Parses p if possible.
Predicates Description
p and Parses p but does not consume its input.
p not Parses p and succeeds when p fails, but does not consume its input.
p negate Parses any input token but the receiver.
p end Parses p and succeeds at the end of the input.

To manipulate the output of a parser, PetitParser offers actions that we can use to decorate the parser.

Actions Description
p ==> aBlock Performs the transformation given in aBlock.
p flatten Creates a string from the result of p.
p token Creates a token from the result of p.
p trim Trims all whitespaces before and after p.
p trimBlanks Trims the blanks before and after p.

Add a Note