blob: 8d18340d4f40868f5c8194ddd51b654d7d863791 [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átb61336d2018-05-28 17:35:03 +020032x3::rule<command_class, command_> const command = "command";
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020033
Václav Kubernát57272422019-02-08 12:48:24 +010034x3::rule<createCommandSuggestions_class, x3::unused_type> const createCommandSuggestions = "createCommandSuggestions";
Václav Kubernát5c75b252018-10-10 18:33:47 +020035
Václav Kubernát41378452018-06-06 16:29:40 +020036#if __clang__
37#pragma GCC diagnostic push
38#pragma GCC diagnostic ignored "-Woverloaded-shift-op-parentheses"
39#endif
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020040
Václav Kubernát509ce652019-05-29 19:46:44 +020041namespace ascii = boost::spirit::x3::ascii;
42
Václav Kubernáte7d4aea2018-09-11 18:15:48 +020043struct ls_options_table : x3::symbols<LsOption> {
44 ls_options_table()
45 {
Václav Kubernátbf083ec2019-02-19 13:58:09 +010046 add
47 ("--recursive", LsOption::Recursive);
Václav Kubernáte7d4aea2018-09-11 18:15:48 +020048 }
49} const ls_options;
50
Václav Kubernát11afac72018-07-18 14:59:53 +020051auto const ls_def =
Václav Kubernát4a58ce62020-05-14 17:58:10 +020052 ls_::name >> *(space_separator >> ls_options) >> -(space_separator >> (anyPath | (module >> "*")));
Václav Kubernát11afac72018-07-18 14:59:53 +020053
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020054auto const cd_def =
Václav Kubernátbf083ec2019-02-19 13:58:09 +010055 cd_::name >> space_separator > dataPath;
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020056
Václav Kubernátb61336d2018-05-28 17:35:03 +020057auto const create_def =
Václav Kubernát5b8a8f32020-05-20 00:57:22 +020058 create_::name >> space_separator > (presenceContainerPath | listInstancePath | leafListElementPath);
Václav Kubernátb61336d2018-05-28 17:35:03 +020059
60auto const delete_rule_def =
Jan Kundrátb7206ad2020-06-18 21:08:14 +020061 delete_::name >> space_separator > (presenceContainerPath | listInstancePath | leafListElementPath | writableLeafPath);
Václav Kubernát07204242018-06-04 18:12:09 +020062
Václav Kubernátb6ff0b62018-08-30 16:14:53 +020063auto const get_def =
Václav Kubernát9456b5c2019-10-02 21:14:52 +020064 get_::name >> -(space_separator >> ((dataPathListEnd | dataPath) | (module >> "*")));
Václav Kubernátb6ff0b62018-08-30 16:14:53 +020065
Václav Kubernát07204242018-06-04 18:12:09 +020066auto const set_def =
Václav Kubernátabf52802020-05-19 01:31:17 +020067 set_::name >> space_separator > writableLeafPath > space_separator > leaf_data;
Václav Kubernátb61336d2018-05-28 17:35:03 +020068
Václav Kubernát812ee282018-08-30 17:10:03 +020069auto const commit_def =
Václav Kubernátbf083ec2019-02-19 13:58:09 +010070 commit_::name >> x3::attr(commit_());
Václav Kubernát812ee282018-08-30 17:10:03 +020071
Václav Kubernát6d791432018-10-25 16:00:35 +020072auto const discard_def =
Václav Kubernátbf083ec2019-02-19 13:58:09 +010073 discard_::name >> x3::attr(discard_());
Václav Kubernát6d791432018-10-25 16:00:35 +020074
Václav Kubernát054cc992019-02-21 14:23:52 +010075struct command_names_table : x3::symbols<decltype(help_::m_cmd)> {
76 command_names_table()
77 {
78 boost::mpl::for_each<CommandTypes, boost::type<boost::mpl::_>>([this](auto cmd) {
Václav Kubernát672889c2020-05-27 04:11:36 +020079 add(commandNamesVisitor(cmd), decltype(help_::m_cmd)(cmd));
Václav Kubernát054cc992019-02-21 14:23:52 +010080 });
81 }
82} const command_names;
83
84auto const help_def =
85 help_::name > createCommandSuggestions >> -command_names;
86
Václav Kubernát7160a132020-04-03 02:11:01 +020087struct datastore_symbol_table : x3::symbols<Datastore> {
88 datastore_symbol_table()
89 {
90 add
91 ("running", Datastore::Running)
92 ("startup", Datastore::Startup);
93 }
94} const datastore;
95
96auto copy_source = x3::rule<class source, Datastore>{"source datastore"} = datastore;
97auto copy_destination = x3::rule<class source, Datastore>{"destination datastore"} = datastore;
98
99const auto datastoreSuggestions = x3::eps[([](auto& ctx) {
100 auto& parserContext = x3::get<parser_context_tag>(ctx);
101 parserContext.m_suggestions = {Completion{"running", " "}, Completion{"startup", " "}};
102 parserContext.m_completionIterator = _where(ctx).begin();
103})];
104
105struct copy_args : x3::parser<copy_args> {
106 using attribute_type = copy_;
107 template <typename It, typename Ctx, typename RCtx>
108 bool parse(It& begin, It end, Ctx const& ctx, RCtx& rctx, copy_& attr) const
109 {
110 auto& parserContext = x3::get<parser_context_tag>(ctx);
111 auto iterBeforeDestination = begin;
112 auto save_iter = x3::no_skip[x3::eps[([&iterBeforeDestination](auto& ctx) {iterBeforeDestination = _where(ctx).begin();})]];
113 auto grammar = datastoreSuggestions > copy_source > space_separator > datastoreSuggestions > save_iter > copy_destination;
114
115 try {
116 grammar.parse(begin, end, ctx, rctx, attr);
117 } catch (x3::expectation_failure<It>& ex) {
118 using namespace std::string_literals;
119 parserContext.m_errorMsg = "Expected "s + ex.which() + " here:";
120 throw;
121 }
122
123 if (attr.m_source == attr.m_destination) {
124 begin = iterBeforeDestination; // Restoring the iterator here makes the error caret point to the second datastore
125 parserContext.m_errorMsg = "Source datastore and destination datastore can't be the same.";
126 return false;
127 }
128
129 return true;
130 }
131} copy_args;
132
133auto const copy_def =
134 copy_::name > space_separator > copy_args;
135
Václav Kubernát9cfcd872020-02-18 12:34:02 +0100136auto const describe_def =
Václav Kubernát4618fa42020-05-07 01:33:19 +0200137 describe_::name >> space_separator > (dataPathListEnd | anyPath);
Václav Kubernát9cfcd872020-02-18 12:34:02 +0100138
Václav Kubernátbf65dd72020-05-28 02:32:31 +0200139struct mode_table : x3::symbols<MoveMode> {
140 mode_table()
141 {
142 add
143 ("after", MoveMode::After)
144 ("before", MoveMode::Before)
145 ("begin", MoveMode::Begin)
146 ("end", MoveMode::End);
147 }
148} const mode_table;
149
150struct move_absolute_table : x3::symbols<yang::move::Absolute> {
151 move_absolute_table()
152 {
153 add
154 ("begin", yang::move::Absolute::Begin)
155 ("end", yang::move::Absolute::End);
156 }
157} const move_absolute_table;
158
159struct move_relative_table : x3::symbols<yang::move::Relative::Position> {
160 move_relative_table()
161 {
162 add
163 ("before", yang::move::Relative::Position::Before)
164 ("after", yang::move::Relative::Position::After);
165 }
166} const move_relative_table;
167
168struct move_args : x3::parser<move_args> {
169 using attribute_type = move_;
170 template <typename It, typename Ctx, typename RCtx>
171 bool parse(It& begin, It end, Ctx const& ctx, RCtx& rctx, move_& attr) const
172 {
173 ParserContext& parserContext = x3::get<parser_context_tag>(ctx);
174 dataPath_ movePath;
175 auto movePathGrammar = listInstancePath | leafListElementPath;
176 auto res = movePathGrammar.parse(begin, end, ctx, rctx, attr.m_source);
177 if (!res) {
178 parserContext.m_errorMsg = "Expected source path here:";
179 return false;
180 }
181
182 // Try absolute move first.
183 res = (space_separator >> move_absolute_table).parse(begin, end, ctx, rctx, attr.m_destination);
184 if (res) {
185 // Absolute move parsing succeeded, we don't need to parse anything else.
186 return true;
187 }
188
189 // If absolute move didn't succeed, try relative.
190 attr.m_destination = yang::move::Relative{};
191 res = (space_separator >> move_relative_table).parse(begin, end, ctx, rctx, std::get<yang::move::Relative>(attr.m_destination).m_position);
192
193 if (!res) {
194 parserContext.m_errorMsg = "Expected a move position (begin, end, before, after) here:";
195 return false;
196 }
197
198 if (std::holds_alternative<leafListElement_>(attr.m_source.m_nodes.back().m_suffix)) {
199 leaf_data_ value;
200 res = (space_separator >> leaf_data).parse(begin, end, ctx, rctx, value);
201 if (res) {
202 std::get<yang::move::Relative>(attr.m_destination).m_path = {{".", value}};
203 }
204 } else {
205 ListInstance listInstance;
206 // The source list instance will be stored inside the parser context path.
207 // The source list instance will be full data path (with keys included).
208 // However, m_tmpListPath is supposed to store a path with a list without the keys.
209 // So, I pop the last listElement_ (which has the keys) and put in a list_ (which doesn't have the keys).
210 // Example: /mod:cont/protocols[name='ftp'] gets turned into /mod:cont/protocols
211 parserContext.m_tmpListPath = parserContext.currentDataPath();
212 parserContext.m_tmpListPath.m_nodes.pop_back();
213 auto list = list_{std::get<listElement_>(attr.m_source.m_nodes.back().m_suffix).m_name};
214 parserContext.m_tmpListPath.m_nodes.push_back(dataNode_{attr.m_source.m_nodes.back().m_prefix, list});
215
216 res = (space_separator >> listSuffix).parse(begin, end, ctx, rctx, listInstance);
217 if (res) {
218 std::get<yang::move::Relative>(attr.m_destination).m_path = listInstance;
219 }
220 }
221
222 if (!res) {
223 parserContext.m_errorMsg = "Expected a destination here:";
224 }
225
226 return res;
227 }
228} const move_args;
229
230auto const move_def =
231 move_::name >> space_separator >> move_args;
232
Václav Kubernát70d7f7a2020-06-23 14:40:40 +0200233struct format_table : x3::symbols<DataFormat> {
234 format_table()
235 {
236 add
237 ("xml", DataFormat::Xml)
238 ("json", DataFormat::Json);
239 }
240} const format_table;
241
242
243auto const dump_def =
244 dump_::name >> space_separator >> format_table;
245
Václav Kubernát57272422019-02-08 12:48:24 +0100246auto const createCommandSuggestions_def =
Václav Kubernátbf083ec2019-02-19 13:58:09 +0100247 x3::eps;
Václav Kubernát57272422019-02-08 12:48:24 +0100248
Václav Kubernátb61336d2018-05-28 17:35:03 +0200249auto const command_def =
Václav Kubernát70d7f7a2020-06-23 14:40:40 +0200250 createCommandSuggestions >> x3::expect[cd | copy | create | delete_rule | set | commit | get | ls | discard | describe | help | move | dump];
Václav Kubernát41378452018-06-06 16:29:40 +0200251
252#if __clang__
253#pragma GCC diagnostic pop
254#endif
Václav Kubernátb61336d2018-05-28 17:35:03 +0200255
Václav Kubernát07204242018-06-04 18:12:09 +0200256BOOST_SPIRIT_DEFINE(set)
Václav Kubernát812ee282018-08-30 17:10:03 +0200257BOOST_SPIRIT_DEFINE(commit)
Václav Kubernátb6ff0b62018-08-30 16:14:53 +0200258BOOST_SPIRIT_DEFINE(get)
Václav Kubernát11afac72018-07-18 14:59:53 +0200259BOOST_SPIRIT_DEFINE(ls)
Václav Kubernát6d791432018-10-25 16:00:35 +0200260BOOST_SPIRIT_DEFINE(discard)
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200261BOOST_SPIRIT_DEFINE(cd)
Václav Kubernátb61336d2018-05-28 17:35:03 +0200262BOOST_SPIRIT_DEFINE(create)
263BOOST_SPIRIT_DEFINE(delete_rule)
Václav Kubernát9cfcd872020-02-18 12:34:02 +0100264BOOST_SPIRIT_DEFINE(describe)
Václav Kubernát054cc992019-02-21 14:23:52 +0100265BOOST_SPIRIT_DEFINE(help)
Václav Kubernát7160a132020-04-03 02:11:01 +0200266BOOST_SPIRIT_DEFINE(copy)
Václav Kubernátbf65dd72020-05-28 02:32:31 +0200267BOOST_SPIRIT_DEFINE(move)
Václav Kubernát70d7f7a2020-06-23 14:40:40 +0200268BOOST_SPIRIT_DEFINE(dump)
Václav Kubernátb61336d2018-05-28 17:35:03 +0200269BOOST_SPIRIT_DEFINE(command)
Václav Kubernát57272422019-02-08 12:48:24 +0100270BOOST_SPIRIT_DEFINE(createCommandSuggestions)