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