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