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