blob: 352824e975449b9fa813c943680109ea7bb24e04 [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átf3e377b2020-05-28 23:26:38 +020050struct node_identifier_class;
Václav Kubernát744f57f2018-06-29 22:46:26 +020051
Václav Kubernát2db124c2020-05-28 21:58:36 +020052boost::optional<std::string> optModuleToOptString(const boost::optional<module_> module);
53
Václav Kubernát7707cae2020-01-16 12:04:53 +010054struct key_identifier_class {
55 template <typename T, typename Iterator, typename Context>
56 void on_success(Iterator const&, Iterator const&, T& ast, Context const& context)
57 {
Václav Kubernát2db124c2020-05-28 21:58:36 +020058 ParserContext& parserContext = x3::get<parser_context_tag>(context);
Václav Kubernát7707cae2020-01-16 12:04:53 +010059 const Schema& schema = parserContext.m_schema;
Václav Kubernát7707cae2020-01-16 12:04:53 +010060
Václav Kubernát2db124c2020-05-28 21:58:36 +020061 if (schema.listHasKey(dataPathToSchemaPath(parserContext.m_tmpListPath), ast)) {
62 parserContext.m_tmpListKeyLeafPath.m_location = dataPathToSchemaPath(parserContext.m_tmpListPath);
Václav Kubernátb4e5b182020-11-16 19:55:09 +010063 parserContext.m_tmpListKeyLeafPath.m_node = {optModuleToOptString(parserContext.m_tmpListPath.m_nodes.back().m_prefix), ast};
Václav Kubernát7707cae2020-01-16 12:04:53 +010064 } else {
Václav Kubernát2db124c2020-05-28 21:58:36 +020065 auto listName = std::get<list_>(parserContext.m_tmpListPath.m_nodes.back().m_suffix).m_name;
66 parserContext.m_errorMsg = listName + " is not indexed by \"" + ast + "\".";
Václav Kubernát7707cae2020-01-16 12:04:53 +010067 _pass(context) = false;
68 }
69 }
70};
Václav Kubernát89728d82018-09-13 16:28:28 +020071
Václav Kubernát744f57f2018-06-29 22:46:26 +020072struct module_identifier_class;
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020073
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020074struct listSuffix_class {
75 template <typename T, typename Iterator, typename Context>
76 void on_success(Iterator const&, Iterator const&, T& ast, Context const& context)
77 {
78 auto& parserContext = x3::get<parser_context_tag>(context);
Václav Kubernát48fc3832018-05-28 14:21:22 +020079 const Schema& schema = parserContext.m_schema;
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020080
Václav Kubernát2db124c2020-05-28 21:58:36 +020081 const auto& keysNeeded = schema.listKeys(dataPathToSchemaPath(parserContext.m_tmpListPath));
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020082 std::set<std::string> keysSupplied;
Václav Kubernát3a433232020-07-08 17:52:50 +020083 for (const auto& it : ast) {
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020084 keysSupplied.insert(it.first);
Václav Kubernát3a433232020-07-08 17:52:50 +020085 }
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020086
87 if (keysNeeded != keysSupplied) {
Václav Kubernát2db124c2020-05-28 21:58:36 +020088 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át0a2a2e82018-05-11 13:59:12 +020090 "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át3a433232020-07-08 17:52:50 +020096 for (const auto& it : missingKeys) {
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020097 parserContext.m_errorMsg += " " + it;
Václav Kubernát3a433232020-07-08 17:52:50 +020098 }
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020099 parserContext.m_errorMsg += ".";
100
101 _pass(context) = false;
Václav Kubernátbf65dd72020-05-28 02:32:31 +0200102 return;
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200103 }
Václav Kubernátbf65dd72020-05-28 02:32:31 +0200104
105 // Clean up after listSuffix, in case someone wants to parse more listSuffixes
106 parserContext.m_tmpListKeys.clear();
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200107 }
Václav Kubernát41378452018-06-06 16:29:40 +0200108
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át3a433232020-07-08 17:52:50 +0200113 if (parserContext.m_errorMsg.empty()) {
Václav Kubernát41378452018-06-06 16:29:40 +0200114 parserContext.m_errorMsg = "Expecting ']' here:";
Václav Kubernát3a433232020-07-08 17:52:50 +0200115 }
Václav Kubernát41378452018-06-06 16:29:40 +0200116 return x3::error_handler_result::rethrow;
Václav Kubernát41378452018-06-06 16:29:40 +0200117 }
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200118};
Václav Kubernát744f57f2018-06-29 22:46:26 +0200119
Václav Kubernát744f57f2018-06-29 22:46:26 +0200120struct module_class {
121 template <typename T, typename Iterator, typename Context>
122 void on_success(Iterator const&, Iterator const&, T& ast, Context const& context)
123 {
124 auto& parserContext = x3::get<parser_context_tag>(context);
125 const auto& schema = parserContext.m_schema;
126
Václav Kubernát1bcee3b2020-05-28 22:19:59 +0200127 if (!schema.isModule(ast.m_name)) {
Václav Kubernát744f57f2018-06-29 22:46:26 +0200128 parserContext.m_errorMsg = "Invalid module name.";
Václav Kubernát07204242018-06-04 18:12:09 +0200129 _pass(context) = false;
130 }
131 }
132};
133
Václav Kubernát37171a12018-08-31 17:01:48 +0200134struct 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át72749c62020-01-03 16:47:34 +0100139 parserContext.clearPath();
Václav Kubernát37171a12018-08-31 17:01:48 +0200140 }
141};
142
Václav Kubernát6d791432018-10-25 16:00:35 +0200143struct discard_class;
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200144
Václav Kubernát11afac72018-07-18 14:59:53 +0200145struct ls_class;
146
Václav Kubernát744f57f2018-06-29 22:46:26 +0200147struct cd_class {
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200148 template <typename Iterator, typename Exception, typename Context>
149 x3::error_handler_result on_error(Iterator&, Iterator const&, Exception const& x, Context const& context)
150 {
151 auto& parserContext = x3::get<parser_context_tag>(context);
Václav Kubernát3a433232020-07-08 17:52:50 +0200152 if (parserContext.m_errorMsg.empty()) {
Václav Kubernát41378452018-06-06 16:29:40 +0200153 parserContext.m_errorMsg = "Expected " + x.which() + " here:";
Václav Kubernát3a433232020-07-08 17:52:50 +0200154 }
Václav Kubernát41378452018-06-06 16:29:40 +0200155 return x3::error_handler_result::rethrow;
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200156 }
157};
Václav Kubernátb61336d2018-05-28 17:35:03 +0200158
Václav Kubernát5b8e2822022-01-14 03:19:54 +0100159#if BOOST_VERSION >= 107800
160template <typename Iterator>
161[[nodiscard]] auto makeIteratorRollbacker(Iterator const& was, Iterator& will, bool& passFlag)
162{
163 return make_unique_resource([] {},
164 [&was, &will, &passFlag] {
165 if (!passFlag) {
166 will = was;
167 }
168 });
169}
170#endif
171
Václav Kubernát6797df02019-03-18 20:21:50 +0100172struct presenceContainerPath_class {
Václav Kubernátb61336d2018-05-28 17:35:03 +0200173 template <typename T, typename Iterator, typename Context>
Václav Kubernát5b8e2822022-01-14 03:19:54 +0100174#if BOOST_VERSION >= 107800
175 void on_success(Iterator const& was, Iterator& will, T& ast, Context const& context)
176#else
Václav Kubernátb61336d2018-05-28 17:35:03 +0200177 void on_success(Iterator const&, Iterator const&, T& ast, Context const& context)
Václav Kubernát5b8e2822022-01-14 03:19:54 +0100178#endif
Václav Kubernátb61336d2018-05-28 17:35:03 +0200179 {
Václav Kubernát5b8e2822022-01-14 03:19:54 +0100180#if BOOST_VERSION >= 107800
181 auto rollback = makeIteratorRollbacker(was, will, _pass(context));
182#endif
Václav Kubernátb61336d2018-05-28 17:35:03 +0200183 auto& parserContext = x3::get<parser_context_tag>(context);
184 const auto& schema = parserContext.m_schema;
Václav Kubernát0d42c512020-05-20 21:18:39 +0200185 if (ast.m_nodes.empty()) {
186 parserContext.m_errorMsg = "This container is not a presence container.";
187 _pass(context) = false;
188 return;
189 }
190
Václav Kubernáte811bfa2020-05-29 02:25:20 +0200191 if (schema.nodeType(pathToSchemaString(parserContext.currentSchemaPath(), Prefixes::Always)) != yang::NodeTypes::PresenceContainer) {
192 parserContext.m_errorMsg = "This container is not a presence container.";
Václav Kubernátb61336d2018-05-28 17:35:03 +0200193 _pass(context) = false;
Václav Kubernátb61336d2018-05-28 17:35:03 +0200194 }
195 }
Václav Kubernát6797df02019-03-18 20:21:50 +0100196};
Václav Kubernátb61336d2018-05-28 17:35:03 +0200197
Václav Kubernátf5f64f02019-03-19 17:15:47 +0100198struct listInstancePath_class {
199 template <typename T, typename Iterator, typename Context>
Václav Kubernát5b8e2822022-01-14 03:19:54 +0100200#if BOOST_VERSION >= 107800
201 void on_success(Iterator const& was, Iterator& will, T& ast, Context const& context)
202#else
Václav Kubernátf5f64f02019-03-19 17:15:47 +0100203 void on_success(Iterator const&, Iterator const&, T& ast, Context const& context)
Václav Kubernát5b8e2822022-01-14 03:19:54 +0100204#endif
Václav Kubernátf5f64f02019-03-19 17:15:47 +0100205 {
Václav Kubernát5b8e2822022-01-14 03:19:54 +0100206#if BOOST_VERSION >= 107800
207 auto rollback = makeIteratorRollbacker(was, will, _pass(context));
208#endif
Václav Kubernátf5f64f02019-03-19 17:15:47 +0100209 auto& parserContext = x3::get<parser_context_tag>(context);
Václav Kubernát2ebab1d2020-05-27 01:28:30 +0200210 if (ast.m_nodes.empty() || !std::holds_alternative<listElement_>(ast.m_nodes.back().m_suffix)) {
Václav Kubernátf5f64f02019-03-19 17:15:47 +0100211 parserContext.m_errorMsg = "This is not a list instance.";
212 _pass(context) = false;
213 }
214 }
215};
216
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200217struct leafListElementPath_class {
218 template <typename T, typename Iterator, typename Context>
Václav Kubernát5b8e2822022-01-14 03:19:54 +0100219#if BOOST_VERSION >= 107800
220 void on_success(Iterator const& was, Iterator& will, T& ast, Context const& context)
221#else
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200222 void on_success(Iterator const&, Iterator const&, T& ast, Context const& context)
Václav Kubernát5b8e2822022-01-14 03:19:54 +0100223#endif
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200224 {
Václav Kubernát5b8e2822022-01-14 03:19:54 +0100225#if BOOST_VERSION >= 107800
226 auto rollback = makeIteratorRollbacker(was, will, _pass(context));
227#endif
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200228 auto& parserContext = x3::get<parser_context_tag>(context);
Václav Kubernátb5ca1542020-05-27 01:03:54 +0200229 if (ast.m_nodes.empty() || !std::holds_alternative<leafListElement_>(ast.m_nodes.back().m_suffix)) {
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200230 parserContext.m_errorMsg = "This is not a leaf list element.";
231 _pass(context) = false;
232 }
233 }
234};
235
Václav Kubernát8028f942019-09-25 16:03:23 +0200236struct space_separator_class {
237 template <typename T, typename Iterator, typename Context>
238 void on_success(Iterator const&, Iterator const&, T&, Context const& context)
239 {
240 auto& parserContext = x3::get<parser_context_tag>(context);
241 parserContext.m_suggestions.clear();
Václav Kubernát1ed4aa32020-01-23 13:13:28 +0100242 parserContext.m_completionIterator = boost::none;
Václav Kubernát8028f942019-09-25 16:03:23 +0200243 }
244};
245
Václav Kubernát6797df02019-03-18 20:21:50 +0100246struct create_class {
Václav Kubernátb61336d2018-05-28 17:35:03 +0200247 template <typename Iterator, typename Exception, typename Context>
Václav Kubernát41378452018-06-06 16:29:40 +0200248 x3::error_handler_result on_error(Iterator&, Iterator const&, Exception const&, Context const& context)
Václav Kubernátb61336d2018-05-28 17:35:03 +0200249 {
250 auto& parserContext = x3::get<parser_context_tag>(context);
Václav Kubernát3a433232020-07-08 17:52:50 +0200251 if (parserContext.m_errorMsg.empty()) {
Václav Kubernát41378452018-06-06 16:29:40 +0200252 parserContext.m_errorMsg = "Couldn't parse create/delete command.";
Václav Kubernát3a433232020-07-08 17:52:50 +0200253 }
Václav Kubernát41378452018-06-06 16:29:40 +0200254 return x3::error_handler_result::rethrow;
Václav Kubernátb61336d2018-05-28 17:35:03 +0200255 }
256};
257
Václav Kubernát6797df02019-03-18 20:21:50 +0100258struct delete_class {
259 template <typename Iterator, typename Exception, typename Context>
260 x3::error_handler_result on_error(Iterator&, Iterator const&, Exception const&, Context const& context)
261 {
262 auto& parserContext = x3::get<parser_context_tag>(context);
Václav Kubernát3a433232020-07-08 17:52:50 +0200263 if (parserContext.m_errorMsg.empty()) {
Václav Kubernát6797df02019-03-18 20:21:50 +0100264 parserContext.m_errorMsg = "Couldn't parse create/delete command.";
Václav Kubernát3a433232020-07-08 17:52:50 +0200265 }
Václav Kubernát6797df02019-03-18 20:21:50 +0100266 return x3::error_handler_result::rethrow;
267 }
Václav Kubernátb61336d2018-05-28 17:35:03 +0200268};
269
Václav Kubernáte7248b22020-06-26 15:38:59 +0200270struct rpcPath_class;
271
Václav Kubernátaa4250a2020-07-22 00:02:23 +0200272struct actionPath_class;
273
Václav Kubernáte7248b22020-06-26 15:38:59 +0200274struct cdPath_class;
275
Václav Kubernát31029682020-10-29 08:23:04 +0100276struct getPath_class;
277
Václav Kubernát07204242018-06-04 18:12:09 +0200278struct set_class {
Václav Kubernát07204242018-06-04 18:12:09 +0200279 template <typename Iterator, typename Exception, typename Context>
280 x3::error_handler_result on_error(Iterator&, Iterator const&, Exception const& x, Context const& context)
281 {
282 auto& parserContext = x3::get<parser_context_tag>(context);
Václav Kubernát3a433232020-07-08 17:52:50 +0200283 if (parserContext.m_errorMsg.empty()) {
Václav Kubernát41378452018-06-06 16:29:40 +0200284 parserContext.m_errorMsg = "Expected " + x.which() + " here:";
Václav Kubernát3a433232020-07-08 17:52:50 +0200285 }
Václav Kubernát41378452018-06-06 16:29:40 +0200286 return x3::error_handler_result::rethrow;
Václav Kubernát07204242018-06-04 18:12:09 +0200287 }
288};
289
Václav Kubernát812ee282018-08-30 17:10:03 +0200290struct commit_class;
291
Václav Kubernát9cfcd872020-02-18 12:34:02 +0100292struct describe_class;
293
Václav Kubernát054cc992019-02-21 14:23:52 +0100294struct help_class;
295
Václav Kubernátb6ff0b62018-08-30 16:14:53 +0200296struct get_class;
297
Václav Kubernát7160a132020-04-03 02:11:01 +0200298struct copy_class;
299
Václav Kubernátbf65dd72020-05-28 02:32:31 +0200300struct move_class;
301
Václav Kubernát70d7f7a2020-06-23 14:40:40 +0200302struct dump_class;
303
Václav Kubernátaa4250a2020-07-22 00:02:23 +0200304struct prepare_class;
305
306struct action_class;
Václav Kubernáte7248b22020-06-26 15:38:59 +0200307
308struct exec_class;
309
Václav Kubernát162165e2021-02-22 09:46:53 +0100310struct switch_class;
311
Václav Kubernáte7248b22020-06-26 15:38:59 +0200312struct cancel_class;
313
Václav Kubernátb61336d2018-05-28 17:35:03 +0200314struct command_class {
315 template <typename Iterator, typename Exception, typename Context>
316 x3::error_handler_result on_error(Iterator&, Iterator const&, Exception const& x, Context const& context)
317 {
318 auto& parserContext = x3::get<parser_context_tag>(context);
319 auto& error_handler = x3::get<x3::error_handler_tag>(context).get();
Václav Kubernát41378452018-06-06 16:29:40 +0200320 if (parserContext.m_errorMsg.empty()) {
321 parserContext.m_errorMsg = "Unknown command.";
322 }
323 error_handler(x.where(), parserContext.m_errorMsg);
Václav Kubernátb61336d2018-05-28 17:35:03 +0200324 return x3::error_handler_result::fail;
325 }
326};
Václav Kubernát5c75b252018-10-10 18:33:47 +0200327
Václav Kubernát4108e0d2018-10-29 13:32:22 +0100328struct initializePath_class {
Václav Kubernát5c75b252018-10-10 18:33:47 +0200329 template <typename T, typename Iterator, typename Context>
330 void on_success(Iterator const&, Iterator const&, T&, Context const& context)
331 {
332 auto& parserContext = x3::get<parser_context_tag>(context);
Václav Kubernát72749c62020-01-03 16:47:34 +0100333 parserContext.resetPath();
Václav Kubernátbf65dd72020-05-28 02:32:31 +0200334 parserContext.m_tmpListPath = dataPath_{};
Václav Kubernát5c75b252018-10-10 18:33:47 +0200335 parserContext.m_tmpListKeys.clear();
Václav Kubernát4108e0d2018-10-29 13:32:22 +0100336 parserContext.m_suggestions.clear();
Václav Kubernát5c75b252018-10-10 18:33:47 +0200337 }
338};
Václav Kubernátd6fd2492018-11-19 15:11:16 +0100339
340struct trailingSlash_class;
Václav Kubernát4108e0d2018-10-29 13:32:22 +0100341
Václav Kubernátc15fe822020-06-04 11:28:39 +0200342std::set<Completion> generateMissingKeyCompletionSet(std::set<std::string> keysNeeded, ListInstance currentSet);
Václav Kubernát329c6c32019-02-06 16:41:53 +0100343
344struct createKeySuggestions_class {
345 template <typename T, typename Iterator, typename Context>
346 void on_success(Iterator const& begin, Iterator const&, T&, Context const& context)
347 {
348 auto& parserContext = x3::get<parser_context_tag>(context);
349 const auto& schema = parserContext.m_schema;
350
351 parserContext.m_completionIterator = begin;
352
Václav Kubernát2db124c2020-05-28 21:58:36 +0200353 const auto& keysNeeded = schema.listKeys(dataPathToSchemaPath(parserContext.m_tmpListPath));
Václav Kubernát329c6c32019-02-06 16:41:53 +0100354 parserContext.m_suggestions = generateMissingKeyCompletionSet(keysNeeded, parserContext.m_tmpListKeys);
355 }
356};
357
Václav Kubernát43908fb2020-01-02 19:05:51 +0100358std::string leafDataToCompletion(const leaf_data_& value);
359
360struct createValueSuggestions_class {
361 template <typename T, typename Iterator, typename Context>
362 void on_success(Iterator const& begin, Iterator const&, T&, Context const& context)
363 {
364 auto& parserContext = x3::get<parser_context_tag>(context);
365 if (!parserContext.m_completing) {
366 return;
367 }
368 const auto& dataQuery = parserContext.m_dataquery;
369
370 parserContext.m_completionIterator = begin;
Václav Kubernát2db124c2020-05-28 21:58:36 +0200371 auto listInstances = dataQuery->listKeys(parserContext.m_tmpListPath);
Václav Kubernát43908fb2020-01-02 19:05:51 +0100372
373 decltype(listInstances) filteredInstances;
374 //This filters out instances, which don't correspond to the partial instance we have.
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100375 const auto partialFitsComplete = [&parserContext](const auto& complete) {
Václav Kubernát43908fb2020-01-02 19:05:51 +0100376 const auto& partial = parserContext.m_tmpListKeys;
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100377 return std::all_of(partial.begin(), partial.end(), [&complete](const auto& oneKV) {
378 const auto& [k, v] = oneKV;
379 return complete.at(k) == v;
380 });
Václav Kubernát43908fb2020-01-02 19:05:51 +0100381 };
382 std::copy_if(listInstances.begin(), listInstances.end(), std::inserter(filteredInstances, filteredInstances.end()), partialFitsComplete);
383
Václav Kubernátcb3af402020-02-12 16:49:17 +0100384 std::set<Completion> validValues;
Václav Kubernát43908fb2020-01-02 19:05:51 +0100385
Václav Kubernátcb3af402020-02-12 16:49:17 +0100386 std::transform(filteredInstances.begin(), filteredInstances.end(), std::inserter(validValues, validValues.end()), [&parserContext](const auto& instance) {
387 return Completion{leafDataToCompletion(instance.at(parserContext.m_tmpListKeyLeafPath.m_node.second))};
Václav Kubernát43908fb2020-01-02 19:05:51 +0100388 });
389
390 parserContext.m_suggestions = validValues;
391 }
392};
393
Václav Kubernát329c6c32019-02-06 16:41:53 +0100394struct suggestKeysEnd_class {
395 template <typename T, typename Iterator, typename Context>
396 void on_success(Iterator const& begin, Iterator const&, T&, Context const& context)
397 {
398 auto& parserContext = x3::get<parser_context_tag>(context);
399 const auto& schema = parserContext.m_schema;
400
401 parserContext.m_completionIterator = begin;
Václav Kubernát2db124c2020-05-28 21:58:36 +0200402 const auto& keysNeeded = schema.listKeys(dataPathToSchemaPath(parserContext.m_tmpListPath));
Václav Kubernát329c6c32019-02-06 16:41:53 +0100403 if (generateMissingKeyCompletionSet(keysNeeded, parserContext.m_tmpListKeys).empty()) {
Václav Kubernátcb3af402020-02-12 16:49:17 +0100404 parserContext.m_suggestions = {Completion{"]/"}};
Václav Kubernát329c6c32019-02-06 16:41:53 +0100405 } else {
Václav Kubernátcb3af402020-02-12 16:49:17 +0100406 parserContext.m_suggestions = {Completion{"]["}};
Václav Kubernát329c6c32019-02-06 16:41:53 +0100407 }
408 }
409};
Václav Kubernát57272422019-02-08 12:48:24 +0100410
Václav Kubernát672889c2020-05-27 04:11:36 +0200411template <typename T>
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100412std::string commandNamesVisitor(boost::type<T>)
Václav Kubernát672889c2020-05-27 04:11:36 +0200413{
414 return T::name;
415}
Václav Kubernát57272422019-02-08 12:48:24 +0100416
417struct createCommandSuggestions_class {
418 template <typename T, typename Iterator, typename Context>
Václav Kubernát5b8e2822022-01-14 03:19:54 +0100419#if BOOST_VERSION >= 107800
420 void on_success(Iterator const& was, Iterator& will, T&, Context const& context)
421#else
422 void on_success(Iterator const& was, Iterator const&, T&, Context const& context)
423#endif
Václav Kubernát57272422019-02-08 12:48:24 +0100424 {
Václav Kubernát5b8e2822022-01-14 03:19:54 +0100425#if BOOST_VERSION >= 107800
426 auto rollback = makeIteratorRollbacker(was, will, _pass(context));
427#endif
Václav Kubernát57272422019-02-08 12:48:24 +0100428 auto& parserContext = x3::get<parser_context_tag>(context);
Václav Kubernát5b8e2822022-01-14 03:19:54 +0100429 parserContext.m_completionIterator = was;
Václav Kubernát57272422019-02-08 12:48:24 +0100430
431 parserContext.m_suggestions.clear();
432 boost::mpl::for_each<CommandTypes, boost::type<boost::mpl::_>>([&parserContext](auto cmd) {
Václav Kubernát672889c2020-05-27 04:11:36 +0200433 parserContext.m_suggestions.insert({commandNamesVisitor(cmd), " "});
Václav Kubernát57272422019-02-08 12:48:24 +0100434 });
435 }
436};
Václav Kubernátac035d62019-02-18 10:59:08 +0100437
438struct completing_class {
439 template <typename T, typename Iterator, typename Context>
440 void on_success(Iterator const&, Iterator const&, T&, Context const& context)
441 {
442 auto& parserContext = x3::get<parser_context_tag>(context);
443
Václav Kubernát3a433232020-07-08 17:52:50 +0200444 if (!parserContext.m_completing) {
Václav Kubernátac035d62019-02-18 10:59:08 +0100445 _pass(context) = false;
Václav Kubernát3a433232020-07-08 17:52:50 +0200446 }
Václav Kubernátac035d62019-02-18 10:59:08 +0100447 }
448};