From ec5ea0c24e55ef0eeae769372b4812f8884b99bb Mon Sep 17 00:00:00 2001 From: zhangrengao Date: Thu, 27 Oct 2022 03:44:26 +0000 Subject: [PATCH] fixed 1fa0a58 from https://gitee.com/zhangrengao1/ark_ts2abc/pulls/627 Add strict mode reserved words of js Issue: https://e.gitee.com/open_harmony/dashboard?issue=I5Y0MO Test: test262,parser tests, compiler tests Signed-off-by: zhangrengao Change-Id: Ib0f870b27d89ad86c8225e133968366c58760145 --- es2panda/lexer/token/token.cpp | 21 +++++++++++++++++++++ es2panda/lexer/token/token.h | 1 + es2panda/parser/expressionParser.cpp | 10 ++++++++-- es2panda/parser/statementParser.cpp | 12 ++++++++++-- 4 files changed, 40 insertions(+), 4 deletions(-) diff --git a/es2panda/lexer/token/token.cpp b/es2panda/lexer/token/token.cpp index 12521b9dad..ba04e5d974 100644 --- a/es2panda/lexer/token/token.cpp +++ b/es2panda/lexer/token/token.cpp @@ -92,6 +92,27 @@ bool Token::IsReservedTypeName() const } } +bool Token::IsJsStrictReservedWord() const +{ + switch (keywordType_) + { + case TokenType::KEYW_ARGUMENTS: + case TokenType::KEYW_EVAL: + case TokenType::KEYW_STATIC: + case TokenType::KEYW_PRIVATE: + case TokenType::KEYW_PROTECTED: + case TokenType::KEYW_PUBLIC: + case TokenType::KEYW_IMPLEMENTS: + case TokenType::KEYW_INTERFACE: + case TokenType::KEYW_PACKAGE: + case TokenType::KEYW_LET: + case TokenType::KEYW_YIELD: + return true; + default: + return false; + } +} + bool Token::IsBinaryToken(TokenType type) { return (type >= TokenType::PUNCTUATOR_NULLISH_COALESCING && type <= TokenType::PUNCTUATOR_EXPONENTIATION); diff --git a/es2panda/lexer/token/token.h b/es2panda/lexer/token/token.h index 46a87d432c..26edb60d38 100644 --- a/es2panda/lexer/token/token.h +++ b/es2panda/lexer/token/token.h @@ -116,6 +116,7 @@ public: bool IsPropNameLiteral() const; bool IsKeyword() const; bool IsReservedTypeName() const; + bool IsJsStrictReservedWord() const; static bool IsBinaryToken(TokenType type); static bool IsBinaryLvalueToken(TokenType type); diff --git a/es2panda/parser/expressionParser.cpp b/es2panda/parser/expressionParser.cpp index e66239d189..7828ec29fb 100644 --- a/es2panda/parser/expressionParser.cpp +++ b/es2panda/parser/expressionParser.cpp @@ -2339,8 +2339,14 @@ ir::FunctionExpression *ParserImpl::ParseFunctionExpression(ParserStatus newStat ThrowSyntaxError("Expected an identifier."); } - if (lexer_->GetToken().KeywordType() >= lexer::TokenType::KEYW_ARGUMENTS) { - ThrowSyntaxError("Unexpected reserved word in strict mode."); + if (Extension() == ScriptExtension::JS) { + if (lexer_->GetToken().IsJsStrictReservedWord()) { + ThrowSyntaxError("Unexpected reserved word in strict mode."); + } + } else { + if (lexer_->GetToken().KeywordType() >= lexer::TokenType::KEYW_ARGUMENTS) { + ThrowSyntaxError("Unexpected reserved word in strict mode."); + } } ident = AllocNode(lexer_->GetToken().Ident(), Allocator()); diff --git a/es2panda/parser/statementParser.cpp b/es2panda/parser/statementParser.cpp index ffa8688480..e6a022f405 100644 --- a/es2panda/parser/statementParser.cpp +++ b/es2panda/parser/statementParser.cpp @@ -1009,8 +1009,16 @@ ir::FunctionDeclaration *ParserImpl::ParseFunctionDeclaration(bool canBeAnonymou ThrowSyntaxError("Unexpected token, expected identifier after 'function' keyword"); } - if (!isDeclare && lexer_->GetToken().KeywordType() >= lexer::TokenType::KEYW_ARGUMENTS) { - ThrowSyntaxError("Unexpected reserved word in strict mode."); + if (!isDeclare) { + if (Extension() == ScriptExtension::JS) { + if (lexer_->GetToken().IsJsStrictReservedWord()) { + ThrowSyntaxError("Unexpected reserved word in strict mode."); + } + } else { + if (lexer_->GetToken().KeywordType() >= lexer::TokenType::KEYW_ARGUMENTS) { + ThrowSyntaxError("Unexpected reserved word in strict mode."); + } + } } util::StringView ident = lexer_->GetToken().Ident(); -- Gitee