diff options
author | Li Jin <dragon-fly@qq.com> | 2020-02-22 01:24:47 +0800 |
---|---|---|
committer | Li Jin <dragon-fly@qq.com> | 2020-02-22 01:24:47 +0800 |
commit | 39457b75c0923cf287c9145fd9c9a6ba4a86767b (patch) | |
tree | 975f5adcad63d17821b2f40020dc2e15fae1bdac | |
parent | 2e9e28ceb3444e0aaf0ff7c704800b9cdc25dc87 (diff) | |
download | yuescript-39457b75c0923cf287c9145fd9c9a6ba4a86767b.tar.gz yuescript-39457b75c0923cf287c9145fd9c9a6ba4a86767b.tar.bz2 yuescript-39457b75c0923cf287c9145fd9c9a6ba4a86767b.zip |
change some interfaces.
-rw-r--r-- | src/MoonP/moon_compiler.cpp | 17 | ||||
-rw-r--r-- | src/MoonP/moon_compiler.h | 12 | ||||
-rw-r--r-- | src/MoonP/moon_parser.cpp | 6 | ||||
-rw-r--r-- | src/MoonP/moon_parser.h | 6 | ||||
-rw-r--r-- | src/moonp.cpp | 4 |
5 files changed, 31 insertions, 14 deletions
diff --git a/src/MoonP/moon_compiler.cpp b/src/MoonP/moon_compiler.cpp index e6d010c..64152cf 100644 --- a/src/MoonP/moon_compiler.cpp +++ b/src/MoonP/moon_compiler.cpp | |||
@@ -35,9 +35,9 @@ const char* moonScriptVersion() { | |||
35 | return "0.5.0-r0.1.3"; | 35 | return "0.5.0-r0.1.3"; |
36 | } | 36 | } |
37 | 37 | ||
38 | class MoonCompiler { | 38 | class MoonCompilerImpl { |
39 | public: | 39 | public: |
40 | std::tuple<std::string,std::string,GlobalVars> compile(const std::string& codes, const MoonConfig& config) { | 40 | std::tuple<std::string,std::string,GlobalVars> compile(std::string_view codes, const MoonConfig& config) { |
41 | _config = config; | 41 | _config = config; |
42 | _info = _parser.parse<File_t>(codes); | 42 | _info = _parser.parse<File_t>(codes); |
43 | GlobalVars globals; | 43 | GlobalVars globals; |
@@ -4275,10 +4275,17 @@ private: | |||
4275 | } | 4275 | } |
4276 | }; | 4276 | }; |
4277 | 4277 | ||
4278 | const std::string MoonCompiler::Empty; | 4278 | const std::string MoonCompilerImpl::Empty; |
4279 | 4279 | ||
4280 | std::tuple<std::string,std::string,GlobalVars> moonCompile(const std::string& codes, const MoonConfig& config) { | 4280 | MoonCompiler::MoonCompiler(): |
4281 | return MoonCompiler{}.compile(codes, config); | 4281 | _compiler(std::make_unique<MoonCompilerImpl>()) |
4282 | { } | ||
4283 | |||
4284 | MoonCompiler::~MoonCompiler() | ||
4285 | { } | ||
4286 | |||
4287 | std::tuple<std::string,std::string,GlobalVars> MoonCompiler::compile(std::string_view codes, const MoonConfig& config) { | ||
4288 | return _compiler->compile(codes, config); | ||
4282 | } | 4289 | } |
4283 | 4290 | ||
4284 | } // namespace MoonP | 4291 | } // namespace MoonP |
diff --git a/src/MoonP/moon_compiler.h b/src/MoonP/moon_compiler.h index 0739a59..36fd77a 100644 --- a/src/MoonP/moon_compiler.h +++ b/src/MoonP/moon_compiler.h | |||
@@ -9,6 +9,7 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI | |||
9 | #pragma once | 9 | #pragma once |
10 | 10 | ||
11 | #include <string> | 11 | #include <string> |
12 | #include <string_view> | ||
12 | #include <tuple> | 13 | #include <tuple> |
13 | #include <list> | 14 | #include <list> |
14 | #include <memory> | 15 | #include <memory> |
@@ -31,6 +32,15 @@ struct GlobalVar { | |||
31 | 32 | ||
32 | using GlobalVars = std::unique_ptr<std::list<GlobalVar>>; | 33 | using GlobalVars = std::unique_ptr<std::list<GlobalVar>>; |
33 | 34 | ||
34 | std::tuple<std::string,std::string,GlobalVars> moonCompile(const std::string& codes, const MoonConfig& config = {}); | 35 | class MoonCompilerImpl; |
36 | |||
37 | class MoonCompiler { | ||
38 | public: | ||
39 | MoonCompiler(); | ||
40 | virtual ~MoonCompiler(); | ||
41 | std::tuple<std::string,std::string,GlobalVars> compile(std::string_view codes, const MoonConfig& config = {}); | ||
42 | private: | ||
43 | std::unique_ptr<MoonCompilerImpl> _compiler; | ||
44 | }; | ||
35 | 45 | ||
36 | } // namespace MoonP | 46 | } // namespace MoonP |
diff --git a/src/MoonP/moon_parser.cpp b/src/MoonP/moon_parser.cpp index 39551d2..1502f3c 100644 --- a/src/MoonP/moon_parser.cpp +++ b/src/MoonP/moon_parser.cpp | |||
@@ -493,11 +493,11 @@ MoonParser::MoonParser() { | |||
493 | File = White >> -Shebang >> Block >> eof(); | 493 | File = White >> -Shebang >> Block >> eof(); |
494 | } | 494 | } |
495 | 495 | ||
496 | ParseInfo MoonParser::parse(const std::string& codes, rule& r) { | 496 | ParseInfo MoonParser::parse(std::string_view codes, rule& r) { |
497 | ParseInfo res; | 497 | ParseInfo res; |
498 | try { | 498 | try { |
499 | res.codes = std::make_unique<input>(); | 499 | res.codes = std::make_unique<input>(); |
500 | *(res.codes) = _converter.from_bytes(codes); | 500 | *(res.codes) = _converter.from_bytes(codes.begin(), codes.end()); |
501 | } catch (const std::range_error&) { | 501 | } catch (const std::range_error&) { |
502 | res.error = "Invalid text encoding."sv; | 502 | res.error = "Invalid text encoding."sv; |
503 | return res; | 503 | return res; |
@@ -537,7 +537,7 @@ std::string MoonParser::toString(input::iterator begin, input::iterator end) { | |||
537 | } | 537 | } |
538 | 538 | ||
539 | input MoonParser::encode(std::string_view codes) { | 539 | input MoonParser::encode(std::string_view codes) { |
540 | return _converter.from_bytes(std::string(codes)); | 540 | return _converter.from_bytes(codes.begin(), codes.end()); |
541 | } | 541 | } |
542 | 542 | ||
543 | std::string MoonParser::decode(const input& codes) { | 543 | std::string MoonParser::decode(const input& codes) { |
diff --git a/src/MoonP/moon_parser.h b/src/MoonP/moon_parser.h index 7a1a8a9..215c6a4 100644 --- a/src/MoonP/moon_parser.h +++ b/src/MoonP/moon_parser.h | |||
@@ -45,7 +45,7 @@ public: | |||
45 | MoonParser(); | 45 | MoonParser(); |
46 | 46 | ||
47 | template<class AST> | 47 | template<class AST> |
48 | ParseInfo parse(const std::string& codes) { | 48 | ParseInfo parse(std::string_view codes) { |
49 | error_list errors; | 49 | error_list errors; |
50 | auto res = parse(codes, getRule<AST>()); | 50 | auto res = parse(codes, getRule<AST>()); |
51 | if (res.node.template is<AST>()) { | 51 | if (res.node.template is<AST>()) { |
@@ -55,7 +55,7 @@ public: | |||
55 | } | 55 | } |
56 | 56 | ||
57 | template <class AST> | 57 | template <class AST> |
58 | bool match(const std::string& codes) { | 58 | bool match(std::string_view codes) { |
59 | auto rEnd = rule(getRule<AST>() >> eof()); | 59 | auto rEnd = rule(getRule<AST>() >> eof()); |
60 | return parse(codes, rEnd).node; | 60 | return parse(codes, rEnd).node; |
61 | } | 61 | } |
@@ -67,7 +67,7 @@ public: | |||
67 | std::string decode(const input& input); | 67 | std::string decode(const input& input); |
68 | 68 | ||
69 | protected: | 69 | protected: |
70 | ParseInfo parse(const std::string& codes, rule& r); | 70 | ParseInfo parse(std::string_view codes, rule& r); |
71 | 71 | ||
72 | struct State { | 72 | struct State { |
73 | State() { | 73 | State() { |
diff --git a/src/moonp.cpp b/src/moonp.cpp index be5f536..3f36d9d 100644 --- a/src/moonp.cpp +++ b/src/moonp.cpp | |||
@@ -91,7 +91,7 @@ int main(int narg, const char** args) { | |||
91 | std::istreambuf_iterator<char>()); | 91 | std::istreambuf_iterator<char>()); |
92 | if (dumpCompileTime) { | 92 | if (dumpCompileTime) { |
93 | auto start = std::chrono::high_resolution_clock::now(); | 93 | auto start = std::chrono::high_resolution_clock::now(); |
94 | auto result = MoonP::moonCompile(s, config); | 94 | auto result = MoonP::MoonCompiler{}.compile(s, config); |
95 | auto end = std::chrono::high_resolution_clock::now(); | 95 | auto end = std::chrono::high_resolution_clock::now(); |
96 | if (!std::get<0>(result).empty()) { | 96 | if (!std::get<0>(result).empty()) { |
97 | std::chrono::duration<double> diff = end - start; | 97 | std::chrono::duration<double> diff = end - start; |
@@ -109,7 +109,7 @@ int main(int narg, const char** args) { | |||
109 | return 1; | 109 | return 1; |
110 | } | 110 | } |
111 | } | 111 | } |
112 | auto result = MoonP::moonCompile(s, config); | 112 | auto result = MoonP::MoonCompiler{}.compile(s, config); |
113 | if (!std::get<0>(result).empty()) { | 113 | if (!std::get<0>(result).empty()) { |
114 | if (!writeToFile) { | 114 | if (!writeToFile) { |
115 | std::cout << std::get<0>(result) << '\n'; | 115 | std::cout << std::get<0>(result) << '\n'; |