1 /** 2 * Copyright (c) 2021-2025 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef ES2PANDA_PARSER_CORE_TS_PARSER_H 17 #define ES2PANDA_PARSER_CORE_TS_PARSER_H 18 19 #include "ThrowingTypedParser.h" 20 #include "parserFlags.h" 21 22 namespace ark::es2panda::ir { 23 class Decorator; 24 enum class TSTupleKind; 25 } // namespace ark::es2panda::ir 26 27 namespace ark::es2panda::parser { 28 class TSParser : public ThrowingTypedParser { 29 public: 30 TSParser(Program *program, const util::Options &options, util::DiagnosticEngine &diagnosticEngine, 31 ParserStatus status = ParserStatus::NO_OPTS) 32 : ThrowingTypedParser(program, &options, diagnosticEngine, status) 33 { 34 } 35 36 private: 37 [[nodiscard]] std::unique_ptr<lexer::Lexer> InitLexer(const SourceFile &sourceFile) override; 38 bool IsStartOfMappedType() const; 39 bool IsStartOfTypePredicate() const; 40 bool IsStartOfAbstractConstructorType() const; 41 bool CurrentLiteralIsBasicType() const; 42 ir::TypeNode *ParseTypeAnnotationElement(ir::TypeNode *typeAnnotation, TypeAnnotationParsingOptions *options); 43 class ParseTypeAnnotationElementHelper; 44 class ParsePotentialArrowExpressionHelper; 45 ir::TypeNode *ParseTypeOperatorOrTypeReference(); 46 ir::TypeNode *ParseIdentifierReference(); 47 class ParseBasicTypeHelper; 48 ir::TypeNode *ParseBasicType(); 49 ir::TSTypeReference *ParseConstExpression(); 50 ir::TSIntersectionType *ParseIntersectionType(ir::Expression *type, bool inUnion, bool restrictExtends); 51 ir::TSUnionType *ParseUnionType(ir::TypeNode *type, bool restrictExtends); 52 ir::TypeNode *ParseParenthesizedOrFunctionType(ir::TypeNode *typeAnnotation, bool throwError); 53 ir::TSArrayType *ParseArrayType(ir::TypeNode *elementType); 54 ir::TypeNode *ParseFunctionType(lexer::SourcePosition startLoc, bool isConstructionType, bool throwError, 55 bool abstractConstructor = false); 56 ir::TSTypeParameter *ParseMappedTypeParameter(); 57 ir::MappedOption ParseMappedOption(lexer::TokenType tokenType); 58 ir::TSMappedType *ParseMappedType(); 59 ir::TSTypePredicate *ParseTypePredicate(); 60 ir::TypeNode *ParseConditionalType(ir::Expression *checkType, bool restrictExtends); 61 ir::TypeNode *ParseThisType(bool throwError); 62 ir::TypeNode *ParseIndexAccessType(ir::TypeNode *typeName); 63 ir::TypeNode *ParseTypeReferenceOrQuery(bool parseQuery = false); 64 ir::TypeNode *ParseTupleElement(ir::TSTupleKind *kind, bool *seenOptional); 65 ir::TSTupleType *ParseTupleType(); 66 ir::TSImportType *ParseImportType(const lexer::SourcePosition &startLoc, bool isTypeof = false); 67 ir::TypeNode *ParseTypeLiteralOrMappedType(ir::TypeNode *typeAnnotation); 68 ir::TypeNode *ParseTypeReferenceOrTypePredicate(ir::TypeNode *typeAnnotation, bool canBeTsTypePredicate); 69 ir::TypeNode *ParseThisTypeOrTypePredicate(ir::TypeNode *typeAnnotation, bool canBeTsTypePredicate, 70 bool throwError); 71 ir::TSSignatureDeclaration *ParseSignatureMember(bool isCallSignature); 72 bool IsPotentiallyIndexSignature(); 73 void CreateTSVariableForProperty(ir::AstNode *node, const ir::Expression *key, varbinder::VariableFlags flags); 74 void ValidateFunctionParam(const ArenaVector<ir::Expression *> ¶ms, const ir::Expression *parameter, 75 bool *seenOptional); 76 ir::TSParameterProperty *CreateParameterProperty(ir::Expression *parameter, ir::ModifierFlags modifiers); 77 void ValidateFunctionOverloadParams(const ArenaVector<ir::Expression *> ¶ms); 78 ir::Expression *ParseModuleReference(); 79 ir::TSImportEqualsDeclaration *ParseTsImportEqualsDeclaration(const lexer::SourcePosition &startLoc, 80 bool isExport = false); 81 void ParseOptionalFunctionParameter(ir::AnnotatedExpression *returnNode, bool isRest = false); 82 83 // NOLINTNEXTLINE(google-default-arguments) 84 ir::Statement *ParseStatement(StatementParsingFlags flags = StatementParsingFlags::NONE) override; 85 ir::AnnotatedExpression *ParsePatternElementGetReturnNode(ExpressionParseFlags &flags, bool &isOptional); 86 // NOLINTNEXTLINE(google-default-arguments) 87 ir::Expression *ParsePatternElement(ExpressionParseFlags flags = ExpressionParseFlags::NO_OPTS, 88 bool allowDefault = true) override; 89 bool CurrentIsBasicType() override; 90 ir::TypeNode *ParseTypeAnnotation(TypeAnnotationParsingOptions *options) override; 91 // NOLINTNEXTLINE(google-default-arguments) 92 ir::ObjectExpression *ParseObjectExpression(ExpressionParseFlags flags = ExpressionParseFlags::NO_OPTS) override; 93 // NOLINTNEXTLINE(google-default-arguments) 94 ir::ArrayExpression *ParseArrayExpression(ExpressionParseFlags flags = ExpressionParseFlags::NO_OPTS) override; 95 ir::ArrowFunctionExpression *ParsePotentialArrowExpression(ir::Expression **returnExpression, 96 const lexer::SourcePosition &startLoc) override; 97 bool ParsePotentialGenericFunctionCall(ir::Expression *primaryExpr, ir::Expression **returnExpression, 98 const lexer::SourcePosition &startLoc, bool ignoreCallExpression) override; 99 bool ParsePotentialNonNullExpression(ir::Expression **returnExpression, lexer::SourcePosition startLoc) override; 100 bool IsNamedFunctionExpression() override; 101 ir::Identifier *ParsePrimaryExpressionIdent(ExpressionParseFlags flags) override; 102 bool ValidateArrowFunctionRestParameter(ir::SpreadElement *restElement) override; 103 ir::Decorator *ParseDecorator() override; 104 void AddDecorators(ir::AstNode *node, ArenaVector<ir::Decorator *> &decorators) override; 105 ir::TSTypeAliasDeclaration *ParseTypeAliasDeclaration() override; 106 ir::AstNode *ParseTypeLiteralOrInterfaceMember() override; 107 // NOLINTNEXTLINE(google-default-arguments) 108 ir::TSIndexSignature *ParseIndexSignature(const lexer::SourcePosition &startLoc, bool isReadonly = false) override; 109 ir::AstNode *ParsePropertyOrMethodSignature(const lexer::SourcePosition &startLoc, bool isReadonly) override; 110 std::tuple<ir::Expression *, bool> ParseInterfacePropertyKey() override; 111 ArenaVector<ir::Expression *> ParseFunctionParams() override; 112 ir::Expression *ParseFunctionParameter() override; 113 ir::TypeNode *ParseClassKeyAnnotation() override; 114 void ValidateClassMethodStart(ClassElementDescriptor *desc, ir::TypeNode *typeAnnotation) override; 115 ir::MethodDefinition *ParseClassMethod(ClassElementDescriptor *desc, const ArenaVector<ir::AstNode *> &properties, 116 ir::Expression *propName, lexer::SourcePosition *propEnd) override; 117 void ValidateClassSetter(ClassElementDescriptor *desc, const ArenaVector<ir::AstNode *> &properties, 118 ir::Expression *propName, ir::ScriptFunction *func) override; 119 void ValidateClassGetter(ClassElementDescriptor *desc, const ArenaVector<ir::AstNode *> &properties, 120 ir::Expression *propName, ir::ScriptFunction *func) override; 121 bool IsModifierKind(const lexer::Token &token) override; 122 void CheckIfTypeParameterNameIsReserved() override; 123 void CheckIfStaticConstructor(ir::ModifierFlags flags) override; 124 std::tuple<bool, bool, bool> ParseComputedClassFieldOrIndexSignature(ir::Expression **propName) override; 125 ir::TypeNode *ParseFunctionReturnType(ParserStatus status) override; 126 std::tuple<bool, ir::BlockStatement *, lexer::SourcePosition, bool> ParseFunctionBody( 127 const ArenaVector<ir::Expression *> ¶ms, ParserStatus newStatus, ParserStatus contextStatus) override; 128 ir::AstNode *ParseImportDefaultSpecifier(ArenaVector<ir::AstNode *> *specifiers) override; 129 ir::Statement *ParseExportDeclaration(StatementParsingFlags flags) override; 130 // NOLINTNEXTLINE(google-default-arguments) 131 ir::Expression *ParseCoverParenthesizedExpressionAndArrowParameterList( 132 ExpressionParseFlags flags = ExpressionParseFlags::NO_OPTS) override; 133 ir::Expression *ParseArrowFunctionRestParameter(lexer::SourcePosition start); 134 ir::Expression *ParseArrowFunctionNoParameter(lexer::SourcePosition start); 135 ir::Statement *ParseConstStatement(StatementParsingFlags flags) override; 136 ir::Statement *ParsePotentialConstEnum(VariableParsingFlags flags) override; 137 void ParseCatchParamTypeAnnotation(ir::AnnotatedExpression *param) override; 138 ir::AnnotatedExpression *ParseVariableDeclaratorKey(VariableParsingFlags flags) override; 139 void ReportPossibleOutOfBoundaryJumpError(bool allowBreak) override; 140 void ReportIllegalBreakError(const lexer::SourcePosition &pos) override; 141 void ReportIllegalContinueError() override; 142 void ReportIfBodyEmptyError(ir::Statement *consequent) override; 143 void ReportMultipleDefaultError() override; 144 void ReportIllegalNewLineErrorAfterThrow() override; 145 // NOLINTNEXTLINE(google-default-arguments) 146 ir::ExportDefaultDeclaration *ParseExportDefaultDeclaration(const lexer::SourcePosition &startLoc, 147 bool isExportEquals = false) override; 148 ir::Statement *GetDeclarationForNamedExport(ir::ClassDefinitionModifiers &classModifiers, ir::ModifierFlags &flags); 149 ir::Statement *ParseNamedExportDeclaration(const lexer::SourcePosition &startLoc) override; 150 ir::Statement *ParseImportDeclaration(StatementParsingFlags flags) override; 151 void ValidateIndexSignatureTypeAnnotation(ir::TypeNode *typeAnnotation) override; 152 ir::Expression *ParsePotentialAsExpression(ir::Expression *expr) override; 153 AllowInterfaceRedeclaration()154 bool AllowInterfaceRedeclaration() override 155 { 156 return true; 157 } 158 }; 159 } // namespace ark::es2panda::parser 160 161 #endif 162