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 | 195eeea | 2018-05-18 13:52:36 +0200 | [diff] [blame] | 11 | #include "parser_context.hpp" |
Václav Kubernát | 48fc383 | 2018-05-28 14:21:22 +0200 | [diff] [blame] | 12 | #include "schema.hpp" |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 13 | |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 14 | struct keyValue_class { |
| 15 | template <typename T, typename Iterator, typename Context> |
| 16 | void on_success(Iterator const&, Iterator const&, T& ast, Context const& context) |
| 17 | { |
| 18 | auto& parserContext = x3::get<parser_context_tag>(context); |
Václav Kubernát | 48fc383 | 2018-05-28 14:21:22 +0200 | [diff] [blame] | 19 | const Schema& schema = parserContext.m_schema; |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 20 | |
| 21 | if (parserContext.m_tmpListKeys.find(ast.first) != parserContext.m_tmpListKeys.end()) { |
| 22 | _pass(context) = false; |
| 23 | 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^] | 24 | } else if (!schema.listHasKey(parserContext.m_curPath, parserContext.m_tmpListName, ast.first)) { |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 25 | _pass(context) = false; |
| 26 | parserContext.m_errorMsg = parserContext.m_tmpListName + " is not indexed by \"" + ast.first + "\"."; |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame^] | 27 | } else { |
| 28 | parserContext.m_tmpListKeys.insert(ast.first); |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 29 | } |
| 30 | } |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame^] | 31 | |
| 32 | template <typename Iterator, typename Exception, typename Context> |
| 33 | x3::error_handler_result on_error(Iterator&, Iterator const&, Exception const&, Context const& context) |
| 34 | { |
| 35 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 36 | parserContext.m_errorMsg = "Error parsing key values here:"; |
| 37 | return x3::error_handler_result::rethrow; |
| 38 | } |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 39 | }; |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame^] | 40 | |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 41 | struct identifier_class; |
| 42 | |
| 43 | struct listPrefix_class { |
| 44 | template <typename T, typename Iterator, typename Context> |
| 45 | void on_success(Iterator const&, Iterator const&, T& ast, Context const& context) |
| 46 | { |
| 47 | auto& parserContext = x3::get<parser_context_tag>(context); |
Václav Kubernát | 48fc383 | 2018-05-28 14:21:22 +0200 | [diff] [blame] | 48 | const Schema& schema = parserContext.m_schema; |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 49 | |
Václav Kubernát | 48fc383 | 2018-05-28 14:21:22 +0200 | [diff] [blame] | 50 | if (schema.isList(parserContext.m_curPath, ast)) { |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 51 | parserContext.m_tmpListName = ast; |
| 52 | } else { |
| 53 | _pass(context) = false; |
| 54 | } |
| 55 | } |
| 56 | }; |
| 57 | |
| 58 | struct listSuffix_class { |
| 59 | template <typename T, typename Iterator, typename Context> |
| 60 | void on_success(Iterator const&, Iterator const&, T& ast, Context const& context) |
| 61 | { |
| 62 | auto& parserContext = x3::get<parser_context_tag>(context); |
Václav Kubernát | 48fc383 | 2018-05-28 14:21:22 +0200 | [diff] [blame] | 63 | const Schema& schema = parserContext.m_schema; |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 64 | |
Václav Kubernát | 48fc383 | 2018-05-28 14:21:22 +0200 | [diff] [blame] | 65 | const auto& keysNeeded = schema.listKeys(parserContext.m_curPath, parserContext.m_tmpListName); |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 66 | std::set<std::string> keysSupplied; |
| 67 | for (const auto& it : ast) |
| 68 | keysSupplied.insert(it.first); |
| 69 | |
| 70 | if (keysNeeded != keysSupplied) { |
| 71 | parserContext.m_errorMsg = "Not enough keys for " + parserContext.m_tmpListName + ". " + |
| 72 | "These keys were not supplied:"; |
| 73 | std::set<std::string> missingKeys; |
| 74 | std::set_difference(keysNeeded.begin(), keysNeeded.end(), |
| 75 | keysSupplied.begin(), keysSupplied.end(), |
| 76 | std::inserter(missingKeys, missingKeys.end())); |
| 77 | |
| 78 | for (const auto& it : missingKeys) |
| 79 | parserContext.m_errorMsg += " " + it; |
| 80 | parserContext.m_errorMsg += "."; |
| 81 | |
| 82 | _pass(context) = false; |
| 83 | } |
| 84 | } |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame^] | 85 | |
| 86 | template <typename Iterator, typename Exception, typename Context> |
| 87 | x3::error_handler_result on_error(Iterator&, Iterator const&, Exception const&, Context const& context) |
| 88 | { |
| 89 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 90 | if (parserContext.m_errorMsg.empty()) |
| 91 | parserContext.m_errorMsg = "Expecting ']' here:"; |
| 92 | return x3::error_handler_result::rethrow; |
| 93 | |
| 94 | } |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 95 | }; |
| 96 | struct listElement_class { |
| 97 | template <typename T, typename Iterator, typename Context> |
| 98 | void on_success(Iterator const&, Iterator const&, T& ast, Context const& context) |
| 99 | { |
| 100 | auto& parserContext = x3::get<parser_context_tag>(context); |
Václav Kubernát | 814fa41 | 2018-05-25 19:47:18 +0200 | [diff] [blame] | 101 | parserContext.m_curPath.m_nodes.push_back(ast); |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | template <typename Iterator, typename Exception, typename Context> |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame^] | 105 | x3::error_handler_result on_error(Iterator&, Iterator const&, Exception const&, Context const& context) |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 106 | { |
| 107 | auto& parserContext = x3::get<parser_context_tag>(context); |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame^] | 108 | if (parserContext.m_errorMsg.empty()) { |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 109 | return x3::error_handler_result::fail; |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame^] | 110 | } else { |
| 111 | return x3::error_handler_result::rethrow; |
| 112 | } |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 113 | |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 114 | } |
| 115 | }; |
| 116 | |
Václav Kubernát | 60d6f29 | 2018-05-25 09:45:32 +0200 | [diff] [blame] | 117 | struct nodeup_class { |
| 118 | template <typename T, typename Iterator, typename Context> |
| 119 | void on_success(Iterator const&, Iterator const&, T&, Context const& context) |
| 120 | { |
| 121 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 122 | |
Václav Kubernát | 814fa41 | 2018-05-25 19:47:18 +0200 | [diff] [blame] | 123 | if (!parserContext.m_curPath.m_nodes.empty()) { |
| 124 | parserContext.m_curPath.m_nodes.pop_back(); |
Václav Kubernát | 60d6f29 | 2018-05-25 09:45:32 +0200 | [diff] [blame] | 125 | } else { |
| 126 | _pass(context) = false; |
| 127 | } |
Václav Kubernát | 60d6f29 | 2018-05-25 09:45:32 +0200 | [diff] [blame] | 128 | } |
| 129 | }; |
| 130 | |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 131 | struct container_class { |
| 132 | template <typename T, typename Iterator, typename Context> |
| 133 | void on_success(Iterator const&, Iterator const&, T& ast, Context const& context) |
| 134 | { |
| 135 | auto& parserContext = x3::get<parser_context_tag>(context); |
Václav Kubernát | 48fc383 | 2018-05-28 14:21:22 +0200 | [diff] [blame] | 136 | const auto& schema = parserContext.m_schema; |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 137 | |
Václav Kubernát | 48fc383 | 2018-05-28 14:21:22 +0200 | [diff] [blame] | 138 | if (schema.isContainer(parserContext.m_curPath, ast.m_name)) { |
Václav Kubernát | 814fa41 | 2018-05-25 19:47:18 +0200 | [diff] [blame] | 139 | parserContext.m_curPath.m_nodes.push_back(ast); |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 140 | } else { |
| 141 | _pass(context) = false; |
| 142 | } |
| 143 | } |
| 144 | }; |
| 145 | |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 146 | struct leaf_class { |
| 147 | template <typename T, typename Iterator, typename Context> |
| 148 | void on_success(Iterator const&, Iterator const&, T& ast, Context const& context) |
| 149 | { |
| 150 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 151 | const auto& schema = parserContext.m_schema; |
| 152 | |
| 153 | if (schema.isLeaf(parserContext.m_curPath, ast.m_name)) { |
| 154 | parserContext.m_curPath.m_nodes.push_back(ast); |
| 155 | } else { |
| 156 | _pass(context) = false; |
| 157 | } |
| 158 | } |
| 159 | }; |
| 160 | |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 161 | struct path_class { |
| 162 | template <typename T, typename Iterator, typename Context> |
| 163 | void on_success(Iterator const&, Iterator const&, T&, Context const&) |
| 164 | { |
| 165 | } |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 166 | |
| 167 | template <typename Iterator, typename Exception, typename Context> |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame^] | 168 | 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] | 169 | { |
| 170 | auto& parserContext = x3::get<parser_context_tag>(context); |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame^] | 171 | if (parserContext.m_errorMsg.empty()) { |
| 172 | parserContext.m_errorMsg = "Expected path."; |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 173 | return x3::error_handler_result::fail; |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame^] | 174 | } else { |
| 175 | return x3::error_handler_result::rethrow; |
| 176 | } |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 177 | } |
| 178 | }; |
| 179 | |
| 180 | struct data_string_class { |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 181 | }; |
| 182 | |
| 183 | struct cd_class { |
| 184 | template <typename T, typename Iterator, typename Context> |
| 185 | void on_success(Iterator const&, Iterator const&, T&, Context const&) |
| 186 | { |
| 187 | } |
| 188 | |
| 189 | template <typename Iterator, typename Exception, typename Context> |
| 190 | x3::error_handler_result on_error(Iterator&, Iterator const&, Exception const& x, Context const& context) |
| 191 | { |
| 192 | auto& parserContext = x3::get<parser_context_tag>(context); |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame^] | 193 | if (parserContext.m_errorMsg.empty()) |
| 194 | parserContext.m_errorMsg = "Expected " + x.which() + " here:"; |
| 195 | return x3::error_handler_result::rethrow; |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 196 | } |
| 197 | }; |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 198 | |
| 199 | struct presenceContainerPathHandler { |
| 200 | template <typename T, typename Iterator, typename Context> |
| 201 | void on_success(Iterator const&, Iterator const&, T& ast, Context const& context) |
| 202 | { |
| 203 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 204 | const auto& schema = parserContext.m_schema; |
| 205 | try { |
| 206 | container_ cont = boost::get<container_>(ast.m_path.m_nodes.back()); |
| 207 | path_ location{decltype(path_::m_nodes)(parserContext.m_curPath.m_nodes.begin(), |
| 208 | parserContext.m_curPath.m_nodes.end() - 1)}; |
| 209 | |
| 210 | if (!schema.isPresenceContainer(location, cont.m_name)) { |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame^] | 211 | parserContext.m_errorMsg = "This container is not a presence container."; |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 212 | _pass(context) = false; |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 213 | } |
| 214 | } catch (boost::bad_get&) { |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame^] | 215 | parserContext.m_errorMsg = "This is not a container."; |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 216 | _pass(context) = false; |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 217 | } |
| 218 | } |
| 219 | |
| 220 | template <typename Iterator, typename Exception, typename Context> |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame^] | 221 | 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] | 222 | { |
| 223 | auto& parserContext = x3::get<parser_context_tag>(context); |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame^] | 224 | if (parserContext.m_errorMsg.empty()) |
| 225 | parserContext.m_errorMsg = "Couldn't parse create/delete command."; |
| 226 | return x3::error_handler_result::rethrow; |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 227 | } |
| 228 | }; |
| 229 | |
| 230 | struct create_class : public presenceContainerPathHandler { |
| 231 | }; |
| 232 | |
| 233 | struct delete_class : public presenceContainerPathHandler { |
| 234 | }; |
| 235 | |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 236 | struct set_class { |
| 237 | template <typename T, typename Iterator, typename Context> |
| 238 | void on_success(Iterator const&, Iterator const&, T& ast, Context const& context) |
| 239 | { |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame^] | 240 | auto& parserContext = x3::get<parser_context_tag>(context); |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 241 | try { |
| 242 | auto leaf = boost::get<leaf_>(ast.m_path.m_nodes.back()); |
| 243 | } catch (boost::bad_get&) { |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame^] | 244 | parserContext.m_errorMsg = "This is not a leaf."; |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 245 | _pass(context) = false; |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 246 | } |
| 247 | } |
| 248 | |
| 249 | template <typename Iterator, typename Exception, typename Context> |
| 250 | x3::error_handler_result on_error(Iterator&, Iterator const&, Exception const& x, Context const& context) |
| 251 | { |
| 252 | auto& parserContext = x3::get<parser_context_tag>(context); |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame^] | 253 | if (parserContext.m_errorMsg.empty()) |
| 254 | parserContext.m_errorMsg = "Expected " + x.which() + " here:"; |
| 255 | return x3::error_handler_result::rethrow; |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 256 | } |
| 257 | }; |
| 258 | |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 259 | struct command_class { |
| 260 | template <typename Iterator, typename Exception, typename Context> |
| 261 | x3::error_handler_result on_error(Iterator&, Iterator const&, Exception const& x, Context const& context) |
| 262 | { |
| 263 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 264 | 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^] | 265 | if (parserContext.m_errorMsg.empty()) { |
| 266 | parserContext.m_errorMsg = "Unknown command."; |
| 267 | } |
| 268 | error_handler(x.where(), parserContext.m_errorMsg); |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 269 | return x3::error_handler_result::fail; |
| 270 | } |
| 271 | }; |