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