From 525591758ce178e44da6aa3a11d557fd75b232e7 Mon Sep 17 00:00:00 2001 From: Li Jin Date: Mon, 5 Mar 2018 14:18:35 +0800 Subject: refactoring some codes. --- MoonParser/moon_ast.cpp | 38 +++++++++++++++++++++++++------------- 1 file changed, 25 insertions(+), 13 deletions(-) (limited to 'MoonParser/moon_ast.cpp') 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 @@ #include -#include #include #include #include @@ -7,28 +6,24 @@ #include #include "moon_ast.h" -std::string& trim(std::string& s) +input& trim(input& s) { - s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](unsigned char ch) + s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](input::value_type ch) { return !std::isspace(ch); })); - s.erase(std::find_if(s.rbegin(), s.rend(), [](unsigned char ch) + s.erase(std::find_if(s.rbegin(), s.rend(), [](input::value_type ch) { return !std::isspace(ch); }).base(), s.end()); return s; } -const std::string& AstLeaf::getValue() +const input& AstLeaf::getValue() { if (_value.empty()) { - for (auto it = m_begin.m_it; it != m_end.m_it; ++it) - { - char ch = static_cast(*it); - _value.append(&ch, 1); - } + _value.assign(m_begin.m_it, m_end.m_it); return trim(_value); } return _value; @@ -151,9 +146,11 @@ AST_IMPL(BlockEnd) int main() { - std::wstring_convert>, char32_t> conv; - std::string s = R"TestCodesHere()TestCodesHere"; - input i = conv.from_bytes(s); + std::string s = R"TestCodesHere( +thing = { var: 10, hello: "world", func: => @var } +import hello, \func from thing +)TestCodesHere"; + input i = Converter{}.from_bytes(s); error_list el; BlockEnd_t* root = nullptr; @@ -161,6 +158,21 @@ int main() if (parse(i, BlockEnd, el, root, &st)) { std::cout << "matched!\n"; + root->visit([](ast_node* node) + { + if (std::string("Seperator") != node->getName()) + { + std::cout << "{" << node->getName(); + } + return false; + }, [](ast_node* node) + { + if (std::string("Seperator") != node->getName()) + { + std::cout << "}" ; + } + return false; + }); } else { -- cgit v1.2.3-55-g6feb