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_TS_PARSER_H
17#define ES2PANDA_PARSER_CORE_TS_PARSER_H
18
19#include "TypedParser.h"
20#include "parserFlags.h"
21
22namespace ark::es2panda::ir {
23class Decorator;
24enum class TSTupleKind;
25}  // namespace ark::es2panda::ir
26
27namespace ark::es2panda::parser {
28class TSParser : public TypedParser {
29public:
30    TSParser(Program *program, const CompilerOptions &options, ParserStatus status = ParserStatus::NO_OPTS)
31        : TypedParser(program, options, status)
32    {
33    }
34
35private:
36    [[nodiscard]] std::unique_ptr<lexer::Lexer> InitLexer(const SourceFile &sourceFile) override;
37    bool IsStartOfMappedType() const;
38    bool IsStartOfTypePredicate() const;
39    bool IsStartOfAbstractConstructorType() const;
40    bool CurrentLiteralIsBasicType() const;
41    ir::TypeNode *ParseTypeAnnotationElement(ir::TypeNode *typeAnnotation, TypeAnnotationParsingOptions *options);
42    class ParseTypeAnnotationElementHelper;
43    class ParsePotentialArrowExpressionHelper;
44    ir::TypeNode *ParseTypeOperatorOrTypeReference();
45    ir::TypeNode *ParseIdentifierReference();
46    class ParseBasicTypeHelper;
47    ir::TypeNode *ParseBasicType();
48    ir::TSTypeReference *ParseConstExpression();
49    ir::TSIntersectionType *ParseIntersectionType(ir::Expression *type, bool inUnion, bool restrictExtends);
50    ir::TSUnionType *ParseUnionType(ir::TypeNode *type, bool restrictExtends);
51    ir::TypeNode *ParseParenthesizedOrFunctionType(ir::TypeNode *typeAnnotation, bool throwError);
52    ir::TSArrayType *ParseArrayType(ir::TypeNode *elementType);
53    ir::TypeNode *ParseFunctionType(lexer::SourcePosition startLoc, bool isConstructionType, bool throwError,
54                                    bool abstractConstructor = false);
55    ir::TSTypeParameter *ParseMappedTypeParameter();
56    ir::MappedOption ParseMappedOption(lexer::TokenType tokenType);
57    ir::TSMappedType *ParseMappedType();
58    ir::TSTypePredicate *ParseTypePredicate();
59    ir::TypeNode *ParseConditionalType(ir::Expression *checkType, bool restrictExtends);
60    ir::TypeNode *ParseThisType(bool throwError);
61    ir::TypeNode *ParseIndexAccessType(ir::TypeNode *typeName);
62    ir::TypeNode *ParseTypeReferenceOrQuery(bool parseQuery = false);
63    ir::TypeNode *ParseTupleElement(ir::TSTupleKind *kind, bool *seenOptional);
64    ir::TSTupleType *ParseTupleType();
65    ir::TSImportType *ParseImportType(const lexer::SourcePosition &startLoc, bool isTypeof = false);
66    ir::TypeNode *ParseTypeLiteralOrMappedType(ir::TypeNode *typeAnnotation);
67    ir::TypeNode *ParseTypeReferenceOrTypePredicate(ir::TypeNode *typeAnnotation, bool canBeTsTypePredicate);
68    ir::TypeNode *ParseThisTypeOrTypePredicate(ir::TypeNode *typeAnnotation, bool canBeTsTypePredicate,
69                                               bool throwError);
70    ir::TSSignatureDeclaration *ParseSignatureMember(bool isCallSignature);
71    bool IsPotentiallyIndexSignature();
72    void CreateTSVariableForProperty(ir::AstNode *node, const ir::Expression *key, varbinder::VariableFlags flags);
73    void ValidateFunctionParam(const ArenaVector<ir::Expression *> &params, const ir::Expression *parameter,
74                               bool *seenOptional);
75    ir::TSParameterProperty *CreateParameterProperty(ir::Expression *parameter, ir::ModifierFlags modifiers);
76    void ValidateFunctionOverloadParams(const ArenaVector<ir::Expression *> &params);
77    ir::Expression *ParseModuleReference();
78    ir::TSImportEqualsDeclaration *ParseTsImportEqualsDeclaration(const lexer::SourcePosition &startLoc,
79                                                                  bool isExport = false);
80    void ParseOptionalFunctionParameter(ir::AnnotatedExpression *returnNode, bool isRest = false);
81
82    // NOLINTNEXTLINE(google-default-arguments)
83    ir::Statement *ParseStatement(StatementParsingFlags flags = StatementParsingFlags::NONE) override;
84    ir::AnnotatedExpression *ParsePatternElementGetReturnNode(ExpressionParseFlags &flags, bool &isOptional);
85    // NOLINTNEXTLINE(google-default-arguments)
86    ir::Expression *ParsePatternElement(ExpressionParseFlags flags = ExpressionParseFlags::NO_OPTS,
87                                        bool allowDefault = true) override;
88    bool CurrentIsBasicType() override;
89    ir::TypeNode *ParseTypeAnnotation(TypeAnnotationParsingOptions *options) override;
90    // NOLINTNEXTLINE(google-default-arguments)
91    ir::ObjectExpression *ParseObjectExpression(ExpressionParseFlags flags = ExpressionParseFlags::NO_OPTS) override;
92    // NOLINTNEXTLINE(google-default-arguments)
93    ir::ArrayExpression *ParseArrayExpression(ExpressionParseFlags flags = ExpressionParseFlags::NO_OPTS) override;
94    ir::ArrowFunctionExpression *ParsePotentialArrowExpression(ir::Expression **returnExpression,
95                                                               const lexer::SourcePosition &startLoc) override;
96    bool ParsePotentialGenericFunctionCall(ir::Expression *primaryExpr, ir::Expression **returnExpression,
97                                           const lexer::SourcePosition &startLoc, bool ignoreCallExpression) override;
98    bool ParsePotentialNonNullExpression(ir::Expression **returnExpression, lexer::SourcePosition startLoc) override;
99    bool IsNamedFunctionExpression() override;
100    ir::Identifier *ParsePrimaryExpressionIdent(ExpressionParseFlags flags) override;
101    void ValidateArrowFunctionRestParameter(ir::SpreadElement *restElement) override;
102    ir::Decorator *ParseDecorator() override;
103    void AddDecorators(ir::AstNode *node, ArenaVector<ir::Decorator *> &decorators) override;
104    ir::TSTypeAliasDeclaration *ParseTypeAliasDeclaration() override;
105    ir::AstNode *ParseTypeLiteralOrInterfaceMember() override;
106    // NOLINTNEXTLINE(google-default-arguments)
107    ir::TSIndexSignature *ParseIndexSignature(const lexer::SourcePosition &startLoc, bool isReadonly = false) override;
108    ir::AstNode *ParsePropertyOrMethodSignature(const lexer::SourcePosition &startLoc, bool isReadonly) override;
109    std::tuple<ir::Expression *, bool> ParseInterfacePropertyKey() override;
110    ArenaVector<ir::Expression *> ParseFunctionParams() override;
111    ir::Expression *ParseFunctionParameter() override;
112    ir::TypeNode *ParseClassKeyAnnotation() override;
113    void ValidateClassMethodStart(ClassElementDescriptor *desc, ir::TypeNode *typeAnnotation) override;
114    ir::MethodDefinition *ParseClassMethod(ClassElementDescriptor *desc, const ArenaVector<ir::AstNode *> &properties,
115                                           ir::Expression *propName, lexer::SourcePosition *propEnd) override;
116    void ValidateClassSetter(ClassElementDescriptor *desc, const ArenaVector<ir::AstNode *> &properties,
117                             ir::Expression *propName, ir::ScriptFunction *func) override;
118    void ValidateClassGetter(ClassElementDescriptor *desc, const ArenaVector<ir::AstNode *> &properties,
119                             ir::Expression *propName, ir::ScriptFunction *func) override;
120    bool IsModifierKind(const lexer::Token &token) override;
121    void CheckIfTypeParameterNameIsReserved() override;
122    void ThrowErrorIfStaticConstructor(ir::ModifierFlags flags) override;
123    std::tuple<bool, bool, bool> ParseComputedClassFieldOrIndexSignature(ir::Expression **propName) override;
124    ir::TypeNode *ParseFunctionReturnType(ParserStatus status) override;
125    std::tuple<bool, ir::BlockStatement *, lexer::SourcePosition, bool> ParseFunctionBody(
126        const ArenaVector<ir::Expression *> &params, ParserStatus newStatus, ParserStatus contextStatus) override;
127    ir::AstNode *ParseImportDefaultSpecifier(ArenaVector<ir::AstNode *> *specifiers) override;
128    ir::Statement *ParseExportDeclaration(StatementParsingFlags flags) override;
129    // NOLINTNEXTLINE(google-default-arguments)
130    ir::Expression *ParseCoverParenthesizedExpressionAndArrowParameterList(
131        ExpressionParseFlags flags = ExpressionParseFlags::NO_OPTS) override;
132    ir::Expression *ParseArrowFunctionRestParameter(lexer::SourcePosition start);
133    ir::Expression *ParseArrowFunctionNoParameter(lexer::SourcePosition start);
134    ir::Statement *ParseConstStatement(StatementParsingFlags flags) override;
135    ir::Statement *ParsePotentialConstEnum(VariableParsingFlags flags) override;
136    void ParseCatchParamTypeAnnotation(ir::AnnotatedExpression *param) override;
137    ir::AnnotatedExpression *ParseVariableDeclaratorKey(VariableParsingFlags flags) override;
138    void ThrowPossibleOutOfBoundaryJumpError(bool allowBreak) override;
139    void ThrowIllegalBreakError() override;
140    void ThrowIllegalContinueError() override;
141    void ThrowIfBodyEmptyError(ir::Statement *consequent) override;
142    void ThrowMultipleDefaultError() override;
143    void ThrowIllegalNewLineErrorAfterThrow() override;
144    // NOLINTNEXTLINE(google-default-arguments)
145    ir::ExportDefaultDeclaration *ParseExportDefaultDeclaration(const lexer::SourcePosition &startLoc,
146                                                                bool isExportEquals = false) override;
147    ir::Statement *GetDeclarationForNamedExport(ir::ClassDefinitionModifiers &classModifiers, ir::ModifierFlags &flags);
148    ir::ExportNamedDeclaration *ParseNamedExportDeclaration(const lexer::SourcePosition &startLoc) override;
149    ir::Statement *ParseImportDeclaration(StatementParsingFlags flags) override;
150    void ValidateIndexSignatureTypeAnnotation(ir::TypeNode *typeAnnotation) override;
151    ir::Expression *ParsePotentialAsExpression(ir::Expression *expr) override;
152
153    bool AllowInterfaceRedeclaration() override
154    {
155        return true;
156    }
157};
158}  // namespace ark::es2panda::parser
159
160#endif
161