1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Stratadox\Parser; |
4
|
|
|
|
5
|
|
|
use Closure; |
6
|
|
|
use Stratadox\Parser\Helpers\AtLeast; |
7
|
|
|
use Stratadox\Parser\Helpers\AtMost; |
8
|
|
|
use Stratadox\Parser\Helpers\Between; |
9
|
|
|
use Stratadox\Parser\Helpers\First; |
10
|
|
|
use Stratadox\Parser\Helpers\Item; |
11
|
|
|
use Stratadox\Parser\Helpers\Join; |
12
|
|
|
use Stratadox\Parser\Helpers\NonEmpty; |
13
|
|
|
use Stratadox\Parser\Helpers\Split; |
14
|
|
|
use Stratadox\Parser\Parsers\End; |
15
|
|
|
use Stratadox\Parser\Parsers\Except; |
16
|
|
|
use Stratadox\Parser\Parsers\Repeatable; |
17
|
|
|
use Stratadox\Parser\Parsers\Either; |
18
|
|
|
use Stratadox\Parser\Parsers\FullyMap; |
19
|
|
|
use Stratadox\Parser\Parsers\Ignore; |
20
|
|
|
use Stratadox\Parser\Parsers\Map; |
21
|
|
|
use Stratadox\Parser\Parsers\Maybe; |
22
|
|
|
use Stratadox\Parser\Parsers\Optional; |
23
|
|
|
use Stratadox\Parser\Parsers\Sequence; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Abstract Parser |
27
|
|
|
* |
28
|
|
|
* Provides shortcut methods for easy parser combining. |
29
|
|
|
*/ |
30
|
|
|
abstract class Parser |
31
|
|
|
{ |
32
|
|
|
abstract public function parse(string $input): Result; |
33
|
|
|
|
34
|
|
|
public function map(Closure $map): Parser |
35
|
|
|
{ |
36
|
|
|
return Map::the($this, $map); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function fullMap(Closure $map): Parser |
40
|
|
|
{ |
41
|
|
|
return FullyMap::the($this, $map); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function nonEmpty(): Parser |
45
|
|
|
{ |
46
|
|
|
return NonEmpty::result($this); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function maybe(): Parser |
50
|
|
|
{ |
51
|
|
|
return Maybe::that($this); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function ignore(): Parser |
55
|
|
|
{ |
56
|
|
|
return Ignore::the($this); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function optional(): Parser |
60
|
|
|
{ |
61
|
|
|
return Optional::ignored($this); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function except(Parser|string $refusal): Parser |
65
|
|
|
{ |
66
|
|
|
return Except::for($refusal, $this); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function join(string $glue = ''): Parser |
70
|
|
|
{ |
71
|
|
|
return Join::with($glue, $this); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function first(): Parser |
75
|
|
|
{ |
76
|
|
|
return First::of($this); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function item(int|string $n): Parser |
80
|
|
|
{ |
81
|
|
|
return Item::number($n, $this); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function between(Parser|string $from, Parser|string $to = null): Parser |
85
|
|
|
{ |
86
|
|
|
return Between::these($from, $to ?: $from, $this); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function split(Parser|string $separator): Parser |
90
|
|
|
{ |
91
|
|
|
return Split::optional($separator, $this); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function mustSplit(Parser|string $separator): Parser |
95
|
|
|
{ |
96
|
|
|
return Split::with($separator, $this); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function keepSplit(Parser|string|array $separator, ?Closure $map = null): Parser |
100
|
|
|
{ |
101
|
|
|
return Split::keep($separator, $this, $map); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function repeatable(): Parser |
105
|
|
|
{ |
106
|
|
|
return Repeatable::parser($this); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function repeatableString(): Parser |
110
|
|
|
{ |
111
|
|
|
return Join::the(Repeatable::parser($this)); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function or(Parser|string ...$other): Parser |
115
|
|
|
{ |
116
|
|
|
return Either::of($this, ...$other); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function andThen(Parser|string ...$other): Parser |
120
|
|
|
{ |
121
|
|
|
return Sequence::of($this, ...$other); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
public function atLeast(int $n): Parser |
125
|
|
|
{ |
126
|
|
|
return AtLeast::results($n, $this); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
public function atMost(int $n): Parser |
130
|
|
|
{ |
131
|
|
|
return AtMost::results($n, $this); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
public function end(): Parser |
135
|
|
|
{ |
136
|
|
|
return End::with($this); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|