• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_PARSER_PRIVATE_CONTEXT_H
17 #define ES2PANDA_PARSER_CORE_PARSER_PRIVATE_CONTEXT_H
18 
19 #include "util/enumbitops.h"
20 #include "util/language.h"
21 #include "util/ustring.h"
22 
23 namespace ark::es2panda::parser {
24 class Program;
25 
26 using ENUMBITOPS_OPERATORS;
27 
28 enum class ParserStatus : uint64_t {
29     NO_OPTS = 0U,
30     DIRECT_EVAL = 1U << 0U,
31 
32     FUNCTION = 1U << 1U,
33     ARROW_FUNCTION = 1U << 2U,
34     GENERATOR_FUNCTION = 1U << 3U,
35     ASYNC_FUNCTION = 1U << 4U,
36     CONSTRUCTOR_FUNCTION = 1U << 5U,
37     FUNCTION_PARAM = 1U << 6U,
38     IS_SPREAD = 1U << 7U,
39     ACCESSOR_FUNCTION = 1U << 8U,
40     FUNCTION_DECLARATION = 1U << 9U,
41 
42     ALLOW_SUPER = 1U << 10U,
43     ALLOW_SUPER_CALL = 1U << 11U,
44 
45     IN_ITERATION = 1U << 14U,
46     IN_LABELED = 1U << 15U,
47 
48     EXPORT_DEFAULT_REACHED = 1U << 16U,
49     HAS_COMPLEX_PARAM = 1U << 17U,
50     IN_SWITCH = 1U << 18U,
51 
52     MODULE = 1U << 19U,
53     ALLOW_NEW_TARGET = 1U << 20U,
54 
55     IN_EXTENDS = 1U << 21U,
56     ALLOW_THIS_TYPE = 1U << 22U,
57     IN_METHOD_DEFINITION = 1U << 23U,
58     IN_AMBIENT_CONTEXT = 1U << 24U,
59     IN_CLASS_BODY = 1U << 25U,
60     NEED_RETURN_TYPE = 1U << 26U,
61 
62     IN_DEFAULT_IMPORTS = 1U << 29U,
63     IN_EXTENSION_FUNCTION = 1U << 30U,
64     FUNCTION_HAS_RETURN_STATEMENT = 1U << 31U,
65     IN_NAMESPACE = 1ULL << 32ULL,
66 
67     ALLOW_DEFAULT_VALUE = 1ULL << 33ULL,
68     FUNCTION_HAS_THROW_STATEMENT = 1ULL << 34ULL,
69     ALLOW_RECEIVER = 1ULL << 35ULL,
70     EXTENSION_ACCESSOR = 1ULL << 36ULL,
71     HAS_RECEIVER = 1ULL << 37ULL,
72     PARSE_TRAILING_BLOCK = 1ULL << 38ULL,
73 
74     DEPENDENCY_ANALYZER_MODE = 1ULL << 39ULL,
75 
76     STATIC_BLOCK = 1ULL << 40ULL,
77     ALLOW_JS_DOC_START = 1ULL << 41ULL,
78     IN_PACKAGE = 1ULL << 42ULL,
79 };
80 
81 }  // namespace ark::es2panda::parser
82 
83 template <>
84 struct enumbitops::IsAllowedType<ark::es2panda::parser::ParserStatus> : std::true_type {
85 };
86 
87 namespace ark::es2panda::parser {
88 
89 class ParserContext {
90 public:
91     // NOLINTNEXTLINE(modernize-avoid-c-arrays)
92     inline static constexpr char const DEFAULT_SOURCE_FILE[] = "<auxiliary_tmp>.ets";
93 
94     explicit ParserContext(const Program *program, ParserStatus status);
95 
96     explicit ParserContext(const Program *program, ParserStatus status, bool isEnableJsdoc);
97 
98     explicit ParserContext(ParserContext *current, ParserStatus newStatus, util::StringView label = "")
99         : program_(current->program_), prev_(current), label_(label), lang_(current->lang_)
100     {
101         ParserStatus currentStatus = current->status_;
102         currentStatus &= (ParserStatus::MODULE | ParserStatus::ALLOW_NEW_TARGET | ParserStatus::IN_EXTENDS |
103                           ParserStatus::ALLOW_THIS_TYPE | ParserStatus::IN_CLASS_BODY | ParserStatus::FUNCTION |
104                           ParserStatus::IN_AMBIENT_CONTEXT);
105         status_ = currentStatus | newStatus;
106         isEnableJsdoc_ = prev_->isEnableJsdoc_;
107     }
108 
109     DEFAULT_COPY_SEMANTIC(ParserContext);
110     DEFAULT_MOVE_SEMANTIC(ParserContext);
111     ~ParserContext() = default;
112     ParserContext() = delete;
113 
114     [[nodiscard]] const Program *GetProgram() const noexcept
115     {
116         return program_;
117     }
118 
119     void SetProgram(Program *program) noexcept
120     {
121         program_ = program;
122     }
123 
124     [[nodiscard]] Language GetLanguage() const noexcept
125     {
126         return lang_;
127     }
128 
129     Language SetLanguage(Language lang) noexcept
130     {
131         auto res = lang_;
132         lang_ = lang;
133         return res;
134     }
135 
136     [[nodiscard]] ParserContext *Prev() const noexcept
137     {
138         return prev_;
139     }
140 
141     [[nodiscard]] const ParserStatus &Status() const noexcept
142     {
143         return status_;
144     }
145 
146     [[nodiscard]] ParserStatus &Status() noexcept
147     {
148         return status_;
149     }
150 
151     [[nodiscard]] bool AllowReceiver() const noexcept
152     {
153         return (status_ & ParserStatus::ALLOW_RECEIVER) != 0;
154     }
155 
156     [[nodiscard]] bool IsExtensionAccessor() const noexcept
157     {
158         return (status_ & ParserStatus::EXTENSION_ACCESSOR) != 0;
159     }
160 
161     [[nodiscard]] bool IsGenerator() const noexcept
162     {
163         return (status_ & ParserStatus::GENERATOR_FUNCTION) != 0;
164     }
165 
166     [[nodiscard]] bool IsFunctionOrParam() const noexcept
167     {
168         return (status_ & (ParserStatus::FUNCTION | ParserStatus::FUNCTION_PARAM)) != 0;
169     }
170 
171     [[nodiscard]] bool IsAsync() const noexcept
172     {
173         return (status_ & ParserStatus::ASYNC_FUNCTION) != 0;
174     }
175 
176     [[nodiscard]] bool IsModule() const noexcept
177     {
178         return (status_ & ParserStatus::MODULE) != 0;
179     }
180 
181     [[nodiscard]] bool IsDynamic() const noexcept
182     {
183         return lang_.IsDynamic();
184     }
185 
186     const ParserContext *FindLabel(const util::StringView &label) const;
187 
188     [[nodiscard]] std::string_view FormattingFileName() const noexcept
189     {
190         return formattingFileName_;
191     }
192 
193     [[nodiscard]] bool IsEnableJsdocParse() const noexcept
194     {
195         return isEnableJsdoc_;
196     }
197 
198     template <typename T>
199     void SetFormattingFileName(T &&fileName)
200     {
201         formattingFileName_ = std::string_view {std::forward<T>(fileName)};
202     }
203 
204 private:
205     const Program *program_;
206     ParserContext *prev_ {};
207     ParserStatus status_ {};
208     util::StringView label_ {};
209     std::string_view formattingFileName_ {DEFAULT_SOURCE_FILE};
210     Language lang_;
211     bool isEnableJsdoc_ {false};
212 };
213 }  // namespace ark::es2panda::parser
214 
215 #endif
216