blob: b172f5830b0d8a26bc0de5848910490c13835dda [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"
Jan Kundrát07ca3922023-06-20 19:03:56 +020019
20// Up to Boost 1.77, calling `_pass(context) = false` was enough.
21// Since 1.78 it became necessary to explicitly reset the input iterator, but unfortunately the function signature
22// was wrong and the whole handler would just be ignored.
23// That was fixed in 1.80.
24// FIXME: change the function signature and remove the const_cast once we're unconditionally on 1.80+
25#define REJECT_PARSING do { const_cast<Iterator&>(after) = before; _pass(context) = false; } while (0)
Václav Kubernátebca2552018-06-08 19:06:02 +020026#include "utils.hpp"
Václav Kubernát329c6c32019-02-06 16:41:53 +010027namespace x3 = boost::spirit::x3;
28
29struct parser_context_tag;
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020030
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020031struct keyValue_class {
32 template <typename T, typename Iterator, typename Context>
Jan Kundrát07ca3922023-06-20 19:03:56 +020033 void on_success(Iterator const& before, Iterator const& after, T& ast, Context const& context)
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020034 {
35 auto& parserContext = x3::get<parser_context_tag>(context);
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020036
37 if (parserContext.m_tmpListKeys.find(ast.first) != parserContext.m_tmpListKeys.end()) {
Jan Kundrát07ca3922023-06-20 19:03:56 +020038 REJECT_PARSING;
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020039 parserContext.m_errorMsg = "Key \"" + ast.first + "\" was entered more than once.";
Václav Kubernát41378452018-06-06 16:29:40 +020040 } else {
Václav Kubernát43908fb2020-01-02 19:05:51 +010041 parserContext.m_tmpListKeys.insert({ast.first, ast.second});
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020042 }
43 }
Václav Kubernát41378452018-06-06 16:29:40 +020044
45 template <typename Iterator, typename Exception, typename Context>
46 x3::error_handler_result on_error(Iterator&, Iterator const&, Exception const&, Context const& context)
47 {
48 auto& parserContext = x3::get<parser_context_tag>(context);
49 parserContext.m_errorMsg = "Error parsing key values here:";
50 return x3::error_handler_result::rethrow;
51 }
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020052};
Václav Kubernát41378452018-06-06 16:29:40 +020053
Václav Kubernát2db124c2020-05-28 21:58:36 +020054boost::optional<std::string> optModuleToOptString(const boost::optional<module_> module);
55
Václav Kubernát7707cae2020-01-16 12:04:53 +010056struct key_identifier_class {
57 template <typename T, typename Iterator, typename Context>
Jan Kundrát07ca3922023-06-20 19:03:56 +020058 void on_success(Iterator const& before, Iterator const& after, T& ast, Context const& context)
Václav Kubernát7707cae2020-01-16 12:04:53 +010059 {
Václav Kubernát2db124c2020-05-28 21:58:36 +020060 ParserContext& parserContext = x3::get<parser_context_tag>(context);
Václav Kubernát7707cae2020-01-16 12:04:53 +010061 const Schema& schema = parserContext.m_schema;
Václav Kubernát7707cae2020-01-16 12:04:53 +010062
Václav Kubernát2db124c2020-05-28 21:58:36 +020063 if (schema.listHasKey(dataPathToSchemaPath(parserContext.m_tmpListPath), ast)) {
64 parserContext.m_tmpListKeyLeafPath.m_location = dataPathToSchemaPath(parserContext.m_tmpListPath);
Václav Kubernátb4e5b182020-11-16 19:55:09 +010065 parserContext.m_tmpListKeyLeafPath.m_node = {optModuleToOptString(parserContext.m_tmpListPath.m_nodes.back().m_prefix), ast};
Václav Kubernát7707cae2020-01-16 12:04:53 +010066 } else {
Václav Kubernát2db124c2020-05-28 21:58:36 +020067 auto listName = std::get<list_>(parserContext.m_tmpListPath.m_nodes.back().m_suffix).m_name;
68 parserContext.m_errorMsg = listName + " is not indexed by \"" + ast + "\".";
Jan Kundrát07ca3922023-06-20 19:03:56 +020069 REJECT_PARSING;
Václav Kubernát7707cae2020-01-16 12:04:53 +010070 }
71 }
72};
Václav Kubernát89728d82018-09-13 16:28:28 +020073
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020074struct listSuffix_class {
75 template <typename T, typename Iterator, typename Context>
Jan Kundrát07ca3922023-06-20 19:03:56 +020076 void on_success(Iterator const& before, Iterator const& after, T& ast, Context const& context)
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020077 {
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
Jan Kundrát07ca3922023-06-20 19:03:56 +0200101 REJECT_PARSING;
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>
Jan Kundrát07ca3922023-06-20 19:03:56 +0200122 void on_success(Iterator const& before, Iterator const& after, T& ast, Context const& context)
Václav Kubernát744f57f2018-06-29 22:46:26 +0200123 {
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.";
Jan Kundrát07ca3922023-06-20 19:03:56 +0200129 REJECT_PARSING;
Václav Kubernát07204242018-06-04 18:12:09 +0200130 }
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át744f57f2018-06-29 22:46:26 +0200143struct cd_class {
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200144 template <typename Iterator, typename Exception, typename Context>
145 x3::error_handler_result on_error(Iterator&, Iterator const&, Exception const& x, Context const& context)
146 {
147 auto& parserContext = x3::get<parser_context_tag>(context);
Václav Kubernát3a433232020-07-08 17:52:50 +0200148 if (parserContext.m_errorMsg.empty()) {
Václav Kubernát41378452018-06-06 16:29:40 +0200149 parserContext.m_errorMsg = "Expected " + x.which() + " here:";
Václav Kubernát3a433232020-07-08 17:52:50 +0200150 }
Václav Kubernát41378452018-06-06 16:29:40 +0200151 return x3::error_handler_result::rethrow;
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200152 }
153};
Václav Kubernátb61336d2018-05-28 17:35:03 +0200154
Václav Kubernát6797df02019-03-18 20:21:50 +0100155struct presenceContainerPath_class {
Václav Kubernátb61336d2018-05-28 17:35:03 +0200156 template <typename T, typename Iterator, typename Context>
Jan Kundrát07ca3922023-06-20 19:03:56 +0200157 void on_success(Iterator const& before, Iterator const& after, T& ast, Context const& context)
Václav Kubernátb61336d2018-05-28 17:35:03 +0200158 {
159 auto& parserContext = x3::get<parser_context_tag>(context);
160 const auto& schema = parserContext.m_schema;
Václav Kubernát0d42c512020-05-20 21:18:39 +0200161 if (ast.m_nodes.empty()) {
162 parserContext.m_errorMsg = "This container is not a presence container.";
Jan Kundrát07ca3922023-06-20 19:03:56 +0200163 REJECT_PARSING;
Václav Kubernát0d42c512020-05-20 21:18:39 +0200164 return;
165 }
166
Václav Kubernáte811bfa2020-05-29 02:25:20 +0200167 if (schema.nodeType(pathToSchemaString(parserContext.currentSchemaPath(), Prefixes::Always)) != yang::NodeTypes::PresenceContainer) {
168 parserContext.m_errorMsg = "This container is not a presence container.";
Jan Kundrát07ca3922023-06-20 19:03:56 +0200169 REJECT_PARSING;
Václav Kubernátb61336d2018-05-28 17:35:03 +0200170 }
171 }
Václav Kubernát6797df02019-03-18 20:21:50 +0100172};
Václav Kubernátb61336d2018-05-28 17:35:03 +0200173
Václav Kubernátf5f64f02019-03-19 17:15:47 +0100174struct listInstancePath_class {
175 template <typename T, typename Iterator, typename Context>
Jan Kundrát07ca3922023-06-20 19:03:56 +0200176 void on_success(Iterator const& before, Iterator const& after, T& ast, Context const& context)
Václav Kubernátf5f64f02019-03-19 17:15:47 +0100177 {
178 auto& parserContext = x3::get<parser_context_tag>(context);
Václav Kubernát2ebab1d2020-05-27 01:28:30 +0200179 if (ast.m_nodes.empty() || !std::holds_alternative<listElement_>(ast.m_nodes.back().m_suffix)) {
Václav Kubernátf5f64f02019-03-19 17:15:47 +0100180 parserContext.m_errorMsg = "This is not a list instance.";
Jan Kundrát07ca3922023-06-20 19:03:56 +0200181 REJECT_PARSING;
Václav Kubernátf5f64f02019-03-19 17:15:47 +0100182 }
183 }
184};
185
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200186struct leafListElementPath_class {
187 template <typename T, typename Iterator, typename Context>
Jan Kundrát07ca3922023-06-20 19:03:56 +0200188 void on_success(Iterator const& before, Iterator const& after, T& ast, Context const& context)
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200189 {
190 auto& parserContext = x3::get<parser_context_tag>(context);
Václav Kubernátb5ca1542020-05-27 01:03:54 +0200191 if (ast.m_nodes.empty() || !std::holds_alternative<leafListElement_>(ast.m_nodes.back().m_suffix)) {
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200192 parserContext.m_errorMsg = "This is not a leaf list element.";
Jan Kundrát07ca3922023-06-20 19:03:56 +0200193 REJECT_PARSING;
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200194 }
195 }
196};
197
Václav Kubernát8028f942019-09-25 16:03:23 +0200198struct space_separator_class {
199 template <typename T, typename Iterator, typename Context>
200 void on_success(Iterator const&, Iterator const&, T&, Context const& context)
201 {
202 auto& parserContext = x3::get<parser_context_tag>(context);
203 parserContext.m_suggestions.clear();
Václav Kubernát1ed4aa32020-01-23 13:13:28 +0100204 parserContext.m_completionIterator = boost::none;
Václav Kubernát8028f942019-09-25 16:03:23 +0200205 }
206};
207
Václav Kubernát6797df02019-03-18 20:21:50 +0100208struct create_class {
Václav Kubernátb61336d2018-05-28 17:35:03 +0200209 template <typename Iterator, typename Exception, typename Context>
Václav Kubernát41378452018-06-06 16:29:40 +0200210 x3::error_handler_result on_error(Iterator&, Iterator const&, Exception const&, Context const& context)
Václav Kubernátb61336d2018-05-28 17:35:03 +0200211 {
212 auto& parserContext = x3::get<parser_context_tag>(context);
Václav Kubernát3a433232020-07-08 17:52:50 +0200213 if (parserContext.m_errorMsg.empty()) {
Václav Kubernát41378452018-06-06 16:29:40 +0200214 parserContext.m_errorMsg = "Couldn't parse create/delete command.";
Václav Kubernát3a433232020-07-08 17:52:50 +0200215 }
Václav Kubernát41378452018-06-06 16:29:40 +0200216 return x3::error_handler_result::rethrow;
Václav Kubernátb61336d2018-05-28 17:35:03 +0200217 }
218};
219
Václav Kubernát6797df02019-03-18 20:21:50 +0100220struct delete_class {
221 template <typename Iterator, typename Exception, typename Context>
222 x3::error_handler_result on_error(Iterator&, Iterator const&, Exception const&, Context const& context)
223 {
224 auto& parserContext = x3::get<parser_context_tag>(context);
Václav Kubernát3a433232020-07-08 17:52:50 +0200225 if (parserContext.m_errorMsg.empty()) {
Václav Kubernát6797df02019-03-18 20:21:50 +0100226 parserContext.m_errorMsg = "Couldn't parse create/delete command.";
Václav Kubernát3a433232020-07-08 17:52:50 +0200227 }
Václav Kubernát6797df02019-03-18 20:21:50 +0100228 return x3::error_handler_result::rethrow;
229 }
Václav Kubernátb61336d2018-05-28 17:35:03 +0200230};
231
Václav Kubernát07204242018-06-04 18:12:09 +0200232struct set_class {
Václav Kubernát07204242018-06-04 18:12:09 +0200233 template <typename Iterator, typename Exception, typename Context>
234 x3::error_handler_result on_error(Iterator&, Iterator const&, Exception const& x, Context const& context)
235 {
236 auto& parserContext = x3::get<parser_context_tag>(context);
Václav Kubernát3a433232020-07-08 17:52:50 +0200237 if (parserContext.m_errorMsg.empty()) {
Václav Kubernát41378452018-06-06 16:29:40 +0200238 parserContext.m_errorMsg = "Expected " + x.which() + " here:";
Václav Kubernát3a433232020-07-08 17:52:50 +0200239 }
Václav Kubernát41378452018-06-06 16:29:40 +0200240 return x3::error_handler_result::rethrow;
Václav Kubernát07204242018-06-04 18:12:09 +0200241 }
242};
243
Václav Kubernátb61336d2018-05-28 17:35:03 +0200244struct command_class {
245 template <typename Iterator, typename Exception, typename Context>
246 x3::error_handler_result on_error(Iterator&, Iterator const&, Exception const& x, Context const& context)
247 {
248 auto& parserContext = x3::get<parser_context_tag>(context);
249 auto& error_handler = x3::get<x3::error_handler_tag>(context).get();
Václav Kubernát41378452018-06-06 16:29:40 +0200250 if (parserContext.m_errorMsg.empty()) {
251 parserContext.m_errorMsg = "Unknown command.";
252 }
253 error_handler(x.where(), parserContext.m_errorMsg);
Václav Kubernátb61336d2018-05-28 17:35:03 +0200254 return x3::error_handler_result::fail;
255 }
256};
Václav Kubernát5c75b252018-10-10 18:33:47 +0200257
Václav Kubernát4108e0d2018-10-29 13:32:22 +0100258struct initializePath_class {
Václav Kubernát5c75b252018-10-10 18:33:47 +0200259 template <typename T, typename Iterator, typename Context>
260 void on_success(Iterator const&, Iterator const&, T&, Context const& context)
261 {
262 auto& parserContext = x3::get<parser_context_tag>(context);
Václav Kubernát72749c62020-01-03 16:47:34 +0100263 parserContext.resetPath();
Václav Kubernátbf65dd72020-05-28 02:32:31 +0200264 parserContext.m_tmpListPath = dataPath_{};
Václav Kubernát5c75b252018-10-10 18:33:47 +0200265 parserContext.m_tmpListKeys.clear();
Václav Kubernát4108e0d2018-10-29 13:32:22 +0100266 parserContext.m_suggestions.clear();
Václav Kubernát5c75b252018-10-10 18:33:47 +0200267 }
268};
Václav Kubernátd6fd2492018-11-19 15:11:16 +0100269
Václav Kubernátc15fe822020-06-04 11:28:39 +0200270std::set<Completion> generateMissingKeyCompletionSet(std::set<std::string> keysNeeded, ListInstance currentSet);
Václav Kubernát329c6c32019-02-06 16:41:53 +0100271
272struct createKeySuggestions_class {
273 template <typename T, typename Iterator, typename Context>
Jan Kundrát07ca3922023-06-20 19:03:56 +0200274 void on_success(Iterator const& before, Iterator const&, T&, Context const& context)
Václav Kubernát329c6c32019-02-06 16:41:53 +0100275 {
276 auto& parserContext = x3::get<parser_context_tag>(context);
277 const auto& schema = parserContext.m_schema;
278
Jan Kundrát07ca3922023-06-20 19:03:56 +0200279 parserContext.m_completionIterator = before;
Václav Kubernát329c6c32019-02-06 16:41:53 +0100280
Václav Kubernát2db124c2020-05-28 21:58:36 +0200281 const auto& keysNeeded = schema.listKeys(dataPathToSchemaPath(parserContext.m_tmpListPath));
Václav Kubernát329c6c32019-02-06 16:41:53 +0100282 parserContext.m_suggestions = generateMissingKeyCompletionSet(keysNeeded, parserContext.m_tmpListKeys);
283 }
284};
285
Václav Kubernát43908fb2020-01-02 19:05:51 +0100286std::string leafDataToCompletion(const leaf_data_& value);
287
288struct createValueSuggestions_class {
289 template <typename T, typename Iterator, typename Context>
Jan Kundrát07ca3922023-06-20 19:03:56 +0200290 void on_success(Iterator const& before, Iterator const&, T&, Context const& context)
Václav Kubernát43908fb2020-01-02 19:05:51 +0100291 {
292 auto& parserContext = x3::get<parser_context_tag>(context);
293 if (!parserContext.m_completing) {
294 return;
295 }
296 const auto& dataQuery = parserContext.m_dataquery;
297
Jan Kundrát07ca3922023-06-20 19:03:56 +0200298 parserContext.m_completionIterator = before;
Václav Kubernát2db124c2020-05-28 21:58:36 +0200299 auto listInstances = dataQuery->listKeys(parserContext.m_tmpListPath);
Václav Kubernát43908fb2020-01-02 19:05:51 +0100300
301 decltype(listInstances) filteredInstances;
302 //This filters out instances, which don't correspond to the partial instance we have.
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100303 const auto partialFitsComplete = [&parserContext](const auto& complete) {
Václav Kubernát43908fb2020-01-02 19:05:51 +0100304 const auto& partial = parserContext.m_tmpListKeys;
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100305 return std::all_of(partial.begin(), partial.end(), [&complete](const auto& oneKV) {
306 const auto& [k, v] = oneKV;
307 return complete.at(k) == v;
308 });
Václav Kubernát43908fb2020-01-02 19:05:51 +0100309 };
310 std::copy_if(listInstances.begin(), listInstances.end(), std::inserter(filteredInstances, filteredInstances.end()), partialFitsComplete);
311
Václav Kubernátcb3af402020-02-12 16:49:17 +0100312 std::set<Completion> validValues;
Václav Kubernát43908fb2020-01-02 19:05:51 +0100313
Václav Kubernátcb3af402020-02-12 16:49:17 +0100314 std::transform(filteredInstances.begin(), filteredInstances.end(), std::inserter(validValues, validValues.end()), [&parserContext](const auto& instance) {
315 return Completion{leafDataToCompletion(instance.at(parserContext.m_tmpListKeyLeafPath.m_node.second))};
Václav Kubernát43908fb2020-01-02 19:05:51 +0100316 });
317
318 parserContext.m_suggestions = validValues;
319 }
320};
321
Václav Kubernát329c6c32019-02-06 16:41:53 +0100322struct suggestKeysEnd_class {
323 template <typename T, typename Iterator, typename Context>
Jan Kundrát07ca3922023-06-20 19:03:56 +0200324 void on_success(Iterator const& before, Iterator const&, T&, Context const& context)
Václav Kubernát329c6c32019-02-06 16:41:53 +0100325 {
326 auto& parserContext = x3::get<parser_context_tag>(context);
327 const auto& schema = parserContext.m_schema;
328
Jan Kundrát07ca3922023-06-20 19:03:56 +0200329 parserContext.m_completionIterator = before;
Václav Kubernát2db124c2020-05-28 21:58:36 +0200330 const auto& keysNeeded = schema.listKeys(dataPathToSchemaPath(parserContext.m_tmpListPath));
Václav Kubernát329c6c32019-02-06 16:41:53 +0100331 if (generateMissingKeyCompletionSet(keysNeeded, parserContext.m_tmpListKeys).empty()) {
Václav Kubernátcb3af402020-02-12 16:49:17 +0100332 parserContext.m_suggestions = {Completion{"]/"}};
Václav Kubernát329c6c32019-02-06 16:41:53 +0100333 } else {
Václav Kubernátcb3af402020-02-12 16:49:17 +0100334 parserContext.m_suggestions = {Completion{"]["}};
Václav Kubernát329c6c32019-02-06 16:41:53 +0100335 }
336 }
337};
Václav Kubernát57272422019-02-08 12:48:24 +0100338
Václav Kubernát672889c2020-05-27 04:11:36 +0200339template <typename T>
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100340std::string commandNamesVisitor(boost::type<T>)
Václav Kubernát672889c2020-05-27 04:11:36 +0200341{
342 return T::name;
343}
Václav Kubernát57272422019-02-08 12:48:24 +0100344
345struct createCommandSuggestions_class {
346 template <typename T, typename Iterator, typename Context>
Jan Kundrát07ca3922023-06-20 19:03:56 +0200347 void on_success(Iterator const& before, Iterator const&, T&, Context const& context)
Václav Kubernát57272422019-02-08 12:48:24 +0100348 {
349 auto& parserContext = x3::get<parser_context_tag>(context);
Jan Kundrát07ca3922023-06-20 19:03:56 +0200350 parserContext.m_completionIterator = before;
Václav Kubernát57272422019-02-08 12:48:24 +0100351
352 parserContext.m_suggestions.clear();
353 boost::mpl::for_each<CommandTypes, boost::type<boost::mpl::_>>([&parserContext](auto cmd) {
Václav Kubernát672889c2020-05-27 04:11:36 +0200354 parserContext.m_suggestions.insert({commandNamesVisitor(cmd), " "});
Václav Kubernát57272422019-02-08 12:48:24 +0100355 });
356 }
357};
Václav Kubernátac035d62019-02-18 10:59:08 +0100358
359struct completing_class {
360 template <typename T, typename Iterator, typename Context>
Jan Kundrát07ca3922023-06-20 19:03:56 +0200361 void on_success(Iterator const& before, Iterator const& after, T&, Context const& context)
Václav Kubernátac035d62019-02-18 10:59:08 +0100362 {
363 auto& parserContext = x3::get<parser_context_tag>(context);
364
Václav Kubernát3a433232020-07-08 17:52:50 +0200365 if (!parserContext.m_completing) {
Jan Kundrát07ca3922023-06-20 19:03:56 +0200366 REJECT_PARSING;
Václav Kubernát3a433232020-07-08 17:52:50 +0200367 }
Václav Kubernátac035d62019-02-18 10:59:08 +0100368 }
369};