blob: 288801d7f28bce6a1ff64eadd953c5ff7b7b9266 [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átf3e377b2020-05-28 23:26:38 +020047struct node_identifier_class;
Václav Kubernát744f57f2018-06-29 22:46:26 +020048
Václav Kubernát2db124c2020-05-28 21:58:36 +020049boost::optional<std::string> optModuleToOptString(const boost::optional<module_> module);
50
Václav Kubernát7707cae2020-01-16 12:04:53 +010051struct key_identifier_class {
52 template <typename T, typename Iterator, typename Context>
53 void on_success(Iterator const&, Iterator const&, T& ast, Context const& context)
54 {
Václav Kubernát2db124c2020-05-28 21:58:36 +020055 ParserContext& parserContext = x3::get<parser_context_tag>(context);
Václav Kubernát7707cae2020-01-16 12:04:53 +010056 const Schema& schema = parserContext.m_schema;
Václav Kubernát7707cae2020-01-16 12:04:53 +010057
Václav Kubernát2db124c2020-05-28 21:58:36 +020058 if (schema.listHasKey(dataPathToSchemaPath(parserContext.m_tmpListPath), ast)) {
59 parserContext.m_tmpListKeyLeafPath.m_location = dataPathToSchemaPath(parserContext.m_tmpListPath);
Václav Kubernátb4e5b182020-11-16 19:55:09 +010060 parserContext.m_tmpListKeyLeafPath.m_node = {optModuleToOptString(parserContext.m_tmpListPath.m_nodes.back().m_prefix), ast};
Václav Kubernát7707cae2020-01-16 12:04:53 +010061 } else {
Václav Kubernát2db124c2020-05-28 21:58:36 +020062 auto listName = std::get<list_>(parserContext.m_tmpListPath.m_nodes.back().m_suffix).m_name;
63 parserContext.m_errorMsg = listName + " is not indexed by \"" + ast + "\".";
Václav Kubernát7707cae2020-01-16 12:04:53 +010064 _pass(context) = false;
65 }
66 }
67};
Václav Kubernát89728d82018-09-13 16:28:28 +020068
Václav Kubernát744f57f2018-06-29 22:46:26 +020069struct module_identifier_class;
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020070
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020071struct listSuffix_class {
72 template <typename T, typename Iterator, typename Context>
73 void on_success(Iterator const&, Iterator const&, T& ast, Context const& context)
74 {
75 auto& parserContext = x3::get<parser_context_tag>(context);
Václav Kubernát48fc3832018-05-28 14:21:22 +020076 const Schema& schema = parserContext.m_schema;
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020077
Václav Kubernát2db124c2020-05-28 21:58:36 +020078 const auto& keysNeeded = schema.listKeys(dataPathToSchemaPath(parserContext.m_tmpListPath));
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020079 std::set<std::string> keysSupplied;
Václav Kubernát3a433232020-07-08 17:52:50 +020080 for (const auto& it : ast) {
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020081 keysSupplied.insert(it.first);
Václav Kubernát3a433232020-07-08 17:52:50 +020082 }
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020083
84 if (keysNeeded != keysSupplied) {
Václav Kubernát2db124c2020-05-28 21:58:36 +020085 auto listName = std::get<list_>(parserContext.m_tmpListPath.m_nodes.back().m_suffix).m_name;
86 parserContext.m_errorMsg = "Not enough keys for " + listName + ". " +
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020087 "These keys were not supplied:";
88 std::set<std::string> missingKeys;
89 std::set_difference(keysNeeded.begin(), keysNeeded.end(),
90 keysSupplied.begin(), keysSupplied.end(),
91 std::inserter(missingKeys, missingKeys.end()));
92
Václav Kubernát3a433232020-07-08 17:52:50 +020093 for (const auto& it : missingKeys) {
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020094 parserContext.m_errorMsg += " " + it;
Václav Kubernát3a433232020-07-08 17:52:50 +020095 }
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020096 parserContext.m_errorMsg += ".";
97
98 _pass(context) = false;
Václav Kubernátbf65dd72020-05-28 02:32:31 +020099 return;
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200100 }
Václav Kubernátbf65dd72020-05-28 02:32:31 +0200101
102 // Clean up after listSuffix, in case someone wants to parse more listSuffixes
103 parserContext.m_tmpListKeys.clear();
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200104 }
Václav Kubernát41378452018-06-06 16:29:40 +0200105
106 template <typename Iterator, typename Exception, typename Context>
107 x3::error_handler_result on_error(Iterator&, Iterator const&, Exception const&, Context const& context)
108 {
109 auto& parserContext = x3::get<parser_context_tag>(context);
Václav Kubernát3a433232020-07-08 17:52:50 +0200110 if (parserContext.m_errorMsg.empty()) {
Václav Kubernát41378452018-06-06 16:29:40 +0200111 parserContext.m_errorMsg = "Expecting ']' here:";
Václav Kubernát3a433232020-07-08 17:52:50 +0200112 }
Václav Kubernát41378452018-06-06 16:29:40 +0200113 return x3::error_handler_result::rethrow;
Václav Kubernát41378452018-06-06 16:29:40 +0200114 }
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200115};
Václav Kubernát744f57f2018-06-29 22:46:26 +0200116
Václav Kubernát744f57f2018-06-29 22:46:26 +0200117struct module_class {
118 template <typename T, typename Iterator, typename Context>
119 void on_success(Iterator const&, Iterator const&, T& ast, Context const& context)
120 {
121 auto& parserContext = x3::get<parser_context_tag>(context);
122 const auto& schema = parserContext.m_schema;
123
Václav Kubernát1bcee3b2020-05-28 22:19:59 +0200124 if (!schema.isModule(ast.m_name)) {
Václav Kubernát744f57f2018-06-29 22:46:26 +0200125 parserContext.m_errorMsg = "Invalid module name.";
Václav Kubernát07204242018-06-04 18:12:09 +0200126 _pass(context) = false;
127 }
128 }
129};
130
Václav Kubernát37171a12018-08-31 17:01:48 +0200131struct absoluteStart_class {
132 template <typename T, typename Iterator, typename Context>
133 void on_success(Iterator const&, Iterator const&, T&, Context const& context)
134 {
135 auto& parserContext = x3::get<parser_context_tag>(context);
Václav Kubernát72749c62020-01-03 16:47:34 +0100136 parserContext.clearPath();
Václav Kubernát37171a12018-08-31 17:01:48 +0200137 }
138};
139
Václav Kubernát6d791432018-10-25 16:00:35 +0200140struct discard_class;
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200141
Václav Kubernát11afac72018-07-18 14:59:53 +0200142struct ls_class;
143
Václav Kubernát744f57f2018-06-29 22:46:26 +0200144struct cd_class {
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200145 template <typename Iterator, typename Exception, typename Context>
146 x3::error_handler_result on_error(Iterator&, Iterator const&, Exception const& x, Context const& context)
147 {
148 auto& parserContext = x3::get<parser_context_tag>(context);
Václav Kubernát3a433232020-07-08 17:52:50 +0200149 if (parserContext.m_errorMsg.empty()) {
Václav Kubernát41378452018-06-06 16:29:40 +0200150 parserContext.m_errorMsg = "Expected " + x.which() + " here:";
Václav Kubernát3a433232020-07-08 17:52:50 +0200151 }
Václav Kubernát41378452018-06-06 16:29:40 +0200152 return x3::error_handler_result::rethrow;
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200153 }
154};
Václav Kubernátb61336d2018-05-28 17:35:03 +0200155
Václav Kubernát6797df02019-03-18 20:21:50 +0100156struct presenceContainerPath_class {
Václav Kubernátb61336d2018-05-28 17:35:03 +0200157 template <typename T, typename Iterator, typename Context>
158 void on_success(Iterator const&, Iterator const&, T& ast, Context const& context)
159 {
160 auto& parserContext = x3::get<parser_context_tag>(context);
161 const auto& schema = parserContext.m_schema;
Václav Kubernát0d42c512020-05-20 21:18:39 +0200162 if (ast.m_nodes.empty()) {
163 parserContext.m_errorMsg = "This container is not a presence container.";
164 _pass(context) = false;
165 return;
166 }
167
Václav Kubernáte811bfa2020-05-29 02:25:20 +0200168 if (schema.nodeType(pathToSchemaString(parserContext.currentSchemaPath(), Prefixes::Always)) != yang::NodeTypes::PresenceContainer) {
169 parserContext.m_errorMsg = "This container is not a presence container.";
Václav Kubernátb61336d2018-05-28 17:35:03 +0200170 _pass(context) = false;
Václav Kubernátb61336d2018-05-28 17:35:03 +0200171 }
172 }
Václav Kubernát6797df02019-03-18 20:21:50 +0100173};
Václav Kubernátb61336d2018-05-28 17:35:03 +0200174
Václav Kubernátf5f64f02019-03-19 17:15:47 +0100175struct listInstancePath_class {
176 template <typename T, typename Iterator, typename Context>
177 void on_success(Iterator const&, Iterator const&, T& ast, Context const& context)
178 {
179 auto& parserContext = x3::get<parser_context_tag>(context);
Václav Kubernát2ebab1d2020-05-27 01:28:30 +0200180 if (ast.m_nodes.empty() || !std::holds_alternative<listElement_>(ast.m_nodes.back().m_suffix)) {
Václav Kubernátf5f64f02019-03-19 17:15:47 +0100181 parserContext.m_errorMsg = "This is not a list instance.";
182 _pass(context) = false;
183 }
184 }
185};
186
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200187struct leafListElementPath_class {
188 template <typename T, typename Iterator, typename Context>
189 void on_success(Iterator const&, Iterator const&, T& ast, Context const& context)
190 {
191 auto& parserContext = x3::get<parser_context_tag>(context);
Václav Kubernátb5ca1542020-05-27 01:03:54 +0200192 if (ast.m_nodes.empty() || !std::holds_alternative<leafListElement_>(ast.m_nodes.back().m_suffix)) {
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200193 parserContext.m_errorMsg = "This is not a leaf list element.";
194 _pass(context) = false;
195 }
196 }
197};
198
Václav Kubernát8028f942019-09-25 16:03:23 +0200199struct space_separator_class {
200 template <typename T, typename Iterator, typename Context>
201 void on_success(Iterator const&, Iterator const&, T&, Context const& context)
202 {
203 auto& parserContext = x3::get<parser_context_tag>(context);
204 parserContext.m_suggestions.clear();
Václav Kubernát1ed4aa32020-01-23 13:13:28 +0100205 parserContext.m_completionIterator = boost::none;
Václav Kubernát8028f942019-09-25 16:03:23 +0200206 }
207};
208
Václav Kubernát6797df02019-03-18 20:21:50 +0100209struct create_class {
Václav Kubernátb61336d2018-05-28 17:35:03 +0200210 template <typename Iterator, typename Exception, typename Context>
Václav Kubernát41378452018-06-06 16:29:40 +0200211 x3::error_handler_result on_error(Iterator&, Iterator const&, Exception const&, Context const& context)
Václav Kubernátb61336d2018-05-28 17:35:03 +0200212 {
213 auto& parserContext = x3::get<parser_context_tag>(context);
Václav Kubernát3a433232020-07-08 17:52:50 +0200214 if (parserContext.m_errorMsg.empty()) {
Václav Kubernát41378452018-06-06 16:29:40 +0200215 parserContext.m_errorMsg = "Couldn't parse create/delete command.";
Václav Kubernát3a433232020-07-08 17:52:50 +0200216 }
Václav Kubernát41378452018-06-06 16:29:40 +0200217 return x3::error_handler_result::rethrow;
Václav Kubernátb61336d2018-05-28 17:35:03 +0200218 }
219};
220
Václav Kubernát6797df02019-03-18 20:21:50 +0100221struct delete_class {
222 template <typename Iterator, typename Exception, typename Context>
223 x3::error_handler_result on_error(Iterator&, Iterator const&, Exception const&, Context const& context)
224 {
225 auto& parserContext = x3::get<parser_context_tag>(context);
Václav Kubernát3a433232020-07-08 17:52:50 +0200226 if (parserContext.m_errorMsg.empty()) {
Václav Kubernát6797df02019-03-18 20:21:50 +0100227 parserContext.m_errorMsg = "Couldn't parse create/delete command.";
Václav Kubernát3a433232020-07-08 17:52:50 +0200228 }
Václav Kubernát6797df02019-03-18 20:21:50 +0100229 return x3::error_handler_result::rethrow;
230 }
Václav Kubernátb61336d2018-05-28 17:35:03 +0200231};
232
Václav Kubernáte7248b22020-06-26 15:38:59 +0200233struct rpcPath_class;
234
Václav Kubernátaa4250a2020-07-22 00:02:23 +0200235struct actionPath_class;
236
Václav Kubernáte7248b22020-06-26 15:38:59 +0200237struct cdPath_class;
238
Václav Kubernát31029682020-10-29 08:23:04 +0100239struct getPath_class;
240
Václav Kubernát07204242018-06-04 18:12:09 +0200241struct set_class {
Václav Kubernát07204242018-06-04 18:12:09 +0200242 template <typename Iterator, typename Exception, typename Context>
243 x3::error_handler_result on_error(Iterator&, Iterator const&, Exception const& x, Context const& context)
244 {
245 auto& parserContext = x3::get<parser_context_tag>(context);
Václav Kubernát3a433232020-07-08 17:52:50 +0200246 if (parserContext.m_errorMsg.empty()) {
Václav Kubernát41378452018-06-06 16:29:40 +0200247 parserContext.m_errorMsg = "Expected " + x.which() + " here:";
Václav Kubernát3a433232020-07-08 17:52:50 +0200248 }
Václav Kubernát41378452018-06-06 16:29:40 +0200249 return x3::error_handler_result::rethrow;
Václav Kubernát07204242018-06-04 18:12:09 +0200250 }
251};
252
Václav Kubernát812ee282018-08-30 17:10:03 +0200253struct commit_class;
254
Václav Kubernát9cfcd872020-02-18 12:34:02 +0100255struct describe_class;
256
Václav Kubernát054cc992019-02-21 14:23:52 +0100257struct help_class;
258
Václav Kubernátb6ff0b62018-08-30 16:14:53 +0200259struct get_class;
260
Václav Kubernát7160a132020-04-03 02:11:01 +0200261struct copy_class;
262
Václav Kubernátbf65dd72020-05-28 02:32:31 +0200263struct move_class;
264
Václav Kubernát70d7f7a2020-06-23 14:40:40 +0200265struct dump_class;
266
Václav Kubernátaa4250a2020-07-22 00:02:23 +0200267struct prepare_class;
268
269struct action_class;
Václav Kubernáte7248b22020-06-26 15:38:59 +0200270
271struct exec_class;
272
Václav Kubernát162165e2021-02-22 09:46:53 +0100273struct switch_class;
274
Václav Kubernáte7248b22020-06-26 15:38:59 +0200275struct cancel_class;
276
Václav Kubernátb61336d2018-05-28 17:35:03 +0200277struct command_class {
278 template <typename Iterator, typename Exception, typename Context>
279 x3::error_handler_result on_error(Iterator&, Iterator const&, Exception const& x, Context const& context)
280 {
281 auto& parserContext = x3::get<parser_context_tag>(context);
282 auto& error_handler = x3::get<x3::error_handler_tag>(context).get();
Václav Kubernát41378452018-06-06 16:29:40 +0200283 if (parserContext.m_errorMsg.empty()) {
284 parserContext.m_errorMsg = "Unknown command.";
285 }
286 error_handler(x.where(), parserContext.m_errorMsg);
Václav Kubernátb61336d2018-05-28 17:35:03 +0200287 return x3::error_handler_result::fail;
288 }
289};
Václav Kubernát5c75b252018-10-10 18:33:47 +0200290
Václav Kubernát4108e0d2018-10-29 13:32:22 +0100291struct initializePath_class {
Václav Kubernát5c75b252018-10-10 18:33:47 +0200292 template <typename T, typename Iterator, typename Context>
293 void on_success(Iterator const&, Iterator const&, T&, Context const& context)
294 {
295 auto& parserContext = x3::get<parser_context_tag>(context);
Václav Kubernát72749c62020-01-03 16:47:34 +0100296 parserContext.resetPath();
Václav Kubernátbf65dd72020-05-28 02:32:31 +0200297 parserContext.m_tmpListPath = dataPath_{};
Václav Kubernát5c75b252018-10-10 18:33:47 +0200298 parserContext.m_tmpListKeys.clear();
Václav Kubernát4108e0d2018-10-29 13:32:22 +0100299 parserContext.m_suggestions.clear();
Václav Kubernát5c75b252018-10-10 18:33:47 +0200300 }
301};
Václav Kubernátd6fd2492018-11-19 15:11:16 +0100302
303struct trailingSlash_class;
Václav Kubernát4108e0d2018-10-29 13:32:22 +0100304
Václav Kubernátc15fe822020-06-04 11:28:39 +0200305std::set<Completion> generateMissingKeyCompletionSet(std::set<std::string> keysNeeded, ListInstance currentSet);
Václav Kubernát329c6c32019-02-06 16:41:53 +0100306
307struct createKeySuggestions_class {
308 template <typename T, typename Iterator, typename Context>
309 void on_success(Iterator const& begin, Iterator const&, T&, Context const& context)
310 {
311 auto& parserContext = x3::get<parser_context_tag>(context);
312 const auto& schema = parserContext.m_schema;
313
314 parserContext.m_completionIterator = begin;
315
Václav Kubernát2db124c2020-05-28 21:58:36 +0200316 const auto& keysNeeded = schema.listKeys(dataPathToSchemaPath(parserContext.m_tmpListPath));
Václav Kubernát329c6c32019-02-06 16:41:53 +0100317 parserContext.m_suggestions = generateMissingKeyCompletionSet(keysNeeded, parserContext.m_tmpListKeys);
318 }
319};
320
Václav Kubernát43908fb2020-01-02 19:05:51 +0100321std::string leafDataToCompletion(const leaf_data_& value);
322
323struct createValueSuggestions_class {
324 template <typename T, typename Iterator, typename Context>
325 void on_success(Iterator const& begin, Iterator const&, T&, Context const& context)
326 {
327 auto& parserContext = x3::get<parser_context_tag>(context);
328 if (!parserContext.m_completing) {
329 return;
330 }
331 const auto& dataQuery = parserContext.m_dataquery;
332
333 parserContext.m_completionIterator = begin;
Václav Kubernát2db124c2020-05-28 21:58:36 +0200334 auto listInstances = dataQuery->listKeys(parserContext.m_tmpListPath);
Václav Kubernát43908fb2020-01-02 19:05:51 +0100335
336 decltype(listInstances) filteredInstances;
337 //This filters out instances, which don't correspond to the partial instance we have.
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100338 const auto partialFitsComplete = [&parserContext](const auto& complete) {
Václav Kubernát43908fb2020-01-02 19:05:51 +0100339 const auto& partial = parserContext.m_tmpListKeys;
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100340 return std::all_of(partial.begin(), partial.end(), [&complete](const auto& oneKV) {
341 const auto& [k, v] = oneKV;
342 return complete.at(k) == v;
343 });
Václav Kubernát43908fb2020-01-02 19:05:51 +0100344 };
345 std::copy_if(listInstances.begin(), listInstances.end(), std::inserter(filteredInstances, filteredInstances.end()), partialFitsComplete);
346
Václav Kubernátcb3af402020-02-12 16:49:17 +0100347 std::set<Completion> validValues;
Václav Kubernát43908fb2020-01-02 19:05:51 +0100348
Václav Kubernátcb3af402020-02-12 16:49:17 +0100349 std::transform(filteredInstances.begin(), filteredInstances.end(), std::inserter(validValues, validValues.end()), [&parserContext](const auto& instance) {
350 return Completion{leafDataToCompletion(instance.at(parserContext.m_tmpListKeyLeafPath.m_node.second))};
Václav Kubernát43908fb2020-01-02 19:05:51 +0100351 });
352
353 parserContext.m_suggestions = validValues;
354 }
355};
356
Václav Kubernát329c6c32019-02-06 16:41:53 +0100357struct suggestKeysEnd_class {
358 template <typename T, typename Iterator, typename Context>
359 void on_success(Iterator const& begin, Iterator const&, T&, Context const& context)
360 {
361 auto& parserContext = x3::get<parser_context_tag>(context);
362 const auto& schema = parserContext.m_schema;
363
364 parserContext.m_completionIterator = begin;
Václav Kubernát2db124c2020-05-28 21:58:36 +0200365 const auto& keysNeeded = schema.listKeys(dataPathToSchemaPath(parserContext.m_tmpListPath));
Václav Kubernát329c6c32019-02-06 16:41:53 +0100366 if (generateMissingKeyCompletionSet(keysNeeded, parserContext.m_tmpListKeys).empty()) {
Václav Kubernátcb3af402020-02-12 16:49:17 +0100367 parserContext.m_suggestions = {Completion{"]/"}};
Václav Kubernát329c6c32019-02-06 16:41:53 +0100368 } else {
Václav Kubernátcb3af402020-02-12 16:49:17 +0100369 parserContext.m_suggestions = {Completion{"]["}};
Václav Kubernát329c6c32019-02-06 16:41:53 +0100370 }
371 }
372};
Václav Kubernát57272422019-02-08 12:48:24 +0100373
Václav Kubernát672889c2020-05-27 04:11:36 +0200374template <typename T>
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100375std::string commandNamesVisitor(boost::type<T>)
Václav Kubernát672889c2020-05-27 04:11:36 +0200376{
377 return T::name;
378}
Václav Kubernát57272422019-02-08 12:48:24 +0100379
380struct createCommandSuggestions_class {
381 template <typename T, typename Iterator, typename Context>
382 void on_success(Iterator const& begin, Iterator const&, T&, Context const& context)
383 {
384 auto& parserContext = x3::get<parser_context_tag>(context);
385 parserContext.m_completionIterator = begin;
386
387 parserContext.m_suggestions.clear();
388 boost::mpl::for_each<CommandTypes, boost::type<boost::mpl::_>>([&parserContext](auto cmd) {
Václav Kubernát672889c2020-05-27 04:11:36 +0200389 parserContext.m_suggestions.insert({commandNamesVisitor(cmd), " "});
Václav Kubernát57272422019-02-08 12:48:24 +0100390 });
391 }
392};
Václav Kubernátac035d62019-02-18 10:59:08 +0100393
394struct completing_class {
395 template <typename T, typename Iterator, typename Context>
396 void on_success(Iterator const&, Iterator const&, T&, Context const& context)
397 {
398 auto& parserContext = x3::get<parser_context_tag>(context);
399
Václav Kubernát3a433232020-07-08 17:52:50 +0200400 if (!parserContext.m_completing) {
Václav Kubernátac035d62019-02-18 10:59:08 +0100401 _pass(context) = false;
Václav Kubernát3a433232020-07-08 17:52:50 +0200402 }
Václav Kubernátac035d62019-02-18 10:59:08 +0100403 }
404};