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 | e7248b2 | 2020-06-26 15:38:59 +0200 | [diff] [blame] | 17 | x3::rule<cdPath_class, dataPath_> const cdPath = "cdPath"; |
| 18 | x3::rule<rpcPath_class, dataPath_> const rpcPath = "rpcPath"; |
Václav Kubernát | d0ea9b2 | 2020-04-24 00:44:15 +0200 | [diff] [blame] | 19 | x3::rule<presenceContainerPath_class, dataPath_> const presenceContainerPath = "presenceContainerPath"; |
| 20 | x3::rule<listInstancePath_class, dataPath_> const listInstancePath = "listInstancePath"; |
Václav Kubernát | 5b8a8f3 | 2020-05-20 00:57:22 +0200 | [diff] [blame] | 21 | x3::rule<leafListElementPath_class, dataPath_> const leafListElementPath = "leafListElementPath"; |
Václav Kubernát | d0ea9b2 | 2020-04-24 00:44:15 +0200 | [diff] [blame] | 22 | x3::rule<initializePath_class, x3::unused_type> const initializePath = "initializePath"; |
Václav Kubernát | d0ea9b2 | 2020-04-24 00:44:15 +0200 | [diff] [blame] | 23 | x3::rule<trailingSlash_class, TrailingSlash> const trailingSlash = "trailingSlash"; |
Václav Kubernát | d0ea9b2 | 2020-04-24 00:44:15 +0200 | [diff] [blame] | 24 | x3::rule<absoluteStart_class, Scope> const absoluteStart = "absoluteStart"; |
| 25 | x3::rule<keyValue_class, keyValue_> const keyValue = "keyValue"; |
| 26 | 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] | 27 | x3::rule<listSuffix_class, std::vector<keyValue_>> const listSuffix = "listSuffix"; |
Václav Kubernát | d0ea9b2 | 2020-04-24 00:44:15 +0200 | [diff] [blame] | 28 | x3::rule<createKeySuggestions_class, x3::unused_type> const createKeySuggestions = "createKeySuggestions"; |
| 29 | x3::rule<createValueSuggestions_class, x3::unused_type> const createValueSuggestions = "createValueSuggestions"; |
| 30 | x3::rule<suggestKeysEnd_class, x3::unused_type> const suggestKeysEnd = "suggestKeysEnd"; |
Václav Kubernát | 5b8a8f3 | 2020-05-20 00:57:22 +0200 | [diff] [blame] | 31 | x3::rule<class leafListValue_class, leaf_data_> const leafListValue = "leafListValue"; |
Václav Kubernát | d0ea9b2 | 2020-04-24 00:44:15 +0200 | [diff] [blame] | 32 | |
Václav Kubernát | 4a58ce6 | 2020-05-14 17:58:10 +0200 | [diff] [blame] | 33 | enum class NodeParserMode { |
| 34 | CompleteDataNode, |
| 35 | IncompleteDataNode, |
| 36 | CompletionsOnly, |
| 37 | SchemaNode |
| 38 | }; |
| 39 | |
| 40 | template <auto> |
| 41 | struct ModeToAttribute; |
| 42 | template <> |
| 43 | struct ModeToAttribute<NodeParserMode::CompleteDataNode> { |
| 44 | using type = dataNode_; |
| 45 | }; |
| 46 | template <> |
| 47 | struct ModeToAttribute<NodeParserMode::IncompleteDataNode> { |
| 48 | using type = dataNode_; |
| 49 | }; |
| 50 | template <> |
| 51 | struct ModeToAttribute<NodeParserMode::SchemaNode> { |
| 52 | using type = schemaNode_; |
| 53 | }; |
| 54 | // The CompletionsOnly attribute is dataNode_ only because of convenience: |
| 55 | // having the same return type means we can get by without a ton of `if constexpr` stanzas. |
| 56 | // So the code will still "parse data into the target attr" for simplicity. |
| 57 | template <> |
| 58 | struct ModeToAttribute<NodeParserMode::CompletionsOnly> { |
| 59 | using type = dataNode_; |
| 60 | }; |
| 61 | |
Václav Kubernát | 743d9eb | 2020-05-18 13:42:36 +0200 | [diff] [blame] | 62 | enum class CompletionMode { |
| 63 | Schema, |
| 64 | Data |
| 65 | }; |
Václav Kubernát | 5b8a8f3 | 2020-05-20 00:57:22 +0200 | [diff] [blame] | 66 | |
Václav Kubernát | 743d9eb | 2020-05-18 13:42:36 +0200 | [diff] [blame] | 67 | template <NodeParserMode PARSER_MODE, CompletionMode COMPLETION_MODE> |
| 68 | struct NodeParser : x3::parser<NodeParser<PARSER_MODE, COMPLETION_MODE>> { |
Václav Kubernát | 4a58ce6 | 2020-05-14 17:58:10 +0200 | [diff] [blame] | 69 | using attribute_type = typename ModeToAttribute<PARSER_MODE>::type; |
Václav Kubernát | abf5280 | 2020-05-19 01:31:17 +0200 | [diff] [blame] | 70 | |
| 71 | std::function<bool(const Schema&, const std::string& path)> m_filterFunction; |
| 72 | |
| 73 | NodeParser(const std::function<bool(const Schema&, const std::string& path)>& filterFunction) |
| 74 | : m_filterFunction(filterFunction) |
| 75 | { |
| 76 | } |
| 77 | |
Václav Kubernát | 4a58ce6 | 2020-05-14 17:58:10 +0200 | [diff] [blame] | 78 | // GCC complains that `end` isn't used when doing completions only |
| 79 | // FIXME: GCC 10.1 doesn't emit a warning here. Remove [[maybe_unused]] when GCC 10 is available |
Václav Kubernát | 60a0ed5 | 2020-04-28 15:21:33 +0200 | [diff] [blame] | 80 | template <typename It, typename Ctx, typename RCtx, typename Attr> |
Václav Kubernát | 4a58ce6 | 2020-05-14 17:58:10 +0200 | [diff] [blame] | 81 | bool parse(It& begin, [[maybe_unused]] It end, Ctx const& ctx, RCtx& rctx, Attr& attr) const |
Václav Kubernát | 60a0ed5 | 2020-04-28 15:21:33 +0200 | [diff] [blame] | 82 | { |
Václav Kubernát | dd5945a | 2020-05-05 01:24:23 +0200 | [diff] [blame] | 83 | std::string tableName; |
Václav Kubernát | 4a58ce6 | 2020-05-14 17:58:10 +0200 | [diff] [blame] | 84 | if constexpr (std::is_same<attribute_type, schemaNode_>()) { |
Václav Kubernát | dd5945a | 2020-05-05 01:24:23 +0200 | [diff] [blame] | 85 | tableName = "schemaNode"; |
| 86 | } else { |
| 87 | tableName = "dataNode"; |
| 88 | } |
Václav Kubernát | 4a58ce6 | 2020-05-14 17:58:10 +0200 | [diff] [blame] | 89 | x3::symbols<attribute_type> table(tableName); |
Václav Kubernát | 60a0ed5 | 2020-04-28 15:21:33 +0200 | [diff] [blame] | 90 | |
| 91 | ParserContext& parserContext = x3::get<parser_context_tag>(ctx); |
| 92 | parserContext.m_suggestions.clear(); |
| 93 | for (const auto& child : parserContext.m_schema.availableNodes(parserContext.currentSchemaPath(), Recursion::NonRecursive)) { |
Václav Kubernát | 4a58ce6 | 2020-05-14 17:58:10 +0200 | [diff] [blame] | 94 | attribute_type out; |
Václav Kubernát | 60a0ed5 | 2020-04-28 15:21:33 +0200 | [diff] [blame] | 95 | std::string parseString; |
| 96 | if (child.first) { |
| 97 | out.m_prefix = module_{*child.first}; |
| 98 | parseString = *child.first + ":"; |
| 99 | } |
| 100 | parseString += child.second; |
Václav Kubernát | abf5280 | 2020-05-19 01:31:17 +0200 | [diff] [blame] | 101 | |
| 102 | if (!m_filterFunction(parserContext.m_schema, joinPaths(pathToSchemaString(parserContext.currentSchemaPath(), Prefixes::Always), parseString))) { |
| 103 | continue; |
| 104 | } |
| 105 | |
Václav Kubernát | 60a0ed5 | 2020-04-28 15:21:33 +0200 | [diff] [blame] | 106 | switch (parserContext.m_schema.nodeType(parserContext.currentSchemaPath(), child)) { |
| 107 | case yang::NodeTypes::Container: |
| 108 | case yang::NodeTypes::PresenceContainer: |
| 109 | out.m_suffix = container_{child.second}; |
| 110 | parserContext.m_suggestions.emplace(Completion{parseString + "/"}); |
| 111 | break; |
| 112 | case yang::NodeTypes::Leaf: |
| 113 | out.m_suffix = leaf_{child.second}; |
| 114 | parserContext.m_suggestions.emplace(Completion{parseString + " "}); |
| 115 | break; |
| 116 | case yang::NodeTypes::List: |
Václav Kubernát | 4a58ce6 | 2020-05-14 17:58:10 +0200 | [diff] [blame] | 117 | if constexpr (std::is_same<attribute_type, schemaNode_>()) { |
Václav Kubernát | dd5945a | 2020-05-05 01:24:23 +0200 | [diff] [blame] | 118 | out.m_suffix = list_{child.second}; |
| 119 | } else { |
| 120 | out.m_suffix = listElement_{child.second, {}}; |
| 121 | } |
Václav Kubernát | 743d9eb | 2020-05-18 13:42:36 +0200 | [diff] [blame] | 122 | |
| 123 | if constexpr (COMPLETION_MODE == CompletionMode::Schema) { |
| 124 | parserContext.m_suggestions.emplace(Completion{parseString + "/"}); |
| 125 | } else { |
| 126 | parserContext.m_suggestions.emplace(Completion{parseString, "[", Completion::WhenToAdd::IfFullMatch}); |
| 127 | } |
Václav Kubernát | 60a0ed5 | 2020-04-28 15:21:33 +0200 | [diff] [blame] | 128 | break; |
Václav Kubernát | 5b8a8f3 | 2020-05-20 00:57:22 +0200 | [diff] [blame] | 129 | case yang::NodeTypes::LeafList: |
| 130 | if constexpr (std::is_same<attribute_type, schemaNode_>()) { |
| 131 | out.m_suffix = leafList_{child.second}; |
| 132 | } else { |
| 133 | out.m_suffix = leafListElement_{child.second, {}}; |
| 134 | } |
Václav Kubernát | 743d9eb | 2020-05-18 13:42:36 +0200 | [diff] [blame] | 135 | |
| 136 | if constexpr (COMPLETION_MODE == CompletionMode::Schema) { |
| 137 | parserContext.m_suggestions.emplace(Completion{parseString + "/"}); |
| 138 | } else { |
| 139 | parserContext.m_suggestions.emplace(Completion{parseString, "[", Completion::WhenToAdd::IfFullMatch}); |
| 140 | } |
Václav Kubernát | 5b8a8f3 | 2020-05-20 00:57:22 +0200 | [diff] [blame] | 141 | break; |
Václav Kubernát | e7248b2 | 2020-06-26 15:38:59 +0200 | [diff] [blame] | 142 | case yang::NodeTypes::Rpc: |
| 143 | out.m_suffix = rpcNode_{child.second}; |
| 144 | parserContext.m_suggestions.emplace(Completion{parseString + "/"}); |
| 145 | break; |
Václav Kubernát | 60a0ed5 | 2020-04-28 15:21:33 +0200 | [diff] [blame] | 146 | case yang::NodeTypes::Action: |
Václav Kubernát | aa4250a | 2020-07-22 00:02:23 +0200 | [diff] [blame] | 147 | out.m_suffix = actionNode_{child.second}; |
| 148 | parserContext.m_suggestions.emplace(Completion{parseString + "/"}); |
| 149 | break; |
Václav Kubernát | 60a0ed5 | 2020-04-28 15:21:33 +0200 | [diff] [blame] | 150 | case yang::NodeTypes::AnyXml: |
Václav Kubernát | 60a0ed5 | 2020-04-28 15:21:33 +0200 | [diff] [blame] | 151 | case yang::NodeTypes::Notification: |
Václav Kubernát | 60a0ed5 | 2020-04-28 15:21:33 +0200 | [diff] [blame] | 152 | continue; |
| 153 | } |
| 154 | table.add(parseString, out); |
Václav Kubernát | 60a0ed5 | 2020-04-28 15:21:33 +0200 | [diff] [blame] | 155 | if (!child.first) { |
| 156 | auto topLevelModule = parserContext.currentSchemaPath().m_nodes.begin()->m_prefix; |
| 157 | out.m_prefix = topLevelModule; |
| 158 | table.add(topLevelModule->m_name + ":" + parseString, out); |
| 159 | } |
| 160 | } |
Václav Kubernát | aed4bc7 | 2020-06-08 01:09:25 +0200 | [diff] [blame] | 161 | table.add("..", attribute_type{nodeup_{}}); |
Václav Kubernát | dd5945a | 2020-05-05 01:24:23 +0200 | [diff] [blame] | 162 | parserContext.m_completionIterator = begin; |
Václav Kubernát | dd5945a | 2020-05-05 01:24:23 +0200 | [diff] [blame] | 163 | |
Václav Kubernát | 4a58ce6 | 2020-05-14 17:58:10 +0200 | [diff] [blame] | 164 | if constexpr (PARSER_MODE == NodeParserMode::CompletionsOnly) { |
| 165 | return true; |
| 166 | } else { |
| 167 | It saveIter; |
| 168 | // GCC complains that I assign saveIter because I use it only if NodeType is dataNode_ |
| 169 | // FIXME: GCC 10.1 doesn't emit a warning here. Make this unconditional when GCC 10 is available. |
| 170 | if constexpr (std::is_same<attribute_type, dataNode_>()) { |
| 171 | saveIter = begin; |
Václav Kubernát | dd5945a | 2020-05-05 01:24:23 +0200 | [diff] [blame] | 172 | } |
Václav Kubernát | dd5945a | 2020-05-05 01:24:23 +0200 | [diff] [blame] | 173 | |
Václav Kubernát | 4a58ce6 | 2020-05-14 17:58:10 +0200 | [diff] [blame] | 174 | auto res = table.parse(begin, end, ctx, rctx, attr); |
Václav Kubernát | dd5945a | 2020-05-05 01:24:23 +0200 | [diff] [blame] | 175 | |
Václav Kubernát | b5ca154 | 2020-05-27 01:03:54 +0200 | [diff] [blame] | 176 | if (std::holds_alternative<leaf_>(attr.m_suffix)) { |
Václav Kubernát | 4a58ce6 | 2020-05-14 17:58:10 +0200 | [diff] [blame] | 177 | parserContext.m_tmpListKeyLeafPath.m_location = parserContext.currentSchemaPath(); |
| 178 | ModuleNodePair node{attr.m_prefix.flat_map([](const auto& it) { |
| 179 | return boost::optional<std::string>{it.m_name}; |
| 180 | }), |
Václav Kubernát | b5ca154 | 2020-05-27 01:03:54 +0200 | [diff] [blame] | 181 | std::get<leaf_>(attr.m_suffix).m_name}; |
Václav Kubernát | 4a58ce6 | 2020-05-14 17:58:10 +0200 | [diff] [blame] | 182 | parserContext.m_tmpListKeyLeafPath.m_node = node; |
| 183 | } |
| 184 | |
| 185 | if constexpr (std::is_same<attribute_type, dataNode_>()) { |
Václav Kubernát | b5ca154 | 2020-05-27 01:03:54 +0200 | [diff] [blame] | 186 | if (std::holds_alternative<listElement_>(attr.m_suffix)) { |
Václav Kubernát | 2db124c | 2020-05-28 21:58:36 +0200 | [diff] [blame] | 187 | parserContext.m_tmpListPath = parserContext.currentDataPath(); |
| 188 | auto tmpList = list_{std::get<listElement_>(attr.m_suffix).m_name}; |
Václav Kubernát | faacd02 | 2020-07-08 16:44:38 +0200 | [diff] [blame] | 189 | parserContext.m_tmpListPath.m_nodes.emplace_back(attr.m_prefix, tmpList); |
Václav Kubernát | 2db124c | 2020-05-28 21:58:36 +0200 | [diff] [blame] | 190 | |
Václav Kubernát | b5ca154 | 2020-05-27 01:03:54 +0200 | [diff] [blame] | 191 | res = listSuffix.parse(begin, end, ctx, rctx, std::get<listElement_>(attr.m_suffix).m_keys); |
Václav Kubernát | 4a58ce6 | 2020-05-14 17:58:10 +0200 | [diff] [blame] | 192 | |
| 193 | // FIXME: think of a better way to do this, that is, get rid of manual iterator reverting |
| 194 | if (!res) { |
| 195 | // If listSuffix didn't succeed, we check, if we allow incomplete nodes. If we do, then we replace listElement_ with list_. |
| 196 | // If we don't, we fail the whole symbol table. |
| 197 | if constexpr (PARSER_MODE == NodeParserMode::IncompleteDataNode) { |
| 198 | res = true; |
Václav Kubernát | b5ca154 | 2020-05-27 01:03:54 +0200 | [diff] [blame] | 199 | attr.m_suffix = list_{std::get<listElement_>(attr.m_suffix).m_name}; |
Václav Kubernát | 4a58ce6 | 2020-05-14 17:58:10 +0200 | [diff] [blame] | 200 | } else { |
| 201 | begin = saveIter; |
| 202 | } |
| 203 | } |
| 204 | } |
Václav Kubernát | 5b8a8f3 | 2020-05-20 00:57:22 +0200 | [diff] [blame] | 205 | |
Václav Kubernát | b5ca154 | 2020-05-27 01:03:54 +0200 | [diff] [blame] | 206 | if (std::holds_alternative<leafListElement_>(attr.m_suffix)) { |
Václav Kubernát | 5b8a8f3 | 2020-05-20 00:57:22 +0200 | [diff] [blame] | 207 | parserContext.m_tmpListKeyLeafPath.m_location = parserContext.currentSchemaPath(); |
| 208 | ModuleNodePair node{attr.m_prefix.flat_map([](const auto& it) { |
| 209 | return boost::optional<std::string>{it.m_name}; |
| 210 | }), |
Václav Kubernát | b5ca154 | 2020-05-27 01:03:54 +0200 | [diff] [blame] | 211 | std::get<leafListElement_>(attr.m_suffix).m_name}; |
Václav Kubernát | 5b8a8f3 | 2020-05-20 00:57:22 +0200 | [diff] [blame] | 212 | parserContext.m_tmpListKeyLeafPath.m_node = node; |
Václav Kubernát | b5ca154 | 2020-05-27 01:03:54 +0200 | [diff] [blame] | 213 | res = leafListValue.parse(begin, end, ctx, rctx, std::get<leafListElement_>(attr.m_suffix).m_value); |
Václav Kubernát | 5b8a8f3 | 2020-05-20 00:57:22 +0200 | [diff] [blame] | 214 | |
| 215 | if (!res) { |
| 216 | if constexpr (PARSER_MODE == NodeParserMode::IncompleteDataNode) { |
| 217 | res = true; |
Václav Kubernát | b5ca154 | 2020-05-27 01:03:54 +0200 | [diff] [blame] | 218 | attr.m_suffix = leafList_{std::get<leafListElement_>(attr.m_suffix).m_name}; |
Václav Kubernát | 5b8a8f3 | 2020-05-20 00:57:22 +0200 | [diff] [blame] | 219 | } else { |
| 220 | begin = saveIter; |
| 221 | } |
| 222 | } |
| 223 | } |
Václav Kubernát | 4a58ce6 | 2020-05-14 17:58:10 +0200 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | if (res) { |
| 227 | parserContext.pushPathFragment(attr); |
Václav Kubernát | 4a58ce6 | 2020-05-14 17:58:10 +0200 | [diff] [blame] | 228 | } |
| 229 | |
Václav Kubernát | 4a58ce6 | 2020-05-14 17:58:10 +0200 | [diff] [blame] | 230 | return res; |
Václav Kubernát | 60a0ed5 | 2020-04-28 15:21:33 +0200 | [diff] [blame] | 231 | } |
Václav Kubernát | 60a0ed5 | 2020-04-28 15:21:33 +0200 | [diff] [blame] | 232 | } |
Václav Kubernát | dd5945a | 2020-05-05 01:24:23 +0200 | [diff] [blame] | 233 | }; |
| 234 | |
Václav Kubernát | 743d9eb | 2020-05-18 13:42:36 +0200 | [diff] [blame] | 235 | template <CompletionMode COMPLETION_MODE> using schemaNode = NodeParser<NodeParserMode::SchemaNode, COMPLETION_MODE>; |
| 236 | template <CompletionMode COMPLETION_MODE> using dataNode = NodeParser<NodeParserMode::CompleteDataNode, COMPLETION_MODE>; |
| 237 | template <CompletionMode COMPLETION_MODE> using incompleteDataNode = NodeParser<NodeParserMode::IncompleteDataNode, COMPLETION_MODE>; |
| 238 | template <CompletionMode COMPLETION_MODE> using pathCompletions = NodeParser<NodeParserMode::CompletionsOnly, COMPLETION_MODE>; |
Václav Kubernát | 60a0ed5 | 2020-04-28 15:21:33 +0200 | [diff] [blame] | 239 | |
Václav Kubernát | 4618fa4 | 2020-05-07 01:33:19 +0200 | [diff] [blame] | 240 | using AnyPath = boost::variant<schemaPath_, dataPath_>; |
| 241 | |
Václav Kubernát | 4a58ce6 | 2020-05-14 17:58:10 +0200 | [diff] [blame] | 242 | enum class PathParserMode { |
| 243 | AnyPath, |
| 244 | DataPath, |
| 245 | DataPathListEnd |
| 246 | }; |
| 247 | |
| 248 | template <> |
| 249 | struct ModeToAttribute<PathParserMode::AnyPath> { |
| 250 | using type = AnyPath; |
| 251 | }; |
| 252 | |
| 253 | template <> |
| 254 | struct ModeToAttribute<PathParserMode::DataPath> { |
| 255 | using type = dataPath_; |
| 256 | }; |
| 257 | |
| 258 | template <> |
| 259 | struct ModeToAttribute<PathParserMode::DataPathListEnd> { |
| 260 | using type = dataPath_; |
| 261 | }; |
| 262 | |
Václav Kubernát | 743d9eb | 2020-05-18 13:42:36 +0200 | [diff] [blame] | 263 | template <PathParserMode PARSER_MODE, CompletionMode COMPLETION_MODE> |
| 264 | struct PathParser : x3::parser<PathParser<PARSER_MODE, COMPLETION_MODE>> { |
Václav Kubernát | 4a58ce6 | 2020-05-14 17:58:10 +0200 | [diff] [blame] | 265 | using attribute_type = ModeToAttribute<PARSER_MODE>; |
Václav Kubernát | abf5280 | 2020-05-19 01:31:17 +0200 | [diff] [blame] | 266 | std::function<bool(const Schema&, const std::string& path)> m_filterFunction; |
| 267 | |
| 268 | PathParser(const std::function<bool(const Schema&, const std::string& path)>& filterFunction = [] (const auto&, const auto&) {return true;}) |
| 269 | : m_filterFunction(filterFunction) |
| 270 | { |
| 271 | } |
| 272 | |
Václav Kubernát | 4618fa4 | 2020-05-07 01:33:19 +0200 | [diff] [blame] | 273 | template <typename It, typename Ctx, typename RCtx, typename Attr> |
| 274 | bool parse(It& begin, It end, Ctx const& ctx, RCtx& rctx, Attr& attr) const |
| 275 | { |
Václav Kubernát | 74d35a4 | 2020-05-15 15:29:16 +0200 | [diff] [blame] | 276 | initializePath.parse(begin, end, ctx, rctx, x3::unused); |
Václav Kubernát | 4618fa4 | 2020-05-07 01:33:19 +0200 | [diff] [blame] | 277 | dataPath_ attrData; |
| 278 | |
Václav Kubernát | 4a58ce6 | 2020-05-14 17:58:10 +0200 | [diff] [blame] | 279 | auto pathEnd = x3::rule<class PathEnd>{"pathEnd"} = &space_separator | x3::eoi; |
Václav Kubernát | 4618fa4 | 2020-05-07 01:33:19 +0200 | [diff] [blame] | 280 | // absoluteStart has to be separate from the dataPath parser, |
| 281 | // otherwise, if the "dataNode % '/'" parser fails, the begin iterator |
| 282 | // gets reverted to before the starting slash. |
Václav Kubernát | 4a58ce6 | 2020-05-14 17:58:10 +0200 | [diff] [blame] | 283 | auto res = (-absoluteStart).parse(begin, end, ctx, rctx, attrData.m_scope); |
| 284 | auto dataPath = x3::attr(attrData.m_scope) |
Václav Kubernát | 743d9eb | 2020-05-18 13:42:36 +0200 | [diff] [blame] | 285 | >> (dataNode<COMPLETION_MODE>{m_filterFunction} % '/' | pathEnd >> x3::attr(std::vector<dataNode_>{})) |
Václav Kubernát | 4a58ce6 | 2020-05-14 17:58:10 +0200 | [diff] [blame] | 286 | >> -trailingSlash; |
Václav Kubernát | 4618fa4 | 2020-05-07 01:33:19 +0200 | [diff] [blame] | 287 | res = dataPath.parse(begin, end, ctx, rctx, attrData); |
Václav Kubernát | 4618fa4 | 2020-05-07 01:33:19 +0200 | [diff] [blame] | 288 | |
Václav Kubernát | 4a58ce6 | 2020-05-14 17:58:10 +0200 | [diff] [blame] | 289 | // If we allow data paths with a list at the end, we just try to parse that separately. |
| 290 | if constexpr (PARSER_MODE == PathParserMode::DataPathListEnd || PARSER_MODE == PathParserMode::AnyPath) { |
Václav Kubernát | 4618fa4 | 2020-05-07 01:33:19 +0200 | [diff] [blame] | 291 | if (!res || !pathEnd.parse(begin, end, ctx, rctx, x3::unused)) { |
Václav Kubernát | 4a58ce6 | 2020-05-14 17:58:10 +0200 | [diff] [blame] | 292 | dataNode_ attrNodeList; |
Václav Kubernát | 743d9eb | 2020-05-18 13:42:36 +0200 | [diff] [blame] | 293 | res = incompleteDataNode<COMPLETION_MODE>{m_filterFunction}.parse(begin, end, ctx, rctx, attrNodeList); |
Václav Kubernát | 4a58ce6 | 2020-05-14 17:58:10 +0200 | [diff] [blame] | 294 | if (res) { |
Václav Kubernát | faacd02 | 2020-07-08 16:44:38 +0200 | [diff] [blame] | 295 | attrData.m_nodes.emplace_back(attrNodeList); |
Václav Kubernát | 4a58ce6 | 2020-05-14 17:58:10 +0200 | [diff] [blame] | 296 | // If the trailing slash matches, no more nodes are parsed. |
| 297 | // That means no more completion. So, I generate them |
| 298 | // manually. |
Václav Kubernát | 743d9eb | 2020-05-18 13:42:36 +0200 | [diff] [blame] | 299 | res = (-(trailingSlash >> x3::omit[pathCompletions<COMPLETION_MODE>{m_filterFunction}])).parse(begin, end, ctx, rctx, attrData.m_trailingSlash); |
Václav Kubernát | 4a58ce6 | 2020-05-14 17:58:10 +0200 | [diff] [blame] | 300 | } |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | attr = attrData; |
| 305 | if constexpr (PARSER_MODE == PathParserMode::AnyPath) { |
| 306 | // If our data path already has some listElement_ fragments, we can't parse rest of the path as a schema path |
| 307 | auto hasLists = std::any_of(attrData.m_nodes.begin(), attrData.m_nodes.end(), |
Václav Kubernát | b5ca154 | 2020-05-27 01:03:54 +0200 | [diff] [blame] | 308 | [] (const auto& node) { return std::holds_alternative<listElement_>(node.m_suffix); }); |
Václav Kubernát | 4a58ce6 | 2020-05-14 17:58:10 +0200 | [diff] [blame] | 309 | // If parsing failed, or if there's more input we try parsing schema nodes. |
| 310 | if (!hasLists) { |
| 311 | if (!res || !pathEnd.parse(begin, end, ctx, rctx, x3::unused)) { |
| 312 | // If dataPath parsed some nodes, they will be saved in `attrData`. We have to keep these. |
| 313 | schemaPath_ attrSchema = dataPathToSchemaPath(attrData); |
Václav Kubernát | 743d9eb | 2020-05-18 13:42:36 +0200 | [diff] [blame] | 314 | auto schemaPath = schemaNode<COMPLETION_MODE>{m_filterFunction} % '/'; |
Václav Kubernát | 4a58ce6 | 2020-05-14 17:58:10 +0200 | [diff] [blame] | 315 | // The schemaPath parser continues where the dataPath parser ended. |
| 316 | res = schemaPath.parse(begin, end, ctx, rctx, attrSchema.m_nodes); |
| 317 | auto trailing = -trailingSlash >> pathEnd; |
| 318 | res = trailing.parse(begin, end, ctx, rctx, attrSchema.m_trailingSlash); |
| 319 | attr = attrSchema; |
| 320 | } |
Václav Kubernát | 4618fa4 | 2020-05-07 01:33:19 +0200 | [diff] [blame] | 321 | } |
| 322 | } |
| 323 | return res; |
| 324 | } |
Václav Kubernát | 74d35a4 | 2020-05-15 15:29:16 +0200 | [diff] [blame] | 325 | }; |
Václav Kubernát | 4618fa4 | 2020-05-07 01:33:19 +0200 | [diff] [blame] | 326 | |
| 327 | // Need to use these wrappers so that my PathParser class gets the proper |
| 328 | // attribute. Otherwise, Spirit injects the attribute of the outer parser that |
| 329 | // uses my PathParser. |
| 330 | // Example grammar: anyPath | module. |
| 331 | // The PathParser class would get a boost::variant as the attribute, but I |
| 332 | // 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] | 333 | // attribute I want (and let Spirit deal with boost::variant). |
Václav Kubernát | 743d9eb | 2020-05-18 13:42:36 +0200 | [diff] [blame] | 334 | auto const anyPath = x3::rule<class anyPath_class, AnyPath>{"anyPath"} = PathParser<PathParserMode::AnyPath, CompletionMode::Schema>{}; |
| 335 | auto const dataPath = x3::rule<class dataPath_class, dataPath_>{"dataPath"} = PathParser<PathParserMode::DataPath, CompletionMode::Data>{}; |
| 336 | auto const dataPathListEnd = x3::rule<class dataPath_class, dataPath_>{"dataPath"} = PathParser<PathParserMode::DataPathListEnd, CompletionMode::Data>{}; |
Václav Kubernát | 4618fa4 | 2020-05-07 01:33:19 +0200 | [diff] [blame] | 337 | |
Václav Kubernát | d0ea9b2 | 2020-04-24 00:44:15 +0200 | [diff] [blame] | 338 | #if __clang__ |
| 339 | #pragma GCC diagnostic push |
| 340 | #pragma GCC diagnostic ignored "-Woverloaded-shift-op-parentheses" |
| 341 | #endif |
| 342 | |
Václav Kubernát | 5b8a8f3 | 2020-05-20 00:57:22 +0200 | [diff] [blame] | 343 | struct SuggestLeafListEnd : x3::parser<SuggestLeafListEnd> { |
| 344 | using attribute_type = x3::unused_type; |
| 345 | template <typename It, typename Ctx, typename RCtx, typename Attr> |
| 346 | bool parse(It& begin, It, Ctx const& ctx, RCtx&, Attr&) const |
| 347 | { |
| 348 | auto& parserContext = x3::get<parser_context_tag>(ctx); |
| 349 | parserContext.m_completionIterator = begin; |
| 350 | parserContext.m_suggestions = {Completion{"]"}}; |
| 351 | |
| 352 | return true; |
| 353 | } |
| 354 | } const suggestLeafListEnd; |
| 355 | |
| 356 | auto const leafListValue_def = |
| 357 | '[' >> leaf_data >> suggestLeafListEnd >> ']'; |
| 358 | |
Václav Kubernát | d0ea9b2 | 2020-04-24 00:44:15 +0200 | [diff] [blame] | 359 | auto const rest = |
| 360 | x3::omit[x3::no_skip[+(x3::char_ - '/' - space_separator)]]; |
| 361 | |
| 362 | auto const key_identifier_def = |
| 363 | x3::lexeme[ |
| 364 | ((x3::alpha | char_("_")) >> *(x3::alnum | char_("_") | char_("-") | char_("."))) |
| 365 | ]; |
| 366 | |
| 367 | auto const createKeySuggestions_def = |
| 368 | x3::eps; |
| 369 | |
| 370 | auto const createValueSuggestions_def = |
| 371 | x3::eps; |
| 372 | |
| 373 | auto const suggestKeysEnd_def = |
| 374 | x3::eps; |
| 375 | |
| 376 | auto const keyValue_def = |
| 377 | key_identifier > '=' > createValueSuggestions > leaf_data; |
| 378 | |
| 379 | auto const keyValueWrapper = |
Václav Kubernát | 9fa5dca | 2020-06-01 03:56:41 +0200 | [diff] [blame] | 380 | x3::no_skip['[' > createKeySuggestions > keyValue > suggestKeysEnd > ']']; |
Václav Kubernát | d0ea9b2 | 2020-04-24 00:44:15 +0200 | [diff] [blame] | 381 | |
Václav Kubernát | d0ea9b2 | 2020-04-24 00:44:15 +0200 | [diff] [blame] | 382 | // even though we don't allow no keys to be supplied, the star allows me to check which keys are missing |
| 383 | auto const listSuffix_def = |
| 384 | *keyValueWrapper; |
| 385 | |
Václav Kubernát | d0ea9b2 | 2020-04-24 00:44:15 +0200 | [diff] [blame] | 386 | auto const list_def = |
| 387 | node_identifier >> !char_('['); |
| 388 | |
Václav Kubernát | d0ea9b2 | 2020-04-24 00:44:15 +0200 | [diff] [blame] | 389 | auto const absoluteStart_def = |
| 390 | x3::omit['/'] >> x3::attr(Scope::Absolute); |
| 391 | |
| 392 | auto const trailingSlash_def = |
| 393 | x3::omit['/'] >> x3::attr(TrailingSlash::Present); |
| 394 | |
Václav Kubernát | abf5280 | 2020-05-19 01:31:17 +0200 | [diff] [blame] | 395 | auto const filterConfigFalse = [] (const Schema& schema, const std::string& path) { |
| 396 | return schema.isConfig(path); |
| 397 | }; |
| 398 | |
Václav Kubernát | 28cf336 | 2020-06-29 17:52:51 +0200 | [diff] [blame] | 399 | struct writableOps_tag; |
| 400 | |
| 401 | PathParser<PathParserMode::DataPath, CompletionMode::Data> const dataPathFilterConfigFalse{filterConfigFalse}; |
| 402 | |
| 403 | struct WritableLeafPath : x3::parser<WritableLeafPath> { |
| 404 | using attribute_type = dataPath_; |
| 405 | template <typename It, typename Ctx, typename RCtx, typename Attr> |
| 406 | static bool parse(It& begin, It end, Ctx const& ctx, RCtx& rctx, Attr& attr) |
| 407 | { |
| 408 | bool res; |
| 409 | if (x3::get<writableOps_tag>(ctx) == WritableOps::Yes) { |
| 410 | res = dataPath.parse(begin, end, ctx, rctx, attr); |
| 411 | } else { |
| 412 | res = dataPathFilterConfigFalse.parse(begin, end, ctx, rctx, attr); |
| 413 | } |
| 414 | if (!res) { |
| 415 | return false; |
| 416 | } |
| 417 | |
| 418 | if (attr.m_nodes.empty() || !std::holds_alternative<leaf_>(attr.m_nodes.back().m_suffix)) { |
| 419 | auto& parserContext = x3::get<parser_context_tag>(ctx); |
| 420 | parserContext.m_errorMsg = "This is not a path to leaf."; |
| 421 | return false; |
| 422 | } |
| 423 | |
| 424 | return true; |
| 425 | } |
| 426 | |
| 427 | } writableLeafPath; |
| 428 | |
Václav Kubernát | aa4250a | 2020-07-22 00:02:23 +0200 | [diff] [blame] | 429 | struct RpcActionPath : x3::parser<RpcActionPath> { |
| 430 | using attribute_type = dataPath_; |
| 431 | template <typename It, typename Ctx, typename RCtx, typename Attr> |
| 432 | static bool parse(It& begin, It end, Ctx const& ctx, RCtx& rctx, Attr& attr) |
| 433 | { |
| 434 | bool res = dataPath.parse(begin, end, ctx, rctx, attr); |
| 435 | if (!res) { |
| 436 | return false; |
| 437 | } |
Václav Kubernát | d0ea9b2 | 2020-04-24 00:44:15 +0200 | [diff] [blame] | 438 | |
Václav Kubernát | aa4250a | 2020-07-22 00:02:23 +0200 | [diff] [blame] | 439 | if (attr.m_nodes.empty() |
| 440 | || (!std::holds_alternative<actionNode_>(attr.m_nodes.back().m_suffix) && !std::holds_alternative<actionNode_>(attr.m_nodes.back().m_suffix))) { |
| 441 | auto& parserContext = x3::get<parser_context_tag>(ctx); |
| 442 | parserContext.m_errorMsg = "This is not a path to an RPC/action."; |
| 443 | return false; |
| 444 | } |
| 445 | |
| 446 | return true; |
| 447 | } |
Václav Kubernát | e7248b2 | 2020-06-26 15:38:59 +0200 | [diff] [blame] | 448 | }; |
| 449 | |
Václav Kubernát | aa4250a | 2020-07-22 00:02:23 +0200 | [diff] [blame] | 450 | auto const rpcActionPath = as<dataPath_>[RpcActionPath()]; |
Václav Kubernát | e7248b2 | 2020-06-26 15:38:59 +0200 | [diff] [blame] | 451 | |
Václav Kubernát | aa4250a | 2020-07-22 00:02:23 +0200 | [diff] [blame] | 452 | auto const noRpcOrAction = [] (const Schema& schema, const std::string& path) { |
| 453 | auto nodeType = schema.nodeType(path); |
| 454 | return nodeType != yang::NodeTypes::Rpc && nodeType != yang::NodeTypes::Action; |
Václav Kubernát | e7248b2 | 2020-06-26 15:38:59 +0200 | [diff] [blame] | 455 | }; |
| 456 | |
| 457 | auto const cdPath_def = |
Václav Kubernát | aa4250a | 2020-07-22 00:02:23 +0200 | [diff] [blame] | 458 | PathParser<PathParserMode::DataPath, CompletionMode::Data>{noRpcOrAction}; |
Václav Kubernát | e7248b2 | 2020-06-26 15:38:59 +0200 | [diff] [blame] | 459 | |
Václav Kubernát | d0ea9b2 | 2020-04-24 00:44:15 +0200 | [diff] [blame] | 460 | auto const presenceContainerPath_def = |
| 461 | dataPath; |
| 462 | |
| 463 | auto const listInstancePath_def = |
| 464 | dataPath; |
| 465 | |
Václav Kubernát | 5b8a8f3 | 2020-05-20 00:57:22 +0200 | [diff] [blame] | 466 | auto const leafListElementPath_def = |
| 467 | dataPath; |
| 468 | |
Václav Kubernát | d0ea9b2 | 2020-04-24 00:44:15 +0200 | [diff] [blame] | 469 | // A "nothing" parser, which is used to indicate we tried to parse a path |
| 470 | auto const initializePath_def = |
| 471 | x3::eps; |
| 472 | |
| 473 | |
| 474 | |
| 475 | #if __clang__ |
| 476 | #pragma GCC diagnostic pop |
| 477 | #endif |
| 478 | |
| 479 | BOOST_SPIRIT_DEFINE(keyValue) |
| 480 | BOOST_SPIRIT_DEFINE(key_identifier) |
Václav Kubernát | d0ea9b2 | 2020-04-24 00:44:15 +0200 | [diff] [blame] | 481 | BOOST_SPIRIT_DEFINE(listSuffix) |
Václav Kubernát | e7248b2 | 2020-06-26 15:38:59 +0200 | [diff] [blame] | 482 | BOOST_SPIRIT_DEFINE(cdPath) |
Václav Kubernát | d0ea9b2 | 2020-04-24 00:44:15 +0200 | [diff] [blame] | 483 | BOOST_SPIRIT_DEFINE(presenceContainerPath) |
| 484 | BOOST_SPIRIT_DEFINE(listInstancePath) |
Václav Kubernát | 5b8a8f3 | 2020-05-20 00:57:22 +0200 | [diff] [blame] | 485 | BOOST_SPIRIT_DEFINE(leafListElementPath) |
Václav Kubernát | d0ea9b2 | 2020-04-24 00:44:15 +0200 | [diff] [blame] | 486 | BOOST_SPIRIT_DEFINE(initializePath) |
| 487 | BOOST_SPIRIT_DEFINE(createKeySuggestions) |
Václav Kubernát | d0ea9b2 | 2020-04-24 00:44:15 +0200 | [diff] [blame] | 488 | BOOST_SPIRIT_DEFINE(createValueSuggestions) |
| 489 | BOOST_SPIRIT_DEFINE(suggestKeysEnd) |
Václav Kubernát | 5b8a8f3 | 2020-05-20 00:57:22 +0200 | [diff] [blame] | 490 | BOOST_SPIRIT_DEFINE(leafListValue) |
Václav Kubernát | d0ea9b2 | 2020-04-24 00:44:15 +0200 | [diff] [blame] | 491 | BOOST_SPIRIT_DEFINE(absoluteStart) |
| 492 | BOOST_SPIRIT_DEFINE(trailingSlash) |