blob: 368d51e39c8278fc88d97ceed7569cafd17ab9f9 [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
86struct listPrefix_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 if (schema.isList(parserContext.currentSchemaPath(), {parserContext.m_curModule, ast})) {
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020094 parserContext.m_tmpListName = ast;
95 } else {
96 _pass(context) = false;
97 }
98 }
99};
100
101struct listSuffix_class {
102 template <typename T, typename Iterator, typename Context>
103 void on_success(Iterator const&, Iterator const&, T& ast, Context const& context)
104 {
105 auto& parserContext = x3::get<parser_context_tag>(context);
Václav Kubernát48fc3832018-05-28 14:21:22 +0200106 const Schema& schema = parserContext.m_schema;
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200107
Václav Kubernát72749c62020-01-03 16:47:34 +0100108 const auto& keysNeeded = schema.listKeys(parserContext.currentSchemaPath(), {parserContext.m_curModule, parserContext.m_tmpListName});
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200109 std::set<std::string> keysSupplied;
110 for (const auto& it : ast)
111 keysSupplied.insert(it.first);
112
113 if (keysNeeded != keysSupplied) {
114 parserContext.m_errorMsg = "Not enough keys for " + parserContext.m_tmpListName + ". " +
115 "These keys were not supplied:";
116 std::set<std::string> missingKeys;
117 std::set_difference(keysNeeded.begin(), keysNeeded.end(),
118 keysSupplied.begin(), keysSupplied.end(),
119 std::inserter(missingKeys, missingKeys.end()));
120
121 for (const auto& it : missingKeys)
122 parserContext.m_errorMsg += " " + it;
123 parserContext.m_errorMsg += ".";
124
125 _pass(context) = false;
126 }
127 }
Václav Kubernát41378452018-06-06 16:29:40 +0200128
129 template <typename Iterator, typename Exception, typename Context>
130 x3::error_handler_result on_error(Iterator&, Iterator const&, Exception const&, Context const& context)
131 {
132 auto& parserContext = x3::get<parser_context_tag>(context);
133 if (parserContext.m_errorMsg.empty())
134 parserContext.m_errorMsg = "Expecting ']' here:";
135 return x3::error_handler_result::rethrow;
Václav Kubernát41378452018-06-06 16:29:40 +0200136 }
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200137};
138struct listElement_class {
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200139 template <typename Iterator, typename Exception, typename Context>
Václav Kubernát41378452018-06-06 16:29:40 +0200140 x3::error_handler_result on_error(Iterator&, Iterator const&, Exception const&, Context const& context)
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200141 {
142 auto& parserContext = x3::get<parser_context_tag>(context);
Václav Kubernát41378452018-06-06 16:29:40 +0200143 if (parserContext.m_errorMsg.empty()) {
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200144 return x3::error_handler_result::fail;
Václav Kubernát41378452018-06-06 16:29:40 +0200145 } else {
146 return x3::error_handler_result::rethrow;
147 }
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200148 }
149};
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200150struct list_class {
151 template <typename T, typename Iterator, typename Context>
152 void on_success(Iterator const&, Iterator const&, T& ast, Context const& context)
153 {
154 auto& parserContext = x3::get<parser_context_tag>(context);
155 const Schema& schema = parserContext.m_schema;
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200156
Václav Kubernát72749c62020-01-03 16:47:34 +0100157 if (!schema.isList(parserContext.currentSchemaPath(), {parserContext.m_curModule, ast.m_name})) {
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200158 _pass(context) = false;
159 }
160 }
161};
Václav Kubernát60d6f292018-05-25 09:45:32 +0200162struct nodeup_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);
167
Václav Kubernát72749c62020-01-03 16:47:34 +0100168 if (parserContext.currentSchemaPath().m_nodes.empty())
Václav Kubernát60d6f292018-05-25 09:45:32 +0200169 _pass(context) = false;
Václav Kubernát60d6f292018-05-25 09:45:32 +0200170 }
171};
172
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200173struct container_class {
174 template <typename T, typename Iterator, typename Context>
175 void on_success(Iterator const&, Iterator const&, T& ast, Context const& context)
176 {
177 auto& parserContext = x3::get<parser_context_tag>(context);
Václav Kubernát48fc3832018-05-28 14:21:22 +0200178 const auto& schema = parserContext.m_schema;
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200179
Václav Kubernát72749c62020-01-03 16:47:34 +0100180 if (!schema.isContainer(parserContext.currentSchemaPath(), {parserContext.m_curModule, ast.m_name}))
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200181 _pass(context) = false;
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200182 }
183};
184
Václav Kubernát07204242018-06-04 18:12:09 +0200185struct leaf_class {
186 template <typename T, typename Iterator, typename Context>
187 void on_success(Iterator const&, Iterator const&, T& ast, Context const& context)
188 {
189 auto& parserContext = x3::get<parser_context_tag>(context);
190 const auto& schema = parserContext.m_schema;
191
Václav Kubernát72749c62020-01-03 16:47:34 +0100192 if (!schema.isLeaf(parserContext.currentSchemaPath(), {parserContext.m_curModule, ast.m_name}))
Václav Kubernát744f57f2018-06-29 22:46:26 +0200193 _pass(context) = false;
194 }
195};
196
Václav Kubernát744f57f2018-06-29 22:46:26 +0200197struct module_class {
198 template <typename T, typename Iterator, typename Context>
199 void on_success(Iterator const&, Iterator const&, T& ast, Context const& context)
200 {
201 auto& parserContext = x3::get<parser_context_tag>(context);
202 const auto& schema = parserContext.m_schema;
203
Václav Kubernát75877de2019-11-20 17:43:02 +0100204 if (schema.isModule(ast.m_name)) {
Václav Kubernát744f57f2018-06-29 22:46:26 +0200205 parserContext.m_curModule = ast.m_name;
206 parserContext.m_topLevelModulePresent = true;
Václav Kubernát07204242018-06-04 18:12:09 +0200207 } else {
Václav Kubernát744f57f2018-06-29 22:46:26 +0200208 parserContext.m_errorMsg = "Invalid module name.";
Václav Kubernát07204242018-06-04 18:12:09 +0200209 _pass(context) = false;
210 }
211 }
212};
213
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200214struct schemaNode_class {
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200215 template <typename T, typename Iterator, typename Context>
Václav Kubernát744f57f2018-06-29 22:46:26 +0200216 void on_success(Iterator const&, Iterator const&, T& ast, Context const& context)
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200217 {
Václav Kubernát744f57f2018-06-29 22:46:26 +0200218 auto& parserContext = x3::get<parser_context_tag>(context);
Václav Kubernát72749c62020-01-03 16:47:34 +0100219 parserContext.pushPathFragment(ast);
220 parserContext.m_curModule = boost::none;
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200221 }
Václav Kubernát744f57f2018-06-29 22:46:26 +0200222};
Václav Kubernát07204242018-06-04 18:12:09 +0200223
Václav Kubernát39ae9592019-02-05 17:35:16 +0100224struct dataNodeList_class {
225 template <typename T, typename Iterator, typename Context>
226 void on_success(Iterator const&, Iterator const&, T& ast, Context const& context)
227 {
228 auto& parserContext = x3::get<parser_context_tag>(context);
Václav Kubernát72749c62020-01-03 16:47:34 +0100229 parserContext.pushPathFragment(ast);
Václav Kubernát39ae9592019-02-05 17:35:16 +0100230 }
231};
Václav Kubernát5c75b252018-10-10 18:33:47 +0200232
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200233struct dataNode_class {
234 template <typename T, typename Iterator, typename Context>
235 void on_success(Iterator const&, Iterator const&, T& ast, Context const& context)
236 {
237 auto& parserContext = x3::get<parser_context_tag>(context);
Václav Kubernát72749c62020-01-03 16:47:34 +0100238 parserContext.pushPathFragment(ast);
239 parserContext.m_curModule = boost::none;
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200240 }
241};
242
Václav Kubernát37171a12018-08-31 17:01:48 +0200243struct absoluteStart_class {
244 template <typename T, typename Iterator, typename Context>
245 void on_success(Iterator const&, Iterator const&, T&, Context const& context)
246 {
247 auto& parserContext = x3::get<parser_context_tag>(context);
Václav Kubernát72749c62020-01-03 16:47:34 +0100248 parserContext.clearPath();
Václav Kubernát37171a12018-08-31 17:01:48 +0200249 }
250};
251
Václav Kubernát5c75b252018-10-10 18:33:47 +0200252struct dataNodesListEnd_class;
253
254struct dataPathListEnd_class;
255
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200256struct dataPath_class {
257 template <typename Iterator, typename Exception, typename Context>
258 x3::error_handler_result on_error(Iterator&, Iterator const&, Exception const&, Context const& context)
259 {
260 auto& parserContext = x3::get<parser_context_tag>(context);
261 if (parserContext.m_errorMsg.empty()) {
262 parserContext.m_errorMsg = "Expected path.";
263 return x3::error_handler_result::fail;
264 } else {
265 return x3::error_handler_result::rethrow;
266 }
267 }
268};
269
270struct schemaPath_class {
Václav Kubernát07204242018-06-04 18:12:09 +0200271 template <typename Iterator, typename Exception, typename Context>
Václav Kubernát41378452018-06-06 16:29:40 +0200272 x3::error_handler_result on_error(Iterator&, Iterator const&, Exception const&, Context const& context)
Václav Kubernát07204242018-06-04 18:12:09 +0200273 {
274 auto& parserContext = x3::get<parser_context_tag>(context);
Václav Kubernát41378452018-06-06 16:29:40 +0200275 if (parserContext.m_errorMsg.empty()) {
276 parserContext.m_errorMsg = "Expected path.";
Václav Kubernát07204242018-06-04 18:12:09 +0200277 return x3::error_handler_result::fail;
Václav Kubernát41378452018-06-06 16:29:40 +0200278 } else {
279 return x3::error_handler_result::rethrow;
280 }
Václav Kubernát07204242018-06-04 18:12:09 +0200281 }
282};
283
Václav Kubernát6d791432018-10-25 16:00:35 +0200284struct discard_class;
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200285
Václav Kubernát11afac72018-07-18 14:59:53 +0200286struct ls_class;
287
Václav Kubernát744f57f2018-06-29 22:46:26 +0200288struct cd_class {
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200289 template <typename Iterator, typename Exception, typename Context>
290 x3::error_handler_result on_error(Iterator&, Iterator const&, Exception const& x, Context const& context)
291 {
292 auto& parserContext = x3::get<parser_context_tag>(context);
Václav Kubernát41378452018-06-06 16:29:40 +0200293 if (parserContext.m_errorMsg.empty())
294 parserContext.m_errorMsg = "Expected " + x.which() + " here:";
295 return x3::error_handler_result::rethrow;
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200296 }
297};
Václav Kubernátb61336d2018-05-28 17:35:03 +0200298
Václav Kubernát6797df02019-03-18 20:21:50 +0100299struct presenceContainerPath_class {
Václav Kubernátb61336d2018-05-28 17:35:03 +0200300 template <typename T, typename Iterator, typename Context>
301 void on_success(Iterator const&, Iterator const&, T& ast, Context const& context)
302 {
303 auto& parserContext = x3::get<parser_context_tag>(context);
304 const auto& schema = parserContext.m_schema;
305 try {
Václav Kubernát744f57f2018-06-29 22:46:26 +0200306 boost::optional<std::string> module;
Václav Kubernát6797df02019-03-18 20:21:50 +0100307 if (ast.m_nodes.back().m_prefix)
308 module = ast.m_nodes.back().m_prefix.value().m_name;
309 container_ cont = boost::get<container_>(ast.m_nodes.back().m_suffix);
Václav Kubernát72749c62020-01-03 16:47:34 +0100310 auto location = pathWithoutLastNode(parserContext.currentSchemaPath());
Václav Kubernátb61336d2018-05-28 17:35:03 +0200311
Václav Kubernát744f57f2018-06-29 22:46:26 +0200312 if (!schema.isPresenceContainer(location, {module, cont.m_name})) {
Václav Kubernát41378452018-06-06 16:29:40 +0200313 parserContext.m_errorMsg = "This container is not a presence container.";
Václav Kubernátb61336d2018-05-28 17:35:03 +0200314 _pass(context) = false;
Václav Kubernátb61336d2018-05-28 17:35:03 +0200315 }
316 } catch (boost::bad_get&) {
Václav Kubernát41378452018-06-06 16:29:40 +0200317 parserContext.m_errorMsg = "This is not a container.";
Václav Kubernátb61336d2018-05-28 17:35:03 +0200318 _pass(context) = false;
Václav Kubernátb61336d2018-05-28 17:35:03 +0200319 }
320 }
Václav Kubernát6797df02019-03-18 20:21:50 +0100321};
Václav Kubernátb61336d2018-05-28 17:35:03 +0200322
Václav Kubernátf5f64f02019-03-19 17:15:47 +0100323struct listInstancePath_class {
324 template <typename T, typename Iterator, typename Context>
325 void on_success(Iterator const&, Iterator const&, T& ast, Context const& context)
326 {
327 auto& parserContext = x3::get<parser_context_tag>(context);
328 if (ast.m_nodes.back().m_suffix.type() != typeid(listElement_)) {
329 parserContext.m_errorMsg = "This is not a list instance.";
330 _pass(context) = false;
331 }
332 }
333};
334
Václav Kubernát8028f942019-09-25 16:03:23 +0200335struct space_separator_class {
336 template <typename T, typename Iterator, typename Context>
337 void on_success(Iterator const&, Iterator const&, T&, Context const& context)
338 {
339 auto& parserContext = x3::get<parser_context_tag>(context);
340 parserContext.m_suggestions.clear();
Václav Kubernát1ed4aa32020-01-23 13:13:28 +0100341 parserContext.m_completionIterator = boost::none;
Václav Kubernáte69133a2019-11-01 19:01:34 +0100342 parserContext.m_completionSuffix.clear();
Václav Kubernát8028f942019-09-25 16:03:23 +0200343 }
344};
345
Václav Kubernát6797df02019-03-18 20:21:50 +0100346struct create_class {
Václav Kubernátb61336d2018-05-28 17:35:03 +0200347 template <typename Iterator, typename Exception, typename Context>
Václav Kubernát41378452018-06-06 16:29:40 +0200348 x3::error_handler_result on_error(Iterator&, Iterator const&, Exception const&, Context const& context)
Václav Kubernátb61336d2018-05-28 17:35:03 +0200349 {
350 auto& parserContext = x3::get<parser_context_tag>(context);
Václav Kubernát41378452018-06-06 16:29:40 +0200351 if (parserContext.m_errorMsg.empty())
352 parserContext.m_errorMsg = "Couldn't parse create/delete command.";
353 return x3::error_handler_result::rethrow;
Václav Kubernátb61336d2018-05-28 17:35:03 +0200354 }
355};
356
Václav Kubernát6797df02019-03-18 20:21:50 +0100357struct delete_class {
358 template <typename Iterator, typename Exception, typename Context>
359 x3::error_handler_result on_error(Iterator&, Iterator const&, Exception const&, Context const& context)
360 {
361 auto& parserContext = x3::get<parser_context_tag>(context);
362 if (parserContext.m_errorMsg.empty())
363 parserContext.m_errorMsg = "Couldn't parse create/delete command.";
364 return x3::error_handler_result::rethrow;
365 }
Václav Kubernátb61336d2018-05-28 17:35:03 +0200366};
367
Václav Kubernát0b0272f2018-06-13 14:13:08 +0200368struct leaf_path_class {
369 template <typename T, typename Iterator, typename Context>
Václav Kubernát7707cae2020-01-16 12:04:53 +0100370 void on_success(Iterator const&, Iterator const&, T&, Context const& context)
Václav Kubernát0b0272f2018-06-13 14:13:08 +0200371 {
372 auto& parserContext = x3::get<parser_context_tag>(context);
373 try {
Václav Kubernát7707cae2020-01-16 12:04:53 +0100374 auto lastNode = parserContext.currentSchemaPath().m_nodes.back();
375 auto leaf = boost::get<leaf_>(lastNode.m_suffix);
376 auto location = pathWithoutLastNode(parserContext.currentSchemaPath());
377 ModuleNodePair node{lastNode.m_prefix.flat_map([](const auto& it) { return boost::optional<std::string>{it.m_name}; }), leaf.m_name};
378
379 parserContext.m_tmpListKeyLeafPath.m_location = location;
380 parserContext.m_tmpListKeyLeafPath.m_node = node;
381
Václav Kubernát0b0272f2018-06-13 14:13:08 +0200382 } catch (boost::bad_get&) {
383 parserContext.m_errorMsg = "This is not a path to leaf.";
384 _pass(context) = false;
385 }
386 }
387};
388
Václav Kubernáteeb38842019-03-20 19:46:05 +0100389// This handler only checks if the module exists
390// It doesn't set any ParserContext flags (except the error message)
391struct data_module_prefix_class {
392 template <typename T, typename Iterator, typename Context>
393 void on_success(Iterator const&, Iterator const&, T& ast, Context const& context)
394 {
395 auto& parserContext = x3::get<parser_context_tag>(context);
396 const auto& schema = parserContext.m_schema;
397
Václav Kubernát72749c62020-01-03 16:47:34 +0100398 if (!schema.isModule(parserContext.currentSchemaPath(), ast.m_name)) {
Václav Kubernáteeb38842019-03-20 19:46:05 +0100399 parserContext.m_errorMsg = "Invalid module name.";
400 _pass(context) = false;
401 }
402 }
403};
404
Václav Kubernátebca2552018-06-08 19:06:02 +0200405struct leaf_data_class {
Václav Kubernát0b0272f2018-06-13 14:13:08 +0200406 template <typename Iterator, typename Exception, typename Context>
407 x3::error_handler_result on_error(Iterator&, Iterator const&, Exception const&, Context const& context)
408 {
409 auto& parserContext = x3::get<parser_context_tag>(context);
410 auto& schema = parserContext.m_schema;
411 if (parserContext.m_errorMsg.empty()) {
Václav Kubernát7707cae2020-01-16 12:04:53 +0100412 parserContext.m_errorMsg = "leaf data type mismatch: Expected " +
413 leafDataTypeToString(schema.leafType(parserContext.m_tmpListKeyLeafPath.m_location, parserContext.m_tmpListKeyLeafPath.m_node)) + " here:";
Václav Kubernát0b0272f2018-06-13 14:13:08 +0200414 return x3::error_handler_result::fail;
415 }
416 return x3::error_handler_result::rethrow;
417 }
Václav Kubernátebca2552018-06-08 19:06:02 +0200418};
419
Václav Kubernát90de9502019-11-20 17:19:44 +0100420template <yang::LeafDataTypes TYPE>
Václav Kubernátebca2552018-06-08 19:06:02 +0200421struct leaf_data_base_class {
422 yang::LeafDataTypes m_type;
423
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200424 leaf_data_base_class()
425 : m_type(TYPE)
Václav Kubernátebca2552018-06-08 19:06:02 +0200426 {
427 }
428
429 template <typename T, typename Iterator, typename Context>
430 void on_success(Iterator const&, Iterator const&, T&, Context const& context)
431 {
432 auto& parserContext = x3::get<parser_context_tag>(context);
433 auto& schema = parserContext.m_schema;
434
Václav Kubernát7707cae2020-01-16 12:04:53 +0100435 auto type = schema.leafType(parserContext.m_tmpListKeyLeafPath.m_location, parserContext.m_tmpListKeyLeafPath.m_node);
Václav Kubernát6a8d1d92019-04-24 20:30:36 +0200436 if (type == yang::LeafDataTypes::LeafRef) {
Václav Kubernát7707cae2020-01-16 12:04:53 +0100437 type = schema.leafrefBase(parserContext.m_tmpListKeyLeafPath.m_location, parserContext.m_tmpListKeyLeafPath.m_node);
Václav Kubernát6a8d1d92019-04-24 20:30:36 +0200438 }
Václav Kubernát7707cae2020-01-16 12:04:53 +0100439
Václav Kubernát6a8d1d92019-04-24 20:30:36 +0200440 if (type != m_type) {
Václav Kubernátebca2552018-06-08 19:06:02 +0200441 _pass(context) = false;
442 }
443 }
444};
445
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200446struct leaf_data_binary_data_class;
447
448struct leaf_data_enum_class : leaf_data_base_class<yang::LeafDataTypes::Enum> {
449 using leaf_data_base_class::leaf_data_base_class;
Václav Kubernátebca2552018-06-08 19:06:02 +0200450
451 template <typename T, typename Iterator, typename Context>
452 void on_success(Iterator const& start, Iterator const& end, T& ast, Context const& context)
453 {
454 leaf_data_base_class::on_success(start, end, ast, context);
Václav Kubernáteeb38842019-03-20 19:46:05 +0100455 // Base class on_success cannot return for us, so we check if it failed the parser.
456 if (_pass(context) == false)
457 return;
Václav Kubernátebca2552018-06-08 19:06:02 +0200458 auto& parserContext = x3::get<parser_context_tag>(context);
459 auto& schema = parserContext.m_schema;
460
Václav Kubernát7707cae2020-01-16 12:04:53 +0100461 if (!schema.leafEnumHasValue(parserContext.m_tmpListKeyLeafPath.m_location, parserContext.m_tmpListKeyLeafPath.m_node, ast.m_value)) {
Václav Kubernátebca2552018-06-08 19:06:02 +0200462 _pass(context) = false;
Václav Kubernát777704d2020-01-15 18:48:06 +0100463 parserContext.m_errorMsg = "leaf data type mismatch: Expected an enum here. Allowed values:";
Václav Kubernát7707cae2020-01-16 12:04:53 +0100464 for (const auto& it : schema.enumValues(parserContext.m_tmpListKeyLeafPath.m_location, parserContext.m_tmpListKeyLeafPath.m_node)) {
Václav Kubernát69710252019-11-15 17:08:30 +0100465 parserContext.m_errorMsg += " " + it;
466 }
Václav Kubernátebca2552018-06-08 19:06:02 +0200467 }
468 }
469};
470
Václav Kubernáteeb38842019-03-20 19:46:05 +0100471struct leaf_data_identityRef_data_class;
472
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200473struct leaf_data_identityRef_class : leaf_data_base_class<yang::LeafDataTypes::IdentityRef> {
474 using leaf_data_base_class::leaf_data_base_class;
Václav Kubernáteeb38842019-03-20 19:46:05 +0100475
476 template <typename T, typename Iterator, typename Context>
477 void on_success(Iterator const& start, Iterator const& end, T& ast, Context const& context)
478 {
479 // FIXME: can I reuse leaf_data_enum_class somehow..?
480 leaf_data_base_class::on_success(start, end, ast, context);
481 // Base class on_success cannot return for us, so we check if it failed the parser.
482 if (_pass(context) == false)
483 return;
484 auto& parserContext = x3::get<parser_context_tag>(context);
485 auto& schema = parserContext.m_schema;
Václav Kubernáteeb38842019-03-20 19:46:05 +0100486
487 ModuleValuePair pair;
488 if (ast.m_prefix) {
489 pair.first = ast.m_prefix.get().m_name;
490 }
491 pair.second = ast.m_value;
492
Václav Kubernát7707cae2020-01-16 12:04:53 +0100493 if (!schema.leafIdentityIsValid(parserContext.m_tmpListKeyLeafPath.m_location, parserContext.m_tmpListKeyLeafPath.m_node, pair)) {
Václav Kubernáteeb38842019-03-20 19:46:05 +0100494 _pass(context) = false;
495 }
496 }
497};
498
Václav Kubernát07204242018-06-04 18:12:09 +0200499struct set_class {
Václav Kubernát07204242018-06-04 18:12:09 +0200500 template <typename Iterator, typename Exception, typename Context>
501 x3::error_handler_result on_error(Iterator&, Iterator const&, Exception const& x, Context const& context)
502 {
503 auto& parserContext = x3::get<parser_context_tag>(context);
Václav Kubernát41378452018-06-06 16:29:40 +0200504 if (parserContext.m_errorMsg.empty())
505 parserContext.m_errorMsg = "Expected " + x.which() + " here:";
506 return x3::error_handler_result::rethrow;
Václav Kubernát07204242018-06-04 18:12:09 +0200507 }
508};
509
Václav Kubernát812ee282018-08-30 17:10:03 +0200510struct commit_class;
511
Václav Kubernát054cc992019-02-21 14:23:52 +0100512struct help_class;
513
Václav Kubernátb6ff0b62018-08-30 16:14:53 +0200514struct get_class;
515
Václav Kubernátb61336d2018-05-28 17:35:03 +0200516struct command_class {
517 template <typename Iterator, typename Exception, typename Context>
518 x3::error_handler_result on_error(Iterator&, Iterator const&, Exception const& x, Context const& context)
519 {
520 auto& parserContext = x3::get<parser_context_tag>(context);
521 auto& error_handler = x3::get<x3::error_handler_tag>(context).get();
Václav Kubernát41378452018-06-06 16:29:40 +0200522 if (parserContext.m_errorMsg.empty()) {
523 parserContext.m_errorMsg = "Unknown command.";
524 }
525 error_handler(x.where(), parserContext.m_errorMsg);
Václav Kubernátb61336d2018-05-28 17:35:03 +0200526 return x3::error_handler_result::fail;
527 }
528};
Václav Kubernát5c75b252018-10-10 18:33:47 +0200529
Václav Kubernát4108e0d2018-10-29 13:32:22 +0100530struct initializePath_class {
Václav Kubernát5c75b252018-10-10 18:33:47 +0200531 template <typename T, typename Iterator, typename Context>
532 void on_success(Iterator const&, Iterator const&, T&, Context const& context)
533 {
534 auto& parserContext = x3::get<parser_context_tag>(context);
Václav Kubernát72749c62020-01-03 16:47:34 +0100535 parserContext.resetPath();
Václav Kubernát5c75b252018-10-10 18:33:47 +0200536 parserContext.m_tmpListKeys.clear();
537 parserContext.m_tmpListName.clear();
Václav Kubernát4108e0d2018-10-29 13:32:22 +0100538 parserContext.m_suggestions.clear();
Václav Kubernát5c75b252018-10-10 18:33:47 +0200539 }
540};
Václav Kubernátd6fd2492018-11-19 15:11:16 +0100541
542struct trailingSlash_class;
Václav Kubernát4108e0d2018-10-29 13:32:22 +0100543
544struct createPathSuggestions_class {
545 template <typename T, typename Iterator, typename Context>
546 void on_success(Iterator const& begin, Iterator const&, T&, Context const& context)
547 {
548 auto& parserContext = x3::get<parser_context_tag>(context);
549 const auto& schema = parserContext.m_schema;
550
551 parserContext.m_completionIterator = begin;
Václav Kubernát72749c62020-01-03 16:47:34 +0100552 auto suggestions = schema.childNodes(parserContext.currentSchemaPath(), Recursion::NonRecursive);
Václav Kubernáte69133a2019-11-01 19:01:34 +0100553 std::set<std::string> suffixesAdded;
554 std::transform(suggestions.begin(), suggestions.end(),
555 std::inserter(suffixesAdded, suffixesAdded.end()),
556 [&parserContext, &schema] (auto it) {
557 ModuleNodePair node;
558 if (auto colonPos = it.find(":"); colonPos != it.npos) {
559 node.first = it.substr(0, colonPos);
560 node.second = it.substr(colonPos + 1, node.second.npos);
561 } else {
562 node.first = boost::none;
563 node.second = it;
564 }
565
Václav Kubernát72749c62020-01-03 16:47:34 +0100566 if (schema.isLeaf(parserContext.currentSchemaPath(), node)) {
Václav Kubernáte69133a2019-11-01 19:01:34 +0100567 return it + " ";
568 }
Václav Kubernát72749c62020-01-03 16:47:34 +0100569 if (schema.isContainer(parserContext.currentSchemaPath(), node)) {
Václav Kubernáte69133a2019-11-01 19:01:34 +0100570 return it + "/";
571 }
Václav Kubernát72749c62020-01-03 16:47:34 +0100572 if (schema.isList(parserContext.currentSchemaPath(), node)) {
Václav Kubernáte69133a2019-11-01 19:01:34 +0100573 return it + "[";
574 }
575 return it;
576 });
577 parserContext.m_suggestions = suffixesAdded;
Václav Kubernát4108e0d2018-10-29 13:32:22 +0100578 }
579};
Václav Kubernát329c6c32019-02-06 16:41:53 +0100580
Václav Kubernát43908fb2020-01-02 19:05:51 +0100581std::set<std::string> generateMissingKeyCompletionSet(std::set<std::string> keysNeeded, std::map<std::string, leaf_data_> currentSet);
Václav Kubernát329c6c32019-02-06 16:41:53 +0100582
583struct createKeySuggestions_class {
584 template <typename T, typename Iterator, typename Context>
585 void on_success(Iterator const& begin, Iterator const&, T&, Context const& context)
586 {
587 auto& parserContext = x3::get<parser_context_tag>(context);
588 const auto& schema = parserContext.m_schema;
589
590 parserContext.m_completionIterator = begin;
591
Václav Kubernát72749c62020-01-03 16:47:34 +0100592 const auto& keysNeeded = schema.listKeys(parserContext.currentSchemaPath(), {parserContext.m_curModule, parserContext.m_tmpListName});
Václav Kubernát329c6c32019-02-06 16:41:53 +0100593 parserContext.m_suggestions = generateMissingKeyCompletionSet(keysNeeded, parserContext.m_tmpListKeys);
594 }
595};
596
Václav Kubernát43908fb2020-01-02 19:05:51 +0100597std::string leafDataToCompletion(const leaf_data_& value);
598
599struct createValueSuggestions_class {
600 template <typename T, typename Iterator, typename Context>
601 void on_success(Iterator const& begin, Iterator const&, T&, Context const& context)
602 {
603 auto& parserContext = x3::get<parser_context_tag>(context);
604 if (!parserContext.m_completing) {
605 return;
606 }
607 const auto& dataQuery = parserContext.m_dataquery;
608
609 parserContext.m_completionIterator = begin;
610 auto listInstances = dataQuery->listKeys(parserContext.currentDataPath(), {parserContext.m_curModule, parserContext.m_tmpListName});
611
612 decltype(listInstances) filteredInstances;
613 //This filters out instances, which don't correspond to the partial instance we have.
614 const auto partialFitsComplete = [&parserContext] (const auto& complete) {
615 const auto& partial = parserContext.m_tmpListKeys;
616 return std::all_of(partial.begin(), partial.end(), [&complete] (const auto& oneKV) {
617 const auto& [k, v] = oneKV;
618 return complete.at(k) == v;
619 });
620 };
621 std::copy_if(listInstances.begin(), listInstances.end(), std::inserter(filteredInstances, filteredInstances.end()), partialFitsComplete);
622
623 std::set<std::string> validValues;
624
625 std::transform(filteredInstances.begin(), filteredInstances.end(), std::inserter(validValues, validValues.end()), [&parserContext] (const auto& instance) {
626 return leafDataToCompletion(instance.at(parserContext.m_tmpListKeyLeafPath.m_node.second));
627 });
628
629 parserContext.m_suggestions = validValues;
630 }
631};
632
Václav Kubernát329c6c32019-02-06 16:41:53 +0100633struct suggestKeysEnd_class {
634 template <typename T, typename Iterator, typename Context>
635 void on_success(Iterator const& begin, Iterator const&, T&, Context const& context)
636 {
637 auto& parserContext = x3::get<parser_context_tag>(context);
638 const auto& schema = parserContext.m_schema;
639
640 parserContext.m_completionIterator = begin;
Václav Kubernát72749c62020-01-03 16:47:34 +0100641 const auto& keysNeeded = schema.listKeys(parserContext.currentSchemaPath(), {parserContext.m_curModule, parserContext.m_tmpListName});
Václav Kubernát329c6c32019-02-06 16:41:53 +0100642 if (generateMissingKeyCompletionSet(keysNeeded, parserContext.m_tmpListKeys).empty()) {
643 parserContext.m_suggestions = {"]/"};
644 } else {
Václav Kubernát4c325482019-04-11 17:51:55 +0200645 parserContext.m_suggestions = {"]["};
Václav Kubernát329c6c32019-02-06 16:41:53 +0100646 }
647 }
648};
Václav Kubernát57272422019-02-08 12:48:24 +0100649
650struct commandNamesVisitor {
651 template <typename T>
652 auto operator()(boost::type<T>)
653 {
654 return T::name;
655 }
656};
657
658struct createCommandSuggestions_class {
659 template <typename T, typename Iterator, typename Context>
660 void on_success(Iterator const& begin, Iterator const&, T&, Context const& context)
661 {
662 auto& parserContext = x3::get<parser_context_tag>(context);
663 parserContext.m_completionIterator = begin;
664
665 parserContext.m_suggestions.clear();
666 boost::mpl::for_each<CommandTypes, boost::type<boost::mpl::_>>([&parserContext](auto cmd) {
667 parserContext.m_suggestions.emplace(commandNamesVisitor()(cmd));
668 });
669 }
670};
Václav Kubernátac035d62019-02-18 10:59:08 +0100671
672struct completing_class {
673 template <typename T, typename Iterator, typename Context>
674 void on_success(Iterator const&, Iterator const&, T&, Context const& context)
675 {
676 auto& parserContext = x3::get<parser_context_tag>(context);
677
678 if (!parserContext.m_completing)
679 _pass(context) = false;
680 }
681};
Václav Kubernát989b5de2019-02-20 16:28:35 +0100682
Václav Kubernát4294a852020-02-14 15:07:14 +0100683template<yang::LeafDataTypes TYPE>
684struct createSetSuggestions_class {
685 decltype(ParserContext::m_suggestions) getSuggestions(const ParserContext& ctx, const Schema& schema) const;
686
Václav Kubernát989b5de2019-02-20 16:28:35 +0100687 template <typename T, typename Iterator, typename Context>
688 void on_success(Iterator const& begin, Iterator const&, T&, Context const& context)
689 {
690 auto& parserContext = x3::get<parser_context_tag>(context);
Václav Kubernát989b5de2019-02-20 16:28:35 +0100691 const Schema& schema = parserContext.m_schema;
692
Václav Kubernát4294a852020-02-14 15:07:14 +0100693 // Only generate completions if the type is correct so that we don't
Václav Kubernáteeb38842019-03-20 19:46:05 +0100694 // overwrite some other completions.
Václav Kubernát4294a852020-02-14 15:07:14 +0100695 if (schema.leafType(parserContext.m_tmpListKeyLeafPath.m_location, parserContext.m_tmpListKeyLeafPath.m_node) == TYPE) {
Václav Kubernát117f7b52020-01-23 16:10:55 +0100696 parserContext.m_completionIterator = begin;
Václav Kubernát4294a852020-02-14 15:07:14 +0100697 parserContext.m_suggestions = getSuggestions(parserContext, schema);
Václav Kubernát117f7b52020-01-23 16:10:55 +0100698 }
Václav Kubernát989b5de2019-02-20 16:28:35 +0100699 }
700};