summaryrefslogtreecommitdiff
path: root/Userland
diff options
context:
space:
mode:
authorIdan Horowitz <idan.horowitz@gmail.com>2021-06-17 01:57:01 +0300
committerAndreas Kling <kling@serenityos.org>2021-06-17 10:56:11 +0200
commit70697a5999266d7b4ca8cc34a36369f3f249a95c (patch)
tree5f8af704c6cb786d2b26182893e565d780d43afa /Userland
parentd6df95530538d7cb475de5fa0ccb5e6bc15887fd (diff)
downloadserenity-70697a5999266d7b4ca8cc34a36369f3f249a95c.zip
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".
Diffstat (limited to 'Userland')
-rw-r--r--Userland/Libraries/LibJS/Parser.cpp13
1 files changed, 12 insertions, 1 deletions
diff --git a/Userland/Libraries/LibJS/Parser.cpp b/Userland/Libraries/LibJS/Parser.cpp
index 90461ff8d6..482aea2c33 100644
--- a/Userland/Libraries/LibJS/Parser.cpp
+++ b/Userland/Libraries/LibJS/Parser.cpp
@@ -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()