blob: 86dda33801552bc37c27c55db00b90d45ab4a2db [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";
34x3::rule<cancel_class, cancel_> const cancel = "cancel";
Václav Kubernátb61336d2018-05-28 17:35:03 +020035x3::rule<command_class, command_> const command = "command";
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020036
Václav Kubernát57272422019-02-08 12:48:24 +010037x3::rule<createCommandSuggestions_class, x3::unused_type> const createCommandSuggestions = "createCommandSuggestions";
Václav Kubernát5c75b252018-10-10 18:33:47 +020038
Václav Kubernát41378452018-06-06 16:29:40 +020039#if __clang__
40#pragma GCC diagnostic push
41#pragma GCC diagnostic ignored "-Woverloaded-shift-op-parentheses"
42#endif
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020043
Václav Kubernát509ce652019-05-29 19:46:44 +020044namespace ascii = boost::spirit::x3::ascii;
45
Václav Kubernáte7d4aea2018-09-11 18:15:48 +020046struct ls_options_table : x3::symbols<LsOption> {
47 ls_options_table()
48 {
Václav Kubernátbf083ec2019-02-19 13:58:09 +010049 add
50 ("--recursive", LsOption::Recursive);
Václav Kubernáte7d4aea2018-09-11 18:15:48 +020051 }
52} const ls_options;
53
Václav Kubernát11afac72018-07-18 14:59:53 +020054auto const ls_def =
Václav Kubernát4a58ce62020-05-14 17:58:10 +020055 ls_::name >> *(space_separator >> ls_options) >> -(space_separator >> (anyPath | (module >> "*")));
Václav Kubernát11afac72018-07-18 14:59:53 +020056
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020057auto const cd_def =
Václav Kubernáte7248b22020-06-26 15:38:59 +020058 cd_::name >> space_separator > cdPath;
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020059
Václav Kubernátb61336d2018-05-28 17:35:03 +020060auto const create_def =
Václav Kubernát5b8a8f32020-05-20 00:57:22 +020061 create_::name >> space_separator > (presenceContainerPath | listInstancePath | leafListElementPath);
Václav Kubernátb61336d2018-05-28 17:35:03 +020062
63auto const delete_rule_def =
Jan Kundrátb7206ad2020-06-18 21:08:14 +020064 delete_::name >> space_separator > (presenceContainerPath | listInstancePath | leafListElementPath | writableLeafPath);
Václav Kubernát07204242018-06-04 18:12:09 +020065
Václav Kubernátb6ff0b62018-08-30 16:14:53 +020066auto const get_def =
Václav Kubernát9456b5c2019-10-02 21:14:52 +020067 get_::name >> -(space_separator >> ((dataPathListEnd | dataPath) | (module >> "*")));
Václav Kubernátb6ff0b62018-08-30 16:14:53 +020068
Václav Kubernát07204242018-06-04 18:12:09 +020069auto const set_def =
Václav Kubernátabf52802020-05-19 01:31:17 +020070 set_::name >> space_separator > writableLeafPath > space_separator > leaf_data;
Václav Kubernátb61336d2018-05-28 17:35:03 +020071
Václav Kubernát812ee282018-08-30 17:10:03 +020072auto const commit_def =
Václav Kubernátbf083ec2019-02-19 13:58:09 +010073 commit_::name >> x3::attr(commit_());
Václav Kubernát812ee282018-08-30 17:10:03 +020074
Václav Kubernát6d791432018-10-25 16:00:35 +020075auto const discard_def =
Václav Kubernátbf083ec2019-02-19 13:58:09 +010076 discard_::name >> x3::attr(discard_());
Václav Kubernát6d791432018-10-25 16:00:35 +020077
Václav Kubernát054cc992019-02-21 14:23:52 +010078struct command_names_table : x3::symbols<decltype(help_::m_cmd)> {
79 command_names_table()
80 {
81 boost::mpl::for_each<CommandTypes, boost::type<boost::mpl::_>>([this](auto cmd) {
Václav Kubernát672889c2020-05-27 04:11:36 +020082 add(commandNamesVisitor(cmd), decltype(help_::m_cmd)(cmd));
Václav Kubernát054cc992019-02-21 14:23:52 +010083 });
84 }
85} const command_names;
86
87auto const help_def =
88 help_::name > createCommandSuggestions >> -command_names;
89
Václav Kubernát7160a132020-04-03 02:11:01 +020090struct datastore_symbol_table : x3::symbols<Datastore> {
91 datastore_symbol_table()
92 {
93 add
94 ("running", Datastore::Running)
95 ("startup", Datastore::Startup);
96 }
97} const datastore;
98
99auto copy_source = x3::rule<class source, Datastore>{"source datastore"} = datastore;
100auto copy_destination = x3::rule<class source, Datastore>{"destination datastore"} = datastore;
101
102const auto datastoreSuggestions = x3::eps[([](auto& ctx) {
103 auto& parserContext = x3::get<parser_context_tag>(ctx);
104 parserContext.m_suggestions = {Completion{"running", " "}, Completion{"startup", " "}};
105 parserContext.m_completionIterator = _where(ctx).begin();
106})];
107
108struct copy_args : x3::parser<copy_args> {
109 using attribute_type = copy_;
110 template <typename It, typename Ctx, typename RCtx>
111 bool parse(It& begin, It end, Ctx const& ctx, RCtx& rctx, copy_& attr) const
112 {
113 auto& parserContext = x3::get<parser_context_tag>(ctx);
114 auto iterBeforeDestination = begin;
115 auto save_iter = x3::no_skip[x3::eps[([&iterBeforeDestination](auto& ctx) {iterBeforeDestination = _where(ctx).begin();})]];
116 auto grammar = datastoreSuggestions > copy_source > space_separator > datastoreSuggestions > save_iter > copy_destination;
117
118 try {
119 grammar.parse(begin, end, ctx, rctx, attr);
120 } catch (x3::expectation_failure<It>& ex) {
121 using namespace std::string_literals;
122 parserContext.m_errorMsg = "Expected "s + ex.which() + " here:";
123 throw;
124 }
125
126 if (attr.m_source == attr.m_destination) {
127 begin = iterBeforeDestination; // Restoring the iterator here makes the error caret point to the second datastore
128 parserContext.m_errorMsg = "Source datastore and destination datastore can't be the same.";
129 return false;
130 }
131
132 return true;
133 }
134} copy_args;
135
136auto const copy_def =
137 copy_::name > space_separator > copy_args;
138
Václav Kubernát9cfcd872020-02-18 12:34:02 +0100139auto const describe_def =
Václav Kubernát4618fa42020-05-07 01:33:19 +0200140 describe_::name >> space_separator > (dataPathListEnd | anyPath);
Václav Kubernát9cfcd872020-02-18 12:34:02 +0100141
Václav Kubernátbf65dd72020-05-28 02:32:31 +0200142struct mode_table : x3::symbols<MoveMode> {
143 mode_table()
144 {
145 add
146 ("after", MoveMode::After)
147 ("before", MoveMode::Before)
148 ("begin", MoveMode::Begin)
149 ("end", MoveMode::End);
150 }
151} const mode_table;
152
153struct move_absolute_table : x3::symbols<yang::move::Absolute> {
154 move_absolute_table()
155 {
156 add
157 ("begin", yang::move::Absolute::Begin)
158 ("end", yang::move::Absolute::End);
159 }
160} const move_absolute_table;
161
162struct move_relative_table : x3::symbols<yang::move::Relative::Position> {
163 move_relative_table()
164 {
165 add
166 ("before", yang::move::Relative::Position::Before)
167 ("after", yang::move::Relative::Position::After);
168 }
169} const move_relative_table;
170
171struct move_args : x3::parser<move_args> {
172 using attribute_type = move_;
173 template <typename It, typename Ctx, typename RCtx>
174 bool parse(It& begin, It end, Ctx const& ctx, RCtx& rctx, move_& attr) const
175 {
176 ParserContext& parserContext = x3::get<parser_context_tag>(ctx);
177 dataPath_ movePath;
178 auto movePathGrammar = listInstancePath | leafListElementPath;
179 auto res = movePathGrammar.parse(begin, end, ctx, rctx, attr.m_source);
180 if (!res) {
181 parserContext.m_errorMsg = "Expected source path here:";
182 return false;
183 }
184
185 // Try absolute move first.
186 res = (space_separator >> move_absolute_table).parse(begin, end, ctx, rctx, attr.m_destination);
187 if (res) {
188 // Absolute move parsing succeeded, we don't need to parse anything else.
189 return true;
190 }
191
192 // If absolute move didn't succeed, try relative.
193 attr.m_destination = yang::move::Relative{};
194 res = (space_separator >> move_relative_table).parse(begin, end, ctx, rctx, std::get<yang::move::Relative>(attr.m_destination).m_position);
195
196 if (!res) {
197 parserContext.m_errorMsg = "Expected a move position (begin, end, before, after) here:";
198 return false;
199 }
200
201 if (std::holds_alternative<leafListElement_>(attr.m_source.m_nodes.back().m_suffix)) {
202 leaf_data_ value;
203 res = (space_separator >> leaf_data).parse(begin, end, ctx, rctx, value);
204 if (res) {
205 std::get<yang::move::Relative>(attr.m_destination).m_path = {{".", value}};
206 }
207 } else {
208 ListInstance listInstance;
209 // The source list instance will be stored inside the parser context path.
210 // The source list instance will be full data path (with keys included).
211 // However, m_tmpListPath is supposed to store a path with a list without the keys.
212 // So, I pop the last listElement_ (which has the keys) and put in a list_ (which doesn't have the keys).
213 // Example: /mod:cont/protocols[name='ftp'] gets turned into /mod:cont/protocols
214 parserContext.m_tmpListPath = parserContext.currentDataPath();
215 parserContext.m_tmpListPath.m_nodes.pop_back();
216 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 +0200217 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 +0200218
219 res = (space_separator >> listSuffix).parse(begin, end, ctx, rctx, listInstance);
220 if (res) {
221 std::get<yang::move::Relative>(attr.m_destination).m_path = listInstance;
222 }
223 }
224
225 if (!res) {
226 parserContext.m_errorMsg = "Expected a destination here:";
227 }
228
229 return res;
230 }
231} const move_args;
232
233auto const move_def =
234 move_::name >> space_separator >> move_args;
235
Václav Kubernát70d7f7a2020-06-23 14:40:40 +0200236struct format_table : x3::symbols<DataFormat> {
237 format_table()
238 {
239 add
240 ("xml", DataFormat::Xml)
241 ("json", DataFormat::Json);
242 }
243} const format_table;
244
Václav Kubernát3b5e6522020-06-26 18:36:09 +0200245struct dump_args : x3::parser<dump_args> {
246 using attribute_type = dump_;
247 template <typename It, typename Ctx, typename RCtx>
248 bool parse(It& begin, It end, Ctx const& ctx, RCtx& rctx, dump_& attr) const
249 {
250 ParserContext& parserContext = x3::get<parser_context_tag>(ctx);
251 parserContext.m_suggestions = {{"xml"}, {"json"}};
252 parserContext.m_completionIterator = begin;
253 auto res = format_table.parse(begin, end, ctx, rctx, attr);
254 if (!res) {
255 parserContext.m_errorMsg = "Expected a data format (xml, json) here:";
256 }
257 return res;
258 }
259} const dump_args;
Václav Kubernát70d7f7a2020-06-23 14:40:40 +0200260
Václav Kubernátaa4250a2020-07-22 00:02:23 +0200261auto const prepare_def =
262 prepare_::name > space_separator > rpcActionPath;
Václav Kubernáte7248b22020-06-26 15:38:59 +0200263
264auto const exec_def =
265 exec_::name >> x3::attr(exec_{});
266
267auto const cancel_def =
268 cancel_::name >> x3::attr(cancel_{});
269
Václav Kubernát70d7f7a2020-06-23 14:40:40 +0200270auto const dump_def =
Václav Kubernát3b5e6522020-06-26 18:36:09 +0200271 dump_::name > space_separator >> dump_args;
Václav Kubernát70d7f7a2020-06-23 14:40:40 +0200272
Václav Kubernát57272422019-02-08 12:48:24 +0100273auto const createCommandSuggestions_def =
Václav Kubernátbf083ec2019-02-19 13:58:09 +0100274 x3::eps;
Václav Kubernát57272422019-02-08 12:48:24 +0100275
Václav Kubernátb61336d2018-05-28 17:35:03 +0200276auto const command_def =
Václav Kubernátaa4250a2020-07-22 00:02:23 +0200277 createCommandSuggestions >> x3::expect[cd | copy | create | delete_rule | set | commit | get | ls | discard | describe | help | move | dump | prepare | exec | cancel];
Václav Kubernát41378452018-06-06 16:29:40 +0200278
279#if __clang__
280#pragma GCC diagnostic pop
281#endif
Václav Kubernátb61336d2018-05-28 17:35:03 +0200282
Václav Kubernát07204242018-06-04 18:12:09 +0200283BOOST_SPIRIT_DEFINE(set)
Václav Kubernát812ee282018-08-30 17:10:03 +0200284BOOST_SPIRIT_DEFINE(commit)
Václav Kubernátb6ff0b62018-08-30 16:14:53 +0200285BOOST_SPIRIT_DEFINE(get)
Václav Kubernát11afac72018-07-18 14:59:53 +0200286BOOST_SPIRIT_DEFINE(ls)
Václav Kubernát6d791432018-10-25 16:00:35 +0200287BOOST_SPIRIT_DEFINE(discard)
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200288BOOST_SPIRIT_DEFINE(cd)
Václav Kubernátb61336d2018-05-28 17:35:03 +0200289BOOST_SPIRIT_DEFINE(create)
290BOOST_SPIRIT_DEFINE(delete_rule)
Václav Kubernát9cfcd872020-02-18 12:34:02 +0100291BOOST_SPIRIT_DEFINE(describe)
Václav Kubernát054cc992019-02-21 14:23:52 +0100292BOOST_SPIRIT_DEFINE(help)
Václav Kubernát7160a132020-04-03 02:11:01 +0200293BOOST_SPIRIT_DEFINE(copy)
Václav Kubernátbf65dd72020-05-28 02:32:31 +0200294BOOST_SPIRIT_DEFINE(move)
Václav Kubernát70d7f7a2020-06-23 14:40:40 +0200295BOOST_SPIRIT_DEFINE(dump)
Václav Kubernátaa4250a2020-07-22 00:02:23 +0200296BOOST_SPIRIT_DEFINE(prepare)
Václav Kubernáte7248b22020-06-26 15:38:59 +0200297BOOST_SPIRIT_DEFINE(exec)
298BOOST_SPIRIT_DEFINE(cancel)
Václav Kubernátb61336d2018-05-28 17:35:03 +0200299BOOST_SPIRIT_DEFINE(command)
Václav Kubernát57272422019-02-08 12:48:24 +0100300BOOST_SPIRIT_DEFINE(createCommandSuggestions)