combinators.php
This file is part of the Parsica library.
Copyright (c) 2020 Mathias Verraes mathias@verraes.net
For the full copyright and license information, please view the LICENSE file that was distributed with this source code.
Table of Contents
Functions
- identity() : Parser
- Identity parser, returns the Parser as is.
- pure() : Parser
- A parser that will have the argument as its output, no matter what the input was. It doesn't consume any input.
- optional() : Parser
- Optionally parse something, but still succeed if the thing is not there
- bind() : Parser
- 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.
- apply() : Parser
- Sequential application. Given a parser which outputs a callable, return a new parser that applies the callable on the output of the second parser.
- sequence() : Parser
- Parse something, then follow by something else. Ignore the result of the first parser and return the result of the second parser.
- keepFirst() : Parser
- Sequence two parsers, and return the output of the first one.
- keepSecond() : Parser
- Sequence two parsers, and return the output of the second one.
- either() : Parser
- Either parse the first thing or the second thing
- append() : Parser
- Combine the parser with another parser of the same type, which will cause the results to be appended.
- assemble() : Parser
- Append all the passed parsers.
- collect() : Parser
- Parse into an array that consists of the results of all parsers.
- any() : Parser
- Tries each parser one by one, returning the result of the first one that succeeds.
- choice() : Parser
- Tries each parser one by one, returning the result of the first one that succeeds.
- atLeastOne() : Parser
- One or more repetitions of Parser, with the outputs appended.
- zeroOrMore() : Parser
- Zero or more repetitions of Parser, with the outputs appended.
- repeat() : Parser
- Parse something exactly n times
- repeatList() : Parser
- Parse something exactly n times and return as an array
- some() : Parser
- Parse something one or more times, and output an array of the successful outputs.
- many() : Parser
- Parse something zero or more times, and output an array of the successful outputs.
- between() : Parser
- Parse $open, followed by $middle, followed by $close, and return the result of $middle. Useful for eg. "(value)".
- sepBy() : Parser
- Parses zero or more occurrences of $parser, separated by $separator. Returns a list of values.
- sepBy1() : Parser
- Parses one or more occurrences of $parser, separated by $separator. Returns a list of values.
- sepBy2() : Parser
- Parses 2 or more occurrences of $parser, separated by $separator. Returns a list of values.
- notFollowedBy() : Parser
- notFollowedBy only succeeds when $parser fails. It never consumes any input.
- map() : Parser
- Map a function over the parser (which in turn maps it over the result).
- lookAhead() : Parser
- 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.
Functions
identity()
Identity parser, returns the Parser as is.
identity(Parser $parser) : Parser
Parameters
- $parser : Parser
Tags
Return values
Parserpure()
A parser that will have the argument as its output, no matter what the input was. It doesn't consume any input.
pure(mixed $output) : Parser
Parameters
- $output : mixed
Tags
Return values
Parseroptional()
Optionally parse something, but still succeed if the thing is not there
optional(Parser $parser) : Parser
Parameters
- $parser : Parser
Tags
Return values
Parserbind()
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.
bind(Parser $parser, callable $f) : Parser
This is a monadic bind aka flatmap.
Parameters
- $parser : Parser
- $f : callable
Tags
Return values
Parserapply()
Sequential application. Given a parser which outputs a callable, return a new parser that applies the callable on the output of the second parser.
apply(Parser $parser1, Parser $parser2) : Parser
The first parser must be of type Parser<callable(T1):T2>. 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)
Parameters
Tags
Return values
Parsersequence()
Parse something, then follow by something else. Ignore the result of the first parser and return the result of the second parser.
sequence(Parser $first, Parser $second) : Parser
Parameters
Tags
Return values
ParserkeepFirst()
Sequence two parsers, and return the output of the first one.
keepFirst(Parser $first, Parser $second) : Parser
Parameters
Tags
Return values
ParserkeepSecond()
Sequence two parsers, and return the output of the second one.
keepSecond(Parser $first, Parser $second) : Parser
Parameters
Tags
Return values
Parsereither()
Either parse the first thing or the second thing
either(Parser $first, Parser $second) : Parser
Parameters
Tags
Return values
Parserappend()
Combine the parser with another parser of the same type, which will cause the results to be appended.
append(Parser $left, Parser $right) : Parser
Parameters
Tags
Return values
Parserassemble()
Append all the passed parsers.
assemble(Parser ...$parsers) : Parser
Parameters
- $parsers : Parser
Tags
Return values
Parsercollect()
Parse into an array that consists of the results of all parsers.
collect(Parser ...$parsers) : Parser
Parameters
- $parsers : Parser
Tags
Return values
Parserany()
Tries each parser one by one, returning the result of the first one that succeeds.
any(Parser ...$parsers) : Parser
Parameters
- $parsers : Parser
Tags
Return values
Parserchoice()
Tries each parser one by one, returning the result of the first one that succeeds.
choice(Parser ...$parsers) : Parser
Alias for any()
Parameters
- $parsers : Parser
Tags
Return values
ParseratLeastOne()
One or more repetitions of Parser, with the outputs appended.
atLeastOne(Parser $parser) : Parser
Parameters
- $parser : Parser
Tags
Return values
ParserzeroOrMore()
Zero or more repetitions of Parser, with the outputs appended.
zeroOrMore(Parser $parser) : Parser
Parameters
- $parser : Parser
Tags
Return values
Parserrepeat()
Parse something exactly n times
repeat(int $n, Parser $parser) : Parser
Parameters
- $n : int
- $parser : Parser
Tags
Return values
ParserrepeatList()
Parse something exactly n times and return as an array
repeatList(int $n, Parser $parser) : Parser
Parameters
- $n : int
- $parser : Parser
Tags
Return values
Parsersome()
Parse something one or more times, and output an array of the successful outputs.
some(Parser $parser) : Parser
Parameters
- $parser : Parser
Tags
Return values
Parsermany()
Parse something zero or more times, and output an array of the successful outputs.
many(Parser $parser) : Parser
Parameters
- $parser : Parser
Tags
Return values
Parserbetween()
Parse $open, followed by $middle, followed by $close, and return the result of $middle. Useful for eg. "(value)".
between(Parser $open, Parser $close, Parser $middle) : Parser
Parameters
Tags
Return values
ParsersepBy()
Parses zero or more occurrences of $parser, separated by $separator. Returns a list of values.
sepBy(Parser $separator, Parser $parser) : Parser
The sepBy parser always succeed, even if it doesn't find anything. Use sepBy1() if you want it to find at least one value.
Parameters
Tags
Return values
ParsersepBy1()
Parses one or more occurrences of $parser, separated by $separator. Returns a list of values.
sepBy1(Parser $separator, Parser $parser) : Parser
Parameters
Tags
Return values
ParsersepBy2()
Parses 2 or more occurrences of $parser, separated by $separator. Returns a list of values.
sepBy2(Parser $separator, Parser $parser) : Parser
Parameters
Tags
Return values
ParsernotFollowedBy()
notFollowedBy only succeeds when $parser fails. It never consumes any input.
notFollowedBy(Parser $parser) : Parser
Example:
string("print") will also match "printXYZ"
keepFirst(string("print"), notFollowedBy(alphaNumChar())) will match "print something" but not "printXYZ something"
Parameters
- $parser : Parser
Tags
Return values
Parsermap()
Map a function over the parser (which in turn maps it over the result).
map(Parser $parser, callable $transform) : Parser
Parameters
- $parser : Parser
- $transform : callable
Tags
Return values
ParserlookAhead()
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.
lookAhead(Parser $parser) : Parser
Parameters
- $parser : Parser