Documentation

Parsica

Table of Contents

Namespaces

Curry
Expression
Internal
JSON
PHPUnit

Interfaces

ParseResult
Stream
Represents an input stream. This allows us to have different types of input, each with their own optimizations.

Classes

Parser
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.
ParserHasFailed
StringStream

Functions

char()  : Parser
Parse a single character.
charI()  : Parser
Parse a single character, case-insensitive and case-preserving. On success, it returns the string cased as the actually parsed input.
controlChar()  : Parser
Parse a control character (a non-printing character of the Latin-1 subset of Unicode).
upperChar()  : Parser
Parse an uppercase character A-Z.
lowerChar()  : Parser
Parse a lowercase character a-z.
alphaChar()  : Parser
Parse an uppercase or lowercase character A-Z, a-z.
alphaNumChar()  : Parser
Parse an alpha or numeric character A-Z, a-z, 0-9.
printChar()  : Parser
Parse a printable ASCII char.
punctuationChar()  : Parser
Parse a single punctuation character !"#$%&'()*+,-./:;<=>?@[\]^_`{|}~
digitChar()  : Parser
Parse 0-9. Returns the digit as a string. Use ->map('intval') or similar to cast it to a numeric type.
binDigitChar()  : Parser
Parse a binary character 0 or 1.
octDigitChar()  : Parser
Parse an octodecimal character 0-7.
hexDigitChar()  : Parser
Parse a hexadecimal numeric character 0123456789abcdefABCDEF.
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.
integer()  : Parser
Parse an integer and return it as a string. Use ->map('intval') or similar to cast it to a numeric type.
float()  : Parser
Parse a float and return it as a string. Use ->map('floatval') or similar to cast it to a numeric type.
isEqual()  : callable
Creates an equality predicate
notPred()  : callable
Negates a predicate.
andPred()  : callable
Boolean And predicate.
orPred()  : callable
Boolean Or predicate.
isCharCode()  : callable
Predicate that checks if a character is in an array of character codes.
isSpace()  : callable
Returns true for a space character, and the control characters \t, \n, \r, \f, \v.
isHSpace()  : callable
Like 'isSpace', but does not accept newlines and carriage returns.
isDigit()  : callable
True for 0-9
isControl()  : callable
Control character predicate (a non-printing character of the Latin-1 subset of Unicode).
isBlank()  : callable
Returns true for a space or a tab character
isWhitespace()  : callable
Returns true for a space character, and \t, \n, \r, \f, \v.
isUpper()  : callable
Returns true for an uppercase character A-Z.
isLower()  : mixed
Returns true for a lowercase character a-z.
isAlpha()  : callable
Returns true for an uppercase or lowercase character A-Z, a-z.
isAlphaNum()  : callable
Returns true for an alpha or numeric character A-Z, a-z, 0-9.
isHexDigit()  : callable
Returns true if the given character is a hexadecimal numeric character 0123456789abcdefABCDEF.
isPrintable()  : callable
Returns true if the given character is a printable ASCII char.
isPunctuation()  : callable
Returns true if the given character is a punctuation character !"#$%&'()*+,-./:;<=>?@[\]^_`{|}~
satisfy()  : Parser
A parser that satisfies a predicate on a single token. Useful as a building block for writing things like char(), digit().
skipWhile()  : Parser
Skip 0 or more characters as long as the predicate holds.
skipWhile1()  : Parser
Skip 1 or more characters as long as the predicate holds.
takeWhile()  : Parser
Keep parsing 0 or more characters as long as the predicate holds.
takeWhile1()  : Parser
Keep parsing 1 or more characters as long as the predicate holds.
anySingle()  : Parser
Parse and return a single character of anything.
anything()  : Parser
Parse and return a single character of anything.
anySingleBut()  : Parser
Match any character but the given one.
oneOf()  : Parser
Succeeds if the current character is in the supplied list of characters. Returns the parsed character.
oneOfS()  : Parser
A compact form of 'oneOf'.
noneOf()  : Parser
The dual of 'oneOf'. Succeeds if the current character is not in the supplied list of characters. Returns the parsed character.
noneOfS()  : Parser
A compact form of 'noneOf'.
takeRest()  : Parser
Consume the rest of the input and return it as a string. This parser never fails, but may return the empty string.
nothing()  : Parser
Parse nothing, but still succeed.
everything()  : Parser
Parse everything; that is, consume the rest of the input until the end.
succeed()  : Parser
Always succeed, no matter what the input was.
fail()  : Parser
Always fail, no matter what the input was.
eof()  : Parser
Parse the end of the input
recursive()  : Parser
Create a recursive parser. Used in combination with Parser#recurse().
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. Other use cases are logging, caching, performing an action whenever a value is matched in a long-running input stream, .
space()  : Parser
Parse a single space character.
tab()  : Parser
Parse a single tab character.
blank()  : Parser
Parse a space or tab.
whitespace()  : Parser
Parse a space character, and \t, \n, \r, \f, \v.
newline()  : Parser
Parse a newline character.
crlf()  : Parser
Parse a carriage return character and a newline character. Return the two characters. {\r\n}
eol()  : Parser
Parse a newline or a crlf.
skipSpace()  : Parser
Skip zero or more white space characters.
skipHSpace()  : Parser
Like 'skipSpace', but does not accept newlines and carriage returns.
skipSpace1()  : Parser
Skip one or more white space characters.
skipHSpace1()  : Parser
Like 'skipSpace1', but does not accept newlines and carriage returns.
string()  : Parser
Parse a non-empty string.
stringI()  : Parser
Parse a non-empty string, case-insensitive, and case-preserving. On success, it returns the string cased as the actually parsed input.

