function JS::getRegexToken
Tokenizes a regular expression if one is found.
If a regular expression is not found, NULL is returned.
Parameters
int $char The index of the possible regex start character.:
string $string The complete content of the string being tokenized.:
array $chars An array of characters being tokenized.:
array $tokens The current array of tokens found in the string.:
Return value
array<string, string>|null
1 call to JS::getRegexToken()
- JS::tokenize in vendor/
squizlabs/ php_codesniffer/ src/ Tokenizers/ JS.php - Creates an array of tokens when given some JS code.
File
-
vendor/
squizlabs/ php_codesniffer/ src/ Tokenizers/ JS.php, line 915
Class
Namespace
PHP_CodeSniffer\TokenizersCode
public function getRegexToken($char, $string, $chars, $tokens) {
$beforeTokens = [
T_EQUAL => true,
T_IS_NOT_EQUAL => true,
T_IS_IDENTICAL => true,
T_IS_NOT_IDENTICAL => true,
T_OPEN_PARENTHESIS => true,
T_OPEN_SQUARE_BRACKET => true,
T_RETURN => true,
T_BOOLEAN_OR => true,
T_BOOLEAN_AND => true,
T_BOOLEAN_NOT => true,
T_BITWISE_OR => true,
T_BITWISE_AND => true,
T_COMMA => true,
T_COLON => true,
T_TYPEOF => true,
T_INLINE_THEN => true,
T_INLINE_ELSE => true,
];
$afterTokens = [
',' => true,
')' => true,
']' => true,
';' => true,
' ' => true,
'.' => true,
':' => true,
$this->eolChar => true,
];
// Find the last non-whitespace token that was added
// to the tokens array.
$numTokens = count($tokens);
for ($prev = $numTokens - 1; $prev >= 0; $prev--) {
if (isset(Util\Tokens::$emptyTokens[$tokens[$prev]['code']]) === false) {
break;
}
}
if (isset($beforeTokens[$tokens[$prev]['code']]) === false) {
return null;
}
// This is probably a regular expression, so look for the end of it.
if (PHP_CODESNIFFER_VERBOSITY > 1) {
echo "\t* token possibly starts a regular expression *" . PHP_EOL;
}
$numChars = count($chars);
for ($next = $char + 1; $next < $numChars; $next++) {
if ($chars[$next] === '/') {
// Just make sure this is not escaped first.
if ($chars[$next - 1] !== '\\') {
// In the simple form: /.../ so we found the end.
break;
}
else {
if ($chars[$next - 2] === '\\') {
// In the form: /...\\/ so we found the end.
break;
}
}
}
else {
$possibleEolChar = substr($string, $next, strlen($this->eolChar));
if ($possibleEolChar === $this->eolChar) {
// This is the last token on the line and regular
// expressions need to be defined on a single line,
// so this is not a regular expression.
break;
}
}
}
if ($chars[$next] !== '/') {
if (PHP_CODESNIFFER_VERBOSITY > 1) {
echo "\t* could not find end of regular expression *" . PHP_EOL;
}
return null;
}
while (preg_match('|[a-zA-Z]|', $chars[$next + 1]) !== 0) {
// The token directly after the end of the regex can
// be modifiers like global and case insensitive
// (.e.g, /pattern/gi).
$next++;
}
$regexEnd = $next;
if (PHP_CODESNIFFER_VERBOSITY > 1) {
echo "\t* found end of regular expression at token {$regexEnd} *" . PHP_EOL;
}
for ($next += 1; $next < $numChars; $next++) {
if ($chars[$next] !== ' ') {
break;
}
else {
$possibleEolChar = substr($string, $next, strlen($this->eolChar));
if ($possibleEolChar === $this->eolChar) {
// This is the last token on the line.
break;
}
}
}
if (isset($afterTokens[$chars[$next]]) === false) {
if (PHP_CODESNIFFER_VERBOSITY > 1) {
echo "\t* tokens after regular expression do not look correct *" . PHP_EOL;
}
return null;
}
// This is a regular expression, so join all the tokens together.
$content = '';
for ($x = $char; $x <= $regexEnd; $x++) {
$content .= $chars[$x];
}
$token = [
'start' => $char,
'end' => $regexEnd,
'content' => $content,
];
return $token;
}