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\Compiler\Lexer; |
11
|
|
|
|
12
|
|
|
use Railt\Compiler\Iterator\RegexNamedGroupsIterator; |
13
|
|
|
use Railt\Compiler\Lexer\Result\Eoi; |
14
|
|
|
use Railt\Compiler\Lexer\Result\Token; |
15
|
|
|
use Railt\Compiler\Lexer\Result\Unknown; |
16
|
|
|
use Railt\Compiler\TokenInterface; |
17
|
|
|
use Railt\Io\Readable; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Class NativeStateful |
21
|
|
|
*/ |
22
|
|
|
class NativeStateful implements Stateful |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
protected $pattern; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var array |
31
|
|
|
*/ |
32
|
|
|
protected $skipped; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* NativeStateful constructor. |
36
|
|
|
* @param string $pattern |
37
|
|
|
* @param array $skipped |
38
|
|
|
*/ |
39
|
54 |
|
public function __construct(string $pattern, array $skipped = []) |
40
|
|
|
{ |
41
|
54 |
|
$this->pattern = $pattern; |
42
|
54 |
|
$this->skipped = $skipped; |
43
|
54 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param Readable $input |
47
|
|
|
* @return \Traversable |
48
|
|
|
*/ |
49
|
54 |
|
public function lex(Readable $input): \Traversable |
50
|
|
|
{ |
51
|
54 |
|
foreach ($this->exec($this->pattern, $input->getContents()) as $token) { |
52
|
54 |
|
if (! \in_array($token->name(), $this->skipped, true)) { |
53
|
54 |
|
yield $token; |
54
|
|
|
} |
55
|
|
|
} |
56
|
54 |
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param string $pattern |
60
|
|
|
* @param string $content |
61
|
|
|
* @return \Traversable|TokenInterface[] |
62
|
|
|
*/ |
63
|
54 |
|
protected function exec(string $pattern, string $content): \Traversable |
64
|
|
|
{ |
65
|
54 |
|
$offset = 0; |
66
|
54 |
|
$regex = new RegexNamedGroupsIterator($pattern, $content); |
67
|
|
|
|
68
|
54 |
|
$iterator = $regex->getIterator(); |
69
|
|
|
|
70
|
54 |
|
while ($iterator->valid()) { |
71
|
|
|
/** @var TokenInterface $token */ |
72
|
54 |
|
$token = $iterator->key() === Token::UNKNOWN_TOKEN |
73
|
|
|
? $this->unknown($iterator, $offset) |
74
|
54 |
|
: $this->token($iterator, $offset); |
75
|
|
|
|
76
|
54 |
|
$offset += $token->bytes(); |
77
|
|
|
|
78
|
54 |
|
yield $token; |
79
|
|
|
} |
80
|
|
|
|
81
|
54 |
|
yield new Eoi($offset); |
82
|
54 |
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param \Traversable $iterator |
86
|
|
|
* @param int $offset |
87
|
|
|
* @return Unknown |
88
|
|
|
*/ |
89
|
|
|
private function unknown(\Traversable $iterator, int $offset): TokenInterface |
90
|
|
|
{ |
91
|
|
|
$body = $iterator->current()[0]; |
|
|
|
|
92
|
|
|
$iterator->next(); |
|
|
|
|
93
|
|
|
|
94
|
|
|
$body .= $this->collapse($iterator, TokenInterface::UNKNOWN_TOKEN); |
95
|
|
|
|
96
|
|
|
return new Unknown($body, $offset); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param \Traversable $iterator |
101
|
|
|
* @return string |
102
|
|
|
*/ |
103
|
|
|
private function collapse(\Traversable $iterator, string $token): string |
104
|
|
|
{ |
105
|
|
|
$body = ''; |
106
|
|
|
|
107
|
|
|
while ($iterator->valid()) { |
|
|
|
|
108
|
|
|
if ($iterator->key() !== $token) { |
|
|
|
|
109
|
|
|
break; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
$body .= $iterator->current()[0]; |
|
|
|
|
113
|
|
|
|
114
|
|
|
$iterator->next(); |
|
|
|
|
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return $body; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param \Traversable $iterator |
122
|
|
|
* @param int $offset |
123
|
|
|
* @return Token |
124
|
|
|
*/ |
125
|
54 |
|
private function token(\Traversable $iterator, int $offset) |
126
|
|
|
{ |
127
|
54 |
|
[$name, $context] = [$iterator->key(), $iterator->current()]; |
|
|
|
|
128
|
|
|
|
129
|
54 |
|
$iterator->next(); |
|
|
|
|
130
|
|
|
|
131
|
54 |
|
return new Token($name, $context, $offset); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
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: