blob: 5b5a1525232104e046c2cb19d21a398e382d155f [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átebca2552018-06-08 19:06:02 +020019#include "utils.hpp"
Václav Kubernát329c6c32019-02-06 16:41:53 +010020namespace x3 = boost::spirit::x3;
21
22struct parser_context_tag;
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020023
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020024struct keyValue_class {
25 template <typename T, typename Iterator, typename Context>
26 void on_success(Iterator const&, Iterator const&, T& ast, Context const& context)
27 {
28 auto& parserContext = x3::get<parser_context_tag>(context);
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020029
30 if (parserContext.m_tmpListKeys.find(ast.first) != parserContext.m_tmpListKeys.end()) {
31 _pass(context) = false;
32 parserContext.m_errorMsg = "Key \"" + ast.first + "\" was entered more than once.";
Václav Kubernát41378452018-06-06 16:29:40 +020033 } else {
Václav Kubernát43908fb2020-01-02 19:05:51 +010034 parserContext.m_tmpListKeys.insert({ast.first, ast.second});
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020035 }
36 }
Václav Kubernát41378452018-06-06 16:29:40 +020037
38 template <typename Iterator, typename Exception, typename Context>
39 x3::error_handler_result on_error(Iterator&, Iterator const&, Exception const&, Context const& context)
40 {
41 auto& parserContext = x3::get<parser_context_tag>(context);
42 parserContext.m_errorMsg = "Error parsing key values here:";
43 return x3::error_handler_result::rethrow;
44 }
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020045};
Václav Kubernát41378452018-06-06 16:29:40 +020046
Václav Kubernát744f57f2018-06-29 22:46:26 +020047struct node_identifier_class {
48 template <typename T, typename Iterator, typename Context>
49 void on_success(Iterator const&, Iterator const&, T&, Context const& context)
50 {
51 auto& parserContext = x3::get<parser_context_tag>(context);
52
53 if (!parserContext.m_topLevelModulePresent) {
54 if (parserContext.m_errorMsg.empty())
55 parserContext.m_errorMsg = "You have to specify a top level module.";
56 _pass(context) = false;
57 }
58 }
59};
60
Václav Kubernát7707cae2020-01-16 12:04:53 +010061struct key_identifier_class {
62 template <typename T, typename Iterator, typename Context>
63 void on_success(Iterator const&, Iterator const&, T& ast, Context const& context)
64 {
65 auto& parserContext = x3::get<parser_context_tag>(context);
66 const Schema& schema = parserContext.m_schema;
67 schemaPath_ location = parserContext.currentSchemaPath();
68 ModuleNodePair list{parserContext.m_curModule, parserContext.m_tmpListName};
69
70 if (schema.listHasKey(location, list, ast)) {
71 schemaNode_ listNode;
Václav Kubernát43908fb2020-01-02 19:05:51 +010072 listNode.m_prefix = parserContext.m_curModule.flat_map([] (auto mod) { return boost::optional<module_>{{mod}}; });;
Václav Kubernát7707cae2020-01-16 12:04:53 +010073 listNode.m_suffix = list_{parserContext.m_tmpListName};
74 location.m_nodes.push_back(listNode);
75 parserContext.m_tmpListKeyLeafPath.m_location = location;
Václav Kubernát43908fb2020-01-02 19:05:51 +010076 parserContext.m_tmpListKeyLeafPath.m_node = { parserContext.m_curModule, ast };
Václav Kubernát7707cae2020-01-16 12:04:53 +010077 } else {
78 parserContext.m_errorMsg = parserContext.m_tmpListName + " is not indexed by \"" + ast + "\".";
79 _pass(context) = false;
80 }
81 }
82};
Václav Kubernát89728d82018-09-13 16:28:28 +020083
Václav Kubernát744f57f2018-06-29 22:46:26 +020084struct module_identifier_class;
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020085
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020086struct listSuffix_class {
87 template <typename T, typename Iterator, typename Context>
88 void on_success(Iterator const&, Iterator const&, T& ast, Context const& context)
89 {
90 auto& parserContext = x3::get<parser_context_tag>(context);
Václav Kubernát48fc3832018-05-28 14:21:22 +020091 const Schema& schema = parserContext.m_schema;
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020092
Václav Kubernát72749c62020-01-03 16:47:34 +010093 const auto& keysNeeded = schema.listKeys(parserContext.currentSchemaPath(), {parserContext.m_curModule, parserContext.m_tmpListName});
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020094 std::set<std::string> keysSupplied;
95 for (const auto& it : ast)
96 keysSupplied.insert(it.first);
97
98 if (keysNeeded != keysSupplied) {
99 parserContext.m_errorMsg = "Not enough keys for " + parserContext.m_tmpListName + ". " +
100 "These keys were not supplied:";
101 std::set<std::string> missingKeys;
102 std::set_difference(keysNeeded.begin(), keysNeeded.end(),
103 keysSupplied.begin(), keysSupplied.end(),
104 std::inserter(missingKeys, missingKeys.end()));
105
106 for (const auto& it : missingKeys)
107 parserContext.m_errorMsg += " " + it;
108 parserContext.m_errorMsg += ".";
109
110 _pass(context) = false;
111 }
112 }
Václav Kubernát41378452018-06-06 16:29:40 +0200113
114 template <typename Iterator, typename Exception, typename Context>
115 x3::error_handler_result on_error(Iterator&, Iterator const&, Exception const&, Context const& context)
116 {
117 auto& parserContext = x3::get<parser_context_tag>(context);
118 if (parserContext.m_errorMsg.empty())
119 parserContext.m_errorMsg = "Expecting ']' here:";
120 return x3::error_handler_result::rethrow;
Václav Kubernát41378452018-06-06 16:29:40 +0200121 }
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200122};
Václav Kubernát744f57f2018-06-29 22:46:26 +0200123
Václav Kubernát744f57f2018-06-29 22:46:26 +0200124struct module_class {
125 template <typename T, typename Iterator, typename Context>
126 void on_success(Iterator const&, Iterator const&, T& ast, Context const& context)
127 {
128 auto& parserContext = x3::get<parser_context_tag>(context);
129 const auto& schema = parserContext.m_schema;
130
Václav Kubernát75877de2019-11-20 17:43:02 +0100131 if (schema.isModule(ast.m_name)) {
Václav Kubernát744f57f2018-06-29 22:46:26 +0200132 parserContext.m_curModule = ast.m_name;
133 parserContext.m_topLevelModulePresent = true;
Václav Kubernát07204242018-06-04 18:12:09 +0200134 } else {
Václav Kubernát744f57f2018-06-29 22:46:26 +0200135 parserContext.m_errorMsg = "Invalid module name.";
Václav Kubernát07204242018-06-04 18:12:09 +0200136 _pass(context) = false;
137 }
138 }
139};
140
Václav Kubernát37171a12018-08-31 17:01:48 +0200141struct absoluteStart_class {
142 template <typename T, typename Iterator, typename Context>
143 void on_success(Iterator const&, Iterator const&, T&, Context const& context)
144 {
145 auto& parserContext = x3::get<parser_context_tag>(context);
Václav Kubernát72749c62020-01-03 16:47:34 +0100146 parserContext.clearPath();
Václav Kubernát37171a12018-08-31 17:01:48 +0200147 }
148};
149
Václav Kubernát6d791432018-10-25 16:00:35 +0200150struct discard_class;
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200151
Václav Kubernát11afac72018-07-18 14:59:53 +0200152struct ls_class;
153
Václav Kubernát744f57f2018-06-29 22:46:26 +0200154struct cd_class {
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200155 template <typename Iterator, typename Exception, typename Context>
156 x3::error_handler_result on_error(Iterator&, Iterator const&, Exception const& x, Context const& context)
157 {
158 auto& parserContext = x3::get<parser_context_tag>(context);
Václav Kubernát41378452018-06-06 16:29:40 +0200159 if (parserContext.m_errorMsg.empty())
160 parserContext.m_errorMsg = "Expected " + x.which() + " here:";
161 return x3::error_handler_result::rethrow;
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200162 }
163};
Václav Kubernátb61336d2018-05-28 17:35:03 +0200164
Václav Kubernát6797df02019-03-18 20:21:50 +0100165struct presenceContainerPath_class {
Václav Kubernátb61336d2018-05-28 17:35:03 +0200166 template <typename T, typename Iterator, typename Context>
167 void on_success(Iterator const&, Iterator const&, T& ast, Context const& context)
168 {
169 auto& parserContext = x3::get<parser_context_tag>(context);
170 const auto& schema = parserContext.m_schema;
Václav Kubernát0d42c512020-05-20 21:18:39 +0200171 if (ast.m_nodes.empty()) {
172 parserContext.m_errorMsg = "This container is not a presence container.";
173 _pass(context) = false;
174 return;
175 }
176
Václav Kubernátb61336d2018-05-28 17:35:03 +0200177 try {
Václav Kubernát744f57f2018-06-29 22:46:26 +0200178 boost::optional<std::string> module;
Václav Kubernát6797df02019-03-18 20:21:50 +0100179 if (ast.m_nodes.back().m_prefix)
180 module = ast.m_nodes.back().m_prefix.value().m_name;
Václav Kubernátb5ca1542020-05-27 01:03:54 +0200181 container_ cont = std::get<container_>(ast.m_nodes.back().m_suffix);
Václav Kubernát72749c62020-01-03 16:47:34 +0100182 auto location = pathWithoutLastNode(parserContext.currentSchemaPath());
Václav Kubernátb61336d2018-05-28 17:35:03 +0200183
Václav Kubernát744f57f2018-06-29 22:46:26 +0200184 if (!schema.isPresenceContainer(location, {module, cont.m_name})) {
Václav Kubernát41378452018-06-06 16:29:40 +0200185 parserContext.m_errorMsg = "This container is not a presence container.";
Václav Kubernátb61336d2018-05-28 17:35:03 +0200186 _pass(context) = false;
Václav Kubernátb61336d2018-05-28 17:35:03 +0200187 }
Václav Kubernátb5ca1542020-05-27 01:03:54 +0200188 } catch (std::bad_variant_access&) {
Václav Kubernát41378452018-06-06 16:29:40 +0200189 parserContext.m_errorMsg = "This is not a container.";
Václav Kubernátb61336d2018-05-28 17:35:03 +0200190 _pass(context) = false;
Václav Kubernátb61336d2018-05-28 17:35:03 +0200191 }
192 }
Václav Kubernát6797df02019-03-18 20:21:50 +0100193};
Václav Kubernátb61336d2018-05-28 17:35:03 +0200194
Václav Kubernátf5f64f02019-03-19 17:15:47 +0100195struct listInstancePath_class {
196 template <typename T, typename Iterator, typename Context>
197 void on_success(Iterator const&, Iterator const&, T& ast, Context const& context)
198 {
199 auto& parserContext = x3::get<parser_context_tag>(context);
Václav Kubernát2ebab1d2020-05-27 01:28:30 +0200200 if (ast.m_nodes.empty() || !std::holds_alternative<listElement_>(ast.m_nodes.back().m_suffix)) {
Václav Kubernátf5f64f02019-03-19 17:15:47 +0100201 parserContext.m_errorMsg = "This is not a list instance.";
202 _pass(context) = false;
203 }
204 }
205};
206
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200207struct leafListElementPath_class {
208 template <typename T, typename Iterator, typename Context>
209 void on_success(Iterator const&, Iterator const&, T& ast, Context const& context)
210 {
211 auto& parserContext = x3::get<parser_context_tag>(context);
Václav Kubernátb5ca1542020-05-27 01:03:54 +0200212 if (ast.m_nodes.empty() || !std::holds_alternative<leafListElement_>(ast.m_nodes.back().m_suffix)) {
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200213 parserContext.m_errorMsg = "This is not a leaf list element.";
214 _pass(context) = false;
215 }
216 }
217};
218
Václav Kubernát8028f942019-09-25 16:03:23 +0200219struct space_separator_class {
220 template <typename T, typename Iterator, typename Context>
221 void on_success(Iterator const&, Iterator const&, T&, Context const& context)
222 {
223 auto& parserContext = x3::get<parser_context_tag>(context);
224 parserContext.m_suggestions.clear();
Václav Kubernát1ed4aa32020-01-23 13:13:28 +0100225 parserContext.m_completionIterator = boost::none;
Václav Kubernát8028f942019-09-25 16:03:23 +0200226 }
227};
228
Václav Kubernát6797df02019-03-18 20:21:50 +0100229struct create_class {
Václav Kubernátb61336d2018-05-28 17:35:03 +0200230 template <typename Iterator, typename Exception, typename Context>
Václav Kubernát41378452018-06-06 16:29:40 +0200231 x3::error_handler_result on_error(Iterator&, Iterator const&, Exception const&, Context const& context)
Václav Kubernátb61336d2018-05-28 17:35:03 +0200232 {
233 auto& parserContext = x3::get<parser_context_tag>(context);
Václav Kubernát41378452018-06-06 16:29:40 +0200234 if (parserContext.m_errorMsg.empty())
235 parserContext.m_errorMsg = "Couldn't parse create/delete command.";
236 return x3::error_handler_result::rethrow;
Václav Kubernátb61336d2018-05-28 17:35:03 +0200237 }
238};
239
Václav Kubernát6797df02019-03-18 20:21:50 +0100240struct delete_class {
241 template <typename Iterator, typename Exception, typename Context>
242 x3::error_handler_result on_error(Iterator&, Iterator const&, Exception const&, Context const& context)
243 {
244 auto& parserContext = x3::get<parser_context_tag>(context);
245 if (parserContext.m_errorMsg.empty())
246 parserContext.m_errorMsg = "Couldn't parse create/delete command.";
247 return x3::error_handler_result::rethrow;
248 }
Václav Kubernátb61336d2018-05-28 17:35:03 +0200249};
250
Václav Kubernátabf52802020-05-19 01:31:17 +0200251struct writable_leaf_path_class {
Václav Kubernát0b0272f2018-06-13 14:13:08 +0200252 template <typename T, typename Iterator, typename Context>
Václav Kubernát7707cae2020-01-16 12:04:53 +0100253 void on_success(Iterator const&, Iterator const&, T&, Context const& context)
Václav Kubernát0b0272f2018-06-13 14:13:08 +0200254 {
255 auto& parserContext = x3::get<parser_context_tag>(context);
Václav Kubernát0d42c512020-05-20 21:18:39 +0200256 if (parserContext.currentSchemaPath().m_nodes.empty()) {
257 parserContext.m_errorMsg = "This is not a path to leaf.";
258 _pass(context) = false;
259 return;
260 }
261
Václav Kubernát0b0272f2018-06-13 14:13:08 +0200262 try {
Václav Kubernát7707cae2020-01-16 12:04:53 +0100263 auto lastNode = parserContext.currentSchemaPath().m_nodes.back();
Václav Kubernátb5ca1542020-05-27 01:03:54 +0200264 auto leaf = std::get<leaf_>(lastNode.m_suffix);
Václav Kubernát7707cae2020-01-16 12:04:53 +0100265 auto location = pathWithoutLastNode(parserContext.currentSchemaPath());
266 ModuleNodePair node{lastNode.m_prefix.flat_map([](const auto& it) { return boost::optional<std::string>{it.m_name}; }), leaf.m_name};
267
268 parserContext.m_tmpListKeyLeafPath.m_location = location;
269 parserContext.m_tmpListKeyLeafPath.m_node = node;
270
Václav Kubernátb5ca1542020-05-27 01:03:54 +0200271 } catch (std::bad_variant_access&) {
Václav Kubernát0b0272f2018-06-13 14:13:08 +0200272 parserContext.m_errorMsg = "This is not a path to leaf.";
273 _pass(context) = false;
274 }
275 }
276};
277
Václav Kubernáteeb38842019-03-20 19:46:05 +0100278// This handler only checks if the module exists
279// It doesn't set any ParserContext flags (except the error message)
280struct data_module_prefix_class {
281 template <typename T, typename Iterator, typename Context>
282 void on_success(Iterator const&, Iterator const&, T& ast, Context const& context)
283 {
284 auto& parserContext = x3::get<parser_context_tag>(context);
285 const auto& schema = parserContext.m_schema;
286
Václav Kubernát72749c62020-01-03 16:47:34 +0100287 if (!schema.isModule(parserContext.currentSchemaPath(), ast.m_name)) {
Václav Kubernáteeb38842019-03-20 19:46:05 +0100288 parserContext.m_errorMsg = "Invalid module name.";
289 _pass(context) = false;
290 }
291 }
292};
293
Václav Kubernát07204242018-06-04 18:12:09 +0200294struct set_class {
Václav Kubernát07204242018-06-04 18:12:09 +0200295 template <typename Iterator, typename Exception, typename Context>
296 x3::error_handler_result on_error(Iterator&, Iterator const&, Exception const& x, Context const& context)
297 {
298 auto& parserContext = x3::get<parser_context_tag>(context);
Václav Kubernát41378452018-06-06 16:29:40 +0200299 if (parserContext.m_errorMsg.empty())
300 parserContext.m_errorMsg = "Expected " + x.which() + " here:";
301 return x3::error_handler_result::rethrow;
Václav Kubernát07204242018-06-04 18:12:09 +0200302 }
303};
304
Václav Kubernát812ee282018-08-30 17:10:03 +0200305struct commit_class;
306
Václav Kubernát9cfcd872020-02-18 12:34:02 +0100307struct describe_class;
308
Václav Kubernát054cc992019-02-21 14:23:52 +0100309struct help_class;
310
Václav Kubernátb6ff0b62018-08-30 16:14:53 +0200311struct get_class;
312
Václav Kubernát7160a132020-04-03 02:11:01 +0200313struct copy_class;
314
Václav Kubernátb61336d2018-05-28 17:35:03 +0200315struct command_class {
316 template <typename Iterator, typename Exception, typename Context>
317 x3::error_handler_result on_error(Iterator&, Iterator const&, Exception const& x, Context const& context)
318 {
319 auto& parserContext = x3::get<parser_context_tag>(context);
320 auto& error_handler = x3::get<x3::error_handler_tag>(context).get();
Václav Kubernát41378452018-06-06 16:29:40 +0200321 if (parserContext.m_errorMsg.empty()) {
322 parserContext.m_errorMsg = "Unknown command.";
323 }
324 error_handler(x.where(), parserContext.m_errorMsg);
Václav Kubernátb61336d2018-05-28 17:35:03 +0200325 return x3::error_handler_result::fail;
326 }
327};
Václav Kubernát5c75b252018-10-10 18:33:47 +0200328
Václav Kubernát4108e0d2018-10-29 13:32:22 +0100329struct initializePath_class {
Václav Kubernát5c75b252018-10-10 18:33:47 +0200330 template <typename T, typename Iterator, typename Context>
331 void on_success(Iterator const&, Iterator const&, T&, Context const& context)
332 {
333 auto& parserContext = x3::get<parser_context_tag>(context);
Václav Kubernát72749c62020-01-03 16:47:34 +0100334 parserContext.resetPath();
Václav Kubernát5c75b252018-10-10 18:33:47 +0200335 parserContext.m_tmpListKeys.clear();
336 parserContext.m_tmpListName.clear();
Václav Kubernát4108e0d2018-10-29 13:32:22 +0100337 parserContext.m_suggestions.clear();
Václav Kubernát5c75b252018-10-10 18:33:47 +0200338 }
339};
Václav Kubernátd6fd2492018-11-19 15:11:16 +0100340
341struct trailingSlash_class;
Václav Kubernát4108e0d2018-10-29 13:32:22 +0100342
Václav Kubernátcb3af402020-02-12 16:49:17 +0100343std::set<Completion> generateMissingKeyCompletionSet(std::set<std::string> keysNeeded, std::map<std::string, leaf_data_> currentSet);
Václav Kubernát329c6c32019-02-06 16:41:53 +0100344
345struct createKeySuggestions_class {
346 template <typename T, typename Iterator, typename Context>
347 void on_success(Iterator const& begin, Iterator const&, T&, Context const& context)
348 {
349 auto& parserContext = x3::get<parser_context_tag>(context);
350 const auto& schema = parserContext.m_schema;
351
352 parserContext.m_completionIterator = begin;
353
Václav Kubernát72749c62020-01-03 16:47:34 +0100354 const auto& keysNeeded = schema.listKeys(parserContext.currentSchemaPath(), {parserContext.m_curModule, parserContext.m_tmpListName});
Václav Kubernát329c6c32019-02-06 16:41:53 +0100355 parserContext.m_suggestions = generateMissingKeyCompletionSet(keysNeeded, parserContext.m_tmpListKeys);
356 }
357};
358
Václav Kubernát43908fb2020-01-02 19:05:51 +0100359std::string leafDataToCompletion(const leaf_data_& value);
360
361struct createValueSuggestions_class {
362 template <typename T, typename Iterator, typename Context>
363 void on_success(Iterator const& begin, Iterator const&, T&, Context const& context)
364 {
365 auto& parserContext = x3::get<parser_context_tag>(context);
366 if (!parserContext.m_completing) {
367 return;
368 }
369 const auto& dataQuery = parserContext.m_dataquery;
370
371 parserContext.m_completionIterator = begin;
372 auto listInstances = dataQuery->listKeys(parserContext.currentDataPath(), {parserContext.m_curModule, parserContext.m_tmpListName});
373
374 decltype(listInstances) filteredInstances;
375 //This filters out instances, which don't correspond to the partial instance we have.
376 const auto partialFitsComplete = [&parserContext] (const auto& complete) {
377 const auto& partial = parserContext.m_tmpListKeys;
378 return std::all_of(partial.begin(), partial.end(), [&complete] (const auto& oneKV) {
379 const auto& [k, v] = oneKV;
380 return complete.at(k) == v;
381 });
382 };
383 std::copy_if(listInstances.begin(), listInstances.end(), std::inserter(filteredInstances, filteredInstances.end()), partialFitsComplete);
384
Václav Kubernátcb3af402020-02-12 16:49:17 +0100385 std::set<Completion> validValues;
Václav Kubernát43908fb2020-01-02 19:05:51 +0100386
Václav Kubernátcb3af402020-02-12 16:49:17 +0100387 std::transform(filteredInstances.begin(), filteredInstances.end(), std::inserter(validValues, validValues.end()), [&parserContext](const auto& instance) {
388 return Completion{leafDataToCompletion(instance.at(parserContext.m_tmpListKeyLeafPath.m_node.second))};
Václav Kubernát43908fb2020-01-02 19:05:51 +0100389 });
390
391 parserContext.m_suggestions = validValues;
392 }
393};
394
Václav Kubernát329c6c32019-02-06 16:41:53 +0100395struct suggestKeysEnd_class {
396 template <typename T, typename Iterator, typename Context>
397 void on_success(Iterator const& begin, Iterator const&, T&, Context const& context)
398 {
399 auto& parserContext = x3::get<parser_context_tag>(context);
400 const auto& schema = parserContext.m_schema;
401
402 parserContext.m_completionIterator = begin;
Václav Kubernát72749c62020-01-03 16:47:34 +0100403 const auto& keysNeeded = schema.listKeys(parserContext.currentSchemaPath(), {parserContext.m_curModule, parserContext.m_tmpListName});
Václav Kubernát329c6c32019-02-06 16:41:53 +0100404 if (generateMissingKeyCompletionSet(keysNeeded, parserContext.m_tmpListKeys).empty()) {
Václav Kubernátcb3af402020-02-12 16:49:17 +0100405 parserContext.m_suggestions = {Completion{"]/"}};
Václav Kubernát329c6c32019-02-06 16:41:53 +0100406 } else {
Václav Kubernátcb3af402020-02-12 16:49:17 +0100407 parserContext.m_suggestions = {Completion{"]["}};
Václav Kubernát329c6c32019-02-06 16:41:53 +0100408 }
409 }
410};
Václav Kubernát57272422019-02-08 12:48:24 +0100411
Václav Kubernát672889c2020-05-27 04:11:36 +0200412template <typename T>
413std::string commandNamesVisitor (boost::type<T>)
414{
415 return T::name;
416}
Václav Kubernát57272422019-02-08 12:48:24 +0100417
418struct createCommandSuggestions_class {
419 template <typename T, typename Iterator, typename Context>
420 void on_success(Iterator const& begin, Iterator const&, T&, Context const& context)
421 {
422 auto& parserContext = x3::get<parser_context_tag>(context);
423 parserContext.m_completionIterator = begin;
424
425 parserContext.m_suggestions.clear();
426 boost::mpl::for_each<CommandTypes, boost::type<boost::mpl::_>>([&parserContext](auto cmd) {
Václav Kubernát672889c2020-05-27 04:11:36 +0200427 parserContext.m_suggestions.insert({commandNamesVisitor(cmd), " "});
Václav Kubernát57272422019-02-08 12:48:24 +0100428 });
429 }
430};
Václav Kubernátac035d62019-02-18 10:59:08 +0100431
432struct completing_class {
433 template <typename T, typename Iterator, typename Context>
434 void on_success(Iterator const&, Iterator const&, T&, Context const& context)
435 {
436 auto& parserContext = x3::get<parser_context_tag>(context);
437
438 if (!parserContext.m_completing)
439 _pass(context) = false;
440 }
441};