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