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_AS_PARSER_H 17 #define ES2PANDA_PARSER_CORE_AS_PARSER_H 18 19 #include "ThrowingTypedParser.h" 20 #include "parserFlags.h" 21 22 namespace ark::es2panda::parser { 23 class ASParser : public ThrowingTypedParser { 24 public: 25 ASParser(Program *program, const util::Options &options, util::DiagnosticEngine &diagnosticEngine, 26 ParserStatus status = ParserStatus::NO_OPTS) 27 : ThrowingTypedParser(program, &options, diagnosticEngine, status) 28 { 29 } 30 31 private: 32 [[nodiscard]] std::unique_ptr<lexer::Lexer> InitLexer(const SourceFile &sourceFile) override; 33 ir::TypeNode *ParseParenthesizedOrFunctionType(bool throwError); 34 ir::TypeNode *ParseFunctionType(lexer::SourcePosition startLoc); 35 void ParseOptionalFunctionParameter(ir::AnnotatedExpression *returnNode, bool inRest = false); 36 37 // NOLINTNEXTLINE(google-default-arguments) 38 ir::Statement *ParseStatement(StatementParsingFlags flags = StatementParsingFlags::NONE) override; 39 std::tuple<ir::AnnotatedExpression *, bool> ParsePatternElementToken(ExpressionParseFlags flags); 40 ir::Expression *ParsePatternElement(ExpressionParseFlags flags, bool allowDefault) override; 41 // NOLINTNEXTLINE(google-default-arguments) 42 ir::Expression *ParsePropertyDefinition( 43 [[maybe_unused]] ExpressionParseFlags flags = ExpressionParseFlags::NO_OPTS) override; 44 bool CurrentIsBasicType() override; 45 ir::TypeNode *ParseTypeAnnotationLiteralIdentHelper(ir::TypeNode *type, TypeAnnotationParsingOptions *options); 46 ir::TypeNode *ParseTypeAnnotationTokens(ir::TypeNode *type, bool throwError, TypeAnnotationParsingOptions *options); 47 ir::TypeNode *ParseTypeAnnotationTokensBitwiseOr(ir::TypeNode *type, bool throwError, bool isNullable); 48 ir::TypeNode *ParseTypeAnnotationTokenLeftSquareBracket(ir::TypeNode *type, bool throwError, bool isNullable); 49 ir::TypeNode *ParseTypeAnnotation(TypeAnnotationParsingOptions *options) override; 50 ir::ArrowFunctionExpression *ParsePotentialArrowExpression(ir::Expression **returnExpression, 51 const lexer::SourcePosition &startLoc) override; 52 bool ParsePotentialGenericFunctionCall(ir::Expression *primaryExpr, ir::Expression **returnExpression, 53 const lexer::SourcePosition &startLoc, bool ignoreCallExpression) override; 54 bool ParsePotentialNonNullExpression(ir::Expression **returnExpression, lexer::SourcePosition startLoc) override; 55 bool IsNamedFunctionExpression() override; 56 ir::Expression *ParsePotentialAsExpression(ir::Expression *primaryExpression) override; 57 ir::Identifier *ParsePrimaryExpressionIdent(ExpressionParseFlags flags) override; 58 bool ValidateArrowFunctionRestParameter(ir::SpreadElement *restElement) override; 59 ir::Decorator *ParseDecorator() override; 60 void AddDecorators(ir::AstNode *node, ArenaVector<ir::Decorator *> &decorators) override; 61 ir::TSTypeAliasDeclaration *ParseTypeAliasDeclaration() override; 62 ArenaVector<ir::TSInterfaceHeritage *> ParseInterfaceExtendsClause() override; 63 ir::AstNode *ParseTypeLiteralOrInterfaceMember() override; 64 // NOLINTNEXTLINE(google-default-arguments) 65 ir::TSIndexSignature *ParseIndexSignature(const lexer::SourcePosition &startLoc, bool isReadonly = false) override; 66 ir::AstNode *ParsePropertyOrMethodSignature(const lexer::SourcePosition &startLoc, bool isReadonly) override; 67 ir::TypeNode *ParseClassKeyAnnotation() override; 68 void ValidateClassMethodStart(ClassElementDescriptor *desc, ir::TypeNode *typeAnnotation) override; 69 void ValidateClassSetter(ClassElementDescriptor *desc, const ArenaVector<ir::AstNode *> &properties, 70 ir::Expression *propName, ir::ScriptFunction *func) override; 71 void ValidateClassGetter(ClassElementDescriptor *desc, const ArenaVector<ir::AstNode *> &properties, 72 ir::Expression *propName, ir::ScriptFunction *func) override; 73 bool IsModifierKind(const lexer::Token &token) override; 74 void ConsumeClassPrivateIdentifier(ClassElementDescriptor *desc, char32_t *nextCp) override; 75 std::tuple<bool, bool, bool> ParseComputedClassFieldOrIndexSignature(ir::Expression **propName) override; 76 std::tuple<bool, ir::BlockStatement *, lexer::SourcePosition, bool> ParseFunctionBody( 77 const ArenaVector<ir::Expression *> ¶ms, ParserStatus newStatus, ParserStatus contextStatus) override; 78 ir::AstNode *ParseImportDefaultSpecifier(ArenaVector<ir::AstNode *> *specifiers) override; 79 std::tuple<ir::Expression *, bool> ParseInterfacePropertyKey() override; 80 // NOLINTNEXTLINE(google-default-arguments) 81 ir::Expression *ParseCoverParenthesizedExpressionAndArrowParameterList( 82 ExpressionParseFlags flags = ExpressionParseFlags::NO_OPTS) override; 83 ir::Expression *ParseArrowFunctionRestParameter(lexer::SourcePosition start); 84 ir::Expression *ParseArrowFunctionNoParameter(lexer::SourcePosition start); 85 ir::Expression *ParsePrefixAssertionExpression() override; 86 ir::Statement *ParseConstStatement(StatementParsingFlags flags) override; 87 ir::AnnotatedExpression *ParseVariableDeclaratorKey(VariableParsingFlags flags) override; 88 ir::Statement *ParsePotentialConstEnum(VariableParsingFlags flags) override; 89 // NOLINTNEXTLINE(google-default-arguments) 90 ir::ExportDefaultDeclaration *ParseExportDefaultDeclaration(const lexer::SourcePosition &startLoc, 91 bool isExportEquals = false) override; 92 class ParseNamedExportDeclarationHelper; 93 ir::Statement *ParseNamedExportDeclaration(const lexer::SourcePosition &startLoc) override; 94 ir::AstNode *ParseImportSpecifiers(ArenaVector<ir::AstNode *> *specifiers) override; 95 ir::Statement *ParseImportDeclaration(StatementParsingFlags flags) override; 96 ArenaVector<ir::TSClassImplements *> ParseClassImplementClause() override; 97 ir::ClassElement *ParseClassStaticBlock() override; 98 void ParseOptionalClassElement(ClassElementDescriptor *desc) override; 99 void ValidateIndexSignatureTypeAnnotation(ir::TypeNode *typeAnnotation) override; 100 ArrowFunctionDescriptor ConvertToArrowParameter(ir::Expression *expr, bool isAsync) override; 101 ParserStatus ValidateArrowExprIdentifier(ir::Expression *expr, bool *seenOptional); 102 ParserStatus ValidateArrowAssignmentExpr(ir::Expression *expr); 103 ParserStatus ValidateArrowParameter(ir::Expression *expr, bool *seenOptional) override; 104 void ReportIllegalBreakError(const lexer::SourcePosition &pos) override; 105 void ReportIllegalContinueError() override; 106 }; 107 } // namespace ark::es2panda::parser 108 109 #endif 110