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