Václav Kubernát | d0ea9b2 | 2020-04-24 00:44:15 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2020 CESNET, https://photonics.cesnet.cz/ |
| 3 | * |
| 4 | * Written by Václav Kubernát <kubernat@cesnet.cz> |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #pragma once |
| 9 | |
| 10 | #include <boost/spirit/home/x3.hpp> |
| 11 | #include "ast_handlers.hpp" |
| 12 | #include "common_parsers.hpp" |
| 13 | #include "leaf_data.hpp" |
| 14 | |
| 15 | namespace x3 = boost::spirit::x3; |
| 16 | |
| 17 | x3::rule<dataPath_class, dataPath_> const dataPath = "dataPath"; |
| 18 | x3::rule<schemaPath_class, schemaPath_> const schemaPath = "schemaPath"; |
| 19 | x3::rule<dataNodeList_class, decltype(dataPath_::m_nodes)::value_type> const dataNodeList = "dataNodeList"; |
| 20 | x3::rule<dataNodesListEnd_class, decltype(dataPath_::m_nodes)> const dataNodesListEnd = "dataNodesListEnd"; |
| 21 | x3::rule<dataPathListEnd_class, dataPath_> const dataPathListEnd = "dataPathListEnd"; |
| 22 | x3::rule<leaf_path_class, dataPath_> const leafPath = "leafPath"; |
| 23 | x3::rule<presenceContainerPath_class, dataPath_> const presenceContainerPath = "presenceContainerPath"; |
| 24 | x3::rule<listInstancePath_class, dataPath_> const listInstancePath = "listInstancePath"; |
| 25 | x3::rule<initializePath_class, x3::unused_type> const initializePath = "initializePath"; |
| 26 | x3::rule<createPathSuggestions_class, x3::unused_type> const createPathSuggestions = "createPathSuggestions"; |
| 27 | x3::rule<trailingSlash_class, TrailingSlash> const trailingSlash = "trailingSlash"; |
Václav Kubernát | d0ea9b2 | 2020-04-24 00:44:15 +0200 | [diff] [blame] | 28 | x3::rule<absoluteStart_class, Scope> const absoluteStart = "absoluteStart"; |
| 29 | x3::rule<keyValue_class, keyValue_> const keyValue = "keyValue"; |
| 30 | x3::rule<key_identifier_class, std::string> const key_identifier = "key_identifier"; |
Václav Kubernát | d0ea9b2 | 2020-04-24 00:44:15 +0200 | [diff] [blame] | 31 | x3::rule<listSuffix_class, std::vector<keyValue_>> const listSuffix = "listSuffix"; |
Václav Kubernát | d0ea9b2 | 2020-04-24 00:44:15 +0200 | [diff] [blame] | 32 | x3::rule<list_class, list_> const list = "list"; |
Václav Kubernát | d0ea9b2 | 2020-04-24 00:44:15 +0200 | [diff] [blame] | 33 | x3::rule<createKeySuggestions_class, x3::unused_type> const createKeySuggestions = "createKeySuggestions"; |
| 34 | x3::rule<createValueSuggestions_class, x3::unused_type> const createValueSuggestions = "createValueSuggestions"; |
| 35 | x3::rule<suggestKeysEnd_class, x3::unused_type> const suggestKeysEnd = "suggestKeysEnd"; |
| 36 | |
Václav Kubernát | dd5945a | 2020-05-05 01:24:23 +0200 | [diff] [blame^] | 37 | template <typename NodeType> |
| 38 | struct NodeParser : x3::parser<NodeParser<NodeType>> { |
| 39 | using attribute_type = NodeType; |
Václav Kubernát | 60a0ed5 | 2020-04-28 15:21:33 +0200 | [diff] [blame] | 40 | template <typename It, typename Ctx, typename RCtx, typename Attr> |
| 41 | bool parse(It& begin, It end, Ctx const& ctx, RCtx& rctx, Attr& attr) const |
| 42 | { |
Václav Kubernát | dd5945a | 2020-05-05 01:24:23 +0200 | [diff] [blame^] | 43 | std::string tableName; |
| 44 | if constexpr (std::is_same<NodeType, schemaNode_>()) { |
| 45 | tableName = "schemaNode"; |
| 46 | } else { |
| 47 | tableName = "dataNode"; |
| 48 | } |
| 49 | x3::symbols<NodeType> table(tableName); |
Václav Kubernát | 60a0ed5 | 2020-04-28 15:21:33 +0200 | [diff] [blame] | 50 | |
| 51 | ParserContext& parserContext = x3::get<parser_context_tag>(ctx); |
| 52 | parserContext.m_suggestions.clear(); |
| 53 | for (const auto& child : parserContext.m_schema.availableNodes(parserContext.currentSchemaPath(), Recursion::NonRecursive)) { |
Václav Kubernát | dd5945a | 2020-05-05 01:24:23 +0200 | [diff] [blame^] | 54 | NodeType out; |
Václav Kubernát | 60a0ed5 | 2020-04-28 15:21:33 +0200 | [diff] [blame] | 55 | std::string parseString; |
| 56 | if (child.first) { |
| 57 | out.m_prefix = module_{*child.first}; |
| 58 | parseString = *child.first + ":"; |
| 59 | } |
| 60 | parseString += child.second; |
| 61 | switch (parserContext.m_schema.nodeType(parserContext.currentSchemaPath(), child)) { |
| 62 | case yang::NodeTypes::Container: |
| 63 | case yang::NodeTypes::PresenceContainer: |
| 64 | out.m_suffix = container_{child.second}; |
| 65 | parserContext.m_suggestions.emplace(Completion{parseString + "/"}); |
| 66 | break; |
| 67 | case yang::NodeTypes::Leaf: |
| 68 | out.m_suffix = leaf_{child.second}; |
| 69 | parserContext.m_suggestions.emplace(Completion{parseString + " "}); |
| 70 | break; |
| 71 | case yang::NodeTypes::List: |
Václav Kubernát | dd5945a | 2020-05-05 01:24:23 +0200 | [diff] [blame^] | 72 | if constexpr (std::is_same<NodeType, schemaNode_>()) { |
| 73 | out.m_suffix = list_{child.second}; |
| 74 | } else { |
| 75 | out.m_suffix = listElement_{child.second, {}}; |
| 76 | } |
Václav Kubernát | 60a0ed5 | 2020-04-28 15:21:33 +0200 | [diff] [blame] | 77 | parserContext.m_suggestions.emplace(Completion{parseString, "[", Completion::WhenToAdd::IfFullMatch}); |
| 78 | break; |
| 79 | case yang::NodeTypes::Action: |
| 80 | case yang::NodeTypes::AnyXml: |
| 81 | case yang::NodeTypes::LeafList: |
| 82 | case yang::NodeTypes::Notification: |
| 83 | case yang::NodeTypes::Rpc: |
| 84 | continue; |
| 85 | } |
| 86 | table.add(parseString, out); |
Václav Kubernát | dd5945a | 2020-05-05 01:24:23 +0200 | [diff] [blame^] | 87 | table.add("..", NodeType{nodeup_{}}); |
Václav Kubernát | 60a0ed5 | 2020-04-28 15:21:33 +0200 | [diff] [blame] | 88 | if (!child.first) { |
| 89 | auto topLevelModule = parserContext.currentSchemaPath().m_nodes.begin()->m_prefix; |
| 90 | out.m_prefix = topLevelModule; |
| 91 | table.add(topLevelModule->m_name + ":" + parseString, out); |
| 92 | } |
| 93 | } |
Václav Kubernát | dd5945a | 2020-05-05 01:24:23 +0200 | [diff] [blame^] | 94 | parserContext.m_completionIterator = begin; |
Václav Kubernát | 60a0ed5 | 2020-04-28 15:21:33 +0200 | [diff] [blame] | 95 | auto res = table.parse(begin, end, ctx, rctx, attr); |
Václav Kubernát | dd5945a | 2020-05-05 01:24:23 +0200 | [diff] [blame^] | 96 | |
| 97 | if (attr.m_prefix) { |
| 98 | parserContext.m_curModule = attr.m_prefix->m_name; |
| 99 | } |
| 100 | |
| 101 | if (attr.m_suffix.type() == typeid(leaf_)) { |
| 102 | parserContext.m_tmpListKeyLeafPath.m_location = parserContext.currentSchemaPath(); |
| 103 | ModuleNodePair node{attr.m_prefix.flat_map([](const auto& it) { |
| 104 | return boost::optional<std::string>{it.m_name}; |
| 105 | }), boost::get<leaf_>(attr.m_suffix).m_name}; |
| 106 | parserContext.m_tmpListKeyLeafPath.m_node = node; |
| 107 | } |
| 108 | |
| 109 | if constexpr (std::is_same<NodeType, dataNode_>()) { |
| 110 | if (attr.m_suffix.type() == typeid(listElement_)) { |
| 111 | parserContext.m_tmpListName = boost::get<listElement_>(attr.m_suffix).m_name; |
| 112 | res = listSuffix.parse(begin, end, ctx, rctx, boost::get<listElement_>(attr.m_suffix).m_keys); |
| 113 | } |
| 114 | } |
| 115 | |
Václav Kubernát | 60a0ed5 | 2020-04-28 15:21:33 +0200 | [diff] [blame] | 116 | if (res) { |
| 117 | parserContext.pushPathFragment(attr); |
Václav Kubernát | dd5945a | 2020-05-05 01:24:23 +0200 | [diff] [blame^] | 118 | parserContext.m_topLevelModulePresent = true; |
| 119 | } |
| 120 | |
| 121 | if (attr.m_prefix) { |
| 122 | parserContext.m_curModule = boost::none; |
Václav Kubernát | 60a0ed5 | 2020-04-28 15:21:33 +0200 | [diff] [blame] | 123 | } |
| 124 | return res; |
| 125 | } |
Václav Kubernát | dd5945a | 2020-05-05 01:24:23 +0200 | [diff] [blame^] | 126 | }; |
| 127 | |
| 128 | NodeParser<schemaNode_> schemaNode; |
| 129 | NodeParser<dataNode_> dataNode; |
Václav Kubernát | 60a0ed5 | 2020-04-28 15:21:33 +0200 | [diff] [blame] | 130 | |
Václav Kubernát | d0ea9b2 | 2020-04-24 00:44:15 +0200 | [diff] [blame] | 131 | #if __clang__ |
| 132 | #pragma GCC diagnostic push |
| 133 | #pragma GCC diagnostic ignored "-Woverloaded-shift-op-parentheses" |
| 134 | #endif |
| 135 | |
| 136 | auto const rest = |
| 137 | x3::omit[x3::no_skip[+(x3::char_ - '/' - space_separator)]]; |
| 138 | |
| 139 | auto const key_identifier_def = |
| 140 | x3::lexeme[ |
| 141 | ((x3::alpha | char_("_")) >> *(x3::alnum | char_("_") | char_("-") | char_("."))) |
| 142 | ]; |
| 143 | |
| 144 | auto const createKeySuggestions_def = |
| 145 | x3::eps; |
| 146 | |
| 147 | auto const createValueSuggestions_def = |
| 148 | x3::eps; |
| 149 | |
| 150 | auto const suggestKeysEnd_def = |
| 151 | x3::eps; |
| 152 | |
| 153 | auto const keyValue_def = |
| 154 | key_identifier > '=' > createValueSuggestions > leaf_data; |
| 155 | |
| 156 | auto const keyValueWrapper = |
| 157 | x3::lexeme['[' > createKeySuggestions > keyValue > suggestKeysEnd > ']']; |
| 158 | |
Václav Kubernát | d0ea9b2 | 2020-04-24 00:44:15 +0200 | [diff] [blame] | 159 | // even though we don't allow no keys to be supplied, the star allows me to check which keys are missing |
| 160 | auto const listSuffix_def = |
| 161 | *keyValueWrapper; |
| 162 | |
Václav Kubernát | d0ea9b2 | 2020-04-24 00:44:15 +0200 | [diff] [blame] | 163 | auto const list_def = |
| 164 | node_identifier >> !char_('['); |
| 165 | |
Václav Kubernát | d0ea9b2 | 2020-04-24 00:44:15 +0200 | [diff] [blame] | 166 | auto const absoluteStart_def = |
| 167 | x3::omit['/'] >> x3::attr(Scope::Absolute); |
| 168 | |
| 169 | auto const trailingSlash_def = |
| 170 | x3::omit['/'] >> x3::attr(TrailingSlash::Present); |
| 171 | |
| 172 | auto const createPathSuggestions_def = |
| 173 | x3::eps; |
| 174 | |
Václav Kubernát | d0ea9b2 | 2020-04-24 00:44:15 +0200 | [diff] [blame] | 175 | // I have to insert an empty vector to the first alternative, otherwise they won't have the same attribute |
| 176 | auto const dataPath_def = initializePath >> absoluteStart >> createPathSuggestions >> x3::attr(decltype(dataPath_::m_nodes)()) >> x3::attr(TrailingSlash::NonPresent) >> x3::eoi | initializePath >> -(absoluteStart >> createPathSuggestions) >> dataNode % '/' >> (-(trailingSlash >> createPathSuggestions) >> -(completing >> rest) >> (&space_separator | x3::eoi)); |
| 177 | |
| 178 | auto const dataNodeList_def = |
| 179 | createPathSuggestions >> -(module) >> list; |
| 180 | |
| 181 | // This intermediate rule is mandatory, because we need the first alternative |
| 182 | // to be collapsed to a vector. If we didn't use the intermediate rule, |
| 183 | // Spirit wouldn't know we want it to collapse. |
| 184 | // https://github.com/boostorg/spirit/issues/408 |
| 185 | auto const dataNodesListEnd_def = |
| 186 | dataNode % '/' >> '/' >> dataNodeList >> -(&char_('/') >> createPathSuggestions) | |
| 187 | x3::attr(decltype(dataPath_::m_nodes)()) >> dataNodeList; |
| 188 | |
| 189 | auto const dataPathListEnd_def = initializePath >> absoluteStart >> createPathSuggestions >> x3::attr(decltype(dataPath_::m_nodes)()) >> x3::attr(TrailingSlash::NonPresent) >> x3::eoi | initializePath >> -(absoluteStart >> createPathSuggestions) >> dataNodesListEnd >> (-(trailingSlash >> createPathSuggestions) >> -(completing >> rest) >> (&space_separator | x3::eoi)); |
| 190 | |
| 191 | auto const schemaPath_def = initializePath >> absoluteStart >> createPathSuggestions >> x3::attr(decltype(schemaPath_::m_nodes)()) >> x3::attr(TrailingSlash::NonPresent) >> x3::eoi | initializePath >> -(absoluteStart >> createPathSuggestions) >> schemaNode % '/' >> (-(trailingSlash >> createPathSuggestions) >> -(completing >> rest) >> (&space_separator | x3::eoi)); |
| 192 | |
| 193 | auto const leafPath_def = |
| 194 | dataPath; |
| 195 | |
| 196 | auto const presenceContainerPath_def = |
| 197 | dataPath; |
| 198 | |
| 199 | auto const listInstancePath_def = |
| 200 | dataPath; |
| 201 | |
| 202 | // A "nothing" parser, which is used to indicate we tried to parse a path |
| 203 | auto const initializePath_def = |
| 204 | x3::eps; |
| 205 | |
| 206 | |
| 207 | |
| 208 | #if __clang__ |
| 209 | #pragma GCC diagnostic pop |
| 210 | #endif |
| 211 | |
| 212 | BOOST_SPIRIT_DEFINE(keyValue) |
| 213 | BOOST_SPIRIT_DEFINE(key_identifier) |
Václav Kubernát | d0ea9b2 | 2020-04-24 00:44:15 +0200 | [diff] [blame] | 214 | BOOST_SPIRIT_DEFINE(listSuffix) |
Václav Kubernát | d0ea9b2 | 2020-04-24 00:44:15 +0200 | [diff] [blame] | 215 | BOOST_SPIRIT_DEFINE(list) |
Václav Kubernát | d0ea9b2 | 2020-04-24 00:44:15 +0200 | [diff] [blame] | 216 | BOOST_SPIRIT_DEFINE(dataNodeList) |
| 217 | BOOST_SPIRIT_DEFINE(dataNodesListEnd) |
| 218 | BOOST_SPIRIT_DEFINE(leafPath) |
| 219 | BOOST_SPIRIT_DEFINE(presenceContainerPath) |
| 220 | BOOST_SPIRIT_DEFINE(listInstancePath) |
| 221 | BOOST_SPIRIT_DEFINE(schemaPath) |
| 222 | BOOST_SPIRIT_DEFINE(dataPath) |
| 223 | BOOST_SPIRIT_DEFINE(dataPathListEnd) |
| 224 | BOOST_SPIRIT_DEFINE(initializePath) |
| 225 | BOOST_SPIRIT_DEFINE(createKeySuggestions) |
| 226 | BOOST_SPIRIT_DEFINE(createPathSuggestions) |
| 227 | BOOST_SPIRIT_DEFINE(createValueSuggestions) |
| 228 | BOOST_SPIRIT_DEFINE(suggestKeysEnd) |
| 229 | BOOST_SPIRIT_DEFINE(absoluteStart) |
| 230 | BOOST_SPIRIT_DEFINE(trailingSlash) |