Merge changes Ia2ec062e,Iab642afe,Ied6dca1b

* changes:
  build: make it possible to use stuff in shared libraries
  Add missing inter-library dependencies
  Remove unimplemented function
diff --git a/src/ast_commands.hpp b/src/ast_commands.hpp
index c5ffb61..7f3160a 100644
--- a/src/ast_commands.hpp
+++ b/src/ast_commands.hpp
@@ -14,7 +14,7 @@
 
 namespace x3 = boost::spirit::x3;
 
-using keyValue_ = std::pair<std::string, std::string>;
+using keyValue_ = std::pair<std::string, leaf_data_>;
 
 enum class LsOption {
     Recursive
diff --git a/src/ast_handlers.hpp b/src/ast_handlers.hpp
index 2e4753d..0ce611d 100644
--- a/src/ast_handlers.hpp
+++ b/src/ast_handlers.hpp
@@ -25,14 +25,10 @@
     void on_success(Iterator const&, Iterator const&, T& ast, Context const& context)
     {
         auto& parserContext = x3::get<parser_context_tag>(context);
-        const Schema& schema = parserContext.m_schema;
 
         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)) {
-            _pass(context) = false;
-            parserContext.m_errorMsg = parserContext.m_tmpListName + " is not indexed by \"" + ast.first + "\".";
         } else {
             parserContext.m_tmpListKeys.insert(ast.first);
         }
@@ -61,7 +57,28 @@
     }
 };
 
-struct key_identifier_class;
+struct key_identifier_class {
+    template <typename T, typename Iterator, typename Context>
+    void on_success(Iterator const&, Iterator const&, T& ast, Context const& context)
+    {
+        auto& parserContext = x3::get<parser_context_tag>(context);
+        const Schema& schema = parserContext.m_schema;
+        schemaPath_ location = parserContext.currentSchemaPath();
+        ModuleNodePair list{parserContext.m_curModule, parserContext.m_tmpListName};
+
+        if (schema.listHasKey(location, list, ast)) {
+            schemaNode_ listNode;
+            listNode.m_prefix = parserContext.m_curModule ? boost::optional<module_>{{*parserContext.m_curModule}} : boost::none;
+            listNode.m_suffix = list_{parserContext.m_tmpListName};
+            location.m_nodes.push_back(listNode);
+            parserContext.m_tmpListKeyLeafPath.m_location = location;
+            parserContext.m_tmpListKeyLeafPath.m_node = {boost::none, ast};
+        } else {
+            parserContext.m_errorMsg = parserContext.m_tmpListName + " is not indexed by \"" + ast + "\".";
+            _pass(context) = false;
+        }
+    }
+};
 
 struct module_identifier_class;
 
