blob: fd01b5b8d64a24baa5837a5e918e52a415db1fc7 [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át68c89422021-10-20 15:32:20 +020018#if BOOST_VERSION <= 107700
Václav Kubernátf5f6d242022-04-07 01:06:19 +020019namespace boost::spirit::x3::traits {
Václav Kubernát68c89422021-10-20 15:32:20 +020020 // Backport https://github.com/boostorg/spirit/pull/702
21 // with instructions from https://github.com/boostorg/spirit/issues/701#issuecomment-946743099
22 template <typename... Types, typename T>
23 struct variant_find_substitute<boost::variant<Types...>, T>
24 {
25 using variant_type = boost::variant<Types...>;
26
27 typedef typename variant_type::types types;
28 typedef typename mpl::end<types>::type end;
29
30 typedef typename mpl::find<types, T>::type iter_1;
31
32 typedef typename
33 mpl::eval_if<
34 is_same<iter_1, end>,
35 mpl::find_if<types, traits::is_substitute<T, mpl::_1> >,
36 mpl::identity<iter_1>
37 >::type
38 iter;
39
40 typedef typename
41 mpl::eval_if<
42 is_same<iter, end>,
43 mpl::identity<T>,
44 mpl::deref<iter>
45 >::type
46 type;
47 };
48}
49#endif
Václav Kubernát60d6f292018-05-25 09:45:32 +020050
Václav Kubernát6d791432018-10-25 16:00:35 +020051x3::rule<discard_class, discard_> const discard = "discard";
Václav Kubernát11afac72018-07-18 14:59:53 +020052x3::rule<ls_class, ls_> const ls = "ls";
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020053x3::rule<cd_class, cd_> const cd = "cd";
Václav Kubernát07204242018-06-04 18:12:09 +020054x3::rule<set_class, set_> const set = "set";
Václav Kubernátb6ff0b62018-08-30 16:14:53 +020055x3::rule<get_class, get_> const get = "get";
Václav Kubernátb61336d2018-05-28 17:35:03 +020056x3::rule<create_class, create_> const create = "create";
57x3::rule<delete_class, delete_> const delete_rule = "delete_rule";
Václav Kubernát812ee282018-08-30 17:10:03 +020058x3::rule<commit_class, commit_> const commit = "commit";
Václav Kubernát9cfcd872020-02-18 12:34:02 +010059x3::rule<describe_class, describe_> const describe = "describe";
Václav Kubernát054cc992019-02-21 14:23:52 +010060x3::rule<help_class, help_> const help = "help";
Václav Kubernát7160a132020-04-03 02:11:01 +020061x3::rule<copy_class, copy_> const copy = "copy";
Václav Kubernátbf65dd72020-05-28 02:32:31 +020062x3::rule<move_class, move_> const move = "move";
Václav Kubernát70d7f7a2020-06-23 14:40:40 +020063x3::rule<dump_class, dump_> const dump = "dump";
Václav Kubernátaa4250a2020-07-22 00:02:23 +020064x3::rule<prepare_class, prepare_> const prepare = "prepare";
Václav Kubernáte7248b22020-06-26 15:38:59 +020065x3::rule<exec_class, exec_> const exec = "exec";
Václav Kubernát1d593902021-03-08 08:55:16 +010066x3::rule<switch_class, switch_> const switch_rule = "switch";
Václav Kubernáte7248b22020-06-26 15:38:59 +020067x3::rule<cancel_class, cancel_> const cancel = "cancel";
Václav Kubernátb61336d2018-05-28 17:35:03 +020068x3::rule<command_class, command_> const command = "command";
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020069
Václav Kubernát57272422019-02-08 12:48:24 +010070x3::rule<createCommandSuggestions_class, x3::unused_type> const createCommandSuggestions = "createCommandSuggestions";
Václav Kubernát5c75b252018-10-10 18:33:47 +020071
Václav Kubernát41378452018-06-06 16:29:40 +020072#if __clang__
73#pragma GCC diagnostic push
74#pragma GCC diagnostic ignored "-Woverloaded-shift-op-parentheses"
75#endif
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020076
Václav Kubernát509ce652019-05-29 19:46:44 +020077namespace ascii = boost::spirit::x3::ascii;
78
Václav Kubernáte7d4aea2018-09-11 18:15:48 +020079struct ls_options_table : x3::symbols<LsOption> {
80 ls_options_table()
81 {
Václav Kubernátbf083ec2019-02-19 13:58:09 +010082 add
83 ("--recursive", LsOption::Recursive);
Václav Kubernáte7d4aea2018-09-11 18:15:48 +020084 }
85} const ls_options;
86
Václav Kubernát11afac72018-07-18 14:59:53 +020087auto const ls_def =
Václav Kubernát4a58ce62020-05-14 17:58:10 +020088 ls_::name >> *(space_separator >> ls_options) >> -(space_separator >> (anyPath | (module >> "*")));
Václav Kubernát11afac72018-07-18 14:59:53 +020089
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020090auto const cd_def =
Václav Kubernáte7248b22020-06-26 15:38:59 +020091 cd_::name >> space_separator > cdPath;
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020092
Václav Kubernát38175022021-11-04 14:58:57 +010093#if BOOST_VERSION <= 107700
94auto const create_def =
95 create_::name >> space_separator >
96 (x3::eps >> presenceContainerPath |
97 x3::eps >> listInstancePath |
98 x3::eps >> leafListElementPath);
99
100auto const delete_rule_def =
101 delete_::name >> space_separator >
102 (x3::eps >> presenceContainerPath |
103 x3::eps >> listInstancePath |
104 x3::eps >> leafListElementPath |
105 x3::eps >> writableLeafPath);
106#else
Václav Kubernátb61336d2018-05-28 17:35:03 +0200107auto const create_def =
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200108 create_::name >> space_separator > (presenceContainerPath | listInstancePath | leafListElementPath);
Václav Kubernátb61336d2018-05-28 17:35:03 +0200109
110auto const delete_rule_def =
Jan Kundrátb7206ad2020-06-18 21:08:14 +0200111 delete_::name >> space_separator > (presenceContainerPath | listInstancePath | leafListElementPath | writableLeafPath);
Václav Kubernát38175022021-11-04 14:58:57 +0100112#endif
Václav Kubernát07204242018-06-04 18:12:09 +0200113
Václav Kubernátb6ff0b62018-08-30 16:14:53 +0200114auto const get_def =
Václav Kubernát31029682020-10-29 08:23:04 +0100115 get_::name >> -(space_separator >> getPath);
Václav Kubernátb6ff0b62018-08-30 16:14:53 +0200116
Václav Kubernát07204242018-06-04 18:12:09 +0200117auto const set_def =
Václav Kubernátabf52802020-05-19 01:31:17 +0200118 set_::name >> space_separator > writableLeafPath > space_separator > leaf_data;
Václav Kubernátb61336d2018-05-28 17:35:03 +0200119
Václav Kubernát812ee282018-08-30 17:10:03 +0200120auto const commit_def =
Václav Kubernátbf083ec2019-02-19 13:58:09 +0100121 commit_::name >> x3::attr(commit_());
Václav Kubernát812ee282018-08-30 17:10:03 +0200122
Václav Kubernát6d791432018-10-25 16:00:35 +0200123auto const discard_def =
Václav Kubernátbf083ec2019-02-19 13:58:09 +0100124 discard_::name >> x3::attr(discard_());
Václav Kubernát6d791432018-10-25 16:00:35 +0200125
Václav Kubernát054cc992019-02-21 14:23:52 +0100126struct command_names_table : x3::symbols<decltype(help_::m_cmd)> {
127 command_names_table()
128 {
129 boost::mpl::for_each<CommandTypes, boost::type<boost::mpl::_>>([this](auto cmd) {
Václav Kubernát672889c2020-05-27 04:11:36 +0200130 add(commandNamesVisitor(cmd), decltype(help_::m_cmd)(cmd));
Václav Kubernát054cc992019-02-21 14:23:52 +0100131 });
132 }
133} const command_names;
134
135auto const help_def =
136 help_::name > createCommandSuggestions >> -command_names;
137
Václav Kubernát7160a132020-04-03 02:11:01 +0200138struct datastore_symbol_table : x3::symbols<Datastore> {
139 datastore_symbol_table()
140 {
141 add
142 ("running", Datastore::Running)
143 ("startup", Datastore::Startup);
144 }
145} const datastore;
146
Václav Kubernát21fc5e22020-11-26 04:28:27 +0100147const auto copy_source = x3::rule<class source, Datastore>{"source datastore"} = datastore;
148const auto copy_destination = x3::rule<class source, Datastore>{"destination datastore"} = datastore;
Václav Kubernát7160a132020-04-03 02:11:01 +0200149
150const auto datastoreSuggestions = x3::eps[([](auto& ctx) {
151 auto& parserContext = x3::get<parser_context_tag>(ctx);
152 parserContext.m_suggestions = {Completion{"running", " "}, Completion{"startup", " "}};
153 parserContext.m_completionIterator = _where(ctx).begin();
154})];
155
156struct copy_args : x3::parser<copy_args> {
157 using attribute_type = copy_;
158 template <typename It, typename Ctx, typename RCtx>
159 bool parse(It& begin, It end, Ctx const& ctx, RCtx& rctx, copy_& attr) const
160 {
161 auto& parserContext = x3::get<parser_context_tag>(ctx);
162 auto iterBeforeDestination = begin;
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100163 auto save_iter = x3::no_skip[x3::eps[([&iterBeforeDestination](auto& ctx) { iterBeforeDestination = _where(ctx).begin(); })]];
Václav Kubernát7160a132020-04-03 02:11:01 +0200164 auto grammar = datastoreSuggestions > copy_source > space_separator > datastoreSuggestions > save_iter > copy_destination;
165
166 try {
167 grammar.parse(begin, end, ctx, rctx, attr);
168 } catch (x3::expectation_failure<It>& ex) {
169 using namespace std::string_literals;
170 parserContext.m_errorMsg = "Expected "s + ex.which() + " here:";
171 throw;
172 }
173
174 if (attr.m_source == attr.m_destination) {
175 begin = iterBeforeDestination; // Restoring the iterator here makes the error caret point to the second datastore
176 parserContext.m_errorMsg = "Source datastore and destination datastore can't be the same.";
177 return false;
178 }
179
180 return true;
181 }
Václav Kubernát21fc5e22020-11-26 04:28:27 +0100182} const copy_args;
Václav Kubernát7160a132020-04-03 02:11:01 +0200183
184auto const copy_def =
185 copy_::name > space_separator > copy_args;
186
Václav Kubernát9cfcd872020-02-18 12:34:02 +0100187auto const describe_def =
Václav Kubernát3200b862020-12-03 02:34:55 +0100188 describe_::name >> space_separator > anyPath;
Václav Kubernát9cfcd872020-02-18 12:34:02 +0100189
Václav Kubernát162165e2021-02-22 09:46:53 +0100190struct move_mode_table : x3::symbols<MoveMode> {
191 move_mode_table()
Václav Kubernátbf65dd72020-05-28 02:32:31 +0200192 {
193 add
194 ("after", MoveMode::After)
195 ("before", MoveMode::Before)
196 ("begin", MoveMode::Begin)
197 ("end", MoveMode::End);
198 }
Václav Kubernát162165e2021-02-22 09:46:53 +0100199} const move_mode_table;
Václav Kubernátbf65dd72020-05-28 02:32:31 +0200200
201struct move_absolute_table : x3::symbols<yang::move::Absolute> {
202 move_absolute_table()
203 {
204 add
205 ("begin", yang::move::Absolute::Begin)
206 ("end", yang::move::Absolute::End);
207 }
208} const move_absolute_table;
209
210struct move_relative_table : x3::symbols<yang::move::Relative::Position> {
211 move_relative_table()
212 {
213 add
214 ("before", yang::move::Relative::Position::Before)
215 ("after", yang::move::Relative::Position::After);
216 }
217} const move_relative_table;
218
219struct move_args : x3::parser<move_args> {
220 using attribute_type = move_;
221 template <typename It, typename Ctx, typename RCtx>
222 bool parse(It& begin, It end, Ctx const& ctx, RCtx& rctx, move_& attr) const
223 {
224 ParserContext& parserContext = x3::get<parser_context_tag>(ctx);
225 dataPath_ movePath;
Václav Kubernát38175022021-11-04 14:58:57 +0100226#if BOOST_VERSION <= 107700
227 auto movePathGrammar = x3::eps >> listInstancePath | x3::eps >> leafListElementPath;
228#else
Václav Kubernátbf65dd72020-05-28 02:32:31 +0200229 auto movePathGrammar = listInstancePath | leafListElementPath;
Václav Kubernát38175022021-11-04 14:58:57 +0100230#endif
Václav Kubernátbf65dd72020-05-28 02:32:31 +0200231 auto res = movePathGrammar.parse(begin, end, ctx, rctx, attr.m_source);
232 if (!res) {
233 parserContext.m_errorMsg = "Expected source path here:";
234 return false;
235 }
236
237 // Try absolute move first.
238 res = (space_separator >> move_absolute_table).parse(begin, end, ctx, rctx, attr.m_destination);
239 if (res) {
240 // Absolute move parsing succeeded, we don't need to parse anything else.
241 return true;
242 }
243
244 // If absolute move didn't succeed, try relative.
245 attr.m_destination = yang::move::Relative{};
246 res = (space_separator >> move_relative_table).parse(begin, end, ctx, rctx, std::get<yang::move::Relative>(attr.m_destination).m_position);
247
248 if (!res) {
249 parserContext.m_errorMsg = "Expected a move position (begin, end, before, after) here:";
250 return false;
251 }
252
253 if (std::holds_alternative<leafListElement_>(attr.m_source.m_nodes.back().m_suffix)) {
254 leaf_data_ value;
255 res = (space_separator >> leaf_data).parse(begin, end, ctx, rctx, value);
256 if (res) {
257 std::get<yang::move::Relative>(attr.m_destination).m_path = {{".", value}};
258 }
259 } else {
260 ListInstance listInstance;
261 // The source list instance will be stored inside the parser context path.
262 // The source list instance will be full data path (with keys included).
263 // However, m_tmpListPath is supposed to store a path with a list without the keys.
264 // So, I pop the last listElement_ (which has the keys) and put in a list_ (which doesn't have the keys).
265 // Example: /mod:cont/protocols[name='ftp'] gets turned into /mod:cont/protocols
266 parserContext.m_tmpListPath = parserContext.currentDataPath();
267 parserContext.m_tmpListPath.m_nodes.pop_back();
268 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 +0200269 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 +0200270
271 res = (space_separator >> listSuffix).parse(begin, end, ctx, rctx, listInstance);
272 if (res) {
273 std::get<yang::move::Relative>(attr.m_destination).m_path = listInstance;
274 }
275 }
276
277 if (!res) {
278 parserContext.m_errorMsg = "Expected a destination here:";
279 }
280
281 return res;
282 }
283} const move_args;
284
285auto const move_def =
286 move_::name >> space_separator >> move_args;
287
Václav Kubernát70d7f7a2020-06-23 14:40:40 +0200288struct format_table : x3::symbols<DataFormat> {
289 format_table()
290 {
291 add
292 ("xml", DataFormat::Xml)
293 ("json", DataFormat::Json);
294 }
295} const format_table;
296
Václav Kubernát3b5e6522020-06-26 18:36:09 +0200297struct dump_args : x3::parser<dump_args> {
298 using attribute_type = dump_;
299 template <typename It, typename Ctx, typename RCtx>
300 bool parse(It& begin, It end, Ctx const& ctx, RCtx& rctx, dump_& attr) const
301 {
302 ParserContext& parserContext = x3::get<parser_context_tag>(ctx);
303 parserContext.m_suggestions = {{"xml"}, {"json"}};
304 parserContext.m_completionIterator = begin;
305 auto res = format_table.parse(begin, end, ctx, rctx, attr);
306 if (!res) {
307 parserContext.m_errorMsg = "Expected a data format (xml, json) here:";
308 }
309 return res;
310 }
311} const dump_args;
Václav Kubernát70d7f7a2020-06-23 14:40:40 +0200312
Václav Kubernátaa4250a2020-07-22 00:02:23 +0200313auto const prepare_def =
Václav Kubernátd8408e02020-12-02 05:13:27 +0100314 prepare_::name > space_separator > as<dataPath_>[RpcActionPath<AllowInput::Yes>{}];
Václav Kubernáte7248b22020-06-26 15:38:59 +0200315
316auto const exec_def =
Václav Kubernátaff1f492021-02-17 10:56:28 +0100317 exec_::name > -(space_separator > -as<dataPath_>[RpcActionPath<AllowInput::No>{}]);
Václav Kubernáte7248b22020-06-26 15:38:59 +0200318
Václav Kubernát162165e2021-02-22 09:46:53 +0100319const auto dsTargetSuggestions = x3::eps[([](auto& ctx) {
320 auto& parserContext = x3::get<parser_context_tag>(ctx);
321 parserContext.m_suggestions = {Completion{"running", " "}, Completion{"startup", " "}, Completion{"operational", " "}};
322 parserContext.m_completionIterator = _where(ctx).begin();
323})];
324
325struct ds_target_table : x3::symbols<DatastoreTarget> {
326 ds_target_table()
327 {
328 add
329 ("operational", DatastoreTarget::Operational)
330 ("startup", DatastoreTarget::Startup)
331 ("running", DatastoreTarget::Running);
332 }
333} const ds_target_table;
334
335auto const switch_rule_def =
Václav Kubernát1d593902021-03-08 08:55:16 +0100336 switch_::name > space_separator > as<x3::unused_type>[dsTargetSuggestions] > ds_target_table;
Václav Kubernát162165e2021-02-22 09:46:53 +0100337
Václav Kubernáte7248b22020-06-26 15:38:59 +0200338auto const cancel_def =
339 cancel_::name >> x3::attr(cancel_{});
340
Václav Kubernát70d7f7a2020-06-23 14:40:40 +0200341auto const dump_def =
Václav Kubernát3b5e6522020-06-26 18:36:09 +0200342 dump_::name > space_separator >> dump_args;
Václav Kubernát70d7f7a2020-06-23 14:40:40 +0200343
Václav Kubernát57272422019-02-08 12:48:24 +0100344auto const createCommandSuggestions_def =
Václav Kubernátbf083ec2019-02-19 13:58:09 +0100345 x3::eps;
Václav Kubernát57272422019-02-08 12:48:24 +0100346
Václav Kubernátb61336d2018-05-28 17:35:03 +0200347auto const command_def =
Václav Kubernát5b8e2822022-01-14 03:19:54 +0100348#if BOOST_VERSION <= 107800
Václav Kubernátf9533c22021-11-04 15:13:30 +0100349 x3::eps >>
350#endif
Václav Kubernát162165e2021-02-22 09:46:53 +0100351 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 +0200352
353#if __clang__
354#pragma GCC diagnostic pop
355#endif
Václav Kubernátb61336d2018-05-28 17:35:03 +0200356
Václav Kubernát07204242018-06-04 18:12:09 +0200357BOOST_SPIRIT_DEFINE(set)
Václav Kubernát812ee282018-08-30 17:10:03 +0200358BOOST_SPIRIT_DEFINE(commit)
Václav Kubernátb6ff0b62018-08-30 16:14:53 +0200359BOOST_SPIRIT_DEFINE(get)
Václav Kubernát11afac72018-07-18 14:59:53 +0200360BOOST_SPIRIT_DEFINE(ls)
Václav Kubernát6d791432018-10-25 16:00:35 +0200361BOOST_SPIRIT_DEFINE(discard)
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200362BOOST_SPIRIT_DEFINE(cd)
Václav Kubernátb61336d2018-05-28 17:35:03 +0200363BOOST_SPIRIT_DEFINE(create)
364BOOST_SPIRIT_DEFINE(delete_rule)
Václav Kubernát9cfcd872020-02-18 12:34:02 +0100365BOOST_SPIRIT_DEFINE(describe)
Václav Kubernát054cc992019-02-21 14:23:52 +0100366BOOST_SPIRIT_DEFINE(help)
Václav Kubernát7160a132020-04-03 02:11:01 +0200367BOOST_SPIRIT_DEFINE(copy)
Václav Kubernátbf65dd72020-05-28 02:32:31 +0200368BOOST_SPIRIT_DEFINE(move)
Václav Kubernát70d7f7a2020-06-23 14:40:40 +0200369BOOST_SPIRIT_DEFINE(dump)
Václav Kubernátaa4250a2020-07-22 00:02:23 +0200370BOOST_SPIRIT_DEFINE(prepare)
Václav Kubernáte7248b22020-06-26 15:38:59 +0200371BOOST_SPIRIT_DEFINE(exec)
Václav Kubernát162165e2021-02-22 09:46:53 +0100372BOOST_SPIRIT_DEFINE(switch_rule)
Václav Kubernáte7248b22020-06-26 15:38:59 +0200373BOOST_SPIRIT_DEFINE(cancel)
Václav Kubernátb61336d2018-05-28 17:35:03 +0200374BOOST_SPIRIT_DEFINE(command)
Václav Kubernát57272422019-02-08 12:48:24 +0100375BOOST_SPIRIT_DEFINE(createCommandSuggestions)