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