Version: 0.7.0

Parser

recursive

Create a recursive parser. Used in combination with recurse(Parser).

public static function recursive() : Parser

recurse

Recurse on a parser. Used in combination with {@see recursive()}. After calling this method, this parser behaves like a regular parser.

public function recurse(Parser $parser) : void

run

Run the parser on an input

public function run(Stream $input) : ParseResult

optional

Optionally parse something, but still succeed if the thing is not there.

public function optional() : Parser

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')

public function or(Parser $other) : Parser

followedBy

Parse something, then follow by something else. Ignore the result of the first parser and return the result of the second parser.

public function followedBy(Parser $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.

public function sequence(Parser $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().

public function then(Parser $second) : Parser

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.

public function bind(callable $f) : Parser

map

Map a function over the parser (which in turn maps it over the result).

public function map(callable $transform) : Parser

continueFrom

Take the remaining input from the result and parse it.

public function continueFrom(ParseResult $result) : ParseResult

append

Combine the parser with another parser of the same type, which will cause the results to be appended.

public function append(Parser $other) : Parser

and

Combine the parser with another parser of the same type, which will cause the results to be appended.

public function and(Parser $other) : Parser

tryString

Try to parse a string. Alias of try(new StringStream($string)).

public function tryString(string $input) : ParseResult

try

Try to parse the input, or throw an exception.

public function try(Stream $input) : ParseResult

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)

public function apply(Parser $parser) : Parser

thenIgnore

Sequence two parsers, and return the output of the first one, ignore the second.

public function thenIgnore(Parser $other) : Parser

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"

public function notFollowedBy(Parser $second) : Parser

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.

public function label(string $label) : Parser

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, ...

public function emit(callable $receiver) : Parser

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()).

public function thenEof() : Parser