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 |
Václav Kubernát | 195eeea | 2018-05-18 13:52:36 +0200 | [diff] [blame] | 10 | |
Václav Kubernát | 5727242 | 2019-02-08 12:48:24 +0100 | [diff] [blame] | 11 | #include <boost/mpl/for_each.hpp> |
Václav Kubernát | 509ce65 | 2019-05-29 19:46:44 +0200 | [diff] [blame] | 12 | #include <boost/spirit/home/x3.hpp> |
| 13 | #include <boost/spirit/home/x3/support/utility/error_reporting.hpp> |
| 14 | |
| 15 | |
Václav Kubernát | 5727242 | 2019-02-08 12:48:24 +0100 | [diff] [blame] | 16 | #include "ast_commands.hpp" |
Václav Kubernát | 4294a85 | 2020-02-14 15:07:14 +0100 | [diff] [blame] | 17 | #include "parser_context.hpp" |
Václav Kubernát | 48fc383 | 2018-05-28 14:21:22 +0200 | [diff] [blame] | 18 | #include "schema.hpp" |
Václav Kubernát | ebca255 | 2018-06-08 19:06:02 +0200 | [diff] [blame] | 19 | #include "utils.hpp" |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 20 | namespace x3 = boost::spirit::x3; |
| 21 | |
| 22 | struct parser_context_tag; |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 23 | |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 24 | struct keyValue_class { |
| 25 | template <typename T, typename Iterator, typename Context> |
| 26 | void on_success(Iterator const&, Iterator const&, T& ast, Context const& context) |
| 27 | { |
| 28 | auto& parserContext = x3::get<parser_context_tag>(context); |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 29 | |
| 30 | if (parserContext.m_tmpListKeys.find(ast.first) != parserContext.m_tmpListKeys.end()) { |
| 31 | _pass(context) = false; |
| 32 | parserContext.m_errorMsg = "Key \"" + ast.first + "\" was entered more than once."; |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame] | 33 | } else { |
Václav Kubernát | 43908fb | 2020-01-02 19:05:51 +0100 | [diff] [blame] | 34 | parserContext.m_tmpListKeys.insert({ast.first, ast.second}); |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 35 | } |
| 36 | } |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame] | 37 | |
| 38 | template <typename Iterator, typename Exception, typename Context> |
| 39 | x3::error_handler_result on_error(Iterator&, Iterator const&, Exception const&, Context const& context) |
| 40 | { |
| 41 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 42 | parserContext.m_errorMsg = "Error parsing key values here:"; |
| 43 | return x3::error_handler_result::rethrow; |
| 44 | } |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 45 | }; |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame] | 46 | |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 47 | struct node_identifier_class { |
| 48 | template <typename T, typename Iterator, typename Context> |
| 49 | void on_success(Iterator const&, Iterator const&, T&, Context const& context) |
| 50 | { |
| 51 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 52 | |
| 53 | if (!parserContext.m_topLevelModulePresent) { |
| 54 | if (parserContext.m_errorMsg.empty()) |
| 55 | parserContext.m_errorMsg = "You have to specify a top level module."; |
| 56 | _pass(context) = false; |
| 57 | } |
| 58 | } |
| 59 | }; |
| 60 | |
Václav Kubernát | 7707cae | 2020-01-16 12:04:53 +0100 | [diff] [blame] | 61 | struct key_identifier_class { |
| 62 | template <typename T, typename Iterator, typename Context> |
| 63 | void on_success(Iterator const&, Iterator const&, T& ast, Context const& context) |
| 64 | { |
| 65 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 66 | const Schema& schema = parserContext.m_schema; |
| 67 | schemaPath_ location = parserContext.currentSchemaPath(); |
| 68 | ModuleNodePair list{parserContext.m_curModule, parserContext.m_tmpListName}; |
| 69 | |
| 70 | if (schema.listHasKey(location, list, ast)) { |
| 71 | schemaNode_ listNode; |
Václav Kubernát | 43908fb | 2020-01-02 19:05:51 +0100 | [diff] [blame] | 72 | listNode.m_prefix = parserContext.m_curModule.flat_map([] (auto mod) { return boost::optional<module_>{{mod}}; });; |
Václav Kubernát | 7707cae | 2020-01-16 12:04:53 +0100 | [diff] [blame] | 73 | listNode.m_suffix = list_{parserContext.m_tmpListName}; |
| 74 | location.m_nodes.push_back(listNode); |
| 75 | parserContext.m_tmpListKeyLeafPath.m_location = location; |
Václav Kubernát | 43908fb | 2020-01-02 19:05:51 +0100 | [diff] [blame] | 76 | parserContext.m_tmpListKeyLeafPath.m_node = { parserContext.m_curModule, ast }; |
Václav Kubernát | 7707cae | 2020-01-16 12:04:53 +0100 | [diff] [blame] | 77 | } else { |
| 78 | parserContext.m_errorMsg = parserContext.m_tmpListName + " is not indexed by \"" + ast + "\"."; |
| 79 | _pass(context) = false; |
| 80 | } |
| 81 | } |
| 82 | }; |
Václav Kubernát | 89728d8 | 2018-09-13 16:28:28 +0200 | [diff] [blame] | 83 | |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 84 | struct module_identifier_class; |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 85 | |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 86 | struct listSuffix_class { |
| 87 | template <typename T, typename Iterator, typename Context> |
| 88 | void on_success(Iterator const&, Iterator const&, T& ast, Context const& context) |
| 89 | { |
| 90 | auto& parserContext = x3::get<parser_context_tag>(context); |
Václav Kubernát | 48fc383 | 2018-05-28 14:21:22 +0200 | [diff] [blame] | 91 | const Schema& schema = parserContext.m_schema; |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 92 | |
Václav Kubernát | 72749c6 | 2020-01-03 16:47:34 +0100 | [diff] [blame] | 93 | const auto& keysNeeded = schema.listKeys(parserContext.currentSchemaPath(), {parserContext.m_curModule, parserContext.m_tmpListName}); |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 94 | std::set<std::string> keysSupplied; |
| 95 | for (const auto& it : ast) |
| 96 | keysSupplied.insert(it.first); |
| 97 | |
| 98 | if (keysNeeded != keysSupplied) { |
| 99 | parserContext.m_errorMsg = "Not enough keys for " + parserContext.m_tmpListName + ". " + |
| 100 | "These keys were not supplied:"; |
| 101 | std::set<std::string> missingKeys; |
| 102 | std::set_difference(keysNeeded.begin(), keysNeeded.end(), |
| 103 | keysSupplied.begin(), keysSupplied.end(), |
| 104 | std::inserter(missingKeys, missingKeys.end())); |
| 105 | |
| 106 | for (const auto& it : missingKeys) |
| 107 | parserContext.m_errorMsg += " " + it; |
| 108 | parserContext.m_errorMsg += "."; |
| 109 | |
| 110 | _pass(context) = false; |
| 111 | } |
| 112 | } |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame] | 113 | |
| 114 | template <typename Iterator, typename Exception, typename Context> |
| 115 | x3::error_handler_result on_error(Iterator&, Iterator const&, Exception const&, Context const& context) |
| 116 | { |
| 117 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 118 | if (parserContext.m_errorMsg.empty()) |
| 119 | parserContext.m_errorMsg = "Expecting ']' here:"; |
| 120 | return x3::error_handler_result::rethrow; |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame] | 121 | } |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 122 | }; |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 123 | struct list_class { |
| 124 | template <typename T, typename Iterator, typename Context> |
| 125 | void on_success(Iterator const&, Iterator const&, T& ast, Context const& context) |
| 126 | { |
| 127 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 128 | const Schema& schema = parserContext.m_schema; |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 129 | |
Václav Kubernát | 72749c6 | 2020-01-03 16:47:34 +0100 | [diff] [blame] | 130 | if (!schema.isList(parserContext.currentSchemaPath(), {parserContext.m_curModule, ast.m_name})) { |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 131 | _pass(context) = false; |
| 132 | } |
| 133 | } |
| 134 | }; |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 135 | |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 136 | struct module_class { |
| 137 | template <typename T, typename Iterator, typename Context> |
| 138 | void on_success(Iterator const&, Iterator const&, T& ast, Context const& context) |
| 139 | { |
| 140 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 141 | const auto& schema = parserContext.m_schema; |
| 142 | |
Václav Kubernát | 75877de | 2019-11-20 17:43:02 +0100 | [diff] [blame] | 143 | if (schema.isModule(ast.m_name)) { |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 144 | parserContext.m_curModule = ast.m_name; |
| 145 | parserContext.m_topLevelModulePresent = true; |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 146 | } else { |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 147 | parserContext.m_errorMsg = "Invalid module name."; |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 148 | _pass(context) = false; |
| 149 | } |
| 150 | } |
| 151 | }; |
| 152 | |
Václav Kubernát | 39ae959 | 2019-02-05 17:35:16 +0100 | [diff] [blame] | 153 | struct dataNodeList_class { |
| 154 | template <typename T, typename Iterator, typename Context> |
| 155 | void on_success(Iterator const&, Iterator const&, T& ast, Context const& context) |
| 156 | { |
| 157 | auto& parserContext = x3::get<parser_context_tag>(context); |
Václav Kubernát | 72749c6 | 2020-01-03 16:47:34 +0100 | [diff] [blame] | 158 | parserContext.pushPathFragment(ast); |
Václav Kubernát | 39ae959 | 2019-02-05 17:35:16 +0100 | [diff] [blame] | 159 | } |
| 160 | }; |
Václav Kubernát | 5c75b25 | 2018-10-10 18:33:47 +0200 | [diff] [blame] | 161 | |
Václav Kubernát | 37171a1 | 2018-08-31 17:01:48 +0200 | [diff] [blame] | 162 | struct absoluteStart_class { |
| 163 | template <typename T, typename Iterator, typename Context> |
| 164 | void on_success(Iterator const&, Iterator const&, T&, Context const& context) |
| 165 | { |
| 166 | auto& parserContext = x3::get<parser_context_tag>(context); |
Václav Kubernát | 72749c6 | 2020-01-03 16:47:34 +0100 | [diff] [blame] | 167 | parserContext.clearPath(); |
Václav Kubernát | 37171a1 | 2018-08-31 17:01:48 +0200 | [diff] [blame] | 168 | } |
| 169 | }; |
| 170 | |
Václav Kubernát | 5c75b25 | 2018-10-10 18:33:47 +0200 | [diff] [blame] | 171 | struct dataNodesListEnd_class; |
| 172 | |
| 173 | struct dataPathListEnd_class; |
| 174 | |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 175 | struct dataPath_class { |
| 176 | template <typename Iterator, typename Exception, typename Context> |
| 177 | x3::error_handler_result on_error(Iterator&, Iterator const&, Exception const&, Context const& context) |
| 178 | { |
| 179 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 180 | if (parserContext.m_errorMsg.empty()) { |
| 181 | parserContext.m_errorMsg = "Expected path."; |
| 182 | return x3::error_handler_result::fail; |
| 183 | } else { |
| 184 | return x3::error_handler_result::rethrow; |
| 185 | } |
| 186 | } |
| 187 | }; |
| 188 | |
| 189 | struct schemaPath_class { |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 190 | template <typename Iterator, typename Exception, typename Context> |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame] | 191 | x3::error_handler_result on_error(Iterator&, Iterator const&, Exception const&, Context const& context) |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 192 | { |
| 193 | auto& parserContext = x3::get<parser_context_tag>(context); |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame] | 194 | if (parserContext.m_errorMsg.empty()) { |
| 195 | parserContext.m_errorMsg = "Expected path."; |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 196 | return x3::error_handler_result::fail; |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame] | 197 | } else { |
| 198 | return x3::error_handler_result::rethrow; |
| 199 | } |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 200 | } |
| 201 | }; |
| 202 | |
Václav Kubernát | 6d79143 | 2018-10-25 16:00:35 +0200 | [diff] [blame] | 203 | struct discard_class; |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 204 | |
Václav Kubernát | 11afac7 | 2018-07-18 14:59:53 +0200 | [diff] [blame] | 205 | struct ls_class; |
| 206 | |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 207 | struct cd_class { |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 208 | template <typename Iterator, typename Exception, typename Context> |
| 209 | x3::error_handler_result on_error(Iterator&, Iterator const&, Exception const& x, Context const& context) |
| 210 | { |
| 211 | auto& parserContext = x3::get<parser_context_tag>(context); |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame] | 212 | if (parserContext.m_errorMsg.empty()) |
| 213 | parserContext.m_errorMsg = "Expected " + x.which() + " here:"; |
| 214 | return x3::error_handler_result::rethrow; |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 215 | } |
| 216 | }; |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 217 | |
Václav Kubernát | 6797df0 | 2019-03-18 20:21:50 +0100 | [diff] [blame] | 218 | struct presenceContainerPath_class { |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 219 | template <typename T, typename Iterator, typename Context> |
| 220 | void on_success(Iterator const&, Iterator const&, T& ast, Context const& context) |
| 221 | { |
| 222 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 223 | const auto& schema = parserContext.m_schema; |
| 224 | try { |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 225 | boost::optional<std::string> module; |
Václav Kubernát | 6797df0 | 2019-03-18 20:21:50 +0100 | [diff] [blame] | 226 | if (ast.m_nodes.back().m_prefix) |
| 227 | module = ast.m_nodes.back().m_prefix.value().m_name; |
| 228 | container_ cont = boost::get<container_>(ast.m_nodes.back().m_suffix); |
Václav Kubernát | 72749c6 | 2020-01-03 16:47:34 +0100 | [diff] [blame] | 229 | auto location = pathWithoutLastNode(parserContext.currentSchemaPath()); |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 230 | |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 231 | if (!schema.isPresenceContainer(location, {module, cont.m_name})) { |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame] | 232 | parserContext.m_errorMsg = "This container is not a presence container."; |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 233 | _pass(context) = false; |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 234 | } |
| 235 | } catch (boost::bad_get&) { |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame] | 236 | parserContext.m_errorMsg = "This is not a container."; |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 237 | _pass(context) = false; |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 238 | } |
| 239 | } |
Václav Kubernát | 6797df0 | 2019-03-18 20:21:50 +0100 | [diff] [blame] | 240 | }; |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 241 | |
Václav Kubernát | f5f64f0 | 2019-03-19 17:15:47 +0100 | [diff] [blame] | 242 | struct listInstancePath_class { |
| 243 | template <typename T, typename Iterator, typename Context> |
| 244 | void on_success(Iterator const&, Iterator const&, T& ast, Context const& context) |
| 245 | { |
| 246 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 247 | if (ast.m_nodes.back().m_suffix.type() != typeid(listElement_)) { |
| 248 | parserContext.m_errorMsg = "This is not a list instance."; |
| 249 | _pass(context) = false; |
| 250 | } |
| 251 | } |
| 252 | }; |
| 253 | |
Václav Kubernát | 8028f94 | 2019-09-25 16:03:23 +0200 | [diff] [blame] | 254 | struct space_separator_class { |
| 255 | template <typename T, typename Iterator, typename Context> |
| 256 | void on_success(Iterator const&, Iterator const&, T&, Context const& context) |
| 257 | { |
| 258 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 259 | parserContext.m_suggestions.clear(); |
Václav Kubernát | 1ed4aa3 | 2020-01-23 13:13:28 +0100 | [diff] [blame] | 260 | parserContext.m_completionIterator = boost::none; |
Václav Kubernát | 8028f94 | 2019-09-25 16:03:23 +0200 | [diff] [blame] | 261 | } |
| 262 | }; |
| 263 | |
Václav Kubernát | 6797df0 | 2019-03-18 20:21:50 +0100 | [diff] [blame] | 264 | struct create_class { |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 265 | template <typename Iterator, typename Exception, typename Context> |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame] | 266 | x3::error_handler_result on_error(Iterator&, Iterator const&, Exception const&, Context const& context) |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 267 | { |
| 268 | auto& parserContext = x3::get<parser_context_tag>(context); |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame] | 269 | if (parserContext.m_errorMsg.empty()) |
| 270 | parserContext.m_errorMsg = "Couldn't parse create/delete command."; |
| 271 | return x3::error_handler_result::rethrow; |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 272 | } |
| 273 | }; |
| 274 | |
Václav Kubernát | 6797df0 | 2019-03-18 20:21:50 +0100 | [diff] [blame] | 275 | struct delete_class { |
| 276 | template <typename Iterator, typename Exception, typename Context> |
| 277 | x3::error_handler_result on_error(Iterator&, Iterator const&, Exception const&, Context const& context) |
| 278 | { |
| 279 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 280 | if (parserContext.m_errorMsg.empty()) |
| 281 | parserContext.m_errorMsg = "Couldn't parse create/delete command."; |
| 282 | return x3::error_handler_result::rethrow; |
| 283 | } |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 284 | }; |
| 285 | |
Václav Kubernát | 0b0272f | 2018-06-13 14:13:08 +0200 | [diff] [blame] | 286 | struct leaf_path_class { |
| 287 | template <typename T, typename Iterator, typename Context> |
Václav Kubernát | 7707cae | 2020-01-16 12:04:53 +0100 | [diff] [blame] | 288 | void on_success(Iterator const&, Iterator const&, T&, Context const& context) |
Václav Kubernát | 0b0272f | 2018-06-13 14:13:08 +0200 | [diff] [blame] | 289 | { |
| 290 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 291 | try { |
Václav Kubernát | 7707cae | 2020-01-16 12:04:53 +0100 | [diff] [blame] | 292 | auto lastNode = parserContext.currentSchemaPath().m_nodes.back(); |
| 293 | auto leaf = boost::get<leaf_>(lastNode.m_suffix); |
| 294 | auto location = pathWithoutLastNode(parserContext.currentSchemaPath()); |
| 295 | ModuleNodePair node{lastNode.m_prefix.flat_map([](const auto& it) { return boost::optional<std::string>{it.m_name}; }), leaf.m_name}; |
| 296 | |
| 297 | parserContext.m_tmpListKeyLeafPath.m_location = location; |
| 298 | parserContext.m_tmpListKeyLeafPath.m_node = node; |
| 299 | |
Václav Kubernát | 0b0272f | 2018-06-13 14:13:08 +0200 | [diff] [blame] | 300 | } catch (boost::bad_get&) { |
| 301 | parserContext.m_errorMsg = "This is not a path to leaf."; |
| 302 | _pass(context) = false; |
| 303 | } |
| 304 | } |
| 305 | }; |
| 306 | |
Václav Kubernát | eeb3884 | 2019-03-20 19:46:05 +0100 | [diff] [blame] | 307 | // This handler only checks if the module exists |
| 308 | // It doesn't set any ParserContext flags (except the error message) |
| 309 | struct data_module_prefix_class { |
| 310 | template <typename T, typename Iterator, typename Context> |
| 311 | void on_success(Iterator const&, Iterator const&, T& ast, Context const& context) |
| 312 | { |
| 313 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 314 | const auto& schema = parserContext.m_schema; |
| 315 | |
Václav Kubernát | 72749c6 | 2020-01-03 16:47:34 +0100 | [diff] [blame] | 316 | if (!schema.isModule(parserContext.currentSchemaPath(), ast.m_name)) { |
Václav Kubernát | eeb3884 | 2019-03-20 19:46:05 +0100 | [diff] [blame] | 317 | parserContext.m_errorMsg = "Invalid module name."; |
| 318 | _pass(context) = false; |
| 319 | } |
| 320 | } |
| 321 | }; |
| 322 | |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 323 | struct set_class { |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 324 | template <typename Iterator, typename Exception, typename Context> |
| 325 | x3::error_handler_result on_error(Iterator&, Iterator const&, Exception const& x, Context const& context) |
| 326 | { |
| 327 | auto& parserContext = x3::get<parser_context_tag>(context); |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame] | 328 | if (parserContext.m_errorMsg.empty()) |
| 329 | parserContext.m_errorMsg = "Expected " + x.which() + " here:"; |
| 330 | return x3::error_handler_result::rethrow; |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 331 | } |
| 332 | }; |
| 333 | |
Václav Kubernát | 812ee28 | 2018-08-30 17:10:03 +0200 | [diff] [blame] | 334 | struct commit_class; |
| 335 | |
Václav Kubernát | 9cfcd87 | 2020-02-18 12:34:02 +0100 | [diff] [blame] | 336 | struct describe_class; |
| 337 | |
Václav Kubernát | 054cc99 | 2019-02-21 14:23:52 +0100 | [diff] [blame] | 338 | struct help_class; |
| 339 | |
Václav Kubernát | b6ff0b6 | 2018-08-30 16:14:53 +0200 | [diff] [blame] | 340 | struct get_class; |
| 341 | |
Václav Kubernát | 7160a13 | 2020-04-03 02:11:01 +0200 | [diff] [blame] | 342 | struct copy_class; |
| 343 | |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 344 | struct command_class { |
| 345 | template <typename Iterator, typename Exception, typename Context> |
| 346 | x3::error_handler_result on_error(Iterator&, Iterator const&, Exception const& x, Context const& context) |
| 347 | { |
| 348 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 349 | auto& error_handler = x3::get<x3::error_handler_tag>(context).get(); |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame] | 350 | if (parserContext.m_errorMsg.empty()) { |
| 351 | parserContext.m_errorMsg = "Unknown command."; |
| 352 | } |
| 353 | error_handler(x.where(), parserContext.m_errorMsg); |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 354 | return x3::error_handler_result::fail; |
| 355 | } |
| 356 | }; |
Václav Kubernát | 5c75b25 | 2018-10-10 18:33:47 +0200 | [diff] [blame] | 357 | |
Václav Kubernát | 4108e0d | 2018-10-29 13:32:22 +0100 | [diff] [blame] | 358 | struct initializePath_class { |
Václav Kubernát | 5c75b25 | 2018-10-10 18:33:47 +0200 | [diff] [blame] | 359 | template <typename T, typename Iterator, typename Context> |
| 360 | void on_success(Iterator const&, Iterator const&, T&, Context const& context) |
| 361 | { |
| 362 | auto& parserContext = x3::get<parser_context_tag>(context); |
Václav Kubernát | 72749c6 | 2020-01-03 16:47:34 +0100 | [diff] [blame] | 363 | parserContext.resetPath(); |
Václav Kubernát | 5c75b25 | 2018-10-10 18:33:47 +0200 | [diff] [blame] | 364 | parserContext.m_tmpListKeys.clear(); |
| 365 | parserContext.m_tmpListName.clear(); |
Václav Kubernát | 4108e0d | 2018-10-29 13:32:22 +0100 | [diff] [blame] | 366 | parserContext.m_suggestions.clear(); |
Václav Kubernát | 5c75b25 | 2018-10-10 18:33:47 +0200 | [diff] [blame] | 367 | } |
| 368 | }; |
Václav Kubernát | d6fd249 | 2018-11-19 15:11:16 +0100 | [diff] [blame] | 369 | |
| 370 | struct trailingSlash_class; |
Václav Kubernát | 4108e0d | 2018-10-29 13:32:22 +0100 | [diff] [blame] | 371 | |
| 372 | struct createPathSuggestions_class { |
| 373 | template <typename T, typename Iterator, typename Context> |
| 374 | void on_success(Iterator const& begin, Iterator const&, T&, Context const& context) |
| 375 | { |
| 376 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 377 | const auto& schema = parserContext.m_schema; |
| 378 | |
| 379 | parserContext.m_completionIterator = begin; |
Václav Kubernát | 3a823f4 | 2020-04-29 23:40:21 +0200 | [diff] [blame] | 380 | auto suggestions = schema.availableNodes(parserContext.currentSchemaPath(), Recursion::NonRecursive); |
Václav Kubernát | cb3af40 | 2020-02-12 16:49:17 +0100 | [diff] [blame] | 381 | std::set<Completion> suffixesAdded; |
Václav Kubernát | e69133a | 2019-11-01 19:01:34 +0100 | [diff] [blame] | 382 | std::transform(suggestions.begin(), suggestions.end(), |
Václav Kubernát | 95b0887 | 2020-04-28 01:04:17 +0200 | [diff] [blame] | 383 | std::inserter(suffixesAdded, suffixesAdded.end()), |
| 384 | [&parserContext, &schema](const ModuleNodePair& node) { |
| 385 | std::string completion = (node.first ? *node.first + ":" : "") + node.second; |
Václav Kubernát | e69133a | 2019-11-01 19:01:34 +0100 | [diff] [blame] | 386 | |
Václav Kubernát | 95b0887 | 2020-04-28 01:04:17 +0200 | [diff] [blame] | 387 | if (schema.isLeaf(parserContext.currentSchemaPath(), node)) { |
| 388 | return Completion{completion + " "}; |
| 389 | } |
| 390 | if (schema.isContainer(parserContext.currentSchemaPath(), node)) { |
| 391 | return Completion{completion + "/"}; |
| 392 | } |
| 393 | if (schema.isList(parserContext.currentSchemaPath(), node)) { |
| 394 | return Completion{completion, "[", Completion::WhenToAdd::IfFullMatch}; |
| 395 | } |
| 396 | return Completion{completion}; |
Václav Kubernát | e69133a | 2019-11-01 19:01:34 +0100 | [diff] [blame] | 397 | }); |
| 398 | parserContext.m_suggestions = suffixesAdded; |
Václav Kubernát | 4108e0d | 2018-10-29 13:32:22 +0100 | [diff] [blame] | 399 | } |
| 400 | }; |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 401 | |
Václav Kubernát | cb3af40 | 2020-02-12 16:49:17 +0100 | [diff] [blame] | 402 | std::set<Completion> generateMissingKeyCompletionSet(std::set<std::string> keysNeeded, std::map<std::string, leaf_data_> currentSet); |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 403 | |
| 404 | struct createKeySuggestions_class { |
| 405 | template <typename T, typename Iterator, typename Context> |
| 406 | void on_success(Iterator const& begin, Iterator const&, T&, Context const& context) |
| 407 | { |
| 408 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 409 | const auto& schema = parserContext.m_schema; |
| 410 | |
| 411 | parserContext.m_completionIterator = begin; |
| 412 | |
Václav Kubernát | 72749c6 | 2020-01-03 16:47:34 +0100 | [diff] [blame] | 413 | const auto& keysNeeded = schema.listKeys(parserContext.currentSchemaPath(), {parserContext.m_curModule, parserContext.m_tmpListName}); |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 414 | parserContext.m_suggestions = generateMissingKeyCompletionSet(keysNeeded, parserContext.m_tmpListKeys); |
| 415 | } |
| 416 | }; |
| 417 | |
Václav Kubernát | 43908fb | 2020-01-02 19:05:51 +0100 | [diff] [blame] | 418 | std::string leafDataToCompletion(const leaf_data_& value); |
| 419 | |
| 420 | struct createValueSuggestions_class { |
| 421 | template <typename T, typename Iterator, typename Context> |
| 422 | void on_success(Iterator const& begin, Iterator const&, T&, Context const& context) |
| 423 | { |
| 424 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 425 | if (!parserContext.m_completing) { |
| 426 | return; |
| 427 | } |
| 428 | const auto& dataQuery = parserContext.m_dataquery; |
| 429 | |
| 430 | parserContext.m_completionIterator = begin; |
| 431 | auto listInstances = dataQuery->listKeys(parserContext.currentDataPath(), {parserContext.m_curModule, parserContext.m_tmpListName}); |
| 432 | |
| 433 | decltype(listInstances) filteredInstances; |
| 434 | //This filters out instances, which don't correspond to the partial instance we have. |
| 435 | const auto partialFitsComplete = [&parserContext] (const auto& complete) { |
| 436 | const auto& partial = parserContext.m_tmpListKeys; |
| 437 | return std::all_of(partial.begin(), partial.end(), [&complete] (const auto& oneKV) { |
| 438 | const auto& [k, v] = oneKV; |
| 439 | return complete.at(k) == v; |
| 440 | }); |
| 441 | }; |
| 442 | std::copy_if(listInstances.begin(), listInstances.end(), std::inserter(filteredInstances, filteredInstances.end()), partialFitsComplete); |
| 443 | |
Václav Kubernát | cb3af40 | 2020-02-12 16:49:17 +0100 | [diff] [blame] | 444 | std::set<Completion> validValues; |
Václav Kubernát | 43908fb | 2020-01-02 19:05:51 +0100 | [diff] [blame] | 445 | |
Václav Kubernát | cb3af40 | 2020-02-12 16:49:17 +0100 | [diff] [blame] | 446 | std::transform(filteredInstances.begin(), filteredInstances.end(), std::inserter(validValues, validValues.end()), [&parserContext](const auto& instance) { |
| 447 | return Completion{leafDataToCompletion(instance.at(parserContext.m_tmpListKeyLeafPath.m_node.second))}; |
Václav Kubernát | 43908fb | 2020-01-02 19:05:51 +0100 | [diff] [blame] | 448 | }); |
| 449 | |
| 450 | parserContext.m_suggestions = validValues; |
| 451 | } |
| 452 | }; |
| 453 | |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 454 | struct suggestKeysEnd_class { |
| 455 | template <typename T, typename Iterator, typename Context> |
| 456 | void on_success(Iterator const& begin, Iterator const&, T&, Context const& context) |
| 457 | { |
| 458 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 459 | const auto& schema = parserContext.m_schema; |
| 460 | |
| 461 | parserContext.m_completionIterator = begin; |
Václav Kubernát | 72749c6 | 2020-01-03 16:47:34 +0100 | [diff] [blame] | 462 | const auto& keysNeeded = schema.listKeys(parserContext.currentSchemaPath(), {parserContext.m_curModule, parserContext.m_tmpListName}); |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 463 | if (generateMissingKeyCompletionSet(keysNeeded, parserContext.m_tmpListKeys).empty()) { |
Václav Kubernát | cb3af40 | 2020-02-12 16:49:17 +0100 | [diff] [blame] | 464 | parserContext.m_suggestions = {Completion{"]/"}}; |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 465 | } else { |
Václav Kubernát | cb3af40 | 2020-02-12 16:49:17 +0100 | [diff] [blame] | 466 | parserContext.m_suggestions = {Completion{"]["}}; |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 467 | } |
| 468 | } |
| 469 | }; |
Václav Kubernát | 5727242 | 2019-02-08 12:48:24 +0100 | [diff] [blame] | 470 | |
| 471 | struct commandNamesVisitor { |
| 472 | template <typename T> |
Václav Kubernát | cb3af40 | 2020-02-12 16:49:17 +0100 | [diff] [blame] | 473 | std::string operator()(boost::type<T>) |
Václav Kubernát | 5727242 | 2019-02-08 12:48:24 +0100 | [diff] [blame] | 474 | { |
| 475 | return T::name; |
| 476 | } |
| 477 | }; |
| 478 | |
| 479 | struct createCommandSuggestions_class { |
| 480 | template <typename T, typename Iterator, typename Context> |
| 481 | void on_success(Iterator const& begin, Iterator const&, T&, Context const& context) |
| 482 | { |
| 483 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 484 | parserContext.m_completionIterator = begin; |
| 485 | |
| 486 | parserContext.m_suggestions.clear(); |
| 487 | boost::mpl::for_each<CommandTypes, boost::type<boost::mpl::_>>([&parserContext](auto cmd) { |
Václav Kubernát | 0165e6c | 2020-02-17 18:18:14 +0100 | [diff] [blame] | 488 | parserContext.m_suggestions.insert({commandNamesVisitor()(cmd), " "}); |
Václav Kubernát | 5727242 | 2019-02-08 12:48:24 +0100 | [diff] [blame] | 489 | }); |
| 490 | } |
| 491 | }; |
Václav Kubernát | ac035d6 | 2019-02-18 10:59:08 +0100 | [diff] [blame] | 492 | |
| 493 | struct completing_class { |
| 494 | template <typename T, typename Iterator, typename Context> |
| 495 | void on_success(Iterator const&, Iterator const&, T&, Context const& context) |
| 496 | { |
| 497 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 498 | |
| 499 | if (!parserContext.m_completing) |
| 500 | _pass(context) = false; |
| 501 | } |
| 502 | }; |