Change ParserContext::m_curPath to dataPath_

This will be used for the upcoming key value completion feature.
Completing key values in a nested list means, that I need the whole
context (all keys of parent list instances). That means m_curPath needs
to be changed to dataPath_.

Change-Id: I29a13576802d01df4f1f420be1c73178a263dad7
diff --git a/src/ast_handlers.hpp b/src/ast_handlers.hpp
index 2e4753d..97c811b 100644
--- a/src/ast_handlers.hpp
+++ b/src/ast_handlers.hpp
@@ -30,7 +30,7 @@
         if (parserContext.m_tmpListKeys.find(ast.first) != parserContext.m_tmpListKeys.end()) {
             _pass(context) = false;
             parserContext.m_errorMsg = "Key \"" + ast.first + "\" was entered more than once.";
-        } else if (!schema.listHasKey(parserContext.m_curPath, {parserContext.m_curModule, parserContext.m_tmpListName}, ast.first)) {
+        } else if (!schema.listHasKey(parserContext.currentSchemaPath(), {parserContext.m_curModule, parserContext.m_tmpListName}, ast.first)) {
             _pass(context) = false;
             parserContext.m_errorMsg = parserContext.m_tmpListName + " is not indexed by \"" + ast.first + "\".";
         } else {
@@ -72,7 +72,7 @@
         auto& parserContext = x3::get<parser_context_tag>(context);
         const Schema& schema = parserContext.m_schema;
 
-        if (schema.isList(parserContext.m_curPath, {parserContext.m_curModule, ast})) {
+        if (schema.isList(parserContext.currentSchemaPath(), {parserContext.m_curModule, ast})) {
             parserContext.m_tmpListName = ast;
         } else {
             _pass(context) = false;
@@ -87,7 +87,7 @@
         auto& parserContext = x3::get<parser_context_tag>(context);
         const Schema& schema = parserContext.m_schema;
 
-        const auto& keysNeeded = schema.listKeys(parserContext.m_curPath, {parserContext.m_curModule, parserContext.m_tmpListName});
+        const auto& keysNeeded = schema.listKeys(parserContext.currentSchemaPath(), {parserContext.m_curModule, parserContext.m_tmpListName});
         std::set<std::string> keysSupplied;
         for (const auto& it : ast)
             keysSupplied.insert(it.first);
@@ -136,7 +136,7 @@
         auto& parserContext = x3::get<parser_context_tag>(context);
         const Schema& schema = parserContext.m_schema;
 
-        if (!schema.isList(parserContext.m_curPath, {parserContext.m_curModule, ast.m_name})) {
+        if (!schema.isList(parserContext.currentSchemaPath(), {parserContext.m_curModule, ast.m_name})) {
             _pass(context) = false;
         }
     }
@@ -147,7 +147,7 @@
     {
         auto& parserContext = x3::get<parser_context_tag>(context);
 
-        if (parserContext.m_curPath.m_nodes.empty())
+        if (parserContext.currentSchemaPath().m_nodes.empty())
             _pass(context) = false;
     }
 };
@@ -159,7 +159,7 @@
         auto& parserContext = x3::get<parser_context_tag>(context);
         const auto& schema = parserContext.m_schema;
 
-        if (!schema.isContainer(parserContext.m_curPath, {parserContext.m_curModule, ast.m_name}))
+        if (!schema.isContainer(parserContext.currentSchemaPath(), {parserContext.m_curModule, ast.m_name}))
             _pass(context) = false;
     }
 };
@@ -171,7 +171,7 @@
         auto& parserContext = x3::get<parser_context_tag>(context);
         const auto& schema = parserContext.m_schema;
 
-        if (!schema.isLeaf(parserContext.m_curPath, {parserContext.m_curModule, ast.m_name}))
+        if (!schema.isLeaf(parserContext.currentSchemaPath(), {parserContext.m_curModule, ast.m_name}))
             _pass(context) = false;
     }
 };
@@ -198,14 +198,8 @@
     void on_success(Iterator const&, Iterator const&, T& ast, Context const& context)
     {
         auto& parserContext = x3::get<parser_context_tag>(context);
-        if (ast.m_suffix.type() == typeid(nodeup_)) {
-            parserContext.m_curPath.m_nodes.pop_back();
-            if (parserContext.m_curPath.m_nodes.empty())
-                parserContext.m_topLevelModulePresent = false;
-        } else {
-            parserContext.m_curPath.m_nodes.push_back(ast);
-            parserContext.m_curModule = boost::none;
-        }
+        parserContext.pushPathFragment(ast);
+        parserContext.m_curModule = boost::none;
     }
 };
 