Functions

char()

Parse a single character.

char(string $c) : Parser
Parameters
$c : string
Tags
psalm-param

string $c A single character

psalm-return

Parser

see
charI()
psalm-pure
Return values
Parser

charI()

Parse a single character, case-insensitive and case-preserving. On success, it returns the string cased as the actually parsed input.

charI(string $c) : Parser

eg charI('a'')->run("ABC") will succeed with "A", not "a".

Parameters
$c : string
Tags
psalm-param

string $c A single character

psalm-return

Parser

see
char()
psalm-pure
Return values
Parser

controlChar()

Parse a control character (a non-printing character of the Latin-1 subset of Unicode).

controlChar() : Parser
Tags
psalm-return

Parser

psalm-pure
Return values
Parser

upperChar()

Parse an uppercase character A-Z.

upperChar() : Parser
Tags
psalm-return

Parser

psalm-pure
Return values
Parser

lowerChar()

Parse a lowercase character a-z.

lowerChar() : Parser
Tags
psalm-return

Parser

psalm-pure
Return values
Parser

alphaChar()

Parse an uppercase or lowercase character A-Z, a-z.

alphaChar() : Parser
Tags
psalm-return

Parser

psalm-pure
Return values
Parser

alphaNumChar()

Parse an alpha or numeric character A-Z, a-z, 0-9.

alphaNumChar() : Parser
Tags
psalm-return

Parser

psalm-pure
Return values
Parser

printChar()

Parse a printable ASCII char.

printChar() : Parser
Tags
psalm-return

Parser

psalm-pure
Return values
Parser

punctuationChar()

Parse a single punctuation character !"#$%&'()*+,-./:;<=>?@[\]^_`{|}~

punctuationChar() : Parser
Tags
psalm-return

Parser

psalm-pure
Return values
Parser

digitChar()

Parse 0-9. Returns the digit as a string. Use ->map('intval') or similar to cast it to a numeric type.

digitChar() : Parser
Tags
psalm-return

Parser

psalm-pure
Return values
Parser

binDigitChar()

Parse a binary character 0 or 1.

binDigitChar() : Parser
Tags
psalm-return

Parser

psalm-pure
Return values
Parser

octDigitChar()

Parse an octodecimal character 0-7.

octDigitChar() : Parser
Tags
psalm-return

Parser

psalm-pure
Return values
Parser

hexDigitChar()

Parse a hexadecimal numeric character 0123456789abcdefABCDEF.

hexDigitChar() : Parser
Tags
psalm-return

Parser

psalm-pure
Return values
Parser

identity()

Identity parser, returns the Parser as is.

identity(Parser $parser) : Parser
Parameters
$parser : Parser
Tags
psalm-param

Parser<T> $parser

psalm-return

Parser<T>

template
psalm-pure
Return values
Parser

pure()

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
psalm-param

T $output

psalm-return

Parser<T>

template
psalm-pure
Return values
Parser

optional()

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

optional(Parser $parser) : Parser
Parameters
$parser : Parser
Tags
psalm-param

Parser<T> $parser

psalm-return

Parser<T|null>

template
psalm-pure
Return values
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.

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

This is a monadic bind aka flatmap.

Parameters
$parser : Parser
$f : callable
Tags
psalm-param

Parser<T1> $parser

psalm-param

pure-callable(T1) : Parser<T2> $f

psalm-return

Parser<T2>

template
template
psalm-pure
Return values
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.

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
$parser1 : Parser
$parser2 : Parser
Tags
template
template
psalm-param

Parser<pure-callable(T1):T2> $parser1

psalm-param

Parser<T1> $parser2

psalm-return

Parser<T2>

psalm-pure
Return values
Parser

sequence()

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
$first : Parser
$second : Parser
Tags
psalm-param

Parser<T1> $first

psalm-param

Parser<T2> $second

psalm-return

Parser<T2>

template
template
see
Parser::sequence()
psalm-pure
Return values
Parser

keepFirst()

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

keepFirst(Parser $first, Parser $second) : Parser
Parameters
$first : Parser
$second : Parser
Tags
template
template
psalm-param

Parser<T1> $first

psalm-param

Parser<T2> $second

psalm-return

Parser<T1>

psalm-pure
Return values
Parser

keepSecond()

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

keepSecond(Parser $first, Parser $second) : Parser
Parameters
$first : Parser
$second : Parser
Tags
template
template
psalm-param

Parser<T1> $first

psalm-param

Parser<T2> $second

psalm-return

Parser<T2>

psalm-pure
Return values
Parser

either()

Either parse the first thing or the second thing

either(Parser $first, Parser $second) : Parser
Parameters
$first : Parser
$second : Parser
Tags
psalm-param

Parser<T1> $first

psalm-param

Parser<T2> $second

psalm-return

Parser<T1|T2>

see
Parser::or()
template
template
psalm-pure
Return values
Parser

append()

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
$left : Parser
$right : Parser
Tags
psalm-param

Parser<T|null> $left

psalm-param

Parser<T|null> $right

psalm-return

Parser<T|null>

template
psalm-pure
Return values
Parser

assemble()

Append all the passed parsers.

assemble(Parser ...$parsers) : Parser
Parameters
$parsers : Parser
Tags
psalm-param

list<Parser<T|null>> $parsers

psalm-return

Parser<T|null>

template
psalm-suppress

MixedReturnStatement

psalm-suppress

MixedInferredReturnType

psalm-pure
Return values
Parser

collect()

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

collect(Parser ...$parsers) : Parser
Parameters
$parsers : Parser
Tags
psalm-param

list<Parser> $parsers

psalm-return

Parser

psalm-pure
Return values
Parser

any()

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

any(Parser ...$parsers) : Parser
Parameters
$parsers : Parser
Tags
no-named-arguments
psalm-param

non-empty-list<Parser> $parsers

psalm-return

Parser

psalm-pure
Return values
Parser

choice()

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
no-named-arguments
psalm-param

non-empty-list<Parser> $parsers

psalm-return

Parser

psalm-pure
Return values
Parser

atLeastOne()

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

atLeastOne(Parser $parser) : Parser
Parameters
$parser : Parser
Tags
psalm-param

Parser<T> $parser

psalm-return

Parser<T>

template
psalm-suppress

MixedArgumentTypeCoercion

psalm-pure
Return values
Parser

zeroOrMore()

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

zeroOrMore(Parser $parser) : Parser
Parameters
$parser : Parser
Tags
TODO

Untested

psalm-param

Parser<T> $parser

psalm-return

Parser<T>

template
psalm-suppress

MixedArgumentTypeCoercion

psalm-pure
Return values
Parser

repeat()

Parse something exactly n times

repeat(int $n, Parser $parser) : Parser
Parameters
$n : int
$parser : Parser
Tags
template
psalm-param

Parser<T> $parser

psalm-return

Parser<T>

psalm-pure
Return values
Parser

repeatList()

Parse something exactly n times and return as an array

repeatList(int $n, Parser $parser) : Parser
Parameters
$n : int
$parser : Parser
Tags
TODO

This doesn't feel very elegant.

template
psalm-param

positive-int $n

psalm-param

Parser<T> $parser

psalm-return

Parser<list<T>>

psalm-pure
Return values
Parser

some()

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

some(Parser $parser) : Parser
Parameters
$parser : Parser
Tags
template
psalm-param

Parser<T> $parser

psalm-return

Parser<list<T>>

psalm-pure
Return values
Parser

many()

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

many(Parser $parser) : Parser
Parameters
$parser : Parser
Tags
template
psalm-param

Parser<T> $parser

psalm-return

Parser<list<T>>

psalm-pure
Return values
Parser

between()

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
$open : Parser
$close : Parser
$middle : Parser
Tags
template
template
template
psalm-param

Parser<TO> $open

psalm-param

Parser<TC> $close

psalm-param

Parser<TM> $middle

psalm-return

Parser<TM>

psalm-pure
Return values
Parser

sepBy()

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
$separator : Parser
$parser : Parser
Tags
template
template
psalm-param

Parser<TSeparator> $separator

psalm-param

Parser<T> $parser

psalm-return

Parser<list<T>>

psalm-pure
Return values
Parser

sepBy1()

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

sepBy1(Parser $separator, Parser $parser) : Parser
Parameters
$separator : Parser
$parser : Parser
Tags
template
template
psalm-param

Parser<TS> $separator

psalm-param

Parser<T> $parser

psalm-return

Parser<list<T>>

psalm-suppress

MissingClosureReturnType

psalm-pure
Return values
Parser

sepBy2()

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

sepBy2(Parser $separator, Parser $parser) : Parser
Parameters
$separator : Parser
$parser : Parser
Tags
template
template
psalm-param

Parser<TS> $separator

psalm-param

Parser<T> $parser

psalm-return

Parser<list<T>>

psalm-suppress

MissingClosureReturnType

psalm-pure
Return values
Parser

notFollowedBy()

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
template
psalm-param

Parser<T> $parser

psalm-return

Parser<T>

see
Parser::notFollowedBy()
psalm-pure
Return values
Parser

map()

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
template
template
psalm-param

pure-callable(T1) : T2 $transform

psalm-return

Parser<T2>

psalm-pure
Return values
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.

lookAhead(Parser $parser) : Parser
Parameters
$parser : Parser
Tags
template
psalm-param

Parser<T> $parser

psalm-return

Parser<T>

psalm-pure
Return values
Parser

integer()

Parse an integer and return it as a string. Use ->map('intval') or similar to cast it to a numeric type.

integer() : Parser

Example: "-123"

Tags
psalm-return

Parser

psalm-pure
Return values
Parser

float()

Parse a float and return it as a string. Use ->map('floatval') or similar to cast it to a numeric type.

float() : Parser

Example: -123.456E-789

Tags
psalm-return

Parser

psalm-suppress

InvalidReturnType

psalm-suppress

InvalidReturnStatement

psalm-pure
Return values
Parser

isEqual()

Creates an equality predicate

isEqual(string $x) : callable
Parameters
$x : string
Tags
psalm-return

pure-callable(string) : bool

psalm-pure
Return values
callable

notPred()

Negates a predicate.

notPred(callable $predicate) : callable
Parameters
$predicate : callable
Tags
psalm-param

pure-callable(string) : bool $predicate

psalm-return

pure-callable(string) : bool

psalm-pure
Return values
callable

andPred()

Boolean And predicate.

andPred(callable $first, callable $second) : callable
Parameters
$first : callable
$second : callable
Tags
psalm-param

pure-callable(string) : bool $first

psalm-param

pure-callable(string) : bool $second

psalm-return

pure-callable(string) : bool

psalm-pure
Return values
callable

orPred()

Boolean Or predicate.

orPred(callable $first, callable $second) : callable
Parameters
$first : callable
$second : callable
Tags
psalm-param

pure-callable(string) : bool $first

psalm-param

pure-callable(string) : bool $second

psalm-return

pure-callable(string) : bool

psalm-pure
Return values
callable

isSpace()

Returns true for a space character, and the control characters \t, \n, \r, \f, \v.

isSpace() : callable
Tags
psalm-return

pure-callable(string) : bool

psalm-pure
Return values
callable

isHSpace()

Like 'isSpace', but does not accept newlines and carriage returns.

isHSpace() : callable
Tags
psalm-return

pure-callable(string) : bool

see
isSpace
psalm-pure
Return values
callable

isDigit()

True for 0-9

isDigit() : callable
Tags
psalm-return

pure-callable(string) : bool

psalm-pure
Return values
callable

isControl()

Control character predicate (a non-printing character of the Latin-1 subset of Unicode).

isControl() : callable
Tags
psalm-return

pure-callable(string) : bool

psalm-pure
Return values
callable

isBlank()

Returns true for a space or a tab character

isBlank() : callable
Tags
psalm-return

pure-callable(string) : bool

psalm-pure
Return values
callable

isWhitespace()

Returns true for a space character, and \t, \n, \r, \f, \v.

isWhitespace() : callable
Tags
psalm-return

pure-callable(string) : bool

psalm-pure
Return values
callable

isUpper()

Returns true for an uppercase character A-Z.

isUpper() : callable
Tags
psalm-return

pure-callable(string) : bool

psalm-pure
Return values
callable

isLower()

Returns true for a lowercase character a-z.

isLower() : mixed
Tags
psalm-return

pure-callable(string) : bool

psalm-pure

isAlpha()

Returns true for an uppercase or lowercase character A-Z, a-z.

isAlpha() : callable
Tags
psalm-return

pure-callable(string) : bool

psalm-pure
Return values
callable

isAlphaNum()

Returns true for an alpha or numeric character A-Z, a-z, 0-9.

isAlphaNum() : callable
Tags
psalm-return

pure-callable(string) : bool

psalm-pure
Return values
callable

isHexDigit()

Returns true if the given character is a hexadecimal numeric character 0123456789abcdefABCDEF.

isHexDigit() : callable
Tags
psalm-return

pure-callable(string) : bool

psalm-pure
Return values
callable

isPrintable()

Returns true if the given character is a printable ASCII char.

isPrintable() : callable
Tags
psalm-return

pure-callable(string) : bool

psalm-pure
Return values
callable

isPunctuation()

Returns true if the given character is a punctuation character !"#$%&'()*+,-./:;<=>?@[\]^_`{|}~

isPunctuation() : callable
Tags
psalm-return

pure-callable(string) : bool

psalm-pure
Return values
callable

satisfy()

A parser that satisfies a predicate on a single token. Useful as a building block for writing things like char(), digit().

satisfy(callable $predicate) : Parser

..

Parameters
$predicate : callable
Tags
template
psalm-param

callable(string) : bool $predicate

psalm-return

Parser<T>

psalm-pure
Return values
Parser

skipWhile()

Skip 0 or more characters as long as the predicate holds.

skipWhile(callable $predicate) : Parser
Parameters
$predicate : callable
Tags
template
psalm-param

pure-callable(string) : bool $predicate

psalm-return

Parser

psalm-pure
Return values
Parser

skipWhile1()

Skip 1 or more characters as long as the predicate holds.

skipWhile1(callable $predicate) : Parser
Parameters
$predicate : callable
Tags
template
psalm-param

pure-callable(string) : bool $predicate

psalm-return

Parser

psalm-pure
Return values
Parser

takeWhile()

Keep parsing 0 or more characters as long as the predicate holds.

takeWhile(callable $predicate) : Parser
Parameters
$predicate : callable
Tags
template
psalm-param

pure-callable(string) : bool $predicate

psalm-return

Parser<T>

psalm-pure
Return values
Parser

takeWhile1()

Keep parsing 1 or more characters as long as the predicate holds.

takeWhile1(callable $predicate) : Parser
Parameters
$predicate : callable
Tags
template
psalm-param

pure-callable(string) : bool $predicate

psalm-return

Parser<T>

psalm-pure
Return values
Parser

anySingle()

Parse and return a single character of anything.

anySingle() : Parser
Tags
template
psalm-return

Parser<T>

psalm-pure
Return values
Parser

anything()

Parse and return a single character of anything.

anything() : Parser
Tags
TODO

This is an alias of anySingle. Should we get rid of one of them?

psalm-return

Parser

psalm-pure
Return values
Parser

anySingleBut()

Match any character but the given one.

anySingleBut(string $x) : Parser
Parameters
$x : string
Tags
psalm-return

Parser

template
psalm-pure
Return values
Parser

oneOf()

Succeeds if the current character is in the supplied list of characters. Returns the parsed character.

oneOf(array<string|int, mixed> $chars) : Parser
Parameters
$chars : array<string|int, mixed>
Tags
psalm-param

list $chars

psalm-return

Parser

template
psalm-pure
Return values
Parser

oneOfS()

A compact form of 'oneOf'.

oneOfS(string $chars) : Parser

oneOfS("abc") == oneOf(['a', 'b', 'c'])

Parameters
$chars : string
Tags
psalm-param

string $chars

psalm-return

Parser

psalm-pure
Return values
Parser

noneOf()

The dual of 'oneOf'. Succeeds if the current character is not in the supplied list of characters. Returns the parsed character.

noneOf(array<string|int, mixed> $chars) : Parser
Parameters
$chars : array<string|int, mixed>
Tags
psalm-param

list $chars

psalm-return

Parser

template
psalm-pure
Return values
Parser

noneOfS()

A compact form of 'noneOf'.

noneOfS(string $chars) : Parser

noneOfS("abc") == noneOf(['a', 'b', 'c'])

Parameters
$chars : string
Tags
psalm-param

string $chars

psalm-return

Parser

template
psalm-pure
Return values
Parser

takeRest()

Consume the rest of the input and return it as a string. This parser never fails, but may return the empty string.

takeRest() : Parser
Tags
psalm-return

Parser

template
psalm-pure
Return values
Parser

nothing()

Parse nothing, but still succeed.

nothing() : Parser

This serves as the zero parser in append() operations.

Tags
psalm-return

Parser

psalm-pure
Return values
Parser

everything()

Parse everything; that is, consume the rest of the input until the end.

everything() : Parser
Tags
psalm-pure
Return values
Parser

succeed()

Always succeed, no matter what the input was.

succeed() : Parser
Tags
psalm-pure
Return values
Parser

fail()

Always fail, no matter what the input was.

fail(string $label) : Parser
Parameters
$label : string
Tags
psalm-pure
Return values
Parser

eof()

Parse the end of the input

eof() : Parser
Tags
psalm-return

Parser<T>

template
psalm-pure
Return values
Parser

recursive()

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

recursive() : Parser
Tags
psalm-return

Parser<T>

template
psalm-pure
Return values
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, .

emit(Parser $parser, callable $receiver) : Parser

..

Parameters
$parser : Parser
$receiver : callable
Tags
template
psalm-param

Parser<T> $parser

psalm-param

callable(T): void $receiver

psalm-return

Parser<T>

Return values
Parser

space()

Parse a single space character.

space() : Parser
Tags
psalm-return

Parser

psalm-pure
Return values
Parser

tab()

Parse a single tab character.

tab() : Parser
Tags
psalm-return

Parser

psalm-pure
Return values
Parser

blank()

Parse a space or tab.

blank() : Parser
Tags
psalm-return

Parser

psalm-pure
Return values
Parser

whitespace()

Parse a space character, and \t, \n, \r, \f, \v.

whitespace() : Parser
Tags
psalm-return

Parser

psalm-pure
Return values
Parser

newline()

Parse a newline character.

newline() : Parser
Tags
psalm-return

Parser

psalm-pure
Return values
Parser

crlf()

Parse a carriage return character and a newline character. Return the two characters. {\r\n}

crlf() : Parser
Tags
psalm-return

Parser

psalm-pure
Return values
Parser

eol()

Parse a newline or a crlf.

eol() : Parser
Tags
psalm-return

Parser

psalm-pure
Return values
Parser

skipSpace()

Skip zero or more white space characters.

skipSpace() : Parser
Tags
psalm-return

Parser

psalm-pure
Return values
Parser

skipHSpace()

Like 'skipSpace', but does not accept newlines and carriage returns.

skipHSpace() : Parser
Tags
psalm-return

Parser

see
skipSpace
psalm-pure
Return values
Parser

skipSpace1()

Skip one or more white space characters.

skipSpace1() : Parser
Tags
psalm-return

Parser

psalm-pure
Return values
Parser

skipHSpace1()

Like 'skipSpace1', but does not accept newlines and carriage returns.

skipHSpace1() : Parser
Tags
psalm-return

Parser

see
skipSpace1
psalm-pure
Return values
Parser

string()

Parse a non-empty string.

string(string $str) : Parser
Parameters
$str : string
Tags
psalm-return

Parser

see
stringI()
psalm-pure
Return values
Parser

stringI()

Parse a non-empty string, case-insensitive, and case-preserving. On success, it returns the string cased as the actually parsed input.

stringI(string $str) : Parser

eg stringI("foobar")->tryString("foObAr") will succeed with "foObAr"

Parameters
$str : string
Tags
TODO

The implementation could be replaced using Stream::takeWhile

psalm-return

Parser

see
string()
psalm-pure
Return values
Parser

        
On this page

Search results