Version: next

combinators

identity

Identity parser, returns the Parser as is.

function identity(Parser $parser) : Parser

pure

A parser that will have the argument as its output, no matter what the input was. It doesn't consume any input.

function pure($output) : Parser

optional

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

function optional(Parser $parser) : 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.

This is a monadic bind aka flatmap.

function bind(Parser $parser, callable $f) : Parser

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)

function apply(Parser $parser1, Parser $parser2) : Parser

sequence

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

function sequence(Parser $first, Parser $second) : Parser

keepFirst

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

function keepFirst(Parser $first, Parser $second) : Parser

keepSecond

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

function keepSecond(Parser $first, Parser $second) : Parser

either

Either parse the first thing or the second thing

function either(Parser $first, Parser $second) : Parser

append

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

function append(Parser $left, Parser $right) : Parser

assemble

Append all the passed parsers.

function assemble(Parser ...$parsers) : Parser

collect

Parse into an array that consists of the results of all parsers.

function collect(Parser ...$parsers) : Parser

any

Tries each parser one by one, returning the result of the first one that succeeds.

function any(Parser ...$parsers) : Parser

choice

Tries each parser one by one, returning the result of the first one that succeeds.

Alias for {@see any()}

function choice(Parser ...$parsers) : Parser

atLeastOne

One or more repetitions of Parser, with the outputs appended.

function atLeastOne(Parser $parser) : Parser

zeroOrMore

Warning: Deprecated

Zero or more repetitions of Parser, with the outputs appended.

function zeroOrMore(Parser $parser) : Parser

repeat

Parse something exactly n times

function repeat(int $n, Parser $parser) : Parser

repeatList

Parse something exactly n times and return as an array

function repeatList(int $n, Parser $parser) : Parser

many

Parse something zero or more times, and output an array of the successful outputs.

function many(Parser $parser) : Parser

some

Parse something one or more times, and output an array of the successful outputs.

function some(Parser $parser) : Parser

between

Parse $open, followed by $middle, followed by $close, and return the result of $middle. Useful for eg. "(value)".

function between(Parser $open, Parser $close, Parser $middle) : Parser

sepBy

Parses zero or more occurrences of $parser, separated by $separator. Returns a list of values.

The sepBy parser always succeed, even if it doesn't find anything. Use {@see sepBy1()} if you want it to find at least one value.

function sepBy(Parser $separator, Parser $parser) : Parser

sepBy1

Parses one or more occurrences of $parser, separated by $separator. Returns a list of values.

function sepBy1(Parser $separator, Parser $parser) : Parser

sepBy2

Warning: Deprecated

Parses 2 or more occurrences of $parser, separated by $separator. Returns a list of values.

function sepBy2(Parser $separator, Parser $parser) : Parser

notFollowedBy

notFollowedBy only succeeds when $parser fails. It never consumes any input.

Example:

string("print") will also match "printXYZ"

keepFirst(string("print"), notFollowedBy(alphaNumChar())) will match "print something" but not "printXYZ something"

function notFollowedBy(Parser $parser) : Parser

map

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

function map(Parser $parser, callable $transform) : Parser

lookAhead

If $parser succeeds (either consuming input or not), lookAhead behaves like $parser succeeded without consuming anything. If $parser fails, lookAhead has no effect, i.e. it will fail to consume input if $parser fails consuming input.

function lookAhead(Parser $parser) : Parser