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