|
2 | 2 |
|
3 | 3 | namespace PHPStan\PhpDocParser\Lexer; |
4 | 4 |
|
| 5 | +use PHPStan\PhpDocParser\Parser\Phplrt\TokenStream; |
5 | 6 | use PHPStan\PhpDocParser\ParserConfig; |
6 | | -use function implode; |
7 | | -use function preg_match_all; |
8 | 7 |
|
9 | 8 | /** |
10 | 9 | * Implementation based on Nette Tokenizer (New BSD License; https://github.com/nette/tokenizer) |
| 10 | + * |
| 11 | + * The pattern is written out of "doc/grammars/lexemes.pp3" by "tools/phplrt/build.php". |
11 | 12 | */ |
12 | 13 | class Lexer |
13 | 14 | { |
@@ -99,110 +100,23 @@ class Lexer |
99 | 100 |
|
100 | 101 | private ParserConfig $config; // @phpstan-ignore property.onlyWritten |
101 | 102 |
|
102 | | - private ?string $regexp = null; |
103 | | - |
104 | 103 | public function __construct(ParserConfig $config) |
105 | 104 | { |
106 | 105 | $this->config = $config; |
107 | 106 | } |
108 | 107 |
|
109 | 108 | /** |
110 | | - * @return list<array{string, int, int}> |
| 109 | + * The tokens of the pattern written out of "doc/grammars/lexemes.pp3", |
| 110 | + * ending with TOKEN_END. |
| 111 | + * |
| 112 | + * The last token carries what the parsers read along: keep the tokens |
| 113 | + * together when handing them over to a {@see TokenIterator}. |
| 114 | + * |
| 115 | + * @return list<array{0: string, 1: int, 2: int, 3?: array<int, int>, 4?: list<int>}> |
111 | 116 | */ |
112 | 117 | public function tokenize(string $s): array |
113 | 118 | { |
114 | | - if ($this->regexp === null) { |
115 | | - $this->regexp = $this->generateRegexp(); |
116 | | - } |
117 | | - |
118 | | - // PREG_PATTERN_ORDER, not PREG_SET_ORDER: it collects the whole PHPDoc |
119 | | - // into two arrays instead of allocating one array per token. This only |
120 | | - // pays off while the token patterns have no capturing groups, because |
121 | | - // every group would get an array of its own, one entry per token. |
122 | | - preg_match_all($this->regexp, $s, $matches); |
123 | | - |
124 | | - $values = $matches[0]; |
125 | | - if ($values === []) { |
126 | | - return [['', self::TOKEN_END, 1]]; |
127 | | - } |
128 | | - |
129 | | - $marks = $matches['MARK']; |
130 | | - |
131 | | - $tokens = []; |
132 | | - $line = 1; |
133 | | - foreach ($values as $i => $value) { |
134 | | - $type = (int) $marks[$i]; |
135 | | - $tokens[] = [$value, $type, $line]; |
136 | | - if ($type !== self::TOKEN_PHPDOC_EOL) { |
137 | | - continue; |
138 | | - } |
139 | | - |
140 | | - $line++; |
141 | | - } |
142 | | - |
143 | | - $tokens[] = ['', self::TOKEN_END, $line]; |
144 | | - |
145 | | - return $tokens; |
146 | | - } |
147 | | - |
148 | | - private function generateRegexp(): string |
149 | | - { |
150 | | - // every group in here must be non-capturing, see tokenize() |
151 | | - $patterns = [ |
152 | | - self::TOKEN_HORIZONTAL_WS => '[\\x09\\x20]++', |
153 | | - |
154 | | - self::TOKEN_IDENTIFIER => '(?:[\\\\]?+[a-z_\\x80-\\xFF][0-9a-z_\\x80-\\xFF-]*+)++', |
155 | | - self::TOKEN_THIS_VARIABLE => '\\$this(?![0-9a-z_\\x80-\\xFF])', |
156 | | - self::TOKEN_VARIABLE => '\\$[a-z_\\x80-\\xFF][0-9a-z_\\x80-\\xFF]*+', |
157 | | - |
158 | | - // '&' followed by TOKEN_VARIADIC, TOKEN_VARIABLE, TOKEN_EQUAL, TOKEN_EQUAL or TOKEN_CLOSE_PARENTHESES |
159 | | - self::TOKEN_REFERENCE => '&(?=\\s*+(?:[,=)]|\\.\\.\\.|(?:\\$(?!this(?![0-9a-z_\\x80-\\xFF])))))', |
160 | | - self::TOKEN_UNION => '\\|', |
161 | | - self::TOKEN_INTERSECTION => '&', |
162 | | - self::TOKEN_NULLABLE => '\\?', |
163 | | - self::TOKEN_NEGATED => '!', |
164 | | - |
165 | | - self::TOKEN_OPEN_PARENTHESES => '\\(', |
166 | | - self::TOKEN_CLOSE_PARENTHESES => '\\)', |
167 | | - self::TOKEN_OPEN_ANGLE_BRACKET => '<', |
168 | | - self::TOKEN_CLOSE_ANGLE_BRACKET => '>', |
169 | | - self::TOKEN_OPEN_SQUARE_BRACKET => '\\[', |
170 | | - self::TOKEN_CLOSE_SQUARE_BRACKET => '\\]', |
171 | | - self::TOKEN_OPEN_CURLY_BRACKET => '\\{', |
172 | | - self::TOKEN_CLOSE_CURLY_BRACKET => '\\}', |
173 | | - |
174 | | - self::TOKEN_COMMA => ',', |
175 | | - self::TOKEN_COMMENT => '\/\/[^\\r\\n]*(?=\n|\r|\*/)', |
176 | | - self::TOKEN_VARIADIC => '\\.\\.\\.', |
177 | | - self::TOKEN_DOUBLE_COLON => '::', |
178 | | - self::TOKEN_DOUBLE_ARROW => '=>', |
179 | | - self::TOKEN_ARROW => '->', |
180 | | - self::TOKEN_EQUAL => '=', |
181 | | - self::TOKEN_COLON => ':', |
182 | | - |
183 | | - self::TOKEN_OPEN_PHPDOC => '/\\*\\*(?=\\s)\\x20?+', |
184 | | - self::TOKEN_CLOSE_PHPDOC => '\\*/', |
185 | | - self::TOKEN_PHPDOC_TAG => '@(?:[a-z][a-z0-9-\\\\]+:)?[a-z][a-z0-9-\\\\]*+', |
186 | | - self::TOKEN_DOCTRINE_TAG => '@[a-z_\\\\][a-z0-9_\:\\\\]*[a-z_][a-z0-9_]*', |
187 | | - self::TOKEN_PHPDOC_EOL => '\\r?+\\n[\\x09\\x20]*+(?:\\*(?!/)\\x20?+)?', |
188 | | - |
189 | | - self::TOKEN_FLOAT => '[+\-]?(?:(?:[0-9]++(?:_[0-9]++)*\\.[0-9]*+(?:_[0-9]++)*(?:e[+\-]?[0-9]++(?:_[0-9]++)*)?)|(?:[0-9]*+(?:_[0-9]++)*\\.[0-9]++(?:_[0-9]++)*(?:e[+\-]?[0-9]++(?:_[0-9]++)*)?)|(?:[0-9]++(?:_[0-9]++)*e[+\-]?[0-9]++(?:_[0-9]++)*))', |
190 | | - self::TOKEN_INTEGER => '[+\-]?(?:(?:0b[0-1]++(?:_[0-1]++)*)|(?:0o[0-7]++(?:_[0-7]++)*)|(?:0x[0-9a-f]++(?:_[0-9a-f]++)*)|(?:[0-9]++(?:_[0-9]++)*))', |
191 | | - self::TOKEN_SINGLE_QUOTED_STRING => '\'(?:\\\\[^\\r\\n]|[^\'\\r\\n\\\\])*+\'', |
192 | | - self::TOKEN_DOUBLE_QUOTED_STRING => '"(?:\\\\[^\\r\\n]|[^"\\r\\n\\\\])*+"', |
193 | | - self::TOKEN_DOCTRINE_ANNOTATION_STRING => '"(?:""|[^"])*+"', |
194 | | - |
195 | | - self::TOKEN_WILDCARD => '\\*', |
196 | | - |
197 | | - // anything but TOKEN_CLOSE_PHPDOC or TOKEN_HORIZONTAL_WS or TOKEN_EOL |
198 | | - self::TOKEN_OTHER => '(?:(?!\\*/)[^\\s])++', |
199 | | - ]; |
200 | | - |
201 | | - foreach ($patterns as $type => &$pattern) { |
202 | | - $pattern = '(?:' . $pattern . ')(*MARK:' . $type . ')'; |
203 | | - } |
204 | | - |
205 | | - return '~' . implode('|', $patterns) . '~Asi'; |
| 119 | + return TokenStream::tokenize($s); |
206 | 120 | } |
207 | 121 |
|
208 | 122 | } |
0 commit comments