Parsica
Table of Contents
Namespaces
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
Return values
ParsercharI()
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
Return values
ParsercontrolChar()
Parse a control character (a non-printing character of the Latin-1 subset of Unicode).
controlChar() : Parser
Tags
Return values
ParserupperChar()
Parse an uppercase character A-Z.
upperChar() : Parser
Tags
Return values
ParserlowerChar()
Parse a lowercase character a-z.
lowerChar() : Parser
Tags
Return values
ParseralphaChar()
Parse an uppercase or lowercase character A-Z, a-z.
alphaChar() : Parser
Tags
Return values
ParseralphaNumChar()
Parse an alpha or numeric character A-Z, a-z, 0-9.
alphaNumChar() : Parser
Tags
Return values
ParserprintChar()
Parse a printable ASCII char.
printChar() : Parser
Tags
Return values
ParserpunctuationChar()
Parse a single punctuation character !"#$%&'()*+,-./:;<=>?@[\]^_`{|}~
punctuationChar() : Parser
Tags
Return values
ParserdigitChar()
Parse 0-9. Returns the digit as a string. Use ->map('intval') or similar to cast it to a numeric type.
digitChar() : Parser
Tags
Return values
ParserbinDigitChar()
Parse a binary character 0 or 1.
binDigitChar() : Parser
Tags
Return values
ParseroctDigitChar()
Parse an octodecimal character 0-7.
octDigitChar() : Parser
Tags
Return values
ParserhexDigitChar()
Parse a hexadecimal numeric character 0123456789abcdefABCDEF.
hexDigitChar() : Parser
Tags
Return values
Parseridentity()
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
Tags
Return values
Parserinteger()
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
Return values
Parserfloat()
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
Return values
ParserisEqual()
Creates an equality predicate
isEqual(string $x) : callable
Parameters
- $x : string
Tags
Return values
callablenotPred()
Negates a predicate.
notPred(callable $predicate) : callable
Parameters
- $predicate : callable
Tags
Return values
callableandPred()
Boolean And predicate.
andPred(callable $first, callable $second) : callable
Parameters
- $first : callable
- $second : callable
Tags
Return values
callableorPred()
Boolean Or predicate.
orPred(callable $first, callable $second) : callable
Parameters
- $first : callable
- $second : callable
Tags
Return values
callableisCharCode()
Predicate that checks if a character is in an array of character codes.
isCharCode(array<string|int, mixed> $chars) : callable
Parameters
- $chars : array<string|int, mixed>
Tags
Return values
callableisSpace()
Returns true for a space character, and the control characters \t, \n, \r, \f, \v.
isSpace() : callable
Tags
Return values
callableisHSpace()
Like 'isSpace', but does not accept newlines and carriage returns.
isHSpace() : callable
Tags
Return values
callableisDigit()
True for 0-9
isDigit() : callable
Tags
Return values
callableisControl()
Control character predicate (a non-printing character of the Latin-1 subset of Unicode).
isControl() : callable
Tags
Return values
callableisBlank()
Returns true for a space or a tab character
isBlank() : callable
Tags
Return values
callableisWhitespace()
Returns true for a space character, and \t, \n, \r, \f, \v.
isWhitespace() : callable
Tags
Return values
callableisUpper()
Returns true for an uppercase character A-Z.
isUpper() : callable
Tags
Return values
callableisLower()
Returns true for a lowercase character a-z.
isLower() : mixed
Tags
isAlpha()
Returns true for an uppercase or lowercase character A-Z, a-z.
isAlpha() : callable
Tags
Return values
callableisAlphaNum()
Returns true for an alpha or numeric character A-Z, a-z, 0-9.
isAlphaNum() : callable
Tags
Return values
callableisHexDigit()
Returns true if the given character is a hexadecimal numeric character 0123456789abcdefABCDEF.
isHexDigit() : callable
Tags
Return values
callableisPrintable()
Returns true if the given character is a printable ASCII char.
isPrintable() : callable
Tags
Return values
callableisPunctuation()
Returns true if the given character is a punctuation character !"#$%&'()*+,-./:;<=>?@[\]^_`{|}~
isPunctuation() : callable
Tags
Return values
callablesatisfy()
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
Return values
ParserskipWhile()
Skip 0 or more characters as long as the predicate holds.
skipWhile(callable $predicate) : Parser
Parameters
- $predicate : callable
Tags
Return values
ParserskipWhile1()
Skip 1 or more characters as long as the predicate holds.
skipWhile1(callable $predicate) : Parser
Parameters
- $predicate : callable
Tags
Return values
ParsertakeWhile()
Keep parsing 0 or more characters as long as the predicate holds.
takeWhile(callable $predicate) : Parser
Parameters
- $predicate : callable
Tags
Return values
ParsertakeWhile1()
Keep parsing 1 or more characters as long as the predicate holds.
takeWhile1(callable $predicate) : Parser
Parameters
- $predicate : callable
Tags
Return values
ParseranySingle()
Parse and return a single character of anything.
anySingle() : Parser
Tags
Return values
Parseranything()
Parse and return a single character of anything.
anything() : Parser
Tags
Return values
ParseranySingleBut()
Match any character but the given one.
anySingleBut(string $x) : Parser
Parameters
- $x : string
Tags
Return values
ParseroneOf()
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
Return values
ParseroneOfS()
A compact form of 'oneOf'.
oneOfS(string $chars) : Parser
oneOfS("abc") == oneOf(['a', 'b', 'c'])
Parameters
- $chars : string
Tags
Return values
ParsernoneOf()
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
Return values
ParsernoneOfS()
A compact form of 'noneOf'.
noneOfS(string $chars) : Parser
noneOfS("abc") == noneOf(['a', 'b', 'c'])
Parameters
- $chars : string
Tags
Return values
ParsertakeRest()
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
Return values
Parsernothing()
Parse nothing, but still succeed.
nothing() : Parser
This serves as the zero parser in append() operations.
Tags
Return values
Parsereverything()
Parse everything; that is, consume the rest of the input until the end.
everything() : Parser
Tags
Return values
Parsersucceed()
Always succeed, no matter what the input was.
succeed() : Parser
Tags
Return values
Parserfail()
Always fail, no matter what the input was.
fail(string $label) : Parser
Parameters
- $label : string
Tags
Return values
Parsereof()
Parse the end of the input
eof() : Parser
Tags
Return values
Parserrecursive()
Create a recursive parser. Used in combination with Parser#recurse().
recursive() : Parser
Tags
Return values
Parseremit()
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
Return values
Parserspace()
Parse a single space character.
space() : Parser
Tags
Return values
Parsertab()
Parse a single tab character.
tab() : Parser
Tags
Return values
Parserblank()
Parse a space or tab.
blank() : Parser
Tags
Return values
Parserwhitespace()
Parse a space character, and \t, \n, \r, \f, \v.
whitespace() : Parser
Tags
Return values
Parsernewline()
Parse a newline character.
newline() : Parser
Tags
Return values
Parsercrlf()
Parse a carriage return character and a newline character. Return the two characters. {\r\n}
crlf() : Parser
Tags
Return values
Parsereol()
Parse a newline or a crlf.
eol() : Parser
Tags
Return values
ParserskipSpace()
Skip zero or more white space characters.
skipSpace() : Parser
Tags
Return values
ParserskipHSpace()
Like 'skipSpace', but does not accept newlines and carriage returns.
skipHSpace() : Parser
Tags
Return values
ParserskipSpace1()
Skip one or more white space characters.
skipSpace1() : Parser
Tags
Return values
ParserskipHSpace1()
Like 'skipSpace1', but does not accept newlines and carriage returns.
skipHSpace1() : Parser
Tags
Return values
Parserstring()
Parse a non-empty string.
string(string $str) : Parser
Parameters
- $str : string
Tags
Return values
ParserstringI()
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