blob: 39b993ddc67ae108753fe7bb5400c96b9525046e [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
19namespace boost::spirit::x3::traits
20{
21 // Backport https://github.com/boostorg/spirit/pull/702
22 // with instructions from https://github.com/boostorg/spirit/issues/701#issuecomment-946743099
23 template <typename... Types, typename T>
24 struct variant_find_substitute<boost::variant<Types...>, T>
25 {
26 using variant_type = boost::variant<Types...>;
27
28 typedef typename variant_type::types types;
29 typedef typename mpl::end<types>::type end;
30
31 typedef typename mpl::find<types, T>::type iter_1;
32
33 typedef typename
34 mpl::eval_if<
35 is_same<iter_1, end>,
36 mpl::find_if<types, traits::is_substitute<T, mpl::_1> >,
37 mpl::identity<iter_1>
38 >::type
39 iter;
40
41 typedef typename
42 mpl::eval_if<
43 is_same<iter, end>,
44 mpl::identity<T>,
45 mpl::deref<iter>
46 >::type
47 type;
48 };
49}
50#endif
Václav Kubernát60d6f292018-05-25 09:45:32 +020051
Václav Kubernát6d791432018-10-25 16:00:35 +020052x3::rule<discard_class, discard_> const discard = "discard";
Václav Kubernát11afac72018-07-18 14:59:53 +020053x3::rule<ls_class, ls_> const ls = "ls";
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020054x3::rule<cd_class, cd_> const cd = "cd";
Václav Kubernát07204242018-06-04 18:12:09 +020055x3::rule<set_class, set_> const set = "set";
Václav Kubernátb6ff0b62018-08-30 16:14:53 +020056x3::rule<get_class, get_> const get = "get";
Václav Kubernátb61336d2018-05-28 17:35:03 +020057x3::rule<create_class, create_> const create = "create";
58x3::rule<delete_class, delete_> const delete_rule = "delete_rule";
Václav Kubernát812ee282018-08-30 17:10:03 +020059x3::rule<commit_class, commit_> const commit = "commit";
Václav Kubernát9cfcd872020-02-18 12:34:02 +010060x3::rule<describe_class, describe_> const describe = "describe";
Václav Kubernát054cc992019-02-21 14:23:52 +010061x3::rule<help_class, help_> const help = "help";
Václav Kubernát7160a132020-04-03 02:11:01 +020062x3::rule<copy_class, copy_> const copy = "copy";
Václav Kubernátbf65dd72020-05-28 02:32:31 +020063x3::rule<move_class, move_> const move = "move";
Václav Kubernát70d7f7a2020-06-23 14:40:40 +020064x3::rule<dump_class, dump_> const dump = "dump";
Václav Kubernátaa4250a2020-07-22 00:02:23 +020065x3::rule<prepare_class, prepare_> const prepare = "prepare";
Václav Kubernáte7248b22020-06-26 15:38:59 +020066x3::rule<exec_class, exec_> const exec = "exec";
Václav Kubernát1d593902021-03-08 08:55:16 +010067x3::rule<switch_class, switch_> const switch_rule = "switch";
Václav Kubernáte7248b22020-06-26 15:38:59 +020068x3::rule<cancel_class, cancel_> const cancel = "cancel";
Václav Kubernátb61336d2018-05-28 17:35:03 +020069x3::rule<command_class, command_> const command = "command";
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020070
Václav Kubernát57272422019-02-08 12:48:24 +010071x3::rule<createCommandSuggestions_class, x3::unused_type> const createCommandSuggestions = "createCommandSuggestions";
Václav Kubernát5c75b252018-10-10 18:33:47 +020072
Václav Kubernát41378452018-06-06 16:29:40 +020073#if __clang__
74#pragma GCC diagnostic push
75#pragma GCC diagnostic ignored "-Woverloaded-shift-op-parentheses"
76#endif
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020077
Václav Kubernát509ce652019-05-29 19:46:44 +020078namespace ascii = boost::spirit::x3::ascii;
79
Václav Kubernáte7d4aea2018-09-11 18:15:48 +020080struct ls_options_table : x3::symbols<LsOption> {
81 ls_options_table()
82 {
Václav Kubernátbf083ec2019-02-19 13:58:09 +010083 add
84 ("--recursive", LsOption::Recursive);
Václav Kubernáte7d4aea2018-09-11 18:15:48 +020085 }
86} const ls_options;
87
Václav Kubernát11afac72018-07-18 14:59:53 +020088auto const ls_def =
Václav Kubernát4a58ce62020-05-14 17:58:10 +020089 ls_::name >> *(space_separator >> ls_options) >> -(space_separator >> (anyPath | (module >> "*")));
Václav Kubernát11afac72018-07-18 14:59:53 +020090
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020091auto const cd_def =
Václav Kubernáte7248b22020-06-26 15:38:59 +020092 cd_::name >> space_separator > cdPath;
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020093
Václav Kubernát38175022021-11-04 14:58:57 +010094#if BOOST_VERSION <= 107700
95auto const create_def =
96 create_::name >> space_separator >
97 (x3::eps >> presenceContainerPath |
98 x3::eps >> listInstancePath |
99 x3::eps >> leafListElementPath);
100
101auto const delete_rule_def =
102 delete_::name >> space_separator >
103 (x3::eps >> presenceContainerPath |
104 x3::eps >> listInstancePath |
105 x3::eps >> leafListElementPath |
106 x3::eps >> writableLeafPath);
107#else
Václav Kubernátb61336d2018-05-28 17:35:03 +0200108auto const create_def =
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200109 create_::name >> space_separator > (presenceContainerPath | listInstancePath | leafListElementPath);
Václav Kubernátb61336d2018-05-28 17:35:03 +0200110
111auto const delete_rule_def =
Jan Kundrátb7206ad2020-06-18 21:08:14 +0200112 delete_::name >> space_separator > (presenceContainerPath | listInstancePath | leafListElementPath | writableLeafPath);
Václav Kubernát38175022021-11-04 14:58:57 +0100113#endif
Václav Kubernát07204242018-06-04 18:12:09 +0200114
Václav Kubernátb6ff0b62018-08-30 16:14:53 +0200115auto const get_def =
Václav Kubernát31029682020-10-29 08:23:04 +0100116 get_::name >> -(space_separator >> getPath);
Václav Kubernátb6ff0b62018-08-30 16:14:53 +0200117
Václav Kubernát07204242018-06-04 18:12:09 +0200118auto const set_def =
Václav Kubernátabf52802020-05-19 01:31:17 +0200119 set_::name >> space_separator > writableLeafPath > space_separator > leaf_data;
Václav Kubernátb61336d2018-05-28 17:35:03 +0200120
Václav Kubernát812ee282018-08-30 17:10:03 +0200121auto const commit_def =
Václav Kubernátbf083ec2019-02-19 13:58:09 +0100122 commit_::name >> x3::attr(commit_());
Václav Kubernát812ee282018-08-30 17:10:03 +0200123
Václav Kubernát6d791432018-10-25 16:00:35 +0200124auto const discard_def =
Václav Kubernátbf083ec2019-02-19 13:58:09 +0100125 discard_::name >> x3::attr(discard_());
Václav Kubernát6d791432018-10-25 16:00:35 +0200126
Václav Kubernát054cc992019-02-21 14:23:52 +0100127struct command_names_table : x3::symbols<decltype(help_::m_cmd)> {
128 command_names_table()
129 {
130 boost::mpl::for_each<CommandTypes, boost::type<boost::mpl::_>>([this](auto cmd) {
Václav Kubernát672889c2020-05-27 04:11:36 +0200131 add(commandNamesVisitor(cmd), decltype(help_::m_cmd)(cmd));
Václav Kubernát054cc992019-02-21 14:23:52 +0100132 });
133 }
134} const command_names;
135
136auto const help_def =
137 help_::name > createCommandSuggestions >> -command_names;
138
Václav Kubernát7160a132020-04-03 02:11:01 +0200139struct datastore_symbol_table : x3::symbols<Datastore> {
140 datastore_symbol_table()
141 {
142 add
143 ("running", Datastore::Running)
144 ("startup", Datastore::Startup);
145 }
146} const datastore;
147
Václav Kubernát21fc5e22020-11-26 04:28:27 +0100148const auto copy_source = x3::rule<class source, Datastore>{"source datastore"} = datastore;
149const auto copy_destination = x3::rule<class source, Datastore>{"destination datastore"} = datastore;
Václav Kubernát7160a132020-04-03 02:11:01 +0200150
151const auto datastoreSuggestions = x3::eps[([](auto& ctx) {
152 auto& parserContext = x3::get<parser_context_tag>(ctx);
153 parserContext.m_suggestions = {Completion{"running", " "}, Completion{"startup", " "}};
154 parserContext.m_completionIterator = _where(ctx).begin();
155})];
156
157struct copy_args : x3::parser<copy_args> {
158 using attribute_type = copy_;
159 template <typename It, typename Ctx, typename RCtx>
160 bool parse(It& begin, It end, Ctx const& ctx, RCtx& rctx, copy_& attr) const
161 {
162 auto& parserContext = x3::get<parser_context_tag>(ctx);
163 auto iterBeforeDestination = begin;
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100164 auto save_iter = x3::no_skip[x3::eps[([&iterBeforeDestination](auto& ctx) { iterBeforeDestination = _where(ctx).begin(); })]];
Václav Kubernát7160a132020-04-03 02:11:01 +0200165 auto grammar = datastoreSuggestions > copy_source > space_separator > datastoreSuggestions > save_iter > copy_destination;
166
167 try {
168 grammar.parse(begin, end, ctx, rctx, attr);
169 } catch (x3::expectation_failure<It>& ex) {
170 using namespace std::string_literals;
171 parserContext.m_errorMsg = "Expected "s + ex.which() + " here:";
172 throw;
173 }
174
175 if (attr.m_source == attr.m_destination) {
176 begin = iterBeforeDestination; // Restoring the iterator here makes the error caret point to the second datastore
177 parserContext.m_errorMsg = "Source datastore and destination datastore can't be the same.";
178 return false;
179 }
180
181 return true;
182 }
Václav Kubernát21fc5e22020-11-26 04:28:27 +0100183} const copy_args;
Václav Kubernát7160a132020-04-03 02:11:01 +0200184
185auto const copy_def =
186 copy_::name > space_separator > copy_args;
187
Václav Kubernát9cfcd872020-02-18 12:34:02 +0100188auto const describe_def =
Václav Kubernát3200b862020-12-03 02:34:55 +0100189 describe_::name >> space_separator > anyPath;
Václav Kubernát9cfcd872020-02-18 12:34:02 +0100190
Václav Kubernát162165e2021-02-22 09:46:53 +0100191struct move_mode_table : x3::symbols<MoveMode> {
192 move_mode_table()
Václav Kubernátbf65dd72020-05-28 02:32:31 +0200193 {
194 add
195 ("after", MoveMode::After)
196 ("before", MoveMode::Before)
197 ("begin", MoveMode::Begin)
198 ("end", MoveMode::End);
199 }
Václav Kubernát162165e2021-02-22 09:46:53 +0100200} const move_mode_table;
Václav Kubernátbf65dd72020-05-28 02:32:31 +0200201
202struct move_absolute_table : x3::symbols<yang::move::Absolute> {
203 move_absolute_table()
204 {
205 add
206 ("begin", yang::move::Absolute::Begin)
207 ("end", yang::move::Absolute::End);
208 }
209} const move_absolute_table;
210
211struct move_relative_table : x3::symbols<yang::move::Relative::Position> {
212 move_relative_table()
213 {
214 add
215 ("before", yang::move::Relative::Position::Before)
216 ("after", yang::move::Relative::Position::After);
217 }
218} const move_relative_table;
219
220struct move_args : x3::parser<move_args> {
221 using attribute_type = move_;
222 template <typename It, typename Ctx, typename RCtx>
223 bool parse(It& begin, It end, Ctx const& ctx, RCtx& rctx, move_& attr) const
224 {
225 ParserContext& parserContext = x3::get<parser_context_tag>(ctx);
226 dataPath_ movePath;
Václav Kubernát38175022021-11-04 14:58:57 +0100227#if BOOST_VERSION <= 107700
228 auto movePathGrammar = x3::eps >> listInstancePath | x3::eps >> leafListElementPath;
229#else
Václav Kubernátbf65dd72020-05-28 02:32:31 +0200230 auto movePathGrammar = listInstancePath | leafListElementPath;
Václav Kubernát38175022021-11-04 14:58:57 +0100231#endif
Václav Kubernátbf65dd72020-05-28 02:32:31 +0200232 auto res = movePathGrammar.parse(begin, end, ctx, rctx, attr.m_source);
233 if (!res) {
234 parserContext.m_errorMsg = "Expected source path here:";
235 return false;
236 }
237
238 // Try absolute move first.
239 res = (space_separator >> move_absolute_table).parse(begin, end, ctx, rctx, attr.m_destination);
240 if (res) {
241 // Absolute move parsing succeeded, we don't need to parse anything else.
242 return true;
243 }
244
245 // If absolute move didn't succeed, try relative.
246 attr.m_destination = yang::move::Relative{};
247 res = (space_separator >> move_relative_table).parse(begin, end, ctx, rctx, std::get<yang::move::Relative>(attr.m_destination).m_position);
248
249 if (!res) {
250 parserContext.m_errorMsg = "Expected a move position (begin, end, before, after) here:";
251 return false;
252 }
253
254 if (std::holds_alternative<leafListElement_>(attr.m_source.m_nodes.back().m_suffix)) {
255 leaf_data_ value;
256 res = (space_separator >> leaf_data).parse(begin, end, ctx, rctx, value);
257 if (res) {
258 std::get<yang::move::Relative>(attr.m_destination).m_path = {{".", value}};
259 }
260 } else {
261 ListInstance listInstance;
262 // The source list instance will be stored inside the parser context path.
263 // The source list instance will be full data path (with keys included).
264 // However, m_tmpListPath is supposed to store a path with a list without the keys.
265 // So, I pop the last listElement_ (which has the keys) and put in a list_ (which doesn't have the keys).
266 // Example: /mod:cont/protocols[name='ftp'] gets turned into /mod:cont/protocols
267 parserContext.m_tmpListPath = parserContext.currentDataPath();
268 parserContext.m_tmpListPath.m_nodes.pop_back();
269 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 +0200270 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 +0200271
272 res = (space_separator >> listSuffix).parse(begin, end, ctx, rctx, listInstance);
273 if (res) {
274 std::get<yang::move::Relative>(attr.m_destination).m_path = listInstance;
275 }
276 }
277
278 if (!res) {
279 parserContext.m_errorMsg = "Expected a destination here:";
280 }
281
282 return res;
283 }
284} const move_args;
285
286auto const move_def =
287 move_::name >> space_separator >> move_args;
288
Václav Kubernát70d7f7a2020-06-23 14:40:40 +0200289struct format_table : x3::symbols<DataFormat> {
290 format_table()
291 {
292 add
293 ("xml", DataFormat::Xml)
294 ("json", DataFormat::Json);
295 }
296} const format_table;
297
Václav Kubernát3b5e6522020-06-26 18:36:09 +0200298struct dump_args : x3::parser<dump_args> {
299 using attribute_type = dump_;
300 template <typename It, typename Ctx, typename RCtx>
301 bool parse(It& begin, It end, Ctx const& ctx, RCtx& rctx, dump_& attr) const
302 {
303 ParserContext& parserContext = x3::get<parser_context_tag>(ctx);
304 parserContext.m_suggestions = {{"xml"}, {"json"}};
305 parserContext.m_completionIterator = begin;
306 auto res = format_table.parse(begin, end, ctx, rctx, attr);
307 if (!res) {
308 parserContext.m_errorMsg = "Expected a data format (xml, json) here:";
309 }
310 return res;
311 }
312} const dump_args;
Václav Kubernát70d7f7a2020-06-23 14:40:40 +0200313
Václav Kubernátaa4250a2020-07-22 00:02:23 +0200314auto const prepare_def =
Václav Kubernátd8408e02020-12-02 05:13:27 +0100315 prepare_::name > space_separator > as<dataPath_>[RpcActionPath<AllowInput::Yes>{}];
Václav Kubernáte7248b22020-06-26 15:38:59 +0200316
317auto const exec_def =
Václav Kubernátaff1f492021-02-17 10:56:28 +0100318 exec_::name > -(space_separator > -as<dataPath_>[RpcActionPath<AllowInput::No>{}]);
Václav Kubernáte7248b22020-06-26 15:38:59 +0200319
Václav Kubernát162165e2021-02-22 09:46:53 +0100320const auto dsTargetSuggestions = x3::eps[([](auto& ctx) {
321 auto& parserContext = x3::get<parser_context_tag>(ctx);
322 parserContext.m_suggestions = {Completion{"running", " "}, Completion{"startup", " "}, Completion{"operational", " "}};
323 parserContext.m_completionIterator = _where(ctx).begin();
324})];
325
326struct ds_target_table : x3::symbols<DatastoreTarget> {
327 ds_target_table()
328 {
329 add
330 ("operational", DatastoreTarget::Operational)
331 ("startup", DatastoreTarget::Startup)
332 ("running", DatastoreTarget::Running);
333 }
334} const ds_target_table;
335
336auto const switch_rule_def =
Václav Kubernát1d593902021-03-08 08:55:16 +0100337 switch_::name > space_separator > as<x3::unused_type>[dsTargetSuggestions] > ds_target_table;
Václav Kubernát162165e2021-02-22 09:46:53 +0100338
Václav Kubernáte7248b22020-06-26 15:38:59 +0200339auto const cancel_def =
340 cancel_::name >> x3::attr(cancel_{});
341
Václav Kubernát70d7f7a2020-06-23 14:40:40 +0200342auto const dump_def =
Václav Kubernát3b5e6522020-06-26 18:36:09 +0200343 dump_::name > space_separator >> dump_args;
Václav Kubernát70d7f7a2020-06-23 14:40:40 +0200344
Václav Kubernát57272422019-02-08 12:48:24 +0100345auto const createCommandSuggestions_def =
Václav Kubernátbf083ec2019-02-19 13:58:09 +0100346 x3::eps;
Václav Kubernát57272422019-02-08 12:48:24 +0100347
Václav Kubernátb61336d2018-05-28 17:35:03 +0200348auto const command_def =
Václav Kubernát162165e2021-02-22 09:46:53 +0100349 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 +0200350
351#if __clang__
352#pragma GCC diagnostic pop
353#endif
Václav Kubernátb61336d2018-05-28 17:35:03 +0200354
Václav Kubernát07204242018-06-04 18:12:09 +0200355BOOST_SPIRIT_DEFINE(set)
Václav Kubernát812ee282018-08-30 17:10:03 +0200356BOOST_SPIRIT_DEFINE(commit)
Václav Kubernátb6ff0b62018-08-30 16:14:53 +0200357BOOST_SPIRIT_DEFINE(get)
Václav Kubernát11afac72018-07-18 14:59:53 +0200358BOOST_SPIRIT_DEFINE(ls)
Václav Kubernát6d791432018-10-25 16:00:35 +0200359BOOST_SPIRIT_DEFINE(discard)
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200360BOOST_SPIRIT_DEFINE(cd)
Václav Kubernátb61336d2018-05-28 17:35:03 +0200361BOOST_SPIRIT_DEFINE(create)
362BOOST_SPIRIT_DEFINE(delete_rule)
Václav Kubernát9cfcd872020-02-18 12:34:02 +0100363BOOST_SPIRIT_DEFINE(describe)
Václav Kubernát054cc992019-02-21 14:23:52 +0100364BOOST_SPIRIT_DEFINE(help)
Václav Kubernát7160a132020-04-03 02:11:01 +0200365BOOST_SPIRIT_DEFINE(copy)
Václav Kubernátbf65dd72020-05-28 02:32:31 +0200366BOOST_SPIRIT_DEFINE(move)
Václav Kubernát70d7f7a2020-06-23 14:40:40 +0200367BOOST_SPIRIT_DEFINE(dump)
Václav Kubernátaa4250a2020-07-22 00:02:23 +0200368BOOST_SPIRIT_DEFINE(prepare)
Václav Kubernáte7248b22020-06-26 15:38:59 +0200369BOOST_SPIRIT_DEFINE(exec)
Václav Kubernát162165e2021-02-22 09:46:53 +0100370BOOST_SPIRIT_DEFINE(switch_rule)
Václav Kubernáte7248b22020-06-26 15:38:59 +0200371BOOST_SPIRIT_DEFINE(cancel)
Václav Kubernátb61336d2018-05-28 17:35:03 +0200372BOOST_SPIRIT_DEFINE(command)
Václav Kubernát57272422019-02-08 12:48:24 +0100373BOOST_SPIRIT_DEFINE(createCommandSuggestions)