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