Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 1 | /* |
| 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át | 509ce65 | 2019-05-29 19:46:44 +0200 | [diff] [blame] | 11 | #include <boost/spirit/home/x3.hpp> |
Václav Kubernát | 24df80e | 2018-06-06 15:18:03 +0200 | [diff] [blame] | 12 | #include "ast_commands.hpp" |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 13 | #include "ast_handlers.hpp" |
Václav Kubernát | 9ae8cc4 | 2020-03-25 19:17:41 +0100 | [diff] [blame] | 14 | #include "common_parsers.hpp" |
| 15 | #include "leaf_data.hpp" |
Václav Kubernát | d0ea9b2 | 2020-04-24 00:44:15 +0200 | [diff] [blame] | 16 | #include "path_parser.hpp" |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 17 | |
Václav Kubernát | 60d6f29 | 2018-05-25 09:45:32 +0200 | [diff] [blame] | 18 | |
Václav Kubernát | 6d79143 | 2018-10-25 16:00:35 +0200 | [diff] [blame] | 19 | x3::rule<discard_class, discard_> const discard = "discard"; |
Václav Kubernát | 11afac7 | 2018-07-18 14:59:53 +0200 | [diff] [blame] | 20 | x3::rule<ls_class, ls_> const ls = "ls"; |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 21 | x3::rule<cd_class, cd_> const cd = "cd"; |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 22 | x3::rule<set_class, set_> const set = "set"; |
Václav Kubernát | b6ff0b6 | 2018-08-30 16:14:53 +0200 | [diff] [blame] | 23 | x3::rule<get_class, get_> const get = "get"; |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 24 | x3::rule<create_class, create_> const create = "create"; |
| 25 | x3::rule<delete_class, delete_> const delete_rule = "delete_rule"; |
Václav Kubernát | 812ee28 | 2018-08-30 17:10:03 +0200 | [diff] [blame] | 26 | x3::rule<commit_class, commit_> const commit = "commit"; |
Václav Kubernát | 9cfcd87 | 2020-02-18 12:34:02 +0100 | [diff] [blame] | 27 | x3::rule<describe_class, describe_> const describe = "describe"; |
Václav Kubernát | 054cc99 | 2019-02-21 14:23:52 +0100 | [diff] [blame] | 28 | x3::rule<help_class, help_> const help = "help"; |
Václav Kubernát | 7160a13 | 2020-04-03 02:11:01 +0200 | [diff] [blame] | 29 | x3::rule<copy_class, copy_> const copy = "copy"; |
Václav Kubernát | bf65dd7 | 2020-05-28 02:32:31 +0200 | [diff] [blame] | 30 | x3::rule<move_class, move_> const move = "move"; |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 31 | x3::rule<command_class, command_> const command = "command"; |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 32 | |
Václav Kubernát | 5727242 | 2019-02-08 12:48:24 +0100 | [diff] [blame] | 33 | x3::rule<createCommandSuggestions_class, x3::unused_type> const createCommandSuggestions = "createCommandSuggestions"; |
Václav Kubernát | 5c75b25 | 2018-10-10 18:33:47 +0200 | [diff] [blame] | 34 | |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame] | 35 | #if __clang__ |
| 36 | #pragma GCC diagnostic push |
| 37 | #pragma GCC diagnostic ignored "-Woverloaded-shift-op-parentheses" |
| 38 | #endif |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 39 | |
Václav Kubernát | 509ce65 | 2019-05-29 19:46:44 +0200 | [diff] [blame] | 40 | namespace ascii = boost::spirit::x3::ascii; |
| 41 | |
Václav Kubernát | e7d4aea | 2018-09-11 18:15:48 +0200 | [diff] [blame] | 42 | struct ls_options_table : x3::symbols<LsOption> { |
| 43 | ls_options_table() |
| 44 | { |
Václav Kubernát | bf083ec | 2019-02-19 13:58:09 +0100 | [diff] [blame] | 45 | add |
| 46 | ("--recursive", LsOption::Recursive); |
Václav Kubernát | e7d4aea | 2018-09-11 18:15:48 +0200 | [diff] [blame] | 47 | } |
| 48 | } const ls_options; |
| 49 | |
Václav Kubernát | 11afac7 | 2018-07-18 14:59:53 +0200 | [diff] [blame] | 50 | auto const ls_def = |
Václav Kubernát | 4a58ce6 | 2020-05-14 17:58:10 +0200 | [diff] [blame] | 51 | ls_::name >> *(space_separator >> ls_options) >> -(space_separator >> (anyPath | (module >> "*"))); |
Václav Kubernát | 11afac7 | 2018-07-18 14:59:53 +0200 | [diff] [blame] | 52 | |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 53 | auto const cd_def = |
Václav Kubernát | bf083ec | 2019-02-19 13:58:09 +0100 | [diff] [blame] | 54 | cd_::name >> space_separator > dataPath; |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 55 | |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 56 | auto const create_def = |
Václav Kubernát | 5b8a8f3 | 2020-05-20 00:57:22 +0200 | [diff] [blame] | 57 | create_::name >> space_separator > (presenceContainerPath | listInstancePath | leafListElementPath); |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 58 | |
| 59 | auto const delete_rule_def = |
Jan Kundrát | b7206ad | 2020-06-18 21:08:14 +0200 | [diff] [blame^] | 60 | delete_::name >> space_separator > (presenceContainerPath | listInstancePath | leafListElementPath | writableLeafPath); |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 61 | |
Václav Kubernát | b6ff0b6 | 2018-08-30 16:14:53 +0200 | [diff] [blame] | 62 | auto const get_def = |
Václav Kubernát | 9456b5c | 2019-10-02 21:14:52 +0200 | [diff] [blame] | 63 | get_::name >> -(space_separator >> ((dataPathListEnd | dataPath) | (module >> "*"))); |
Václav Kubernát | b6ff0b6 | 2018-08-30 16:14:53 +0200 | [diff] [blame] | 64 | |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 65 | auto const set_def = |
Václav Kubernát | abf5280 | 2020-05-19 01:31:17 +0200 | [diff] [blame] | 66 | set_::name >> space_separator > writableLeafPath > space_separator > leaf_data; |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 67 | |
Václav Kubernát | 812ee28 | 2018-08-30 17:10:03 +0200 | [diff] [blame] | 68 | auto const commit_def = |
Václav Kubernát | bf083ec | 2019-02-19 13:58:09 +0100 | [diff] [blame] | 69 | commit_::name >> x3::attr(commit_()); |
Václav Kubernát | 812ee28 | 2018-08-30 17:10:03 +0200 | [diff] [blame] | 70 | |
Václav Kubernát | 6d79143 | 2018-10-25 16:00:35 +0200 | [diff] [blame] | 71 | auto const discard_def = |
Václav Kubernát | bf083ec | 2019-02-19 13:58:09 +0100 | [diff] [blame] | 72 | discard_::name >> x3::attr(discard_()); |
Václav Kubernát | 6d79143 | 2018-10-25 16:00:35 +0200 | [diff] [blame] | 73 | |
Václav Kubernát | 054cc99 | 2019-02-21 14:23:52 +0100 | [diff] [blame] | 74 | struct command_names_table : x3::symbols<decltype(help_::m_cmd)> { |
| 75 | command_names_table() |
| 76 | { |
| 77 | boost::mpl::for_each<CommandTypes, boost::type<boost::mpl::_>>([this](auto cmd) { |
Václav Kubernát | 672889c | 2020-05-27 04:11:36 +0200 | [diff] [blame] | 78 | add(commandNamesVisitor(cmd), decltype(help_::m_cmd)(cmd)); |
Václav Kubernát | 054cc99 | 2019-02-21 14:23:52 +0100 | [diff] [blame] | 79 | }); |
| 80 | } |
| 81 | } const command_names; |
| 82 | |
| 83 | auto const help_def = |
| 84 | help_::name > createCommandSuggestions >> -command_names; |
| 85 | |
Václav Kubernát | 7160a13 | 2020-04-03 02:11:01 +0200 | [diff] [blame] | 86 | struct datastore_symbol_table : x3::symbols<Datastore> { |
| 87 | datastore_symbol_table() |
| 88 | { |
| 89 | add |
| 90 | ("running", Datastore::Running) |
| 91 | ("startup", Datastore::Startup); |
| 92 | } |
| 93 | } const datastore; |
| 94 | |
| 95 | auto copy_source = x3::rule<class source, Datastore>{"source datastore"} = datastore; |
| 96 | auto copy_destination = x3::rule<class source, Datastore>{"destination datastore"} = datastore; |
| 97 | |
| 98 | const auto datastoreSuggestions = x3::eps[([](auto& ctx) { |
| 99 | auto& parserContext = x3::get<parser_context_tag>(ctx); |
| 100 | parserContext.m_suggestions = {Completion{"running", " "}, Completion{"startup", " "}}; |
| 101 | parserContext.m_completionIterator = _where(ctx).begin(); |
| 102 | })]; |
| 103 | |
| 104 | struct copy_args : x3::parser<copy_args> { |
| 105 | using attribute_type = copy_; |
| 106 | template <typename It, typename Ctx, typename RCtx> |
| 107 | bool parse(It& begin, It end, Ctx const& ctx, RCtx& rctx, copy_& attr) const |
| 108 | { |
| 109 | auto& parserContext = x3::get<parser_context_tag>(ctx); |
| 110 | auto iterBeforeDestination = begin; |
| 111 | auto save_iter = x3::no_skip[x3::eps[([&iterBeforeDestination](auto& ctx) {iterBeforeDestination = _where(ctx).begin();})]]; |
| 112 | auto grammar = datastoreSuggestions > copy_source > space_separator > datastoreSuggestions > save_iter > copy_destination; |
| 113 | |
| 114 | try { |
| 115 | grammar.parse(begin, end, ctx, rctx, attr); |
| 116 | } catch (x3::expectation_failure<It>& ex) { |
| 117 | using namespace std::string_literals; |
| 118 | parserContext.m_errorMsg = "Expected "s + ex.which() + " here:"; |
| 119 | throw; |
| 120 | } |
| 121 | |
| 122 | if (attr.m_source == attr.m_destination) { |
| 123 | begin = iterBeforeDestination; // Restoring the iterator here makes the error caret point to the second datastore |
| 124 | parserContext.m_errorMsg = "Source datastore and destination datastore can't be the same."; |
| 125 | return false; |
| 126 | } |
| 127 | |
| 128 | return true; |
| 129 | } |
| 130 | } copy_args; |
| 131 | |
| 132 | auto const copy_def = |
| 133 | copy_::name > space_separator > copy_args; |
| 134 | |
Václav Kubernát | 9cfcd87 | 2020-02-18 12:34:02 +0100 | [diff] [blame] | 135 | auto const describe_def = |
Václav Kubernát | 4618fa4 | 2020-05-07 01:33:19 +0200 | [diff] [blame] | 136 | describe_::name >> space_separator > (dataPathListEnd | anyPath); |
Václav Kubernát | 9cfcd87 | 2020-02-18 12:34:02 +0100 | [diff] [blame] | 137 | |
Václav Kubernát | bf65dd7 | 2020-05-28 02:32:31 +0200 | [diff] [blame] | 138 | struct mode_table : x3::symbols<MoveMode> { |
| 139 | mode_table() |
| 140 | { |
| 141 | add |
| 142 | ("after", MoveMode::After) |
| 143 | ("before", MoveMode::Before) |
| 144 | ("begin", MoveMode::Begin) |
| 145 | ("end", MoveMode::End); |
| 146 | } |
| 147 | } const mode_table; |
| 148 | |
| 149 | struct move_absolute_table : x3::symbols<yang::move::Absolute> { |
| 150 | move_absolute_table() |
| 151 | { |
| 152 | add |
| 153 | ("begin", yang::move::Absolute::Begin) |
| 154 | ("end", yang::move::Absolute::End); |
| 155 | } |
| 156 | } const move_absolute_table; |
| 157 | |
| 158 | struct move_relative_table : x3::symbols<yang::move::Relative::Position> { |
| 159 | move_relative_table() |
| 160 | { |
| 161 | add |
| 162 | ("before", yang::move::Relative::Position::Before) |
| 163 | ("after", yang::move::Relative::Position::After); |
| 164 | } |
| 165 | } const move_relative_table; |
| 166 | |
| 167 | struct move_args : x3::parser<move_args> { |
| 168 | using attribute_type = move_; |
| 169 | template <typename It, typename Ctx, typename RCtx> |
| 170 | bool parse(It& begin, It end, Ctx const& ctx, RCtx& rctx, move_& attr) const |
| 171 | { |
| 172 | ParserContext& parserContext = x3::get<parser_context_tag>(ctx); |
| 173 | dataPath_ movePath; |
| 174 | auto movePathGrammar = listInstancePath | leafListElementPath; |
| 175 | auto res = movePathGrammar.parse(begin, end, ctx, rctx, attr.m_source); |
| 176 | if (!res) { |
| 177 | parserContext.m_errorMsg = "Expected source path here:"; |
| 178 | return false; |
| 179 | } |
| 180 | |
| 181 | // Try absolute move first. |
| 182 | res = (space_separator >> move_absolute_table).parse(begin, end, ctx, rctx, attr.m_destination); |
| 183 | if (res) { |
| 184 | // Absolute move parsing succeeded, we don't need to parse anything else. |
| 185 | return true; |
| 186 | } |
| 187 | |
| 188 | // If absolute move didn't succeed, try relative. |
| 189 | attr.m_destination = yang::move::Relative{}; |
| 190 | res = (space_separator >> move_relative_table).parse(begin, end, ctx, rctx, std::get<yang::move::Relative>(attr.m_destination).m_position); |
| 191 | |
| 192 | if (!res) { |
| 193 | parserContext.m_errorMsg = "Expected a move position (begin, end, before, after) here:"; |
| 194 | return false; |
| 195 | } |
| 196 | |
| 197 | if (std::holds_alternative<leafListElement_>(attr.m_source.m_nodes.back().m_suffix)) { |
| 198 | leaf_data_ value; |
| 199 | res = (space_separator >> leaf_data).parse(begin, end, ctx, rctx, value); |
| 200 | if (res) { |
| 201 | std::get<yang::move::Relative>(attr.m_destination).m_path = {{".", value}}; |
| 202 | } |
| 203 | } else { |
| 204 | ListInstance listInstance; |
| 205 | // The source list instance will be stored inside the parser context path. |
| 206 | // The source list instance will be full data path (with keys included). |
| 207 | // However, m_tmpListPath is supposed to store a path with a list without the keys. |
| 208 | // So, I pop the last listElement_ (which has the keys) and put in a list_ (which doesn't have the keys). |
| 209 | // Example: /mod:cont/protocols[name='ftp'] gets turned into /mod:cont/protocols |
| 210 | parserContext.m_tmpListPath = parserContext.currentDataPath(); |
| 211 | parserContext.m_tmpListPath.m_nodes.pop_back(); |
| 212 | auto list = list_{std::get<listElement_>(attr.m_source.m_nodes.back().m_suffix).m_name}; |
| 213 | parserContext.m_tmpListPath.m_nodes.push_back(dataNode_{attr.m_source.m_nodes.back().m_prefix, list}); |
| 214 | |
| 215 | res = (space_separator >> listSuffix).parse(begin, end, ctx, rctx, listInstance); |
| 216 | if (res) { |
| 217 | std::get<yang::move::Relative>(attr.m_destination).m_path = listInstance; |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | if (!res) { |
| 222 | parserContext.m_errorMsg = "Expected a destination here:"; |
| 223 | } |
| 224 | |
| 225 | return res; |
| 226 | } |
| 227 | } const move_args; |
| 228 | |
| 229 | auto const move_def = |
| 230 | move_::name >> space_separator >> move_args; |
| 231 | |
Václav Kubernát | 5727242 | 2019-02-08 12:48:24 +0100 | [diff] [blame] | 232 | auto const createCommandSuggestions_def = |
Václav Kubernát | bf083ec | 2019-02-19 13:58:09 +0100 | [diff] [blame] | 233 | x3::eps; |
Václav Kubernát | 5727242 | 2019-02-08 12:48:24 +0100 | [diff] [blame] | 234 | |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 235 | auto const command_def = |
Václav Kubernát | bf65dd7 | 2020-05-28 02:32:31 +0200 | [diff] [blame] | 236 | createCommandSuggestions >> x3::expect[cd | copy | create | delete_rule | set | commit | get | ls | discard | describe | help | move]; |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame] | 237 | |
| 238 | #if __clang__ |
| 239 | #pragma GCC diagnostic pop |
| 240 | #endif |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 241 | |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 242 | BOOST_SPIRIT_DEFINE(set) |
Václav Kubernát | 812ee28 | 2018-08-30 17:10:03 +0200 | [diff] [blame] | 243 | BOOST_SPIRIT_DEFINE(commit) |
Václav Kubernát | b6ff0b6 | 2018-08-30 16:14:53 +0200 | [diff] [blame] | 244 | BOOST_SPIRIT_DEFINE(get) |
Václav Kubernát | 11afac7 | 2018-07-18 14:59:53 +0200 | [diff] [blame] | 245 | BOOST_SPIRIT_DEFINE(ls) |
Václav Kubernát | 6d79143 | 2018-10-25 16:00:35 +0200 | [diff] [blame] | 246 | BOOST_SPIRIT_DEFINE(discard) |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 247 | BOOST_SPIRIT_DEFINE(cd) |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 248 | BOOST_SPIRIT_DEFINE(create) |
| 249 | BOOST_SPIRIT_DEFINE(delete_rule) |
Václav Kubernát | 9cfcd87 | 2020-02-18 12:34:02 +0100 | [diff] [blame] | 250 | BOOST_SPIRIT_DEFINE(describe) |
Václav Kubernát | 054cc99 | 2019-02-21 14:23:52 +0100 | [diff] [blame] | 251 | BOOST_SPIRIT_DEFINE(help) |
Václav Kubernát | 7160a13 | 2020-04-03 02:11:01 +0200 | [diff] [blame] | 252 | BOOST_SPIRIT_DEFINE(copy) |
Václav Kubernát | bf65dd7 | 2020-05-28 02:32:31 +0200 | [diff] [blame] | 253 | BOOST_SPIRIT_DEFINE(move) |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 254 | BOOST_SPIRIT_DEFINE(command) |
Václav Kubernát | 5727242 | 2019-02-08 12:48:24 +0100 | [diff] [blame] | 255 | BOOST_SPIRIT_DEFINE(createCommandSuggestions) |