blob: ab9958ac4e6e8deef2e963245509c3153df5cf17 [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át2eaceb82018-10-08 19:56:30 +0200123struct list_class {
124 template <typename T, typename Iterator, typename Context>
125 void on_success(Iterator const&, Iterator const&, T& ast, Context const& context)
126 {
127 auto& parserContext = x3::get<parser_context_tag>(context);
128 const Schema& schema = parserContext.m_schema;
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200129
Václav Kubernát72749c62020-01-03 16:47:34 +0100130 if (!schema.isList(parserContext.currentSchemaPath(), {parserContext.m_curModule, ast.m_name})) {
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200131 _pass(context) = false;
132 }
133 }
134};
Václav Kubernát744f57f2018-06-29 22:46:26 +0200135
Václav Kubernát744f57f2018-06-29 22:46:26 +0200136struct module_class {
137 template <typename T, typename Iterator, typename Context>
138 void on_success(Iterator const&, Iterator const&, T& ast, Context const& context)
139 {
140 auto& parserContext = x3::get<parser_context_tag>(context);
141 const auto& schema = parserContext.m_schema;
142
Václav Kubernát75877de2019-11-20 17:43:02 +0100143 if (schema.isModule(ast.m_name)) {
Václav Kubernát744f57f2018-06-29 22:46:26 +0200144 parserContext.m_curModule = ast.m_name;
145 parserContext.m_topLevelModulePresent = true;
Václav Kubernát07204242018-06-04 18:12:09 +0200146 } else {
Václav Kubernát744f57f2018-06-29 22:46:26 +0200147 parserContext.m_errorMsg = "Invalid module name.";
Václav Kubernát07204242018-06-04 18:12:09 +0200148 _pass(context) = false;
149 }
150 }
151};
152
Václav Kubernát39ae9592019-02-05 17:35:16 +0100153struct dataNodeList_class {
154 template <typename T, typename Iterator, typename Context>
155 void on_success(Iterator const&, Iterator const&, T& ast, Context const& context)
156 {
157 auto& parserContext = x3::get<parser_context_tag>(context);
Václav Kubernát72749c62020-01-03 16:47:34 +0100158 parserContext.pushPathFragment(ast);
Václav Kubernát39ae9592019-02-05 17:35:16 +0100159 }
160};
Václav Kubernát5c75b252018-10-10 18:33:47 +0200161
Václav Kubernát37171a12018-08-31 17:01:48 +0200162struct absoluteStart_class {
163 template <typename T, typename Iterator, typename Context>
164 void on_success(Iterator const&, Iterator const&, T&, Context const& context)
165 {
166 auto& parserContext = x3::get<parser_context_tag>(context);
Václav Kubernát72749c62020-01-03 16:47:34 +0100167 parserContext.clearPath();
Václav Kubernát37171a12018-08-31 17:01:48 +0200168 }
169};
170
Václav Kubernát5c75b252018-10-10 18:33:47 +0200171struct dataNodesListEnd_class;
172
173struct dataPathListEnd_class;
174
Václav Kubernát6d791432018-10-25 16:00:35 +0200175struct discard_class;
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200176
Václav Kubernát11afac72018-07-18 14:59:53 +0200177struct ls_class;
178
Václav Kubernát744f57f2018-06-29 22:46:26 +0200179struct cd_class {
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200180 template <typename Iterator, typename Exception, typename Context>
181 x3::error_handler_result on_error(Iterator&, Iterator const&, Exception const& x, Context const& context)
182 {
183 auto& parserContext = x3::get<parser_context_tag>(context);
Václav Kubernát41378452018-06-06 16:29:40 +0200184 if (parserContext.m_errorMsg.empty())
185 parserContext.m_errorMsg = "Expected " + x.which() + " here:";
186 return x3::error_handler_result::rethrow;
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200187 }
188};
Václav Kubernátb61336d2018-05-28 17:35:03 +0200189
Václav Kubernát6797df02019-03-18 20:21:50 +0100190struct presenceContainerPath_class {
Václav Kubernátb61336d2018-05-28 17:35:03 +0200191 template <typename T, typename Iterator, typename Context>
192 void on_success(Iterator const&, Iterator const&, T& ast, Context const& context)
193 {
194 auto& parserContext = x3::get<parser_context_tag>(context);
195 const auto& schema = parserContext.m_schema;
196 try {
Václav Kubernát744f57f2018-06-29 22:46:26 +0200197 boost::optional<std::string> module;
Václav Kubernát6797df02019-03-18 20:21:50 +0100198 if (ast.m_nodes.back().m_prefix)
199 module = ast.m_nodes.back().m_prefix.value().m_name;
200 container_ cont = boost::get<container_>(ast.m_nodes.back().m_suffix);
Václav Kubernát72749c62020-01-03 16:47:34 +0100201 auto location = pathWithoutLastNode(parserContext.currentSchemaPath());
Václav Kubernátb61336d2018-05-28 17:35:03 +0200202
Václav Kubernát744f57f2018-06-29 22:46:26 +0200203 if (!schema.isPresenceContainer(location, {module, cont.m_name})) {
Václav Kubernát41378452018-06-06 16:29:40 +0200204 parserContext.m_errorMsg = "This container is not a presence container.";
Václav Kubernátb61336d2018-05-28 17:35:03 +0200205 _pass(context) = false;
Václav Kubernátb61336d2018-05-28 17:35:03 +0200206 }
207 } catch (boost::bad_get&) {
Václav Kubernát41378452018-06-06 16:29:40 +0200208 parserContext.m_errorMsg = "This is not a container.";
Václav Kubernátb61336d2018-05-28 17:35:03 +0200209 _pass(context) = false;
Václav Kubernátb61336d2018-05-28 17:35:03 +0200210 }
211 }
Václav Kubernát6797df02019-03-18 20:21:50 +0100212};
Václav Kubernátb61336d2018-05-28 17:35:03 +0200213
Václav Kubernátf5f64f02019-03-19 17:15:47 +0100214struct listInstancePath_class {
215 template <typename T, typename Iterator, typename Context>
216 void on_success(Iterator const&, Iterator const&, T& ast, Context const& context)
217 {
218 auto& parserContext = x3::get<parser_context_tag>(context);
219 if (ast.m_nodes.back().m_suffix.type() != typeid(listElement_)) {
220 parserContext.m_errorMsg = "This is not a list instance.";
221 _pass(context) = false;
222 }
223 }
224};
225
Václav Kubernát8028f942019-09-25 16:03:23 +0200226struct space_separator_class {
227 template <typename T, typename Iterator, typename Context>
228 void on_success(Iterator const&, Iterator const&, T&, Context const& context)
229 {
230 auto& parserContext = x3::get<parser_context_tag>(context);
231 parserContext.m_suggestions.clear();
Václav Kubernát1ed4aa32020-01-23 13:13:28 +0100232 parserContext.m_completionIterator = boost::none;
Václav Kubernát8028f942019-09-25 16:03:23 +0200233 }
234};
235
Václav Kubernát6797df02019-03-18 20:21:50 +0100236struct create_class {
Václav Kubernátb61336d2018-05-28 17:35:03 +0200237 template <typename Iterator, typename Exception, typename Context>
Václav Kubernát41378452018-06-06 16:29:40 +0200238 x3::error_handler_result on_error(Iterator&, Iterator const&, Exception const&, Context const& context)
Václav Kubernátb61336d2018-05-28 17:35:03 +0200239 {
240 auto& parserContext = x3::get<parser_context_tag>(context);
Václav Kubernát41378452018-06-06 16:29:40 +0200241 if (parserContext.m_errorMsg.empty())
242 parserContext.m_errorMsg = "Couldn't parse create/delete command.";
243 return x3::error_handler_result::rethrow;
Václav Kubernátb61336d2018-05-28 17:35:03 +0200244 }
245};
246
Václav Kubernát6797df02019-03-18 20:21:50 +0100247struct delete_class {
248 template <typename Iterator, typename Exception, typename Context>
249 x3::error_handler_result on_error(Iterator&, Iterator const&, Exception const&, Context const& context)
250 {
251 auto& parserContext = x3::get<parser_context_tag>(context);
252 if (parserContext.m_errorMsg.empty())
253 parserContext.m_errorMsg = "Couldn't parse create/delete command.";
254 return x3::error_handler_result::rethrow;
255 }
Václav Kubernátb61336d2018-05-28 17:35:03 +0200256};
257
Václav Kubernát0b0272f2018-06-13 14:13:08 +0200258struct leaf_path_class {
259 template <typename T, typename Iterator, typename Context>
Václav Kubernát7707cae2020-01-16 12:04:53 +0100260 void on_success(Iterator const&, Iterator const&, T&, Context const& context)
Václav Kubernát0b0272f2018-06-13 14:13:08 +0200261 {
262 auto& parserContext = x3::get<parser_context_tag>(context);
263 try {
Václav Kubernát7707cae2020-01-16 12:04:53 +0100264 auto lastNode = parserContext.currentSchemaPath().m_nodes.back();
265 auto leaf = boost::get<leaf_>(lastNode.m_suffix);
266 auto location = pathWithoutLastNode(parserContext.currentSchemaPath());
267 ModuleNodePair node{lastNode.m_prefix.flat_map([](const auto& it) { return boost::optional<std::string>{it.m_name}; }), leaf.m_name};
268
269 parserContext.m_tmpListKeyLeafPath.m_location = location;
270 parserContext.m_tmpListKeyLeafPath.m_node = node;
271
Václav Kubernát0b0272f2018-06-13 14:13:08 +0200272 } catch (boost::bad_get&) {
273 parserContext.m_errorMsg = "This is not a path to leaf.";
274 _pass(context) = false;
275 }
276 }
277};
278
Václav Kubernáteeb38842019-03-20 19:46:05 +0100279// This handler only checks if the module exists
280// It doesn't set any ParserContext flags (except the error message)
281struct data_module_prefix_class {
282 template <typename T, typename Iterator, typename Context>
283 void on_success(Iterator const&, Iterator const&, T& ast, Context const& context)
284 {
285 auto& parserContext = x3::get<parser_context_tag>(context);
286 const auto& schema = parserContext.m_schema;
287
Václav Kubernát72749c62020-01-03 16:47:34 +0100288 if (!schema.isModule(parserContext.currentSchemaPath(), ast.m_name)) {
Václav Kubernáteeb38842019-03-20 19:46:05 +0100289 parserContext.m_errorMsg = "Invalid module name.";
290 _pass(context) = false;
291 }
292 }
293};
294
Václav Kubernát07204242018-06-04 18:12:09 +0200295struct set_class {
Václav Kubernát07204242018-06-04 18:12:09 +0200296 template <typename Iterator, typename Exception, typename Context>
297 x3::error_handler_result on_error(Iterator&, Iterator const&, Exception const& x, Context const& context)
298 {
299 auto& parserContext = x3::get<parser_context_tag>(context);
Václav Kubernát41378452018-06-06 16:29:40 +0200300 if (parserContext.m_errorMsg.empty())
301 parserContext.m_errorMsg = "Expected " + x.which() + " here:";
302 return x3::error_handler_result::rethrow;
Václav Kubernát07204242018-06-04 18:12:09 +0200303 }
304};
305
Václav Kubernát812ee282018-08-30 17:10:03 +0200306struct commit_class;
307
Václav Kubernát9cfcd872020-02-18 12:34:02 +0100308struct describe_class;
309
Václav Kubernát054cc992019-02-21 14:23:52 +0100310struct help_class;
311
Václav Kubernátb6ff0b62018-08-30 16:14:53 +0200312struct get_class;
313
Václav Kubernát7160a132020-04-03 02:11:01 +0200314struct copy_class;
315
Václav Kubernátb61336d2018-05-28 17:35:03 +0200316struct command_class {
317 template <typename Iterator, typename Exception, typename Context>
318 x3::error_handler_result on_error(Iterator&, Iterator const&, Exception const& x, Context const& context)
319 {
320 auto& parserContext = x3::get<parser_context_tag>(context);
321 auto& error_handler = x3::get<x3::error_handler_tag>(context).get();
Václav Kubernát41378452018-06-06 16:29:40 +0200322 if (parserContext.m_errorMsg.empty()) {
323 parserContext.m_errorMsg = "Unknown command.";
324 }
325 error_handler(x.where(), parserContext.m_errorMsg);
Václav Kubernátb61336d2018-05-28 17:35:03 +0200326 return x3::error_handler_result::fail;
327 }
328};
Václav Kubernát5c75b252018-10-10 18:33:47 +0200329
Václav Kubernát4108e0d2018-10-29 13:32:22 +0100330struct initializePath_class {
Václav Kubernát5c75b252018-10-10 18:33:47 +0200331 template <typename T, typename Iterator, typename Context>
332 void on_success(Iterator const&, Iterator const&, T&, Context const& context)
333 {
334 auto& parserContext = x3::get<parser_context_tag>(context);
Václav Kubernát72749c62020-01-03 16:47:34 +0100335 parserContext.resetPath();
Václav Kubernát5c75b252018-10-10 18:33:47 +0200336 parserContext.m_tmpListKeys.clear();
337 parserContext.m_tmpListName.clear();
Václav Kubernát4108e0d2018-10-29 13:32:22 +0100338 parserContext.m_suggestions.clear();
Václav Kubernát5c75b252018-10-10 18:33:47 +0200339 }
340};
Václav Kubernátd6fd2492018-11-19 15:11:16 +0100341
342struct trailingSlash_class;
Václav Kubernát4108e0d2018-10-29 13:32:22 +0100343
344struct createPathSuggestions_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;
Václav Kubernát3a823f42020-04-29 23:40:21 +0200352 auto suggestions = schema.availableNodes(parserContext.currentSchemaPath(), Recursion::NonRecursive);
Václav Kubernátcb3af402020-02-12 16:49:17 +0100353 std::set<Completion> suffixesAdded;
Václav Kubernáte69133a2019-11-01 19:01:34 +0100354 std::transform(suggestions.begin(), suggestions.end(),
Václav Kubernát95b08872020-04-28 01:04:17 +0200355 std::inserter(suffixesAdded, suffixesAdded.end()),
356 [&parserContext, &schema](const ModuleNodePair& node) {
357 std::string completion = (node.first ? *node.first + ":" : "") + node.second;
Václav Kubernáte69133a2019-11-01 19:01:34 +0100358
Václav Kubernát95b08872020-04-28 01:04:17 +0200359 if (schema.isLeaf(parserContext.currentSchemaPath(), node)) {
360 return Completion{completion + " "};
361 }
362 if (schema.isContainer(parserContext.currentSchemaPath(), node)) {
363 return Completion{completion + "/"};
364 }
365 if (schema.isList(parserContext.currentSchemaPath(), node)) {
366 return Completion{completion, "[", Completion::WhenToAdd::IfFullMatch};
367 }
368 return Completion{completion};
Václav Kubernáte69133a2019-11-01 19:01:34 +0100369 });
370 parserContext.m_suggestions = suffixesAdded;
Václav Kubernát4108e0d2018-10-29 13:32:22 +0100371 }
372};
Václav Kubernát329c6c32019-02-06 16:41:53 +0100373
Václav Kubernátcb3af402020-02-12 16:49:17 +0100374std::set<Completion> generateMissingKeyCompletionSet(std::set<std::string> keysNeeded, std::map<std::string, leaf_data_> currentSet);
Václav Kubernát329c6c32019-02-06 16:41:53 +0100375
376struct createKeySuggestions_class {
377 template <typename T, typename Iterator, typename Context>
378 void on_success(Iterator const& begin, Iterator const&, T&, Context const& context)
379 {
380 auto& parserContext = x3::get<parser_context_tag>(context);
381 const auto& schema = parserContext.m_schema;
382
383 parserContext.m_completionIterator = begin;
384
Václav Kubernát72749c62020-01-03 16:47:34 +0100385 const auto& keysNeeded = schema.listKeys(parserContext.currentSchemaPath(), {parserContext.m_curModule, parserContext.m_tmpListName});
Václav Kubernát329c6c32019-02-06 16:41:53 +0100386 parserContext.m_suggestions = generateMissingKeyCompletionSet(keysNeeded, parserContext.m_tmpListKeys);
387 }
388};
389
Václav Kubernát43908fb2020-01-02 19:05:51 +0100390std::string leafDataToCompletion(const leaf_data_& value);
391
392struct createValueSuggestions_class {
393 template <typename T, typename Iterator, typename Context>
394 void on_success(Iterator const& begin, Iterator const&, T&, Context const& context)
395 {
396 auto& parserContext = x3::get<parser_context_tag>(context);
397 if (!parserContext.m_completing) {
398 return;
399 }
400 const auto& dataQuery = parserContext.m_dataquery;
401
402 parserContext.m_completionIterator = begin;
403 auto listInstances = dataQuery->listKeys(parserContext.currentDataPath(), {parserContext.m_curModule, parserContext.m_tmpListName});
404
405 decltype(listInstances) filteredInstances;
406 //This filters out instances, which don't correspond to the partial instance we have.
407 const auto partialFitsComplete = [&parserContext] (const auto& complete) {
408 const auto& partial = parserContext.m_tmpListKeys;
409 return std::all_of(partial.begin(), partial.end(), [&complete] (const auto& oneKV) {
410 const auto& [k, v] = oneKV;
411 return complete.at(k) == v;
412 });
413 };
414 std::copy_if(listInstances.begin(), listInstances.end(), std::inserter(filteredInstances, filteredInstances.end()), partialFitsComplete);
415
Václav Kubernátcb3af402020-02-12 16:49:17 +0100416 std::set<Completion> validValues;
Václav Kubernát43908fb2020-01-02 19:05:51 +0100417
Václav Kubernátcb3af402020-02-12 16:49:17 +0100418 std::transform(filteredInstances.begin(), filteredInstances.end(), std::inserter(validValues, validValues.end()), [&parserContext](const auto& instance) {
419 return Completion{leafDataToCompletion(instance.at(parserContext.m_tmpListKeyLeafPath.m_node.second))};
Václav Kubernát43908fb2020-01-02 19:05:51 +0100420 });
421
422 parserContext.m_suggestions = validValues;
423 }
424};
425
Václav Kubernát329c6c32019-02-06 16:41:53 +0100426struct suggestKeysEnd_class {
427 template <typename T, typename Iterator, typename Context>
428 void on_success(Iterator const& begin, Iterator const&, T&, Context const& context)
429 {
430 auto& parserContext = x3::get<parser_context_tag>(context);
431 const auto& schema = parserContext.m_schema;
432
433 parserContext.m_completionIterator = begin;
Václav Kubernát72749c62020-01-03 16:47:34 +0100434 const auto& keysNeeded = schema.listKeys(parserContext.currentSchemaPath(), {parserContext.m_curModule, parserContext.m_tmpListName});
Václav Kubernát329c6c32019-02-06 16:41:53 +0100435 if (generateMissingKeyCompletionSet(keysNeeded, parserContext.m_tmpListKeys).empty()) {
Václav Kubernátcb3af402020-02-12 16:49:17 +0100436 parserContext.m_suggestions = {Completion{"]/"}};
Václav Kubernát329c6c32019-02-06 16:41:53 +0100437 } else {
Václav Kubernátcb3af402020-02-12 16:49:17 +0100438 parserContext.m_suggestions = {Completion{"]["}};
Václav Kubernát329c6c32019-02-06 16:41:53 +0100439 }
440 }
441};
Václav Kubernát57272422019-02-08 12:48:24 +0100442
443struct commandNamesVisitor {
444 template <typename T>
Václav Kubernátcb3af402020-02-12 16:49:17 +0100445 std::string operator()(boost::type<T>)
Václav Kubernát57272422019-02-08 12:48:24 +0100446 {
447 return T::name;
448 }
449};
450
451struct createCommandSuggestions_class {
452 template <typename T, typename Iterator, typename Context>
453 void on_success(Iterator const& begin, Iterator const&, T&, Context const& context)
454 {
455 auto& parserContext = x3::get<parser_context_tag>(context);
456 parserContext.m_completionIterator = begin;
457
458 parserContext.m_suggestions.clear();
459 boost::mpl::for_each<CommandTypes, boost::type<boost::mpl::_>>([&parserContext](auto cmd) {
Václav Kubernát0165e6c2020-02-17 18:18:14 +0100460 parserContext.m_suggestions.insert({commandNamesVisitor()(cmd), " "});
Václav Kubernát57272422019-02-08 12:48:24 +0100461 });
462 }
463};
Václav Kubernátac035d62019-02-18 10:59:08 +0100464
465struct completing_class {
466 template <typename T, typename Iterator, typename Context>
467 void on_success(Iterator const&, Iterator const&, T&, Context const& context)
468 {
469 auto& parserContext = x3::get<parser_context_tag>(context);
470
471 if (!parserContext.m_completing)
472 _pass(context) = false;
473 }
474};