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 | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 28 | |
| 29 | if (parserContext.m_tmpListKeys.find(ast.first) != parserContext.m_tmpListKeys.end()) { |
| 30 | _pass(context) = false; |
| 31 | parserContext.m_errorMsg = "Key \"" + ast.first + "\" was entered more than once."; |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame] | 32 | } else { |
Václav Kubernát | 43908fb | 2020-01-02 19:05:51 +0100 | [diff] [blame] | 33 | parserContext.m_tmpListKeys.insert({ast.first, ast.second}); |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 34 | } |
| 35 | } |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame] | 36 | |
| 37 | template <typename Iterator, typename Exception, typename Context> |
| 38 | x3::error_handler_result on_error(Iterator&, Iterator const&, Exception const&, Context const& context) |
| 39 | { |
| 40 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 41 | parserContext.m_errorMsg = "Error parsing key values here:"; |
| 42 | return x3::error_handler_result::rethrow; |
| 43 | } |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 44 | }; |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame] | 45 | |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 46 | struct node_identifier_class { |
| 47 | template <typename T, typename Iterator, typename Context> |
| 48 | void on_success(Iterator const&, Iterator const&, T&, Context const& context) |
| 49 | { |
| 50 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 51 | |
| 52 | if (!parserContext.m_topLevelModulePresent) { |
| 53 | if (parserContext.m_errorMsg.empty()) |
| 54 | parserContext.m_errorMsg = "You have to specify a top level module."; |
| 55 | _pass(context) = false; |
| 56 | } |
| 57 | } |
| 58 | }; |
| 59 | |
Václav Kubernát | 7707cae | 2020-01-16 12:04:53 +0100 | [diff] [blame] | 60 | struct key_identifier_class { |
| 61 | template <typename T, typename Iterator, typename Context> |
| 62 | void on_success(Iterator const&, Iterator const&, T& ast, Context const& context) |
| 63 | { |
| 64 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 65 | const Schema& schema = parserContext.m_schema; |
| 66 | schemaPath_ location = parserContext.currentSchemaPath(); |
| 67 | ModuleNodePair list{parserContext.m_curModule, parserContext.m_tmpListName}; |
| 68 | |
| 69 | if (schema.listHasKey(location, list, ast)) { |
| 70 | schemaNode_ listNode; |
Václav Kubernát | 43908fb | 2020-01-02 19:05:51 +0100 | [diff] [blame] | 71 | listNode.m_prefix = parserContext.m_curModule.flat_map([] (auto mod) { return boost::optional<module_>{{mod}}; });; |
Václav Kubernát | 7707cae | 2020-01-16 12:04:53 +0100 | [diff] [blame] | 72 | listNode.m_suffix = list_{parserContext.m_tmpListName}; |
| 73 | location.m_nodes.push_back(listNode); |
| 74 | parserContext.m_tmpListKeyLeafPath.m_location = location; |
Václav Kubernát | 43908fb | 2020-01-02 19:05:51 +0100 | [diff] [blame] | 75 | parserContext.m_tmpListKeyLeafPath.m_node = { parserContext.m_curModule, ast }; |
Václav Kubernát | 7707cae | 2020-01-16 12:04:53 +0100 | [diff] [blame] | 76 | } else { |
| 77 | parserContext.m_errorMsg = parserContext.m_tmpListName + " is not indexed by \"" + ast + "\"."; |
| 78 | _pass(context) = false; |
| 79 | } |
| 80 | } |
| 81 | }; |
Václav Kubernát | 89728d8 | 2018-09-13 16:28:28 +0200 | [diff] [blame] | 82 | |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 83 | struct module_identifier_class; |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 84 | |
| 85 | struct listPrefix_class { |
| 86 | template <typename T, typename Iterator, typename Context> |
| 87 | void on_success(Iterator const&, Iterator const&, T& ast, Context const& context) |
| 88 | { |
| 89 | auto& parserContext = x3::get<parser_context_tag>(context); |
Václav Kubernát | 48fc383 | 2018-05-28 14:21:22 +0200 | [diff] [blame] | 90 | const Schema& schema = parserContext.m_schema; |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 91 | |
Václav Kubernát | 72749c6 | 2020-01-03 16:47:34 +0100 | [diff] [blame] | 92 | if (schema.isList(parserContext.currentSchemaPath(), {parserContext.m_curModule, ast})) { |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 93 | parserContext.m_tmpListName = ast; |
| 94 | } else { |
| 95 | _pass(context) = false; |
| 96 | } |
| 97 | } |
| 98 | }; |
| 99 | |
| 100 | struct listSuffix_class { |
| 101 | template <typename T, typename Iterator, typename Context> |
| 102 | void on_success(Iterator const&, Iterator const&, T& ast, Context const& context) |
| 103 | { |
| 104 | auto& parserContext = x3::get<parser_context_tag>(context); |
Václav Kubernát | 48fc383 | 2018-05-28 14:21:22 +0200 | [diff] [blame] | 105 | const Schema& schema = parserContext.m_schema; |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 106 | |
Václav Kubernát | 72749c6 | 2020-01-03 16:47:34 +0100 | [diff] [blame] | 107 | const auto& keysNeeded = schema.listKeys(parserContext.currentSchemaPath(), {parserContext.m_curModule, parserContext.m_tmpListName}); |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 108 | std::set<std::string> keysSupplied; |
| 109 | for (const auto& it : ast) |
| 110 | keysSupplied.insert(it.first); |
| 111 | |
| 112 | if (keysNeeded != keysSupplied) { |
| 113 | parserContext.m_errorMsg = "Not enough keys for " + parserContext.m_tmpListName + ". " + |
| 114 | "These keys were not supplied:"; |
| 115 | std::set<std::string> missingKeys; |
| 116 | std::set_difference(keysNeeded.begin(), keysNeeded.end(), |
| 117 | keysSupplied.begin(), keysSupplied.end(), |
| 118 | std::inserter(missingKeys, missingKeys.end())); |
| 119 | |
| 120 | for (const auto& it : missingKeys) |
| 121 | parserContext.m_errorMsg += " " + it; |
| 122 | parserContext.m_errorMsg += "."; |
| 123 | |
| 124 | _pass(context) = false; |
| 125 | } |
| 126 | } |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame] | 127 | |
| 128 | template <typename Iterator, typename Exception, typename Context> |
| 129 | x3::error_handler_result on_error(Iterator&, Iterator const&, Exception const&, Context const& context) |
| 130 | { |
| 131 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 132 | if (parserContext.m_errorMsg.empty()) |
| 133 | parserContext.m_errorMsg = "Expecting ']' here:"; |
| 134 | return x3::error_handler_result::rethrow; |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame] | 135 | } |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 136 | }; |
| 137 | struct listElement_class { |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 138 | template <typename Iterator, typename Exception, typename Context> |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame] | 139 | 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] | 140 | { |
| 141 | auto& parserContext = x3::get<parser_context_tag>(context); |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame] | 142 | if (parserContext.m_errorMsg.empty()) { |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 143 | return x3::error_handler_result::fail; |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame] | 144 | } else { |
| 145 | return x3::error_handler_result::rethrow; |
| 146 | } |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 147 | } |
| 148 | }; |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 149 | struct list_class { |
| 150 | template <typename T, typename Iterator, typename Context> |
| 151 | void on_success(Iterator const&, Iterator const&, T& ast, Context const& context) |
| 152 | { |
| 153 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 154 | const Schema& schema = parserContext.m_schema; |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 155 | |
Václav Kubernát | 72749c6 | 2020-01-03 16:47:34 +0100 | [diff] [blame] | 156 | if (!schema.isList(parserContext.currentSchemaPath(), {parserContext.m_curModule, ast.m_name})) { |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 157 | _pass(context) = false; |
| 158 | } |
| 159 | } |
| 160 | }; |
Václav Kubernát | 60d6f29 | 2018-05-25 09:45:32 +0200 | [diff] [blame] | 161 | struct nodeup_class { |
| 162 | template <typename T, typename Iterator, typename Context> |
| 163 | void on_success(Iterator const&, Iterator const&, T&, Context const& context) |
| 164 | { |
| 165 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 166 | |
Václav Kubernát | 72749c6 | 2020-01-03 16:47:34 +0100 | [diff] [blame] | 167 | if (parserContext.currentSchemaPath().m_nodes.empty()) |
Václav Kubernát | 60d6f29 | 2018-05-25 09:45:32 +0200 | [diff] [blame] | 168 | _pass(context) = false; |
Václav Kubernát | 60d6f29 | 2018-05-25 09:45:32 +0200 | [diff] [blame] | 169 | } |
| 170 | }; |
| 171 | |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 172 | struct container_class { |
| 173 | template <typename T, typename Iterator, typename Context> |
| 174 | void on_success(Iterator const&, Iterator const&, T& ast, Context const& context) |
| 175 | { |
| 176 | auto& parserContext = x3::get<parser_context_tag>(context); |
Václav Kubernát | 48fc383 | 2018-05-28 14:21:22 +0200 | [diff] [blame] | 177 | const auto& schema = parserContext.m_schema; |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 178 | |
Václav Kubernát | 72749c6 | 2020-01-03 16:47:34 +0100 | [diff] [blame] | 179 | if (!schema.isContainer(parserContext.currentSchemaPath(), {parserContext.m_curModule, ast.m_name})) |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 180 | _pass(context) = false; |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 181 | } |
| 182 | }; |
| 183 | |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 184 | struct leaf_class { |
| 185 | template <typename T, typename Iterator, typename Context> |
| 186 | void on_success(Iterator const&, Iterator const&, T& ast, Context const& context) |
| 187 | { |
| 188 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 189 | const auto& schema = parserContext.m_schema; |
| 190 | |
Václav Kubernát | 72749c6 | 2020-01-03 16:47:34 +0100 | [diff] [blame] | 191 | if (!schema.isLeaf(parserContext.currentSchemaPath(), {parserContext.m_curModule, ast.m_name})) |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 192 | _pass(context) = false; |
| 193 | } |
| 194 | }; |
| 195 | |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 196 | struct module_class { |
| 197 | template <typename T, typename Iterator, typename Context> |
| 198 | void on_success(Iterator const&, Iterator const&, T& ast, Context const& context) |
| 199 | { |
| 200 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 201 | const auto& schema = parserContext.m_schema; |
| 202 | |
Václav Kubernát | 75877de | 2019-11-20 17:43:02 +0100 | [diff] [blame] | 203 | if (schema.isModule(ast.m_name)) { |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 204 | parserContext.m_curModule = ast.m_name; |
| 205 | parserContext.m_topLevelModulePresent = true; |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 206 | } else { |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 207 | parserContext.m_errorMsg = "Invalid module name."; |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 208 | _pass(context) = false; |
| 209 | } |
| 210 | } |
| 211 | }; |
| 212 | |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 213 | struct schemaNode_class { |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 214 | template <typename T, typename Iterator, typename Context> |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 215 | 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] | 216 | { |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 217 | auto& parserContext = x3::get<parser_context_tag>(context); |
Václav Kubernát | 72749c6 | 2020-01-03 16:47:34 +0100 | [diff] [blame] | 218 | parserContext.pushPathFragment(ast); |
| 219 | parserContext.m_curModule = boost::none; |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 220 | } |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 221 | }; |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 222 | |
Václav Kubernát | 39ae959 | 2019-02-05 17:35:16 +0100 | [diff] [blame] | 223 | struct dataNodeList_class { |
| 224 | template <typename T, typename Iterator, typename Context> |
| 225 | void on_success(Iterator const&, Iterator const&, T& ast, Context const& context) |
| 226 | { |
| 227 | auto& parserContext = x3::get<parser_context_tag>(context); |
Václav Kubernát | 72749c6 | 2020-01-03 16:47:34 +0100 | [diff] [blame] | 228 | parserContext.pushPathFragment(ast); |
Václav Kubernát | 39ae959 | 2019-02-05 17:35:16 +0100 | [diff] [blame] | 229 | } |
| 230 | }; |
Václav Kubernát | 5c75b25 | 2018-10-10 18:33:47 +0200 | [diff] [blame] | 231 | |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 232 | struct dataNode_class { |
| 233 | template <typename T, typename Iterator, typename Context> |
| 234 | void on_success(Iterator const&, Iterator const&, T& ast, Context const& context) |
| 235 | { |
| 236 | auto& parserContext = x3::get<parser_context_tag>(context); |
Václav Kubernát | 72749c6 | 2020-01-03 16:47:34 +0100 | [diff] [blame] | 237 | parserContext.pushPathFragment(ast); |
| 238 | parserContext.m_curModule = boost::none; |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 239 | } |
| 240 | }; |
| 241 | |
Václav Kubernát | 37171a1 | 2018-08-31 17:01:48 +0200 | [diff] [blame] | 242 | struct absoluteStart_class { |
| 243 | template <typename T, typename Iterator, typename Context> |
| 244 | void on_success(Iterator const&, Iterator const&, T&, Context const& context) |
| 245 | { |
| 246 | auto& parserContext = x3::get<parser_context_tag>(context); |
Václav Kubernát | 72749c6 | 2020-01-03 16:47:34 +0100 | [diff] [blame] | 247 | parserContext.clearPath(); |
Václav Kubernát | 37171a1 | 2018-08-31 17:01:48 +0200 | [diff] [blame] | 248 | } |
| 249 | }; |
| 250 | |
Václav Kubernát | 5c75b25 | 2018-10-10 18:33:47 +0200 | [diff] [blame] | 251 | struct dataNodesListEnd_class; |
| 252 | |
| 253 | struct dataPathListEnd_class; |
| 254 | |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 255 | struct dataPath_class { |
| 256 | template <typename Iterator, typename Exception, typename Context> |
| 257 | x3::error_handler_result on_error(Iterator&, Iterator const&, Exception const&, Context const& context) |
| 258 | { |
| 259 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 260 | if (parserContext.m_errorMsg.empty()) { |
| 261 | parserContext.m_errorMsg = "Expected path."; |
| 262 | return x3::error_handler_result::fail; |
| 263 | } else { |
| 264 | return x3::error_handler_result::rethrow; |
| 265 | } |
| 266 | } |
| 267 | }; |
| 268 | |
| 269 | struct schemaPath_class { |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 270 | template <typename Iterator, typename Exception, typename Context> |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame] | 271 | 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] | 272 | { |
| 273 | auto& parserContext = x3::get<parser_context_tag>(context); |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame] | 274 | if (parserContext.m_errorMsg.empty()) { |
| 275 | parserContext.m_errorMsg = "Expected path."; |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 276 | return x3::error_handler_result::fail; |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame] | 277 | } else { |
| 278 | return x3::error_handler_result::rethrow; |
| 279 | } |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 280 | } |
| 281 | }; |
| 282 | |
Václav Kubernát | 6d79143 | 2018-10-25 16:00:35 +0200 | [diff] [blame] | 283 | struct discard_class; |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 284 | |
Václav Kubernát | 11afac7 | 2018-07-18 14:59:53 +0200 | [diff] [blame] | 285 | struct ls_class; |
| 286 | |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 287 | struct cd_class { |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 288 | template <typename Iterator, typename Exception, typename Context> |
| 289 | x3::error_handler_result on_error(Iterator&, Iterator const&, Exception const& x, Context const& context) |
| 290 | { |
| 291 | auto& parserContext = x3::get<parser_context_tag>(context); |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame] | 292 | if (parserContext.m_errorMsg.empty()) |
| 293 | parserContext.m_errorMsg = "Expected " + x.which() + " here:"; |
| 294 | return x3::error_handler_result::rethrow; |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 295 | } |
| 296 | }; |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 297 | |
Václav Kubernát | 6797df0 | 2019-03-18 20:21:50 +0100 | [diff] [blame] | 298 | struct presenceContainerPath_class { |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 299 | template <typename T, typename Iterator, typename Context> |
| 300 | void on_success(Iterator const&, Iterator const&, T& ast, Context const& context) |
| 301 | { |
| 302 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 303 | const auto& schema = parserContext.m_schema; |
| 304 | try { |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 305 | boost::optional<std::string> module; |
Václav Kubernát | 6797df0 | 2019-03-18 20:21:50 +0100 | [diff] [blame] | 306 | if (ast.m_nodes.back().m_prefix) |
| 307 | module = ast.m_nodes.back().m_prefix.value().m_name; |
| 308 | container_ cont = boost::get<container_>(ast.m_nodes.back().m_suffix); |
Václav Kubernát | 72749c6 | 2020-01-03 16:47:34 +0100 | [diff] [blame] | 309 | auto location = pathWithoutLastNode(parserContext.currentSchemaPath()); |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 310 | |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 311 | if (!schema.isPresenceContainer(location, {module, cont.m_name})) { |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame] | 312 | parserContext.m_errorMsg = "This container is not a presence 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 | } catch (boost::bad_get&) { |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame] | 316 | parserContext.m_errorMsg = "This is not a container."; |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 317 | _pass(context) = false; |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 318 | } |
| 319 | } |
Václav Kubernát | 6797df0 | 2019-03-18 20:21:50 +0100 | [diff] [blame] | 320 | }; |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 321 | |
Václav Kubernát | f5f64f0 | 2019-03-19 17:15:47 +0100 | [diff] [blame] | 322 | struct listInstancePath_class { |
| 323 | template <typename T, typename Iterator, typename Context> |
| 324 | void on_success(Iterator const&, Iterator const&, T& ast, Context const& context) |
| 325 | { |
| 326 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 327 | if (ast.m_nodes.back().m_suffix.type() != typeid(listElement_)) { |
| 328 | parserContext.m_errorMsg = "This is not a list instance."; |
| 329 | _pass(context) = false; |
| 330 | } |
| 331 | } |
| 332 | }; |
| 333 | |
Václav Kubernát | 8028f94 | 2019-09-25 16:03:23 +0200 | [diff] [blame] | 334 | struct space_separator_class { |
| 335 | template <typename T, typename Iterator, typename Context> |
| 336 | void on_success(Iterator const&, Iterator const&, T&, Context const& context) |
| 337 | { |
| 338 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 339 | parserContext.m_suggestions.clear(); |
Václav Kubernát | 1ed4aa3 | 2020-01-23 13:13:28 +0100 | [diff] [blame] | 340 | parserContext.m_completionIterator = boost::none; |
Václav Kubernát | e69133a | 2019-11-01 19:01:34 +0100 | [diff] [blame] | 341 | parserContext.m_completionSuffix.clear(); |
Václav Kubernát | 8028f94 | 2019-09-25 16:03:23 +0200 | [diff] [blame] | 342 | } |
| 343 | }; |
| 344 | |
Václav Kubernát | 6797df0 | 2019-03-18 20:21:50 +0100 | [diff] [blame] | 345 | struct create_class { |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 346 | template <typename Iterator, typename Exception, typename Context> |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame] | 347 | 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] | 348 | { |
| 349 | auto& parserContext = x3::get<parser_context_tag>(context); |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame] | 350 | if (parserContext.m_errorMsg.empty()) |
| 351 | parserContext.m_errorMsg = "Couldn't parse create/delete command."; |
| 352 | return x3::error_handler_result::rethrow; |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 353 | } |
| 354 | }; |
| 355 | |
Václav Kubernát | 6797df0 | 2019-03-18 20:21:50 +0100 | [diff] [blame] | 356 | struct delete_class { |
| 357 | template <typename Iterator, typename Exception, typename Context> |
| 358 | x3::error_handler_result on_error(Iterator&, Iterator const&, Exception const&, Context const& context) |
| 359 | { |
| 360 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 361 | if (parserContext.m_errorMsg.empty()) |
| 362 | parserContext.m_errorMsg = "Couldn't parse create/delete command."; |
| 363 | return x3::error_handler_result::rethrow; |
| 364 | } |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 365 | }; |
| 366 | |
Václav Kubernát | 0b0272f | 2018-06-13 14:13:08 +0200 | [diff] [blame] | 367 | struct leaf_path_class { |
| 368 | template <typename T, typename Iterator, typename Context> |
Václav Kubernát | 7707cae | 2020-01-16 12:04:53 +0100 | [diff] [blame] | 369 | void on_success(Iterator const&, Iterator const&, T&, Context const& context) |
Václav Kubernát | 0b0272f | 2018-06-13 14:13:08 +0200 | [diff] [blame] | 370 | { |
| 371 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 372 | try { |
Václav Kubernát | 7707cae | 2020-01-16 12:04:53 +0100 | [diff] [blame] | 373 | auto lastNode = parserContext.currentSchemaPath().m_nodes.back(); |
| 374 | auto leaf = boost::get<leaf_>(lastNode.m_suffix); |
| 375 | auto location = pathWithoutLastNode(parserContext.currentSchemaPath()); |
| 376 | ModuleNodePair node{lastNode.m_prefix.flat_map([](const auto& it) { return boost::optional<std::string>{it.m_name}; }), leaf.m_name}; |
| 377 | |
| 378 | parserContext.m_tmpListKeyLeafPath.m_location = location; |
| 379 | parserContext.m_tmpListKeyLeafPath.m_node = node; |
| 380 | |
Václav Kubernát | 0b0272f | 2018-06-13 14:13:08 +0200 | [diff] [blame] | 381 | } catch (boost::bad_get&) { |
| 382 | parserContext.m_errorMsg = "This is not a path to leaf."; |
| 383 | _pass(context) = false; |
| 384 | } |
| 385 | } |
| 386 | }; |
| 387 | |
Václav Kubernát | eeb3884 | 2019-03-20 19:46:05 +0100 | [diff] [blame] | 388 | // This handler only checks if the module exists |
| 389 | // It doesn't set any ParserContext flags (except the error message) |
| 390 | struct data_module_prefix_class { |
| 391 | template <typename T, typename Iterator, typename Context> |
| 392 | void on_success(Iterator const&, Iterator const&, T& ast, Context const& context) |
| 393 | { |
| 394 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 395 | const auto& schema = parserContext.m_schema; |
| 396 | |
Václav Kubernát | 72749c6 | 2020-01-03 16:47:34 +0100 | [diff] [blame] | 397 | if (!schema.isModule(parserContext.currentSchemaPath(), ast.m_name)) { |
Václav Kubernát | eeb3884 | 2019-03-20 19:46:05 +0100 | [diff] [blame] | 398 | parserContext.m_errorMsg = "Invalid module name."; |
| 399 | _pass(context) = false; |
| 400 | } |
| 401 | } |
| 402 | }; |
| 403 | |
Václav Kubernát | ebca255 | 2018-06-08 19:06:02 +0200 | [diff] [blame] | 404 | struct leaf_data_class { |
Václav Kubernát | 0b0272f | 2018-06-13 14:13:08 +0200 | [diff] [blame] | 405 | template <typename Iterator, typename Exception, typename Context> |
| 406 | x3::error_handler_result on_error(Iterator&, Iterator const&, Exception const&, Context const& context) |
| 407 | { |
| 408 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 409 | auto& schema = parserContext.m_schema; |
| 410 | if (parserContext.m_errorMsg.empty()) { |
Václav Kubernát | 7707cae | 2020-01-16 12:04:53 +0100 | [diff] [blame] | 411 | parserContext.m_errorMsg = "leaf data type mismatch: Expected " + |
| 412 | leafDataTypeToString(schema.leafType(parserContext.m_tmpListKeyLeafPath.m_location, parserContext.m_tmpListKeyLeafPath.m_node)) + " here:"; |
Václav Kubernát | 0b0272f | 2018-06-13 14:13:08 +0200 | [diff] [blame] | 413 | return x3::error_handler_result::fail; |
| 414 | } |
| 415 | return x3::error_handler_result::rethrow; |
| 416 | } |
Václav Kubernát | ebca255 | 2018-06-08 19:06:02 +0200 | [diff] [blame] | 417 | }; |
| 418 | |
Václav Kubernát | 90de950 | 2019-11-20 17:19:44 +0100 | [diff] [blame] | 419 | template <yang::LeafDataTypes TYPE> |
Václav Kubernát | ebca255 | 2018-06-08 19:06:02 +0200 | [diff] [blame] | 420 | struct leaf_data_base_class { |
| 421 | yang::LeafDataTypes m_type; |
| 422 | |
Ivona Oboňová | 88c78ca | 2019-07-02 18:40:07 +0200 | [diff] [blame] | 423 | leaf_data_base_class() |
| 424 | : m_type(TYPE) |
Václav Kubernát | ebca255 | 2018-06-08 19:06:02 +0200 | [diff] [blame] | 425 | { |
| 426 | } |
| 427 | |
| 428 | template <typename T, typename Iterator, typename Context> |
| 429 | void on_success(Iterator const&, Iterator const&, T&, Context const& context) |
| 430 | { |
| 431 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 432 | auto& schema = parserContext.m_schema; |
| 433 | |
Václav Kubernát | 7707cae | 2020-01-16 12:04:53 +0100 | [diff] [blame] | 434 | auto type = schema.leafType(parserContext.m_tmpListKeyLeafPath.m_location, parserContext.m_tmpListKeyLeafPath.m_node); |
Václav Kubernát | 6a8d1d9 | 2019-04-24 20:30:36 +0200 | [diff] [blame] | 435 | if (type == yang::LeafDataTypes::LeafRef) { |
Václav Kubernát | 7707cae | 2020-01-16 12:04:53 +0100 | [diff] [blame] | 436 | type = schema.leafrefBase(parserContext.m_tmpListKeyLeafPath.m_location, parserContext.m_tmpListKeyLeafPath.m_node); |
Václav Kubernát | 6a8d1d9 | 2019-04-24 20:30:36 +0200 | [diff] [blame] | 437 | } |
Václav Kubernát | 7707cae | 2020-01-16 12:04:53 +0100 | [diff] [blame] | 438 | |
Václav Kubernát | 6a8d1d9 | 2019-04-24 20:30:36 +0200 | [diff] [blame] | 439 | if (type != m_type) { |
Václav Kubernát | ebca255 | 2018-06-08 19:06:02 +0200 | [diff] [blame] | 440 | _pass(context) = false; |
| 441 | } |
| 442 | } |
| 443 | }; |
| 444 | |
Ivona Oboňová | 88c78ca | 2019-07-02 18:40:07 +0200 | [diff] [blame] | 445 | struct leaf_data_binary_data_class; |
| 446 | |
| 447 | struct leaf_data_enum_class : leaf_data_base_class<yang::LeafDataTypes::Enum> { |
| 448 | using leaf_data_base_class::leaf_data_base_class; |
Václav Kubernát | ebca255 | 2018-06-08 19:06:02 +0200 | [diff] [blame] | 449 | |
| 450 | template <typename T, typename Iterator, typename Context> |
| 451 | void on_success(Iterator const& start, Iterator const& end, T& ast, Context const& context) |
| 452 | { |
| 453 | leaf_data_base_class::on_success(start, end, ast, context); |
Václav Kubernát | eeb3884 | 2019-03-20 19:46:05 +0100 | [diff] [blame] | 454 | // Base class on_success cannot return for us, so we check if it failed the parser. |
| 455 | if (_pass(context) == false) |
| 456 | return; |
Václav Kubernát | ebca255 | 2018-06-08 19:06:02 +0200 | [diff] [blame] | 457 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 458 | auto& schema = parserContext.m_schema; |
| 459 | |
Václav Kubernát | 7707cae | 2020-01-16 12:04:53 +0100 | [diff] [blame] | 460 | if (!schema.leafEnumHasValue(parserContext.m_tmpListKeyLeafPath.m_location, parserContext.m_tmpListKeyLeafPath.m_node, ast.m_value)) { |
Václav Kubernát | ebca255 | 2018-06-08 19:06:02 +0200 | [diff] [blame] | 461 | _pass(context) = false; |
Václav Kubernát | 777704d | 2020-01-15 18:48:06 +0100 | [diff] [blame] | 462 | parserContext.m_errorMsg = "leaf data type mismatch: Expected an enum here. Allowed values:"; |
Václav Kubernát | 7707cae | 2020-01-16 12:04:53 +0100 | [diff] [blame] | 463 | for (const auto& it : schema.enumValues(parserContext.m_tmpListKeyLeafPath.m_location, parserContext.m_tmpListKeyLeafPath.m_node)) { |
Václav Kubernát | 6971025 | 2019-11-15 17:08:30 +0100 | [diff] [blame] | 464 | parserContext.m_errorMsg += " " + it; |
| 465 | } |
Václav Kubernát | ebca255 | 2018-06-08 19:06:02 +0200 | [diff] [blame] | 466 | } |
| 467 | } |
| 468 | }; |
| 469 | |
Václav Kubernát | eeb3884 | 2019-03-20 19:46:05 +0100 | [diff] [blame] | 470 | struct leaf_data_identityRef_data_class; |
| 471 | |
Ivona Oboňová | 88c78ca | 2019-07-02 18:40:07 +0200 | [diff] [blame] | 472 | struct leaf_data_identityRef_class : leaf_data_base_class<yang::LeafDataTypes::IdentityRef> { |
| 473 | using leaf_data_base_class::leaf_data_base_class; |
Václav Kubernát | eeb3884 | 2019-03-20 19:46:05 +0100 | [diff] [blame] | 474 | |
| 475 | template <typename T, typename Iterator, typename Context> |
| 476 | void on_success(Iterator const& start, Iterator const& end, T& ast, Context const& context) |
| 477 | { |
| 478 | // FIXME: can I reuse leaf_data_enum_class somehow..? |
| 479 | leaf_data_base_class::on_success(start, end, ast, context); |
| 480 | // Base class on_success cannot return for us, so we check if it failed the parser. |
| 481 | if (_pass(context) == false) |
| 482 | return; |
| 483 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 484 | auto& schema = parserContext.m_schema; |
Václav Kubernát | eeb3884 | 2019-03-20 19:46:05 +0100 | [diff] [blame] | 485 | |
| 486 | ModuleValuePair pair; |
| 487 | if (ast.m_prefix) { |
| 488 | pair.first = ast.m_prefix.get().m_name; |
| 489 | } |
| 490 | pair.second = ast.m_value; |
| 491 | |
Václav Kubernát | 7707cae | 2020-01-16 12:04:53 +0100 | [diff] [blame] | 492 | if (!schema.leafIdentityIsValid(parserContext.m_tmpListKeyLeafPath.m_location, parserContext.m_tmpListKeyLeafPath.m_node, pair)) { |
Václav Kubernát | eeb3884 | 2019-03-20 19:46:05 +0100 | [diff] [blame] | 493 | _pass(context) = false; |
| 494 | } |
| 495 | } |
| 496 | }; |
| 497 | |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 498 | struct set_class { |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 499 | template <typename Iterator, typename Exception, typename Context> |
| 500 | x3::error_handler_result on_error(Iterator&, Iterator const&, Exception const& x, Context const& context) |
| 501 | { |
| 502 | auto& parserContext = x3::get<parser_context_tag>(context); |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame] | 503 | if (parserContext.m_errorMsg.empty()) |
| 504 | parserContext.m_errorMsg = "Expected " + x.which() + " here:"; |
| 505 | return x3::error_handler_result::rethrow; |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 506 | } |
| 507 | }; |
| 508 | |
Václav Kubernát | 812ee28 | 2018-08-30 17:10:03 +0200 | [diff] [blame] | 509 | struct commit_class; |
| 510 | |
Václav Kubernát | 054cc99 | 2019-02-21 14:23:52 +0100 | [diff] [blame] | 511 | struct help_class; |
| 512 | |
Václav Kubernát | b6ff0b6 | 2018-08-30 16:14:53 +0200 | [diff] [blame] | 513 | struct get_class; |
| 514 | |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 515 | struct command_class { |
| 516 | template <typename Iterator, typename Exception, typename Context> |
| 517 | x3::error_handler_result on_error(Iterator&, Iterator const&, Exception const& x, Context const& context) |
| 518 | { |
| 519 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 520 | 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] | 521 | if (parserContext.m_errorMsg.empty()) { |
| 522 | parserContext.m_errorMsg = "Unknown command."; |
| 523 | } |
| 524 | error_handler(x.where(), parserContext.m_errorMsg); |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 525 | return x3::error_handler_result::fail; |
| 526 | } |
| 527 | }; |
Václav Kubernát | 5c75b25 | 2018-10-10 18:33:47 +0200 | [diff] [blame] | 528 | |
Václav Kubernát | 4108e0d | 2018-10-29 13:32:22 +0100 | [diff] [blame] | 529 | struct initializePath_class { |
Václav Kubernát | 5c75b25 | 2018-10-10 18:33:47 +0200 | [diff] [blame] | 530 | template <typename T, typename Iterator, typename Context> |
| 531 | void on_success(Iterator const&, Iterator const&, T&, Context const& context) |
| 532 | { |
| 533 | auto& parserContext = x3::get<parser_context_tag>(context); |
Václav Kubernát | 72749c6 | 2020-01-03 16:47:34 +0100 | [diff] [blame] | 534 | parserContext.resetPath(); |
Václav Kubernát | 5c75b25 | 2018-10-10 18:33:47 +0200 | [diff] [blame] | 535 | parserContext.m_tmpListKeys.clear(); |
| 536 | parserContext.m_tmpListName.clear(); |
Václav Kubernát | 4108e0d | 2018-10-29 13:32:22 +0100 | [diff] [blame] | 537 | parserContext.m_suggestions.clear(); |
Václav Kubernát | 5c75b25 | 2018-10-10 18:33:47 +0200 | [diff] [blame] | 538 | } |
| 539 | }; |
Václav Kubernát | d6fd249 | 2018-11-19 15:11:16 +0100 | [diff] [blame] | 540 | |
| 541 | struct trailingSlash_class; |
Václav Kubernát | 4108e0d | 2018-10-29 13:32:22 +0100 | [diff] [blame] | 542 | |
| 543 | struct createPathSuggestions_class { |
| 544 | template <typename T, typename Iterator, typename Context> |
| 545 | void on_success(Iterator const& begin, Iterator const&, T&, Context const& context) |
| 546 | { |
| 547 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 548 | const auto& schema = parserContext.m_schema; |
| 549 | |
| 550 | parserContext.m_completionIterator = begin; |
Václav Kubernát | 72749c6 | 2020-01-03 16:47:34 +0100 | [diff] [blame] | 551 | auto suggestions = schema.childNodes(parserContext.currentSchemaPath(), Recursion::NonRecursive); |
Václav Kubernát | e69133a | 2019-11-01 19:01:34 +0100 | [diff] [blame] | 552 | std::set<std::string> suffixesAdded; |
| 553 | std::transform(suggestions.begin(), suggestions.end(), |
| 554 | std::inserter(suffixesAdded, suffixesAdded.end()), |
| 555 | [&parserContext, &schema] (auto it) { |
| 556 | ModuleNodePair node; |
| 557 | if (auto colonPos = it.find(":"); colonPos != it.npos) { |
| 558 | node.first = it.substr(0, colonPos); |
| 559 | node.second = it.substr(colonPos + 1, node.second.npos); |
| 560 | } else { |
| 561 | node.first = boost::none; |
| 562 | node.second = it; |
| 563 | } |
| 564 | |
Václav Kubernát | 72749c6 | 2020-01-03 16:47:34 +0100 | [diff] [blame] | 565 | if (schema.isLeaf(parserContext.currentSchemaPath(), node)) { |
Václav Kubernát | e69133a | 2019-11-01 19:01:34 +0100 | [diff] [blame] | 566 | return it + " "; |
| 567 | } |
Václav Kubernát | 72749c6 | 2020-01-03 16:47:34 +0100 | [diff] [blame] | 568 | if (schema.isContainer(parserContext.currentSchemaPath(), node)) { |
Václav Kubernát | e69133a | 2019-11-01 19:01:34 +0100 | [diff] [blame] | 569 | return it + "/"; |
| 570 | } |
Václav Kubernát | 72749c6 | 2020-01-03 16:47:34 +0100 | [diff] [blame] | 571 | if (schema.isList(parserContext.currentSchemaPath(), node)) { |
Václav Kubernát | e69133a | 2019-11-01 19:01:34 +0100 | [diff] [blame] | 572 | return it + "["; |
| 573 | } |
| 574 | return it; |
| 575 | }); |
| 576 | parserContext.m_suggestions = suffixesAdded; |
Václav Kubernát | 4108e0d | 2018-10-29 13:32:22 +0100 | [diff] [blame] | 577 | } |
| 578 | }; |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 579 | |
Václav Kubernát | 43908fb | 2020-01-02 19:05:51 +0100 | [diff] [blame] | 580 | std::set<std::string> generateMissingKeyCompletionSet(std::set<std::string> keysNeeded, std::map<std::string, leaf_data_> currentSet); |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 581 | |
| 582 | struct createKeySuggestions_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 | |
Václav Kubernát | 72749c6 | 2020-01-03 16:47:34 +0100 | [diff] [blame] | 591 | const auto& keysNeeded = schema.listKeys(parserContext.currentSchemaPath(), {parserContext.m_curModule, parserContext.m_tmpListName}); |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 592 | parserContext.m_suggestions = generateMissingKeyCompletionSet(keysNeeded, parserContext.m_tmpListKeys); |
| 593 | } |
| 594 | }; |
| 595 | |
Václav Kubernát | 43908fb | 2020-01-02 19:05:51 +0100 | [diff] [blame] | 596 | std::string leafDataToCompletion(const leaf_data_& value); |
| 597 | |
| 598 | struct createValueSuggestions_class { |
| 599 | template <typename T, typename Iterator, typename Context> |
| 600 | void on_success(Iterator const& begin, Iterator const&, T&, Context const& context) |
| 601 | { |
| 602 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 603 | if (!parserContext.m_completing) { |
| 604 | return; |
| 605 | } |
| 606 | const auto& dataQuery = parserContext.m_dataquery; |
| 607 | |
| 608 | parserContext.m_completionIterator = begin; |
| 609 | auto listInstances = dataQuery->listKeys(parserContext.currentDataPath(), {parserContext.m_curModule, parserContext.m_tmpListName}); |
| 610 | |
| 611 | decltype(listInstances) filteredInstances; |
| 612 | //This filters out instances, which don't correspond to the partial instance we have. |
| 613 | const auto partialFitsComplete = [&parserContext] (const auto& complete) { |
| 614 | const auto& partial = parserContext.m_tmpListKeys; |
| 615 | return std::all_of(partial.begin(), partial.end(), [&complete] (const auto& oneKV) { |
| 616 | const auto& [k, v] = oneKV; |
| 617 | return complete.at(k) == v; |
| 618 | }); |
| 619 | }; |
| 620 | std::copy_if(listInstances.begin(), listInstances.end(), std::inserter(filteredInstances, filteredInstances.end()), partialFitsComplete); |
| 621 | |
| 622 | std::set<std::string> validValues; |
| 623 | |
| 624 | std::transform(filteredInstances.begin(), filteredInstances.end(), std::inserter(validValues, validValues.end()), [&parserContext] (const auto& instance) { |
| 625 | return leafDataToCompletion(instance.at(parserContext.m_tmpListKeyLeafPath.m_node.second)); |
| 626 | }); |
| 627 | |
| 628 | parserContext.m_suggestions = validValues; |
| 629 | } |
| 630 | }; |
| 631 | |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 632 | struct suggestKeysEnd_class { |
| 633 | template <typename T, typename Iterator, typename Context> |
| 634 | void on_success(Iterator const& begin, Iterator const&, T&, Context const& context) |
| 635 | { |
| 636 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 637 | const auto& schema = parserContext.m_schema; |
| 638 | |
| 639 | parserContext.m_completionIterator = begin; |
Václav Kubernát | 72749c6 | 2020-01-03 16:47:34 +0100 | [diff] [blame] | 640 | const auto& keysNeeded = schema.listKeys(parserContext.currentSchemaPath(), {parserContext.m_curModule, parserContext.m_tmpListName}); |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 641 | if (generateMissingKeyCompletionSet(keysNeeded, parserContext.m_tmpListKeys).empty()) { |
| 642 | parserContext.m_suggestions = {"]/"}; |
| 643 | } else { |
Václav Kubernát | 4c32548 | 2019-04-11 17:51:55 +0200 | [diff] [blame] | 644 | parserContext.m_suggestions = {"]["}; |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 645 | } |
| 646 | } |
| 647 | }; |
Václav Kubernát | 5727242 | 2019-02-08 12:48:24 +0100 | [diff] [blame] | 648 | |
| 649 | struct commandNamesVisitor { |
| 650 | template <typename T> |
| 651 | auto operator()(boost::type<T>) |
| 652 | { |
| 653 | return T::name; |
| 654 | } |
| 655 | }; |
| 656 | |
| 657 | struct createCommandSuggestions_class { |
| 658 | template <typename T, typename Iterator, typename Context> |
| 659 | void on_success(Iterator const& begin, Iterator const&, T&, Context const& context) |
| 660 | { |
| 661 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 662 | parserContext.m_completionIterator = begin; |
| 663 | |
| 664 | parserContext.m_suggestions.clear(); |
| 665 | boost::mpl::for_each<CommandTypes, boost::type<boost::mpl::_>>([&parserContext](auto cmd) { |
| 666 | parserContext.m_suggestions.emplace(commandNamesVisitor()(cmd)); |
| 667 | }); |
| 668 | } |
| 669 | }; |
Václav Kubernát | ac035d6 | 2019-02-18 10:59:08 +0100 | [diff] [blame] | 670 | |
| 671 | struct completing_class { |
| 672 | template <typename T, typename Iterator, typename Context> |
| 673 | void on_success(Iterator const&, Iterator const&, T&, Context const& context) |
| 674 | { |
| 675 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 676 | |
| 677 | if (!parserContext.m_completing) |
| 678 | _pass(context) = false; |
| 679 | } |
| 680 | }; |
Václav Kubernát | 989b5de | 2019-02-20 16:28:35 +0100 | [diff] [blame] | 681 | |
| 682 | struct createEnumSuggestions_class { |
| 683 | template <typename T, typename Iterator, typename Context> |
| 684 | void on_success(Iterator const& begin, Iterator const&, T&, Context const& context) |
| 685 | { |
| 686 | auto& parserContext = x3::get<parser_context_tag>(context); |
Václav Kubernát | 989b5de | 2019-02-20 16:28:35 +0100 | [diff] [blame] | 687 | const Schema& schema = parserContext.m_schema; |
| 688 | |
Václav Kubernát | eeb3884 | 2019-03-20 19:46:05 +0100 | [diff] [blame] | 689 | // Only generate completions if the type is enum so that we don't |
| 690 | // overwrite some other completions. |
Václav Kubernát | 117f7b5 | 2020-01-23 16:10:55 +0100 | [diff] [blame] | 691 | if (schema.leafType(parserContext.m_tmpListKeyLeafPath.m_location, parserContext.m_tmpListKeyLeafPath.m_node) == yang::LeafDataTypes::Enum) { |
| 692 | parserContext.m_completionIterator = begin; |
Václav Kubernát | 7707cae | 2020-01-16 12:04:53 +0100 | [diff] [blame] | 693 | parserContext.m_suggestions = schema.enumValues(parserContext.m_tmpListKeyLeafPath.m_location, parserContext.m_tmpListKeyLeafPath.m_node); |
Václav Kubernát | 117f7b5 | 2020-01-23 16:10:55 +0100 | [diff] [blame] | 694 | } |
Václav Kubernát | eeb3884 | 2019-03-20 19:46:05 +0100 | [diff] [blame] | 695 | } |
| 696 | }; |
| 697 | |
| 698 | // FIXME: can I reuse createEnumSuggestions? |
| 699 | struct createIdentitySuggestions_class { |
| 700 | template <typename T, typename Iterator, typename Context> |
| 701 | void on_success(Iterator const& begin, Iterator const&, T&, Context const& context) |
| 702 | { |
| 703 | auto& parserContext = x3::get<parser_context_tag>(context); |
Václav Kubernát | eeb3884 | 2019-03-20 19:46:05 +0100 | [diff] [blame] | 704 | const Schema& schema = parserContext.m_schema; |
| 705 | |
Václav Kubernát | eeb3884 | 2019-03-20 19:46:05 +0100 | [diff] [blame] | 706 | // Only generate completions if the type is identityref so that we |
| 707 | // don't overwrite some other completions. |
Václav Kubernát | 117f7b5 | 2020-01-23 16:10:55 +0100 | [diff] [blame] | 708 | if (schema.leafType(parserContext.m_tmpListKeyLeafPath.m_location, parserContext.m_tmpListKeyLeafPath.m_node) == yang::LeafDataTypes::IdentityRef) { |
| 709 | parserContext.m_completionIterator = begin; |
Václav Kubernát | 7707cae | 2020-01-16 12:04:53 +0100 | [diff] [blame] | 710 | parserContext.m_suggestions = schema.validIdentities(parserContext.m_tmpListKeyLeafPath.m_location, parserContext.m_tmpListKeyLeafPath.m_node, Prefixes::WhenNeeded); |
Václav Kubernát | 117f7b5 | 2020-01-23 16:10:55 +0100 | [diff] [blame] | 711 | } |
Václav Kubernát | 989b5de | 2019-02-20 16:28:35 +0100 | [diff] [blame] | 712 | } |
| 713 | }; |