refactor the parser class and parsercontext to work with path_
Change-Id: Ia4c8b5d183c39883d9c7190cc74983578b22d46a
diff --git a/src/CParser.cpp b/src/CParser.cpp
index 357970f..cb81de0 100644
--- a/src/CParser.cpp
+++ b/src/CParser.cpp
@@ -12,17 +12,6 @@
InvalidCommandException::~InvalidCommandException() = default;
-struct nodeToString : public boost::static_visitor<std::string> {
- std::string operator()(const nodeup_&) const
- {
- return "..";
- }
- template <class T>
- std::string operator()(const T& node) const
- {
- return node.m_name;
- }
-};
CParser::CParser(const CTree& tree)
: m_tree(tree)
@@ -52,22 +41,21 @@
void CParser::changeNode(const path_& name)
{
- if (name.m_nodes.empty()) {
- m_curDir = "";
- return;
- }
for (const auto& it : name.m_nodes) {
- const std::string node = boost::apply_visitor(nodeToString(), it);
- if (node == "..") {
- m_curDir = stripLastNodeFromPath(m_curDir);
- } else {
- m_curDir = joinPaths(m_curDir, boost::apply_visitor(nodeToString(), it));
- }
-
+ if (it.type() == typeid(nodeup_))
+ m_curDir.m_nodes.pop_back();
+ else
+ m_curDir.m_nodes.push_back(it);
}
}
+
std::string CParser::currentNode() const
{
- return m_curDir;
+ std::string res;
+ for (const auto& it : m_curDir.m_nodes) {
+ res = joinPaths(res, boost::apply_visitor(nodeToString(), it));
+ }
+
+ return res;
}
diff --git a/src/CParser.hpp b/src/CParser.hpp
index a7d5790..bf21300 100644
--- a/src/CParser.hpp
+++ b/src/CParser.hpp
@@ -41,5 +41,5 @@
private:
const CTree& m_tree;
- std::string m_curDir;
+ path_ m_curDir;
};
diff --git a/src/CTree.cpp b/src/CTree.cpp
index e68a2bc..6840697 100644
--- a/src/CTree.cpp
+++ b/src/CTree.cpp
@@ -13,6 +13,13 @@
InvalidNodeException::~InvalidNodeException() = default;
+std::string pathToString(const path_& path)
+{
+ std::string res;
+ for (const auto it : path.m_nodes)
+ res = joinPaths(res, boost::apply_visitor(nodeToString(), it));
+ return res;
+}
CTree::CTree()
{
@@ -33,12 +40,13 @@
return childrenRef.find(name) != childrenRef.end();
}
-bool CTree::isContainer(const std::string& location, const std::string& name) const
+bool CTree::isContainer(const path_& location, const std::string& name) const
{
- if (!nodeExists(location, name))
+ std::string locationString = pathToString(location);
+ if (!nodeExists(locationString, name))
return false;
- return children(location).at(name).type() == typeid(schema::container);
+ return children(locationString).at(name).type() == typeid(schema::container);
}
void CTree::addContainer(const std::string& location, const std::string& name)
@@ -51,29 +59,32 @@
}
-bool CTree::listHasKey(const std::string& location, const std::string& name, const std::string& key) const
+bool CTree::listHasKey(const path_& location, const std::string& name, const std::string& key) const
{
+ std::string locationString = pathToString(location);
assert(isList(location, name));
- const auto& child = children(location).at(name);
+ const auto& child = children(locationString).at(name);
const auto& list = boost::get<schema::list>(child);
return list.m_keys.find(key) != list.m_keys.end();
}
-const std::set<std::string>& CTree::listKeys(const std::string& location, const std::string& name) const
+const std::set<std::string>& CTree::listKeys(const path_& location, const std::string& name) const
{
+ std::string locationString = pathToString(location);
assert(isList(location, name));
- const auto& child = children(location).at(name);
+ const auto& child = children(locationString).at(name);
const auto& list = boost::get<schema::list>(child);
return list.m_keys;
}
-bool CTree::isList(const std::string& location, const std::string& name) const
+bool CTree::isList(const path_& location, const std::string& name) const
{
- if (!nodeExists(location, name))
+ std::string locationString = pathToString(location);
+ if (!nodeExists(locationString, name))
return false;
- const auto& child = children(location).at(name);
+ const auto& child = children(locationString).at(name);
if (child.type() != typeid(schema::list))
return false;
diff --git a/src/CTree.hpp b/src/CTree.hpp
index 6093ee1..8b42371 100644
--- a/src/CTree.hpp
+++ b/src/CTree.hpp
@@ -22,6 +22,17 @@
};
}
+struct nodeToString : public boost::static_visitor<std::string> {
+ std::string operator()(const nodeup_&) const
+ {
+ return "..";
+ }
+ template <class T>
+ std::string operator()(const T& node) const
+ {
+ return node.m_name;
+ }
+};
using NodeType = boost::variant<schema::container, schema::list>;
@@ -43,11 +54,11 @@
CTree();
bool nodeExists(const std::string& location, const std::string& name) const;
- bool isContainer(const std::string& location, const std::string& name) const;
+ bool isContainer(const path_& location, const std::string& name) const;
void addContainer(const std::string& location, const std::string& name);
- const std::set<std::string>& listKeys(const std::string& location, const std::string& name) const;
- bool listHasKey(const std::string& location, const std::string& name, const std::string& key) const;
- bool isList(const std::string& location, const std::string& name) const;
+ const std::set<std::string>& listKeys(const path_& location, const std::string& name) const;
+ bool listHasKey(const path_& location, const std::string& name, const std::string& key) const;
+ bool isList(const path_& location, const std::string& name) const;
void addList(const std::string& location, const std::string& name, const std::set<std::string>& keys);
private:
diff --git a/src/ast_handlers.hpp b/src/ast_handlers.hpp
index 9ab4738..ce8a196 100644
--- a/src/ast_handlers.hpp
+++ b/src/ast_handlers.hpp
@@ -79,7 +79,7 @@
void on_success(Iterator const&, Iterator const&, T& ast, Context const& context)
{
auto& parserContext = x3::get<parser_context_tag>(context);
- parserContext.m_curPath = joinPaths(parserContext.m_curPath, ast.m_name);
+ parserContext.m_curPath.m_nodes.push_back(ast);
}
template <typename Iterator, typename Exception, typename Context>
@@ -105,8 +105,8 @@
{
auto& parserContext = x3::get<parser_context_tag>(context);
- if (!parserContext.m_curPath.empty()) {
- parserContext.m_curPath = stripLastNodeFromPath(parserContext.m_curPath);
+ if (!parserContext.m_curPath.m_nodes.empty()) {
+ parserContext.m_curPath.m_nodes.pop_back();
} else {
_pass(context) = false;
}
@@ -122,7 +122,7 @@
const auto& tree = parserContext.m_tree;
if (tree.isContainer(parserContext.m_curPath, ast.m_name)) {
- parserContext.m_curPath = joinPaths(parserContext.m_curPath, ast.m_name);
+ parserContext.m_curPath.m_nodes.push_back(ast);
} else {
_pass(context) = false;
}
diff --git a/src/parser_context.cpp b/src/parser_context.cpp
index 5766c2f..db2bb43 100644
--- a/src/parser_context.cpp
+++ b/src/parser_context.cpp
@@ -7,7 +7,7 @@
*/
#include "parser_context.hpp"
-ParserContext::ParserContext(const CTree& tree, const std::string curDir)
+ParserContext::ParserContext(const CTree& tree, const path_ curDir)
: m_tree(tree)
{
m_curPath = curDir;
diff --git a/src/parser_context.hpp b/src/parser_context.hpp
index 4d6cf99..2c90120 100644
--- a/src/parser_context.hpp
+++ b/src/parser_context.hpp
@@ -8,9 +8,9 @@
#include "CTree.hpp"
struct ParserContext {
- ParserContext(const CTree& tree, const std::string curDir);
+ ParserContext(const CTree& tree, const path_ curDir);
const CTree& m_tree;
- std::string m_curPath;
+ path_ m_curPath;
std::string m_errorMsg;
std::string m_tmpListName;
std::set<std::string> m_tmpListKeys;