blob: d0d2d0c2bfaf8d26b2f94e2aa8542949ac277fbd [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át5c75b252018-10-10 18:33:47 +020070
Václav Kubernát41378452018-06-06 16:29:40 +020071#if __clang__
72#pragma GCC diagnostic push
73#pragma GCC diagnostic ignored "-Woverloaded-shift-op-parentheses"
74#endif
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020075
Václav Kubernát509ce652019-05-29 19:46:44 +020076namespace ascii = boost::spirit::x3::ascii;
77
Václav Kubernáte7d4aea2018-09-11 18:15:48 +020078struct ls_options_table : x3::symbols<LsOption> {
79 ls_options_table()
80 {
Václav Kubernátbf083ec2019-02-19 13:58:09 +010081 add
82 ("--recursive", LsOption::Recursive);
Václav Kubernáte7d4aea2018-09-11 18:15:48 +020083 }
84} const ls_options;
85
Václav Kubernát52b90222022-04-27 11:29:54 +020086auto const ls = x3::rule<ls_class, ls_>{"ls"} =
Václav Kubernát76bb8c22022-01-19 01:32:31 +010087 ls_::name >> *(space_separator >> ls_options) >> -(space_separator > (anyPath | (module >> "*")));
Václav Kubernát11afac72018-07-18 14:59:53 +020088
Václav Kubernát52b90222022-04-27 11:29:54 +020089auto const cd = x3::rule<cd_class, cd_>{"cd"} =
Václav Kubernáte7248b22020-06-26 15:38:59 +020090 cd_::name >> space_separator > cdPath;
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020091
Václav Kubernát38175022021-11-04 14:58:57 +010092#if BOOST_VERSION <= 107700
Václav Kubernát52b90222022-04-27 11:29:54 +020093auto const create = x3::rule<create_class, create_>{"create"} =
Václav Kubernát38175022021-11-04 14:58:57 +010094 create_::name >> space_separator >
95 (x3::eps >> presenceContainerPath |
96 x3::eps >> listInstancePath |
97 x3::eps >> leafListElementPath);
98
Václav Kubernát52b90222022-04-27 11:29:54 +020099auto const delete_rule = x3::rule<delete_class, delete_>{"delete_rule"} =
Václav Kubernát38175022021-11-04 14:58:57 +0100100 delete_::name >> space_separator >
101 (x3::eps >> presenceContainerPath |
102 x3::eps >> listInstancePath |
103 x3::eps >> leafListElementPath |
104 x3::eps >> writableLeafPath);
105#else
Václav Kubernát52b90222022-04-27 11:29:54 +0200106auto const create = x3::rule<create_class, create_>{"create"} =
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200107 create_::name >> space_separator > (presenceContainerPath | listInstancePath | leafListElementPath);
Václav Kubernátb61336d2018-05-28 17:35:03 +0200108
Václav Kubernát52b90222022-04-27 11:29:54 +0200109auto const delete_rule = x3::rule<delete_class, delete_>{"delete_rule"} =
Jan Kundrátb7206ad2020-06-18 21:08:14 +0200110 delete_::name >> space_separator > (presenceContainerPath | listInstancePath | leafListElementPath | writableLeafPath);
Václav Kubernát38175022021-11-04 14:58:57 +0100111#endif
Václav Kubernát07204242018-06-04 18:12:09 +0200112
Václav Kubernáte066fe22022-04-06 00:32:26 +0200113const auto dsTargetSuggestions = staticSuggestions({"running", "startup", "operational"});
114
115struct ds_target_table : x3::symbols<DatastoreTarget> {
116 ds_target_table()
117 {
118 add
119 ("operational", DatastoreTarget::Operational)
120 ("startup", DatastoreTarget::Startup)
121 ("running", DatastoreTarget::Running);
122 }
123} const ds_target_table;
124
Václav Kubernát52b90222022-04-27 11:29:54 +0200125auto const get = x3::rule<get_class, get_>{"get"} =
Václav Kubernáte066fe22022-04-06 00:32:26 +0200126 get_::name
127 >> -(space_separator >> "-" > staticSuggestions({"-datastore"}) > "-datastore" > space_separator > dsTargetSuggestions > ds_target_table)
Václav Kubernát76bb8c22022-01-19 01:32:31 +0100128 >> -(space_separator > getPath);
Václav Kubernátb6ff0b62018-08-30 16:14:53 +0200129
Václav Kubernát52b90222022-04-27 11:29:54 +0200130auto const set = x3::rule<set_class, set_>{"set"} =
Václav Kubernátabf52802020-05-19 01:31:17 +0200131 set_::name >> space_separator > writableLeafPath > space_separator > leaf_data;
Václav Kubernátb61336d2018-05-28 17:35:03 +0200132
Václav Kubernát52b90222022-04-27 11:29:54 +0200133auto const commit = x3::rule<commit_class, commit_>{"commit"} =
Václav Kubernátbf083ec2019-02-19 13:58:09 +0100134 commit_::name >> x3::attr(commit_());
Václav Kubernát812ee282018-08-30 17:10:03 +0200135
Václav Kubernát52b90222022-04-27 11:29:54 +0200136auto const discard = x3::rule<discard_class, discard_>{"discard"} =
Václav Kubernátbf083ec2019-02-19 13:58:09 +0100137 discard_::name >> x3::attr(discard_());
Václav Kubernát6d791432018-10-25 16:00:35 +0200138
Václav Kubernát054cc992019-02-21 14:23:52 +0100139struct command_names_table : x3::symbols<decltype(help_::m_cmd)> {
140 command_names_table()
141 {
142 boost::mpl::for_each<CommandTypes, boost::type<boost::mpl::_>>([this](auto cmd) {
Václav Kubernát672889c2020-05-27 04:11:36 +0200143 add(commandNamesVisitor(cmd), decltype(help_::m_cmd)(cmd));
Václav Kubernát054cc992019-02-21 14:23:52 +0100144 });
145 }
146} const command_names;
147
Václav Kubernát52b90222022-04-27 11:29:54 +0200148auto const createCommandSuggestions = x3::rule<createCommandSuggestions_class, x3::unused_type>{"createCommandSuggestions"} =
149 x3::eps;
150
151auto const help = x3::rule<help_class, help_>{"help"} =
Václav Kubernát054cc992019-02-21 14:23:52 +0100152 help_::name > createCommandSuggestions >> -command_names;
153
Václav Kubernát7160a132020-04-03 02:11:01 +0200154struct datastore_symbol_table : x3::symbols<Datastore> {
155 datastore_symbol_table()
156 {
157 add
158 ("running", Datastore::Running)
159 ("startup", Datastore::Startup);
160 }
161} const datastore;
162
Václav Kubernát21fc5e22020-11-26 04:28:27 +0100163const auto copy_source = x3::rule<class source, Datastore>{"source datastore"} = datastore;
164const auto copy_destination = x3::rule<class source, Datastore>{"destination datastore"} = datastore;
Václav Kubernát7160a132020-04-03 02:11:01 +0200165
Václav Kubernát7ff22072022-04-05 10:22:40 +0200166const auto datastoreSuggestions = staticSuggestions({"running", "startup"});
Václav Kubernát7160a132020-04-03 02:11:01 +0200167
168struct copy_args : x3::parser<copy_args> {
169 using attribute_type = copy_;
170 template <typename It, typename Ctx, typename RCtx>
171 bool parse(It& begin, It end, Ctx const& ctx, RCtx& rctx, copy_& attr) const
172 {
173 auto& parserContext = x3::get<parser_context_tag>(ctx);
174 auto iterBeforeDestination = begin;
Václav Kubernát76bb8c22022-01-19 01:32:31 +0100175 auto save_iter = x3::eps[([&iterBeforeDestination](auto& ctx) { iterBeforeDestination = _where(ctx).begin(); })];
Václav Kubernát7160a132020-04-03 02:11:01 +0200176 auto grammar = datastoreSuggestions > copy_source > space_separator > datastoreSuggestions > save_iter > copy_destination;
177
178 try {
179 grammar.parse(begin, end, ctx, rctx, attr);
180 } catch (x3::expectation_failure<It>& ex) {
181 using namespace std::string_literals;
182 parserContext.m_errorMsg = "Expected "s + ex.which() + " here:";
183 throw;
184 }
185
186 if (attr.m_source == attr.m_destination) {
187 begin = iterBeforeDestination; // Restoring the iterator here makes the error caret point to the second datastore
188 parserContext.m_errorMsg = "Source datastore and destination datastore can't be the same.";
189 return false;
190 }
191
192 return true;
193 }
Václav Kubernát21fc5e22020-11-26 04:28:27 +0100194} const copy_args;
Václav Kubernát7160a132020-04-03 02:11:01 +0200195
Václav Kubernát52b90222022-04-27 11:29:54 +0200196auto const copy = x3::rule<copy_class, copy_>{"copy"} =
Václav Kubernát7160a132020-04-03 02:11:01 +0200197 copy_::name > space_separator > copy_args;
198
Václav Kubernát52b90222022-04-27 11:29:54 +0200199auto const describe = x3::rule<describe_class, describe_>{"describe"} =
Václav Kubernát3200b862020-12-03 02:34:55 +0100200 describe_::name >> space_separator > anyPath;
Václav Kubernát9cfcd872020-02-18 12:34:02 +0100201
Václav Kubernát162165e2021-02-22 09:46:53 +0100202struct move_mode_table : x3::symbols<MoveMode> {
203 move_mode_table()
Václav Kubernátbf65dd72020-05-28 02:32:31 +0200204 {
205 add
206 ("after", MoveMode::After)
207 ("before", MoveMode::Before)
208 ("begin", MoveMode::Begin)
209 ("end", MoveMode::End);
210 }
Václav Kubernát162165e2021-02-22 09:46:53 +0100211} const move_mode_table;
Václav Kubernátbf65dd72020-05-28 02:32:31 +0200212
213struct move_absolute_table : x3::symbols<yang::move::Absolute> {
214 move_absolute_table()
215 {
216 add
217 ("begin", yang::move::Absolute::Begin)
218 ("end", yang::move::Absolute::End);
219 }
220} const move_absolute_table;
221
222struct move_relative_table : x3::symbols<yang::move::Relative::Position> {
223 move_relative_table()
224 {
225 add
226 ("before", yang::move::Relative::Position::Before)
227 ("after", yang::move::Relative::Position::After);
228 }
229} const move_relative_table;
230
231struct move_args : x3::parser<move_args> {
232 using attribute_type = move_;
233 template <typename It, typename Ctx, typename RCtx>
234 bool parse(It& begin, It end, Ctx const& ctx, RCtx& rctx, move_& attr) const
235 {
236 ParserContext& parserContext = x3::get<parser_context_tag>(ctx);
237 dataPath_ movePath;
Václav Kubernát38175022021-11-04 14:58:57 +0100238#if BOOST_VERSION <= 107700
239 auto movePathGrammar = x3::eps >> listInstancePath | x3::eps >> leafListElementPath;
240#else
Václav Kubernátbf65dd72020-05-28 02:32:31 +0200241 auto movePathGrammar = listInstancePath | leafListElementPath;
Václav Kubernát38175022021-11-04 14:58:57 +0100242#endif
Václav Kubernátbf65dd72020-05-28 02:32:31 +0200243 auto res = movePathGrammar.parse(begin, end, ctx, rctx, attr.m_source);
244 if (!res) {
245 parserContext.m_errorMsg = "Expected source path here:";
246 return false;
247 }
248
249 // Try absolute move first.
250 res = (space_separator >> move_absolute_table).parse(begin, end, ctx, rctx, attr.m_destination);
251 if (res) {
252 // Absolute move parsing succeeded, we don't need to parse anything else.
253 return true;
254 }
255
256 // If absolute move didn't succeed, try relative.
257 attr.m_destination = yang::move::Relative{};
258 res = (space_separator >> move_relative_table).parse(begin, end, ctx, rctx, std::get<yang::move::Relative>(attr.m_destination).m_position);
259
260 if (!res) {
261 parserContext.m_errorMsg = "Expected a move position (begin, end, before, after) here:";
262 return false;
263 }
264
265 if (std::holds_alternative<leafListElement_>(attr.m_source.m_nodes.back().m_suffix)) {
266 leaf_data_ value;
267 res = (space_separator >> leaf_data).parse(begin, end, ctx, rctx, value);
268 if (res) {
269 std::get<yang::move::Relative>(attr.m_destination).m_path = {{".", value}};
270 }
271 } else {
272 ListInstance listInstance;
273 // The source list instance will be stored inside the parser context path.
274 // The source list instance will be full data path (with keys included).
275 // However, m_tmpListPath is supposed to store a path with a list without the keys.
276 // So, I pop the last listElement_ (which has the keys) and put in a list_ (which doesn't have the keys).
277 // Example: /mod:cont/protocols[name='ftp'] gets turned into /mod:cont/protocols
278 parserContext.m_tmpListPath = parserContext.currentDataPath();
279 parserContext.m_tmpListPath.m_nodes.pop_back();
280 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 +0200281 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 +0200282
283 res = (space_separator >> listSuffix).parse(begin, end, ctx, rctx, listInstance);
284 if (res) {
285 std::get<yang::move::Relative>(attr.m_destination).m_path = listInstance;
286 }
287 }
288
289 if (!res) {
290 parserContext.m_errorMsg = "Expected a destination here:";
291 }
292
293 return res;
294 }
295} const move_args;
296
Václav Kubernát52b90222022-04-27 11:29:54 +0200297auto const move = x3::rule<move_class, move_>{"move"} =
Václav Kubernátbf65dd72020-05-28 02:32:31 +0200298 move_::name >> space_separator >> move_args;
299
Václav Kubernát70d7f7a2020-06-23 14:40:40 +0200300struct format_table : x3::symbols<DataFormat> {
301 format_table()
302 {
303 add
304 ("xml", DataFormat::Xml)
305 ("json", DataFormat::Json);
306 }
307} const format_table;
308
Václav Kubernát3b5e6522020-06-26 18:36:09 +0200309struct dump_args : x3::parser<dump_args> {
310 using attribute_type = dump_;
311 template <typename It, typename Ctx, typename RCtx>
312 bool parse(It& begin, It end, Ctx const& ctx, RCtx& rctx, dump_& attr) const
313 {
314 ParserContext& parserContext = x3::get<parser_context_tag>(ctx);
315 parserContext.m_suggestions = {{"xml"}, {"json"}};
316 parserContext.m_completionIterator = begin;
317 auto res = format_table.parse(begin, end, ctx, rctx, attr);
318 if (!res) {
319 parserContext.m_errorMsg = "Expected a data format (xml, json) here:";
320 }
321 return res;
322 }
323} const dump_args;
Václav Kubernát70d7f7a2020-06-23 14:40:40 +0200324
Václav Kubernát52b90222022-04-27 11:29:54 +0200325auto const prepare = x3::rule<prepare_class, prepare_>{"prepare"} =
Václav Kubernátd8408e02020-12-02 05:13:27 +0100326 prepare_::name > space_separator > as<dataPath_>[RpcActionPath<AllowInput::Yes>{}];
Václav Kubernáte7248b22020-06-26 15:38:59 +0200327
Václav Kubernát52b90222022-04-27 11:29:54 +0200328auto const exec = x3::rule<exec_class, exec_>{"exec"} =
Václav Kubernátaff1f492021-02-17 10:56:28 +0100329 exec_::name > -(space_separator > -as<dataPath_>[RpcActionPath<AllowInput::No>{}]);
Václav Kubernáte7248b22020-06-26 15:38:59 +0200330
Václav Kubernát52b90222022-04-27 11:29:54 +0200331auto const switch_rule = x3::rule<switch_class, switch_>{"switch"} =
Václav Kubernát7ff22072022-04-05 10:22:40 +0200332 switch_::name > space_separator > dsTargetSuggestions > ds_target_table;
Václav Kubernát162165e2021-02-22 09:46:53 +0100333
Václav Kubernát52b90222022-04-27 11:29:54 +0200334auto const cancel = x3::rule<cancel_class, cancel_>{"cancel"} =
Václav Kubernáte7248b22020-06-26 15:38:59 +0200335 cancel_::name >> x3::attr(cancel_{});
336
Václav Kubernát52b90222022-04-27 11:29:54 +0200337auto const dump = x3::rule<dump_class, dump_>{"dump"} =
Václav Kubernát3b5e6522020-06-26 18:36:09 +0200338 dump_::name > space_separator >> dump_args;
Václav Kubernát70d7f7a2020-06-23 14:40:40 +0200339
Václav Kubernát52b90222022-04-27 11:29:54 +0200340auto const quit = x3::rule<quit_class, quit_>{"quit"} =
Petr Gotthard1c76dea2022-04-13 17:56:58 +0200341 quit_::name >> x3::attr(quit_{});
342
Václav Kubernát52b90222022-04-27 11:29:54 +0200343auto const command = x3::rule<command_class, command_>{"command"} =
Václav Kubernát5b8e2822022-01-14 03:19:54 +0100344#if BOOST_VERSION <= 107800
Václav Kubernátf9533c22021-11-04 15:13:30 +0100345 x3::eps >>
346#endif
Václav Kubernát76bb8c22022-01-19 01:32:31 +0100347 -space_separator >> createCommandSuggestions >> x3::expect[cd | copy | create | delete_rule | set | commit | get | ls | discard | describe | help | move | dump | prepare | exec | cancel | switch_rule | quit] >> -space_separator;
Václav Kubernát41378452018-06-06 16:29:40 +0200348
349#if __clang__
350#pragma GCC diagnostic pop
351#endif