blob: 75d85537012bc261be08cc5b2279b191337637c4 [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át48fc3832018-05-28 14:21:22 +020017#include "schema.hpp"
Václav Kubernátebca2552018-06-08 19:06:02 +020018#include "utils.hpp"
Václav Kubernát329c6c32019-02-06 16:41:53 +010019namespace x3 = boost::spirit::x3;
20
21struct parser_context_tag;
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020022
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020023struct keyValue_class {
24 template <typename T, typename Iterator, typename Context>
25 void on_success(Iterator const&, Iterator const&, T& ast, Context const& context)
26 {
27 auto& parserContext = x3::get<parser_context_tag>(context);
Václav Kubernát48fc3832018-05-28 14:21:22 +020028 const Schema& schema = parserContext.m_schema;
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át744f57f2018-06-29 22:46:26 +020033 } else if (!schema.listHasKey(parserContext.m_curPath, {parserContext.m_curModule, parserContext.m_tmpListName}, ast.first)) {
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020034 _pass(context) = false;
35 parserContext.m_errorMsg = parserContext.m_tmpListName + " is not indexed by \"" + ast.first + "\".";
Václav Kubernát41378452018-06-06 16:29:40 +020036 } else {
37 parserContext.m_tmpListKeys.insert(ast.first);
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át744f57f2018-06-29 22:46:26 +020050struct node_identifier_class {
51 template <typename T, typename Iterator, typename Context>
52 void on_success(Iterator const&, Iterator const&, T&, Context const& context)
53 {
54 auto& parserContext = x3::get<parser_context_tag>(context);
55
56 if (!parserContext.m_topLevelModulePresent) {
57 if (parserContext.m_errorMsg.empty())
58 parserContext.m_errorMsg = "You have to specify a top level module.";
59 _pass(context) = false;
60 }
61 }
62};
63
Václav Kubernát89728d82018-09-13 16:28:28 +020064struct key_identifier_class;
65
Václav Kubernát744f57f2018-06-29 22:46:26 +020066struct module_identifier_class;
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020067
68struct listPrefix_class {
69 template <typename T, typename Iterator, typename Context>
70 void on_success(Iterator const&, Iterator const&, T& ast, Context const& context)
71 {
72 auto& parserContext = x3::get<parser_context_tag>(context);
Václav Kubernát48fc3832018-05-28 14:21:22 +020073 const Schema& schema = parserContext.m_schema;
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020074
Václav Kubernát744f57f2018-06-29 22:46:26 +020075 if (schema.isList(parserContext.m_curPath, {parserContext.m_curModule, ast})) {
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020076 parserContext.m_tmpListName = ast;
77 } else {
78 _pass(context) = false;
79 }
80 }
81};
82
83struct listSuffix_class {
84 template <typename T, typename Iterator, typename Context>
85 void on_success(Iterator const&, Iterator const&, T& ast, Context const& context)
86 {
87 auto& parserContext = x3::get<parser_context_tag>(context);
Václav Kubernát48fc3832018-05-28 14:21:22 +020088 const Schema& schema = parserContext.m_schema;
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020089
Václav Kubernát744f57f2018-06-29 22:46:26 +020090 const auto& keysNeeded = schema.listKeys(parserContext.m_curPath, {parserContext.m_curModule, parserContext.m_tmpListName});
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020091 std::set<std::string> keysSupplied;
92 for (const auto& it : ast)
93 keysSupplied.insert(it.first);
94
95 if (keysNeeded != keysSupplied) {
96 parserContext.m_errorMsg = "Not enough keys for " + parserContext.m_tmpListName + ". " +
97 "These keys were not supplied:";
98 std::set<std::string> missingKeys;
99 std::set_difference(keysNeeded.begin(), keysNeeded.end(),
100 keysSupplied.begin(), keysSupplied.end(),
101 std::inserter(missingKeys, missingKeys.end()));
102
103 for (const auto& it : missingKeys)
104 parserContext.m_errorMsg += " " + it;
105 parserContext.m_errorMsg += ".";
106
107 _pass(context) = false;
108 }
109 }
Václav Kubernát41378452018-06-06 16:29:40 +0200110
111 template <typename Iterator, typename Exception, typename Context>
112 x3::error_handler_result on_error(Iterator&, Iterator const&, Exception const&, Context const& context)
113 {
114 auto& parserContext = x3::get<parser_context_tag>(context);
115 if (parserContext.m_errorMsg.empty())
116 parserContext.m_errorMsg = "Expecting ']' here:";
117 return x3::error_handler_result::rethrow;
Václav Kubernát41378452018-06-06 16:29:40 +0200118 }
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200119};
120struct listElement_class {
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200121 template <typename Iterator, typename Exception, typename Context>
Václav Kubernát41378452018-06-06 16:29:40 +0200122 x3::error_handler_result on_error(Iterator&, Iterator const&, Exception const&, Context const& context)
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200123 {
124 auto& parserContext = x3::get<parser_context_tag>(context);
Václav Kubernát41378452018-06-06 16:29:40 +0200125 if (parserContext.m_errorMsg.empty()) {
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200126 return x3::error_handler_result::fail;
Václav Kubernát41378452018-06-06 16:29:40 +0200127 } else {
128 return x3::error_handler_result::rethrow;
129 }
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200130 }
131};
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200132struct list_class {
133 template <typename T, typename Iterator, typename Context>
134 void on_success(Iterator const&, Iterator const&, T& ast, Context const& context)
135 {
136 auto& parserContext = x3::get<parser_context_tag>(context);
137 const Schema& schema = parserContext.m_schema;
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200138
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200139 if (!schema.isList(parserContext.m_curPath, {parserContext.m_curModule, ast.m_name})) {
140 _pass(context) = false;
141 }
142 }
143};
Václav Kubernát60d6f292018-05-25 09:45:32 +0200144struct nodeup_class {
145 template <typename T, typename Iterator, typename Context>
146 void on_success(Iterator const&, Iterator const&, T&, Context const& context)
147 {
148 auto& parserContext = x3::get<parser_context_tag>(context);
149
Václav Kubernát744f57f2018-06-29 22:46:26 +0200150 if (parserContext.m_curPath.m_nodes.empty())
Václav Kubernát60d6f292018-05-25 09:45:32 +0200151 _pass(context) = false;
Václav Kubernát60d6f292018-05-25 09:45:32 +0200152 }
153};
154
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200155struct container_class {
156 template <typename T, typename Iterator, typename Context>
157 void on_success(Iterator const&, Iterator const&, T& ast, Context const& context)
158 {
159 auto& parserContext = x3::get<parser_context_tag>(context);
Václav Kubernát48fc3832018-05-28 14:21:22 +0200160 const auto& schema = parserContext.m_schema;
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200161
Václav Kubernát744f57f2018-06-29 22:46:26 +0200162 if (!schema.isContainer(parserContext.m_curPath, {parserContext.m_curModule, ast.m_name}))
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200163 _pass(context) = false;
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200164 }
165};
166
Václav Kubernát07204242018-06-04 18:12:09 +0200167struct leaf_class {
168 template <typename T, typename Iterator, typename Context>
169 void on_success(Iterator const&, Iterator const&, T& ast, Context const& context)
170 {
171 auto& parserContext = x3::get<parser_context_tag>(context);
172 const auto& schema = parserContext.m_schema;
173
Václav Kubernát744f57f2018-06-29 22:46:26 +0200174 if (!schema.isLeaf(parserContext.m_curPath, {parserContext.m_curModule, ast.m_name}))
175 _pass(context) = false;
176 }
177};
178
Václav Kubernát744f57f2018-06-29 22:46:26 +0200179struct module_class {
180 template <typename T, typename Iterator, typename Context>
181 void on_success(Iterator const&, Iterator const&, T& ast, Context const& context)
182 {
183 auto& parserContext = x3::get<parser_context_tag>(context);
184 const auto& schema = parserContext.m_schema;
185
186 if (schema.isModule(parserContext.m_curPath, ast.m_name)) {
187 parserContext.m_curModule = ast.m_name;
188 parserContext.m_topLevelModulePresent = true;
Václav Kubernát07204242018-06-04 18:12:09 +0200189 } else {
Václav Kubernát744f57f2018-06-29 22:46:26 +0200190 parserContext.m_errorMsg = "Invalid module name.";
Václav Kubernát07204242018-06-04 18:12:09 +0200191 _pass(context) = false;
192 }
193 }
194};
195
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200196struct schemaNode_class {
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200197 template <typename T, typename Iterator, typename Context>
Václav Kubernát744f57f2018-06-29 22:46:26 +0200198 void on_success(Iterator const&, Iterator const&, T& ast, Context const& context)
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200199 {
Václav Kubernát744f57f2018-06-29 22:46:26 +0200200 auto& parserContext = x3::get<parser_context_tag>(context);
201 if (ast.m_suffix.type() == typeid(nodeup_)) {
202 parserContext.m_curPath.m_nodes.pop_back();
203 if (parserContext.m_curPath.m_nodes.empty())
204 parserContext.m_topLevelModulePresent = false;
205 } else {
206 parserContext.m_curPath.m_nodes.push_back(ast);
207 parserContext.m_curModule = boost::none;
208 }
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200209 }
Václav Kubernát744f57f2018-06-29 22:46:26 +0200210};
Václav Kubernát07204242018-06-04 18:12:09 +0200211
Václav Kubernát39ae9592019-02-05 17:35:16 +0100212struct dataNodeList_class {
213 template <typename T, typename Iterator, typename Context>
214 void on_success(Iterator const&, Iterator const&, T& ast, Context const& context)
215 {
216 auto& parserContext = x3::get<parser_context_tag>(context);
217 parserContext.m_curPath.m_nodes.push_back(dataNodeToSchemaNode(ast));
218 }
219};
Václav Kubernát5c75b252018-10-10 18:33:47 +0200220
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200221struct dataNode_class {
222 template <typename T, typename Iterator, typename Context>
223 void on_success(Iterator const&, Iterator const&, T& ast, Context const& context)
224 {
225 auto& parserContext = x3::get<parser_context_tag>(context);
226 if (ast.m_suffix.type() == typeid(nodeup_)) {
227 parserContext.m_curPath.m_nodes.pop_back();
228 if (parserContext.m_curPath.m_nodes.empty())
229 parserContext.m_topLevelModulePresent = false;
230 } else {
231 parserContext.m_curPath.m_nodes.push_back(dataNodeToSchemaNode(ast));
232 parserContext.m_curModule = boost::none;
233 }
234 }
235};
236
Václav Kubernát37171a12018-08-31 17:01:48 +0200237struct absoluteStart_class {
238 template <typename T, typename Iterator, typename Context>
239 void on_success(Iterator const&, Iterator const&, T&, Context const& context)
240 {
241 auto& parserContext = x3::get<parser_context_tag>(context);
242 parserContext.m_curPath.m_nodes.clear();
Václav Kubernátb9621052019-03-18 18:27:49 +0100243 parserContext.m_topLevelModulePresent = false;
Václav Kubernát37171a12018-08-31 17:01:48 +0200244 }
245};
246
Václav Kubernát5c75b252018-10-10 18:33:47 +0200247struct dataNodesListEnd_class;
248
249struct dataPathListEnd_class;
250
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200251struct dataPath_class {
252 template <typename Iterator, typename Exception, typename Context>
253 x3::error_handler_result on_error(Iterator&, Iterator const&, Exception const&, Context const& context)
254 {
255 auto& parserContext = x3::get<parser_context_tag>(context);
256 if (parserContext.m_errorMsg.empty()) {
257 parserContext.m_errorMsg = "Expected path.";
258 return x3::error_handler_result::fail;
259 } else {
260 return x3::error_handler_result::rethrow;
261 }
262 }
263};
264
265struct schemaPath_class {
Václav Kubernát07204242018-06-04 18:12:09 +0200266 template <typename Iterator, typename Exception, typename Context>
Václav Kubernát41378452018-06-06 16:29:40 +0200267 x3::error_handler_result on_error(Iterator&, Iterator const&, Exception const&, Context const& context)
Václav Kubernát07204242018-06-04 18:12:09 +0200268 {
269 auto& parserContext = x3::get<parser_context_tag>(context);
Václav Kubernát41378452018-06-06 16:29:40 +0200270 if (parserContext.m_errorMsg.empty()) {
271 parserContext.m_errorMsg = "Expected path.";
Václav Kubernát07204242018-06-04 18:12:09 +0200272 return x3::error_handler_result::fail;
Václav Kubernát41378452018-06-06 16:29:40 +0200273 } else {
274 return x3::error_handler_result::rethrow;
275 }
Václav Kubernát07204242018-06-04 18:12:09 +0200276 }
277};
278
Václav Kubernát6d791432018-10-25 16:00:35 +0200279struct discard_class;
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200280
Václav Kubernát11afac72018-07-18 14:59:53 +0200281struct ls_class;
282
Václav Kubernát744f57f2018-06-29 22:46:26 +0200283struct cd_class {
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200284 template <typename Iterator, typename Exception, typename Context>
285 x3::error_handler_result on_error(Iterator&, Iterator const&, Exception const& x, Context const& context)
286 {
287 auto& parserContext = x3::get<parser_context_tag>(context);
Václav Kubernát41378452018-06-06 16:29:40 +0200288 if (parserContext.m_errorMsg.empty())
289 parserContext.m_errorMsg = "Expected " + x.which() + " here:";
290 return x3::error_handler_result::rethrow;
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200291 }
292};
Václav Kubernátb61336d2018-05-28 17:35:03 +0200293
Václav Kubernát6797df02019-03-18 20:21:50 +0100294struct presenceContainerPath_class {
Václav Kubernátb61336d2018-05-28 17:35:03 +0200295 template <typename T, typename Iterator, typename Context>
296 void on_success(Iterator const&, Iterator const&, T& ast, Context const& context)
297 {
298 auto& parserContext = x3::get<parser_context_tag>(context);
299 const auto& schema = parserContext.m_schema;
300 try {
Václav Kubernát744f57f2018-06-29 22:46:26 +0200301 boost::optional<std::string> module;
Václav Kubernát6797df02019-03-18 20:21:50 +0100302 if (ast.m_nodes.back().m_prefix)
303 module = ast.m_nodes.back().m_prefix.value().m_name;
304 container_ cont = boost::get<container_>(ast.m_nodes.back().m_suffix);
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200305 schemaPath_ location = pathWithoutLastNode(parserContext.m_curPath);
Václav Kubernátb61336d2018-05-28 17:35:03 +0200306
Václav Kubernát744f57f2018-06-29 22:46:26 +0200307 if (!schema.isPresenceContainer(location, {module, cont.m_name})) {
Václav Kubernát41378452018-06-06 16:29:40 +0200308 parserContext.m_errorMsg = "This container is not a presence container.";
Václav Kubernátb61336d2018-05-28 17:35:03 +0200309 _pass(context) = false;
Václav Kubernátb61336d2018-05-28 17:35:03 +0200310 }
311 } catch (boost::bad_get&) {
Václav Kubernát41378452018-06-06 16:29:40 +0200312 parserContext.m_errorMsg = "This is not a container.";
Václav Kubernátb61336d2018-05-28 17:35:03 +0200313 _pass(context) = false;
Václav Kubernátb61336d2018-05-28 17:35:03 +0200314 }
315 }
Václav Kubernát6797df02019-03-18 20:21:50 +0100316};
Václav Kubernátb61336d2018-05-28 17:35:03 +0200317
Václav Kubernátf5f64f02019-03-19 17:15:47 +0100318struct listInstancePath_class {
319 template <typename T, typename Iterator, typename Context>
320 void on_success(Iterator const&, Iterator const&, T& ast, Context const& context)
321 {
322 auto& parserContext = x3::get<parser_context_tag>(context);
323 if (ast.m_nodes.back().m_suffix.type() != typeid(listElement_)) {
324 parserContext.m_errorMsg = "This is not a list instance.";
325 _pass(context) = false;
326 }
327 }
328};
329
Václav Kubernát8028f942019-09-25 16:03:23 +0200330struct space_separator_class {
331 template <typename T, typename Iterator, typename Context>
332 void on_success(Iterator const&, Iterator const&, T&, Context const& context)
333 {
334 auto& parserContext = x3::get<parser_context_tag>(context);
335 parserContext.m_suggestions.clear();
Václav Kubernáte69133a2019-11-01 19:01:34 +0100336 parserContext.m_completionSuffix.clear();
Václav Kubernát8028f942019-09-25 16:03:23 +0200337 }
338};
339
Václav Kubernát6797df02019-03-18 20:21:50 +0100340struct create_class {
Václav Kubernátb61336d2018-05-28 17:35:03 +0200341 template <typename Iterator, typename Exception, typename Context>
Václav Kubernát41378452018-06-06 16:29:40 +0200342 x3::error_handler_result on_error(Iterator&, Iterator const&, Exception const&, Context const& context)
Václav Kubernátb61336d2018-05-28 17:35:03 +0200343 {
344 auto& parserContext = x3::get<parser_context_tag>(context);
Václav Kubernát41378452018-06-06 16:29:40 +0200345 if (parserContext.m_errorMsg.empty())
346 parserContext.m_errorMsg = "Couldn't parse create/delete command.";
347 return x3::error_handler_result::rethrow;
Václav Kubernátb61336d2018-05-28 17:35:03 +0200348 }
349};
350
Václav Kubernát6797df02019-03-18 20:21:50 +0100351struct delete_class {
352 template <typename Iterator, typename Exception, typename Context>
353 x3::error_handler_result on_error(Iterator&, Iterator const&, Exception const&, Context const& context)
354 {
355 auto& parserContext = x3::get<parser_context_tag>(context);
356 if (parserContext.m_errorMsg.empty())
357 parserContext.m_errorMsg = "Couldn't parse create/delete command.";
358 return x3::error_handler_result::rethrow;
359 }
Václav Kubernátb61336d2018-05-28 17:35:03 +0200360};
361
Václav Kubernát0b0272f2018-06-13 14:13:08 +0200362struct leaf_path_class {
363 template <typename T, typename Iterator, typename Context>
364 void on_success(Iterator const&, Iterator const&, T& ast, Context const& context)
365 {
366 auto& parserContext = x3::get<parser_context_tag>(context);
367 try {
Václav Kubernát744f57f2018-06-29 22:46:26 +0200368 auto leaf = boost::get<leaf_>(ast.m_nodes.back().m_suffix);
Václav Kubernát0b0272f2018-06-13 14:13:08 +0200369 } catch (boost::bad_get&) {
370 parserContext.m_errorMsg = "This is not a path to leaf.";
371 _pass(context) = false;
372 }
373 }
374};
375
Václav Kubernáteeb38842019-03-20 19:46:05 +0100376// This handler only checks if the module exists
377// It doesn't set any ParserContext flags (except the error message)
378struct data_module_prefix_class {
379 template <typename T, typename Iterator, typename Context>
380 void on_success(Iterator const&, Iterator const&, T& ast, Context const& context)
381 {
382 auto& parserContext = x3::get<parser_context_tag>(context);
383 const auto& schema = parserContext.m_schema;
384
385 if (!schema.isModule(parserContext.m_curPath, ast.m_name)) {
386 parserContext.m_errorMsg = "Invalid module name.";
387 _pass(context) = false;
388 }
389 }
390};
391
Václav Kubernátebca2552018-06-08 19:06:02 +0200392struct leaf_data_class {
Václav Kubernát0b0272f2018-06-13 14:13:08 +0200393 template <typename Iterator, typename Exception, typename Context>
394 x3::error_handler_result on_error(Iterator&, Iterator const&, Exception const&, Context const& context)
395 {
396 auto& parserContext = x3::get<parser_context_tag>(context);
397 auto& schema = parserContext.m_schema;
398 if (parserContext.m_errorMsg.empty()) {
Václav Kubernát744f57f2018-06-29 22:46:26 +0200399 leaf_ leaf = boost::get<leaf_>(parserContext.m_curPath.m_nodes.back().m_suffix);
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200400 schemaPath_ location = pathWithoutLastNode(parserContext.m_curPath);
Václav Kubernáteeb38842019-03-20 19:46:05 +0100401 boost::optional<std::string> module;
402 if (parserContext.m_curPath.m_nodes.back().m_prefix)
403 module = parserContext.m_curPath.m_nodes.back().m_prefix.value().m_name;
404 parserContext.m_errorMsg = "Expected " + leafDataTypeToString(schema.leafType(location, {module, leaf.m_name})) + " here:";
Václav Kubernát0b0272f2018-06-13 14:13:08 +0200405 return x3::error_handler_result::fail;
406 }
407 return x3::error_handler_result::rethrow;
408 }
Václav Kubernátebca2552018-06-08 19:06:02 +0200409};
410
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200411template<yang::LeafDataTypes TYPE>
Václav Kubernátebca2552018-06-08 19:06:02 +0200412struct leaf_data_base_class {
413 yang::LeafDataTypes m_type;
414
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200415 leaf_data_base_class()
416 : m_type(TYPE)
Václav Kubernátebca2552018-06-08 19:06:02 +0200417 {
418 }
419
420 template <typename T, typename Iterator, typename Context>
421 void on_success(Iterator const&, Iterator const&, T&, Context const& context)
422 {
423 auto& parserContext = x3::get<parser_context_tag>(context);
424 auto& schema = parserContext.m_schema;
Václav Kubernát744f57f2018-06-29 22:46:26 +0200425 boost::optional<std::string> module;
426 if (parserContext.m_curPath.m_nodes.back().m_prefix)
427 module = parserContext.m_curPath.m_nodes.back().m_prefix.value().m_name;
Václav Kubernátebca2552018-06-08 19:06:02 +0200428
Václav Kubernát744f57f2018-06-29 22:46:26 +0200429 leaf_ leaf = boost::get<leaf_>(parserContext.m_curPath.m_nodes.back().m_suffix);
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200430 schemaPath_ location = pathWithoutLastNode(parserContext.m_curPath);
Václav Kubernátebca2552018-06-08 19:06:02 +0200431
Václav Kubernát6a8d1d92019-04-24 20:30:36 +0200432 auto type = schema.leafType(location, {module, leaf.m_name});
433 if (type == yang::LeafDataTypes::LeafRef) {
434 type = schema.leafrefBase(location, {module, leaf.m_name});
435 }
436 if (type != m_type) {
Václav Kubernátebca2552018-06-08 19:06:02 +0200437 _pass(context) = false;
438 }
439 }
440};
441
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200442struct leaf_data_binary_data_class;
443
444struct leaf_data_enum_class : leaf_data_base_class<yang::LeafDataTypes::Enum> {
445 using leaf_data_base_class::leaf_data_base_class;
Václav Kubernátebca2552018-06-08 19:06:02 +0200446
447 template <typename T, typename Iterator, typename Context>
448 void on_success(Iterator const& start, Iterator const& end, T& ast, Context const& context)
449 {
450 leaf_data_base_class::on_success(start, end, ast, context);
Václav Kubernáteeb38842019-03-20 19:46:05 +0100451 // Base class on_success cannot return for us, so we check if it failed the parser.
452 if (_pass(context) == false)
453 return;
Václav Kubernátebca2552018-06-08 19:06:02 +0200454 auto& parserContext = x3::get<parser_context_tag>(context);
455 auto& schema = parserContext.m_schema;
Václav Kubernát744f57f2018-06-29 22:46:26 +0200456 boost::optional<std::string> module;
457 if (parserContext.m_curPath.m_nodes.back().m_prefix)
458 module = parserContext.m_curPath.m_nodes.back().m_prefix.value().m_name;
Václav Kubernátebca2552018-06-08 19:06:02 +0200459
Václav Kubernát744f57f2018-06-29 22:46:26 +0200460 leaf_ leaf = boost::get<leaf_>(parserContext.m_curPath.m_nodes.back().m_suffix);
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200461 schemaPath_ location = pathWithoutLastNode(parserContext.m_curPath);
Václav Kubernátebca2552018-06-08 19:06:02 +0200462
Václav Kubernát744f57f2018-06-29 22:46:26 +0200463 if (!schema.leafEnumHasValue(location, {module, leaf.m_name}, ast.m_value)) {
Václav Kubernátebca2552018-06-08 19:06:02 +0200464 _pass(context) = false;
465 }
466 }
467};
468
Václav Kubernáteeb38842019-03-20 19:46:05 +0100469struct leaf_data_identityRef_data_class;
470
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200471struct leaf_data_identityRef_class : leaf_data_base_class<yang::LeafDataTypes::IdentityRef> {
472 using leaf_data_base_class::leaf_data_base_class;
Václav Kubernáteeb38842019-03-20 19:46:05 +0100473
474 template <typename T, typename Iterator, typename Context>
475 void on_success(Iterator const& start, Iterator const& end, T& ast, Context const& context)
476 {
477 // FIXME: can I reuse leaf_data_enum_class somehow..?
478 leaf_data_base_class::on_success(start, end, ast, context);
479 // Base class on_success cannot return for us, so we check if it failed the parser.
480 if (_pass(context) == false)
481 return;
482 auto& parserContext = x3::get<parser_context_tag>(context);
483 auto& schema = parserContext.m_schema;
484 boost::optional<std::string> module;
485 if (parserContext.m_curPath.m_nodes.back().m_prefix)
486 module = parserContext.m_curPath.m_nodes.back().m_prefix.value().m_name;
487
488 leaf_ leaf = boost::get<leaf_>(parserContext.m_curPath.m_nodes.back().m_suffix);
489 schemaPath_ location = pathWithoutLastNode(parserContext.m_curPath);
490
491 ModuleValuePair pair;
492 if (ast.m_prefix) {
493 pair.first = ast.m_prefix.get().m_name;
494 }
495 pair.second = ast.m_value;
496
497 if (!schema.leafIdentityIsValid(location, {module, leaf.m_name}, pair)) {
498 _pass(context) = false;
499 }
500 }
501};
502
Václav Kubernát07204242018-06-04 18:12:09 +0200503struct set_class {
Václav Kubernát07204242018-06-04 18:12:09 +0200504 template <typename Iterator, typename Exception, typename Context>
505 x3::error_handler_result on_error(Iterator&, Iterator const&, Exception const& x, Context const& context)
506 {
507 auto& parserContext = x3::get<parser_context_tag>(context);
Václav Kubernát41378452018-06-06 16:29:40 +0200508 if (parserContext.m_errorMsg.empty())
509 parserContext.m_errorMsg = "Expected " + x.which() + " here:";
510 return x3::error_handler_result::rethrow;
Václav Kubernát07204242018-06-04 18:12:09 +0200511 }
512};
513
Václav Kubernát812ee282018-08-30 17:10:03 +0200514struct commit_class;
515
Václav Kubernát054cc992019-02-21 14:23:52 +0100516struct help_class;
517
Václav Kubernátb6ff0b62018-08-30 16:14:53 +0200518struct get_class;
519
Václav Kubernátb61336d2018-05-28 17:35:03 +0200520struct command_class {
521 template <typename Iterator, typename Exception, typename Context>
522 x3::error_handler_result on_error(Iterator&, Iterator const&, Exception const& x, Context const& context)
523 {
524 auto& parserContext = x3::get<parser_context_tag>(context);
525 auto& error_handler = x3::get<x3::error_handler_tag>(context).get();
Václav Kubernát41378452018-06-06 16:29:40 +0200526 if (parserContext.m_errorMsg.empty()) {
527 parserContext.m_errorMsg = "Unknown command.";
528 }
529 error_handler(x.where(), parserContext.m_errorMsg);
Václav Kubernátb61336d2018-05-28 17:35:03 +0200530 return x3::error_handler_result::fail;
531 }
532};
Václav Kubernát5c75b252018-10-10 18:33:47 +0200533
Václav Kubernát4108e0d2018-10-29 13:32:22 +0100534struct initializePath_class {
Václav Kubernát5c75b252018-10-10 18:33:47 +0200535 template <typename T, typename Iterator, typename Context>
536 void on_success(Iterator const&, Iterator const&, T&, Context const& context)
537 {
538 auto& parserContext = x3::get<parser_context_tag>(context);
539 parserContext.m_curPath = parserContext.m_curPathOrig;
540 parserContext.m_tmpListKeys.clear();
541 parserContext.m_tmpListName.clear();
Václav Kubernát4108e0d2018-10-29 13:32:22 +0100542 parserContext.m_suggestions.clear();
Václav Kubernát5c75b252018-10-10 18:33:47 +0200543 if (!parserContext.m_curPath.m_nodes.empty() && parserContext.m_curPath.m_nodes.at(0).m_prefix)
544 parserContext.m_topLevelModulePresent = true;
545 else
546 parserContext.m_topLevelModulePresent = false;
547 }
548};
Václav Kubernátd6fd2492018-11-19 15:11:16 +0100549
550struct trailingSlash_class;
Václav Kubernát4108e0d2018-10-29 13:32:22 +0100551
552struct createPathSuggestions_class {
553 template <typename T, typename Iterator, typename Context>
554 void on_success(Iterator const& begin, Iterator const&, T&, Context const& context)
555 {
556 auto& parserContext = x3::get<parser_context_tag>(context);
557 const auto& schema = parserContext.m_schema;
558
559 parserContext.m_completionIterator = begin;
Václav Kubernáte69133a2019-11-01 19:01:34 +0100560 auto suggestions = schema.childNodes(parserContext.m_curPath, Recursion::NonRecursive);
561 std::set<std::string> suffixesAdded;
562 std::transform(suggestions.begin(), suggestions.end(),
563 std::inserter(suffixesAdded, suffixesAdded.end()),
564 [&parserContext, &schema] (auto it) {
565 ModuleNodePair node;
566 if (auto colonPos = it.find(":"); colonPos != it.npos) {
567 node.first = it.substr(0, colonPos);
568 node.second = it.substr(colonPos + 1, node.second.npos);
569 } else {
570 node.first = boost::none;
571 node.second = it;
572 }
573
574 if (schema.isLeaf(parserContext.m_curPath, node)) {
575 return it + " ";
576 }
577 if (schema.isContainer(parserContext.m_curPath, node)) {
578 return it + "/";
579 }
580 if (schema.isList(parserContext.m_curPath, node)) {
581 return it + "[";
582 }
583 return it;
584 });
585 parserContext.m_suggestions = suffixesAdded;
Václav Kubernát4108e0d2018-10-29 13:32:22 +0100586 }
587};
Václav Kubernát329c6c32019-02-06 16:41:53 +0100588
589std::set<std::string> generateMissingKeyCompletionSet(std::set<std::string> keysNeeded, std::set<std::string> currentSet);
590
591struct createKeySuggestions_class {
592 template <typename T, typename Iterator, typename Context>
593 void on_success(Iterator const& begin, Iterator const&, T&, Context const& context)
594 {
595 auto& parserContext = x3::get<parser_context_tag>(context);
596 const auto& schema = parserContext.m_schema;
597
598 parserContext.m_completionIterator = begin;
599
600 const auto& keysNeeded = schema.listKeys(parserContext.m_curPath, {parserContext.m_curModule, parserContext.m_tmpListName});
601 parserContext.m_suggestions = generateMissingKeyCompletionSet(keysNeeded, parserContext.m_tmpListKeys);
602 }
603};
604
605struct suggestKeysEnd_class {
606 template <typename T, typename Iterator, typename Context>
607 void on_success(Iterator const& begin, Iterator const&, T&, Context const& context)
608 {
609 auto& parserContext = x3::get<parser_context_tag>(context);
610 const auto& schema = parserContext.m_schema;
611
612 parserContext.m_completionIterator = begin;
613 const auto& keysNeeded = schema.listKeys(parserContext.m_curPath, {parserContext.m_curModule, parserContext.m_tmpListName});
614 if (generateMissingKeyCompletionSet(keysNeeded, parserContext.m_tmpListKeys).empty()) {
615 parserContext.m_suggestions = {"]/"};
616 } else {
Václav Kubernát4c325482019-04-11 17:51:55 +0200617 parserContext.m_suggestions = {"]["};
Václav Kubernát329c6c32019-02-06 16:41:53 +0100618 }
619 }
620};
Václav Kubernát57272422019-02-08 12:48:24 +0100621
622struct commandNamesVisitor {
623 template <typename T>
624 auto operator()(boost::type<T>)
625 {
626 return T::name;
627 }
628};
629
630struct createCommandSuggestions_class {
631 template <typename T, typename Iterator, typename Context>
632 void on_success(Iterator const& begin, Iterator const&, T&, Context const& context)
633 {
634 auto& parserContext = x3::get<parser_context_tag>(context);
635 parserContext.m_completionIterator = begin;
636
637 parserContext.m_suggestions.clear();
638 boost::mpl::for_each<CommandTypes, boost::type<boost::mpl::_>>([&parserContext](auto cmd) {
639 parserContext.m_suggestions.emplace(commandNamesVisitor()(cmd));
640 });
641 }
642};
Václav Kubernátac035d62019-02-18 10:59:08 +0100643
644struct completing_class {
645 template <typename T, typename Iterator, typename Context>
646 void on_success(Iterator const&, Iterator const&, T&, Context const& context)
647 {
648 auto& parserContext = x3::get<parser_context_tag>(context);
649
650 if (!parserContext.m_completing)
651 _pass(context) = false;
652 }
653};
Václav Kubernát989b5de2019-02-20 16:28:35 +0100654
655struct createEnumSuggestions_class {
656 template <typename T, typename Iterator, typename Context>
657 void on_success(Iterator const& begin, Iterator const&, T&, Context const& context)
658 {
659 auto& parserContext = x3::get<parser_context_tag>(context);
660 parserContext.m_completionIterator = begin;
661 const Schema& schema = parserContext.m_schema;
662
663 boost::optional<std::string> module;
664 if (parserContext.m_curPath.m_nodes.back().m_prefix)
665 module = parserContext.m_curPath.m_nodes.back().m_prefix.value().m_name;
666
667 leaf_ leaf = boost::get<leaf_>(parserContext.m_curPath.m_nodes.back().m_suffix);
668 schemaPath_ location = pathWithoutLastNode(parserContext.m_curPath);
669
Václav Kubernáteeb38842019-03-20 19:46:05 +0100670 // Only generate completions if the type is enum so that we don't
671 // overwrite some other completions.
672 if (schema.leafType(location, {module, leaf.m_name}) == yang::LeafDataTypes::Enum)
673 parserContext.m_suggestions = schema.enumValues(location, {module, leaf.m_name});
674 }
675};
676
677// FIXME: can I reuse createEnumSuggestions?
678struct createIdentitySuggestions_class {
679 template <typename T, typename Iterator, typename Context>
680 void on_success(Iterator const& begin, Iterator const&, T&, Context const& context)
681 {
682 auto& parserContext = x3::get<parser_context_tag>(context);
683 parserContext.m_completionIterator = begin;
684 const Schema& schema = parserContext.m_schema;
685
686 boost::optional<std::string> module;
687 if (parserContext.m_curPath.m_nodes.back().m_prefix)
688 module = parserContext.m_curPath.m_nodes.back().m_prefix.value().m_name;
689
690 leaf_ leaf = boost::get<leaf_>(parserContext.m_curPath.m_nodes.back().m_suffix);
691 schemaPath_ location = pathWithoutLastNode(parserContext.m_curPath);
692
693 // Only generate completions if the type is identityref so that we
694 // don't overwrite some other completions.
695 if (schema.leafType(location, {module, leaf.m_name}) == yang::LeafDataTypes::IdentityRef)
696 parserContext.m_suggestions = schema.validIdentities(location, {module, leaf.m_name}, Prefixes::WhenNeeded);
Václav Kubernát989b5de2019-02-20 16:28:35 +0100697 }
698};