Parser
in package
A parser is any function that takes a string input and returns a {@see ParseResult}. The Parser class is a wrapper around such functions. The {@see Parser::make()} static constructor takes a callable that does the actual parsing.
Usually you don't need to instantiate this class directly. Instead, build your parser from existing parsers and combinators.
At the moment, there is no Parser interface, and no Parser abstract class to extend from. This is intentional, but will be changed if we find use cases where those would be the best solutions.
The type is Parser<T>, where T is the type of the output that the parser will produce after completing successfully.
Tags
Table of Contents
Properties
- $label : string
- $parserFunction : mixed
- $recursionStatus : string
Methods
- and() : Parser
- Combine the parser with another parser of the same type, which will cause the results to be appended.
- append() : Parser
- Combine the parser with another parser of the same type, which will cause the results to be appended.
- 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.
- 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.
- continueFrom() : ParseResult
- Take the remaining input from the result and parse it.
- emit() : Parser
- 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.
- followedBy() : Parser
- Parse something, then follow by something else. Ignore the result of the first parser and return the result of the second parser.
- label() : Parser
- 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.
- map() : Parser
- Map a function over the parser (which in turn maps it over the result).
- notFollowedBy() : Parser
- notFollowedBy only succeeds when $second fails. It never consumes any input.
- optional() : Parser
- Optionally parse something, but still succeed if the thing is not there.
- or() : Parser
- Try the first parser, and failing that, try the second parser. Returns the first succeeding result, or the first failing result.
- recurse() : void
- Recurse on a parser. Used in combination with {@see recursive()}. After calling this method, this parser behaves like a regular parser.
- recursive() : Parser
- Create a recursive parser. Used in combination with recurse(Parser).
- run() : ParseResult
- Run the parser on an input
- sequence() : Parser
- Parse something, then follow by something else. Ignore the result of the first parser and return the result of the second parser.
- then() : Parser
- 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().
- thenEof() : Parser
- Make sure that the input ends after the parser has successfully completed. The output is the output of the original parser.
- thenIgnore() : Parser
- Sequence two parsers, and return the output of the first one, ignore the second.
- try() : ParseResult
- Try to parse the input, or throw an exception.
- tryString() : ParseResult
- Try to parse a string. Alias of `try(new StringStream($string))`.
- voidLeft() : Parser
- Ignore the output of the parser and return the new output instead.
- __construct() : mixed
Properties
$label
private
string
$label
$parserFunction
private
mixed
$parserFunction
Tags
$recursionStatus
private
string
$recursionStatus
Tags
Methods
and()
Combine the parser with another parser of the same type, which will cause the results to be appended.
public
and(Parser $other) : Parser
Parameters
- $other : Parser
Tags
Return values
Parserappend()
Combine the parser with another parser of the same type, which will cause the results to be appended.
public
append(Parser $other) : Parser
Parameters
- $other : Parser
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.
public
apply(Parser $parser) : 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
- $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.
public
bind(callable $f) : Parser
Parameters
- $f : callable
Tags
Return values
ParsercontinueFrom()
Take the remaining input from the result and parse it.
public
continueFrom(ParseResult $result) : ParseResult
Parameters
- $result : ParseResult
Tags
Return values
ParseResultemit()
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.
public
emit(callable $receiver) : Parser
Other use cases are logging, caching, performing an action whenever a value is matched in a long running input stream, ...
Parameters
- $receiver : callable
Tags
Return values
ParserfollowedBy()
Parse something, then follow by something else. Ignore the result of the first parser and return the result of the second parser.
public
followedBy(Parser $second) : Parser
Parameters
- $second : Parser
Tags
Return values
Parserlabel()
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
label(string $label) : Parser
Parameters
- $label : string
Tags
Return values
Parsermap()
Map a function over the parser (which in turn maps it over the result).
public
map(callable $transform) : Parser
Parameters
- $transform : callable
Tags
Return values
ParsernotFollowedBy()
notFollowedBy only succeeds when $second fails. It never consumes any input.
public
notFollowedBy(Parser $second) : Parser
Example:
string("print") will also match "printXYZ"
string("print")->notFollowedBy(alphaNumChar())) will match "print something" but not "printXYZ something"
Parameters
- $second : Parser
Tags
Return values
Parseroptional()
Optionally parse something, but still succeed if the thing is not there.
public
optional() : Parser
Tags
Return values
Parseror()
Try the first parser, and failing that, try the second parser. Returns the first succeeding result, or the first failing result.
public
or(Parser $other) : Parser
Caveat: The order matters! string('http')->or(string('https')
Parameters
- $other : Parser
Tags
Return values
Parserrecurse()
Recurse on a parser. Used in combination with {@see recursive()}. After calling this method, this parser behaves like a regular parser.
public
recurse(Parser $parser) : void
Parameters
- $parser : Parser
Tags
recursive()
Create a recursive parser. Used in combination with recurse(Parser).
public
static recursive() : Parser
Tags
Return values
Parserrun()
Run the parser on an input
public
run(Stream $input) : ParseResult
Parameters
- $input : Stream
Tags
Return values
ParseResultsequence()
Parse something, then follow by something else. Ignore the result of the first parser and return the result of the second parser.
public
sequence(Parser $second) : Parser
Parameters
- $second : Parser
Tags
Return values
Parserthen()
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
then(Parser $second) : Parser
Parameters
- $second : Parser
Tags
Return values
ParserthenEof()
Make sure that the input ends after the parser has successfully completed. The output is the output of the original parser.
public
thenEof() : Parser
Also useful in unit tests to make sure a parser doesn't consume more than you intended.
Alias for $parser->thenIgnore(eof()).
Tags
Return values
ParserthenIgnore()
Sequence two parsers, and return the output of the first one, ignore the second.
public
thenIgnore(Parser $other) : Parser
Parameters
- $other : Parser
Tags
Return values
Parsertry()
Try to parse the input, or throw an exception.
public
try(Stream $input) : ParseResult
Parameters
- $input : Stream
Tags
Return values
ParseResulttryString()
Try to parse a string. Alias of `try(new StringStream($string))`.
public
tryString(string $input) : ParseResult
Parameters
- $input : string
Tags
Return values
ParseResultvoidLeft()
Ignore the output of the parser and return the new output instead.
public
voidLeft(mixed $output) : Parser
@TODO needs test
Parameters
- $output : mixed
Tags
Return values
Parser__construct()
private
__construct(callable $parserFunction, string $recursionStatus, string $label) : mixed
Parameters
- $parserFunction : callable
- $recursionStatus : string
- $label : string