Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 CESNET, https://photonics.cesnet.cz/ |
| 3 | * Copyright (C) 2018 FIT CVUT, https://fit.cvut.cz/ |
| 4 | * |
| 5 | * Written by Václav Kubernát <kubervac@fit.cvut.cz> |
| 6 | * |
| 7 | */ |
| 8 | |
| 9 | #pragma once |
Václav Kubernát | 195eeea | 2018-05-18 13:52:36 +0200 | [diff] [blame] | 10 | |
Václav Kubernát | 5727242 | 2019-02-08 12:48:24 +0100 | [diff] [blame] | 11 | #include <boost/mpl/for_each.hpp> |
Václav Kubernát | 509ce65 | 2019-05-29 19:46:44 +0200 | [diff] [blame] | 12 | #include <boost/spirit/home/x3.hpp> |
| 13 | #include <boost/spirit/home/x3/support/utility/error_reporting.hpp> |
| 14 | |
| 15 | |
Václav Kubernát | 5727242 | 2019-02-08 12:48:24 +0100 | [diff] [blame] | 16 | #include "ast_commands.hpp" |
Václav Kubernát | 4294a85 | 2020-02-14 15:07:14 +0100 | [diff] [blame] | 17 | #include "parser_context.hpp" |
Václav Kubernát | 48fc383 | 2018-05-28 14:21:22 +0200 | [diff] [blame] | 18 | #include "schema.hpp" |
Václav Kubernát | ebca255 | 2018-06-08 19:06:02 +0200 | [diff] [blame] | 19 | #include "utils.hpp" |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 20 | namespace x3 = boost::spirit::x3; |
| 21 | |
| 22 | struct parser_context_tag; |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 23 | |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 24 | struct keyValue_class { |
| 25 | template <typename T, typename Iterator, typename Context> |
| 26 | void on_success(Iterator const&, Iterator const&, T& ast, Context const& context) |
| 27 | { |
| 28 | auto& parserContext = x3::get<parser_context_tag>(context); |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 29 | |
| 30 | if (parserContext.m_tmpListKeys.find(ast.first) != parserContext.m_tmpListKeys.end()) { |
| 31 | _pass(context) = false; |
| 32 | parserContext.m_errorMsg = "Key \"" + ast.first + "\" was entered more than once."; |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame] | 33 | } else { |
Václav Kubernát | 43908fb | 2020-01-02 19:05:51 +0100 | [diff] [blame] | 34 | parserContext.m_tmpListKeys.insert({ast.first, ast.second}); |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 35 | } |
| 36 | } |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame] | 37 | |
| 38 | template <typename Iterator, typename Exception, typename Context> |
| 39 | x3::error_handler_result on_error(Iterator&, Iterator const&, Exception const&, Context const& context) |
| 40 | { |
| 41 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 42 | parserContext.m_errorMsg = "Error parsing key values here:"; |
| 43 | return x3::error_handler_result::rethrow; |
| 44 | } |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 45 | }; |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame] | 46 | |
Václav Kubernát | f3e377b | 2020-05-28 23:26:38 +0200 | [diff] [blame] | 47 | struct node_identifier_class; |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 48 | |
Václav Kubernát | 7707cae | 2020-01-16 12:04:53 +0100 | [diff] [blame] | 49 | struct key_identifier_class { |
| 50 | template <typename T, typename Iterator, typename Context> |
| 51 | void on_success(Iterator const&, Iterator const&, T& ast, Context const& context) |
| 52 | { |
| 53 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 54 | const Schema& schema = parserContext.m_schema; |
| 55 | schemaPath_ location = parserContext.currentSchemaPath(); |
| 56 | ModuleNodePair list{parserContext.m_curModule, parserContext.m_tmpListName}; |
| 57 | |
| 58 | if (schema.listHasKey(location, list, ast)) { |
| 59 | schemaNode_ listNode; |
Václav Kubernát | 43908fb | 2020-01-02 19:05:51 +0100 | [diff] [blame] | 60 | listNode.m_prefix = parserContext.m_curModule.flat_map([] (auto mod) { return boost::optional<module_>{{mod}}; });; |
Václav Kubernát | 7707cae | 2020-01-16 12:04:53 +0100 | [diff] [blame] | 61 | listNode.m_suffix = list_{parserContext.m_tmpListName}; |
| 62 | location.m_nodes.push_back(listNode); |
| 63 | parserContext.m_tmpListKeyLeafPath.m_location = location; |
Václav Kubernát | 43908fb | 2020-01-02 19:05:51 +0100 | [diff] [blame] | 64 | parserContext.m_tmpListKeyLeafPath.m_node = { parserContext.m_curModule, ast }; |
Václav Kubernát | 7707cae | 2020-01-16 12:04:53 +0100 | [diff] [blame] | 65 | } else { |
| 66 | parserContext.m_errorMsg = parserContext.m_tmpListName + " is not indexed by \"" + ast + "\"."; |
| 67 | _pass(context) = false; |
| 68 | } |
| 69 | } |
| 70 | }; |
Václav Kubernát | 89728d8 | 2018-09-13 16:28:28 +0200 | [diff] [blame] | 71 | |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 72 | struct module_identifier_class; |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 73 | |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 74 | struct listSuffix_class { |
| 75 | template <typename T, typename Iterator, typename Context> |
| 76 | void on_success(Iterator const&, Iterator const&, T& ast, Context const& context) |
| 77 | { |
| 78 | auto& parserContext = x3::get<parser_context_tag>(context); |
Václav Kubernát | 48fc383 | 2018-05-28 14:21:22 +0200 | [diff] [blame] | 79 | const Schema& schema = parserContext.m_schema; |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 80 | |
Václav Kubernát | 72749c6 | 2020-01-03 16:47:34 +0100 | [diff] [blame] | 81 | const auto& keysNeeded = schema.listKeys(parserContext.currentSchemaPath(), {parserContext.m_curModule, parserContext.m_tmpListName}); |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 82 | std::set<std::string> keysSupplied; |
| 83 | for (const auto& it : ast) |
| 84 | keysSupplied.insert(it.first); |
| 85 | |
| 86 | if (keysNeeded != keysSupplied) { |
| 87 | parserContext.m_errorMsg = "Not enough keys for " + parserContext.m_tmpListName + ". " + |
| 88 | "These keys were not supplied:"; |
| 89 | std::set<std::string> missingKeys; |
| 90 | std::set_difference(keysNeeded.begin(), keysNeeded.end(), |
| 91 | keysSupplied.begin(), keysSupplied.end(), |
| 92 | std::inserter(missingKeys, missingKeys.end())); |
| 93 | |
| 94 | for (const auto& it : missingKeys) |
| 95 | parserContext.m_errorMsg += " " + it; |
| 96 | parserContext.m_errorMsg += "."; |
| 97 | |
| 98 | _pass(context) = false; |
| 99 | } |
| 100 | } |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame] | 101 | |
| 102 | template <typename Iterator, typename Exception, typename Context> |
| 103 | x3::error_handler_result on_error(Iterator&, Iterator const&, Exception const&, Context const& context) |
| 104 | { |
| 105 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 106 | if (parserContext.m_errorMsg.empty()) |
| 107 | parserContext.m_errorMsg = "Expecting ']' here:"; |
| 108 | return x3::error_handler_result::rethrow; |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame] | 109 | } |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 110 | }; |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 111 | |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 112 | struct module_class { |
| 113 | template <typename T, typename Iterator, typename Context> |
| 114 | void on_success(Iterator const&, Iterator const&, T& ast, Context const& context) |
| 115 | { |
| 116 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 117 | const auto& schema = parserContext.m_schema; |
| 118 | |
Václav Kubernát | 1bcee3b | 2020-05-28 22:19:59 +0200 | [diff] [blame^] | 119 | if (!schema.isModule(ast.m_name)) { |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 120 | parserContext.m_errorMsg = "Invalid module name."; |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 121 | _pass(context) = false; |
| 122 | } |
| 123 | } |
| 124 | }; |
| 125 | |
Václav Kubernát | 37171a1 | 2018-08-31 17:01:48 +0200 | [diff] [blame] | 126 | struct absoluteStart_class { |
| 127 | template <typename T, typename Iterator, typename Context> |
| 128 | void on_success(Iterator const&, Iterator const&, T&, Context const& context) |
| 129 | { |
| 130 | auto& parserContext = x3::get<parser_context_tag>(context); |
Václav Kubernát | 72749c6 | 2020-01-03 16:47:34 +0100 | [diff] [blame] | 131 | parserContext.clearPath(); |
Václav Kubernát | 37171a1 | 2018-08-31 17:01:48 +0200 | [diff] [blame] | 132 | } |
| 133 | }; |
| 134 | |
Václav Kubernát | 6d79143 | 2018-10-25 16:00:35 +0200 | [diff] [blame] | 135 | struct discard_class; |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 136 | |
Václav Kubernát | 11afac7 | 2018-07-18 14:59:53 +0200 | [diff] [blame] | 137 | struct ls_class; |
| 138 | |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 139 | struct cd_class { |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 140 | template <typename Iterator, typename Exception, typename Context> |
| 141 | x3::error_handler_result on_error(Iterator&, Iterator const&, Exception const& x, Context const& context) |
| 142 | { |
| 143 | auto& parserContext = x3::get<parser_context_tag>(context); |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame] | 144 | if (parserContext.m_errorMsg.empty()) |
| 145 | parserContext.m_errorMsg = "Expected " + x.which() + " here:"; |
| 146 | return x3::error_handler_result::rethrow; |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 147 | } |
| 148 | }; |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 149 | |
Václav Kubernát | 6797df0 | 2019-03-18 20:21:50 +0100 | [diff] [blame] | 150 | struct presenceContainerPath_class { |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 151 | template <typename T, typename Iterator, typename Context> |
| 152 | void on_success(Iterator const&, Iterator const&, T& ast, Context const& context) |
| 153 | { |
| 154 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 155 | const auto& schema = parserContext.m_schema; |
Václav Kubernát | 0d42c51 | 2020-05-20 21:18:39 +0200 | [diff] [blame] | 156 | if (ast.m_nodes.empty()) { |
| 157 | parserContext.m_errorMsg = "This container is not a presence container."; |
| 158 | _pass(context) = false; |
| 159 | return; |
| 160 | } |
| 161 | |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 162 | try { |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 163 | boost::optional<std::string> module; |
Václav Kubernát | 6797df0 | 2019-03-18 20:21:50 +0100 | [diff] [blame] | 164 | if (ast.m_nodes.back().m_prefix) |
| 165 | module = ast.m_nodes.back().m_prefix.value().m_name; |
Václav Kubernát | b5ca154 | 2020-05-27 01:03:54 +0200 | [diff] [blame] | 166 | container_ cont = std::get<container_>(ast.m_nodes.back().m_suffix); |
Václav Kubernát | 72749c6 | 2020-01-03 16:47:34 +0100 | [diff] [blame] | 167 | auto location = pathWithoutLastNode(parserContext.currentSchemaPath()); |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 168 | |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 169 | if (!schema.isPresenceContainer(location, {module, cont.m_name})) { |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame] | 170 | parserContext.m_errorMsg = "This container is not a presence container."; |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 171 | _pass(context) = false; |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 172 | } |
Václav Kubernát | b5ca154 | 2020-05-27 01:03:54 +0200 | [diff] [blame] | 173 | } catch (std::bad_variant_access&) { |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame] | 174 | parserContext.m_errorMsg = "This is not a container."; |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 175 | _pass(context) = false; |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 176 | } |
| 177 | } |
Václav Kubernát | 6797df0 | 2019-03-18 20:21:50 +0100 | [diff] [blame] | 178 | }; |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 179 | |
Václav Kubernát | f5f64f0 | 2019-03-19 17:15:47 +0100 | [diff] [blame] | 180 | struct listInstancePath_class { |
| 181 | template <typename T, typename Iterator, typename Context> |
| 182 | void on_success(Iterator const&, Iterator const&, T& ast, Context const& context) |
| 183 | { |
| 184 | auto& parserContext = x3::get<parser_context_tag>(context); |
Václav Kubernát | 2ebab1d | 2020-05-27 01:28:30 +0200 | [diff] [blame] | 185 | if (ast.m_nodes.empty() || !std::holds_alternative<listElement_>(ast.m_nodes.back().m_suffix)) { |
Václav Kubernát | f5f64f0 | 2019-03-19 17:15:47 +0100 | [diff] [blame] | 186 | parserContext.m_errorMsg = "This is not a list instance."; |
| 187 | _pass(context) = false; |
| 188 | } |
| 189 | } |
| 190 | }; |
| 191 | |
Václav Kubernát | 5b8a8f3 | 2020-05-20 00:57:22 +0200 | [diff] [blame] | 192 | struct leafListElementPath_class { |
| 193 | template <typename T, typename Iterator, typename Context> |
| 194 | void on_success(Iterator const&, Iterator const&, T& ast, Context const& context) |
| 195 | { |
| 196 | auto& parserContext = x3::get<parser_context_tag>(context); |
Václav Kubernát | b5ca154 | 2020-05-27 01:03:54 +0200 | [diff] [blame] | 197 | if (ast.m_nodes.empty() || !std::holds_alternative<leafListElement_>(ast.m_nodes.back().m_suffix)) { |
Václav Kubernát | 5b8a8f3 | 2020-05-20 00:57:22 +0200 | [diff] [blame] | 198 | parserContext.m_errorMsg = "This is not a leaf list element."; |
| 199 | _pass(context) = false; |
| 200 | } |
| 201 | } |
| 202 | }; |
| 203 | |
Václav Kubernát | 8028f94 | 2019-09-25 16:03:23 +0200 | [diff] [blame] | 204 | struct space_separator_class { |
| 205 | template <typename T, typename Iterator, typename Context> |
| 206 | void on_success(Iterator const&, Iterator const&, T&, Context const& context) |
| 207 | { |
| 208 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 209 | parserContext.m_suggestions.clear(); |
Václav Kubernát | 1ed4aa3 | 2020-01-23 13:13:28 +0100 | [diff] [blame] | 210 | parserContext.m_completionIterator = boost::none; |
Václav Kubernát | 8028f94 | 2019-09-25 16:03:23 +0200 | [diff] [blame] | 211 | } |
| 212 | }; |
| 213 | |
Václav Kubernát | 6797df0 | 2019-03-18 20:21:50 +0100 | [diff] [blame] | 214 | struct create_class { |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 215 | template <typename Iterator, typename Exception, typename Context> |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame] | 216 | x3::error_handler_result on_error(Iterator&, Iterator const&, Exception const&, Context const& context) |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 217 | { |
| 218 | auto& parserContext = x3::get<parser_context_tag>(context); |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame] | 219 | if (parserContext.m_errorMsg.empty()) |
| 220 | parserContext.m_errorMsg = "Couldn't parse create/delete command."; |
| 221 | return x3::error_handler_result::rethrow; |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 222 | } |
| 223 | }; |
| 224 | |
Václav Kubernát | 6797df0 | 2019-03-18 20:21:50 +0100 | [diff] [blame] | 225 | struct delete_class { |
| 226 | template <typename Iterator, typename Exception, typename Context> |
| 227 | x3::error_handler_result on_error(Iterator&, Iterator const&, Exception const&, Context const& context) |
| 228 | { |
| 229 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 230 | if (parserContext.m_errorMsg.empty()) |
| 231 | parserContext.m_errorMsg = "Couldn't parse create/delete command."; |
| 232 | return x3::error_handler_result::rethrow; |
| 233 | } |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 234 | }; |
| 235 | |
Václav Kubernát | abf5280 | 2020-05-19 01:31:17 +0200 | [diff] [blame] | 236 | struct writable_leaf_path_class { |
Václav Kubernát | 0b0272f | 2018-06-13 14:13:08 +0200 | [diff] [blame] | 237 | template <typename T, typename Iterator, typename Context> |
Václav Kubernát | 7707cae | 2020-01-16 12:04:53 +0100 | [diff] [blame] | 238 | void on_success(Iterator const&, Iterator const&, T&, Context const& context) |
Václav Kubernát | 0b0272f | 2018-06-13 14:13:08 +0200 | [diff] [blame] | 239 | { |
| 240 | auto& parserContext = x3::get<parser_context_tag>(context); |
Václav Kubernát | 0d42c51 | 2020-05-20 21:18:39 +0200 | [diff] [blame] | 241 | if (parserContext.currentSchemaPath().m_nodes.empty()) { |
| 242 | parserContext.m_errorMsg = "This is not a path to leaf."; |
| 243 | _pass(context) = false; |
| 244 | return; |
| 245 | } |
| 246 | |
Václav Kubernát | 0b0272f | 2018-06-13 14:13:08 +0200 | [diff] [blame] | 247 | try { |
Václav Kubernát | 7707cae | 2020-01-16 12:04:53 +0100 | [diff] [blame] | 248 | auto lastNode = parserContext.currentSchemaPath().m_nodes.back(); |
Václav Kubernát | b5ca154 | 2020-05-27 01:03:54 +0200 | [diff] [blame] | 249 | auto leaf = std::get<leaf_>(lastNode.m_suffix); |
Václav Kubernát | 7707cae | 2020-01-16 12:04:53 +0100 | [diff] [blame] | 250 | auto location = pathWithoutLastNode(parserContext.currentSchemaPath()); |
| 251 | ModuleNodePair node{lastNode.m_prefix.flat_map([](const auto& it) { return boost::optional<std::string>{it.m_name}; }), leaf.m_name}; |
| 252 | |
| 253 | parserContext.m_tmpListKeyLeafPath.m_location = location; |
| 254 | parserContext.m_tmpListKeyLeafPath.m_node = node; |
| 255 | |
Václav Kubernát | b5ca154 | 2020-05-27 01:03:54 +0200 | [diff] [blame] | 256 | } catch (std::bad_variant_access&) { |
Václav Kubernát | 0b0272f | 2018-06-13 14:13:08 +0200 | [diff] [blame] | 257 | parserContext.m_errorMsg = "This is not a path to leaf."; |
| 258 | _pass(context) = false; |
| 259 | } |
| 260 | } |
| 261 | }; |
| 262 | |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 263 | struct set_class { |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 264 | template <typename Iterator, typename Exception, typename Context> |
| 265 | x3::error_handler_result on_error(Iterator&, Iterator const&, Exception const& x, Context const& context) |
| 266 | { |
| 267 | auto& parserContext = x3::get<parser_context_tag>(context); |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame] | 268 | if (parserContext.m_errorMsg.empty()) |
| 269 | parserContext.m_errorMsg = "Expected " + x.which() + " here:"; |
| 270 | return x3::error_handler_result::rethrow; |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 271 | } |
| 272 | }; |
| 273 | |
Václav Kubernát | 812ee28 | 2018-08-30 17:10:03 +0200 | [diff] [blame] | 274 | struct commit_class; |
| 275 | |
Václav Kubernát | 9cfcd87 | 2020-02-18 12:34:02 +0100 | [diff] [blame] | 276 | struct describe_class; |
| 277 | |
Václav Kubernát | 054cc99 | 2019-02-21 14:23:52 +0100 | [diff] [blame] | 278 | struct help_class; |
| 279 | |
Václav Kubernát | b6ff0b6 | 2018-08-30 16:14:53 +0200 | [diff] [blame] | 280 | struct get_class; |
| 281 | |
Václav Kubernát | 7160a13 | 2020-04-03 02:11:01 +0200 | [diff] [blame] | 282 | struct copy_class; |
| 283 | |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 284 | struct command_class { |
| 285 | template <typename Iterator, typename Exception, typename Context> |
| 286 | x3::error_handler_result on_error(Iterator&, Iterator const&, Exception const& x, Context const& context) |
| 287 | { |
| 288 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 289 | auto& error_handler = x3::get<x3::error_handler_tag>(context).get(); |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame] | 290 | if (parserContext.m_errorMsg.empty()) { |
| 291 | parserContext.m_errorMsg = "Unknown command."; |
| 292 | } |
| 293 | error_handler(x.where(), parserContext.m_errorMsg); |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 294 | return x3::error_handler_result::fail; |
| 295 | } |
| 296 | }; |
Václav Kubernát | 5c75b25 | 2018-10-10 18:33:47 +0200 | [diff] [blame] | 297 | |
Václav Kubernát | 4108e0d | 2018-10-29 13:32:22 +0100 | [diff] [blame] | 298 | struct initializePath_class { |
Václav Kubernát | 5c75b25 | 2018-10-10 18:33:47 +0200 | [diff] [blame] | 299 | template <typename T, typename Iterator, typename Context> |
| 300 | void on_success(Iterator const&, Iterator const&, T&, Context const& context) |
| 301 | { |
| 302 | auto& parserContext = x3::get<parser_context_tag>(context); |
Václav Kubernát | 72749c6 | 2020-01-03 16:47:34 +0100 | [diff] [blame] | 303 | parserContext.resetPath(); |
Václav Kubernát | 5c75b25 | 2018-10-10 18:33:47 +0200 | [diff] [blame] | 304 | parserContext.m_tmpListKeys.clear(); |
| 305 | parserContext.m_tmpListName.clear(); |
Václav Kubernát | 4108e0d | 2018-10-29 13:32:22 +0100 | [diff] [blame] | 306 | parserContext.m_suggestions.clear(); |
Václav Kubernát | 5c75b25 | 2018-10-10 18:33:47 +0200 | [diff] [blame] | 307 | } |
| 308 | }; |
Václav Kubernát | d6fd249 | 2018-11-19 15:11:16 +0100 | [diff] [blame] | 309 | |
| 310 | struct trailingSlash_class; |
Václav Kubernát | 4108e0d | 2018-10-29 13:32:22 +0100 | [diff] [blame] | 311 | |
Václav Kubernát | cb3af40 | 2020-02-12 16:49:17 +0100 | [diff] [blame] | 312 | std::set<Completion> generateMissingKeyCompletionSet(std::set<std::string> keysNeeded, std::map<std::string, leaf_data_> currentSet); |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 313 | |
| 314 | struct createKeySuggestions_class { |
| 315 | template <typename T, typename Iterator, typename Context> |
| 316 | void on_success(Iterator const& begin, Iterator const&, T&, Context const& context) |
| 317 | { |
| 318 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 319 | const auto& schema = parserContext.m_schema; |
| 320 | |
| 321 | parserContext.m_completionIterator = begin; |
| 322 | |
Václav Kubernát | 72749c6 | 2020-01-03 16:47:34 +0100 | [diff] [blame] | 323 | const auto& keysNeeded = schema.listKeys(parserContext.currentSchemaPath(), {parserContext.m_curModule, parserContext.m_tmpListName}); |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 324 | parserContext.m_suggestions = generateMissingKeyCompletionSet(keysNeeded, parserContext.m_tmpListKeys); |
| 325 | } |
| 326 | }; |
| 327 | |
Václav Kubernát | 43908fb | 2020-01-02 19:05:51 +0100 | [diff] [blame] | 328 | std::string leafDataToCompletion(const leaf_data_& value); |
| 329 | |
| 330 | struct createValueSuggestions_class { |
| 331 | template <typename T, typename Iterator, typename Context> |
| 332 | void on_success(Iterator const& begin, Iterator const&, T&, Context const& context) |
| 333 | { |
| 334 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 335 | if (!parserContext.m_completing) { |
| 336 | return; |
| 337 | } |
| 338 | const auto& dataQuery = parserContext.m_dataquery; |
| 339 | |
| 340 | parserContext.m_completionIterator = begin; |
| 341 | auto listInstances = dataQuery->listKeys(parserContext.currentDataPath(), {parserContext.m_curModule, parserContext.m_tmpListName}); |
| 342 | |
| 343 | decltype(listInstances) filteredInstances; |
| 344 | //This filters out instances, which don't correspond to the partial instance we have. |
| 345 | const auto partialFitsComplete = [&parserContext] (const auto& complete) { |
| 346 | const auto& partial = parserContext.m_tmpListKeys; |
| 347 | return std::all_of(partial.begin(), partial.end(), [&complete] (const auto& oneKV) { |
| 348 | const auto& [k, v] = oneKV; |
| 349 | return complete.at(k) == v; |
| 350 | }); |
| 351 | }; |
| 352 | std::copy_if(listInstances.begin(), listInstances.end(), std::inserter(filteredInstances, filteredInstances.end()), partialFitsComplete); |
| 353 | |
Václav Kubernát | cb3af40 | 2020-02-12 16:49:17 +0100 | [diff] [blame] | 354 | std::set<Completion> validValues; |
Václav Kubernát | 43908fb | 2020-01-02 19:05:51 +0100 | [diff] [blame] | 355 | |
Václav Kubernát | cb3af40 | 2020-02-12 16:49:17 +0100 | [diff] [blame] | 356 | std::transform(filteredInstances.begin(), filteredInstances.end(), std::inserter(validValues, validValues.end()), [&parserContext](const auto& instance) { |
| 357 | return Completion{leafDataToCompletion(instance.at(parserContext.m_tmpListKeyLeafPath.m_node.second))}; |
Václav Kubernát | 43908fb | 2020-01-02 19:05:51 +0100 | [diff] [blame] | 358 | }); |
| 359 | |
| 360 | parserContext.m_suggestions = validValues; |
| 361 | } |
| 362 | }; |
| 363 | |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 364 | struct suggestKeysEnd_class { |
| 365 | template <typename T, typename Iterator, typename Context> |
| 366 | void on_success(Iterator const& begin, Iterator const&, T&, Context const& context) |
| 367 | { |
| 368 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 369 | const auto& schema = parserContext.m_schema; |
| 370 | |
| 371 | parserContext.m_completionIterator = begin; |
Václav Kubernát | 72749c6 | 2020-01-03 16:47:34 +0100 | [diff] [blame] | 372 | const auto& keysNeeded = schema.listKeys(parserContext.currentSchemaPath(), {parserContext.m_curModule, parserContext.m_tmpListName}); |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 373 | if (generateMissingKeyCompletionSet(keysNeeded, parserContext.m_tmpListKeys).empty()) { |
Václav Kubernát | cb3af40 | 2020-02-12 16:49:17 +0100 | [diff] [blame] | 374 | parserContext.m_suggestions = {Completion{"]/"}}; |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 375 | } else { |
Václav Kubernát | cb3af40 | 2020-02-12 16:49:17 +0100 | [diff] [blame] | 376 | parserContext.m_suggestions = {Completion{"]["}}; |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 377 | } |
| 378 | } |
| 379 | }; |
Václav Kubernát | 5727242 | 2019-02-08 12:48:24 +0100 | [diff] [blame] | 380 | |
Václav Kubernát | 672889c | 2020-05-27 04:11:36 +0200 | [diff] [blame] | 381 | template <typename T> |
| 382 | std::string commandNamesVisitor (boost::type<T>) |
| 383 | { |
| 384 | return T::name; |
| 385 | } |
Václav Kubernát | 5727242 | 2019-02-08 12:48:24 +0100 | [diff] [blame] | 386 | |
| 387 | struct createCommandSuggestions_class { |
| 388 | template <typename T, typename Iterator, typename Context> |
| 389 | void on_success(Iterator const& begin, Iterator const&, T&, Context const& context) |
| 390 | { |
| 391 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 392 | parserContext.m_completionIterator = begin; |
| 393 | |
| 394 | parserContext.m_suggestions.clear(); |
| 395 | boost::mpl::for_each<CommandTypes, boost::type<boost::mpl::_>>([&parserContext](auto cmd) { |
Václav Kubernát | 672889c | 2020-05-27 04:11:36 +0200 | [diff] [blame] | 396 | parserContext.m_suggestions.insert({commandNamesVisitor(cmd), " "}); |
Václav Kubernát | 5727242 | 2019-02-08 12:48:24 +0100 | [diff] [blame] | 397 | }); |
| 398 | } |
| 399 | }; |
Václav Kubernát | ac035d6 | 2019-02-18 10:59:08 +0100 | [diff] [blame] | 400 | |
| 401 | struct completing_class { |
| 402 | template <typename T, typename Iterator, typename Context> |
| 403 | void on_success(Iterator const&, Iterator const&, T&, Context const& context) |
| 404 | { |
| 405 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 406 | |
| 407 | if (!parserContext.m_completing) |
| 408 | _pass(context) = false; |
| 409 | } |
| 410 | }; |