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 | |
Václav Kubernát | d0ea9b2 | 2020-04-24 00:44:15 +0200 | [diff] [blame] | 17 | x3::rule<dataNodeList_class, decltype(dataPath_::m_nodes)::value_type> const dataNodeList = "dataNodeList"; |
| 18 | x3::rule<dataNodesListEnd_class, decltype(dataPath_::m_nodes)> const dataNodesListEnd = "dataNodesListEnd"; |
| 19 | x3::rule<dataPathListEnd_class, dataPath_> const dataPathListEnd = "dataPathListEnd"; |
| 20 | x3::rule<leaf_path_class, dataPath_> const leafPath = "leafPath"; |
| 21 | x3::rule<presenceContainerPath_class, dataPath_> const presenceContainerPath = "presenceContainerPath"; |
| 22 | x3::rule<listInstancePath_class, dataPath_> const listInstancePath = "listInstancePath"; |
| 23 | x3::rule<initializePath_class, x3::unused_type> const initializePath = "initializePath"; |
| 24 | x3::rule<createPathSuggestions_class, x3::unused_type> const createPathSuggestions = "createPathSuggestions"; |
| 25 | x3::rule<trailingSlash_class, TrailingSlash> const trailingSlash = "trailingSlash"; |
Václav Kubernát | d0ea9b2 | 2020-04-24 00:44:15 +0200 | [diff] [blame] | 26 | x3::rule<absoluteStart_class, Scope> const absoluteStart = "absoluteStart"; |
| 27 | x3::rule<keyValue_class, keyValue_> const keyValue = "keyValue"; |
| 28 | 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] | 29 | x3::rule<listSuffix_class, std::vector<keyValue_>> const listSuffix = "listSuffix"; |
Václav Kubernát | d0ea9b2 | 2020-04-24 00:44:15 +0200 | [diff] [blame] | 30 | x3::rule<list_class, list_> const list = "list"; |
Václav Kubernát | d0ea9b2 | 2020-04-24 00:44:15 +0200 | [diff] [blame] | 31 | x3::rule<createKeySuggestions_class, x3::unused_type> const createKeySuggestions = "createKeySuggestions"; |
| 32 | x3::rule<createValueSuggestions_class, x3::unused_type> const createValueSuggestions = "createValueSuggestions"; |
| 33 | x3::rule<suggestKeysEnd_class, x3::unused_type> const suggestKeysEnd = "suggestKeysEnd"; |
| 34 | |
Václav Kubernát | dd5945a | 2020-05-05 01:24:23 +0200 | [diff] [blame] | 35 | template <typename NodeType> |
| 36 | struct NodeParser : x3::parser<NodeParser<NodeType>> { |
| 37 | using attribute_type = NodeType; |
Václav Kubernát | 60a0ed5 | 2020-04-28 15:21:33 +0200 | [diff] [blame] | 38 | template <typename It, typename Ctx, typename RCtx, typename Attr> |
| 39 | bool parse(It& begin, It end, Ctx const& ctx, RCtx& rctx, Attr& attr) const |
| 40 | { |
Václav Kubernát | dd5945a | 2020-05-05 01:24:23 +0200 | [diff] [blame] | 41 | std::string tableName; |
| 42 | if constexpr (std::is_same<NodeType, schemaNode_>()) { |
| 43 | tableName = "schemaNode"; |
| 44 | } else { |
| 45 | tableName = "dataNode"; |
| 46 | } |
| 47 | x3::symbols<NodeType> table(tableName); |
Václav Kubernát | 60a0ed5 | 2020-04-28 15:21:33 +0200 | [diff] [blame] | 48 | |
| 49 | ParserContext& parserContext = x3::get<parser_context_tag>(ctx); |
| 50 | parserContext.m_suggestions.clear(); |
| 51 | 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] | 52 | NodeType out; |
Václav Kubernát | 60a0ed5 | 2020-04-28 15:21:33 +0200 | [diff] [blame] | 53 | std::string parseString; |
| 54 | if (child.first) { |
| 55 | out.m_prefix = module_{*child.first}; |
| 56 | parseString = *child.first + ":"; |
| 57 | } |
| 58 | parseString += child.second; |
| 59 | switch (parserContext.m_schema.nodeType(parserContext.currentSchemaPath(), child)) { |
| 60 | case yang::NodeTypes::Container: |
| 61 | case yang::NodeTypes::PresenceContainer: |
| 62 | out.m_suffix = container_{child.second}; |
| 63 | parserContext.m_suggestions.emplace(Completion{parseString + "/"}); |
| 64 | break; |
| 65 | case yang::NodeTypes::Leaf: |
| 66 | out.m_suffix = leaf_{child.second}; |
| 67 | parserContext.m_suggestions.emplace(Completion{parseString + " "}); |
| 68 | break; |
| 69 | case yang::NodeTypes::List: |
Václav Kubernát | dd5945a | 2020-05-05 01:24:23 +0200 | [diff] [blame] | 70 | if constexpr (std::is_same<NodeType, schemaNode_>()) { |
| 71 | out.m_suffix = list_{child.second}; |
| 72 | } else { |
| 73 | out.m_suffix = listElement_{child.second, {}}; |
| 74 | } |
Václav Kubernát | 60a0ed5 | 2020-04-28 15:21:33 +0200 | [diff] [blame] | 75 | parserContext.m_suggestions.emplace(Completion{parseString, "[", Completion::WhenToAdd::IfFullMatch}); |
| 76 | break; |
| 77 | case yang::NodeTypes::Action: |
| 78 | case yang::NodeTypes::AnyXml: |
| 79 | case yang::NodeTypes::LeafList: |
| 80 | case yang::NodeTypes::Notification: |
| 81 | case yang::NodeTypes::Rpc: |
| 82 | continue; |
| 83 | } |
| 84 | table.add(parseString, out); |
Václav Kubernát | dd5945a | 2020-05-05 01:24:23 +0200 | [diff] [blame] | 85 | table.add("..", NodeType{nodeup_{}}); |
Václav Kubernát | 60a0ed5 | 2020-04-28 15:21:33 +0200 | [diff] [blame] | 86 | if (!child.first) { |
| 87 | auto topLevelModule = parserContext.currentSchemaPath().m_nodes.begin()->m_prefix; |
| 88 | out.m_prefix = topLevelModule; |
| 89 | table.add(topLevelModule->m_name + ":" + parseString, out); |
| 90 | } |
| 91 | } |
Václav Kubernát | dd5945a | 2020-05-05 01:24:23 +0200 | [diff] [blame] | 92 | parserContext.m_completionIterator = begin; |
Václav Kubernát | 60a0ed5 | 2020-04-28 15:21:33 +0200 | [diff] [blame] | 93 | auto res = table.parse(begin, end, ctx, rctx, attr); |
Václav Kubernát | dd5945a | 2020-05-05 01:24:23 +0200 | [diff] [blame] | 94 | |
| 95 | if (attr.m_prefix) { |
| 96 | parserContext.m_curModule = attr.m_prefix->m_name; |
| 97 | } |
| 98 | |
| 99 | if (attr.m_suffix.type() == typeid(leaf_)) { |
| 100 | parserContext.m_tmpListKeyLeafPath.m_location = parserContext.currentSchemaPath(); |
| 101 | ModuleNodePair node{attr.m_prefix.flat_map([](const auto& it) { |
| 102 | return boost::optional<std::string>{it.m_name}; |
| 103 | }), boost::get<leaf_>(attr.m_suffix).m_name}; |
| 104 | parserContext.m_tmpListKeyLeafPath.m_node = node; |
| 105 | } |
| 106 | |
| 107 | if constexpr (std::is_same<NodeType, dataNode_>()) { |
| 108 | if (attr.m_suffix.type() == typeid(listElement_)) { |
| 109 | parserContext.m_tmpListName = boost::get<listElement_>(attr.m_suffix).m_name; |
| 110 | res = listSuffix.parse(begin, end, ctx, rctx, boost::get<listElement_>(attr.m_suffix).m_keys); |
| 111 | } |
| 112 | } |
| 113 | |
Václav Kubernát | 60a0ed5 | 2020-04-28 15:21:33 +0200 | [diff] [blame] | 114 | if (res) { |
| 115 | parserContext.pushPathFragment(attr); |
Václav Kubernát | dd5945a | 2020-05-05 01:24:23 +0200 | [diff] [blame] | 116 | parserContext.m_topLevelModulePresent = true; |
| 117 | } |
| 118 | |
| 119 | if (attr.m_prefix) { |
| 120 | parserContext.m_curModule = boost::none; |
Václav Kubernát | 60a0ed5 | 2020-04-28 15:21:33 +0200 | [diff] [blame] | 121 | } |
| 122 | return res; |
| 123 | } |
Václav Kubernát | dd5945a | 2020-05-05 01:24:23 +0200 | [diff] [blame] | 124 | }; |
| 125 | |
| 126 | NodeParser<schemaNode_> schemaNode; |
| 127 | NodeParser<dataNode_> dataNode; |
Václav Kubernát | 60a0ed5 | 2020-04-28 15:21:33 +0200 | [diff] [blame] | 128 | |
Václav Kubernát | 4618fa4 | 2020-05-07 01:33:19 +0200 | [diff] [blame] | 129 | using AnyPath = boost::variant<schemaPath_, dataPath_>; |
| 130 | |
Václav Kubernát | 74d35a4 | 2020-05-15 15:29:16 +0200 | [diff] [blame^] | 131 | template <typename PathType> |
| 132 | struct PathParser : x3::parser<PathParser<PathType>> { |
| 133 | using attribute_type = PathType; |
Václav Kubernát | 4618fa4 | 2020-05-07 01:33:19 +0200 | [diff] [blame] | 134 | template <typename It, typename Ctx, typename RCtx, typename Attr> |
| 135 | bool parse(It& begin, It end, Ctx const& ctx, RCtx& rctx, Attr& attr) const |
| 136 | { |
Václav Kubernát | 74d35a4 | 2020-05-15 15:29:16 +0200 | [diff] [blame^] | 137 | initializePath.parse(begin, end, ctx, rctx, x3::unused); |
Václav Kubernát | 4618fa4 | 2020-05-07 01:33:19 +0200 | [diff] [blame] | 138 | dataPath_ attrData; |
| 139 | |
| 140 | // absoluteStart has to be separate from the dataPath parser, |
| 141 | // otherwise, if the "dataNode % '/'" parser fails, the begin iterator |
| 142 | // gets reverted to before the starting slash. |
| 143 | auto res = -absoluteStart.parse(begin, end, ctx, rctx, attrData.m_scope); |
| 144 | auto dataPath = x3::attr(attrData.m_scope) >> dataNode % '/' >> -trailingSlash; |
| 145 | res = dataPath.parse(begin, end, ctx, rctx, attrData); |
| 146 | attr = attrData; |
| 147 | |
| 148 | if constexpr (std::is_same<Attr, AnyPath>()) { |
| 149 | auto pathEnd = x3::rule<class PathEnd>{"pathEnd"} = &space_separator | x3::eoi; |
| 150 | // If parsing failed, or if there's more input we try parsing schema nodes |
| 151 | if (!res || !pathEnd.parse(begin, end, ctx, rctx, x3::unused)) { |
| 152 | // If dataPath parsed some nodes, they will be saved in `attrData`. We have to keep these. |
| 153 | schemaPath_ attrSchema = dataPathToSchemaPath(attrData); |
| 154 | auto schemaPath = schemaNode % '/'; |
| 155 | // The schemaPath parser continues where the dataPath parser ended. |
| 156 | res = schemaPath.parse(begin, end, ctx, rctx, attrSchema.m_nodes); |
| 157 | auto trailing = -trailingSlash >> pathEnd; |
| 158 | res = trailing.parse(begin, end, ctx, rctx, attrSchema.m_trailingSlash); |
| 159 | attr = attrSchema; |
| 160 | } |
| 161 | } |
| 162 | return res; |
| 163 | } |
Václav Kubernát | 74d35a4 | 2020-05-15 15:29:16 +0200 | [diff] [blame^] | 164 | }; |
Václav Kubernát | 4618fa4 | 2020-05-07 01:33:19 +0200 | [diff] [blame] | 165 | |
| 166 | // Need to use these wrappers so that my PathParser class gets the proper |
| 167 | // attribute. Otherwise, Spirit injects the attribute of the outer parser that |
| 168 | // uses my PathParser. |
| 169 | // Example grammar: anyPath | module. |
| 170 | // The PathParser class would get a boost::variant as the attribute, but I |
| 171 | // don't want to deal with that, so I use these wrappers to ensure the |
Václav Kubernát | 74d35a4 | 2020-05-15 15:29:16 +0200 | [diff] [blame^] | 172 | // attribute I want (and let Spirit deal with boost::variant). |
| 173 | auto const anyPath = x3::rule<class anyPath_class, AnyPath>{"anyPath"} = PathParser<AnyPath>{}; |
| 174 | auto const dataPath = x3::rule<class dataPath_class, dataPath_>{"dataPath"} = PathParser<dataPath_>{}; |
Václav Kubernát | 4618fa4 | 2020-05-07 01:33:19 +0200 | [diff] [blame] | 175 | |
Václav Kubernát | d0ea9b2 | 2020-04-24 00:44:15 +0200 | [diff] [blame] | 176 | #if __clang__ |
| 177 | #pragma GCC diagnostic push |
| 178 | #pragma GCC diagnostic ignored "-Woverloaded-shift-op-parentheses" |
| 179 | #endif |
| 180 | |
| 181 | auto const rest = |
| 182 | x3::omit[x3::no_skip[+(x3::char_ - '/' - space_separator)]]; |
| 183 | |
| 184 | auto const key_identifier_def = |
| 185 | x3::lexeme[ |
| 186 | ((x3::alpha | char_("_")) >> *(x3::alnum | char_("_") | char_("-") | char_("."))) |
| 187 | ]; |
| 188 | |
| 189 | auto const createKeySuggestions_def = |
| 190 | x3::eps; |
| 191 | |
| 192 | auto const createValueSuggestions_def = |
| 193 | x3::eps; |
| 194 | |
| 195 | auto const suggestKeysEnd_def = |
| 196 | x3::eps; |
| 197 | |
| 198 | auto const keyValue_def = |
| 199 | key_identifier > '=' > createValueSuggestions > leaf_data; |
| 200 | |
| 201 | auto const keyValueWrapper = |
| 202 | x3::lexeme['[' > createKeySuggestions > keyValue > suggestKeysEnd > ']']; |
| 203 | |
Václav Kubernát | d0ea9b2 | 2020-04-24 00:44:15 +0200 | [diff] [blame] | 204 | // even though we don't allow no keys to be supplied, the star allows me to check which keys are missing |
| 205 | auto const listSuffix_def = |
| 206 | *keyValueWrapper; |
| 207 | |
Václav Kubernát | d0ea9b2 | 2020-04-24 00:44:15 +0200 | [diff] [blame] | 208 | auto const list_def = |
| 209 | node_identifier >> !char_('['); |
| 210 | |
Václav Kubernát | d0ea9b2 | 2020-04-24 00:44:15 +0200 | [diff] [blame] | 211 | auto const absoluteStart_def = |
| 212 | x3::omit['/'] >> x3::attr(Scope::Absolute); |
| 213 | |
| 214 | auto const trailingSlash_def = |
| 215 | x3::omit['/'] >> x3::attr(TrailingSlash::Present); |
| 216 | |
| 217 | auto const createPathSuggestions_def = |
| 218 | x3::eps; |
| 219 | |
Václav Kubernát | d0ea9b2 | 2020-04-24 00:44:15 +0200 | [diff] [blame] | 220 | auto const dataNodeList_def = |
| 221 | createPathSuggestions >> -(module) >> list; |
| 222 | |
| 223 | // This intermediate rule is mandatory, because we need the first alternative |
| 224 | // to be collapsed to a vector. If we didn't use the intermediate rule, |
| 225 | // Spirit wouldn't know we want it to collapse. |
| 226 | // https://github.com/boostorg/spirit/issues/408 |
| 227 | auto const dataNodesListEnd_def = |
| 228 | dataNode % '/' >> '/' >> dataNodeList >> -(&char_('/') >> createPathSuggestions) | |
| 229 | x3::attr(decltype(dataPath_::m_nodes)()) >> dataNodeList; |
| 230 | |
| 231 | 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)); |
| 232 | |
Václav Kubernát | d0ea9b2 | 2020-04-24 00:44:15 +0200 | [diff] [blame] | 233 | auto const leafPath_def = |
| 234 | dataPath; |
| 235 | |
| 236 | auto const presenceContainerPath_def = |
| 237 | dataPath; |
| 238 | |
| 239 | auto const listInstancePath_def = |
| 240 | dataPath; |
| 241 | |
| 242 | // A "nothing" parser, which is used to indicate we tried to parse a path |
| 243 | auto const initializePath_def = |
| 244 | x3::eps; |
| 245 | |
| 246 | |
| 247 | |
| 248 | #if __clang__ |
| 249 | #pragma GCC diagnostic pop |
| 250 | #endif |
| 251 | |
| 252 | BOOST_SPIRIT_DEFINE(keyValue) |
| 253 | BOOST_SPIRIT_DEFINE(key_identifier) |
Václav Kubernát | d0ea9b2 | 2020-04-24 00:44:15 +0200 | [diff] [blame] | 254 | BOOST_SPIRIT_DEFINE(listSuffix) |
Václav Kubernát | d0ea9b2 | 2020-04-24 00:44:15 +0200 | [diff] [blame] | 255 | BOOST_SPIRIT_DEFINE(list) |
Václav Kubernát | d0ea9b2 | 2020-04-24 00:44:15 +0200 | [diff] [blame] | 256 | BOOST_SPIRIT_DEFINE(dataNodeList) |
| 257 | BOOST_SPIRIT_DEFINE(dataNodesListEnd) |
| 258 | BOOST_SPIRIT_DEFINE(leafPath) |
| 259 | BOOST_SPIRIT_DEFINE(presenceContainerPath) |
| 260 | BOOST_SPIRIT_DEFINE(listInstancePath) |
Václav Kubernát | d0ea9b2 | 2020-04-24 00:44:15 +0200 | [diff] [blame] | 261 | BOOST_SPIRIT_DEFINE(dataPathListEnd) |
| 262 | BOOST_SPIRIT_DEFINE(initializePath) |
| 263 | BOOST_SPIRIT_DEFINE(createKeySuggestions) |
| 264 | BOOST_SPIRIT_DEFINE(createPathSuggestions) |
| 265 | BOOST_SPIRIT_DEFINE(createValueSuggestions) |
| 266 | BOOST_SPIRIT_DEFINE(suggestKeysEnd) |
| 267 | BOOST_SPIRIT_DEFINE(absoluteStart) |
| 268 | BOOST_SPIRIT_DEFINE(trailingSlash) |