@@ -214,7 +208,7 @@
     void on_success(Iterator const&, Iterator const&, T& ast, Context const& context)
     {
         auto& parserContext = x3::get<parser_context_tag>(context);
-        parserContext.m_curPath.m_nodes.push_back(dataNodeToSchemaNode(ast));
+        parserContext.pushPathFragment(ast);
     }
 };
 
@@ -223,14 +217,8 @@
     void on_success(Iterator const&, Iterator const&, T& ast, Context const& context)
     {
         auto& parserContext = x3::get<parser_context_tag>(context);
-        if (ast.m_suffix.type() == typeid(nodeup_)) {
-            parserContext.m_curPath.m_nodes.pop_back();
-            if (parserContext.m_curPath.m_nodes.empty())
-                parserContext.m_topLevelModulePresent = false;
-        } else {
-            parserContext.m_curPath.m_nodes.push_back(dataNodeToSchemaNode(ast));
-            parserContext.m_curModule = boost::none;
-        }
+        parserContext.pushPathFragment(ast);
+        parserContext.m_curModule = boost::none;
     }
 };
 
@@ -239,8 +227,7 @@
     void on_success(Iterator const&, Iterator const&, T&, Context const& context)
     {
         auto& parserContext = x3::get<parser_context_tag>(context);
-        parserContext.m_curPath.m_nodes.clear();
-        parserContext.m_topLevelModulePresent = false;
+        parserContext.clearPath();
     }
 };
 
@@ -302,7 +289,7 @@
             if (ast.m_nodes.back().m_prefix)
                 module = ast.m_nodes.back().m_prefix.value().m_name;
             container_ cont = boost::get<container_>(ast.m_nodes.back().m_suffix);
