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