19declare(strict_types=1);
36 return $this->
variadic([$this,
'or2'], $parse);
41 return $this->
variadic([$this,
'seq'], $parse);
49 public function until(?
int $max, $parse): Closure
55 $max =
null === $max ? null : $max - 1;
61 $this->
lazy([$this,
'until'], $max, $parse)
72 return $x instanceof Closure ? $x : $this->
mustEqual($x);
75 private function or2(Closure
$f, Closure $g): Closure
78 $f($x, $cc)->except(
static fn():
Result => $g($x, $cc))
87 private function seq(Closure
$f, Closure $g): Closure
102 private function eq(
int $atom): Closure
117 private function variadic(callable $call, array $parse): Closure
119 $parse = array_map([$this,
'parserFrom'], $parse);
121 return array_reduce(array_slice($parse, 1), $call, $parse[0]);
130 if (strlen($expected) === 1) {
131 return $this->
eq(ord($expected));
132 } elseif (
'' === $expected) {
142 private function nop(): Closure
149 private function lazy(callable $call, ...$arguments): Closure
152 ($call(...$arguments))($x, $cc)
A result encapsulates a value or an error and simplifies the handling of those.
@phpstan-type Continuation Closure(Result<Intermediate>): Result<Intermediate> @phpstan-type Parser C...
simpleSequence(array $parse)
variadic(callable $call, array $parse)
Makes a variadic function from a binary one (left associative and requires at leat 1 argument): f(f(a...
mustEqual(string $expected)
simpleEither(array $parse)
lazy(callable $call,... $arguments)
or2(Closure $f, Closure $g)
seq(Closure $f, Closure $g)
A result encapsulates a value or an error and simplifies the handling of those.