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