Parse key values as leaf_data

Change-Id: Ib70a47dd4bcea0426d3b2063a845d4ce075d81f4
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 1fb7242..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.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 {
             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;
 
@@ -348,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;
@@ -383,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.currentSchemaPath().m_nodes.back().m_suffix);
-            schemaPath_ location = pathWithoutLastNode(parserContext.currentSchemaPath());
-            boost::optional<std::string> module;
-            if (parserContext.currentSchemaPath().m_nodes.back().m_prefix)
-                module = parserContext.currentSchemaPath().m_nodes.back().m_prefix.value().m_name;
-            parserContext.m_errorMsg = "leaf data type mismatch: 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;
@@ -409,17 +429,12 @@
     {
         auto& parserContext = x3::get<parser_context_tag>(context);
         auto& schema = parserContext.m_schema;
-        boost::optional<std::string> module;
-        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.currentSchemaPath().m_nodes.back().m_suffix);
-        schemaPath_ location = pathWithoutLastNode(parserContext.currentSchemaPath());
-
-        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;
         }
@@ -440,18 +455,11 @@
             return;
         auto& parserContext = x3::get<parser_context_tag>(context);
         auto& schema = parserContext.m_schema;
-        boost::optional<std::string> module;
-        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.currentSchemaPath().m_nodes.back().m_suffix);
-        schemaPath_ location = pathWithoutLastNode(parserContext.currentSchemaPath());
-
-        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 = "leaf data type mismatch: Expected an enum here. Allowed values:";
-            for (const auto& it : schema.enumValues(location, {module, leaf.m_name})) {
+            for (const auto& it : schema.enumValues(parserContext.m_tmpListKeyLeafPath.m_location, parserContext.m_tmpListKeyLeafPath.m_node)) {
                 parserContext.m_errorMsg += " " + it;
             }
         }
@@ -473,12 +481,6 @@
             return;
         auto& parserContext = x3::get<parser_context_tag>(context);
         auto& schema = parserContext.m_schema;
-        boost::optional<std::string> module;
-        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.currentSchemaPath().m_nodes.back().m_suffix);
-        schemaPath_ location = pathWithoutLastNode(parserContext.currentSchemaPath());
 
         ModuleValuePair pair;
         if (ast.m_prefix) {
@@ -486,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;
         }
     }
@@ -645,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.currentSchemaPath().m_nodes.back().m_prefix)
-            module = parserContext.currentSchemaPath().m_nodes.back().m_prefix.value().m_name;
-
-        leaf_ leaf = boost::get<leaf_>(parserContext.currentSchemaPath().m_nodes.back().m_suffix);
-        schemaPath_ location = pathWithoutLastNode(parserContext.currentSchemaPath());
+        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);
     }
 };
 
@@ -668,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.currentSchemaPath().m_nodes.back().m_prefix)
-            module = parserContext.currentSchemaPath().m_nodes.back().m_prefix.value().m_name;
-
-        leaf_ leaf = boost::get<leaf_>(parserContext.currentSchemaPath().m_nodes.back().m_suffix);
-        schemaPath_ location = pathWithoutLastNode(parserContext.currentSchemaPath());
+        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 2574019..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 > ']'];
diff --git a/src/parser_context.hpp b/src/parser_context.hpp
index b20c6ab..10a6634 100644
--- a/src/parser_context.hpp
+++ b/src/parser_context.hpp
@@ -23,6 +23,11 @@
     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;