blob: dc116a9a9273b3402eb1357662290a60c008ff14 [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át60d6f292018-05-25 09:45:32 +020018
Václav Kubernát6d791432018-10-25 16:00:35 +020019x3::rule<discard_class, discard_> const discard = "discard";
Václav Kubernát11afac72018-07-18 14:59:53 +020020x3::rule<ls_class, ls_> const ls = "ls";
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020021x3::rule<cd_class, cd_> const cd = "cd";
Václav Kubernát07204242018-06-04 18:12:09 +020022x3::rule<set_class, set_> const set = "set";
Václav Kubernátb6ff0b62018-08-30 16:14:53 +020023x3::rule<get_class, get_> const get = "get";
Václav Kubernátb61336d2018-05-28 17:35:03 +020024x3::rule<create_class, create_> const create = "create";
25x3::rule<delete_class, delete_> const delete_rule = "delete_rule";
Václav Kubernát812ee282018-08-30 17:10:03 +020026x3::rule<commit_class, commit_> const commit = "commit";
Václav Kubernát9cfcd872020-02-18 12:34:02 +010027x3::rule<describe_class, describe_> const describe = "describe";
Václav Kubernát054cc992019-02-21 14:23:52 +010028x3::rule<help_class, help_> const help = "help";
Václav Kubernát7160a132020-04-03 02:11:01 +020029x3::rule<copy_class, copy_> const copy = "copy";
Václav Kubernátbf65dd72020-05-28 02:32:31 +020030x3::rule<move_class, move_> const move = "move";
Václav Kubernát70d7f7a2020-06-23 14:40:40 +020031x3::rule<dump_class, dump_> const dump = "dump";
Václav Kubernátaa4250a2020-07-22 00:02:23 +020032x3::rule<prepare_class, prepare_> const prepare = "prepare";
Václav Kubernáte7248b22020-06-26 15:38:59 +020033x3::rule<exec_class, exec_> const exec = "exec";
Václav Kubernát1d593902021-03-08 08:55:16 +010034x3::rule<switch_class, switch_> const switch_rule = "switch";
Václav Kubernáte7248b22020-06-26 15:38:59 +020035x3::rule<cancel_class, cancel_> const cancel = "cancel";
Václav Kubernátb61336d2018-05-28 17:35:03 +020036x3::rule<command_class, command_> const command = "command";
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020037
Václav Kubernát57272422019-02-08 12:48:24 +010038x3::rule<createCommandSuggestions_class, x3::unused_type> const createCommandSuggestions = "createCommandSuggestions";
Václav Kubernát5c75b252018-10-10 18:33:47 +020039
Václav Kubernát41378452018-06-06 16:29:40 +020040#if __clang__
41#pragma GCC diagnostic push
42#pragma GCC diagnostic ignored "-Woverloaded-shift-op-parentheses"
43#endif
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020044
Václav Kubernát509ce652019-05-29 19:46:44 +020045namespace ascii = boost::spirit::x3::ascii;
46
Václav Kubernáte7d4aea2018-09-11 18:15:48 +020047struct ls_options_table : x3::symbols<LsOption> {
48 ls_options_table()
49 {
Václav Kubernátbf083ec2019-02-19 13:58:09 +010050 add
51 ("--recursive", LsOption::Recursive);
Václav Kubernáte7d4aea2018-09-11 18:15:48 +020052 }
53} const ls_options;
54
Václav Kubernát11afac72018-07-18 14:59:53 +020055auto const ls_def =
Václav Kubernát4a58ce62020-05-14 17:58:10 +020056 ls_::name >> *(space_separator >> ls_options) >> -(space_separator >> (anyPath | (module >> "*")));
Václav Kubernát11afac72018-07-18 14:59:53 +020057
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020058auto const cd_def =
Václav Kubernáte7248b22020-06-26 15:38:59 +020059 cd_::name >> space_separator > cdPath;
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020060
Václav Kubernátb61336d2018-05-28 17:35:03 +020061auto const create_def =
Václav Kubernát5b8a8f32020-05-20 00:57:22 +020062 create_::name >> space_separator > (presenceContainerPath | listInstancePath | leafListElementPath);
Václav Kubernátb61336d2018-05-28 17:35:03 +020063
64auto const delete_rule_def =
Jan Kundrátb7206ad2020-06-18 21:08:14 +020065 delete_::name >> space_separator > (presenceContainerPath | listInstancePath | leafListElementPath | writableLeafPath);
Václav Kubernát07204242018-06-04 18:12:09 +020066
Václav Kubernátb6ff0b62018-08-30 16:14:53 +020067auto const get_def =
Václav Kubernát31029682020-10-29 08:23:04 +010068 get_::name >> -(space_separator >> getPath);
Václav Kubernátb6ff0b62018-08-30 16:14:53 +020069
Václav Kubernát07204242018-06-04 18:12:09 +020070auto const set_def =
Václav Kubernátabf52802020-05-19 01:31:17 +020071 set_::name >> space_separator > writableLeafPath > space_separator > leaf_data;
Václav Kubernátb61336d2018-05-28 17:35:03 +020072
Václav Kubernát812ee282018-08-30 17:10:03 +020073auto const commit_def =
Václav Kubernátbf083ec2019-02-19 13:58:09 +010074 commit_::name >> x3::attr(commit_());
Václav Kubernát812ee282018-08-30 17:10:03 +020075
Václav Kubernát6d791432018-10-25 16:00:35 +020076auto const discard_def =
Václav Kubernátbf083ec2019-02-19 13:58:09 +010077 discard_::name >> x3::attr(discard_());
Václav Kubernát6d791432018-10-25 16:00:35 +020078
Václav Kubernát054cc992019-02-21 14:23:52 +010079struct command_names_table : x3::symbols<decltype(help_::m_cmd)> {
80 command_names_table()
81 {
82 boost::mpl::for_each<CommandTypes, boost::type<boost::mpl::_>>([this](auto cmd) {
Václav Kubernát672889c2020-05-27 04:11:36 +020083 add(commandNamesVisitor(cmd), decltype(help_::m_cmd)(cmd));
Václav Kubernát054cc992019-02-21 14:23:52 +010084 });
85 }
86} const command_names;
87
88auto const help_def =
89 help_::name > createCommandSuggestions >> -command_names;
90
Václav Kubernát7160a132020-04-03 02:11:01 +020091struct datastore_symbol_table : x3::symbols<Datastore> {
92 datastore_symbol_table()
93 {
94 add
95 ("running", Datastore::Running)
96 ("startup", Datastore::Startup);
97 }
98} const datastore;
99
Václav Kubernát21fc5e22020-11-26 04:28:27 +0100100const auto copy_source = x3::rule<class source, Datastore>{"source datastore"} = datastore;
101const auto copy_destination = x3::rule<class source, Datastore>{"destination datastore"} = datastore;
Václav Kubernát7160a132020-04-03 02:11:01 +0200102
103const auto datastoreSuggestions = x3::eps[([](auto& ctx) {
104 auto& parserContext = x3::get<parser_context_tag>(ctx);
105 parserContext.m_suggestions = {Completion{"running", " "}, Completion{"startup", " "}};
106 parserContext.m_completionIterator = _where(ctx).begin();
107})];
108
109struct copy_args : x3::parser<copy_args> {
110 using attribute_type = copy_;
111 template <typename It, typename Ctx, typename RCtx>
112 bool parse(It& begin, It end, Ctx const& ctx, RCtx& rctx, copy_& attr) const
113 {
114 auto& parserContext = x3::get<parser_context_tag>(ctx);
115 auto iterBeforeDestination = begin;
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100116 auto save_iter = x3::no_skip[x3::eps[([&iterBeforeDestination](auto& ctx) { iterBeforeDestination = _where(ctx).begin(); })]];
Václav Kubernát7160a132020-04-03 02:11:01 +0200117 auto grammar = datastoreSuggestions > copy_source > space_separator > datastoreSuggestions > save_iter > copy_destination;
118
119 try {
120 grammar.parse(begin, end, ctx, rctx, attr);
121 } catch (x3::expectation_failure<It>& ex) {
122 using namespace std::string_literals;
123 parserContext.m_errorMsg = "Expected "s + ex.which() + " here:";
124 throw;
125 }
126
127 if (attr.m_source == attr.m_destination) {
128 begin = iterBeforeDestination; // Restoring the iterator here makes the error caret point to the second datastore
129 parserContext.m_errorMsg = "Source datastore and destination datastore can't be the same.";
130 return false;
131 }
132
133 return true;
134 }
Václav Kubernát21fc5e22020-11-26 04:28:27 +0100135} const copy_args;
Václav Kubernát7160a132020-04-03 02:11:01 +0200136
137auto const copy_def =
138 copy_::name > space_separator > copy_args;
139
Václav Kubernát9cfcd872020-02-18 12:34:02 +0100140auto const describe_def =
Václav Kubernát3200b862020-12-03 02:34:55 +0100141 describe_::name >> space_separator > anyPath;
Václav Kubernát9cfcd872020-02-18 12:34:02 +0100142
Václav Kubernát162165e2021-02-22 09:46:53 +0100143struct move_mode_table : x3::symbols<MoveMode> {
144 move_mode_table()
Václav Kubernátbf65dd72020-05-28 02:32:31 +0200145 {
146 add
147 ("after", MoveMode::After)
148 ("before", MoveMode::Before)
149 ("begin", MoveMode::Begin)
150 ("end", MoveMode::End);
151 }
Václav Kubernát162165e2021-02-22 09:46:53 +0100152} const move_mode_table;
Václav Kubernátbf65dd72020-05-28 02:32:31 +0200153
154struct move_absolute_table : x3::symbols<yang::move::Absolute> {
155 move_absolute_table()
156 {
157 add
158 ("begin", yang::move::Absolute::Begin)
159 ("end", yang::move::Absolute::End);
160 }
161} const move_absolute_table;
162
163struct move_relative_table : x3::symbols<yang::move::Relative::Position> {
164 move_relative_table()
165 {
166 add
167 ("before", yang::move::Relative::Position::Before)
168 ("after", yang::move::Relative::Position::After);
169 }
170} const move_relative_table;
171
172struct move_args : x3::parser<move_args> {
173 using attribute_type = move_;
174 template <typename It, typename Ctx, typename RCtx>
175 bool parse(It& begin, It end, Ctx const& ctx, RCtx& rctx, move_& attr) const
176 {
177 ParserContext& parserContext = x3::get<parser_context_tag>(ctx);
178 dataPath_ movePath;
179 auto movePathGrammar = listInstancePath | leafListElementPath;
180 auto res = movePathGrammar.parse(begin, end, ctx, rctx, attr.m_source);
181 if (!res) {
182 parserContext.m_errorMsg = "Expected source path here:";
183 return false;
184 }
185
186 // Try absolute move first.
187 res = (space_separator >> move_absolute_table).parse(begin, end, ctx, rctx, attr.m_destination);
188 if (res) {
189 // Absolute move parsing succeeded, we don't need to parse anything else.
190 return true;
191 }
192
193 // If absolute move didn't succeed, try relative.
194 attr.m_destination = yang::move::Relative{};
195 res = (space_separator >> move_relative_table).parse(begin, end, ctx, rctx, std::get<yang::move::Relative>(attr.m_destination).m_position);
196
197 if (!res) {
198 parserContext.m_errorMsg = "Expected a move position (begin, end, before, after) here:";
199 return false;
200 }
201
202 if (std::holds_alternative<leafListElement_>(attr.m_source.m_nodes.back().m_suffix)) {
203 leaf_data_ value;
204 res = (space_separator >> leaf_data).parse(begin, end, ctx, rctx, value);
205 if (res) {
206 std::get<yang::move::Relative>(attr.m_destination).m_path = {{".", value}};
207 }
208 } else {
209 ListInstance listInstance;
210 // The source list instance will be stored inside the parser context path.
211 // The source list instance will be full data path (with keys included).
212 // However, m_tmpListPath is supposed to store a path with a list without the keys.
213 // So, I pop the last listElement_ (which has the keys) and put in a list_ (which doesn't have the keys).
214 // Example: /mod:cont/protocols[name='ftp'] gets turned into /mod:cont/protocols
215 parserContext.m_tmpListPath = parserContext.currentDataPath();
216 parserContext.m_tmpListPath.m_nodes.pop_back();
217 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 +0200218 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 +0200219
220 res = (space_separator >> listSuffix).parse(begin, end, ctx, rctx, listInstance);
221 if (res) {
222 std::get<yang::move::Relative>(attr.m_destination).m_path = listInstance;
223 }
224 }
225
226 if (!res) {
227 parserContext.m_errorMsg = "Expected a destination here:";
228 }
229
230 return res;
231 }
232} const move_args;
233
234auto const move_def =
235 move_::name >> space_separator >> move_args;
236
Václav Kubernát70d7f7a2020-06-23 14:40:40 +0200237struct format_table : x3::symbols<DataFormat> {
238 format_table()
239 {
240 add
241 ("xml", DataFormat::Xml)
242 ("json", DataFormat::Json);
243 }
244} const format_table;
245
Václav Kubernát3b5e6522020-06-26 18:36:09 +0200246struct dump_args : x3::parser<dump_args> {
247 using attribute_type = dump_;
248 template <typename It, typename Ctx, typename RCtx>
249 bool parse(It& begin, It end, Ctx const& ctx, RCtx& rctx, dump_& attr) const
250 {
251 ParserContext& parserContext = x3::get<parser_context_tag>(ctx);
252 parserContext.m_suggestions = {{"xml"}, {"json"}};
253 parserContext.m_completionIterator = begin;
254 auto res = format_table.parse(begin, end, ctx, rctx, attr);
255 if (!res) {
256 parserContext.m_errorMsg = "Expected a data format (xml, json) here:";
257 }
258 return res;
259 }
260} const dump_args;
Václav Kubernát70d7f7a2020-06-23 14:40:40 +0200261
Václav Kubernátaa4250a2020-07-22 00:02:23 +0200262auto const prepare_def =
Václav Kubernátd8408e02020-12-02 05:13:27 +0100263 prepare_::name > space_separator > as<dataPath_>[RpcActionPath<AllowInput::Yes>{}];
Václav Kubernáte7248b22020-06-26 15:38:59 +0200264
265auto const exec_def =
Václav Kubernátaff1f492021-02-17 10:56:28 +0100266 exec_::name > -(space_separator > -as<dataPath_>[RpcActionPath<AllowInput::No>{}]);
Václav Kubernáte7248b22020-06-26 15:38:59 +0200267
Václav Kubernát162165e2021-02-22 09:46:53 +0100268const auto dsTargetSuggestions = x3::eps[([](auto& ctx) {
269 auto& parserContext = x3::get<parser_context_tag>(ctx);
270 parserContext.m_suggestions = {Completion{"running", " "}, Completion{"startup", " "}, Completion{"operational", " "}};
271 parserContext.m_completionIterator = _where(ctx).begin();
272})];
273
274struct ds_target_table : x3::symbols<DatastoreTarget> {
275 ds_target_table()
276 {
277 add
278 ("operational", DatastoreTarget::Operational)
279 ("startup", DatastoreTarget::Startup)
280 ("running", DatastoreTarget::Running);
281 }
282} const ds_target_table;
283
284auto const switch_rule_def =
Václav Kubernát1d593902021-03-08 08:55:16 +0100285 switch_::name > space_separator > as<x3::unused_type>[dsTargetSuggestions] > ds_target_table;
Václav Kubernát162165e2021-02-22 09:46:53 +0100286
Václav Kubernáte7248b22020-06-26 15:38:59 +0200287auto const cancel_def =
288 cancel_::name >> x3::attr(cancel_{});
289
Václav Kubernát70d7f7a2020-06-23 14:40:40 +0200290auto const dump_def =
Václav Kubernát3b5e6522020-06-26 18:36:09 +0200291 dump_::name > space_separator >> dump_args;
Václav Kubernát70d7f7a2020-06-23 14:40:40 +0200292
Václav Kubernát57272422019-02-08 12:48:24 +0100293auto const createCommandSuggestions_def =
Václav Kubernátbf083ec2019-02-19 13:58:09 +0100294 x3::eps;
Václav Kubernát57272422019-02-08 12:48:24 +0100295
Václav Kubernátb61336d2018-05-28 17:35:03 +0200296auto const command_def =
Václav Kubernát162165e2021-02-22 09:46:53 +0100297 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 +0200298
299#if __clang__
300#pragma GCC diagnostic pop
301#endif
Václav Kubernátb61336d2018-05-28 17:35:03 +0200302
Václav Kubernát07204242018-06-04 18:12:09 +0200303BOOST_SPIRIT_DEFINE(set)
Václav Kubernát812ee282018-08-30 17:10:03 +0200304BOOST_SPIRIT_DEFINE(commit)
Václav Kubernátb6ff0b62018-08-30 16:14:53 +0200305BOOST_SPIRIT_DEFINE(get)
Václav Kubernát11afac72018-07-18 14:59:53 +0200306BOOST_SPIRIT_DEFINE(ls)
Václav Kubernát6d791432018-10-25 16:00:35 +0200307BOOST_SPIRIT_DEFINE(discard)
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200308BOOST_SPIRIT_DEFINE(cd)
Václav Kubernátb61336d2018-05-28 17:35:03 +0200309BOOST_SPIRIT_DEFINE(create)
310BOOST_SPIRIT_DEFINE(delete_rule)
Václav Kubernát9cfcd872020-02-18 12:34:02 +0100311BOOST_SPIRIT_DEFINE(describe)
Václav Kubernát054cc992019-02-21 14:23:52 +0100312BOOST_SPIRIT_DEFINE(help)
Václav Kubernát7160a132020-04-03 02:11:01 +0200313BOOST_SPIRIT_DEFINE(copy)
Václav Kubernátbf65dd72020-05-28 02:32:31 +0200314BOOST_SPIRIT_DEFINE(move)
Václav Kubernát70d7f7a2020-06-23 14:40:40 +0200315BOOST_SPIRIT_DEFINE(dump)
Václav Kubernátaa4250a2020-07-22 00:02:23 +0200316BOOST_SPIRIT_DEFINE(prepare)
Václav Kubernáte7248b22020-06-26 15:38:59 +0200317BOOST_SPIRIT_DEFINE(exec)
Václav Kubernát162165e2021-02-22 09:46:53 +0100318BOOST_SPIRIT_DEFINE(switch_rule)
Václav Kubernáte7248b22020-06-26 15:38:59 +0200319BOOST_SPIRIT_DEFINE(cancel)
Václav Kubernátb61336d2018-05-28 17:35:03 +0200320BOOST_SPIRIT_DEFINE(command)
Václav Kubernát57272422019-02-08 12:48:24 +0100321BOOST_SPIRIT_DEFINE(createCommandSuggestions)