-            schemaPath_ location = pathWithoutLastNode(parserContext.m_curPath);
+            auto location = pathWithoutLastNode(parserContext.currentSchemaPath());
 
             if (!schema.isPresenceContainer(location, {module, cont.m_name})) {
                 parserContext.m_errorMsg = "This container is not a presence container.";
@@ -382,7 +369,7 @@
         auto& parserContext = x3::get<parser_context_tag>(context);
         const auto& schema = parserContext.m_schema;
 
-        if (!schema.isModule(parserContext.m_curPath, ast.m_name)) {
+        if (!schema.isModule(parserContext.currentSchemaPath(), ast.m_name)) {
             parserContext.m_errorMsg = "Invalid module name.";
             _pass(context) = false;
         }
@@ -396,11 +383,11 @@
         auto& parserContext = x3::get<parser_context_tag>(context);
         auto& schema = parserContext.m_schema;
         if (parserContext.m_errorMsg.empty()) {
-            leaf_ leaf = boost::get<leaf_>(parserContext.m_curPath.m_nodes.back().m_suffix);
-            schemaPath_ location = pathWithoutLastNode(parserContext.m_curPath);
+            leaf_ leaf = boost::get<leaf_>(parserContext.currentSchemaPath().m_nodes.back().m_suffix);
+            schemaPath_ location = pathWithoutLastNode(parserContext.currentSchemaPath());
             boost::optional<std::string> module;
-            if (parserContext.m_curPath.m_nodes.back().m_prefix)
-                module = parserContext.m_curPath.m_nodes.back().m_prefix.value().m_name;
+            if (parserContext.currentSchemaPath().m_nodes.back().m_prefix)
+                module = parserContext.currentSchemaPath().m_nodes.back().m_prefix.value().m_name;
             parserContext.m_errorMsg = "Expected " + leafDataTypeToString(schema.leafType(location, {module, leaf.m_name})) + " here:";
             return x3::error_handler_result::fail;
         }
@@ -423,11 +410,11 @@
         auto& parserContext = x3::get<parser_context_tag>(context);
         auto& schema = parserContext.m_schema;
         boost::optional<std::string> module;
-        if (parserContext.m_curPath.m_nodes.back().m_prefix)
-            module = parserContext.m_curPath.m_nodes.back().m_prefix.value().m_name;
+        if (parserContext.currentSchemaPath().m_nodes.back().m_prefix)
+            module = parserContext.currentSchemaPath().m_nodes.back().m_prefix.value().m_name;
 
-        leaf_ leaf = boost::get<leaf_>(parserContext.m_curPath.m_nodes.back().m_suffix);
-        schemaPath_ location = pathWithoutLastNode(parserContext.m_curPath);
+        leaf_ leaf = boost::get<leaf_>(parserContext.currentSchemaPath().m_nodes.back().m_suffix);
+        schemaPath_ location = pathWithoutLastNode(parserContext.currentSchemaPath());
 
         auto type = schema.leafType(location, {module, leaf.m_name});
         if (type == yang::LeafDataTypes::LeafRef) {
@@ -454,11 +441,11 @@
         auto& parserContext = x3::get<parser_context_tag>(context);
         auto& schema = parserContext.m_schema;
         boost::optional<std::string> module;
-        if (parserContext.m_curPath.m_nodes.back().m_prefix)
-            module = parserContext.m_curPath.m_nodes.back().m_prefix.value().m_name;
+        if (parserContext.currentSchemaPath().m_nodes.back().m_prefix)
+            module = parserContext.currentSchemaPath().m_nodes.back().m_prefix.value().m_name;
 
-        leaf_ leaf = boost::get<leaf_>(parserContext.m_curPath.m_nodes.back().m_suffix);
-        schemaPath_ location = pathWithoutLastNode(parserContext.m_curPath);
+        leaf_ leaf = boost::get<leaf_>(parserContext.currentSchemaPath().m_nodes.back().m_suffix);
+        schemaPath_ location = pathWithoutLastNode(parserContext.currentSchemaPath());
 
         if (!schema.leafEnumHasValue(location, {module, leaf.m_name}, ast.m_value)) {
             _pass(context) = false;
@@ -487,11 +474,11 @@
         auto& parserContext = x3::get<parser_context_tag>(context);
         auto& schema = parserContext.m_schema;
         boost::optional<std::string> module;
-        if (parserContext.m_curPath.m_nodes.back().m_prefix)
-            module = parserContext.m_curPath.m_nodes.back().m_prefix.value().m_name;
+        if (parserContext.currentSchemaPath().m_nodes.back().m_prefix)
+            module = parserContext.currentSchemaPath().m_nodes.back().m_prefix.value().m_name;
 
-        leaf_ leaf = boost::get<leaf_>(parserContext.m_curPath.m_nodes.back().m_suffix);
-        schemaPath_ location = pathWithoutLastNode(parserContext.m_curPath);
+        leaf_ leaf = boost::get<leaf_>(parserContext.currentSchemaPath().m_nodes.back().m_suffix);
+        schemaPath_ location = pathWithoutLastNode(parserContext.currentSchemaPath());
 
         ModuleValuePair pair;
         if (ast.m_prefix) {
@@ -541,14 +528,10 @@
     void on_success(Iterator const&, Iterator const&, T&, Context const& context)
     {
         auto& parserContext = x3::get<parser_context_tag>(context);
-        parserContext.m_curPath = parserContext.m_curPathOrig;
+        parserContext.resetPath();
         parserContext.m_tmpListKeys.clear();
         parserContext.m_tmpListName.clear();
         parserContext.m_suggestions.clear();
-        if (!parserContext.m_curPath.m_nodes.empty() && parserContext.m_curPath.m_nodes.at(0).m_prefix)
-            parserContext.m_topLevelModulePresent = true;
-        else
-            parserContext.m_topLevelModulePresent = false;
     }
 };
 
@@ -562,7 +545,7 @@
         const auto& schema = parserContext.m_schema;
 
         parserContext.m_completionIterator = begin;
-        auto suggestions = schema.childNodes(parserContext.m_curPath, Recursion::NonRecursive);
+        auto suggestions = schema.childNodes(parserContext.currentSchemaPath(), Recursion::NonRecursive);
         std::set<std::string> suffixesAdded;
         std::transform(suggestions.begin(), suggestions.end(),
             std::inserter(suffixesAdded, suffixesAdded.end()),
@@ -576,13 +559,13 @@
                     node.second = it;
                 }
 
-                if (schema.isLeaf(parserContext.m_curPath, node)) {
+                if (schema.isLeaf(parserContext.currentSchemaPath(), node)) {
                     return it + " ";
                 }
-                if (schema.isContainer(parserContext.m_curPath, node)) {
+                if (schema.isContainer(parserContext.currentSchemaPath(), node)) {
                     return it + "/";
                 }
-                if (schema.isList(parserContext.m_curPath, node)) {
+                if (schema.isList(parserContext.currentSchemaPath(), node)) {
                     return it + "[";
                 }
                 return it;
@@ -602,7 +585,7 @@
 
         parserContext.m_completionIterator = begin;
 
-        const auto& keysNeeded = schema.listKeys(parserContext.m_curPath, {parserContext.m_curModule, parserContext.m_tmpListName});
+        const auto& keysNeeded = schema.listKeys(parserContext.currentSchemaPath(), {parserContext.m_curModule, parserContext.m_tmpListName});
         parserContext.m_suggestions = generateMissingKeyCompletionSet(keysNeeded, parserContext.m_tmpListKeys);
     }
 };
@@ -615,7 +598,7 @@
         const auto& schema = parserContext.m_schema;
 
         parserContext.m_completionIterator = begin;
-        const auto& keysNeeded = schema.listKeys(parserContext.m_curPath, {parserContext.m_curModule, parserContext.m_tmpListName});
+        const auto& keysNeeded = schema.listKeys(parserContext.currentSchemaPath(), {parserContext.m_curModule, parserContext.m_tmpListName});
         if (generateMissingKeyCompletionSet(keysNeeded, parserContext.m_tmpListKeys).empty()) {
             parserContext.m_suggestions = {"]/"};
         } else {
@@ -666,11 +649,11 @@
         const Schema& schema = parserContext.m_schema;
 
         boost::optional<std::string> module;
-        if (parserContext.m_curPath.m_nodes.back().m_prefix)
-            module = parserContext.m_curPath.m_nodes.back().m_prefix.value().m_name;
+        if (parserContext.currentSchemaPath().m_nodes.back().m_prefix)
+            module = parserContext.currentSchemaPath().m_nodes.back().m_prefix.value().m_name;
 
-        leaf_ leaf = boost::get<leaf_>(parserContext.m_curPath.m_nodes.back().m_suffix);
-        schemaPath_ location = pathWithoutLastNode(parserContext.m_curPath);
+        leaf_ leaf = boost::get<leaf_>(parserContext.currentSchemaPath().m_nodes.back().m_suffix);
+        schemaPath_ location = pathWithoutLastNode(parserContext.currentSchemaPath());
 
         // Only generate completions if the type is enum so that we don't
         // overwrite some other completions.
@@ -689,11 +672,11 @@
         const Schema& schema = parserContext.m_schema;
 
         boost::optional<std::string> module;
-        if (parserContext.m_curPath.m_nodes.back().m_prefix)
-            module = parserContext.m_curPath.m_nodes.back().m_prefix.value().m_name;
+        if (parserContext.currentSchemaPath().m_nodes.back().m_prefix)
+            module = parserContext.currentSchemaPath().m_nodes.back().m_prefix.value().m_name;
 
-        leaf_ leaf = boost::get<leaf_>(parserContext.m_curPath.m_nodes.back().m_suffix);
-        schemaPath_ location = pathWithoutLastNode(parserContext.m_curPath);
+        leaf_ leaf = boost::get<leaf_>(parserContext.currentSchemaPath().m_nodes.back().m_suffix);
+        schemaPath_ location = pathWithoutLastNode(parserContext.currentSchemaPath());
 
         // Only generate completions if the type is identityref so that we
         // don't overwrite some other completions.
diff --git a/src/parser.cpp b/src/parser.cpp
index ce6ea01..de375f8 100644
--- a/src/parser.cpp
+++ b/src/parser.cpp
@@ -24,7 +24,7 @@
 command_ Parser::parseCommand(const std::string& line, std::ostream& errorStream)
 {
     command_ parsedCommand;
-    ParserContext ctx(*m_schema, dataPathToSchemaPath(m_curDir));
+    ParserContext ctx(*m_schema, m_curDir);
     auto it = line.begin();
 
     boost::spirit::x3::error_handler<std::string::const_iterator> errorHandler(it, line.end(), errorStream);
@@ -46,7 +46,7 @@
 {
     std::set<std::string> completions;
     command_ parsedCommand;
-    ParserContext ctx(*m_schema, dataPathToSchemaPath(m_curDir));
+    ParserContext ctx(*m_schema, m_curDir);
     ctx.m_completing = true;
     auto it = line.begin();
     boost::spirit::x3::error_handler<std::string::const_iterator> errorHandler(it, line.end(), errorStream);
diff --git a/src/parser_context.cpp b/src/parser_context.cpp
index c240984..7750301 100644
--- a/src/parser_context.cpp
+++ b/src/parser_context.cpp
@@ -7,11 +7,72 @@
 */
 
 #include "parser_context.hpp"
-ParserContext::ParserContext(const Schema& schema, const schemaPath_& curDir)
+ParserContext::ParserContext(const Schema& schema, const dataPath_& curDir)
     : m_schema(schema)
-    , m_curPath(curDir)
     , m_curPathOrig(curDir)
+    , m_curPath(curDir)
 {
-    if (!m_curPath.m_nodes.empty() && m_curPath.m_nodes.at(0).m_prefix)
+    if (!currentDataPath().m_nodes.empty() && currentDataPath().m_nodes.at(0).m_prefix)
         m_topLevelModulePresent = true;
 }
+
+void ParserContext::clearPath()
+{
+    m_curPath = dataPath_{Scope::Absolute, {}};
+    m_topLevelModulePresent = false;
+}
+
+schemaPath_ ParserContext::currentSchemaPath()
+{
+    if (m_curPath.type() == typeid(dataPath_)) {
+        return dataPathToSchemaPath(boost::get<dataPath_>(m_curPath));
+    } else {
+        return boost::get<schemaPath_>(m_curPath);
+    }
+}
+
+dataPath_ ParserContext::currentDataPath()
+{
+    if (m_curPath.type() != typeid(dataPath_)) {
+        throw std::runtime_error("Tried getting a dataPath_ from ParserContext when only schemaPath_ was available.");
+    }
+    return boost::get<dataPath_>(m_curPath);
+}
+
+void ParserContext::pushPathFragment(const dataNode_& node)
+{
+    auto pushNode = [this] (auto& where, const auto& what) {
+        if (what.m_suffix.type() == typeid(nodeup_)) {
+            where.m_nodes.pop_back();
+            if (where.m_nodes.empty()) {
+                m_topLevelModulePresent = false;
+            }
+        } else {
+            where.m_nodes.push_back(what);
+        }
+    };
+
+    if (m_curPath.type() == typeid(dataPath_)) {
+        pushNode(boost::get<dataPath_>(m_curPath), node);
+    } else {
+        pushNode(boost::get<schemaPath_>(m_curPath), dataNodeToSchemaNode(node));
+    }
+}
+
+void ParserContext::pushPathFragment(const schemaNode_& node)
+{
+    if (m_curPath.type() == typeid(dataPath_)) {
+        m_curPath = dataPathToSchemaPath(boost::get<dataPath_>(m_curPath));
+    }
+
+    boost::get<schemaPath_>(m_curPath).m_nodes.push_back(node);
+    if (currentSchemaPath().m_nodes.empty()) {
+        m_topLevelModulePresent = false;
+    }
+}
+
+void ParserContext::resetPath()
+{
+    m_curPath = m_curPathOrig;
+    m_topLevelModulePresent = !currentDataPath().m_nodes.empty() && currentDataPath().m_nodes.at(0).m_prefix;
+}
diff --git a/src/parser_context.hpp b/src/parser_context.hpp
index 5bf1b26..b20c6ab 100644
--- a/src/parser_context.hpp
+++ b/src/parser_context.hpp
@@ -8,10 +8,16 @@
 
 #include "schema.hpp"
 struct ParserContext {
-    ParserContext(const Schema& schema, const schemaPath_& curDir);
+    ParserContext(const Schema& schema, const dataPath_& curDir);
+    schemaPath_ currentSchemaPath();
+    dataPath_ currentDataPath();
+    void clearPath();
+    void pushPathFragment(const dataNode_& node);
+    void pushPathFragment(const schemaNode_& node);
+    void resetPath();
+
     const Schema& m_schema;
-    schemaPath_ m_curPath;
-    const schemaPath_ m_curPathOrig;
+    const dataPath_ m_curPathOrig;
     boost::optional<std::string> m_curModule;
     std::string m_errorMsg;
     std::string m_tmpListName;
@@ -26,4 +32,7 @@
     // filtering by prefix), this suffix gets added to the completion (for
     // example a left bracket after a list)
     std::string m_completionSuffix;
+
+private:
+    boost::variant<dataPath_, schemaPath_> m_curPath;
 };