diff --git a/ets2panda/lexer/scripts/keywords.yaml b/ets2panda/lexer/scripts/keywords.yaml index 800f183c3b0f40d6893a4360ae338b457934473b..ace576723af03fa8a85c19d770a794ab81cd9f0d 100644 --- a/ets2panda/lexer/scripts/keywords.yaml +++ b/ets2panda/lexer/scripts/keywords.yaml @@ -312,8 +312,7 @@ keywords: - name: 'instanceof' token: KEYW_INSTANCEOF - keyword: [as, js, ts] - keyword_like: [ets] + keyword: [as, js, ets, ts] flags: [binary] - name: 'int' diff --git a/ets2panda/parser/ETSparserExpressions.cpp b/ets2panda/parser/ETSparserExpressions.cpp index 67e3c40be1fdd9f28ef505cd496bc5babdfaeb11..c252a3d5769d563a90bb34b9483d4f46af48afb6 100644 --- a/ets2panda/parser/ETSparserExpressions.cpp +++ b/ets2panda/parser/ETSparserExpressions.cpp @@ -132,13 +132,8 @@ ir::Expression *ETSParser::CreateUnaryExpressionFromArgument(ir::Expression *arg return returnExpr; } -static bool IsLeftHandSideExpression(lexer::TokenType &operatorType, lexer::NextTokenFlags &tokenFlags, - lexer::TokenType keywordType) +static bool IsLeftHandSideExpression(lexer::TokenType &operatorType, lexer::NextTokenFlags &tokenFlags) { - if (operatorType == lexer::TokenType::LITERAL_IDENT && keywordType == lexer::TokenType::KEYW_TYPEOF) { - operatorType = lexer::TokenType::KEYW_TYPEOF; - } - switch (operatorType) { case lexer::TokenType::PUNCTUATOR_MINUS: tokenFlags = lexer::NextTokenFlags::UNARY_MINUS; @@ -161,7 +156,7 @@ ir::Expression *ETSParser::ParseUnaryOrPrefixUpdateExpression(ExpressionParseFla { auto tokenFlags = lexer::NextTokenFlags::NONE; lexer::TokenType operatorType = Lexer()->GetToken().Type(); - if (IsLeftHandSideExpression(operatorType, tokenFlags, Lexer()->GetToken().KeywordType())) { + if (IsLeftHandSideExpression(operatorType, tokenFlags)) { return ParseLeftHandSideExpression(flags); } diff --git a/ets2panda/parser/expressionParser.cpp b/ets2panda/parser/expressionParser.cpp index 89cd7fa0698f0e5de84cc37f8b57832665a8f888..f3cec2504f864fdcc28a85cba8623eef901123ef 100644 --- a/ets2panda/parser/expressionParser.cpp +++ b/ets2panda/parser/expressionParser.cpp @@ -606,13 +606,12 @@ ir::Expression *ParserImpl::ParseAssignmentExpression(ir::Expression *lhsExpress conditionalExpr->SetRange({lhsExpression->Start(), alternate->End()}); return conditionalExpr; } - case lexer::TokenType::PUNCTUATOR_ARROW: { + case lexer::TokenType::PUNCTUATOR_ARROW: if (lexer_->GetToken().NewLine()) { LogError(diagnostic::EXPECTED_EXPRESSION_GOT_ARROW); } return ParseArrowFunctionExpression(lhsExpression, nullptr, nullptr, false); - } case lexer::TokenType::PUNCTUATOR_SUBSTITUTION: { ValidateAssignmentTarget(flags, lhsExpression); @@ -620,17 +619,12 @@ ir::Expression *ParserImpl::ParseAssignmentExpression(ir::Expression *lhsExpress ir::Expression *assignmentExpression = ParseExpression(CarryPatternFlags(flags)); return CreateBinaryAssignmentExpression(assignmentExpression, lhsExpression, tokenType); } - case lexer::TokenType::KEYW_AS: { + case lexer::TokenType::KEYW_AS: if (auto asExpression = ParsePotentialAsExpression(lhsExpression); asExpression != nullptr) { return ParseAssignmentExpression(asExpression); } break; - } default: { - if (tokenType == lexer::TokenType::LITERAL_IDENT && - lexer_->GetToken().KeywordType() == lexer::TokenType::KEYW_INSTANCEOF) { - tokenType = lexer::TokenType::KEYW_INSTANCEOF; - } auto expression = ParseAssignmentBinaryExpression(tokenType, lhsExpression, flags); if (expression == nullptr) { expression = ParseAssignmentEqualExpression(tokenType, lhsExpression, flags); @@ -683,7 +677,7 @@ ir::Expression *ParserImpl::ParseAssignmentBinaryExpression(const lexer::TokenTy case lexer::TokenType::PUNCTUATOR_MOD: case lexer::TokenType::KEYW_INSTANCEOF: case lexer::TokenType::PUNCTUATOR_EXPONENTIATION: { - return ParseAssignmentExpression(ParseBinaryExpression(lhsExpression, tokenType)); + return ParseAssignmentExpression(ParseBinaryExpression(lhsExpression)); } default: break; @@ -1331,8 +1325,9 @@ static ir::Expression *FindAndAmendChildExpression(ir::Expression *expression, c return shouldBeAmended ? expression : parentExpression; } -ir::Expression *ParserImpl::ParseBinaryExpression(ir::Expression *left, const lexer::TokenType operatorType) +ir::Expression *ParserImpl::ParseBinaryExpression(ir::Expression *left, ExpressionParseFlags flags) { + lexer::TokenType operatorType = lexer_->GetToken().Type(); ES2PANDA_ASSERT(lexer::Token::IsBinaryToken(operatorType)); if (operatorType == lexer::TokenType::PUNCTUATOR_EXPONENTIATION) { @@ -1343,6 +1338,11 @@ ir::Expression *ParserImpl::ParseBinaryExpression(ir::Expression *left, const le lexer_->NextToken(); + ExpressionParseFlags newFlags = ExpressionParseFlags::DISALLOW_YIELD; + if ((operatorType == lexer::TokenType::KEYW_INSTANCEOF) || ((flags & ExpressionParseFlags::INSTANCEOF) != 0)) { + newFlags |= ExpressionParseFlags::INSTANCEOF; + } + ir::Expression *rightExpr = ParseExpressionOrTypeAnnotation(operatorType, ExpressionParseFlags::DISALLOW_YIELD); ir::ConditionalExpression *conditionalExpr = nullptr; if (rightExpr->IsConditionalExpression() && !rightExpr->IsGrouped()) { diff --git a/ets2panda/parser/parserImpl.h b/ets2panda/parser/parserImpl.h index 83ee66ad7bcee45602b6b38df169937e916fe117..83d83d5907268532d4e6f281255c8d72962971c9 100644 --- a/ets2panda/parser/parserImpl.h +++ b/ets2panda/parser/parserImpl.h @@ -148,7 +148,8 @@ protected: // ExpressionParser.Cpp ir::Expression *ParseKeywordExpression(); - ir::Expression *ParseBinaryExpression(ir::Expression *left, const lexer::TokenType operatorType); + ir::Expression *ParseBinaryExpression(ir::Expression *left, + ExpressionParseFlags flags = ExpressionParseFlags::NO_OPTS); void ValidateUpdateExpression(ir::Expression *returnExpression, bool isChainExpression); ir::Expression *ParseMemberExpression(bool ignoreCallExpression = false, ExpressionParseFlags flags = ExpressionParseFlags::NO_OPTS); diff --git a/ets2panda/test/ast/parser/ets/user_defined_26.ets b/ets2panda/test/ast/parser/ets/user_defined_26.ets deleted file mode 100644 index 19aeb05f0b5911ee21e006696ae7fbd91d55c24d..0000000000000000000000000000000000000000 --- a/ets2panda/test/ast/parser/ets/user_defined_26.ets +++ /dev/null @@ -1,18 +0,0 @@ -/* - * Copyright (c) 2025 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -class instanceof{ - a : string = "15"; -} diff --git a/ets2panda/test/ast/parser/ets/user_defined_27.ets b/ets2panda/test/ast/parser/ets/user_defined_27.ets deleted file mode 100644 index 73cfa2236bcae2dfc1bc2a9ce90e035ce31852a7..0000000000000000000000000000000000000000 --- a/ets2panda/test/ast/parser/ets/user_defined_27.ets +++ /dev/null @@ -1,20 +0,0 @@ -/* - * Copyright (c) 2025 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -struct instanceof{ - a : string = "15"; -} - -/* @@? 16:1 Error TypeError: Structs are only used to define UI components, it should be translated at 'plugin after parser' phase. */