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