@@ -72,7 +89,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 +104,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 +153,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 +164,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 +176,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 +188,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 +215,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 +225,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 +234,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 +244,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 +306,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.";
@@ -361,11 +365,18 @@
 
 struct leaf_path_class {
     template <typename T, typename Iterator, typename Context>
-    void on_success(Iterator const&, Iterator const&, T& ast, Context const& context)
+    void on_success(Iterator const&, Iterator const&, T&, Context const& context)
     {
         auto& parserContext = x3::get<parser_context_tag>(context);
         try {
-            auto leaf = boost::get<leaf_>(ast.m_nodes.back().m_suffix);
+            auto lastNode = parserContext.currentSchemaPath().m_nodes.back();
+            auto leaf = boost::get<leaf_>(lastNode.m_suffix);
+            auto location = pathWithoutLastNode(parserContext.currentSchemaPath());
+            ModuleNodePair node{lastNode.m_prefix.flat_map([](const auto& it) { return boost::optional<std::string>{it.m_name}; }), leaf.m_name};
+
+            parserContext.m_tmpListKeyLeafPath.m_location = location;
+            parserContext.m_tmpListKeyLeafPath.m_node = node;
+
         } catch (boost::bad_get&) {
             parserContext.m_errorMsg = "This is not a path to leaf.";
             _pass(context) = false;
@@ -382,7 +393,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,12 +407,8 @@
         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);
-            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;
-            parserContext.m_errorMsg = "Expected " + leafDataTypeToString(schema.leafType(location, {module, leaf.m_name})) + " here:";
+            parserContext.m_errorMsg = "leaf data type mismatch: Expected " +
+                leafDataTypeToString(schema.leafType(parserContext.m_tmpListKeyLeafPath.m_location, parserContext.m_tmpListKeyLeafPath.m_node)) + " here:";
             return x3::error_handler_result::fail;
         }
         return x3::error_handler_result::rethrow;
@@ -422,17 +429,12 @@
     {
         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;
 
-        leaf_ leaf = boost::get<leaf_>(parserContext.m_curPath.m_nodes.back().m_suffix);
-        schemaPath_ location = pathWithoutLastNode(parserContext.m_curPath);
-
-        auto type = schema.leafType(location, {module, leaf.m_name});
+        auto type = schema.leafType(parserContext.m_tmpListKeyLeafPath.m_location, parserContext.m_tmpListKeyLeafPath.m_node);
         if (type == yang::LeafDataTypes::LeafRef) {
-            type = schema.leafrefBase(location, {module, leaf.m_name});
+            type = schema.leafrefBase(parserContext.m_tmpListKeyLeafPath.m_location, parserContext.m_tmpListKeyLeafPath.m_node);
         }
+
         if (type != m_type) {
             _pass(context) = false;
         }
@@ -453,18 +455,11 @@
             return;
         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;
 
-        leaf_ leaf = boost::get<leaf_>(parserContext.m_curPath.m_nodes.back().m_suffix);
-        schemaPath_ location = pathWithoutLastNode(parserContext.m_curPath);
-
-        if (!schema.leafEnumHasValue(location, {module, leaf.m_name}, ast.m_value)) {
+        if (!schema.leafEnumHasValue(parserContext.m_tmpListKeyLeafPath.m_location, parserContext.m_tmpListKeyLeafPath.m_node, ast.m_value)) {
             _pass(context) = false;
-
-            parserContext.m_errorMsg = "Expected an enum here. Allowed values:";
-            for (const auto& it : schema.enumValues(location, {module, leaf.m_name})) {
+            parserContext.m_errorMsg = "leaf data type mismatch: Expected an enum here. Allowed values:";
+            for (const auto& it : schema.enumValues(parserContext.m_tmpListKeyLeafPath.m_location, parserContext.m_tmpListKeyLeafPath.m_node)) {
                 parserContext.m_errorMsg += " " + it;
             }
         }
@@ -486,12 +481,6 @@
             return;
         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;
-
-        leaf_ leaf = boost::get<leaf_>(parserContext.m_curPath.m_nodes.back().m_suffix);
-        schemaPath_ location = pathWithoutLastNode(parserContext.m_curPath);
 
         ModuleValuePair pair;
         if (ast.m_prefix) {
@@ -499,7 +488,7 @@
         }
         pair.second = ast.m_value;
 
-        if (!schema.leafIdentityIsValid(location, {module, leaf.m_name}, pair)) {
+        if (!schema.leafIdentityIsValid(parserContext.m_tmpListKeyLeafPath.m_location, parserContext.m_tmpListKeyLeafPath.m_node, pair)) {
             _pass(context) = false;
         }
     }
@@ -541,14 +530,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 +547,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 +561,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 +587,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 +600,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 {
@@ -662,20 +647,14 @@
     void on_success(Iterator const& begin, Iterator const&, T&, Context const& context)
     {
         auto& parserContext = x3::get<parser_context_tag>(context);
-        parserContext.m_completionIterator = begin;
         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;
-
-        leaf_ leaf = boost::get<leaf_>(parserContext.m_curPath.m_nodes.back().m_suffix);
-        schemaPath_ location = pathWithoutLastNode(parserContext.m_curPath);
+        parserContext.m_completionIterator = begin;
 
         // Only generate completions if the type is enum so that we don't
         // overwrite some other completions.
-        if (schema.leafType(location, {module, leaf.m_name}) == yang::LeafDataTypes::Enum)
-            parserContext.m_suggestions = schema.enumValues(location, {module, leaf.m_name});
+        if (schema.leafType(parserContext.m_tmpListKeyLeafPath.m_location, parserContext.m_tmpListKeyLeafPath.m_node) == yang::LeafDataTypes::Enum)
+            parserContext.m_suggestions = schema.enumValues(parserContext.m_tmpListKeyLeafPath.m_location, parserContext.m_tmpListKeyLeafPath.m_node);
     }
 };
 
@@ -685,19 +664,13 @@
     void on_success(Iterator const& begin, Iterator const&, T&, Context const& context)
     {
         auto& parserContext = x3::get<parser_context_tag>(context);
-        parserContext.m_completionIterator = begin;
         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;
-
-        leaf_ leaf = boost::get<leaf_>(parserContext.m_curPath.m_nodes.back().m_suffix);
-        schemaPath_ location = pathWithoutLastNode(parserContext.m_curPath);
+        parserContext.m_completionIterator = begin;
 
         // Only generate completions if the type is identityref so that we
         // don't overwrite some other completions.
-        if (schema.leafType(location, {module, leaf.m_name}) == yang::LeafDataTypes::IdentityRef)
-            parserContext.m_suggestions = schema.validIdentities(location, {module, leaf.m_name}, Prefixes::WhenNeeded);
+        if (schema.leafType(parserContext.m_tmpListKeyLeafPath.m_location, parserContext.m_tmpListKeyLeafPath.m_node) == yang::LeafDataTypes::IdentityRef)
+            parserContext.m_suggestions = schema.validIdentities(parserContext.m_tmpListKeyLeafPath.m_location, parserContext.m_tmpListKeyLeafPath.m_node, Prefixes::WhenNeeded);
     }
 };
diff --git a/src/ast_path.cpp b/src/ast_path.cpp
index bdb3447..e3e45d4 100644
--- a/src/ast_path.cpp
+++ b/src/ast_path.cpp
@@ -72,7 +72,7 @@
     return this->m_name == b.m_name;
 }
 
-listElement_::listElement_(const std::string& listName, const std::map<std::string, std::string>& keys)
+listElement_::listElement_(const std::string& listName, const std::map<std::string, leaf_data_>& keys)
     : m_name(listName)
     , m_keys(keys)
 {
@@ -138,7 +138,7 @@
         res << node.m_name + "[";
         std::transform(node.m_keys.begin(), node.m_keys.end(),
                 std::experimental::make_ostream_joiner(res, "]["),
-                [] (const auto& it) { return it.first + "=" + escapeListKeyString(it.second); });
+                [] (const auto& it) { return it.first + "=" + escapeListKeyString(leafDataToString(it.second)); });
         res << "]";
         return res.str();
     }
diff --git a/src/ast_path.hpp b/src/ast_path.hpp
index 338c766..966f771 100644
--- a/src/ast_path.hpp
+++ b/src/ast_path.hpp
@@ -48,12 +48,12 @@
 
 struct listElement_ {
     listElement_() {}
-    listElement_(const std::string& listName, const std::map<std::string, std::string>& keys);
+    listElement_(const std::string& listName, const std::map<std::string, leaf_data_>& keys);
 
     bool operator==(const listElement_& b) const;
 
     std::string m_name;
-    std::map<std::string, std::string> m_keys;
+    std::map<std::string, leaf_data_> m_keys;
 };
 
 struct list_ {
diff --git a/src/grammars.hpp b/src/grammars.hpp
index 9f50634..98ed8b7 100644
--- a/src/grammars.hpp
+++ b/src/grammars.hpp
@@ -107,13 +107,6 @@
         ((alpha | char_("_")) >> *(alnum | char_("_") | char_("-") | char_(".")))
     ];
 
-auto const quotedValue =
-    ('\'' > +(char_-'\'') > '\'') |
-    ('\"' > +(char_-'\"') > '\"');
-
-auto const number =
-    +x3::digit;
-
 auto const createKeySuggestions_def =
     x3::eps;
 
@@ -121,7 +114,7 @@
     x3::eps;
 
 auto const keyValue_def =
-    key_identifier > '=' > (quotedValue | number);
+    key_identifier > '=' > leaf_data;
 
 auto const keyValueWrapper =
     lexeme['[' > createKeySuggestions > keyValue > suggestKeysEnd > ']'];
@@ -254,7 +247,8 @@
 auto const leaf_data_uint64_def =
     uint64;
 auto const leaf_data_string_def =
-    *char_;
+    '\'' >> *(char_-'\'') >> '\'' |
+    '\"' >> *(char_-'\"') >> '\"';
 
 // This intermediate rule is neccessary for coercing to std::string.
 auto const leaf_data_binary_data_def =
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..10a6634 100644
--- a/src/parser_context.hpp
+++ b/src/parser_context.hpp
@@ -8,15 +8,26 @@
 
 #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;
     bool m_topLevelModulePresent = false;
     std::set<std::string> m_tmpListKeys;
+
+    struct {
+        schemaPath_ m_location;
+        ModuleNodePair m_node;
+    } m_tmpListKeyLeafPath;
     bool m_errorHandled = false;
     bool m_completing = false;
     std::set<std::string> m_suggestions;
@@ -26,4 +37,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;
 };
