diff options
author | Li Jin <dragon-fly@qq.com> | 2018-03-05 14:18:35 +0800 |
---|---|---|
committer | Li Jin <dragon-fly@qq.com> | 2018-03-05 14:18:35 +0800 |
commit | 525591758ce178e44da6aa3a11d557fd75b232e7 (patch) | |
tree | 27a2eaef7a47697888ad16ec5b27b9aee999ab71 /MoonParser/moon_ast.cpp | |
parent | 3b270690501cfcc9220c8d5b63ab6f13fc2bd6b0 (diff) | |
download | yuescript-525591758ce178e44da6aa3a11d557fd75b232e7.tar.gz yuescript-525591758ce178e44da6aa3a11d557fd75b232e7.tar.bz2 yuescript-525591758ce178e44da6aa3a11d557fd75b232e7.zip |
refactoring some codes.
Diffstat (limited to 'MoonParser/moon_ast.cpp')
-rw-r--r-- | MoonParser/moon_ast.cpp | 38 |
1 files changed, 25 insertions, 13 deletions
diff --git a/MoonParser/moon_ast.cpp b/MoonParser/moon_ast.cpp index d8a0db9..9627eab 100644 --- a/MoonParser/moon_ast.cpp +++ b/MoonParser/moon_ast.cpp | |||
@@ -1,5 +1,4 @@ | |||
1 | #include <string> | 1 | #include <string> |
2 | #include <codecvt> | ||
3 | #include <unordered_set> | 2 | #include <unordered_set> |
4 | #include <stack> | 3 | #include <stack> |
5 | #include <algorithm> | 4 | #include <algorithm> |
@@ -7,28 +6,24 @@ | |||
7 | #include <vector> | 6 | #include <vector> |
8 | #include "moon_ast.h" | 7 | #include "moon_ast.h" |
9 | 8 | ||
10 | std::string& trim(std::string& s) | 9 | input& trim(input& s) |
11 | { | 10 | { |
12 | s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](unsigned char ch) | 11 | s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](input::value_type ch) |
13 | { | 12 | { |
14 | return !std::isspace(ch); | 13 | return !std::isspace(ch); |
15 | })); | 14 | })); |
16 | s.erase(std::find_if(s.rbegin(), s.rend(), [](unsigned char ch) | 15 | s.erase(std::find_if(s.rbegin(), s.rend(), [](input::value_type ch) |
17 | { | 16 | { |
18 | return !std::isspace(ch); | 17 | return !std::isspace(ch); |
19 | }).base(), s.end()); | 18 | }).base(), s.end()); |
20 | return s; | 19 | return s; |
21 | } | 20 | } |
22 | 21 | ||
23 | const std::string& AstLeaf::getValue() | 22 | const input& AstLeaf::getValue() |
24 | { | 23 | { |
25 | if (_value.empty()) | 24 | if (_value.empty()) |
26 | { | 25 | { |
27 | for (auto it = m_begin.m_it; it != m_end.m_it; ++it) | 26 | _value.assign(m_begin.m_it, m_end.m_it); |
28 | { | ||
29 | char ch = static_cast<char>(*it); | ||
30 | _value.append(&ch, 1); | ||
31 | } | ||
32 | return trim(_value); | 27 | return trim(_value); |
33 | } | 28 | } |
34 | return _value; | 29 | return _value; |
@@ -151,9 +146,11 @@ AST_IMPL(BlockEnd) | |||
151 | 146 | ||
152 | int main() | 147 | int main() |
153 | { | 148 | { |
154 | std::wstring_convert<deletable_facet<std::codecvt<char32_t, char, std::mbstate_t>>, char32_t> conv; | 149 | std::string s = R"TestCodesHere( |
155 | std::string s = R"TestCodesHere()TestCodesHere"; | 150 | thing = { var: 10, hello: "world", func: => @var } |
156 | input i = conv.from_bytes(s); | 151 | import hello, \func from thing |
152 | )TestCodesHere"; | ||
153 | input i = Converter{}.from_bytes(s); | ||
157 | 154 | ||
158 | error_list el; | 155 | error_list el; |
159 | BlockEnd_t* root = nullptr; | 156 | BlockEnd_t* root = nullptr; |
@@ -161,6 +158,21 @@ int main() | |||
161 | if (parse(i, BlockEnd, el, root, &st)) | 158 | if (parse(i, BlockEnd, el, root, &st)) |
162 | { | 159 | { |
163 | std::cout << "matched!\n"; | 160 | std::cout << "matched!\n"; |
161 | root->visit([](ast_node* node) | ||
162 | { | ||
163 | if (std::string("Seperator") != node->getName()) | ||
164 | { | ||
165 | std::cout << "{" << node->getName(); | ||
166 | } | ||
167 | return false; | ||
168 | }, [](ast_node* node) | ||
169 | { | ||
170 | if (std::string("Seperator") != node->getName()) | ||
171 | { | ||
172 | std::cout << "}" ; | ||
173 | } | ||
174 | return false; | ||
175 | }); | ||
164 | } | 176 | } |
165 | else | 177 | else |
166 | { | 178 | { |