Skip to content

Commit fda3d6e

Browse files
committed
Build regexes from lists using deduplicated function
1 parent 19bb3ab commit fda3d6e

File tree

1 file changed

+15
-32
lines changed

1 file changed

+15
-32
lines changed

src/Tokenizer.php

Lines changed: 15 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -771,24 +771,22 @@ public function __construct()
771771
return array_keys($valuesMap);
772772
};
773773

774+
$buildRegexFromListFx = static function ($values) use ($sortByLengthFx) {
775+
return '(?>' . implode(
776+
'|',
777+
array_map(
778+
static fn ($v) => preg_quote($v, '/'),
779+
$sortByLengthFx($values),
780+
),
781+
) . ')';
782+
};
783+
774784
// Set up regular expressions
775-
$regexBoundaries = '(?>' . implode(
776-
'|',
777-
$this->quoteRegex($this->boundaries),
778-
) . ')';
779-
$regexReserved = '(?>' . implode(
780-
'|',
781-
$this->quoteRegex($sortByLengthFx($this->reserved)),
782-
) . ')';
783-
$regexReservedToplevel = '(?>' . str_replace(' ', '\s+', implode(
784-
'|',
785-
$this->quoteRegex($sortByLengthFx($this->reservedToplevel)),
786-
)) . ')';
787-
$regexReservedNewline = '(?>' . str_replace(' ', '\s+', implode(
788-
'|',
789-
$this->quoteRegex($sortByLengthFx($this->reservedNewline)),
790-
)) . ')';
791-
$regexFunction = '(?>' . implode('|', $this->quoteRegex($sortByLengthFx($this->functions))) . ')';
785+
$regexBoundaries = $buildRegexFromListFx($this->boundaries);
786+
$regexReserved = $buildRegexFromListFx($this->reserved);
787+
$regexReservedToplevel = str_replace(' ', '\s+', $buildRegexFromListFx($this->reservedToplevel));
788+
$regexReservedNewline = str_replace(' ', '\s+', $buildRegexFromListFx($this->reservedNewline));
789+
$regexFunction = $buildRegexFromListFx($this->functions);
792790

793791
$this->nextTokenRegexNumber = '/\G(?:\d+(?:\.\d+)?|0x[\da-fA-F]+|0b[01]+)(?=$|\s|"\'`|' . $regexBoundaries . ')/';
794792
$this->nextTokenRegexBoundaryCharacter = '/\G' . $regexBoundaries . '/';
@@ -987,21 +985,6 @@ private function createNextToken(string $string, string $upper, int $offset, Tok
987985
return new Token(Token::TOKEN_TYPE_WORD, $matches[0]);
988986
}
989987

990-
/**
991-
* Helper function for building regular expressions for reserved words and boundary characters
992-
*
993-
* @param string[] $strings The strings to be quoted
994-
*
995-
* @return string[] The quoted strings
996-
*/
997-
private function quoteRegex(array $strings): array
998-
{
999-
return array_map(
1000-
static fn (string $string): string => preg_quote($string, '/'),
1001-
$strings,
1002-
);
1003-
}
1004-
1005988
private function getNextQuotedString(string $string, int $offset): string
1006989
{
1007990
$ret = '';

0 commit comments

Comments
 (0)