diff --git a/src/utils.cpp b/src/utils.cpp
index cd0858f..d0e8639 100644
--- a/src/utils.cpp
+++ b/src/utils.cpp
@@ -67,6 +67,8 @@
         return "an identity";
     case yang::LeafDataTypes::LeafRef:
         return "a leafref";
+    case yang::LeafDataTypes::Binary:
+        return "a base64-encoded binary value";
     default:
         throw std::runtime_error("leafDataTypeToString: unsupported leaf data type");
     }
diff --git a/tests/cd.cpp b/tests/cd.cpp
index 1d03e78..8b569b7 100644
--- a/tests/cd.cpp
+++ b/tests/cd.cpp
@@ -24,8 +24,11 @@
     schema->addContainer("/example:a/example:a2", "example:a3");
     schema->addContainer("/example:b/example:b2", "example:b3");
     schema->addList("/", "example:list", {"number"});
+    schema->addLeaf("/example:list", "example:number", yang::LeafDataTypes::Int32);
     schema->addContainer("/example:list", "example:contInList");
     schema->addList("/", "example:twoKeyList", {"number", "name"});
+    schema->addLeaf("/example:twoKeyList", "example:number", yang::LeafDataTypes::Int32);
+    schema->addLeaf("/example:twoKeyList", "example:name", yang::LeafDataTypes::String);
     Parser parser(schema);
     std::string input;
     std::ostringstream errorStream;
