LibJS: Throw a syntax error when an identifier is a reserved word

From the specification:
It is a Syntax Error if StringValue of IdentifierName is the same
String value as the StringValue of any ReservedWord except for yield
or await.
It is a Syntax Error if this phrase is contained in strict mode code
and the StringValue of IdentifierName is: "implements", "interface",
"let", "package", "private", "protected", "public", "static", or
"yield".
This commit is contained in:
Idan Horowitz 2021-06-17 01:57:01 +03:00 committed by Andreas Kling
parent d6df955305
commit 70697a5999

View file

@ -6,6 +6,7 @@
*/
#include "Parser.h"
#include <AK/Array.h>
#include <AK/CharacterTypes.h>
#include <AK/HashTable.h>
#include <AK/ScopeGuard.h>
@ -2251,12 +2252,22 @@ void Parser::consume_or_insert_semicolon()
expected("Semicolon");
}
static constexpr AK::Array<StringView, 38> reserved_words = { "await", "break", "case", "catch", "class", "const", "continue", "debugger", "default", "delete", "do", "else", "enum", "export", "extends", "false", "finally", "for", "function", "if", "import", "in", "instanceof", "new", "null", "return", "super", "switch", "this", "throw", "true", "try", "typeof", "var", "void", "while", "with", "yield" };
static constexpr AK::Array<StringView, 9> strict_reserved_words = { "implements", "interface", "let", "package", "private", "protected", "public", "static", "yield" };
Token Parser::consume(TokenType expected_type)
{
if (!match(expected_type)) {
expected(Token::name(expected_type));
}
return consume();
auto token = consume();
if (expected_type == TokenType::Identifier) {
if (any_of(reserved_words.begin(), reserved_words.end(), [&](auto const& word) { return word == token.value(); }))
syntax_error("Identifier must not be a reserved word");
if (m_parser_state.m_strict_mode && any_of(strict_reserved_words.begin(), strict_reserved_words.end(), [&](auto const& word) { return word == token.value(); }))
syntax_error("Identifier must not be a class-related reserved word in strict mode");
}
return token;
}
Token Parser::consume_and_validate_numeric_literal()