2
2
3
3
namespace PHPStan \PhpDocParser \Lexer ;
4
4
5
- use function array_keys ;
6
- use function assert ;
7
- use function count ;
8
5
use function implode ;
9
6
use function preg_match_all ;
10
7
use const PREG_SET_ORDER ;
@@ -93,23 +90,17 @@ class Lexer
93
90
/** @var string|null */
94
91
private $ regexp ;
95
92
96
- /** @var int[]|null */
97
- private $ types ;
98
-
99
93
public function tokenize (string $ s ): array
100
94
{
101
- if ($ this ->regexp === null || $ this -> types === null ) {
102
- $ this ->initialize ();
95
+ if ($ this ->regexp === null ) {
96
+ $ this ->regexp = $ this -> generateRegexp ();
103
97
}
104
98
105
- assert ($ this ->regexp !== null );
106
- assert ($ this ->types !== null );
107
-
108
99
preg_match_all ($ this ->regexp , $ s , $ matches , PREG_SET_ORDER );
109
100
110
101
$ tokens = [];
111
102
foreach ($ matches as $ match ) {
112
- $ tokens [] = [$ match [0 ], $ this -> types [ count ( $ match ) - 2 ]];
103
+ $ tokens [] = [$ match [0 ], ( int ) $ match [ ' MARK ' ]];
113
104
}
114
105
115
106
$ tokens [] = ['' , self ::TOKEN_END ];
@@ -118,7 +109,7 @@ public function tokenize(string $s): array
118
109
}
119
110
120
111
121
- private function initialize (): void
112
+ private function generateRegexp (): string
122
113
{
123
114
$ patterns = [
124
115
self ::TOKEN_HORIZONTAL_WS => '[ \\x09 \\x20]++ ' ,
@@ -166,8 +157,11 @@ private function initialize(): void
166
157
self ::TOKEN_OTHER => '(?:(?! \\*/)[^ \\s])++ ' ,
167
158
];
168
159
169
- $ this ->regexp = '~( ' . implode (')|( ' , $ patterns ) . ')~Asi ' ;
170
- $ this ->types = array_keys ($ patterns );
160
+ foreach ($ patterns as $ type => &$ pattern ) {
161
+ $ pattern = '(?: ' . $ pattern . ')(*MARK: ' . $ type . ') ' ;
162
+ }
163
+
164
+ return '~ ' . implode ('| ' , $ patterns ) . '~Asi ' ;
171
165
}
172
166
173
167
}
0 commit comments