Skip to content

Commit

Permalink
Optimize Lexer::tokenize()
Browse files Browse the repository at this point in the history
  • Loading branch information
rvanvelzen authored and ondrejmirtes committed Jun 10, 2022
1 parent 07c49b0 commit 5aaeb83
Showing 1 changed file with 5 additions and 10 deletions.
15 changes: 5 additions & 10 deletions src/Lexer/Lexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,16 +105,11 @@ public function tokenize(string $s): array
assert($this->regexp !== null);
assert($this->types !== null);

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

$count = count($this->types);
foreach ($tokens as &$match) {
for ($i = 1; $i <= $count; $i++) {
if ($match[$i] !== null && $match[$i] !== '') {
$match = [$match[0], $this->types[$i - 1]];
break;
}
}
preg_match_all($this->regexp, $s, $matches, PREG_SET_ORDER);

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

$tokens[] = ['', self::TOKEN_END];
Expand Down

0 comments on commit 5aaeb83

Please sign in to comment.