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(); |
Václav Kubernát | b962105 | 2019-03-18 18:27:49 +0100 | [diff] [blame] | 240 | parserContext.m_topLevelModulePresent = false; |
Václav Kubernát | 37171a1 | 2018-08-31 17:01:48 +0200 | [diff] [blame] | 241 | } |
| 242 | }; |
| 243 | |
Václav Kubernát | 5c75b25 | 2018-10-10 18:33:47 +0200 | [diff] [blame] | 244 | struct dataNodesListEnd_class; |
| 245 | |
| 246 | struct dataPathListEnd_class; |
| 247 | |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 248 | struct dataPath_class { |
| 249 | template <typename Iterator, typename Exception, typename Context> |
| 250 | x3::error_handler_result on_error(Iterator&, Iterator const&, Exception const&, Context const& context) |
| 251 | { |
| 252 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 253 | if (parserContext.m_errorMsg.empty()) { |
| 254 | parserContext.m_errorMsg = "Expected path."; |
| 255 | return x3::error_handler_result::fail; |
| 256 | } else { |
| 257 | return x3::error_handler_result::rethrow; |
| 258 | } |
| 259 | } |
| 260 | }; |
| 261 | |
| 262 | struct schemaPath_class { |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 263 | template <typename Iterator, typename Exception, typename Context> |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame] | 264 | 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] | 265 | { |
| 266 | auto& parserContext = x3::get<parser_context_tag>(context); |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame] | 267 | if (parserContext.m_errorMsg.empty()) { |
| 268 | parserContext.m_errorMsg = "Expected path."; |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 269 | return x3::error_handler_result::fail; |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame] | 270 | } else { |
| 271 | return x3::error_handler_result::rethrow; |
| 272 | } |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 273 | } |
| 274 | }; |
| 275 | |
Václav Kubernát | 6d79143 | 2018-10-25 16:00:35 +0200 | [diff] [blame] | 276 | struct discard_class; |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 277 | |
Václav Kubernát | 11afac7 | 2018-07-18 14:59:53 +0200 | [diff] [blame] | 278 | struct ls_class; |
| 279 | |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 280 | struct cd_class { |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 281 | template <typename Iterator, typename Exception, typename Context> |
| 282 | x3::error_handler_result on_error(Iterator&, Iterator const&, Exception const& x, Context const& context) |
| 283 | { |
| 284 | auto& parserContext = x3::get<parser_context_tag>(context); |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame] | 285 | if (parserContext.m_errorMsg.empty()) |
| 286 | parserContext.m_errorMsg = "Expected " + x.which() + " here:"; |
| 287 | return x3::error_handler_result::rethrow; |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 288 | } |
| 289 | }; |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 290 | |
Václav Kubernát | 6797df0 | 2019-03-18 20:21:50 +0100 | [diff] [blame] | 291 | struct presenceContainerPath_class { |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 292 | template <typename T, typename Iterator, typename Context> |
| 293 | void on_success(Iterator const&, Iterator const&, T& ast, Context const& context) |
| 294 | { |
| 295 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 296 | const auto& schema = parserContext.m_schema; |
| 297 | try { |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 298 | boost::optional<std::string> module; |
Václav Kubernát | 6797df0 | 2019-03-18 20:21:50 +0100 | [diff] [blame] | 299 | if (ast.m_nodes.back().m_prefix) |
| 300 | module = ast.m_nodes.back().m_prefix.value().m_name; |
| 301 | container_ cont = boost::get<container_>(ast.m_nodes.back().m_suffix); |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 302 | schemaPath_ location = pathWithoutLastNode(parserContext.m_curPath); |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 303 | |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 304 | if (!schema.isPresenceContainer(location, {module, cont.m_name})) { |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame] | 305 | parserContext.m_errorMsg = "This container is not a presence container."; |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 306 | _pass(context) = false; |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 307 | } |
| 308 | } catch (boost::bad_get&) { |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame] | 309 | parserContext.m_errorMsg = "This is not a container."; |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 310 | _pass(context) = false; |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 311 | } |
| 312 | } |
Václav Kubernát | 6797df0 | 2019-03-18 20:21:50 +0100 | [diff] [blame] | 313 | }; |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 314 | |
Václav Kubernát | f5f64f0 | 2019-03-19 17:15:47 +0100 | [diff] [blame] | 315 | struct listInstancePath_class { |
| 316 | template <typename T, typename Iterator, typename Context> |
| 317 | void on_success(Iterator const&, Iterator const&, T& ast, Context const& context) |
| 318 | { |
| 319 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 320 | if (ast.m_nodes.back().m_suffix.type() != typeid(listElement_)) { |
| 321 | parserContext.m_errorMsg = "This is not a list instance."; |
| 322 | _pass(context) = false; |
| 323 | } |
| 324 | } |
| 325 | }; |
| 326 | |
Václav Kubernát | 6797df0 | 2019-03-18 20:21:50 +0100 | [diff] [blame] | 327 | struct create_class { |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 328 | template <typename Iterator, typename Exception, typename Context> |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame] | 329 | 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] | 330 | { |
| 331 | auto& parserContext = x3::get<parser_context_tag>(context); |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame] | 332 | if (parserContext.m_errorMsg.empty()) |
| 333 | parserContext.m_errorMsg = "Couldn't parse create/delete command."; |
| 334 | return x3::error_handler_result::rethrow; |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 335 | } |
| 336 | }; |
| 337 | |
Václav Kubernát | 6797df0 | 2019-03-18 20:21:50 +0100 | [diff] [blame] | 338 | struct delete_class { |
| 339 | template <typename Iterator, typename Exception, typename Context> |
| 340 | x3::error_handler_result on_error(Iterator&, Iterator const&, Exception const&, Context const& context) |
| 341 | { |
| 342 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 343 | if (parserContext.m_errorMsg.empty()) |
| 344 | parserContext.m_errorMsg = "Couldn't parse create/delete command."; |
| 345 | return x3::error_handler_result::rethrow; |
| 346 | } |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 347 | }; |
| 348 | |
Václav Kubernát | 0b0272f | 2018-06-13 14:13:08 +0200 | [diff] [blame] | 349 | struct leaf_path_class { |
| 350 | template <typename T, typename Iterator, typename Context> |
| 351 | void on_success(Iterator const&, Iterator const&, T& ast, Context const& context) |
| 352 | { |
| 353 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 354 | try { |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 355 | 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] | 356 | } catch (boost::bad_get&) { |
| 357 | parserContext.m_errorMsg = "This is not a path to leaf."; |
| 358 | _pass(context) = false; |
| 359 | } |
| 360 | } |
| 361 | }; |
| 362 | |
Václav Kubernát | eeb3884 | 2019-03-20 19:46:05 +0100 | [diff] [blame] | 363 | // This handler only checks if the module exists |
| 364 | // It doesn't set any ParserContext flags (except the error message) |
| 365 | struct data_module_prefix_class { |
| 366 | template <typename T, typename Iterator, typename Context> |
| 367 | void on_success(Iterator const&, Iterator const&, T& ast, Context const& context) |
| 368 | { |
| 369 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 370 | const auto& schema = parserContext.m_schema; |
| 371 | |
| 372 | if (!schema.isModule(parserContext.m_curPath, ast.m_name)) { |
| 373 | parserContext.m_errorMsg = "Invalid module name."; |
| 374 | _pass(context) = false; |
| 375 | } |
| 376 | } |
| 377 | }; |
| 378 | |
Václav Kubernát | ebca255 | 2018-06-08 19:06:02 +0200 | [diff] [blame] | 379 | struct leaf_data_class { |
Václav Kubernát | 0b0272f | 2018-06-13 14:13:08 +0200 | [diff] [blame] | 380 | template <typename Iterator, typename Exception, typename Context> |
| 381 | x3::error_handler_result on_error(Iterator&, Iterator const&, Exception const&, Context const& context) |
| 382 | { |
| 383 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 384 | auto& schema = parserContext.m_schema; |
| 385 | if (parserContext.m_errorMsg.empty()) { |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 386 | 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] | 387 | schemaPath_ location = pathWithoutLastNode(parserContext.m_curPath); |
Václav Kubernát | eeb3884 | 2019-03-20 19:46:05 +0100 | [diff] [blame] | 388 | boost::optional<std::string> module; |
| 389 | if (parserContext.m_curPath.m_nodes.back().m_prefix) |
| 390 | module = parserContext.m_curPath.m_nodes.back().m_prefix.value().m_name; |
| 391 | parserContext.m_errorMsg = "Expected " + leafDataTypeToString(schema.leafType(location, {module, leaf.m_name})) + " here:"; |
Václav Kubernát | 0b0272f | 2018-06-13 14:13:08 +0200 | [diff] [blame] | 392 | return x3::error_handler_result::fail; |
| 393 | } |
| 394 | return x3::error_handler_result::rethrow; |
| 395 | } |
Václav Kubernát | ebca255 | 2018-06-08 19:06:02 +0200 | [diff] [blame] | 396 | }; |
| 397 | |
| 398 | struct leaf_data_base_class { |
| 399 | yang::LeafDataTypes m_type; |
| 400 | |
| 401 | leaf_data_base_class(yang::LeafDataTypes type) |
| 402 | : m_type(type) |
| 403 | { |
| 404 | } |
| 405 | |
| 406 | template <typename T, typename Iterator, typename Context> |
| 407 | void on_success(Iterator const&, Iterator const&, T&, Context const& context) |
| 408 | { |
| 409 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 410 | auto& schema = parserContext.m_schema; |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 411 | boost::optional<std::string> module; |
| 412 | if (parserContext.m_curPath.m_nodes.back().m_prefix) |
| 413 | 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] | 414 | |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 415 | 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] | 416 | schemaPath_ location = pathWithoutLastNode(parserContext.m_curPath); |
Václav Kubernát | ebca255 | 2018-06-08 19:06:02 +0200 | [diff] [blame] | 417 | |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 418 | if (schema.leafType(location, {module, leaf.m_name}) != m_type) { |
Václav Kubernát | ebca255 | 2018-06-08 19:06:02 +0200 | [diff] [blame] | 419 | _pass(context) = false; |
| 420 | } |
| 421 | } |
| 422 | }; |
| 423 | |
| 424 | struct leaf_data_enum_class : leaf_data_base_class { |
| 425 | leaf_data_enum_class() |
| 426 | : leaf_data_base_class(yang::LeafDataTypes::Enum) |
| 427 | { |
| 428 | } |
| 429 | |
| 430 | template <typename T, typename Iterator, typename Context> |
| 431 | void on_success(Iterator const& start, Iterator const& end, T& ast, Context const& context) |
| 432 | { |
| 433 | leaf_data_base_class::on_success(start, end, ast, context); |
Václav Kubernát | eeb3884 | 2019-03-20 19:46:05 +0100 | [diff] [blame] | 434 | // Base class on_success cannot return for us, so we check if it failed the parser. |
| 435 | if (_pass(context) == false) |
| 436 | return; |
Václav Kubernát | ebca255 | 2018-06-08 19:06:02 +0200 | [diff] [blame] | 437 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 438 | auto& schema = parserContext.m_schema; |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 439 | boost::optional<std::string> module; |
| 440 | if (parserContext.m_curPath.m_nodes.back().m_prefix) |
| 441 | 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] | 442 | |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 443 | 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] | 444 | schemaPath_ location = pathWithoutLastNode(parserContext.m_curPath); |
Václav Kubernát | ebca255 | 2018-06-08 19:06:02 +0200 | [diff] [blame] | 445 | |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 446 | 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] | 447 | _pass(context) = false; |
| 448 | } |
| 449 | } |
| 450 | }; |
| 451 | |
| 452 | struct leaf_data_decimal_class : leaf_data_base_class { |
| 453 | leaf_data_decimal_class() |
| 454 | : leaf_data_base_class(yang::LeafDataTypes::Decimal) |
| 455 | { |
| 456 | } |
| 457 | }; |
| 458 | |
| 459 | struct leaf_data_bool_class : leaf_data_base_class { |
| 460 | leaf_data_bool_class() |
| 461 | : leaf_data_base_class(yang::LeafDataTypes::Bool) |
| 462 | { |
| 463 | } |
| 464 | }; |
| 465 | |
| 466 | struct leaf_data_int_class : leaf_data_base_class { |
| 467 | leaf_data_int_class() |
| 468 | : leaf_data_base_class(yang::LeafDataTypes::Int) |
| 469 | { |
| 470 | } |
| 471 | }; |
| 472 | |
| 473 | struct leaf_data_uint_class : leaf_data_base_class { |
| 474 | leaf_data_uint_class() |
| 475 | : leaf_data_base_class(yang::LeafDataTypes::Uint) |
| 476 | { |
| 477 | } |
| 478 | }; |
| 479 | |
| 480 | struct leaf_data_string_class : leaf_data_base_class { |
| 481 | leaf_data_string_class() |
| 482 | : leaf_data_base_class(yang::LeafDataTypes::String) |
| 483 | { |
| 484 | } |
| 485 | }; |
| 486 | |
Václav Kubernát | ab53899 | 2019-03-06 15:30:50 +0100 | [diff] [blame] | 487 | struct leaf_data_binary_data_class; |
| 488 | |
| 489 | struct leaf_data_binary_class : leaf_data_base_class { |
| 490 | leaf_data_binary_class() |
| 491 | : leaf_data_base_class(yang::LeafDataTypes::Binary) |
| 492 | { |
| 493 | } |
| 494 | }; |
| 495 | |
Václav Kubernát | eeb3884 | 2019-03-20 19:46:05 +0100 | [diff] [blame] | 496 | struct leaf_data_identityRef_data_class; |
| 497 | |
| 498 | struct leaf_data_identityRef_class : leaf_data_base_class { |
| 499 | leaf_data_identityRef_class() |
| 500 | : leaf_data_base_class(yang::LeafDataTypes::IdentityRef) |
| 501 | { |
| 502 | } |
| 503 | |
| 504 | template <typename T, typename Iterator, typename Context> |
| 505 | void on_success(Iterator const& start, Iterator const& end, T& ast, Context const& context) |
| 506 | { |
| 507 | // FIXME: can I reuse leaf_data_enum_class somehow..? |
| 508 | leaf_data_base_class::on_success(start, end, ast, context); |
| 509 | // Base class on_success cannot return for us, so we check if it failed the parser. |
| 510 | if (_pass(context) == false) |
| 511 | return; |
| 512 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 513 | auto& schema = parserContext.m_schema; |
| 514 | boost::optional<std::string> module; |
| 515 | if (parserContext.m_curPath.m_nodes.back().m_prefix) |
| 516 | module = parserContext.m_curPath.m_nodes.back().m_prefix.value().m_name; |
| 517 | |
| 518 | leaf_ leaf = boost::get<leaf_>(parserContext.m_curPath.m_nodes.back().m_suffix); |
| 519 | schemaPath_ location = pathWithoutLastNode(parserContext.m_curPath); |
| 520 | |
| 521 | ModuleValuePair pair; |
| 522 | if (ast.m_prefix) { |
| 523 | pair.first = ast.m_prefix.get().m_name; |
| 524 | } |
| 525 | pair.second = ast.m_value; |
| 526 | |
| 527 | if (!schema.leafIdentityIsValid(location, {module, leaf.m_name}, pair)) { |
| 528 | _pass(context) = false; |
| 529 | } |
| 530 | } |
| 531 | }; |
| 532 | |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 533 | struct set_class { |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 534 | template <typename Iterator, typename Exception, typename Context> |
| 535 | x3::error_handler_result on_error(Iterator&, Iterator const&, Exception const& x, Context const& context) |
| 536 | { |
| 537 | auto& parserContext = x3::get<parser_context_tag>(context); |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame] | 538 | if (parserContext.m_errorMsg.empty()) |
| 539 | parserContext.m_errorMsg = "Expected " + x.which() + " here:"; |
| 540 | return x3::error_handler_result::rethrow; |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 541 | } |
| 542 | }; |
| 543 | |
Václav Kubernát | 812ee28 | 2018-08-30 17:10:03 +0200 | [diff] [blame] | 544 | struct commit_class; |
| 545 | |
Václav Kubernát | 054cc99 | 2019-02-21 14:23:52 +0100 | [diff] [blame] | 546 | struct help_class; |
| 547 | |
Václav Kubernát | b6ff0b6 | 2018-08-30 16:14:53 +0200 | [diff] [blame] | 548 | struct get_class; |
| 549 | |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 550 | struct command_class { |
| 551 | template <typename Iterator, typename Exception, typename Context> |
| 552 | x3::error_handler_result on_error(Iterator&, Iterator const&, Exception const& x, Context const& context) |
| 553 | { |
| 554 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 555 | 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] | 556 | if (parserContext.m_errorMsg.empty()) { |
| 557 | parserContext.m_errorMsg = "Unknown command."; |
| 558 | } |
| 559 | error_handler(x.where(), parserContext.m_errorMsg); |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 560 | return x3::error_handler_result::fail; |
| 561 | } |
| 562 | }; |
Václav Kubernát | 5c75b25 | 2018-10-10 18:33:47 +0200 | [diff] [blame] | 563 | |
Václav Kubernát | 4108e0d | 2018-10-29 13:32:22 +0100 | [diff] [blame] | 564 | struct initializePath_class { |
Václav Kubernát | 5c75b25 | 2018-10-10 18:33:47 +0200 | [diff] [blame] | 565 | template <typename T, typename Iterator, typename Context> |
| 566 | void on_success(Iterator const&, Iterator const&, T&, Context const& context) |
| 567 | { |
| 568 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 569 | parserContext.m_curPath = parserContext.m_curPathOrig; |
| 570 | parserContext.m_tmpListKeys.clear(); |
| 571 | parserContext.m_tmpListName.clear(); |
Václav Kubernát | 4108e0d | 2018-10-29 13:32:22 +0100 | [diff] [blame] | 572 | parserContext.m_suggestions.clear(); |
Václav Kubernát | 5c75b25 | 2018-10-10 18:33:47 +0200 | [diff] [blame] | 573 | if (!parserContext.m_curPath.m_nodes.empty() && parserContext.m_curPath.m_nodes.at(0).m_prefix) |
| 574 | parserContext.m_topLevelModulePresent = true; |
| 575 | else |
| 576 | parserContext.m_topLevelModulePresent = false; |
| 577 | } |
| 578 | }; |
Václav Kubernát | d6fd249 | 2018-11-19 15:11:16 +0100 | [diff] [blame] | 579 | |
| 580 | struct trailingSlash_class; |
Václav Kubernát | 4108e0d | 2018-10-29 13:32:22 +0100 | [diff] [blame] | 581 | |
| 582 | struct createPathSuggestions_class { |
| 583 | template <typename T, typename Iterator, typename Context> |
| 584 | void on_success(Iterator const& begin, Iterator const&, T&, Context const& context) |
| 585 | { |
| 586 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 587 | const auto& schema = parserContext.m_schema; |
| 588 | |
| 589 | parserContext.m_completionIterator = begin; |
| 590 | parserContext.m_suggestions = schema.childNodes(parserContext.m_curPath, Recursion::NonRecursive); |
| 591 | } |
| 592 | }; |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 593 | |
| 594 | std::set<std::string> generateMissingKeyCompletionSet(std::set<std::string> keysNeeded, std::set<std::string> currentSet); |
| 595 | |
| 596 | struct createKeySuggestions_class { |
| 597 | template <typename T, typename Iterator, typename Context> |
| 598 | void on_success(Iterator const& begin, Iterator const&, T&, Context const& context) |
| 599 | { |
| 600 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 601 | const auto& schema = parserContext.m_schema; |
| 602 | |
| 603 | parserContext.m_completionIterator = begin; |
| 604 | |
| 605 | const auto& keysNeeded = schema.listKeys(parserContext.m_curPath, {parserContext.m_curModule, parserContext.m_tmpListName}); |
| 606 | parserContext.m_suggestions = generateMissingKeyCompletionSet(keysNeeded, parserContext.m_tmpListKeys); |
| 607 | } |
| 608 | }; |
| 609 | |
| 610 | struct suggestKeysEnd_class { |
| 611 | template <typename T, typename Iterator, typename Context> |
| 612 | void on_success(Iterator const& begin, Iterator const&, T&, Context const& context) |
| 613 | { |
| 614 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 615 | const auto& schema = parserContext.m_schema; |
| 616 | |
| 617 | parserContext.m_completionIterator = begin; |
| 618 | const auto& keysNeeded = schema.listKeys(parserContext.m_curPath, {parserContext.m_curModule, parserContext.m_tmpListName}); |
| 619 | if (generateMissingKeyCompletionSet(keysNeeded, parserContext.m_tmpListKeys).empty()) { |
| 620 | parserContext.m_suggestions = {"]/"}; |
| 621 | } else { |
| 622 | parserContext.m_suggestions = {"]"}; |
| 623 | } |
| 624 | } |
| 625 | }; |
Václav Kubernát | 5727242 | 2019-02-08 12:48:24 +0100 | [diff] [blame] | 626 | |
| 627 | struct commandNamesVisitor { |
| 628 | template <typename T> |
| 629 | auto operator()(boost::type<T>) |
| 630 | { |
| 631 | return T::name; |
| 632 | } |
| 633 | }; |
| 634 | |
| 635 | struct createCommandSuggestions_class { |
| 636 | template <typename T, typename Iterator, typename Context> |
| 637 | void on_success(Iterator const& begin, Iterator const&, T&, Context const& context) |
| 638 | { |
| 639 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 640 | parserContext.m_completionIterator = begin; |
| 641 | |
| 642 | parserContext.m_suggestions.clear(); |
| 643 | boost::mpl::for_each<CommandTypes, boost::type<boost::mpl::_>>([&parserContext](auto cmd) { |
| 644 | parserContext.m_suggestions.emplace(commandNamesVisitor()(cmd)); |
| 645 | }); |
| 646 | } |
| 647 | }; |
Václav Kubernát | ac035d6 | 2019-02-18 10:59:08 +0100 | [diff] [blame] | 648 | |
| 649 | struct completing_class { |
| 650 | template <typename T, typename Iterator, typename Context> |
| 651 | void on_success(Iterator const&, Iterator const&, T&, Context const& context) |
| 652 | { |
| 653 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 654 | |
| 655 | if (!parserContext.m_completing) |
| 656 | _pass(context) = false; |
| 657 | } |
| 658 | }; |
Václav Kubernát | 989b5de | 2019-02-20 16:28:35 +0100 | [diff] [blame] | 659 | |
| 660 | struct createEnumSuggestions_class { |
| 661 | template <typename T, typename Iterator, typename Context> |
| 662 | void on_success(Iterator const& begin, Iterator const&, T&, Context const& context) |
| 663 | { |
| 664 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 665 | parserContext.m_completionIterator = begin; |
| 666 | const Schema& schema = parserContext.m_schema; |
| 667 | |
| 668 | boost::optional<std::string> module; |
| 669 | if (parserContext.m_curPath.m_nodes.back().m_prefix) |
| 670 | module = parserContext.m_curPath.m_nodes.back().m_prefix.value().m_name; |
| 671 | |
| 672 | leaf_ leaf = boost::get<leaf_>(parserContext.m_curPath.m_nodes.back().m_suffix); |
| 673 | schemaPath_ location = pathWithoutLastNode(parserContext.m_curPath); |
| 674 | |
Václav Kubernát | eeb3884 | 2019-03-20 19:46:05 +0100 | [diff] [blame] | 675 | // Only generate completions if the type is enum so that we don't |
| 676 | // overwrite some other completions. |
| 677 | if (schema.leafType(location, {module, leaf.m_name}) == yang::LeafDataTypes::Enum) |
| 678 | parserContext.m_suggestions = schema.enumValues(location, {module, leaf.m_name}); |
| 679 | } |
| 680 | }; |
| 681 | |
| 682 | // FIXME: can I reuse createEnumSuggestions? |
| 683 | struct createIdentitySuggestions_class { |
| 684 | template <typename T, typename Iterator, typename Context> |
| 685 | void on_success(Iterator const& begin, Iterator const&, T&, Context const& context) |
| 686 | { |
| 687 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 688 | parserContext.m_completionIterator = begin; |
| 689 | const Schema& schema = parserContext.m_schema; |
| 690 | |
| 691 | boost::optional<std::string> module; |
| 692 | if (parserContext.m_curPath.m_nodes.back().m_prefix) |
| 693 | module = parserContext.m_curPath.m_nodes.back().m_prefix.value().m_name; |
| 694 | |
| 695 | leaf_ leaf = boost::get<leaf_>(parserContext.m_curPath.m_nodes.back().m_suffix); |
| 696 | schemaPath_ location = pathWithoutLastNode(parserContext.m_curPath); |
| 697 | |
| 698 | // Only generate completions if the type is identityref so that we |
| 699 | // don't overwrite some other completions. |
| 700 | if (schema.leafType(location, {module, leaf.m_name}) == yang::LeafDataTypes::IdentityRef) |
| 701 | parserContext.m_suggestions = schema.validIdentities(location, {module, leaf.m_name}, Prefixes::WhenNeeded); |
Václav Kubernát | 989b5de | 2019-02-20 16:28:35 +0100 | [diff] [blame] | 702 | } |
| 703 | }; |