function Parser::checkInvalidEscapeSequences
Checks if the given string or number contains invalid escape sequences
Parameters
string $val Value to check:
bool $number True if the value is a number:
bool $forceLegacyOctalCheck True to force legacy octal: form check
bool $taggedTemplate True if the value is a tagged: template
Return value
void
3 calls to Parser::checkInvalidEscapeSequences()
- Parser::parseNumericLiteral in vendor/
mck89/ peast/ lib/ Peast/ Syntax/ Parser.php - Parses a numeric literal
- Parser::parseStringLiteral in vendor/
mck89/ peast/ lib/ Peast/ Syntax/ Parser.php - Parses a string literal
- Parser::parseTemplateLiteral in vendor/
mck89/ peast/ lib/ Peast/ Syntax/ Parser.php - Parses a template literal
File
-
vendor/
mck89/ peast/ lib/ Peast/ Syntax/ Parser.php, line 4018
Class
- Parser
- Parser class
Namespace
Peast\SyntaxCode
protected function checkInvalidEscapeSequences($val, $number = false, $forceLegacyOctalCheck = false, $taggedTemplate = false) {
if ($this->features->skipEscapeSeqCheckInTaggedTemplates && $taggedTemplate) {
return;
}
$checkLegacyOctal = $forceLegacyOctalCheck || $this->scanner
->getStrictMode();
if ($number) {
if ($val && $val[0] === "0" && preg_match("#^0[0-9_]+\$#", $val)) {
if ($checkLegacyOctal) {
$this->error("Octal literals are not allowed in strict mode");
}
if ($this->features->numericLiteralSeparator && strpos($val, '_') !== false) {
$this->error("Numeric separators are not allowed in legacy octal numbers");
}
}
}
elseif (strpos($val, "\\") !== false) {
$hex = "0-9a-fA-F";
$invalidSyntax = array(
"x[{$hex}]?[^{$hex}]",
"x[{$hex}]?\$",
"u\\{\\}",
"u\\{(?:[{$hex}]*[^{$hex}\\}]+)+[{$hex}]*\\}",
"u\\{[^\\}]*\$",
"u(?!{)[{$hex}]{0,3}[^{$hex}\\{]",
"u[{$hex}]{0,3}\$",
);
if ($checkLegacyOctal) {
$invalidSyntax[] = "\\d{2}";
$invalidSyntax[] = "[1-7]";
$invalidSyntax[] = "0[89]";
}
$reg = "#(\\\\+)(" . implode("|", $invalidSyntax) . ")#";
if (preg_match_all($reg, $val, $matches, PREG_SET_ORDER)) {
foreach ($matches as $match) {
if (strlen($match[1]) % 2) {
$first = $match[2][0];
if ($first === "u") {
$err = "Malformed unicode escape sequence";
}
elseif ($first === "x") {
$err = "Malformed hexadecimal escape sequence";
}
else {
$err = "Octal literals are not allowed in strict mode";
}
$this->error($err);
}
}
}
}
}