|
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\Common\PCRECompiler; |
|
14
|
|
|
use Railt\Lexer\Stateless; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Class NativeStateless |
|
18
|
|
|
*/ |
|
19
|
|
|
class NativeStateless extends NativeStateful implements Stateless |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* @var PCRECompiler |
|
23
|
|
|
*/ |
|
24
|
|
|
private $pcre; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* NativeStateless constructor. |
|
28
|
|
|
*/ |
|
29
|
|
|
public function __construct() |
|
30
|
3 |
|
{ |
|
31
|
|
|
$this->pcre = new PCRECompiler(); |
|
32
|
3 |
|
parent::__construct(''); |
|
33
|
3 |
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @param Readable $input |
|
37
|
|
|
* @return \Traversable |
|
38
|
|
|
* @throws \InvalidArgumentException |
|
39
|
|
|
* @throws \RuntimeException |
|
40
|
|
|
*/ |
|
41
|
11 |
|
public function lex(Readable $input): \Traversable |
|
42
|
|
|
{ |
|
43
|
11 |
|
foreach (parent::exec($this->pcre->compile(), $input->getContents()) as $token) { |
|
|
|
|
|
|
44
|
|
|
if (! \in_array($token->name(), $this->skipped, true)) { |
|
45
|
11 |
|
yield $token; |
|
46
|
4 |
|
} |
|
47
|
|
|
} |
|
48
|
|
|
} |
|
49
|
11 |
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @param string $name |
|
52
|
|
|
* @return Stateless |
|
53
|
|
|
*/ |
|
54
|
|
|
public function skip(string $name): Stateless |
|
55
|
|
|
{ |
|
56
|
2 |
|
$this->skipped[] = $name; |
|
57
|
|
|
|
|
58
|
2 |
|
return $this; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @param string $name |
|
63
|
|
|
* @param string $pcre |
|
64
|
2 |
|
* @param bool $skip |
|
65
|
|
|
* @return Stateless |
|
66
|
2 |
|
*/ |
|
67
|
|
|
public function add(string $name, string $pcre, bool $skip = false): Stateless |
|
68
|
|
|
{ |
|
69
|
|
|
$this->pcre->addToken($name, $pcre); |
|
|
|
|
|
|
70
|
|
|
|
|
71
|
|
|
return $this; |
|
|
|
|
|
|
72
|
|
|
} |
|
73
|
8 |
|
|
|
74
|
|
|
/** |
|
75
|
8 |
|
* @param string $name |
|
76
|
|
|
* @return bool |
|
77
|
8 |
|
*/ |
|
78
|
|
|
public function has(string $name): bool |
|
79
|
|
|
{ |
|
80
|
|
|
return \array_key_exists($name, $this->pcre->getTokens()); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @return iterable |
|
85
|
|
|
*/ |
|
86
|
|
|
public function getDefinedTokens(): iterable |
|
87
|
|
|
{ |
|
88
|
|
|
return $this->pcre->getTokens(); |
|
|
|
|
|
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @param string $name |
|
93
|
|
|
* @return bool |
|
94
|
|
|
*/ |
|
95
|
|
|
public function isSkipped(string $name): bool |
|
96
|
|
|
{ |
|
97
|
|
|
return \in_array($name, $this->skipped, true); |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|
This check looks for a call to a parent method whose name is different than the method from which it is called.
Consider the following code:
The
getFirstName()method in theSoncalls the wrong method in the parent class.