• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2021-2022 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_UTIL_OPTIONS_H
17 #define ES2PANDA_UTIL_OPTIONS_H
18 
19 #include "libpandabase/os/file.h"
20 #include "es2panda.h"
21 #include "util/helpers.h"
22 
23 #include <exception>
24 #include <fstream>
25 #include <iostream>
26 
27 namespace panda {
28 class PandArgParser;
29 class PandaArg;
30 }  // namespace panda
31 
32 namespace panda::es2panda::util {
33 enum class OptionFlags : uint32_t {
34     DEFAULT = 0U,
35     PARSE_ONLY = 1U << 0U,
36     PARSE_MODULE = 1U << 1U,
37     SIZE_STAT = 1U << 2U,
38 };
39 
40 inline std::underlying_type_t<OptionFlags> operator&(OptionFlags a, OptionFlags b)
41 {
42     using Utype = std::underlying_type_t<OptionFlags>;
43     /* NOLINTNEXTLINE(hicpp-signed-bitwise) */
44     return static_cast<Utype>(static_cast<Utype>(a) & static_cast<Utype>(b));
45 }
46 
47 inline OptionFlags &operator|=(OptionFlags &a, OptionFlags b)
48 {
49     using Utype = std::underlying_type_t<OptionFlags>;
50     /* NOLINTNEXTLINE(hicpp-signed-bitwise) */
51     return a = static_cast<OptionFlags>(static_cast<Utype>(a) | static_cast<Utype>(b));
52 }
53 
54 template <class T>
BaseName(T const & path)55 T BaseName(T const &path)
56 {
57     return path.substr(path.find_last_of(panda::os::file::File::GetPathDelim()) + 1);
58 }
59 
60 class Options {
61 public:
62     Options();
63     NO_COPY_SEMANTIC(Options);
64     NO_MOVE_SEMANTIC(Options);
65     ~Options();
66 
67     bool Parse(int argc, const char **argv);
68 
Extension()69     es2panda::ScriptExtension Extension() const
70     {
71         return extension_;
72     }
73 
CompilerOptions()74     const es2panda::CompilerOptions &CompilerOptions() const
75     {
76         return compilerOptions_;
77     }
78 
ParserInput()79     const std::string &ParserInput() const
80     {
81         return parserInput_;
82     }
83 
CompilerOutput()84     const std::string &CompilerOutput() const
85     {
86         return compilerOutput_;
87     }
88 
SetCompilerOutput(const std::string & compilerOutput)89     void SetCompilerOutput(const std::string &compilerOutput)
90     {
91         compilerOutput_ = compilerOutput;
92     }
93 
LogLevel()94     std::string_view LogLevel() const
95     {
96         switch (logLevel_) {
97             case util::LogLevel::DEBUG: {
98                 return "debug";
99             }
100             case util::LogLevel::INFO: {
101                 return "info";
102             }
103             case util::LogLevel::WARNING: {
104                 return "warning";
105             }
106             case util::LogLevel::ERROR: {
107                 return "error";
108             }
109             case util::LogLevel::FATAL: {
110                 return "fatal";
111             }
112             default: {
113                 UNREACHABLE();
114             }
115         }
116     }
117 
SourceFile()118     const std::string &SourceFile() const
119     {
120         return sourceFile_;
121     }
122 
ErrorMsg()123     const std::string &ErrorMsg() const
124     {
125         return errorMsg_;
126     }
127 
OptLevel()128     int OptLevel() const
129     {
130         return optLevel_;
131     }
132 
ThreadCount()133     int ThreadCount() const
134     {
135         return threadCount_;
136     }
137 
ParseModule()138     bool ParseModule() const
139     {
140         return (options_ & OptionFlags::PARSE_MODULE) != 0;
141     }
142 
ParseOnly()143     bool ParseOnly() const
144     {
145         return (options_ & OptionFlags::PARSE_ONLY) != 0;
146     }
147 
SizeStat()148     bool SizeStat() const
149     {
150         return (options_ & OptionFlags::SIZE_STAT) != 0;
151     }
152 
IsDynamic()153     bool IsDynamic() const
154     {
155         return extension_ != es2panda::ScriptExtension::ETS;
156     }
157 
ListFiles()158     bool ListFiles() const
159     {
160         return listFiles_;
161     }
162 
163 private:
164     es2panda::ScriptExtension extension_ {es2panda::ScriptExtension::JS};
165     OptionFlags options_ {OptionFlags::DEFAULT};
166     es2panda::CompilerOptions compilerOptions_ {};
167     panda::PandArgParser *argparser_;
168     std::string parserInput_;
169     std::string compilerOutput_;
170     std::string result_;
171     std::string sourceFile_;
172     std::string errorMsg_;
173     int optLevel_ {0};
174     int threadCount_ {0};
175     bool listFiles_ {false};
176     util::LogLevel logLevel_ {util::LogLevel::ERROR};
177 };
178 }  // namespace panda::es2panda::util
179 
180 #endif  // UTIL_OPTIONS_H
181