1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of Lexer 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 Parle\Lexer as Parle; |
13
|
|
|
use Parle\LexerException; |
14
|
|
|
use Parle\Token as InternalToken; |
15
|
|
|
use Railt\Io\Readable; |
16
|
|
|
use Railt\Lexer\Definition\TokenDefinition; |
17
|
|
|
use Railt\Lexer\Exception\BadLexemeException; |
18
|
|
|
use Railt\Lexer\LexerInterface; |
19
|
|
|
use Railt\Lexer\Result\Eoi; |
20
|
|
|
use Railt\Lexer\Result\Token; |
21
|
|
|
use Railt\Lexer\Result\Unknown; |
22
|
|
|
use Railt\Lexer\TokenInterface; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Class ParleStateless |
26
|
|
|
*/ |
27
|
|
|
class ParleLexer extends SimpleLexer |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @var array|string[] |
31
|
|
|
*/ |
32
|
|
|
private $map = []; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var int |
36
|
|
|
*/ |
37
|
|
|
private $id = 1; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var Parle |
41
|
|
|
*/ |
42
|
|
|
private $lexer; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* ParleStateless constructor. |
46
|
|
|
* @param array $tokens |
47
|
|
|
* @param array $skip |
48
|
|
|
* @throws BadLexemeException |
49
|
|
|
*/ |
50
|
|
|
public function __construct(array $tokens = [], array $skip = []) |
51
|
|
|
{ |
52
|
|
|
\assert(\class_exists(Parle::class, false)); |
53
|
|
|
|
54
|
|
|
$this->lexer = new Parle(); |
55
|
|
|
$this->skipped = $skip; |
56
|
|
|
|
57
|
|
|
foreach ($tokens as $name => $pcre) { |
58
|
|
|
$this->add($name, $pcre); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param string $name |
64
|
|
|
* @param string $pcre |
65
|
|
|
* @return LexerInterface |
66
|
|
|
* @throws BadLexemeException |
67
|
|
|
*/ |
68
|
|
|
public function add(string $name, string $pcre): LexerInterface |
69
|
|
|
{ |
70
|
|
|
try { |
71
|
|
|
$this->lexer->push($pcre, $this->id); |
72
|
|
|
|
73
|
|
|
$this->map[$this->id] = $name; |
74
|
|
|
$this->tokens[$this->id] = $pcre; |
75
|
|
|
} catch (LexerException $e) { |
|
|
|
|
76
|
|
|
$message = \preg_replace('/rule\h+id\h+\d+/iu', 'token ' . $name, $e->getMessage()); |
77
|
|
|
|
78
|
|
|
throw new BadLexemeException($message); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
++$this->id; |
82
|
|
|
|
83
|
|
|
return $this; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param Readable $input |
88
|
|
|
* @return \Traversable|TokenInterface[] |
89
|
|
|
*/ |
90
|
|
|
protected function exec(Readable $input): \Traversable |
91
|
|
|
{ |
92
|
|
|
$iterator = $this->getInnerIterator($input); |
93
|
|
|
|
94
|
|
|
while ($iterator->valid()) { |
95
|
|
|
/** @var InternalToken $current */ |
96
|
|
|
$current = $iterator->current(); |
97
|
|
|
|
98
|
|
|
/** @var TokenInterface $token */ |
99
|
|
|
yield $current->id === InternalToken::UNKNOWN |
100
|
|
|
? $this->unknown($iterator) |
101
|
|
|
: $this->token($iterator); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
yield new Eoi($this->lexer->marker); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param Readable $input |
109
|
|
|
* @return \Generator|\Traversable |
110
|
|
|
*/ |
111
|
|
|
protected function getInnerIterator(Readable $input): \Traversable |
112
|
|
|
{ |
113
|
|
|
$this->lexer->build(); |
114
|
|
|
$this->lexer->consume($input->getContents()); |
115
|
|
|
|
116
|
|
|
$this->lexer->advance(); |
117
|
|
|
$token = $this->lexer->getToken(); |
118
|
|
|
|
119
|
|
|
while ($token->id !== InternalToken::EOI) { |
120
|
|
|
yield $token->id => $token; |
121
|
|
|
|
122
|
|
|
$this->lexer->advance(); |
123
|
|
|
$token = $this->lexer->getToken(); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param \Traversable|\Generator $iterator |
129
|
|
|
* @return Unknown |
130
|
|
|
*/ |
131
|
|
|
private function unknown(\Traversable $iterator): TokenInterface |
132
|
|
|
{ |
133
|
|
|
/** @var InternalToken $current */ |
134
|
|
|
$current = $iterator->current(); |
|
|
|
|
135
|
|
|
$offset = $this->lexer->marker; |
136
|
|
|
$body = ''; |
137
|
|
|
|
138
|
|
|
while ($current->id === InternalToken::UNKNOWN) { |
139
|
|
|
$body .= $current->value; |
140
|
|
|
$iterator->next(); |
|
|
|
|
141
|
|
|
$current = $iterator->current(); |
|
|
|
|
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
return new Unknown($body, $offset); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @param \Traversable|\Iterator $iterator |
149
|
|
|
* @return Token |
150
|
|
|
*/ |
151
|
|
|
private function token(\Traversable $iterator): TokenInterface |
152
|
|
|
{ |
153
|
|
|
/** @var InternalToken $current */ |
154
|
|
|
$current = $iterator->current(); |
|
|
|
|
155
|
|
|
|
156
|
|
|
$iterator->next(); |
|
|
|
|
157
|
|
|
|
158
|
|
|
return new Token($this->map[$current->id], $current->value, $this->lexer->marker); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @return iterable|TokenDefinition[] |
163
|
|
|
*/ |
164
|
|
|
public function getTokenDefinitions(): iterable |
165
|
|
|
{ |
166
|
|
View Code Duplication |
foreach ($this->tokens as $id => $pcre) { |
|
|
|
|
167
|
|
|
$name = $this->map[$id]; |
168
|
|
|
|
169
|
|
|
yield new TokenDefinition($name, $pcre, ! \in_array($name, $this->skipped, true)); |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|
Scrutinizer analyzes your
composer.json
/composer.lock
file if available to determine the classes, and functions that are defined by your dependencies.It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.