|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of Railt package. |
|
4
|
|
|
* |
|
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
6
|
|
|
* file that was distributed with this source code. |
|
7
|
|
|
*/ |
|
8
|
|
|
declare(strict_types=1); |
|
9
|
|
|
|
|
10
|
|
|
namespace Railt\Lexer\Driver; |
|
11
|
|
|
|
|
12
|
|
|
use Railt\Io\Readable; |
|
13
|
|
|
use Railt\Lexer\Iterator\RegexNamedGroupsIterator; |
|
14
|
|
|
use Railt\Lexer\Result\Eoi; |
|
15
|
|
|
use Railt\Lexer\Result\Token; |
|
16
|
|
|
use Railt\Lexer\Result\Unknown; |
|
17
|
|
|
use Railt\Lexer\Stateful; |
|
18
|
|
|
use Railt\Lexer\TokenInterface; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Class NativeStateful |
|
22
|
|
|
*/ |
|
23
|
|
|
class NativeStateful implements Stateful |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* @var string |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $pattern; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var array |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $skipped; |
|
34
|
|
|
|
|
35
|
11 |
|
/** |
|
36
|
|
|
* NativeStateful constructor. |
|
37
|
11 |
|
* @param string $pattern |
|
38
|
11 |
|
* @param array $skipped |
|
39
|
11 |
|
*/ |
|
40
|
|
|
public function __construct(string $pattern, array $skipped = []) |
|
41
|
|
|
{ |
|
42
|
|
|
$this->pattern = $pattern; |
|
43
|
|
|
$this->skipped = $skipped; |
|
44
|
|
|
} |
|
45
|
17 |
|
|
|
46
|
|
|
/** |
|
47
|
17 |
|
* @param Readable $input |
|
48
|
17 |
|
* @return \Traversable |
|
49
|
|
|
* @throws \InvalidArgumentException |
|
50
|
17 |
|
* @throws \RuntimeException |
|
51
|
|
|
*/ |
|
52
|
17 |
|
public function lex(Readable $input): \Traversable |
|
53
|
|
|
{ |
|
54
|
17 |
|
foreach ($this->exec($this->pattern, $input->getContents()) as $token) { |
|
55
|
9 |
|
if (! \in_array($token->name(), $this->skipped, true)) { |
|
56
|
17 |
|
yield $token; |
|
57
|
|
|
} |
|
58
|
17 |
|
} |
|
59
|
|
|
} |
|
60
|
17 |
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @param string $pattern |
|
63
|
17 |
|
* @param string $content |
|
64
|
17 |
|
* @return \Traversable|TokenInterface[] |
|
65
|
|
|
* @throws \InvalidArgumentException |
|
66
|
|
|
* @throws \RuntimeException |
|
67
|
|
|
*/ |
|
68
|
|
|
protected function exec(string $pattern, string $content): \Traversable |
|
69
|
|
|
{ |
|
70
|
|
|
$offset = 0; |
|
71
|
9 |
|
$regex = new RegexNamedGroupsIterator($pattern, $content); |
|
72
|
|
|
|
|
73
|
9 |
|
$iterator = $regex->getIterator(); |
|
74
|
9 |
|
|
|
75
|
|
|
while ($iterator->valid()) { |
|
76
|
9 |
|
/** @var TokenInterface $token */ |
|
77
|
|
|
$token = $iterator->key() === Unknown::T_NAME |
|
78
|
9 |
|
? $this->unknown($iterator, $offset) |
|
79
|
|
|
: $this->token($iterator, $offset); |
|
80
|
|
|
|
|
81
|
|
|
$offset += $token->bytes(); |
|
82
|
|
|
|
|
83
|
|
|
yield $token; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
9 |
|
yield new Eoi($offset); |
|
87
|
|
|
} |
|
88
|
9 |
|
|
|
89
|
|
|
/** |
|
90
|
9 |
|
* @param \Traversable|\Generator $iterator |
|
91
|
9 |
|
* @param int $offset |
|
92
|
9 |
|
* @return Unknown |
|
93
|
|
|
*/ |
|
94
|
|
|
private function unknown(\Traversable $iterator, int $offset): TokenInterface |
|
95
|
3 |
|
{ |
|
96
|
|
|
$body = $iterator->current()[0]; |
|
|
|
|
|
|
97
|
3 |
|
$iterator->next(); |
|
|
|
|
|
|
98
|
|
|
|
|
99
|
|
|
$body .= $this->collapse($iterator, Unknown::T_NAME); |
|
100
|
9 |
|
|
|
101
|
|
|
return new Unknown($body, $offset); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* @param \Traversable|\Iterator $iterator |
|
106
|
|
|
* @param string $token |
|
107
|
|
|
* @return string |
|
108
|
17 |
|
*/ |
|
109
|
|
|
private function collapse(\Traversable $iterator, string $token): string |
|
110
|
17 |
|
{ |
|
111
|
|
|
$body = ''; |
|
112
|
17 |
|
|
|
113
|
|
|
while ($iterator->valid()) { |
|
|
|
|
|
|
114
|
17 |
|
if ($iterator->key() !== $token) { |
|
|
|
|
|
|
115
|
|
|
break; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
$body .= $iterator->current()[0]; |
|
|
|
|
|
|
119
|
|
|
|
|
120
|
|
|
$iterator->next(); |
|
|
|
|
|
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
return $body; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* @param \Traversable|\Iterator $iterator |
|
128
|
|
|
* @param int $offset |
|
129
|
|
|
* @return Token |
|
130
|
|
|
*/ |
|
131
|
|
|
private function token(\Traversable $iterator, int $offset): TokenInterface |
|
132
|
|
|
{ |
|
133
|
|
|
[$name, $context] = [$iterator->key(), $iterator->current()]; |
|
|
|
|
|
|
134
|
|
|
|
|
135
|
|
|
$iterator->next(); |
|
|
|
|
|
|
136
|
|
|
|
|
137
|
|
|
return new Token($name, $context, $offset); |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: