• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2021 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 <macros.h>
20 #include <parser/program/program.h>
21 #include <util/enumbitops.h>
22 #include <util/ustring.h>
23 
24 #include <vector>
25 
26 namespace panda::es2panda::parser {
27 enum class ParserStatus {
28     NO_OPTS = 0,
29     STRICT = (1 << 0),
30 
31     FUNCTION = (1 << 1),
32     ARROW_FUNCTION = (1 << 2),
33     GENERATOR_FUNCTION = (1 << 3),
34     ASYNC_FUNCTION = (1 << 4),
35     CONSTRUCTOR_FUNCTION = (1 << 5),
36     FUNCTION_PARAM = (1 << 6),
37     IS_SPREAD = (1 << 7),
38     ACCESSOR_FUNCTION = (1 << 8),
39     FUNCTION_DECLARATION = (1 << 9),
40 
41     ALLOW_SUPER = (1 << 10),
42     ALLOW_SUPER_CALL = (1 << 11),
43 
44     DISALLOW_AWAIT = (1 << 12),
45     ALLOW_YIELD = (1 << 13),
46     IN_ITERATION = (1 << 14),
47     IN_LABELED = (1 << 15),
48 
49     EXPORT_REACHED = (1 << 16),
50     HAS_COMPLEX_PARAM = (1 << 17),
51     IN_SWITCH = (1 << 18),
52 
53     MODULE = (1 << 19),
54     ALLOW_NEW_TARGET = (1 << 20),
55 
56     IN_EXTENDS = (1 << 21),
57     ALLOW_THIS_TYPE = (1 << 22),
58     IN_METHOD_DEFINITION = (1 << 23),
59     IN_AMBIENT_CONTEXT = (1 << 24),
60     IN_CLASS_BODY = (1 << 25),
61     IN_DECORATOR = (1 << 26),
62     DISALLOW_CONTINUE = (1 << 27),
63 
64     TS_MODULE = (1 << 28),
65 };
66 
DEFINE_BITOPS(ParserStatus)67 DEFINE_BITOPS(ParserStatus)
68 
69 class ParserContext {
70 public:
71     explicit ParserContext(const Program *program) : program_(program) {}
72     explicit ParserContext(ParserContext *current, ParserStatus newStatus, util::StringView label = "")
73         : program_(current->program_), prev_(current), label_(label)
74     {
75         ParserStatus currentStatus = current->status_;
76         currentStatus &= (ParserStatus::MODULE | ParserStatus::ALLOW_NEW_TARGET | ParserStatus::IN_EXTENDS |
77                           ParserStatus::ALLOW_THIS_TYPE | ParserStatus::IN_CLASS_BODY | ParserStatus::FUNCTION);
78         status_ = currentStatus | newStatus;
79     }
80 
81     DEFAULT_COPY_SEMANTIC(ParserContext);
82     DEFAULT_MOVE_SEMANTIC(ParserContext);
83     ~ParserContext() = default;
84 
85     const Program *GetProgram() const
86     {
87         return program_;
88     }
89 
90     ParserContext *Prev() const
91     {
92         return prev_;
93     }
94 
95     const ParserStatus &Status() const
96     {
97         return status_;
98     }
99 
100     ParserStatus &Status()
101     {
102         return status_;
103     }
104 
105     bool IsGenerator() const
106     {
107         return (status_ & ParserStatus::GENERATOR_FUNCTION) != 0;
108     }
109 
110     bool AllowYield() const
111     {
112         return (status_ & ParserStatus::ALLOW_YIELD) != 0;
113     }
114 
115     bool DisallowAwait() const
116     {
117         return (status_ & ParserStatus::DISALLOW_AWAIT) != 0;
118     }
119 
120     bool IsAsync() const
121     {
122         return (status_ & ParserStatus::ASYNC_FUNCTION) != 0;
123     }
124 
125     bool IsModule() const
126     {
127         return (status_ & ParserStatus::MODULE) != 0;
128     }
129 
130     bool IsTsModule() const
131     {
132         return (status_ & ParserStatus::TS_MODULE) != 0;
133     }
134 
135     const ParserContext *FindLabel(const util::StringView &label) const;
136 
137 private:
138     const Program *program_;
139     ParserContext *prev_ {};
140     ParserStatus status_ {};
141     util::StringView label_ {};
142 };
143 }  // namespace panda::es2panda::parser
144 
145 #endif
146