Parser
recursive
Create a recursive parser. Used in combination with recurse(Parser).
recurse
Recurse on a parser. Used in combination with {@see recursive()}. After calling this method, this parser behaves like a regular parser.
run
Run the parser on an input
optional
Optionally parse something, but still succeed if the thing is not there.
or
Try the first parser, and failing that, try the second parser. Returns the first succeeding result, or the first failing result.
Caveat: The order matters! string('http')->or(string('https')
followedBy
Parse something, then follow by something else. Ignore the result of the first parser and return the result of the second parser.
sequence
Parse something, then follow by something else. Ignore the result of the first parser and return the result of the second parser.
then
Parse something, then follow by something else. Ignore the result of the first parser and return the result of the second parser. Alias for sequence().
bind
Create a parser that takes the output from the first parser (if successful) and feeds it to the callable. The callable must return another parser. If the first parser fails, the first parser is returned.
map
Map a function over the parser (which in turn maps it over the result).
continueFrom
Take the remaining input from the result and parse it.
append
Combine the parser with another parser of the same type, which will cause the results to be appended.
and
Combine the parser with another parser of the same type, which will cause the results to be appended.
tryString
Try to parse a string. Alias of try(new StringStream($string))
.
try
Try to parse the input, or throw an exception.
apply
Sequential application. Given a parser which outputs a callable, return a new parser that applies the callable on the output of the second parser.
The first parser must be of type Parser<callable(T1):T2>. {@see pure()} can be used to wrap a callable in a Parser.
Callables with more than 1 argument need to be curried: pure(curry(fn($x, $y)))->apply($parser2)->apply($parser3)
thenIgnore
Sequence two parsers, and return the output of the first one, ignore the second.
notFollowedBy
notFollowedBy only succeeds when $second fails. It never consumes any input.
Example:
string("print")
will also match "printXYZ"
string("print")->notFollowedBy(alphaNumChar()))
will match "print something" but not "printXYZ something"
label
Label a parser. When a parser fails, you'll see your label as the "expected" value. As a best practice, the labels should make sense to the person who provides the input for your parser. That's often an end user or a third party, so keep them in mind.
emit
If the parser is successful, call the $receiver function with the output of the parser. The resulting parser behaves identical to the original one. This combinator is useful for expressing side effects during the parsing process. It can be hooked into existing event publishing libraries by using $receiver as an adapter for those. Other use cases are logging, caching, performing an action whenever a value is matched in a long running input stream, ...
thenEof
Make sure that the input ends after the parser has successfully completed. The output is the output of the original parser.
Also useful in unit tests to make sure a parser doesn't consume more than you intended.
Alias for $parser->thenIgnore(eof()).