• 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_DYNAMIC_LANGUAGE_H
17 #define ES2PANDA_DYNAMIC_LANGUAGE_H
18 
19 #include <algorithm>
20 #include <array>
21 #include <optional>
22 #include <string_view>
23 #include <tuple>
24 
25 #include "util/es2pandaMacros.h"
26 
27 namespace ark::es2panda {
28 
29 class Language {
30 public:
31     enum class Id {
32         AS,
33         JS,
34         TS,
35         ETS,
36         COUNT,
37     };
38 
39     // NOLINTNEXTLINE(*-explicit-constructor)
Language(Id id)40     constexpr Language(Id id) : id_(id) {}
41 
ToString()42     constexpr std::string_view ToString() const
43     {
44         for (auto [id, name, _] : ID_TABLE) {
45             if (id_ == id) {
46                 return name;
47             }
48         }
49 
50         ES2PANDA_UNREACHABLE();
51     }
52 
FromString(std::string_view str)53     static std::optional<Language> FromString(std::string_view str)
54     {
55         for (auto [id, name, _] : ID_TABLE) {
56             if (str == name) {
57                 return Language(id);
58             }
59         }
60 
61         return std::nullopt;
62     }
63 
GetId()64     Id GetId() const
65     {
66         return id_;
67     }
68 
IsDynamic()69     bool IsDynamic() const
70     {
71         for (auto [id, _, isDynamic] : ID_TABLE) {
72             if (id_ == id) {
73                 return isDynamic;
74             }
75         }
76 
77         ES2PANDA_UNREACHABLE();
78     }
79 
80     bool operator==(const Language &l) const
81     {
82         return id_ == l.id_;
83     }
84 
85     bool operator!=(const Language &l) const
86     {
87         return id_ != l.id_;
88     }
89 
90 private:
91     static constexpr auto COUNT = static_cast<size_t>(Id::COUNT);
92     static constexpr auto ID_TABLE = {
93         std::tuple {Id::AS, "as", false},
94         {Id::JS, "js", true},
95         {Id::TS, "ts", true},
96         {Id::ETS, "ets", false},
97     };
98 
99 public:
All()100     static std::array<Language, COUNT> All()
101     {
102         static_assert(std::size(ID_TABLE) == COUNT);
103 
104         std::array<Id, COUNT> arr = {};
105         std::transform(ID_TABLE.begin(), ID_TABLE.end(), arr.begin(), [](const auto &tpl) { return std::get<0>(tpl); });
106 
107         return std::apply([](auto... id) { return std::array<Language, COUNT> {Language(id)...}; }, arr);
108     }
109 
110 private:
111     Id id_;
112 };
113 
114 }  // namespace ark::es2panda
115 
116 // NOLINTNEXTLINE(cert-dcl58-cpp)
117 namespace std {
118 
119 template <>
120 // NOLINTNEXTLINE(altera-struct-pack-align)
121 struct hash<ark::es2panda::Language> {
122     std::size_t operator()(ark::es2panda::Language lang) const
123     {
124         return std::hash<ark::es2panda::Language::Id> {}(lang.GetId());
125     }
126 };
127 
128 }  // namespace std
129 
130 #endif  // ES2PANDA_DYNAMIC_LANGUAGE_H
131