From 948d49bbada0e10b9cb3b818de3f37217df07583 Mon Sep 17 00:00:00 2001 From: zhangchen168 Date: Wed, 21 Sep 2022 15:46:45 +0800 Subject: [PATCH] Fix the bug of parsing the conditional expression in parenthesis Support parsing conditional expressions in parentheses Add test case and expectation: es2panda/test/parser/ts/test-stringliteral-parenthesized-expressions-expected.txt es2panda/test/parser/ts/test-stringliteral-parenthesized-expressions.ts Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/I5SB3W Test: parser test262, parser tests Signed-off-by: zhangchen168 Change-Id: I3b546ed61c4b239e048c5f2543a29daa7ca91edd --- es2panda/parser/expressionParser.cpp | 10 +- ...ral-parenthesized-expressions-expected.txt | 582 ++++++++++++++++++ ...stringliteral-parenthesized-expressions.ts | 21 + 3 files changed, 610 insertions(+), 3 deletions(-) create mode 100644 es2panda/test/parser/ts/test-stringliteral-parenthesized-expressions-expected.txt create mode 100644 es2panda/test/parser/ts/test-stringliteral-parenthesized-expressions.ts diff --git a/es2panda/parser/expressionParser.cpp b/es2panda/parser/expressionParser.cpp index 56bbe33c8d..68b368a605 100644 --- a/es2panda/parser/expressionParser.cpp +++ b/es2panda/parser/expressionParser.cpp @@ -978,9 +978,13 @@ ir::Expression *ParserImpl::ParsePrimaryExpression(ExpressionParseFlags flags) lexer_->NextToken(); if (Extension() == ScriptExtension::TS && (flags & ExpressionParseFlags::ALLOW_TS_PARAM_TOKEN) && - lexer::Token::IsTsParamToken(lexer_->GetToken().Type())) { - context_.Status() |= ParserStatus::FUNCTION_PARAM; - ParsePotentialTsFunctionParameter(ExpressionParseFlags::NO_OPTS, identNode); + lexer::Token::IsTsParamToken(lexer_->GetToken().Type()) && + (lexer_->GetToken().Type() != lexer::TokenType::PUNCTUATOR_QUESTION_MARK || + (lexer_->Lookahead() == LEX_CHAR_COLON || + lexer_->Lookahead() == LEX_CHAR_COMMA || + lexer_->Lookahead() == LEX_CHAR_RIGHT_PAREN))) { + context_.Status() |= ParserStatus::FUNCTION_PARAM; + ParsePotentialTsFunctionParameter(ExpressionParseFlags::NO_OPTS, identNode); } return identNode; diff --git a/es2panda/test/parser/ts/test-stringliteral-parenthesized-expressions-expected.txt b/es2panda/test/parser/ts/test-stringliteral-parenthesized-expressions-expected.txt new file mode 100644 index 0000000000..dc857c64ff --- /dev/null +++ b/es2panda/test/parser/ts/test-stringliteral-parenthesized-expressions-expected.txt @@ -0,0 +1,582 @@ +{ + "type": "Program", + "statements": [ + { + "type": "TSDeclareFunction", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "myBool", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 18 + }, + "end": { + "line": 17, + "column": 24 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "returnType": { + "type": "TSBooleanKeyword", + "loc": { + "start": { + "line": 17, + "column": 28 + }, + "end": { + "line": 17, + "column": 35 + } + } + }, + "declare": true, + "loc": { + "start": { + "line": 17, + "column": 9 + }, + "end": { + "line": 17, + "column": 36 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 9 + }, + "end": { + "line": 17, + "column": 36 + } + } + }, + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "x", + "typeAnnotation": { + "type": "TSLiteralType", + "literal": { + "type": "StringLiteral", + "value": "func", + "loc": { + "start": { + "line": 18, + "column": 8 + }, + "end": { + "line": 18, + "column": 14 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 8 + }, + "end": { + "line": 18, + "column": 14 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 5 + }, + "end": { + "line": 18, + "column": 6 + } + } + }, + "init": { + "type": "StringLiteral", + "value": "func", + "loc": { + "start": { + "line": 18, + "column": 17 + }, + "end": { + "line": 18, + "column": 25 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 5 + }, + "end": { + "line": 18, + "column": 25 + } + } + } + ], + "kind": "let", + "loc": { + "start": { + "line": 18, + "column": 1 + }, + "end": { + "line": 18, + "column": 26 + } + } + }, + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "y", + "typeAnnotation": { + "type": "TSUnionType", + "types": [ + { + "type": "TSLiteralType", + "literal": { + "type": "StringLiteral", + "value": "func", + "loc": { + "start": { + "line": 19, + "column": 8 + }, + "end": { + "line": 19, + "column": 14 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 8 + }, + "end": { + "line": 19, + "column": 14 + } + } + }, + { + "type": "TSLiteralType", + "literal": { + "type": "StringLiteral", + "value": "car", + "loc": { + "start": { + "line": 19, + "column": 17 + }, + "end": { + "line": 19, + "column": 22 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 17 + }, + "end": { + "line": 19, + "column": 22 + } + } + } + ], + "loc": { + "start": { + "line": 19, + "column": 8 + }, + "end": { + "line": 19, + "column": 22 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 5 + }, + "end": { + "line": 19, + "column": 6 + } + } + }, + "init": { + "type": "StringLiteral", + "value": "func", + "loc": { + "start": { + "line": 19, + "column": 25 + }, + "end": { + "line": 19, + "column": 33 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 5 + }, + "end": { + "line": 19, + "column": 33 + } + } + } + ], + "kind": "let", + "loc": { + "start": { + "line": 19, + "column": 1 + }, + "end": { + "line": 19, + "column": 34 + } + } + }, + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "z", + "typeAnnotation": { + "type": "TSLiteralType", + "literal": { + "type": "StringLiteral", + "value": "func", + "loc": { + "start": { + "line": 20, + "column": 8 + }, + "end": { + "line": 20, + "column": 14 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 8 + }, + "end": { + "line": 20, + "column": 14 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 5 + }, + "end": { + "line": 20, + "column": 6 + } + } + }, + "init": { + "type": "ConditionalExpression", + "test": { + "type": "Identifier", + "name": "myBool", + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 18 + }, + "end": { + "line": 20, + "column": 24 + } + } + }, + "consequent": { + "type": "StringLiteral", + "value": "func", + "loc": { + "start": { + "line": 20, + "column": 27 + }, + "end": { + "line": 20, + "column": 33 + } + } + }, + "alternate": { + "type": "StringLiteral", + "value": "func", + "loc": { + "start": { + "line": 20, + "column": 36 + }, + "end": { + "line": 20, + "column": 44 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 17 + }, + "end": { + "line": 20, + "column": 45 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 5 + }, + "end": { + "line": 20, + "column": 45 + } + } + } + ], + "kind": "let", + "loc": { + "start": { + "line": 20, + "column": 1 + }, + "end": { + "line": 20, + "column": 46 + } + } + }, + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "w", + "typeAnnotation": { + "type": "TSUnionType", + "types": [ + { + "type": "TSLiteralType", + "literal": { + "type": "StringLiteral", + "value": "func", + "loc": { + "start": { + "line": 21, + "column": 8 + }, + "end": { + "line": 21, + "column": 14 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 8 + }, + "end": { + "line": 21, + "column": 14 + } + } + }, + { + "type": "TSLiteralType", + "literal": { + "type": "StringLiteral", + "value": "car", + "loc": { + "start": { + "line": 21, + "column": 17 + }, + "end": { + "line": 21, + "column": 22 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 17 + }, + "end": { + "line": 21, + "column": 22 + } + } + } + ], + "loc": { + "start": { + "line": 21, + "column": 8 + }, + "end": { + "line": 21, + "column": 22 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 5 + }, + "end": { + "line": 21, + "column": 6 + } + } + }, + "init": { + "type": "ConditionalExpression", + "test": { + "type": "Identifier", + "name": "myBool", + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 26 + }, + "end": { + "line": 21, + "column": 32 + } + } + }, + "consequent": { + "type": "StringLiteral", + "value": "func", + "loc": { + "start": { + "line": 21, + "column": 35 + }, + "end": { + "line": 21, + "column": 41 + } + } + }, + "alternate": { + "type": "StringLiteral", + "value": "car", + "loc": { + "start": { + "line": 21, + "column": 44 + }, + "end": { + "line": 21, + "column": 51 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 25 + }, + "end": { + "line": 21, + "column": 52 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 5 + }, + "end": { + "line": 21, + "column": 52 + } + } + } + ], + "kind": "let", + "loc": { + "start": { + "line": 21, + "column": 1 + }, + "end": { + "line": 21, + "column": 53 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 22, + "column": 1 + } + } +} diff --git a/es2panda/test/parser/ts/test-stringliteral-parenthesized-expressions.ts b/es2panda/test/parser/ts/test-stringliteral-parenthesized-expressions.ts new file mode 100644 index 0000000000..39b05b91d7 --- /dev/null +++ b/es2panda/test/parser/ts/test-stringliteral-parenthesized-expressions.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2022 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. + */ + + +declare function myBool(): boolean; +let x: "func" = ("func"); +let y: "func" | "car" = ("func"); +let z: "func" = (myBool ? "func" : ("func")); +let w: "func" | "car" = (myBool ? "func" : ("car")); -- Gitee