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