|
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\NativeStateful; |
|
11
|
|
|
|
|
12
|
|
|
use Railt\Lexer\Result\Unknown; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Class PCRECompiler |
|
16
|
|
|
*/ |
|
17
|
|
|
class PCRECompiler |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* Regex delimiter |
|
21
|
|
|
*/ |
|
22
|
|
|
protected const REGEX_DELIMITER = '/'; |
|
23
|
|
|
|
|
24
|
|
|
private const FLAG_UNICODE = 'u'; |
|
25
|
|
|
private const FLAG_DOT_ALL = 's'; |
|
26
|
|
|
private const FLAG_ANALYZED = 'S'; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var array|string[] |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $tokens = []; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* PCRECompiler constructor. |
|
35
|
|
|
* @param array $tokens |
|
36
|
|
|
*/ |
|
37
|
6 |
|
public function __construct(array $tokens = []) |
|
38
|
|
|
{ |
|
39
|
6 |
|
$this->tokens = $tokens; |
|
40
|
6 |
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param string $token |
|
44
|
|
|
* @param string $pcre |
|
45
|
|
|
* @return PCRECompiler |
|
46
|
|
|
*/ |
|
47
|
|
|
public function add(string $token, string $pcre): self |
|
48
|
|
|
{ |
|
49
|
|
|
$this->tokens[$token] = $pcre; |
|
50
|
|
|
|
|
51
|
|
|
return $this; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @return string |
|
56
|
|
|
*/ |
|
57
|
6 |
|
public function compile(): string |
|
58
|
|
|
{ |
|
59
|
6 |
|
$tokens = \array_merge($this->tokens, [Unknown::T_NAME => '.*?']); |
|
60
|
|
|
|
|
61
|
6 |
|
return $this->tokensToPattern($tokens); |
|
|
|
|
|
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @param iterable $tokens |
|
66
|
|
|
* @return string |
|
67
|
|
|
*/ |
|
68
|
6 |
|
private function tokensToPattern(iterable $tokens): string |
|
69
|
|
|
{ |
|
70
|
6 |
|
$tokensList = []; |
|
71
|
|
|
|
|
72
|
6 |
|
foreach ($tokens as $name => $pcre) { |
|
73
|
6 |
|
$name = $this->escapeTokenName($name); |
|
74
|
6 |
|
$value = $this->escapeTokenPattern($pcre); |
|
75
|
|
|
|
|
76
|
6 |
|
$tokensList[] = \sprintf('(?P<%s>%s)', \trim($name), $value); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
6 |
|
$pcre = \implode('|', $tokensList); |
|
80
|
|
|
|
|
81
|
6 |
|
return \sprintf('%s\G%s%1$s%s', self::REGEX_DELIMITER, $pcre, $this->renderFlags()); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @param string $pattern |
|
86
|
|
|
* @return string |
|
87
|
|
|
*/ |
|
88
|
6 |
|
protected function escapeTokenName(string $pattern): string |
|
89
|
|
|
{ |
|
90
|
6 |
|
return \preg_quote($pattern, static::REGEX_DELIMITER); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @param string $value |
|
95
|
|
|
* @return string |
|
96
|
|
|
*/ |
|
97
|
6 |
|
protected function escapeTokenPattern(string $value): string |
|
98
|
|
|
{ |
|
99
|
6 |
|
return \str_replace(static::REGEX_DELIMITER, '\\' . self::REGEX_DELIMITER, $value); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @return string |
|
104
|
|
|
*/ |
|
105
|
6 |
|
private function renderFlags(): string |
|
106
|
|
|
{ |
|
107
|
6 |
|
return self::FLAG_UNICODE . self::FLAG_DOT_ALL . self::FLAG_ANALYZED; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @return array |
|
112
|
|
|
*/ |
|
113
|
|
|
public function getTokens(): array |
|
114
|
|
|
{ |
|
115
|
|
|
return $this->tokens; |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: