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> |
| 12 | #include "ast_commands.hpp" |
Václav Kubernát | 195eeea | 2018-05-18 13:52:36 +0200 | [diff] [blame] | 13 | #include "parser_context.hpp" |
Václav Kubernát | 48fc383 | 2018-05-28 14:21:22 +0200 | [diff] [blame] | 14 | #include "schema.hpp" |
Václav Kubernát | ebca255 | 2018-06-08 19:06:02 +0200 | [diff] [blame] | 15 | #include "utils.hpp" |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 16 | namespace x3 = boost::spirit::x3; |
| 17 | |
| 18 | struct parser_context_tag; |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 19 | |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 20 | struct keyValue_class { |
| 21 | template <typename T, typename Iterator, typename Context> |
| 22 | void on_success(Iterator const&, Iterator const&, T& ast, Context const& context) |
| 23 | { |
| 24 | auto& parserContext = x3::get<parser_context_tag>(context); |
Václav Kubernát | 48fc383 | 2018-05-28 14:21:22 +0200 | [diff] [blame] | 25 | const Schema& schema = parserContext.m_schema; |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 26 | |
| 27 | if (parserContext.m_tmpListKeys.find(ast.first) != parserContext.m_tmpListKeys.end()) { |
| 28 | _pass(context) = false; |
| 29 | parserContext.m_errorMsg = "Key \"" + ast.first + "\" was entered more than once."; |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 30 | } else if (!schema.listHasKey(parserContext.m_curPath, {parserContext.m_curModule, parserContext.m_tmpListName}, ast.first)) { |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 31 | _pass(context) = false; |
| 32 | 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] | 33 | } else { |
| 34 | parserContext.m_tmpListKeys.insert(ast.first); |
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 | 89728d8 | 2018-09-13 16:28:28 +0200 | [diff] [blame] | 61 | struct key_identifier_class; |
| 62 | |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 63 | struct module_identifier_class; |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 64 | |
| 65 | struct listPrefix_class { |
| 66 | template <typename T, typename Iterator, typename Context> |
| 67 | void on_success(Iterator const&, Iterator const&, T& ast, Context const& context) |
| 68 | { |
| 69 | auto& parserContext = x3::get<parser_context_tag>(context); |
Václav Kubernát | 48fc383 | 2018-05-28 14:21:22 +0200 | [diff] [blame] | 70 | const Schema& schema = parserContext.m_schema; |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 71 | |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 72 | if (schema.isList(parserContext.m_curPath, {parserContext.m_curModule, ast})) { |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 73 | parserContext.m_tmpListName = ast; |
| 74 | } else { |
| 75 | _pass(context) = false; |
| 76 | } |
| 77 | } |
| 78 | }; |
| 79 | |
| 80 | struct listSuffix_class { |
| 81 | template <typename T, typename Iterator, typename Context> |
| 82 | void on_success(Iterator const&, Iterator const&, T& ast, Context const& context) |
| 83 | { |
| 84 | auto& parserContext = x3::get<parser_context_tag>(context); |
Václav Kubernát | 48fc383 | 2018-05-28 14:21:22 +0200 | [diff] [blame] | 85 | const Schema& schema = parserContext.m_schema; |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 86 | |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 87 | const auto& keysNeeded = schema.listKeys(parserContext.m_curPath, {parserContext.m_curModule, parserContext.m_tmpListName}); |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 88 | std::set<std::string> keysSupplied; |
| 89 | for (const auto& it : ast) |
| 90 | keysSupplied.insert(it.first); |
| 91 | |
| 92 | if (keysNeeded != keysSupplied) { |
| 93 | parserContext.m_errorMsg = "Not enough keys for " + parserContext.m_tmpListName + ". " + |
| 94 | "These keys were not supplied:"; |
| 95 | std::set<std::string> missingKeys; |
| 96 | std::set_difference(keysNeeded.begin(), keysNeeded.end(), |
| 97 | keysSupplied.begin(), keysSupplied.end(), |
| 98 | std::inserter(missingKeys, missingKeys.end())); |
| 99 | |
| 100 | for (const auto& it : missingKeys) |
| 101 | parserContext.m_errorMsg += " " + it; |
| 102 | parserContext.m_errorMsg += "."; |
| 103 | |
| 104 | _pass(context) = false; |
| 105 | } |
| 106 | } |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame] | 107 | |
| 108 | template <typename Iterator, typename Exception, typename Context> |
| 109 | x3::error_handler_result on_error(Iterator&, Iterator const&, Exception const&, Context const& context) |
| 110 | { |
| 111 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 112 | if (parserContext.m_errorMsg.empty()) |
| 113 | parserContext.m_errorMsg = "Expecting ']' here:"; |
| 114 | return x3::error_handler_result::rethrow; |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame] | 115 | } |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 116 | }; |
| 117 | struct listElement_class { |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 118 | template <typename Iterator, typename Exception, typename Context> |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame] | 119 | 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] | 120 | { |
| 121 | auto& parserContext = x3::get<parser_context_tag>(context); |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame] | 122 | if (parserContext.m_errorMsg.empty()) { |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 123 | return x3::error_handler_result::fail; |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame] | 124 | } else { |
| 125 | return x3::error_handler_result::rethrow; |
| 126 | } |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 127 | } |
| 128 | }; |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 129 | struct list_class { |
| 130 | template <typename T, typename Iterator, typename Context> |
| 131 | void on_success(Iterator const&, Iterator const&, T& ast, Context const& context) |
| 132 | { |
| 133 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 134 | const Schema& schema = parserContext.m_schema; |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 135 | |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 136 | if (!schema.isList(parserContext.m_curPath, {parserContext.m_curModule, ast.m_name})) { |
| 137 | _pass(context) = false; |
| 138 | } |
| 139 | } |
| 140 | }; |
Václav Kubernát | 60d6f29 | 2018-05-25 09:45:32 +0200 | [diff] [blame] | 141 | struct nodeup_class { |
| 142 | template <typename T, typename Iterator, typename Context> |
| 143 | void on_success(Iterator const&, Iterator const&, T&, Context const& context) |
| 144 | { |
| 145 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 146 | |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 147 | if (parserContext.m_curPath.m_nodes.empty()) |
Václav Kubernát | 60d6f29 | 2018-05-25 09:45:32 +0200 | [diff] [blame] | 148 | _pass(context) = false; |
Václav Kubernát | 60d6f29 | 2018-05-25 09:45:32 +0200 | [diff] [blame] | 149 | } |
| 150 | }; |
| 151 | |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 152 | struct container_class { |
| 153 | template <typename T, typename Iterator, typename Context> |
| 154 | void on_success(Iterator const&, Iterator const&, T& ast, Context const& context) |
| 155 | { |
| 156 | auto& parserContext = x3::get<parser_context_tag>(context); |
Václav Kubernát | 48fc383 | 2018-05-28 14:21:22 +0200 | [diff] [blame] | 157 | const auto& schema = parserContext.m_schema; |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 158 | |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 159 | if (!schema.isContainer(parserContext.m_curPath, {parserContext.m_curModule, ast.m_name})) |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 160 | _pass(context) = false; |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 161 | } |
| 162 | }; |
| 163 | |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 164 | struct leaf_class { |
| 165 | template <typename T, typename Iterator, typename Context> |
| 166 | void on_success(Iterator const&, Iterator const&, T& ast, Context const& context) |
| 167 | { |
| 168 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 169 | const auto& schema = parserContext.m_schema; |
| 170 | |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 171 | if (!schema.isLeaf(parserContext.m_curPath, {parserContext.m_curModule, ast.m_name})) |
| 172 | _pass(context) = false; |
| 173 | } |
| 174 | }; |
| 175 | |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 176 | struct module_class { |
| 177 | template <typename T, typename Iterator, typename Context> |
| 178 | void on_success(Iterator const&, Iterator const&, T& ast, Context const& context) |
| 179 | { |
| 180 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 181 | const auto& schema = parserContext.m_schema; |
| 182 | |
| 183 | if (schema.isModule(parserContext.m_curPath, ast.m_name)) { |
| 184 | parserContext.m_curModule = ast.m_name; |
| 185 | parserContext.m_topLevelModulePresent = true; |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 186 | } else { |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 187 | parserContext.m_errorMsg = "Invalid module name."; |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 188 | _pass(context) = false; |
| 189 | } |
| 190 | } |
| 191 | }; |
| 192 | |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 193 | struct schemaNode_class { |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 194 | template <typename T, typename Iterator, typename Context> |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 195 | void on_success(Iterator const&, Iterator const&, T& ast, Context const& context) |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 196 | { |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 197 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 198 | if (ast.m_suffix.type() == typeid(nodeup_)) { |
| 199 | parserContext.m_curPath.m_nodes.pop_back(); |
| 200 | if (parserContext.m_curPath.m_nodes.empty()) |
| 201 | parserContext.m_topLevelModulePresent = false; |
| 202 | } else { |
| 203 | parserContext.m_curPath.m_nodes.push_back(ast); |
| 204 | parserContext.m_curModule = boost::none; |
| 205 | } |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 206 | } |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 207 | }; |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 208 | |
Václav Kubernát | 39ae959 | 2019-02-05 17:35:16 +0100 | [diff] [blame] | 209 | struct dataNodeList_class { |
| 210 | template <typename T, typename Iterator, typename Context> |
| 211 | void on_success(Iterator const&, Iterator const&, T& ast, Context const& context) |
| 212 | { |
| 213 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 214 | parserContext.m_curPath.m_nodes.push_back(dataNodeToSchemaNode(ast)); |
| 215 | } |
| 216 | }; |
Václav Kubernát | 5c75b25 | 2018-10-10 18:33:47 +0200 | [diff] [blame] | 217 | |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 218 | struct dataNode_class { |
| 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 | if (ast.m_suffix.type() == typeid(nodeup_)) { |
| 224 | parserContext.m_curPath.m_nodes.pop_back(); |
| 225 | if (parserContext.m_curPath.m_nodes.empty()) |
| 226 | parserContext.m_topLevelModulePresent = false; |
| 227 | } else { |
| 228 | parserContext.m_curPath.m_nodes.push_back(dataNodeToSchemaNode(ast)); |
| 229 | parserContext.m_curModule = boost::none; |
| 230 | } |
| 231 | } |
| 232 | }; |
| 233 | |
Václav Kubernát | 37171a1 | 2018-08-31 17:01:48 +0200 | [diff] [blame] | 234 | struct absoluteStart_class { |
| 235 | template <typename T, typename Iterator, typename Context> |
| 236 | void on_success(Iterator const&, Iterator const&, T&, Context const& context) |
| 237 | { |
| 238 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 239 | parserContext.m_curPath.m_nodes.clear(); |
| 240 | } |
| 241 | }; |
| 242 | |
Václav Kubernát | 5c75b25 | 2018-10-10 18:33:47 +0200 | [diff] [blame] | 243 | struct dataNodesListEnd_class; |
| 244 | |
| 245 | struct dataPathListEnd_class; |
| 246 | |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 247 | struct dataPath_class { |
| 248 | template <typename Iterator, typename Exception, typename Context> |
| 249 | x3::error_handler_result on_error(Iterator&, Iterator const&, Exception const&, Context const& context) |
| 250 | { |
| 251 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 252 | if (parserContext.m_errorMsg.empty()) { |
| 253 | parserContext.m_errorMsg = "Expected path."; |
| 254 | return x3::error_handler_result::fail; |
| 255 | } else { |
| 256 | return x3::error_handler_result::rethrow; |
| 257 | } |
| 258 | } |
| 259 | }; |
| 260 | |
| 261 | struct schemaPath_class { |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 262 | template <typename Iterator, typename Exception, typename Context> |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame] | 263 | 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] | 264 | { |
| 265 | auto& parserContext = x3::get<parser_context_tag>(context); |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame] | 266 | if (parserContext.m_errorMsg.empty()) { |
| 267 | parserContext.m_errorMsg = "Expected path."; |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 268 | return x3::error_handler_result::fail; |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame] | 269 | } else { |
| 270 | return x3::error_handler_result::rethrow; |
| 271 | } |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 272 | } |
| 273 | }; |
| 274 | |
Václav Kubernát | 6d79143 | 2018-10-25 16:00:35 +0200 | [diff] [blame] | 275 | struct discard_class; |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 276 | |
Václav Kubernát | 11afac7 | 2018-07-18 14:59:53 +0200 | [diff] [blame] | 277 | struct ls_class; |
| 278 | |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 279 | struct cd_class { |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 280 | template <typename Iterator, typename Exception, typename Context> |
| 281 | x3::error_handler_result on_error(Iterator&, Iterator const&, Exception const& x, Context const& context) |
| 282 | { |
| 283 | auto& parserContext = x3::get<parser_context_tag>(context); |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame] | 284 | if (parserContext.m_errorMsg.empty()) |
| 285 | parserContext.m_errorMsg = "Expected " + x.which() + " here:"; |
| 286 | return x3::error_handler_result::rethrow; |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 287 | } |
| 288 | }; |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 289 | |
| 290 | struct presenceContainerPathHandler { |
| 291 | template <typename T, typename Iterator, typename Context> |
| 292 | void on_success(Iterator const&, Iterator const&, T& ast, Context const& context) |
| 293 | { |
| 294 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 295 | const auto& schema = parserContext.m_schema; |
| 296 | try { |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 297 | boost::optional<std::string> module; |
| 298 | if (ast.m_path.m_nodes.back().m_prefix) |
| 299 | module = ast.m_path.m_nodes.back().m_prefix.value().m_name; |
| 300 | container_ cont = boost::get<container_>(ast.m_path.m_nodes.back().m_suffix); |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 301 | schemaPath_ location = pathWithoutLastNode(parserContext.m_curPath); |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 302 | |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 303 | if (!schema.isPresenceContainer(location, {module, cont.m_name})) { |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame] | 304 | parserContext.m_errorMsg = "This container is not a presence container."; |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 305 | _pass(context) = false; |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 306 | } |
| 307 | } catch (boost::bad_get&) { |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame] | 308 | parserContext.m_errorMsg = "This is not a container."; |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 309 | _pass(context) = false; |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 310 | } |
| 311 | } |
| 312 | |
| 313 | template <typename Iterator, typename Exception, typename Context> |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame] | 314 | 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] | 315 | { |
| 316 | auto& parserContext = x3::get<parser_context_tag>(context); |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame] | 317 | if (parserContext.m_errorMsg.empty()) |
| 318 | parserContext.m_errorMsg = "Couldn't parse create/delete command."; |
| 319 | return x3::error_handler_result::rethrow; |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 320 | } |
| 321 | }; |
| 322 | |
| 323 | struct create_class : public presenceContainerPathHandler { |
| 324 | }; |
| 325 | |
| 326 | struct delete_class : public presenceContainerPathHandler { |
| 327 | }; |
| 328 | |
Václav Kubernát | 0b0272f | 2018-06-13 14:13:08 +0200 | [diff] [blame] | 329 | struct leaf_path_class { |
| 330 | template <typename T, typename Iterator, typename Context> |
| 331 | void on_success(Iterator const&, Iterator const&, T& ast, Context const& context) |
| 332 | { |
| 333 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 334 | try { |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 335 | auto leaf = boost::get<leaf_>(ast.m_nodes.back().m_suffix); |
Václav Kubernát | 0b0272f | 2018-06-13 14:13:08 +0200 | [diff] [blame] | 336 | } catch (boost::bad_get&) { |
| 337 | parserContext.m_errorMsg = "This is not a path to leaf."; |
| 338 | _pass(context) = false; |
| 339 | } |
| 340 | } |
| 341 | }; |
| 342 | |
Václav Kubernát | ebca255 | 2018-06-08 19:06:02 +0200 | [diff] [blame] | 343 | struct leaf_data_class { |
Václav Kubernát | 0b0272f | 2018-06-13 14:13:08 +0200 | [diff] [blame] | 344 | template <typename Iterator, typename Exception, typename Context> |
| 345 | x3::error_handler_result on_error(Iterator&, Iterator const&, Exception const&, Context const& context) |
| 346 | { |
| 347 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 348 | auto& schema = parserContext.m_schema; |
| 349 | if (parserContext.m_errorMsg.empty()) { |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 350 | leaf_ leaf = boost::get<leaf_>(parserContext.m_curPath.m_nodes.back().m_suffix); |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 351 | schemaPath_ location = pathWithoutLastNode(parserContext.m_curPath); |
| 352 | if (location.m_nodes.empty()) { |
Václav Kubernát | 5c75b25 | 2018-10-10 18:33:47 +0200 | [diff] [blame] | 353 | parserContext.m_curModule = parserContext.m_curPath.m_nodes.back().m_prefix->m_name; |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 354 | } |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 355 | parserContext.m_errorMsg = "Expected " + leafDataTypeToString(schema.leafType(location, {parserContext.m_curModule, leaf.m_name})) + " here:"; |
Václav Kubernát | 0b0272f | 2018-06-13 14:13:08 +0200 | [diff] [blame] | 356 | return x3::error_handler_result::fail; |
| 357 | } |
| 358 | return x3::error_handler_result::rethrow; |
| 359 | } |
Václav Kubernát | ebca255 | 2018-06-08 19:06:02 +0200 | [diff] [blame] | 360 | }; |
| 361 | |
| 362 | struct leaf_data_base_class { |
| 363 | yang::LeafDataTypes m_type; |
| 364 | |
| 365 | leaf_data_base_class(yang::LeafDataTypes type) |
| 366 | : m_type(type) |
| 367 | { |
| 368 | } |
| 369 | |
| 370 | template <typename T, typename Iterator, typename Context> |
| 371 | void on_success(Iterator const&, Iterator const&, T&, Context const& context) |
| 372 | { |
| 373 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 374 | auto& schema = parserContext.m_schema; |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 375 | boost::optional<std::string> module; |
| 376 | if (parserContext.m_curPath.m_nodes.back().m_prefix) |
| 377 | module = parserContext.m_curPath.m_nodes.back().m_prefix.value().m_name; |
Václav Kubernát | ebca255 | 2018-06-08 19:06:02 +0200 | [diff] [blame] | 378 | |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 379 | leaf_ leaf = boost::get<leaf_>(parserContext.m_curPath.m_nodes.back().m_suffix); |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 380 | schemaPath_ location = pathWithoutLastNode(parserContext.m_curPath); |
Václav Kubernát | ebca255 | 2018-06-08 19:06:02 +0200 | [diff] [blame] | 381 | |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 382 | if (schema.leafType(location, {module, leaf.m_name}) != m_type) { |
Václav Kubernát | ebca255 | 2018-06-08 19:06:02 +0200 | [diff] [blame] | 383 | _pass(context) = false; |
| 384 | } |
| 385 | } |
| 386 | }; |
| 387 | |
| 388 | struct leaf_data_enum_class : leaf_data_base_class { |
| 389 | leaf_data_enum_class() |
| 390 | : leaf_data_base_class(yang::LeafDataTypes::Enum) |
| 391 | { |
| 392 | } |
| 393 | |
| 394 | template <typename T, typename Iterator, typename Context> |
| 395 | void on_success(Iterator const& start, Iterator const& end, T& ast, Context const& context) |
| 396 | { |
| 397 | leaf_data_base_class::on_success(start, end, ast, context); |
| 398 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 399 | auto& schema = parserContext.m_schema; |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 400 | boost::optional<std::string> module; |
| 401 | if (parserContext.m_curPath.m_nodes.back().m_prefix) |
| 402 | module = parserContext.m_curPath.m_nodes.back().m_prefix.value().m_name; |
Václav Kubernát | ebca255 | 2018-06-08 19:06:02 +0200 | [diff] [blame] | 403 | |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 404 | leaf_ leaf = boost::get<leaf_>(parserContext.m_curPath.m_nodes.back().m_suffix); |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 405 | schemaPath_ location = pathWithoutLastNode(parserContext.m_curPath); |
Václav Kubernát | ebca255 | 2018-06-08 19:06:02 +0200 | [diff] [blame] | 406 | |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 407 | if (!schema.leafEnumHasValue(location, {module, leaf.m_name}, ast.m_value)) { |
Václav Kubernát | ebca255 | 2018-06-08 19:06:02 +0200 | [diff] [blame] | 408 | _pass(context) = false; |
| 409 | } |
| 410 | } |
| 411 | }; |
| 412 | |
| 413 | struct leaf_data_decimal_class : leaf_data_base_class { |
| 414 | leaf_data_decimal_class() |
| 415 | : leaf_data_base_class(yang::LeafDataTypes::Decimal) |
| 416 | { |
| 417 | } |
| 418 | }; |
| 419 | |
| 420 | struct leaf_data_bool_class : leaf_data_base_class { |
| 421 | leaf_data_bool_class() |
| 422 | : leaf_data_base_class(yang::LeafDataTypes::Bool) |
| 423 | { |
| 424 | } |
| 425 | }; |
| 426 | |
| 427 | struct leaf_data_int_class : leaf_data_base_class { |
| 428 | leaf_data_int_class() |
| 429 | : leaf_data_base_class(yang::LeafDataTypes::Int) |
| 430 | { |
| 431 | } |
| 432 | }; |
| 433 | |
| 434 | struct leaf_data_uint_class : leaf_data_base_class { |
| 435 | leaf_data_uint_class() |
| 436 | : leaf_data_base_class(yang::LeafDataTypes::Uint) |
| 437 | { |
| 438 | } |
| 439 | }; |
| 440 | |
| 441 | struct leaf_data_string_class : leaf_data_base_class { |
| 442 | leaf_data_string_class() |
| 443 | : leaf_data_base_class(yang::LeafDataTypes::String) |
| 444 | { |
| 445 | } |
| 446 | }; |
| 447 | |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 448 | struct set_class { |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 449 | template <typename Iterator, typename Exception, typename Context> |
| 450 | x3::error_handler_result on_error(Iterator&, Iterator const&, Exception const& x, Context const& context) |
| 451 | { |
| 452 | auto& parserContext = x3::get<parser_context_tag>(context); |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame] | 453 | if (parserContext.m_errorMsg.empty()) |
| 454 | parserContext.m_errorMsg = "Expected " + x.which() + " here:"; |
| 455 | return x3::error_handler_result::rethrow; |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 456 | } |
| 457 | }; |
| 458 | |
Václav Kubernát | 812ee28 | 2018-08-30 17:10:03 +0200 | [diff] [blame] | 459 | struct commit_class; |
| 460 | |
Václav Kubernát | b6ff0b6 | 2018-08-30 16:14:53 +0200 | [diff] [blame] | 461 | struct get_class; |
| 462 | |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 463 | struct command_class { |
| 464 | template <typename Iterator, typename Exception, typename Context> |
| 465 | x3::error_handler_result on_error(Iterator&, Iterator const&, Exception const& x, Context const& context) |
| 466 | { |
| 467 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 468 | 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] | 469 | if (parserContext.m_errorMsg.empty()) { |
| 470 | parserContext.m_errorMsg = "Unknown command."; |
| 471 | } |
| 472 | error_handler(x.where(), parserContext.m_errorMsg); |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 473 | return x3::error_handler_result::fail; |
| 474 | } |
| 475 | }; |
Václav Kubernát | 5c75b25 | 2018-10-10 18:33:47 +0200 | [diff] [blame] | 476 | |
Václav Kubernát | 4108e0d | 2018-10-29 13:32:22 +0100 | [diff] [blame] | 477 | struct initializePath_class { |
Václav Kubernát | 5c75b25 | 2018-10-10 18:33:47 +0200 | [diff] [blame] | 478 | template <typename T, typename Iterator, typename Context> |
| 479 | void on_success(Iterator const&, Iterator const&, T&, Context const& context) |
| 480 | { |
| 481 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 482 | parserContext.m_curPath = parserContext.m_curPathOrig; |
| 483 | parserContext.m_tmpListKeys.clear(); |
| 484 | parserContext.m_tmpListName.clear(); |
Václav Kubernát | 4108e0d | 2018-10-29 13:32:22 +0100 | [diff] [blame] | 485 | parserContext.m_suggestions.clear(); |
Václav Kubernát | 5c75b25 | 2018-10-10 18:33:47 +0200 | [diff] [blame] | 486 | if (!parserContext.m_curPath.m_nodes.empty() && parserContext.m_curPath.m_nodes.at(0).m_prefix) |
| 487 | parserContext.m_topLevelModulePresent = true; |
| 488 | else |
| 489 | parserContext.m_topLevelModulePresent = false; |
| 490 | } |
| 491 | }; |
Václav Kubernát | d6fd249 | 2018-11-19 15:11:16 +0100 | [diff] [blame] | 492 | |
| 493 | struct trailingSlash_class; |
Václav Kubernát | 4108e0d | 2018-10-29 13:32:22 +0100 | [diff] [blame] | 494 | |
| 495 | struct createPathSuggestions_class { |
| 496 | template <typename T, typename Iterator, typename Context> |
| 497 | void on_success(Iterator const& begin, Iterator const&, T&, Context const& context) |
| 498 | { |
| 499 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 500 | const auto& schema = parserContext.m_schema; |
| 501 | |
| 502 | parserContext.m_completionIterator = begin; |
| 503 | parserContext.m_suggestions = schema.childNodes(parserContext.m_curPath, Recursion::NonRecursive); |
| 504 | } |
| 505 | }; |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 506 | |
| 507 | std::set<std::string> generateMissingKeyCompletionSet(std::set<std::string> keysNeeded, std::set<std::string> currentSet); |
| 508 | |
| 509 | struct createKeySuggestions_class { |
| 510 | template <typename T, typename Iterator, typename Context> |
| 511 | void on_success(Iterator const& begin, Iterator const&, T&, Context const& context) |
| 512 | { |
| 513 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 514 | const auto& schema = parserContext.m_schema; |
| 515 | |
| 516 | parserContext.m_completionIterator = begin; |
| 517 | |
| 518 | const auto& keysNeeded = schema.listKeys(parserContext.m_curPath, {parserContext.m_curModule, parserContext.m_tmpListName}); |
| 519 | parserContext.m_suggestions = generateMissingKeyCompletionSet(keysNeeded, parserContext.m_tmpListKeys); |
| 520 | } |
| 521 | }; |
| 522 | |
| 523 | struct suggestKeysEnd_class { |
| 524 | template <typename T, typename Iterator, typename Context> |
| 525 | void on_success(Iterator const& begin, Iterator const&, T&, Context const& context) |
| 526 | { |
| 527 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 528 | const auto& schema = parserContext.m_schema; |
| 529 | |
| 530 | parserContext.m_completionIterator = begin; |
| 531 | const auto& keysNeeded = schema.listKeys(parserContext.m_curPath, {parserContext.m_curModule, parserContext.m_tmpListName}); |
| 532 | if (generateMissingKeyCompletionSet(keysNeeded, parserContext.m_tmpListKeys).empty()) { |
| 533 | parserContext.m_suggestions = {"]/"}; |
| 534 | } else { |
| 535 | parserContext.m_suggestions = {"]"}; |
| 536 | } |
| 537 | } |
| 538 | }; |
Václav Kubernát | 5727242 | 2019-02-08 12:48:24 +0100 | [diff] [blame] | 539 | |
| 540 | struct commandNamesVisitor { |
| 541 | template <typename T> |
| 542 | auto operator()(boost::type<T>) |
| 543 | { |
| 544 | return T::name; |
| 545 | } |
| 546 | }; |
| 547 | |
| 548 | struct createCommandSuggestions_class { |
| 549 | template <typename T, typename Iterator, typename Context> |
| 550 | void on_success(Iterator const& begin, Iterator const&, T&, Context const& context) |
| 551 | { |
| 552 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 553 | parserContext.m_completionIterator = begin; |
| 554 | |
| 555 | parserContext.m_suggestions.clear(); |
| 556 | boost::mpl::for_each<CommandTypes, boost::type<boost::mpl::_>>([&parserContext](auto cmd) { |
| 557 | parserContext.m_suggestions.emplace(commandNamesVisitor()(cmd)); |
| 558 | }); |
| 559 | } |
| 560 | }; |
Václav Kubernát | ac035d6 | 2019-02-18 10:59:08 +0100 | [diff] [blame] | 561 | |
| 562 | struct completing_class { |
| 563 | template <typename T, typename Iterator, typename Context> |
| 564 | void on_success(Iterator const&, Iterator const&, T&, Context const& context) |
| 565 | { |
| 566 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 567 | |
| 568 | if (!parserContext.m_completing) |
| 569 | _pass(context) = false; |
| 570 | } |
| 571 | }; |