blob: 4e3b97c0be68a0c7740ad50acb7b00cd63075ae7 [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
10
Václav Kubernát509ce652019-05-29 19:46:44 +020011#include <boost/spirit/home/x3.hpp>
Václav Kubernát24df80e2018-06-06 15:18:03 +020012#include "ast_commands.hpp"
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020013#include "ast_handlers.hpp"
Václav Kubernát9ae8cc42020-03-25 19:17:41 +010014#include "common_parsers.hpp"
15#include "leaf_data.hpp"
Václav Kubernátd0ea9b22020-04-24 00:44:15 +020016#include "path_parser.hpp"
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020017
Václav Kubernát7ff22072022-04-05 10:22:40 +020018/**
19 * Returns a parser that generates suggestions based on a Completion set.
20 * Usage:
21 * const auto suggestSomeStuff = staticSuggestions({"some", "stuff", "yay"});
22 *
23 * You can use this as a standard parser in a Spirit grammar.
24 */
25auto staticSuggestions(const std::initializer_list<std::string>& strings)
26{
27 std::set<Completion> completions;
28 std::transform(begin(strings), end(strings), std::inserter(completions, completions.end()),
29 [](const auto s) { return Completion{s, " "}; });
30 return as<x3::unused_type>[x3::eps[([completions](auto& ctx) {
31 auto& parserContext = x3::get<parser_context_tag>(ctx);
32 parserContext.m_suggestions = completions;
33 parserContext.m_completionIterator = _where(ctx).begin();
34 })]];
35}
36
Václav Kubernát68c89422021-10-20 15:32:20 +020037#if BOOST_VERSION <= 107700
Václav Kubernátf5f6d242022-04-07 01:06:19 +020038namespace boost::spirit::x3::traits {
Václav Kubernát68c89422021-10-20 15:32:20 +020039 // Backport https://github.com/boostorg/spirit/pull/702
40 // with instructions from https://github.com/boostorg/spirit/issues/701#issuecomment-946743099
41 template <typename... Types, typename T>
42 struct variant_find_substitute<boost::variant<Types...>, T>
43 {
44 using variant_type = boost::variant<Types...>;
45
46 typedef typename variant_type::types types;
47 typedef typename mpl::end<types>::type end;
48
49 typedef typename mpl::find<types, T>::type iter_1;
50
51 typedef typename
52 mpl::eval_if<
53 is_same<iter_1, end>,
54 mpl::find_if<types, traits::is_substitute<T, mpl::_1> >,
55 mpl::identity<iter_1>
56 >::type
57 iter;
58
59 typedef typename
60 mpl::eval_if<
61 is_same<iter, end>,
62 mpl::identity<T>,
63 mpl::deref<iter>
64 >::type
65 type;
66 };
67}
68#endif
Václav Kubernát60d6f292018-05-25 09:45:32 +020069
Václav Kubernát6d791432018-10-25 16:00:35 +020070x3::rule<discard_class, discard_> const discard = "discard";
Václav Kubernát11afac72018-07-18 14:59:53 +020071x3::rule<ls_class, ls_> const ls = "ls";
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020072x3::rule<cd_class, cd_> const cd = "cd";
Václav Kubernát07204242018-06-04 18:12:09 +020073x3::rule<set_class, set_> const set = "set";
Václav Kubernátb6ff0b62018-08-30 16:14:53 +020074x3::rule<get_class, get_> const get = "get";
Václav Kubernátb61336d2018-05-28 17:35:03 +020075x3::rule<create_class, create_> const create = "create";
76x3::rule<delete_class, delete_> const delete_rule = "delete_rule";
Václav Kubernát812ee282018-08-30 17:10:03 +020077x3::rule<commit_class, commit_> const commit = "commit";
Václav Kubernát9cfcd872020-02-18 12:34:02 +010078x3::rule<describe_class, describe_> const describe = "describe";
Václav Kubernát054cc992019-02-21 14:23:52 +010079x3::rule<help_class, help_> const help = "help";
Václav Kubernát7160a132020-04-03 02:11:01 +020080x3::rule<copy_class, copy_> const copy = "copy";
Václav Kubernátbf65dd72020-05-28 02:32:31 +020081x3::rule<move_class, move_> const move = "move";
Václav Kubernát70d7f7a2020-06-23 14:40:40 +020082x3::rule<dump_class, dump_> const dump = "dump";
Václav Kubernátaa4250a2020-07-22 00:02:23 +020083x3::rule<prepare_class, prepare_> const prepare = "prepare";
Václav Kubernáte7248b22020-06-26 15:38:59 +020084x3::rule<exec_class, exec_> const exec = "exec";
Václav Kubernát1d593902021-03-08 08:55:16 +010085x3::rule<switch_class, switch_> const switch_rule = "switch";
Václav Kubernáte7248b22020-06-26 15:38:59 +020086x3::rule<cancel_class, cancel_> const cancel = "cancel";
Václav Kubernátb61336d2018-05-28 17:35:03 +020087x3::rule<command_class, command_> const command = "command";
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020088
Václav Kubernát57272422019-02-08 12:48:24 +010089x3::rule<createCommandSuggestions_class, x3::unused_type> const createCommandSuggestions = "createCommandSuggestions";
Václav Kubernát5c75b252018-10-10 18:33:47 +020090
Václav Kubernát41378452018-06-06 16:29:40 +020091#if __clang__
92#pragma GCC diagnostic push
93#pragma GCC diagnostic ignored "-Woverloaded-shift-op-parentheses"
94#endif
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020095
Václav Kubernát509ce652019-05-29 19:46:44 +020096namespace ascii = boost::spirit::x3::ascii;
97
Václav Kubernáte7d4aea2018-09-11 18:15:48 +020098struct ls_options_table : x3::symbols<LsOption> {
99 ls_options_table()
100 {
Václav Kubernátbf083ec2019-02-19 13:58:09 +0100101 add
102 ("--recursive", LsOption::Recursive);
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200103 }
104} const ls_options;
105
Václav Kubernát11afac72018-07-18 14:59:53 +0200106auto const ls_def =
Václav Kubernát4a58ce62020-05-14 17:58:10 +0200107 ls_::name >> *(space_separator >> ls_options) >> -(space_separator >> (anyPath | (module >> "*")));
Václav Kubernát11afac72018-07-18 14:59:53 +0200108
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200109auto const cd_def =
Václav Kubernáte7248b22020-06-26 15:38:59 +0200110 cd_::name >> space_separator > cdPath;
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200111
Václav Kubernát38175022021-11-04 14:58:57 +0100112#if BOOST_VERSION <= 107700
113auto const create_def =
114 create_::name >> space_separator >
115 (x3::eps >> presenceContainerPath |
116 x3::eps >> listInstancePath |
117 x3::eps >> leafListElementPath);
118
119auto const delete_rule_def =
120 delete_::name >> space_separator >
121 (x3::eps >> presenceContainerPath |
122 x3::eps >> listInstancePath |
123 x3::eps >> leafListElementPath |
124 x3::eps >> writableLeafPath);
125#else
Václav Kubernátb61336d2018-05-28 17:35:03 +0200126auto const create_def =
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200127 create_::name >> space_separator > (presenceContainerPath | listInstancePath | leafListElementPath);
Václav Kubernátb61336d2018-05-28 17:35:03 +0200128
129auto const delete_rule_def =
Jan Kundrátb7206ad2020-06-18 21:08:14 +0200130 delete_::name >> space_separator > (presenceContainerPath | listInstancePath | leafListElementPath | writableLeafPath);
Václav Kubernát38175022021-11-04 14:58:57 +0100131#endif
Václav Kubernát07204242018-06-04 18:12:09 +0200132
Václav Kubernáte066fe22022-04-06 00:32:26 +0200133const auto dsTargetSuggestions = staticSuggestions({"running", "startup", "operational"});
134
135struct ds_target_table : x3::symbols<DatastoreTarget> {
136 ds_target_table()
137 {
138 add
139 ("operational", DatastoreTarget::Operational)
140 ("startup", DatastoreTarget::Startup)
141 ("running", DatastoreTarget::Running);
142 }
143} const ds_target_table;
144
Václav Kubernátb6ff0b62018-08-30 16:14:53 +0200145auto const get_def =
Václav Kubernáte066fe22022-04-06 00:32:26 +0200146 get_::name
147 >> -(space_separator >> "-" > staticSuggestions({"-datastore"}) > "-datastore" > space_separator > dsTargetSuggestions > ds_target_table)
148 >> -(space_separator >> getPath);
Václav Kubernátb6ff0b62018-08-30 16:14:53 +0200149
Václav Kubernát07204242018-06-04 18:12:09 +0200150auto const set_def =
Václav Kubernátabf52802020-05-19 01:31:17 +0200151 set_::name >> space_separator > writableLeafPath > space_separator > leaf_data;
Václav Kubernátb61336d2018-05-28 17:35:03 +0200152
Václav Kubernát812ee282018-08-30 17:10:03 +0200153auto const commit_def =
Václav Kubernátbf083ec2019-02-19 13:58:09 +0100154 commit_::name >> x3::attr(commit_());
Václav Kubernát812ee282018-08-30 17:10:03 +0200155
Václav Kubernát6d791432018-10-25 16:00:35 +0200156auto const discard_def =
Václav Kubernátbf083ec2019-02-19 13:58:09 +0100157 discard_::name >> x3::attr(discard_());
Václav Kubernát6d791432018-10-25 16:00:35 +0200158
Václav Kubernát054cc992019-02-21 14:23:52 +0100159struct command_names_table : x3::symbols<decltype(help_::m_cmd)> {
160 command_names_table()
161 {
162 boost::mpl::for_each<CommandTypes, boost::type<boost::mpl::_>>([this](auto cmd) {
Václav Kubernát672889c2020-05-27 04:11:36 +0200163 add(commandNamesVisitor(cmd), decltype(help_::m_cmd)(cmd));
Václav Kubernát054cc992019-02-21 14:23:52 +0100164 });
165 }
166} const command_names;
167
168auto const help_def =
169 help_::name > createCommandSuggestions >> -command_names;
170
Václav Kubernát7160a132020-04-03 02:11:01 +0200171struct datastore_symbol_table : x3::symbols<Datastore> {
172 datastore_symbol_table()
173 {
174 add
175 ("running", Datastore::Running)
176 ("startup", Datastore::Startup);
177 }
178} const datastore;
179
Václav Kubernát21fc5e22020-11-26 04:28:27 +0100180const auto copy_source = x3::rule<class source, Datastore>{"source datastore"} = datastore;
181const auto copy_destination = x3::rule<class source, Datastore>{"destination datastore"} = datastore;
Václav Kubernát7160a132020-04-03 02:11:01 +0200182
Václav Kubernát7ff22072022-04-05 10:22:40 +0200183const auto datastoreSuggestions = staticSuggestions({"running", "startup"});
Václav Kubernát7160a132020-04-03 02:11:01 +0200184
185struct copy_args : x3::parser<copy_args> {
186 using attribute_type = copy_;
187 template <typename It, typename Ctx, typename RCtx>
188 bool parse(It& begin, It end, Ctx const& ctx, RCtx& rctx, copy_& attr) const
189 {
190 auto& parserContext = x3::get<parser_context_tag>(ctx);
191 auto iterBeforeDestination = begin;
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100192 auto save_iter = x3::no_skip[x3::eps[([&iterBeforeDestination](auto& ctx) { iterBeforeDestination = _where(ctx).begin(); })]];
Václav Kubernát7160a132020-04-03 02:11:01 +0200193 auto grammar = datastoreSuggestions > copy_source > space_separator > datastoreSuggestions > save_iter > copy_destination;
194
195 try {
196 grammar.parse(begin, end, ctx, rctx, attr);
197 } catch (x3::expectation_failure<It>& ex) {
198 using namespace std::string_literals;
199 parserContext.m_errorMsg = "Expected "s + ex.which() + " here:";
200 throw;
201 }
202
203 if (attr.m_source == attr.m_destination) {
204 begin = iterBeforeDestination; // Restoring the iterator here makes the error caret point to the second datastore
205 parserContext.m_errorMsg = "Source datastore and destination datastore can't be the same.";
206 return false;
207 }
208
209 return true;
210 }
Václav Kubernát21fc5e22020-11-26 04:28:27 +0100211} const copy_args;
Václav Kubernát7160a132020-04-03 02:11:01 +0200212
213auto const copy_def =
214 copy_::name > space_separator > copy_args;
215
Václav Kubernát9cfcd872020-02-18 12:34:02 +0100216auto const describe_def =
Václav Kubernát3200b862020-12-03 02:34:55 +0100217 describe_::name >> space_separator > anyPath;
Václav Kubernát9cfcd872020-02-18 12:34:02 +0100218
Václav Kubernát162165e2021-02-22 09:46:53 +0100219struct move_mode_table : x3::symbols<MoveMode> {
220 move_mode_table()
Václav Kubernátbf65dd72020-05-28 02:32:31 +0200221 {
222 add
223 ("after", MoveMode::After)
224 ("before", MoveMode::Before)
225 ("begin", MoveMode::Begin)
226 ("end", MoveMode::End);
227 }
Václav Kubernát162165e2021-02-22 09:46:53 +0100228} const move_mode_table;
Václav Kubernátbf65dd72020-05-28 02:32:31 +0200229
230struct move_absolute_table : x3::symbols<yang::move::Absolute> {
231 move_absolute_table()
232 {
233 add
234 ("begin", yang::move::Absolute::Begin)
235 ("end", yang::move::Absolute::End);
236 }
237} const move_absolute_table;
238
239struct move_relative_table : x3::symbols<yang::move::Relative::Position> {
240 move_relative_table()
241 {
242 add
243 ("before", yang::move::Relative::Position::Before)
244 ("after", yang::move::Relative::Position::After);
245 }
246} const move_relative_table;
247
248struct move_args : x3::parser<move_args> {
249 using attribute_type = move_;
250 template <typename It, typename Ctx, typename RCtx>
251 bool parse(It& begin, It end, Ctx const& ctx, RCtx& rctx, move_& attr) const
252 {
253 ParserContext& parserContext = x3::get<parser_context_tag>(ctx);
254 dataPath_ movePath;
Václav Kubernát38175022021-11-04 14:58:57 +0100255#if BOOST_VERSION <= 107700
256 auto movePathGrammar = x3::eps >> listInstancePath | x3::eps >> leafListElementPath;
257#else
Václav Kubernátbf65dd72020-05-28 02:32:31 +0200258 auto movePathGrammar = listInstancePath | leafListElementPath;
Václav Kubernát38175022021-11-04 14:58:57 +0100259#endif
Václav Kubernátbf65dd72020-05-28 02:32:31 +0200260 auto res = movePathGrammar.parse(begin, end, ctx, rctx, attr.m_source);
261 if (!res) {
262 parserContext.m_errorMsg = "Expected source path here:";
263 return false;
264 }
265
266 // Try absolute move first.
267 res = (space_separator >> move_absolute_table).parse(begin, end, ctx, rctx, attr.m_destination);
268 if (res) {
269 // Absolute move parsing succeeded, we don't need to parse anything else.
270 return true;
271 }
272
273 // If absolute move didn't succeed, try relative.
274 attr.m_destination = yang::move::Relative{};
275 res = (space_separator >> move_relative_table).parse(begin, end, ctx, rctx, std::get<yang::move::Relative>(attr.m_destination).m_position);
276
277 if (!res) {
278 parserContext.m_errorMsg = "Expected a move position (begin, end, before, after) here:";
279 return false;
280 }
281
282 if (std::holds_alternative<leafListElement_>(attr.m_source.m_nodes.back().m_suffix)) {
283 leaf_data_ value;
284 res = (space_separator >> leaf_data).parse(begin, end, ctx, rctx, value);
285 if (res) {
286 std::get<yang::move::Relative>(attr.m_destination).m_path = {{".", value}};
287 }
288 } else {
289 ListInstance listInstance;
290 // The source list instance will be stored inside the parser context path.
291 // The source list instance will be full data path (with keys included).
292 // However, m_tmpListPath is supposed to store a path with a list without the keys.
293 // So, I pop the last listElement_ (which has the keys) and put in a list_ (which doesn't have the keys).
294 // Example: /mod:cont/protocols[name='ftp'] gets turned into /mod:cont/protocols
295 parserContext.m_tmpListPath = parserContext.currentDataPath();
296 parserContext.m_tmpListPath.m_nodes.pop_back();
297 auto list = list_{std::get<listElement_>(attr.m_source.m_nodes.back().m_suffix).m_name};
Václav Kubernátfaacd022020-07-08 16:44:38 +0200298 parserContext.m_tmpListPath.m_nodes.emplace_back(attr.m_source.m_nodes.back().m_prefix, list);
Václav Kubernátbf65dd72020-05-28 02:32:31 +0200299
300 res = (space_separator >> listSuffix).parse(begin, end, ctx, rctx, listInstance);
301 if (res) {
302 std::get<yang::move::Relative>(attr.m_destination).m_path = listInstance;
303 }
304 }
305
306 if (!res) {
307 parserContext.m_errorMsg = "Expected a destination here:";
308 }
309
310 return res;
311 }
312} const move_args;
313
314auto const move_def =
315 move_::name >> space_separator >> move_args;
316
Václav Kubernát70d7f7a2020-06-23 14:40:40 +0200317struct format_table : x3::symbols<DataFormat> {
318 format_table()
319 {
320 add
321 ("xml", DataFormat::Xml)
322 ("json", DataFormat::Json);
323 }
324} const format_table;
325
Václav Kubernát3b5e6522020-06-26 18:36:09 +0200326struct dump_args : x3::parser<dump_args> {
327 using attribute_type = dump_;
328 template <typename It, typename Ctx, typename RCtx>
329 bool parse(It& begin, It end, Ctx const& ctx, RCtx& rctx, dump_& attr) const
330 {
331 ParserContext& parserContext = x3::get<parser_context_tag>(ctx);
332 parserContext.m_suggestions = {{"xml"}, {"json"}};
333 parserContext.m_completionIterator = begin;
334 auto res = format_table.parse(begin, end, ctx, rctx, attr);
335 if (!res) {
336 parserContext.m_errorMsg = "Expected a data format (xml, json) here:";
337 }
338 return res;
339 }
340} const dump_args;
Václav Kubernát70d7f7a2020-06-23 14:40:40 +0200341
Václav Kubernátaa4250a2020-07-22 00:02:23 +0200342auto const prepare_def =
Václav Kubernátd8408e02020-12-02 05:13:27 +0100343 prepare_::name > space_separator > as<dataPath_>[RpcActionPath<AllowInput::Yes>{}];
Václav Kubernáte7248b22020-06-26 15:38:59 +0200344
345auto const exec_def =
Václav Kubernátaff1f492021-02-17 10:56:28 +0100346 exec_::name > -(space_separator > -as<dataPath_>[RpcActionPath<AllowInput::No>{}]);
Václav Kubernáte7248b22020-06-26 15:38:59 +0200347
Václav Kubernát162165e2021-02-22 09:46:53 +0100348auto const switch_rule_def =
Václav Kubernát7ff22072022-04-05 10:22:40 +0200349 switch_::name > space_separator > dsTargetSuggestions > ds_target_table;
Václav Kubernát162165e2021-02-22 09:46:53 +0100350
Václav Kubernáte7248b22020-06-26 15:38:59 +0200351auto const cancel_def =
352 cancel_::name >> x3::attr(cancel_{});
353
Václav Kubernát70d7f7a2020-06-23 14:40:40 +0200354auto const dump_def =
Václav Kubernát3b5e6522020-06-26 18:36:09 +0200355 dump_::name > space_separator >> dump_args;
Václav Kubernát70d7f7a2020-06-23 14:40:40 +0200356
Václav Kubernát57272422019-02-08 12:48:24 +0100357auto const createCommandSuggestions_def =
Václav Kubernátbf083ec2019-02-19 13:58:09 +0100358 x3::eps;
Václav Kubernát57272422019-02-08 12:48:24 +0100359
Václav Kubernátb61336d2018-05-28 17:35:03 +0200360auto const command_def =
Václav Kubernát5b8e2822022-01-14 03:19:54 +0100361#if BOOST_VERSION <= 107800
Václav Kubernátf9533c22021-11-04 15:13:30 +0100362 x3::eps >>
363#endif
Václav Kubernát162165e2021-02-22 09:46:53 +0100364 createCommandSuggestions >> x3::expect[cd | copy | create | delete_rule | set | commit | get | ls | discard | describe | help | move | dump | prepare | exec | cancel | switch_rule];
Václav Kubernát41378452018-06-06 16:29:40 +0200365
366#if __clang__
367#pragma GCC diagnostic pop
368#endif
Václav Kubernátb61336d2018-05-28 17:35:03 +0200369
Václav Kubernát07204242018-06-04 18:12:09 +0200370BOOST_SPIRIT_DEFINE(set)
Václav Kubernát812ee282018-08-30 17:10:03 +0200371BOOST_SPIRIT_DEFINE(commit)
Václav Kubernátb6ff0b62018-08-30 16:14:53 +0200372BOOST_SPIRIT_DEFINE(get)
Václav Kubernát11afac72018-07-18 14:59:53 +0200373BOOST_SPIRIT_DEFINE(ls)
Václav Kubernát6d791432018-10-25 16:00:35 +0200374BOOST_SPIRIT_DEFINE(discard)
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200375BOOST_SPIRIT_DEFINE(cd)
Václav Kubernátb61336d2018-05-28 17:35:03 +0200376BOOST_SPIRIT_DEFINE(create)
377BOOST_SPIRIT_DEFINE(delete_rule)
Václav Kubernát9cfcd872020-02-18 12:34:02 +0100378BOOST_SPIRIT_DEFINE(describe)
Václav Kubernát054cc992019-02-21 14:23:52 +0100379BOOST_SPIRIT_DEFINE(help)
Václav Kubernát7160a132020-04-03 02:11:01 +0200380BOOST_SPIRIT_DEFINE(copy)
Václav Kubernátbf65dd72020-05-28 02:32:31 +0200381BOOST_SPIRIT_DEFINE(move)
Václav Kubernát70d7f7a2020-06-23 14:40:40 +0200382BOOST_SPIRIT_DEFINE(dump)
Václav Kubernátaa4250a2020-07-22 00:02:23 +0200383BOOST_SPIRIT_DEFINE(prepare)
Václav Kubernáte7248b22020-06-26 15:38:59 +0200384BOOST_SPIRIT_DEFINE(exec)
Václav Kubernát162165e2021-02-22 09:46:53 +0100385BOOST_SPIRIT_DEFINE(switch_rule)
Václav Kubernáte7248b22020-06-26 15:38:59 +0200386BOOST_SPIRIT_DEFINE(cancel)
Václav Kubernátb61336d2018-05-28 17:35:03 +0200387BOOST_SPIRIT_DEFINE(command)
Václav Kubernát57272422019-02-08 12:48:24 +0100388BOOST_SPIRIT_DEFINE(createCommandSuggestions)