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" |
Jan Kundrát | 07ca392 | 2023-06-20 19:03:56 +0200 | [diff] [blame] | 19 | |
| 20 | // Up to Boost 1.77, calling `_pass(context) = false` was enough. |
| 21 | // Since 1.78 it became necessary to explicitly reset the input iterator, but unfortunately the function signature |
| 22 | // was wrong and the whole handler would just be ignored. |
| 23 | // That was fixed in 1.80. |
| 24 | // FIXME: change the function signature and remove the const_cast once we're unconditionally on 1.80+ |
| 25 | #define REJECT_PARSING do { const_cast<Iterator&>(after) = before; _pass(context) = false; } while (0) |
Václav Kubernát | ebca255 | 2018-06-08 19:06:02 +0200 | [diff] [blame] | 26 | #include "utils.hpp" |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 27 | namespace x3 = boost::spirit::x3; |
| 28 | |
| 29 | struct parser_context_tag; |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 30 | |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 31 | struct keyValue_class { |
| 32 | template <typename T, typename Iterator, typename Context> |
Jan Kundrát | 07ca392 | 2023-06-20 19:03:56 +0200 | [diff] [blame] | 33 | void on_success(Iterator const& before, Iterator const& after, T& ast, Context const& context) |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 34 | { |
| 35 | auto& parserContext = x3::get<parser_context_tag>(context); |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 36 | |
| 37 | if (parserContext.m_tmpListKeys.find(ast.first) != parserContext.m_tmpListKeys.end()) { |
Jan Kundrát | 07ca392 | 2023-06-20 19:03:56 +0200 | [diff] [blame] | 38 | REJECT_PARSING; |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 39 | 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] | 40 | } else { |
Václav Kubernát | 43908fb | 2020-01-02 19:05:51 +0100 | [diff] [blame] | 41 | parserContext.m_tmpListKeys.insert({ast.first, ast.second}); |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 42 | } |
| 43 | } |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame] | 44 | |
| 45 | template <typename Iterator, typename Exception, typename Context> |
| 46 | x3::error_handler_result on_error(Iterator&, Iterator const&, Exception const&, Context const& context) |
| 47 | { |
| 48 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 49 | parserContext.m_errorMsg = "Error parsing key values here:"; |
| 50 | return x3::error_handler_result::rethrow; |
| 51 | } |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 52 | }; |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame] | 53 | |
Václav Kubernát | 2db124c | 2020-05-28 21:58:36 +0200 | [diff] [blame] | 54 | boost::optional<std::string> optModuleToOptString(const boost::optional<module_> module); |
| 55 | |
Václav Kubernát | 7707cae | 2020-01-16 12:04:53 +0100 | [diff] [blame] | 56 | struct key_identifier_class { |
| 57 | template <typename T, typename Iterator, typename Context> |
Jan Kundrát | 07ca392 | 2023-06-20 19:03:56 +0200 | [diff] [blame] | 58 | void on_success(Iterator const& before, Iterator const& after, T& ast, Context const& context) |
Václav Kubernát | 7707cae | 2020-01-16 12:04:53 +0100 | [diff] [blame] | 59 | { |
Václav Kubernát | 2db124c | 2020-05-28 21:58:36 +0200 | [diff] [blame] | 60 | ParserContext& parserContext = x3::get<parser_context_tag>(context); |
Václav Kubernát | 7707cae | 2020-01-16 12:04:53 +0100 | [diff] [blame] | 61 | const Schema& schema = parserContext.m_schema; |
Václav Kubernát | 7707cae | 2020-01-16 12:04:53 +0100 | [diff] [blame] | 62 | |
Václav Kubernát | 2db124c | 2020-05-28 21:58:36 +0200 | [diff] [blame] | 63 | if (schema.listHasKey(dataPathToSchemaPath(parserContext.m_tmpListPath), ast)) { |
| 64 | parserContext.m_tmpListKeyLeafPath.m_location = dataPathToSchemaPath(parserContext.m_tmpListPath); |
Václav Kubernát | b4e5b18 | 2020-11-16 19:55:09 +0100 | [diff] [blame] | 65 | 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] | 66 | } else { |
Václav Kubernát | 2db124c | 2020-05-28 21:58:36 +0200 | [diff] [blame] | 67 | auto listName = std::get<list_>(parserContext.m_tmpListPath.m_nodes.back().m_suffix).m_name; |
| 68 | parserContext.m_errorMsg = listName + " is not indexed by \"" + ast + "\"."; |
Jan Kundrát | 07ca392 | 2023-06-20 19:03:56 +0200 | [diff] [blame] | 69 | REJECT_PARSING; |
Václav Kubernát | 7707cae | 2020-01-16 12:04:53 +0100 | [diff] [blame] | 70 | } |
| 71 | } |
| 72 | }; |
Václav Kubernát | 89728d8 | 2018-09-13 16:28:28 +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> |
Jan Kundrát | 07ca392 | 2023-06-20 19:03:56 +0200 | [diff] [blame] | 76 | void on_success(Iterator const& before, Iterator const& after, T& ast, Context const& context) |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 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 | |
Jan Kundrát | 07ca392 | 2023-06-20 19:03:56 +0200 | [diff] [blame] | 101 | REJECT_PARSING; |
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> |
Jan Kundrát | 07ca392 | 2023-06-20 19:03:56 +0200 | [diff] [blame] | 122 | void on_success(Iterator const& before, Iterator const& after, T& ast, Context const& context) |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 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."; |
Jan Kundrát | 07ca392 | 2023-06-20 19:03:56 +0200 | [diff] [blame] | 129 | REJECT_PARSING; |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 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 | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 143 | struct cd_class { |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 144 | template <typename Iterator, typename Exception, typename Context> |
| 145 | x3::error_handler_result on_error(Iterator&, Iterator const&, Exception const& x, Context const& context) |
| 146 | { |
| 147 | auto& parserContext = x3::get<parser_context_tag>(context); |
Václav Kubernát | 3a43323 | 2020-07-08 17:52:50 +0200 | [diff] [blame] | 148 | if (parserContext.m_errorMsg.empty()) { |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame] | 149 | parserContext.m_errorMsg = "Expected " + x.which() + " here:"; |
Václav Kubernát | 3a43323 | 2020-07-08 17:52:50 +0200 | [diff] [blame] | 150 | } |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame] | 151 | return x3::error_handler_result::rethrow; |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 152 | } |
| 153 | }; |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 154 | |
Václav Kubernát | 6797df0 | 2019-03-18 20:21:50 +0100 | [diff] [blame] | 155 | struct presenceContainerPath_class { |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 156 | template <typename T, typename Iterator, typename Context> |
Jan Kundrát | 07ca392 | 2023-06-20 19:03:56 +0200 | [diff] [blame] | 157 | void on_success(Iterator const& before, Iterator const& after, T& ast, Context const& context) |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 158 | { |
| 159 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 160 | const auto& schema = parserContext.m_schema; |
Václav Kubernát | 0d42c51 | 2020-05-20 21:18:39 +0200 | [diff] [blame] | 161 | if (ast.m_nodes.empty()) { |
| 162 | parserContext.m_errorMsg = "This container is not a presence container."; |
Jan Kundrát | 07ca392 | 2023-06-20 19:03:56 +0200 | [diff] [blame] | 163 | REJECT_PARSING; |
Václav Kubernát | 0d42c51 | 2020-05-20 21:18:39 +0200 | [diff] [blame] | 164 | return; |
| 165 | } |
| 166 | |
Václav Kubernát | e811bfa | 2020-05-29 02:25:20 +0200 | [diff] [blame] | 167 | if (schema.nodeType(pathToSchemaString(parserContext.currentSchemaPath(), Prefixes::Always)) != yang::NodeTypes::PresenceContainer) { |
| 168 | parserContext.m_errorMsg = "This container is not a presence container."; |
Jan Kundrát | 07ca392 | 2023-06-20 19:03:56 +0200 | [diff] [blame] | 169 | REJECT_PARSING; |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 170 | } |
| 171 | } |
Václav Kubernát | 6797df0 | 2019-03-18 20:21:50 +0100 | [diff] [blame] | 172 | }; |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 173 | |
Václav Kubernát | f5f64f0 | 2019-03-19 17:15:47 +0100 | [diff] [blame] | 174 | struct listInstancePath_class { |
| 175 | template <typename T, typename Iterator, typename Context> |
Jan Kundrát | 07ca392 | 2023-06-20 19:03:56 +0200 | [diff] [blame] | 176 | void on_success(Iterator const& before, Iterator const& after, T& ast, Context const& context) |
Václav Kubernát | f5f64f0 | 2019-03-19 17:15:47 +0100 | [diff] [blame] | 177 | { |
| 178 | auto& parserContext = x3::get<parser_context_tag>(context); |
Václav Kubernát | 2ebab1d | 2020-05-27 01:28:30 +0200 | [diff] [blame] | 179 | 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] | 180 | parserContext.m_errorMsg = "This is not a list instance."; |
Jan Kundrát | 07ca392 | 2023-06-20 19:03:56 +0200 | [diff] [blame] | 181 | REJECT_PARSING; |
Václav Kubernát | f5f64f0 | 2019-03-19 17:15:47 +0100 | [diff] [blame] | 182 | } |
| 183 | } |
| 184 | }; |
| 185 | |
Václav Kubernát | 5b8a8f3 | 2020-05-20 00:57:22 +0200 | [diff] [blame] | 186 | struct leafListElementPath_class { |
| 187 | template <typename T, typename Iterator, typename Context> |
Jan Kundrát | 07ca392 | 2023-06-20 19:03:56 +0200 | [diff] [blame] | 188 | void on_success(Iterator const& before, Iterator const& after, T& ast, Context const& context) |
Václav Kubernát | 5b8a8f3 | 2020-05-20 00:57:22 +0200 | [diff] [blame] | 189 | { |
| 190 | auto& parserContext = x3::get<parser_context_tag>(context); |
Václav Kubernát | b5ca154 | 2020-05-27 01:03:54 +0200 | [diff] [blame] | 191 | 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] | 192 | parserContext.m_errorMsg = "This is not a leaf list element."; |
Jan Kundrát | 07ca392 | 2023-06-20 19:03:56 +0200 | [diff] [blame] | 193 | REJECT_PARSING; |
Václav Kubernát | 5b8a8f3 | 2020-05-20 00:57:22 +0200 | [diff] [blame] | 194 | } |
| 195 | } |
| 196 | }; |
| 197 | |
Václav Kubernát | 8028f94 | 2019-09-25 16:03:23 +0200 | [diff] [blame] | 198 | struct space_separator_class { |
| 199 | template <typename T, typename Iterator, typename Context> |
| 200 | void on_success(Iterator const&, Iterator const&, T&, Context const& context) |
| 201 | { |
| 202 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 203 | parserContext.m_suggestions.clear(); |
Václav Kubernát | 1ed4aa3 | 2020-01-23 13:13:28 +0100 | [diff] [blame] | 204 | parserContext.m_completionIterator = boost::none; |
Václav Kubernát | 8028f94 | 2019-09-25 16:03:23 +0200 | [diff] [blame] | 205 | } |
| 206 | }; |
| 207 | |
Václav Kubernát | 6797df0 | 2019-03-18 20:21:50 +0100 | [diff] [blame] | 208 | struct create_class { |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 209 | template <typename Iterator, typename Exception, typename Context> |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame] | 210 | 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] | 211 | { |
| 212 | auto& parserContext = x3::get<parser_context_tag>(context); |
Václav Kubernát | 3a43323 | 2020-07-08 17:52:50 +0200 | [diff] [blame] | 213 | if (parserContext.m_errorMsg.empty()) { |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame] | 214 | parserContext.m_errorMsg = "Couldn't parse create/delete command."; |
Václav Kubernát | 3a43323 | 2020-07-08 17:52:50 +0200 | [diff] [blame] | 215 | } |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame] | 216 | return x3::error_handler_result::rethrow; |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 217 | } |
| 218 | }; |
| 219 | |
Václav Kubernát | 6797df0 | 2019-03-18 20:21:50 +0100 | [diff] [blame] | 220 | struct delete_class { |
| 221 | template <typename Iterator, typename Exception, typename Context> |
| 222 | x3::error_handler_result on_error(Iterator&, Iterator const&, Exception const&, Context const& context) |
| 223 | { |
| 224 | auto& parserContext = x3::get<parser_context_tag>(context); |
Václav Kubernát | 3a43323 | 2020-07-08 17:52:50 +0200 | [diff] [blame] | 225 | if (parserContext.m_errorMsg.empty()) { |
Václav Kubernát | 6797df0 | 2019-03-18 20:21:50 +0100 | [diff] [blame] | 226 | parserContext.m_errorMsg = "Couldn't parse create/delete command."; |
Václav Kubernát | 3a43323 | 2020-07-08 17:52:50 +0200 | [diff] [blame] | 227 | } |
Václav Kubernát | 6797df0 | 2019-03-18 20:21:50 +0100 | [diff] [blame] | 228 | return x3::error_handler_result::rethrow; |
| 229 | } |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 230 | }; |
| 231 | |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 232 | struct set_class { |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 233 | template <typename Iterator, typename Exception, typename Context> |
| 234 | x3::error_handler_result on_error(Iterator&, Iterator const&, Exception const& x, Context const& context) |
| 235 | { |
| 236 | auto& parserContext = x3::get<parser_context_tag>(context); |
Václav Kubernát | 3a43323 | 2020-07-08 17:52:50 +0200 | [diff] [blame] | 237 | if (parserContext.m_errorMsg.empty()) { |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame] | 238 | parserContext.m_errorMsg = "Expected " + x.which() + " here:"; |
Václav Kubernát | 3a43323 | 2020-07-08 17:52:50 +0200 | [diff] [blame] | 239 | } |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame] | 240 | return x3::error_handler_result::rethrow; |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 241 | } |
| 242 | }; |
| 243 | |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 244 | struct command_class { |
| 245 | template <typename Iterator, typename Exception, typename Context> |
| 246 | x3::error_handler_result on_error(Iterator&, Iterator const&, Exception const& x, Context const& context) |
| 247 | { |
| 248 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 249 | 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] | 250 | if (parserContext.m_errorMsg.empty()) { |
| 251 | parserContext.m_errorMsg = "Unknown command."; |
| 252 | } |
| 253 | error_handler(x.where(), parserContext.m_errorMsg); |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 254 | return x3::error_handler_result::fail; |
| 255 | } |
| 256 | }; |
Václav Kubernát | 5c75b25 | 2018-10-10 18:33:47 +0200 | [diff] [blame] | 257 | |
Václav Kubernát | 4108e0d | 2018-10-29 13:32:22 +0100 | [diff] [blame] | 258 | struct initializePath_class { |
Václav Kubernát | 5c75b25 | 2018-10-10 18:33:47 +0200 | [diff] [blame] | 259 | template <typename T, typename Iterator, typename Context> |
| 260 | void on_success(Iterator const&, Iterator const&, T&, Context const& context) |
| 261 | { |
| 262 | auto& parserContext = x3::get<parser_context_tag>(context); |
Václav Kubernát | 72749c6 | 2020-01-03 16:47:34 +0100 | [diff] [blame] | 263 | parserContext.resetPath(); |
Václav Kubernát | bf65dd7 | 2020-05-28 02:32:31 +0200 | [diff] [blame] | 264 | parserContext.m_tmpListPath = dataPath_{}; |
Václav Kubernát | 5c75b25 | 2018-10-10 18:33:47 +0200 | [diff] [blame] | 265 | parserContext.m_tmpListKeys.clear(); |
Václav Kubernát | 4108e0d | 2018-10-29 13:32:22 +0100 | [diff] [blame] | 266 | parserContext.m_suggestions.clear(); |
Václav Kubernát | 5c75b25 | 2018-10-10 18:33:47 +0200 | [diff] [blame] | 267 | } |
| 268 | }; |
Václav Kubernát | d6fd249 | 2018-11-19 15:11:16 +0100 | [diff] [blame] | 269 | |
Václav Kubernát | c15fe82 | 2020-06-04 11:28:39 +0200 | [diff] [blame] | 270 | 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] | 271 | |
| 272 | struct createKeySuggestions_class { |
| 273 | template <typename T, typename Iterator, typename Context> |
Jan Kundrát | 07ca392 | 2023-06-20 19:03:56 +0200 | [diff] [blame] | 274 | void on_success(Iterator const& before, Iterator const&, T&, Context const& context) |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 275 | { |
| 276 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 277 | const auto& schema = parserContext.m_schema; |
| 278 | |
Jan Kundrát | 07ca392 | 2023-06-20 19:03:56 +0200 | [diff] [blame] | 279 | parserContext.m_completionIterator = before; |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 280 | |
Václav Kubernát | 2db124c | 2020-05-28 21:58:36 +0200 | [diff] [blame] | 281 | const auto& keysNeeded = schema.listKeys(dataPathToSchemaPath(parserContext.m_tmpListPath)); |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 282 | parserContext.m_suggestions = generateMissingKeyCompletionSet(keysNeeded, parserContext.m_tmpListKeys); |
| 283 | } |
| 284 | }; |
| 285 | |
Václav Kubernát | 43908fb | 2020-01-02 19:05:51 +0100 | [diff] [blame] | 286 | std::string leafDataToCompletion(const leaf_data_& value); |
| 287 | |
| 288 | struct createValueSuggestions_class { |
| 289 | template <typename T, typename Iterator, typename Context> |
Jan Kundrát | 07ca392 | 2023-06-20 19:03:56 +0200 | [diff] [blame] | 290 | void on_success(Iterator const& before, Iterator const&, T&, Context const& context) |
Václav Kubernát | 43908fb | 2020-01-02 19:05:51 +0100 | [diff] [blame] | 291 | { |
| 292 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 293 | if (!parserContext.m_completing) { |
| 294 | return; |
| 295 | } |
| 296 | const auto& dataQuery = parserContext.m_dataquery; |
| 297 | |
Jan Kundrát | 07ca392 | 2023-06-20 19:03:56 +0200 | [diff] [blame] | 298 | parserContext.m_completionIterator = before; |
Václav Kubernát | 2db124c | 2020-05-28 21:58:36 +0200 | [diff] [blame] | 299 | auto listInstances = dataQuery->listKeys(parserContext.m_tmpListPath); |
Václav Kubernát | 43908fb | 2020-01-02 19:05:51 +0100 | [diff] [blame] | 300 | |
| 301 | decltype(listInstances) filteredInstances; |
| 302 | //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] | 303 | const auto partialFitsComplete = [&parserContext](const auto& complete) { |
Václav Kubernát | 43908fb | 2020-01-02 19:05:51 +0100 | [diff] [blame] | 304 | const auto& partial = parserContext.m_tmpListKeys; |
Václav Kubernát | b4e5b18 | 2020-11-16 19:55:09 +0100 | [diff] [blame] | 305 | return std::all_of(partial.begin(), partial.end(), [&complete](const auto& oneKV) { |
| 306 | const auto& [k, v] = oneKV; |
| 307 | return complete.at(k) == v; |
| 308 | }); |
Václav Kubernát | 43908fb | 2020-01-02 19:05:51 +0100 | [diff] [blame] | 309 | }; |
| 310 | std::copy_if(listInstances.begin(), listInstances.end(), std::inserter(filteredInstances, filteredInstances.end()), partialFitsComplete); |
| 311 | |
Václav Kubernát | cb3af40 | 2020-02-12 16:49:17 +0100 | [diff] [blame] | 312 | std::set<Completion> validValues; |
Václav Kubernát | 43908fb | 2020-01-02 19:05:51 +0100 | [diff] [blame] | 313 | |
Václav Kubernát | cb3af40 | 2020-02-12 16:49:17 +0100 | [diff] [blame] | 314 | std::transform(filteredInstances.begin(), filteredInstances.end(), std::inserter(validValues, validValues.end()), [&parserContext](const auto& instance) { |
| 315 | 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] | 316 | }); |
| 317 | |
| 318 | parserContext.m_suggestions = validValues; |
| 319 | } |
| 320 | }; |
| 321 | |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 322 | struct suggestKeysEnd_class { |
| 323 | template <typename T, typename Iterator, typename Context> |
Jan Kundrát | 07ca392 | 2023-06-20 19:03:56 +0200 | [diff] [blame] | 324 | void on_success(Iterator const& before, Iterator const&, T&, Context const& context) |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 325 | { |
| 326 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 327 | const auto& schema = parserContext.m_schema; |
| 328 | |
Jan Kundrát | 07ca392 | 2023-06-20 19:03:56 +0200 | [diff] [blame] | 329 | parserContext.m_completionIterator = before; |
Václav Kubernát | 2db124c | 2020-05-28 21:58:36 +0200 | [diff] [blame] | 330 | const auto& keysNeeded = schema.listKeys(dataPathToSchemaPath(parserContext.m_tmpListPath)); |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 331 | if (generateMissingKeyCompletionSet(keysNeeded, parserContext.m_tmpListKeys).empty()) { |
Václav Kubernát | cb3af40 | 2020-02-12 16:49:17 +0100 | [diff] [blame] | 332 | parserContext.m_suggestions = {Completion{"]/"}}; |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 333 | } else { |
Václav Kubernát | cb3af40 | 2020-02-12 16:49:17 +0100 | [diff] [blame] | 334 | parserContext.m_suggestions = {Completion{"]["}}; |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 335 | } |
| 336 | } |
| 337 | }; |
Václav Kubernát | 5727242 | 2019-02-08 12:48:24 +0100 | [diff] [blame] | 338 | |
Václav Kubernát | 672889c | 2020-05-27 04:11:36 +0200 | [diff] [blame] | 339 | template <typename T> |
Václav Kubernát | b4e5b18 | 2020-11-16 19:55:09 +0100 | [diff] [blame] | 340 | std::string commandNamesVisitor(boost::type<T>) |
Václav Kubernát | 672889c | 2020-05-27 04:11:36 +0200 | [diff] [blame] | 341 | { |
| 342 | return T::name; |
| 343 | } |
Václav Kubernát | 5727242 | 2019-02-08 12:48:24 +0100 | [diff] [blame] | 344 | |
| 345 | struct createCommandSuggestions_class { |
| 346 | template <typename T, typename Iterator, typename Context> |
Jan Kundrát | 07ca392 | 2023-06-20 19:03:56 +0200 | [diff] [blame] | 347 | void on_success(Iterator const& before, Iterator const&, T&, Context const& context) |
Václav Kubernát | 5727242 | 2019-02-08 12:48:24 +0100 | [diff] [blame] | 348 | { |
| 349 | auto& parserContext = x3::get<parser_context_tag>(context); |
Jan Kundrát | 07ca392 | 2023-06-20 19:03:56 +0200 | [diff] [blame] | 350 | parserContext.m_completionIterator = before; |
Václav Kubernát | 5727242 | 2019-02-08 12:48:24 +0100 | [diff] [blame] | 351 | |
| 352 | parserContext.m_suggestions.clear(); |
| 353 | 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] | 354 | parserContext.m_suggestions.insert({commandNamesVisitor(cmd), " "}); |
Václav Kubernát | 5727242 | 2019-02-08 12:48:24 +0100 | [diff] [blame] | 355 | }); |
| 356 | } |
| 357 | }; |
Václav Kubernát | ac035d6 | 2019-02-18 10:59:08 +0100 | [diff] [blame] | 358 | |
| 359 | struct completing_class { |
| 360 | template <typename T, typename Iterator, typename Context> |
Jan Kundrát | 07ca392 | 2023-06-20 19:03:56 +0200 | [diff] [blame] | 361 | void on_success(Iterator const& before, Iterator const& after, T&, Context const& context) |
Václav Kubernát | ac035d6 | 2019-02-18 10:59:08 +0100 | [diff] [blame] | 362 | { |
| 363 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 364 | |
Václav Kubernát | 3a43323 | 2020-07-08 17:52:50 +0200 | [diff] [blame] | 365 | if (!parserContext.m_completing) { |
Jan Kundrát | 07ca392 | 2023-06-20 19:03:56 +0200 | [diff] [blame] | 366 | REJECT_PARSING; |
Václav Kubernát | 3a43323 | 2020-07-08 17:52:50 +0200 | [diff] [blame] | 367 | } |
Václav Kubernát | ac035d6 | 2019-02-18 10:59:08 +0100 | [diff] [blame] | 368 | } |
| 369 | }; |