blob: 7fadd2971b00f878c2bca82ac8a633e10e0b64fc [file] [log] [blame]
Václav Kubernát0a2a2e82018-05-11 13:59:12 +02001/*
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át195eeea2018-05-18 13:52:36 +020010
Václav Kubernát57272422019-02-08 12:48:24 +010011#include <boost/mpl/for_each.hpp>
Václav Kubernát509ce652019-05-29 19:46:44 +020012#include <boost/spirit/home/x3.hpp>
13#include <boost/spirit/home/x3/support/utility/error_reporting.hpp>
14
15
Václav Kubernát57272422019-02-08 12:48:24 +010016#include "ast_commands.hpp"
Václav Kubernát4294a852020-02-14 15:07:14 +010017#include "parser_context.hpp"
Václav Kubernát48fc3832018-05-28 14:21:22 +020018#include "schema.hpp"
Václav Kubernát5b8e2822022-01-14 03:19:54 +010019#if BOOST_VERSION >= 107800
20#include "UniqueResource.hpp"
21#endif
Václav Kubernátebca2552018-06-08 19:06:02 +020022#include "utils.hpp"
Václav Kubernát329c6c32019-02-06 16:41:53 +010023namespace x3 = boost::spirit::x3;
24
25struct parser_context_tag;
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020026
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020027struct 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át0a2a2e82018-05-11 13:59:12 +020032
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át41378452018-06-06 16:29:40 +020036 } else {
Václav Kubernát43908fb2020-01-02 19:05:51 +010037 parserContext.m_tmpListKeys.insert({ast.first, ast.second});
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020038 }
39 }
Václav Kubernát41378452018-06-06 16:29:40 +020040
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át0a2a2e82018-05-11 13:59:12 +020048};
Václav Kubernát41378452018-06-06 16:29:40 +020049
Václav Kubernát2db124c2020-05-28 21:58:36 +020050boost::optional<std::string> optModuleToOptString(const boost::optional<module_> module);
51
Václav Kubernát7707cae2020-01-16 12:04:53 +010052struct 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át2db124c2020-05-28 21:58:36 +020056 ParserContext& parserContext = x3::get<parser_context_tag>(context);
Václav Kubernát7707cae2020-01-16 12:04:53 +010057 const Schema& schema = parserContext.m_schema;
Václav Kubernát7707cae2020-01-16 12:04:53 +010058
Václav Kubernát2db124c2020-05-28 21:58:36 +020059 if (schema.listHasKey(dataPathToSchemaPath(parserContext.m_tmpListPath), ast)) {
60 parserContext.m_tmpListKeyLeafPath.m_location = dataPathToSchemaPath(parserContext.m_tmpListPath);
Václav Kubernátb4e5b182020-11-16 19:55:09 +010061 parserContext.m_tmpListKeyLeafPath.m_node = {optModuleToOptString(parserContext.m_tmpListPath.m_nodes.back().m_prefix), ast};
Václav Kubernát7707cae2020-01-16 12:04:53 +010062 } else {
Václav Kubernát2db124c2020-05-28 21:58:36 +020063 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át7707cae2020-01-16 12:04:53 +010065 _pass(context) = false;
66 }
67 }
68};
Václav Kubernát89728d82018-09-13 16:28:28 +020069
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020070struct 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át48fc3832018-05-28 14:21:22 +020075 const Schema& schema = parserContext.m_schema;
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020076
Václav Kubernát2db124c2020-05-28 21:58:36 +020077 const auto& keysNeeded = schema.listKeys(dataPathToSchemaPath(parserContext.m_tmpListPath));
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020078 std::set<std::string> keysSupplied;
Václav Kubernát3a433232020-07-08 17:52:50 +020079 for (const auto& it : ast) {
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020080 keysSupplied.insert(it.first);
Václav Kubernát3a433232020-07-08 17:52:50 +020081 }
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020082
83 if (keysNeeded != keysSupplied) {
Václav Kubernát2db124c2020-05-28 21:58:36 +020084 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át0a2a2e82018-05-11 13:59:12 +020086 "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át3a433232020-07-08 17:52:50 +020092 for (const auto& it : missingKeys) {
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020093 parserContext.m_errorMsg += " " + it;
Václav Kubernát3a433232020-07-08 17:52:50 +020094 }
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020095 parserContext.m_errorMsg += ".";
96
97 _pass(context) = false;
Václav Kubernátbf65dd72020-05-28 02:32:31 +020098 return;
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020099 }
Václav Kubernátbf65dd72020-05-28 02:32:31 +0200100
101 // Clean up after listSuffix, in case someone wants to parse more listSuffixes
102 parserContext.m_tmpListKeys.clear();
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200103 }
Václav Kubernát41378452018-06-06 16:29:40 +0200104
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át3a433232020-07-08 17:52:50 +0200109 if (parserContext.m_errorMsg.empty()) {
Václav Kubernát41378452018-06-06 16:29:40 +0200110 parserContext.m_errorMsg = "Expecting ']' here:";
Václav Kubernát3a433232020-07-08 17:52:50 +0200111 }
Václav Kubernát41378452018-06-06 16:29:40 +0200112 return x3::error_handler_result::rethrow;
Václav Kubernát41378452018-06-06 16:29:40 +0200113 }
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200114};
Václav Kubernát744f57f2018-06-29 22:46:26 +0200115
Václav Kubernát744f57f2018-06-29 22:46:26 +0200116struct 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át1bcee3b2020-05-28 22:19:59 +0200123 if (!schema.isModule(ast.m_name)) {
Václav Kubernát744f57f2018-06-29 22:46:26 +0200124 parserContext.m_errorMsg = "Invalid module name.";
Václav Kubernát07204242018-06-04 18:12:09 +0200125 _pass(context) = false;
126 }
127 }
128};
129
Václav Kubernát37171a12018-08-31 17:01:48 +0200130struct 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át72749c62020-01-03 16:47:34 +0100135 parserContext.clearPath();
Václav Kubernát37171a12018-08-31 17:01:48 +0200136 }
137};
138
Václav Kubernát744f57f2018-06-29 22:46:26 +0200139struct cd_class {
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200140 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át3a433232020-07-08 17:52:50 +0200144 if (parserContext.m_errorMsg.empty()) {
Václav Kubernát41378452018-06-06 16:29:40 +0200145 parserContext.m_errorMsg = "Expected " + x.which() + " here:";
Václav Kubernát3a433232020-07-08 17:52:50 +0200146 }
Václav Kubernát41378452018-06-06 16:29:40 +0200147 return x3::error_handler_result::rethrow;
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200148 }
149};
Václav Kubernátb61336d2018-05-28 17:35:03 +0200150
Václav Kubernát5b8e2822022-01-14 03:19:54 +0100151#if BOOST_VERSION >= 107800
152template <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át6797df02019-03-18 20:21:50 +0100164struct presenceContainerPath_class {
Václav Kubernátb61336d2018-05-28 17:35:03 +0200165 template <typename T, typename Iterator, typename Context>
Václav Kubernát5b8e2822022-01-14 03:19:54 +0100166#if BOOST_VERSION >= 107800
167 void on_success(Iterator const& was, Iterator& will, T& ast, Context const& context)
168#else
Václav Kubernátb61336d2018-05-28 17:35:03 +0200169 void on_success(Iterator const&, Iterator const&, T& ast, Context const& context)
Václav Kubernát5b8e2822022-01-14 03:19:54 +0100170#endif
Václav Kubernátb61336d2018-05-28 17:35:03 +0200171 {
Václav Kubernát5b8e2822022-01-14 03:19:54 +0100172#if BOOST_VERSION >= 107800
173 auto rollback = makeIteratorRollbacker(was, will, _pass(context));
174#endif
Václav Kubernátb61336d2018-05-28 17:35:03 +0200175 auto& parserContext = x3::get<parser_context_tag>(context);
176 const auto& schema = parserContext.m_schema;
Václav Kubernát0d42c512020-05-20 21:18:39 +0200177 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áte811bfa2020-05-29 02:25:20 +0200183 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átb61336d2018-05-28 17:35:03 +0200185 _pass(context) = false;
Václav Kubernátb61336d2018-05-28 17:35:03 +0200186 }
187 }
Václav Kubernát6797df02019-03-18 20:21:50 +0100188};
Václav Kubernátb61336d2018-05-28 17:35:03 +0200189
Václav Kubernátf5f64f02019-03-19 17:15:47 +0100190struct listInstancePath_class {
191 template <typename T, typename Iterator, typename Context>
Václav Kubernát5b8e2822022-01-14 03:19:54 +0100192#if BOOST_VERSION >= 107800
193 void on_success(Iterator const& was, Iterator& will, T& ast, Context const& context)
194#else
Václav Kubernátf5f64f02019-03-19 17:15:47 +0100195 void on_success(Iterator const&, Iterator const&, T& ast, Context const& context)
Václav Kubernát5b8e2822022-01-14 03:19:54 +0100196#endif
Václav Kubernátf5f64f02019-03-19 17:15:47 +0100197 {
Václav Kubernát5b8e2822022-01-14 03:19:54 +0100198#if BOOST_VERSION >= 107800
199 auto rollback = makeIteratorRollbacker(was, will, _pass(context));
200#endif
Václav Kubernátf5f64f02019-03-19 17:15:47 +0100201 auto& parserContext = x3::get<parser_context_tag>(context);
Václav Kubernát2ebab1d2020-05-27 01:28:30 +0200202 if (ast.m_nodes.empty() || !std::holds_alternative<listElement_>(ast.m_nodes.back().m_suffix)) {
Václav Kubernátf5f64f02019-03-19 17:15:47 +0100203 parserContext.m_errorMsg = "This is not a list instance.";
204 _pass(context) = false;
205 }
206 }
207};
208
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200209struct leafListElementPath_class {
210 template <typename T, typename Iterator, typename Context>
Václav Kubernát5b8e2822022-01-14 03:19:54 +0100211#if BOOST_VERSION >= 107800
212 void on_success(Iterator const& was, Iterator& will, T& ast, Context const& context)
213#else
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200214 void on_success(Iterator const&, Iterator const&, T& ast, Context const& context)
Václav Kubernát5b8e2822022-01-14 03:19:54 +0100215#endif
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200216 {
Václav Kubernát5b8e2822022-01-14 03:19:54 +0100217#if BOOST_VERSION >= 107800
218 auto rollback = makeIteratorRollbacker(was, will, _pass(context));
219#endif
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200220 auto& parserContext = x3::get<parser_context_tag>(context);
Václav Kubernátb5ca1542020-05-27 01:03:54 +0200221 if (ast.m_nodes.empty() || !std::holds_alternative<leafListElement_>(ast.m_nodes.back().m_suffix)) {
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200222 parserContext.m_errorMsg = "This is not a leaf list element.";
223 _pass(context) = false;
224 }
225 }
226};
227
Václav Kubernát8028f942019-09-25 16:03:23 +0200228struct 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át1ed4aa32020-01-23 13:13:28 +0100234 parserContext.m_completionIterator = boost::none;
Václav Kubernát8028f942019-09-25 16:03:23 +0200235 }
236};
237
Václav Kubernát6797df02019-03-18 20:21:50 +0100238struct create_class {
Václav Kubernátb61336d2018-05-28 17:35:03 +0200239 template <typename Iterator, typename Exception, typename Context>
Václav Kubernát41378452018-06-06 16:29:40 +0200240 x3::error_handler_result on_error(Iterator&, Iterator const&, Exception const&, Context const& context)
Václav Kubernátb61336d2018-05-28 17:35:03 +0200241 {
242 auto& parserContext = x3::get<parser_context_tag>(context);
Václav Kubernát3a433232020-07-08 17:52:50 +0200243 if (parserContext.m_errorMsg.empty()) {
Václav Kubernát41378452018-06-06 16:29:40 +0200244 parserContext.m_errorMsg = "Couldn't parse create/delete command.";
Václav Kubernát3a433232020-07-08 17:52:50 +0200245 }
Václav Kubernát41378452018-06-06 16:29:40 +0200246 return x3::error_handler_result::rethrow;
Václav Kubernátb61336d2018-05-28 17:35:03 +0200247 }
248};
249
Václav Kubernát6797df02019-03-18 20:21:50 +0100250struct 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át3a433232020-07-08 17:52:50 +0200255 if (parserContext.m_errorMsg.empty()) {
Václav Kubernát6797df02019-03-18 20:21:50 +0100256 parserContext.m_errorMsg = "Couldn't parse create/delete command.";
Václav Kubernát3a433232020-07-08 17:52:50 +0200257 }
Václav Kubernát6797df02019-03-18 20:21:50 +0100258 return x3::error_handler_result::rethrow;
259 }
Václav Kubernátb61336d2018-05-28 17:35:03 +0200260};
261
Václav Kubernát07204242018-06-04 18:12:09 +0200262struct set_class {
Václav Kubernát07204242018-06-04 18:12:09 +0200263 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át3a433232020-07-08 17:52:50 +0200267 if (parserContext.m_errorMsg.empty()) {
Václav Kubernát41378452018-06-06 16:29:40 +0200268 parserContext.m_errorMsg = "Expected " + x.which() + " here:";
Václav Kubernát3a433232020-07-08 17:52:50 +0200269 }
Václav Kubernát41378452018-06-06 16:29:40 +0200270 return x3::error_handler_result::rethrow;
Václav Kubernát07204242018-06-04 18:12:09 +0200271 }
272};
273
Václav Kubernátb61336d2018-05-28 17:35:03 +0200274struct 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át41378452018-06-06 16:29:40 +0200280 if (parserContext.m_errorMsg.empty()) {
281 parserContext.m_errorMsg = "Unknown command.";
282 }
283 error_handler(x.where(), parserContext.m_errorMsg);
Václav Kubernátb61336d2018-05-28 17:35:03 +0200284 return x3::error_handler_result::fail;
285 }
286};
Václav Kubernát5c75b252018-10-10 18:33:47 +0200287
Václav Kubernát4108e0d2018-10-29 13:32:22 +0100288struct initializePath_class {
Václav Kubernát5c75b252018-10-10 18:33:47 +0200289 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át72749c62020-01-03 16:47:34 +0100293 parserContext.resetPath();
Václav Kubernátbf65dd72020-05-28 02:32:31 +0200294 parserContext.m_tmpListPath = dataPath_{};
Václav Kubernát5c75b252018-10-10 18:33:47 +0200295 parserContext.m_tmpListKeys.clear();
Václav Kubernát4108e0d2018-10-29 13:32:22 +0100296 parserContext.m_suggestions.clear();
Václav Kubernát5c75b252018-10-10 18:33:47 +0200297 }
298};
Václav Kubernátd6fd2492018-11-19 15:11:16 +0100299
Václav Kubernátc15fe822020-06-04 11:28:39 +0200300std::set<Completion> generateMissingKeyCompletionSet(std::set<std::string> keysNeeded, ListInstance currentSet);
Václav Kubernát329c6c32019-02-06 16:41:53 +0100301
302struct 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át2db124c2020-05-28 21:58:36 +0200311 const auto& keysNeeded = schema.listKeys(dataPathToSchemaPath(parserContext.m_tmpListPath));
Václav Kubernát329c6c32019-02-06 16:41:53 +0100312 parserContext.m_suggestions = generateMissingKeyCompletionSet(keysNeeded, parserContext.m_tmpListKeys);
313 }
314};
315
Václav Kubernát43908fb2020-01-02 19:05:51 +0100316std::string leafDataToCompletion(const leaf_data_& value);
317
318struct 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át2db124c2020-05-28 21:58:36 +0200329 auto listInstances = dataQuery->listKeys(parserContext.m_tmpListPath);
Václav Kubernát43908fb2020-01-02 19:05:51 +0100330
331 decltype(listInstances) filteredInstances;
332 //This filters out instances, which don't correspond to the partial instance we have.
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100333 const auto partialFitsComplete = [&parserContext](const auto& complete) {
Václav Kubernát43908fb2020-01-02 19:05:51 +0100334 const auto& partial = parserContext.m_tmpListKeys;
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100335 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át43908fb2020-01-02 19:05:51 +0100339 };
340 std::copy_if(listInstances.begin(), listInstances.end(), std::inserter(filteredInstances, filteredInstances.end()), partialFitsComplete);
341
Václav Kubernátcb3af402020-02-12 16:49:17 +0100342 std::set<Completion> validValues;
Václav Kubernát43908fb2020-01-02 19:05:51 +0100343
Václav Kubernátcb3af402020-02-12 16:49:17 +0100344 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át43908fb2020-01-02 19:05:51 +0100346 });
347
348 parserContext.m_suggestions = validValues;
349 }
350};
351
Václav Kubernát329c6c32019-02-06 16:41:53 +0100352struct 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át2db124c2020-05-28 21:58:36 +0200360 const auto& keysNeeded = schema.listKeys(dataPathToSchemaPath(parserContext.m_tmpListPath));
Václav Kubernát329c6c32019-02-06 16:41:53 +0100361 if (generateMissingKeyCompletionSet(keysNeeded, parserContext.m_tmpListKeys).empty()) {
Václav Kubernátcb3af402020-02-12 16:49:17 +0100362 parserContext.m_suggestions = {Completion{"]/"}};
Václav Kubernát329c6c32019-02-06 16:41:53 +0100363 } else {
Václav Kubernátcb3af402020-02-12 16:49:17 +0100364 parserContext.m_suggestions = {Completion{"]["}};
Václav Kubernát329c6c32019-02-06 16:41:53 +0100365 }
366 }
367};
Václav Kubernát57272422019-02-08 12:48:24 +0100368
Václav Kubernát672889c2020-05-27 04:11:36 +0200369template <typename T>
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100370std::string commandNamesVisitor(boost::type<T>)
Václav Kubernát672889c2020-05-27 04:11:36 +0200371{
372 return T::name;
373}
Václav Kubernát57272422019-02-08 12:48:24 +0100374
375struct createCommandSuggestions_class {
376 template <typename T, typename Iterator, typename Context>
Václav Kubernát5b8e2822022-01-14 03:19:54 +0100377#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át57272422019-02-08 12:48:24 +0100382 {
Václav Kubernát5b8e2822022-01-14 03:19:54 +0100383#if BOOST_VERSION >= 107800
384 auto rollback = makeIteratorRollbacker(was, will, _pass(context));
385#endif
Václav Kubernát57272422019-02-08 12:48:24 +0100386 auto& parserContext = x3::get<parser_context_tag>(context);
Václav Kubernát5b8e2822022-01-14 03:19:54 +0100387 parserContext.m_completionIterator = was;
Václav Kubernát57272422019-02-08 12:48:24 +0100388
389 parserContext.m_suggestions.clear();
390 boost::mpl::for_each<CommandTypes, boost::type<boost::mpl::_>>([&parserContext](auto cmd) {
Václav Kubernát672889c2020-05-27 04:11:36 +0200391 parserContext.m_suggestions.insert({commandNamesVisitor(cmd), " "});
Václav Kubernát57272422019-02-08 12:48:24 +0100392 });
393 }
394};
Václav Kubernátac035d62019-02-18 10:59:08 +0100395
396struct 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át3a433232020-07-08 17:52:50 +0200402 if (!parserContext.m_completing) {
Václav Kubernátac035d62019-02-18 10:59:08 +0100403 _pass(context) = false;
Václav Kubernát3a433232020-07-08 17:52:50 +0200404 }
Václav Kubernátac035d62019-02-18 10:59:08 +0100405 }
406};