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