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 | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 14 | |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 15 | struct keyValue_class { |
| 16 | template <typename T, typename Iterator, typename Context> |
| 17 | void on_success(Iterator const&, Iterator const&, T& ast, Context const& context) |
| 18 | { |
| 19 | auto& parserContext = x3::get<parser_context_tag>(context); |
Václav Kubernát | 48fc383 | 2018-05-28 14:21:22 +0200 | [diff] [blame] | 20 | const Schema& schema = parserContext.m_schema; |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 21 | |
| 22 | if (parserContext.m_tmpListKeys.find(ast.first) != parserContext.m_tmpListKeys.end()) { |
| 23 | _pass(context) = false; |
| 24 | 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^] | 25 | } 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] | 26 | _pass(context) = false; |
| 27 | 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] | 28 | } else { |
| 29 | parserContext.m_tmpListKeys.insert(ast.first); |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 30 | } |
| 31 | } |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame] | 32 | |
| 33 | template <typename Iterator, typename Exception, typename Context> |
| 34 | x3::error_handler_result on_error(Iterator&, Iterator const&, Exception const&, Context const& context) |
| 35 | { |
| 36 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 37 | parserContext.m_errorMsg = "Error parsing key values here:"; |
| 38 | return x3::error_handler_result::rethrow; |
| 39 | } |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 40 | }; |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame] | 41 | |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame^] | 42 | struct node_identifier_class { |
| 43 | template <typename T, typename Iterator, typename Context> |
| 44 | void on_success(Iterator const&, Iterator const&, T&, Context const& context) |
| 45 | { |
| 46 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 47 | |
| 48 | if (!parserContext.m_topLevelModulePresent) { |
| 49 | if (parserContext.m_errorMsg.empty()) |
| 50 | parserContext.m_errorMsg = "You have to specify a top level module."; |
| 51 | _pass(context) = false; |
| 52 | } |
| 53 | } |
| 54 | }; |
| 55 | |
| 56 | struct module_identifier_class; |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 57 | |
| 58 | struct listPrefix_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 | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame^] | 65 | if (schema.isList(parserContext.m_curPath, {parserContext.m_curModule, ast})) { |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 66 | parserContext.m_tmpListName = ast; |
| 67 | } else { |
| 68 | _pass(context) = false; |
| 69 | } |
| 70 | } |
| 71 | }; |
| 72 | |
| 73 | struct listSuffix_class { |
| 74 | template <typename T, typename Iterator, typename Context> |
| 75 | void on_success(Iterator const&, Iterator const&, T& ast, Context const& context) |
| 76 | { |
| 77 | auto& parserContext = x3::get<parser_context_tag>(context); |
Václav Kubernát | 48fc383 | 2018-05-28 14:21:22 +0200 | [diff] [blame] | 78 | const Schema& schema = parserContext.m_schema; |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 79 | |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame^] | 80 | 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] | 81 | std::set<std::string> keysSupplied; |
| 82 | for (const auto& it : ast) |
| 83 | keysSupplied.insert(it.first); |
| 84 | |
| 85 | if (keysNeeded != keysSupplied) { |
| 86 | parserContext.m_errorMsg = "Not enough keys for " + parserContext.m_tmpListName + ". " + |
| 87 | "These keys were not supplied:"; |
| 88 | std::set<std::string> missingKeys; |
| 89 | std::set_difference(keysNeeded.begin(), keysNeeded.end(), |
| 90 | keysSupplied.begin(), keysSupplied.end(), |
| 91 | std::inserter(missingKeys, missingKeys.end())); |
| 92 | |
| 93 | for (const auto& it : missingKeys) |
| 94 | parserContext.m_errorMsg += " " + it; |
| 95 | parserContext.m_errorMsg += "."; |
| 96 | |
| 97 | _pass(context) = false; |
| 98 | } |
| 99 | } |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame] | 100 | |
| 101 | template <typename Iterator, typename Exception, typename Context> |
| 102 | x3::error_handler_result on_error(Iterator&, Iterator const&, Exception const&, Context const& context) |
| 103 | { |
| 104 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 105 | if (parserContext.m_errorMsg.empty()) |
| 106 | parserContext.m_errorMsg = "Expecting ']' here:"; |
| 107 | return x3::error_handler_result::rethrow; |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame] | 108 | } |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 109 | }; |
| 110 | struct listElement_class { |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 111 | template <typename Iterator, typename Exception, typename Context> |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame] | 112 | 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] | 113 | { |
| 114 | auto& parserContext = x3::get<parser_context_tag>(context); |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame] | 115 | if (parserContext.m_errorMsg.empty()) { |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 116 | return x3::error_handler_result::fail; |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame] | 117 | } else { |
| 118 | return x3::error_handler_result::rethrow; |
| 119 | } |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 120 | } |
| 121 | }; |
| 122 | |
Václav Kubernát | 60d6f29 | 2018-05-25 09:45:32 +0200 | [diff] [blame] | 123 | struct nodeup_class { |
| 124 | template <typename T, typename Iterator, typename Context> |
| 125 | void on_success(Iterator const&, Iterator const&, T&, Context const& context) |
| 126 | { |
| 127 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 128 | |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame^] | 129 | if (parserContext.m_curPath.m_nodes.empty()) |
Václav Kubernát | 60d6f29 | 2018-05-25 09:45:32 +0200 | [diff] [blame] | 130 | _pass(context) = false; |
Václav Kubernát | 60d6f29 | 2018-05-25 09:45:32 +0200 | [diff] [blame] | 131 | } |
| 132 | }; |
| 133 | |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 134 | struct container_class { |
| 135 | template <typename T, typename Iterator, typename Context> |
| 136 | void on_success(Iterator const&, Iterator const&, T& ast, Context const& context) |
| 137 | { |
| 138 | auto& parserContext = x3::get<parser_context_tag>(context); |
Václav Kubernát | 48fc383 | 2018-05-28 14:21:22 +0200 | [diff] [blame] | 139 | const auto& schema = parserContext.m_schema; |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 140 | |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame^] | 141 | 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] | 142 | _pass(context) = false; |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 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 | |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame^] | 153 | if (!schema.isLeaf(parserContext.m_curPath, {parserContext.m_curModule, ast.m_name})) |
| 154 | _pass(context) = false; |
| 155 | } |
| 156 | }; |
| 157 | |
| 158 | |
| 159 | struct module_class { |
| 160 | template <typename T, typename Iterator, typename Context> |
| 161 | void on_success(Iterator const&, Iterator const&, T& ast, Context const& context) |
| 162 | { |
| 163 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 164 | const auto& schema = parserContext.m_schema; |
| 165 | |
| 166 | if (schema.isModule(parserContext.m_curPath, ast.m_name)) { |
| 167 | parserContext.m_curModule = ast.m_name; |
| 168 | parserContext.m_topLevelModulePresent = true; |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 169 | } else { |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame^] | 170 | parserContext.m_errorMsg = "Invalid module name."; |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 171 | _pass(context) = false; |
| 172 | } |
| 173 | } |
| 174 | }; |
| 175 | |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame^] | 176 | struct node_class { |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 177 | template <typename T, typename Iterator, typename Context> |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame^] | 178 | 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] | 179 | { |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame^] | 180 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 181 | if (ast.m_suffix.type() == typeid(nodeup_)) { |
| 182 | parserContext.m_curPath.m_nodes.pop_back(); |
| 183 | if (parserContext.m_curPath.m_nodes.empty()) |
| 184 | parserContext.m_topLevelModulePresent = false; |
| 185 | } else { |
| 186 | parserContext.m_curPath.m_nodes.push_back(ast); |
| 187 | parserContext.m_curModule = boost::none; |
| 188 | } |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 189 | } |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame^] | 190 | }; |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 191 | |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame^] | 192 | struct path_class { |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 193 | template <typename Iterator, typename Exception, typename Context> |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame] | 194 | 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] | 195 | { |
| 196 | auto& parserContext = x3::get<parser_context_tag>(context); |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame] | 197 | if (parserContext.m_errorMsg.empty()) { |
| 198 | parserContext.m_errorMsg = "Expected path."; |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 199 | return x3::error_handler_result::fail; |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame] | 200 | } else { |
| 201 | return x3::error_handler_result::rethrow; |
| 202 | } |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 203 | } |
| 204 | }; |
| 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 | struct cd_class { |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 208 | template <typename Iterator, typename Exception, typename Context> |
| 209 | x3::error_handler_result on_error(Iterator&, Iterator const&, Exception const& x, Context const& context) |
| 210 | { |
| 211 | auto& parserContext = x3::get<parser_context_tag>(context); |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame] | 212 | if (parserContext.m_errorMsg.empty()) |
| 213 | parserContext.m_errorMsg = "Expected " + x.which() + " here:"; |
| 214 | return x3::error_handler_result::rethrow; |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 215 | } |
| 216 | }; |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 217 | |
| 218 | struct presenceContainerPathHandler { |
| 219 | template <typename T, typename Iterator, typename Context> |
| 220 | void on_success(Iterator const&, Iterator const&, T& ast, Context const& context) |
| 221 | { |
| 222 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 223 | const auto& schema = parserContext.m_schema; |
| 224 | try { |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame^] | 225 | boost::optional<std::string> module; |
| 226 | if (ast.m_path.m_nodes.back().m_prefix) |
| 227 | module = ast.m_path.m_nodes.back().m_prefix.value().m_name; |
| 228 | container_ cont = boost::get<container_>(ast.m_path.m_nodes.back().m_suffix); |
Václav Kubernát | ebca255 | 2018-06-08 19:06:02 +0200 | [diff] [blame] | 229 | path_ location = pathWithoutLastNode(parserContext.m_curPath); |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 230 | |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame^] | 231 | if (!schema.isPresenceContainer(location, {module, cont.m_name})) { |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame] | 232 | parserContext.m_errorMsg = "This container is not a presence container."; |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 233 | _pass(context) = false; |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 234 | } |
| 235 | } catch (boost::bad_get&) { |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame] | 236 | parserContext.m_errorMsg = "This is not a container."; |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 237 | _pass(context) = false; |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 238 | } |
| 239 | } |
| 240 | |
| 241 | template <typename Iterator, typename Exception, typename Context> |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame] | 242 | 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] | 243 | { |
| 244 | auto& parserContext = x3::get<parser_context_tag>(context); |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame] | 245 | if (parserContext.m_errorMsg.empty()) |
| 246 | parserContext.m_errorMsg = "Couldn't parse create/delete command."; |
| 247 | return x3::error_handler_result::rethrow; |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 248 | } |
| 249 | }; |
| 250 | |
| 251 | struct create_class : public presenceContainerPathHandler { |
| 252 | }; |
| 253 | |
| 254 | struct delete_class : public presenceContainerPathHandler { |
| 255 | }; |
| 256 | |
Václav Kubernát | 0b0272f | 2018-06-13 14:13:08 +0200 | [diff] [blame] | 257 | struct leaf_path_class { |
| 258 | template <typename T, typename Iterator, typename Context> |
| 259 | void on_success(Iterator const&, Iterator const&, T& ast, Context const& context) |
| 260 | { |
| 261 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 262 | try { |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame^] | 263 | 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] | 264 | } catch (boost::bad_get&) { |
| 265 | parserContext.m_errorMsg = "This is not a path to leaf."; |
| 266 | _pass(context) = false; |
| 267 | } |
| 268 | } |
| 269 | }; |
| 270 | |
Václav Kubernát | ebca255 | 2018-06-08 19:06:02 +0200 | [diff] [blame] | 271 | struct leaf_data_class { |
Václav Kubernát | 0b0272f | 2018-06-13 14:13:08 +0200 | [diff] [blame] | 272 | template <typename Iterator, typename Exception, typename Context> |
| 273 | x3::error_handler_result on_error(Iterator&, Iterator const&, Exception const&, Context const& context) |
| 274 | { |
| 275 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 276 | auto& schema = parserContext.m_schema; |
| 277 | if (parserContext.m_errorMsg.empty()) { |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame^] | 278 | leaf_ leaf = boost::get<leaf_>(parserContext.m_curPath.m_nodes.back().m_suffix); |
Václav Kubernát | 0b0272f | 2018-06-13 14:13:08 +0200 | [diff] [blame] | 279 | path_ location = pathWithoutLastNode(parserContext.m_curPath); |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame^] | 280 | 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] | 281 | return x3::error_handler_result::fail; |
| 282 | } |
| 283 | return x3::error_handler_result::rethrow; |
| 284 | } |
Václav Kubernát | ebca255 | 2018-06-08 19:06:02 +0200 | [diff] [blame] | 285 | }; |
| 286 | |
| 287 | struct leaf_data_base_class { |
| 288 | yang::LeafDataTypes m_type; |
| 289 | |
| 290 | leaf_data_base_class(yang::LeafDataTypes type) |
| 291 | : m_type(type) |
| 292 | { |
| 293 | } |
| 294 | |
| 295 | template <typename T, typename Iterator, typename Context> |
| 296 | void on_success(Iterator const&, Iterator const&, T&, Context const& context) |
| 297 | { |
| 298 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 299 | auto& schema = parserContext.m_schema; |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame^] | 300 | boost::optional<std::string> module; |
| 301 | if (parserContext.m_curPath.m_nodes.back().m_prefix) |
| 302 | 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] | 303 | |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame^] | 304 | leaf_ leaf = boost::get<leaf_>(parserContext.m_curPath.m_nodes.back().m_suffix); |
Václav Kubernát | ebca255 | 2018-06-08 19:06:02 +0200 | [diff] [blame] | 305 | path_ location = pathWithoutLastNode(parserContext.m_curPath); |
| 306 | |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame^] | 307 | if (schema.leafType(location, {module, leaf.m_name}) != m_type) { |
Václav Kubernát | ebca255 | 2018-06-08 19:06:02 +0200 | [diff] [blame] | 308 | _pass(context) = false; |
| 309 | } |
| 310 | } |
| 311 | }; |
| 312 | |
| 313 | struct leaf_data_enum_class : leaf_data_base_class { |
| 314 | leaf_data_enum_class() |
| 315 | : leaf_data_base_class(yang::LeafDataTypes::Enum) |
| 316 | { |
| 317 | } |
| 318 | |
| 319 | template <typename T, typename Iterator, typename Context> |
| 320 | void on_success(Iterator const& start, Iterator const& end, T& ast, Context const& context) |
| 321 | { |
| 322 | leaf_data_base_class::on_success(start, end, ast, context); |
| 323 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 324 | auto& schema = parserContext.m_schema; |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame^] | 325 | boost::optional<std::string> module; |
| 326 | if (parserContext.m_curPath.m_nodes.back().m_prefix) |
| 327 | 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] | 328 | |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame^] | 329 | leaf_ leaf = boost::get<leaf_>(parserContext.m_curPath.m_nodes.back().m_suffix); |
Václav Kubernát | ebca255 | 2018-06-08 19:06:02 +0200 | [diff] [blame] | 330 | path_ location = pathWithoutLastNode(parserContext.m_curPath); |
| 331 | |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame^] | 332 | 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] | 333 | _pass(context) = false; |
| 334 | } |
| 335 | } |
| 336 | }; |
| 337 | |
| 338 | struct leaf_data_decimal_class : leaf_data_base_class { |
| 339 | leaf_data_decimal_class() |
| 340 | : leaf_data_base_class(yang::LeafDataTypes::Decimal) |
| 341 | { |
| 342 | } |
| 343 | }; |
| 344 | |
| 345 | struct leaf_data_bool_class : leaf_data_base_class { |
| 346 | leaf_data_bool_class() |
| 347 | : leaf_data_base_class(yang::LeafDataTypes::Bool) |
| 348 | { |
| 349 | } |
| 350 | }; |
| 351 | |
| 352 | struct leaf_data_int_class : leaf_data_base_class { |
| 353 | leaf_data_int_class() |
| 354 | : leaf_data_base_class(yang::LeafDataTypes::Int) |
| 355 | { |
| 356 | } |
| 357 | }; |
| 358 | |
| 359 | struct leaf_data_uint_class : leaf_data_base_class { |
| 360 | leaf_data_uint_class() |
| 361 | : leaf_data_base_class(yang::LeafDataTypes::Uint) |
| 362 | { |
| 363 | } |
| 364 | }; |
| 365 | |
| 366 | struct leaf_data_string_class : leaf_data_base_class { |
| 367 | leaf_data_string_class() |
| 368 | : leaf_data_base_class(yang::LeafDataTypes::String) |
| 369 | { |
| 370 | } |
| 371 | }; |
| 372 | |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 373 | struct set_class { |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 374 | template <typename Iterator, typename Exception, typename Context> |
| 375 | x3::error_handler_result on_error(Iterator&, Iterator const&, Exception const& x, Context const& context) |
| 376 | { |
| 377 | auto& parserContext = x3::get<parser_context_tag>(context); |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame] | 378 | if (parserContext.m_errorMsg.empty()) |
| 379 | parserContext.m_errorMsg = "Expected " + x.which() + " here:"; |
| 380 | return x3::error_handler_result::rethrow; |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 381 | } |
| 382 | }; |
| 383 | |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 384 | struct command_class { |
| 385 | template <typename Iterator, typename Exception, typename Context> |
| 386 | x3::error_handler_result on_error(Iterator&, Iterator const&, Exception const& x, Context const& context) |
| 387 | { |
| 388 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 389 | 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] | 390 | if (parserContext.m_errorMsg.empty()) { |
| 391 | parserContext.m_errorMsg = "Unknown command."; |
| 392 | } |
| 393 | error_handler(x.where(), parserContext.m_errorMsg); |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 394 | return x3::error_handler_result::fail; |
| 395 | } |
| 396 | }; |