Skip to content

Commit

Permalink
Micro-optimize lexer
Browse files Browse the repository at this point in the history
  • Loading branch information
rvanvelzen authored and ondrejmirtes committed Jun 14, 2022
1 parent 76150ae commit 4a07085
Showing 1 changed file with 9 additions and 15 deletions.
24 changes: 9 additions & 15 deletions src/Lexer/Lexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@

namespace PHPStan\PhpDocParser\Lexer;

use function array_keys;
use function assert;
use function count;
use function implode;
use function preg_match_all;
use const PREG_SET_ORDER;
Expand Down Expand Up @@ -93,23 +90,17 @@ class Lexer
/** @var string|null */
private $regexp;

/** @var int[]|null */
private $types;

public function tokenize(string $s): array
{
if ($this->regexp === null || $this->types === null) {
$this->initialize();
if ($this->regexp === null) {
$this->regexp = $this->generateRegexp();
}

assert($this->regexp !== null);
assert($this->types !== null);

preg_match_all($this->regexp, $s, $matches, PREG_SET_ORDER);

$tokens = [];
foreach ($matches as $match) {
$tokens[] = [$match[0], $this->types[count($match) - 2]];
$tokens[] = [$match[0], (int) $match['MARK']];
}

$tokens[] = ['', self::TOKEN_END];
Expand All @@ -118,7 +109,7 @@ public function tokenize(string $s): array
}


private function initialize(): void
private function generateRegexp(): string
{
$patterns = [
self::TOKEN_HORIZONTAL_WS => '[\\x09\\x20]++',
Expand Down Expand Up @@ -166,8 +157,11 @@ private function initialize(): void
self::TOKEN_OTHER => '(?:(?!\\*/)[^\\s])++',
];

$this->regexp = '~(' . implode(')|(', $patterns) . ')~Asi';
$this->types = array_keys($patterns);
foreach ($patterns as $type => &$pattern) {
$pattern = '(?:' . $pattern . ')(*MARK:' . $type . ')';
}

return '~' . implode('|', $patterns) . '~Asi';
}

}

0 comments on commit 4a07085

Please sign in to comment.