@@ -98,16 +101,16 @@
             SECTION("example:list[number=1]")
             {
                 input = "cd example:list[number=1]";
-                auto keys = std::map<std::string, std::string>{
-                    {"number", "1"}};
+                auto keys = std::map<std::string, leaf_data_>{
+                    {"number", int32_t{1}}};
                 expected.m_path.m_nodes.push_back(dataNode_(module_{"example"}, listElement_("list", keys)));
             }
 
             SECTION("example:list[number=1]/contInList")
             {
                 input = "cd example:list[number=1]/contInList";
-                auto keys = std::map<std::string, std::string>{
-                    {"number", "1"}};
+                auto keys = std::map<std::string, leaf_data_>{
+                    {"number", int32_t{1}}};
                 expected.m_path.m_nodes.push_back(dataNode_(module_{"example"}, listElement_("list", keys)));
                 expected.m_path.m_nodes.push_back(dataNode_(container_("contInList")));
             }
@@ -115,9 +118,9 @@
             SECTION("example:twoKeyList[number=4][name='abcd']")
             {
                 input = "cd example:twoKeyList[number=4][name='abcd']";
-                auto keys = std::map<std::string, std::string>{
-                    {"number", "4"},
-                    {"name", "abcd"}};
+                auto keys = std::map<std::string, leaf_data_>{
+                    {"number", int32_t{4}},
+                    {"name", std::string{"abcd"}}};
                 expected.m_path.m_nodes.push_back(dataNode_(module_{"example"}, listElement_("twoKeyList", keys)));
             }
         }
diff --git a/tests/enum_completion.cpp b/tests/enum_completion.cpp
index a8ec4d2..d7dfd58 100644
--- a/tests/enum_completion.cpp
+++ b/tests/enum_completion.cpp
@@ -19,6 +19,7 @@
     schema->addLeafEnum("/", "mod:leafEnum", {"lala", "lol", "data", "coze"});
     schema->addLeafEnum("/mod:contA", "mod:leafInCont", {"abc", "def"});
     schema->addList("/", "mod:list", {"number"});
+    schema->addLeaf("/mod:list", "mod:number", yang::LeafDataTypes::Int32);
     schema->addLeafEnum("/mod:list", "mod:leafInList", {"ano", "anoda", "ne", "katoda"});
     Parser parser(schema);
     std::string input;
