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