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 | 11afac7 | 2018-07-18 14:59:53 +0200 | [diff] [blame] | 207 | struct ls_class; |
| 208 | |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 209 | struct cd_class { |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 210 | template <typename Iterator, typename Exception, typename Context> |
| 211 | x3::error_handler_result on_error(Iterator&, Iterator const&, Exception const& x, Context const& context) |
| 212 | { |
| 213 | auto& parserContext = x3::get<parser_context_tag>(context); |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame] | 214 | if (parserContext.m_errorMsg.empty()) |
| 215 | parserContext.m_errorMsg = "Expected " + x.which() + " here:"; |
| 216 | return x3::error_handler_result::rethrow; |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 217 | } |
| 218 | }; |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 219 | |
| 220 | struct presenceContainerPathHandler { |
| 221 | template <typename T, typename Iterator, typename Context> |
| 222 | void on_success(Iterator const&, Iterator const&, T& ast, Context const& context) |
| 223 | { |
| 224 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 225 | const auto& schema = parserContext.m_schema; |
| 226 | try { |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 227 | boost::optional<std::string> module; |
| 228 | if (ast.m_path.m_nodes.back().m_prefix) |
| 229 | module = ast.m_path.m_nodes.back().m_prefix.value().m_name; |
| 230 | 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] | 231 | path_ location = pathWithoutLastNode(parserContext.m_curPath); |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 232 | |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 233 | if (!schema.isPresenceContainer(location, {module, cont.m_name})) { |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame] | 234 | parserContext.m_errorMsg = "This container is not a presence container."; |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 235 | _pass(context) = false; |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 236 | } |
| 237 | } catch (boost::bad_get&) { |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame] | 238 | parserContext.m_errorMsg = "This is not a container."; |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 239 | _pass(context) = false; |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 240 | } |
| 241 | } |
| 242 | |
| 243 | template <typename Iterator, typename Exception, typename Context> |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame] | 244 | 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] | 245 | { |
| 246 | auto& parserContext = x3::get<parser_context_tag>(context); |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame] | 247 | if (parserContext.m_errorMsg.empty()) |
| 248 | parserContext.m_errorMsg = "Couldn't parse create/delete command."; |
| 249 | return x3::error_handler_result::rethrow; |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 250 | } |
| 251 | }; |
| 252 | |
| 253 | struct create_class : public presenceContainerPathHandler { |
| 254 | }; |
| 255 | |
| 256 | struct delete_class : public presenceContainerPathHandler { |
| 257 | }; |
| 258 | |
Václav Kubernát | 0b0272f | 2018-06-13 14:13:08 +0200 | [diff] [blame] | 259 | struct leaf_path_class { |
| 260 | template <typename T, typename Iterator, typename Context> |
| 261 | void on_success(Iterator const&, Iterator const&, T& ast, Context const& context) |
| 262 | { |
| 263 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 264 | try { |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 265 | 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] | 266 | } catch (boost::bad_get&) { |
| 267 | parserContext.m_errorMsg = "This is not a path to leaf."; |
| 268 | _pass(context) = false; |
| 269 | } |
| 270 | } |
| 271 | }; |
| 272 | |
Václav Kubernát | ebca255 | 2018-06-08 19:06:02 +0200 | [diff] [blame] | 273 | struct leaf_data_class { |
Václav Kubernát | 0b0272f | 2018-06-13 14:13:08 +0200 | [diff] [blame] | 274 | template <typename Iterator, typename Exception, typename Context> |
| 275 | x3::error_handler_result on_error(Iterator&, Iterator const&, Exception const&, Context const& context) |
| 276 | { |
| 277 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 278 | auto& schema = parserContext.m_schema; |
| 279 | if (parserContext.m_errorMsg.empty()) { |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 280 | 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] | 281 | path_ location = pathWithoutLastNode(parserContext.m_curPath); |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 282 | 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] | 283 | return x3::error_handler_result::fail; |
| 284 | } |
| 285 | return x3::error_handler_result::rethrow; |
| 286 | } |
Václav Kubernát | ebca255 | 2018-06-08 19:06:02 +0200 | [diff] [blame] | 287 | }; |
| 288 | |
| 289 | struct leaf_data_base_class { |
| 290 | yang::LeafDataTypes m_type; |
| 291 | |
| 292 | leaf_data_base_class(yang::LeafDataTypes type) |
| 293 | : m_type(type) |
| 294 | { |
| 295 | } |
| 296 | |
| 297 | template <typename T, typename Iterator, typename Context> |
| 298 | void on_success(Iterator const&, Iterator const&, T&, Context const& context) |
| 299 | { |
| 300 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 301 | auto& schema = parserContext.m_schema; |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 302 | boost::optional<std::string> module; |
| 303 | if (parserContext.m_curPath.m_nodes.back().m_prefix) |
| 304 | 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] | 305 | |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 306 | 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] | 307 | path_ location = pathWithoutLastNode(parserContext.m_curPath); |
| 308 | |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 309 | if (schema.leafType(location, {module, leaf.m_name}) != m_type) { |
Václav Kubernát | ebca255 | 2018-06-08 19:06:02 +0200 | [diff] [blame] | 310 | _pass(context) = false; |
| 311 | } |
| 312 | } |
| 313 | }; |
| 314 | |
| 315 | struct leaf_data_enum_class : leaf_data_base_class { |
| 316 | leaf_data_enum_class() |
| 317 | : leaf_data_base_class(yang::LeafDataTypes::Enum) |
| 318 | { |
| 319 | } |
| 320 | |
| 321 | template <typename T, typename Iterator, typename Context> |
| 322 | void on_success(Iterator const& start, Iterator const& end, T& ast, Context const& context) |
| 323 | { |
| 324 | leaf_data_base_class::on_success(start, end, ast, context); |
| 325 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 326 | auto& schema = parserContext.m_schema; |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 327 | boost::optional<std::string> module; |
| 328 | if (parserContext.m_curPath.m_nodes.back().m_prefix) |
| 329 | 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] | 330 | |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 331 | 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] | 332 | path_ location = pathWithoutLastNode(parserContext.m_curPath); |
| 333 | |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 334 | 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] | 335 | _pass(context) = false; |
| 336 | } |
| 337 | } |
| 338 | }; |
| 339 | |
| 340 | struct leaf_data_decimal_class : leaf_data_base_class { |
| 341 | leaf_data_decimal_class() |
| 342 | : leaf_data_base_class(yang::LeafDataTypes::Decimal) |
| 343 | { |
| 344 | } |
| 345 | }; |
| 346 | |
| 347 | struct leaf_data_bool_class : leaf_data_base_class { |
| 348 | leaf_data_bool_class() |
| 349 | : leaf_data_base_class(yang::LeafDataTypes::Bool) |
| 350 | { |
| 351 | } |
| 352 | }; |
| 353 | |
| 354 | struct leaf_data_int_class : leaf_data_base_class { |
| 355 | leaf_data_int_class() |
| 356 | : leaf_data_base_class(yang::LeafDataTypes::Int) |
| 357 | { |
| 358 | } |
| 359 | }; |
| 360 | |
| 361 | struct leaf_data_uint_class : leaf_data_base_class { |
| 362 | leaf_data_uint_class() |
| 363 | : leaf_data_base_class(yang::LeafDataTypes::Uint) |
| 364 | { |
| 365 | } |
| 366 | }; |
| 367 | |
| 368 | struct leaf_data_string_class : leaf_data_base_class { |
| 369 | leaf_data_string_class() |
| 370 | : leaf_data_base_class(yang::LeafDataTypes::String) |
| 371 | { |
| 372 | } |
| 373 | }; |
| 374 | |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 375 | struct set_class { |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 376 | template <typename Iterator, typename Exception, typename Context> |
| 377 | x3::error_handler_result on_error(Iterator&, Iterator const&, Exception const& x, Context const& context) |
| 378 | { |
| 379 | auto& parserContext = x3::get<parser_context_tag>(context); |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame] | 380 | if (parserContext.m_errorMsg.empty()) |
| 381 | parserContext.m_errorMsg = "Expected " + x.which() + " here:"; |
| 382 | return x3::error_handler_result::rethrow; |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 383 | } |
| 384 | }; |
| 385 | |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 386 | struct command_class { |
| 387 | template <typename Iterator, typename Exception, typename Context> |
| 388 | x3::error_handler_result on_error(Iterator&, Iterator const&, Exception const& x, Context const& context) |
| 389 | { |
| 390 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 391 | 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] | 392 | if (parserContext.m_errorMsg.empty()) { |
| 393 | parserContext.m_errorMsg = "Unknown command."; |
| 394 | } |
| 395 | error_handler(x.where(), parserContext.m_errorMsg); |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 396 | return x3::error_handler_result::fail; |
| 397 | } |
| 398 | }; |