diff --git a/tests/leaf_editing.cpp b/tests/leaf_editing.cpp
index edf89b4..61704f6 100644
--- a/tests/leaf_editing.cpp
+++ b/tests/leaf_editing.cpp
@@ -47,6 +47,7 @@
     schema->addLeafEnum("/", "mod:leafEnum", {"lol", "data", "coze"});
     schema->addLeaf("/mod:contA", "mod:leafInCont", yang::LeafDataTypes::String);
     schema->addList("/", "mod:list", {"number"});
+    schema->addLeaf("/mod:list", "mod:number", yang::LeafDataTypes::Int32);
     schema->addLeaf("/mod:list", "mod:leafInList", yang::LeafDataTypes::String);
     schema->addLeafRef("/", "mod:refToString", "/mod:leafString");
     schema->addLeafRef("/", "mod:refToInt8", "/mod:leafInt8");
@@ -58,34 +59,50 @@
     {
         set_ expected;
 
-        SECTION("set mod:leafString some_data")
+        SECTION("set mod:leafString \"some_data\"")
         {
-            input = "set mod:leafString some_data";
+            input = "set mod:leafString \'some_data\'";
             expected.m_path.m_nodes.push_back(dataNode_{module_{"mod"}, leaf_("leafString")});
             expected.m_data = std::string("some_data");
         }
 
-        SECTION("set mod:contA/leafInCont more_data")
+        SECTION("set mod:contA/leafInCont 'more_data'")
         {
-            input = "set mod:contA/leafInCont more_data";
+            input = "set mod:contA/leafInCont 'more_data'";
             expected.m_path.m_nodes.push_back(dataNode_{module_{"mod"}, container_("contA")});
             expected.m_path.m_nodes.push_back(dataNode_{leaf_("leafInCont")});
             expected.m_data = std::string("more_data");
         }
 
-        SECTION("set mod:contA/leafInCont   more   d\tata") // spaces in string
+        SECTION("set mod:contA/leafInCont \"data with' a quote\"")
         {
-            input = "set mod:contA/leafInCont more   d\tata";
+            input = "set mod:contA/leafInCont \"data with' a quote\"";
+            expected.m_path.m_nodes.push_back(dataNode_{module_{"mod"}, container_("contA")});
+            expected.m_path.m_nodes.push_back(dataNode_{leaf_("leafInCont")});
+            expected.m_data = std::string("data with' a quote");
+        }
+
+        SECTION("set mod:contA/leafInCont 'data with\" a quote'")
+        {
+            input = "set mod:contA/leafInCont 'data with\" a quote'";
+            expected.m_path.m_nodes.push_back(dataNode_{module_{"mod"}, container_("contA")});
+            expected.m_path.m_nodes.push_back(dataNode_{leaf_("leafInCont")});
+            expected.m_data = std::string("data with\" a quote");
+        }
+
+        SECTION("set mod:contA/leafInCont   'more   d\tata'") // spaces in string
+        {
+            input = "set mod:contA/leafInCont 'more   d\tata'";
             expected.m_path.m_nodes.push_back(dataNode_{module_{"mod"}, container_("contA")});
             expected.m_path.m_nodes.push_back(dataNode_{leaf_("leafInCont")});
             expected.m_data = std::string("more   d\tata");
         }
 
-        SECTION("set mod:list[number=1]/leafInList another_data")
+        SECTION("set mod:list[number=1]/leafInList \"another_data\"")
         {
-            input = "set mod:list[number=1]/leafInList another_data";
-            auto keys = std::map<std::string, std::string>{
-                {"number", "1"}};
+            input = "set mod:list[number=1]/leafInList \"another_data\"";
+            auto keys = std::map<std::string, leaf_data_>{
+                {"number", int32_t{1}}};
             expected.m_path.m_nodes.push_back(dataNode_{module_{"mod"}, listElement_("list", keys)});
             expected.m_path.m_nodes.push_back(dataNode_{leaf_("leafInList")});
             expected.m_data = std::string("another_data");
@@ -95,7 +112,7 @@
         {
             SECTION("string")
             {
-                input = "set mod:leafString somedata";
+                input = "set mod:leafString \"somedata\"";
                 expected.m_path.m_nodes.push_back(dataNode_{module_{"mod"}, leaf_("leafString")});
                 expected.m_data = std::string("somedata");
             }
@@ -285,7 +302,7 @@
             {
                 SECTION("refToString")
                 {
-                    input = "set mod:refToString blabal";
+                    input = "set mod:refToString \"blabal\"";
                     expected.m_path.m_nodes.push_back(dataNode_{module_{"mod"}, leaf_("refToString")});
                     expected.m_data = std::string("blabal");
                 }
@@ -306,97 +323,101 @@
 
     SECTION("invalid input")
     {
+        std::string expectedError;
         SECTION("missing space between a command and its arguments")
         {
-            SECTION("setleaf some_data")
+            SECTION("setmod:leafString some_data")
             {
-                input = "setleaf some_data";
+                input = "setmod:leafString 'some_data'";
             }
         }
 
         SECTION("missing space between arguments")
         {
-            SECTION("set leaflol")
+            SECTION("set mod:leafString'lol'")
             {
-                input = "set leaflol";
+                input = "set mod:leafString'lol'";
             }
         }
 
         SECTION("non-leaf identifiers")
         {
-            SECTION("set nonexistent blabla")
+            SECTION("set mod:nonexistent 'blabla'")
             {
-                input = "set nonexistent blabla";
+                input = "set mod:nonexistent 'blabla'";
             }
 
-            SECTION("set contA abde")
+            SECTION("set mod:contA 'abde'")
             {
-                input = "set contA abde";
+                input = "set mod:contA 'abde'";
             }
         }
 
         SECTION("wrong types")
         {
-            SECTION("set leafBool blabla")
+            expectedError = "leaf data type mismatch";
+            SECTION("set mod:leafBool 'blabla'")
             {
-                input = "set leafBool blabla";
+                input = "set mod:leafBool 'blabla'";
             }
-            SECTION("set leafUint8 blabla")
+            SECTION("set mod:leafUint8 'blabla'")
             {
-                input = "set leafUint8 blabla";
+                input = "set mod:leafUint8 'blabla'";
             }
-            SECTION("set leafUint8 -5")
+            SECTION("set mod:leafUint8 -5")
             {
-                input = "set leafUint8 -5";
+                input = "set mod:leafUint8 -5";
             }
-            SECTION("set leafInt8 blabla")
+            SECTION("set mod:leafInt8 'blabla'")
             {
-                input = "set leafInt8 blabla";
+                input = "set mod:leafInt8 'blabla'";
             }
-            SECTION("set leafInt8 130")
+            SECTION("set mod:leafInt8 130")
             {
-                input = "set leafInt8 130";
+                input = "set mod:leafInt8 130";
             }
-            SECTION("set leafUint16 blabla")
+            SECTION("set mod:leafUint16 'blabla'")
             {
-                input = "set leafUint16 blabla";
+                input = "set mod:leafUint16 'blabla'";
             }
-            SECTION("set leafInt16 blabla")
+            SECTION("set mod:leafInt16 'blabla'")
             {
-                input = "set leafInt16 blabla";
+                input = "set mod:leafInt16 'blabla'";
             }
-            SECTION("set leafUint32 blabla")
+            SECTION("set mod:leafUint32 'blabla'")
             {
-                input = "set leafUint32 blabla";
+                input = "set mod:leafUint32 'blabla'";
             }
-            SECTION("set leafInt32 blabla")
+            SECTION("set mod:leafInt32 'blabla'")
             {
-                input = "set leafInt32 blabla";
+                input = "set mod:leafInt32 'blabla'";
             }
-            SECTION("set leafUint64 blabla")
+            SECTION("set mod:leafUint64 'blabla'")
             {
-                input = "set leafUint64 blabla";
+                input = "set mod:leafUint64 'blabla'";
             }
-            SECTION("set leafInt64 blabla")
+            SECTION("set mod:leafInt64 'blabla'")
             {
-                input = "set leafInt64 blabla";
+                input = "set mod:leafInt64 'blabla'";
             }
-            SECTION("set leafEnum blabla")
+            SECTION("set mod:leafEnum 'blabla'")
             {
-                input = "set leafEnum blabla";
+                input = "set mod:leafEnum 'blabla'";
             }
-            SECTION("set mod:refToInt8 blabla")
+            SECTION("set mod:refToInt8 'blabla'")
             {
-                input = "set mod:refToInt8 blabla";
+                input = "set mod:refToInt8 'blabla'";
             }
         }
 
         SECTION("wrong base64 strings")
         {
             SECTION("invalid character")
-                input = "set leafBinary dbahj-";
+                input = "set mod:leafBinary dbahj-";
             SECTION("equal sign in the middle")
-                input = "set leafBinary db=ahj";
+                input = "set mod:leafBinary db=ahj";
+            SECTION("enclosing in quotes")
+                input = "set mod:leafBinary 'dbahj'";
         }
 
         SECTION("non-existing identity")
@@ -425,5 +446,6 @@
         }
 
         REQUIRE_THROWS_AS(parser.parseCommand(input, errorStream), InvalidCommandException);
+        REQUIRE(errorStream.str().find(expectedError) != std::string::npos);
     }
 }
diff --git a/tests/list_manipulation.cpp b/tests/list_manipulation.cpp
index 3fa7a8b..5100c61 100644
--- a/tests/list_manipulation.cpp
+++ b/tests/list_manipulation.cpp
@@ -14,6 +14,7 @@
     auto schema = std::make_shared<StaticSchema>();
     schema->addModule("mod");
     schema->addList("/", "mod:list", {"number"});
+    schema->addLeaf("/mod:list", "mod:number", yang::LeafDataTypes::Int32);
     schema->addLeaf("/mod:list", "mod:leafInList", yang::LeafDataTypes::String);
     Parser parser(schema);
     std::string input;
@@ -25,8 +26,8 @@
         SECTION("mod:list[number=3]")
         {
             input = "mod:list[number=3]";
-            auto keys = std::map<std::string, std::string>{
-                {"number", "3"}};
+            auto keys = std::map<std::string, leaf_data_>{
+                {"number", int32_t{3}}};
             expectedPath.m_nodes.push_back(dataNode_{module_{"mod"}, listElement_("list", keys)});
         }
 
diff --git a/tests/ls.cpp b/tests/ls.cpp
index 0df428b..8709f13 100644
--- a/tests/ls.cpp
+++ b/tests/ls.cpp
@@ -25,8 +25,11 @@
     schema->addContainer("/example:a/example:a2", "example:a3");
     schema->addContainer("/example:b/example:b2", "example:b3");
     schema->addList("/", "example:list", {"number"});
+    schema->addLeaf("/example:list", "example:number", yang::LeafDataTypes::Int32);
     schema->addContainer("/example:list", "example:contInList");
     schema->addList("/", "example:twoKeyList", {"number", "name"});
+    schema->addLeaf("/example:twoKeyList", "example:number", yang::LeafDataTypes::Int32);
+    schema->addLeaf("/example:twoKeyList", "example:name", yang::LeafDataTypes::String);
     Parser parser(schema);
     std::string input;
     std::ostringstream errorStream;
@@ -153,8 +156,8 @@
             SECTION("ls example:list[number=342]/contInList")
             {
                 input = "ls example:list[number=342]/contInList";
-                auto keys = std::map<std::string, std::string>{
-                    {"number", "342"}};
+                auto keys = std::map<std::string, leaf_data_>{
+                    {"number", int32_t{342}}};
                 expected.m_path = dataPath_{Scope::Relative, {dataNode_(module_{"example"}, listElement_{"list", keys}),
                                                                 dataNode_(container_{"contInList"})}};
             }
diff --git a/tests/path_completion.cpp b/tests/path_completion.cpp
index 115cfdd..77dae51 100644
--- a/tests/path_completion.cpp
+++ b/tests/path_completion.cpp
@@ -36,10 +36,15 @@
     schema->addContainer("/example:ano/example:a2", "example:a3");
     schema->addContainer("/example:bota/example:b2", "example:b3");
     schema->addList("/", "example:list", {"number"});
-    schema->addList("/", "example:ovoce", {"name"});
-    schema->addList("/", "example:ovocezelenina", {"name"});
+    schema->addLeaf("/example:list", "example:number", yang::LeafDataTypes::Int32);
     schema->addContainer("/example:list", "example:contInList");
+    schema->addList("/", "example:ovoce", {"name"});
+    schema->addLeaf("/example:ovoce", "example:name", yang::LeafDataTypes::String);
+    schema->addList("/", "example:ovocezelenina", {"name"});
+    schema->addLeaf("/example:ovocezelenina", "example:name", yang::LeafDataTypes::String);
     schema->addList("/", "example:twoKeyList", {"number", "name"});
+    schema->addLeaf("/example:twoKeyList", "example:name", yang::LeafDataTypes::String);
+    schema->addLeaf("/example:twoKeyList", "example:number", yang::LeafDataTypes::Int32);
     schema->addLeaf("/", "example:leafInt", yang::LeafDataTypes::Int32);
     Parser parser(schema);
     std::string input;
@@ -111,13 +116,13 @@
         SECTION("ls /example:list[number=3]/")
         {
             input = "ls /example:list[number=3]/";
-            expected = {"example:contInList/"};
+            expected = {"example:contInList/", "example:number "};
         }
 
         SECTION("ls /example:list[number=3]/c")
         {
             input = "ls /example:list[number=3]/e";
-            expected = {"example:contInList/"};
+            expected = {"example:contInList/", "example:number "};
         }
 
         SECTION("ls /example:list[number=3]/a")
diff --git a/tests/path_utils.cpp b/tests/path_utils.cpp
index f8dc250..c23984d 100644
--- a/tests/path_utils.cpp
+++ b/tests/path_utils.cpp
@@ -25,7 +25,7 @@
             {
                 path.m_scope = Scope::Relative;
             }
-            path.m_nodes.push_back(dataNode_{module_{"example-schema"}, listElement_{"twoKeyList", {{"first", "a"}, {"second", "b"}}}});
+            path.m_nodes.push_back(dataNode_{module_{"example-schema"}, listElement_{"twoKeyList", {{"first", std::string{"a"}}, {"second", std::string{"b"}}}}});
             expected += "example-schema:twoKeyList[first='a'][second='b']";
         }
         REQUIRE(pathToDataString(path, Prefixes::WhenNeeded) == expected);
diff --git a/tests/presence_containers.cpp b/tests/presence_containers.cpp
index ba8938e..d634daf 100644
--- a/tests/presence_containers.cpp
+++ b/tests/presence_containers.cpp
@@ -22,6 +22,7 @@
     schema->addContainer("/mod:a/mod:a2", "mod:a3", yang::ContainerTraits::Presence);
     schema->addContainer("/mod:b", "mod:b2", yang::ContainerTraits::Presence);
     schema->addList("/", "mod:list", {"quote"});
+    schema->addLeaf("/mod:list", "mod:quote", yang::LeafDataTypes::String);
     schema->addContainer("/mod:list", "mod:contInList", yang::ContainerTraits::Presence);
     Parser parser(schema);
     std::string input;
@@ -52,24 +53,24 @@
         SECTION("mod:list[quote='lol']/contInList")
         {
             input = "mod:list[quote='lol']/contInList";
-            auto keys = std::map<std::string, std::string>{
-                {"quote", "lol"}};
+            auto keys = std::map<std::string, leaf_data_>{
+                {"quote", std::string{"lol"}}};
             expectedPath.m_nodes = {{{module_{"mod"}}, listElement_("list", keys)}, {container_("contInList")}};
         }
 
         SECTION("mod:list[quote='double\"quote']/contInList")
         {
             input = "mod:list[quote='double\"quote']/contInList";
-            auto keys = std::map<std::string, std::string>{
-                {"quote", "double\"quote"}};
+            auto keys = std::map<std::string, leaf_data_>{
+                {"quote", std::string{"double\"quote"}}};
             expectedPath.m_nodes = {{{module_{"mod"}}, listElement_("list", keys)}, {container_("contInList")}};
         }
 
         SECTION("mod:list[quote=\"single'quote\"]/contInList")
         {
             input = "mod:list[quote=\"single'quote\"]/contInList";
-            auto keys = std::map<std::string, std::string>{
-                {"quote", "single'quote"}};
+            auto keys = std::map<std::string, leaf_data_>{
+                {"quote", std::string{"single'quote"}}};
             expectedPath.m_nodes = {{{module_{"mod"}}, listElement_("list", keys)}, {container_("contInList")}};
         }