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 | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 30 | x3::rule<command_class, command_> const command = "command"; |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 31 | |
Václav Kubernát | 5727242 | 2019-02-08 12:48:24 +0100 | [diff] [blame] | 32 | x3::rule<createCommandSuggestions_class, x3::unused_type> const createCommandSuggestions = "createCommandSuggestions"; |
Václav Kubernát | 5c75b25 | 2018-10-10 18:33:47 +0200 | [diff] [blame] | 33 | |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame] | 34 | #if __clang__ |
| 35 | #pragma GCC diagnostic push |
| 36 | #pragma GCC diagnostic ignored "-Woverloaded-shift-op-parentheses" |
| 37 | #endif |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 38 | |
Václav Kubernát | 509ce65 | 2019-05-29 19:46:44 +0200 | [diff] [blame] | 39 | namespace ascii = boost::spirit::x3::ascii; |
| 40 | |
Václav Kubernát | e7d4aea | 2018-09-11 18:15:48 +0200 | [diff] [blame] | 41 | struct ls_options_table : x3::symbols<LsOption> { |
| 42 | ls_options_table() |
| 43 | { |
Václav Kubernát | bf083ec | 2019-02-19 13:58:09 +0100 | [diff] [blame] | 44 | add |
| 45 | ("--recursive", LsOption::Recursive); |
Václav Kubernát | e7d4aea | 2018-09-11 18:15:48 +0200 | [diff] [blame] | 46 | } |
| 47 | } const ls_options; |
| 48 | |
Václav Kubernát | 11afac7 | 2018-07-18 14:59:53 +0200 | [diff] [blame] | 49 | auto const ls_def = |
Václav Kubernát | 4a58ce6 | 2020-05-14 17:58:10 +0200 | [diff] [blame^] | 50 | ls_::name >> *(space_separator >> ls_options) >> -(space_separator >> (anyPath | (module >> "*"))); |
Václav Kubernát | 11afac7 | 2018-07-18 14:59:53 +0200 | [diff] [blame] | 51 | |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 52 | auto const cd_def = |
Václav Kubernát | bf083ec | 2019-02-19 13:58:09 +0100 | [diff] [blame] | 53 | cd_::name >> space_separator > dataPath; |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 54 | |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 55 | auto const create_def = |
Václav Kubernát | f5f64f0 | 2019-03-19 17:15:47 +0100 | [diff] [blame] | 56 | create_::name >> space_separator > (presenceContainerPath | listInstancePath); |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 57 | |
| 58 | auto const delete_rule_def = |
Václav Kubernát | f5f64f0 | 2019-03-19 17:15:47 +0100 | [diff] [blame] | 59 | delete_::name >> space_separator > (presenceContainerPath | listInstancePath); |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 60 | |
Václav Kubernát | b6ff0b6 | 2018-08-30 16:14:53 +0200 | [diff] [blame] | 61 | auto const get_def = |
Václav Kubernát | 9456b5c | 2019-10-02 21:14:52 +0200 | [diff] [blame] | 62 | get_::name >> -(space_separator >> ((dataPathListEnd | dataPath) | (module >> "*"))); |
Václav Kubernát | b6ff0b6 | 2018-08-30 16:14:53 +0200 | [diff] [blame] | 63 | |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 64 | auto const set_def = |
Václav Kubernát | 989b5de | 2019-02-20 16:28:35 +0100 | [diff] [blame] | 65 | set_::name >> space_separator > leafPath > space_separator > leaf_data; |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 66 | |
Václav Kubernát | 812ee28 | 2018-08-30 17:10:03 +0200 | [diff] [blame] | 67 | auto const commit_def = |
Václav Kubernát | bf083ec | 2019-02-19 13:58:09 +0100 | [diff] [blame] | 68 | commit_::name >> x3::attr(commit_()); |
Václav Kubernát | 812ee28 | 2018-08-30 17:10:03 +0200 | [diff] [blame] | 69 | |
Václav Kubernát | 6d79143 | 2018-10-25 16:00:35 +0200 | [diff] [blame] | 70 | auto const discard_def = |
Václav Kubernát | bf083ec | 2019-02-19 13:58:09 +0100 | [diff] [blame] | 71 | discard_::name >> x3::attr(discard_()); |
Václav Kubernát | 6d79143 | 2018-10-25 16:00:35 +0200 | [diff] [blame] | 72 | |
Václav Kubernát | 054cc99 | 2019-02-21 14:23:52 +0100 | [diff] [blame] | 73 | struct command_names_table : x3::symbols<decltype(help_::m_cmd)> { |
| 74 | command_names_table() |
| 75 | { |
| 76 | boost::mpl::for_each<CommandTypes, boost::type<boost::mpl::_>>([this](auto cmd) { |
| 77 | add(commandNamesVisitor()(cmd), decltype(help_::m_cmd)(cmd)); |
| 78 | }); |
| 79 | } |
| 80 | } const command_names; |
| 81 | |
| 82 | auto const help_def = |
| 83 | help_::name > createCommandSuggestions >> -command_names; |
| 84 | |
Václav Kubernát | 7160a13 | 2020-04-03 02:11:01 +0200 | [diff] [blame] | 85 | struct datastore_symbol_table : x3::symbols<Datastore> { |
| 86 | datastore_symbol_table() |
| 87 | { |
| 88 | add |
| 89 | ("running", Datastore::Running) |
| 90 | ("startup", Datastore::Startup); |
| 91 | } |
| 92 | } const datastore; |
| 93 | |
| 94 | auto copy_source = x3::rule<class source, Datastore>{"source datastore"} = datastore; |
| 95 | auto copy_destination = x3::rule<class source, Datastore>{"destination datastore"} = datastore; |
| 96 | |
| 97 | const auto datastoreSuggestions = x3::eps[([](auto& ctx) { |
| 98 | auto& parserContext = x3::get<parser_context_tag>(ctx); |
| 99 | parserContext.m_suggestions = {Completion{"running", " "}, Completion{"startup", " "}}; |
| 100 | parserContext.m_completionIterator = _where(ctx).begin(); |
| 101 | })]; |
| 102 | |
| 103 | struct copy_args : x3::parser<copy_args> { |
| 104 | using attribute_type = copy_; |
| 105 | template <typename It, typename Ctx, typename RCtx> |
| 106 | bool parse(It& begin, It end, Ctx const& ctx, RCtx& rctx, copy_& attr) const |
| 107 | { |
| 108 | auto& parserContext = x3::get<parser_context_tag>(ctx); |
| 109 | auto iterBeforeDestination = begin; |
| 110 | auto save_iter = x3::no_skip[x3::eps[([&iterBeforeDestination](auto& ctx) {iterBeforeDestination = _where(ctx).begin();})]]; |
| 111 | auto grammar = datastoreSuggestions > copy_source > space_separator > datastoreSuggestions > save_iter > copy_destination; |
| 112 | |
| 113 | try { |
| 114 | grammar.parse(begin, end, ctx, rctx, attr); |
| 115 | } catch (x3::expectation_failure<It>& ex) { |
| 116 | using namespace std::string_literals; |
| 117 | parserContext.m_errorMsg = "Expected "s + ex.which() + " here:"; |
| 118 | throw; |
| 119 | } |
| 120 | |
| 121 | if (attr.m_source == attr.m_destination) { |
| 122 | begin = iterBeforeDestination; // Restoring the iterator here makes the error caret point to the second datastore |
| 123 | parserContext.m_errorMsg = "Source datastore and destination datastore can't be the same."; |
| 124 | return false; |
| 125 | } |
| 126 | |
| 127 | return true; |
| 128 | } |
| 129 | } copy_args; |
| 130 | |
| 131 | auto const copy_def = |
| 132 | copy_::name > space_separator > copy_args; |
| 133 | |
Václav Kubernát | 9cfcd87 | 2020-02-18 12:34:02 +0100 | [diff] [blame] | 134 | auto const describe_def = |
Václav Kubernát | 4618fa4 | 2020-05-07 01:33:19 +0200 | [diff] [blame] | 135 | describe_::name >> space_separator > (dataPathListEnd | anyPath); |
Václav Kubernát | 9cfcd87 | 2020-02-18 12:34:02 +0100 | [diff] [blame] | 136 | |
Václav Kubernát | 5727242 | 2019-02-08 12:48:24 +0100 | [diff] [blame] | 137 | auto const createCommandSuggestions_def = |
Václav Kubernát | bf083ec | 2019-02-19 13:58:09 +0100 | [diff] [blame] | 138 | x3::eps; |
Václav Kubernát | 5727242 | 2019-02-08 12:48:24 +0100 | [diff] [blame] | 139 | |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 140 | auto const command_def = |
Václav Kubernát | 7160a13 | 2020-04-03 02:11:01 +0200 | [diff] [blame] | 141 | createCommandSuggestions >> x3::expect[cd | copy | create | delete_rule | set | commit | get | ls | discard | describe | help]; |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame] | 142 | |
| 143 | #if __clang__ |
| 144 | #pragma GCC diagnostic pop |
| 145 | #endif |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 146 | |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 147 | BOOST_SPIRIT_DEFINE(set) |
Václav Kubernát | 812ee28 | 2018-08-30 17:10:03 +0200 | [diff] [blame] | 148 | BOOST_SPIRIT_DEFINE(commit) |
Václav Kubernát | b6ff0b6 | 2018-08-30 16:14:53 +0200 | [diff] [blame] | 149 | BOOST_SPIRIT_DEFINE(get) |
Václav Kubernát | 11afac7 | 2018-07-18 14:59:53 +0200 | [diff] [blame] | 150 | BOOST_SPIRIT_DEFINE(ls) |
Václav Kubernát | 6d79143 | 2018-10-25 16:00:35 +0200 | [diff] [blame] | 151 | BOOST_SPIRIT_DEFINE(discard) |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 152 | BOOST_SPIRIT_DEFINE(cd) |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 153 | BOOST_SPIRIT_DEFINE(create) |
| 154 | BOOST_SPIRIT_DEFINE(delete_rule) |
Václav Kubernát | 9cfcd87 | 2020-02-18 12:34:02 +0100 | [diff] [blame] | 155 | BOOST_SPIRIT_DEFINE(describe) |
Václav Kubernát | 054cc99 | 2019-02-21 14:23:52 +0100 | [diff] [blame] | 156 | BOOST_SPIRIT_DEFINE(help) |
Václav Kubernát | 7160a13 | 2020-04-03 02:11:01 +0200 | [diff] [blame] | 157 | BOOST_SPIRIT_DEFINE(copy) |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 158 | BOOST_SPIRIT_DEFINE(command) |
Václav Kubernát | 5727242 | 2019-02-08 12:48:24 +0100 | [diff] [blame] | 159 | BOOST_SPIRIT_DEFINE(createCommandSuggestions) |