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