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 { |
| 33 | parserContext.m_tmpListKeys.insert(ast.first); |
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; |
| 71 | listNode.m_prefix = parserContext.m_curModule ? boost::optional<module_>{{*parserContext.m_curModule}} : boost::none; |
| 72 | listNode.m_suffix = list_{parserContext.m_tmpListName}; |
| 73 | location.m_nodes.push_back(listNode); |
| 74 | parserContext.m_tmpListKeyLeafPath.m_location = location; |
| 75 | parserContext.m_tmpListKeyLeafPath.m_node = {boost::none, ast}; |
| 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 | e69133a | 2019-11-01 19:01:34 +0100 | [diff] [blame] | 340 | parserContext.m_completionSuffix.clear(); |
Václav Kubernát | 8028f94 | 2019-09-25 16:03:23 +0200 | [diff] [blame] | 341 | } |
| 342 | }; |
| 343 | |
Václav Kubernát | 6797df0 | 2019-03-18 20:21:50 +0100 | [diff] [blame] | 344 | struct create_class { |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 345 | template <typename Iterator, typename Exception, typename Context> |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame] | 346 | 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] | 347 | { |
| 348 | auto& parserContext = x3::get<parser_context_tag>(context); |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame] | 349 | if (parserContext.m_errorMsg.empty()) |
| 350 | parserContext.m_errorMsg = "Couldn't parse create/delete command."; |
| 351 | return x3::error_handler_result::rethrow; |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 352 | } |
| 353 | }; |
| 354 | |
Václav Kubernát | 6797df0 | 2019-03-18 20:21:50 +0100 | [diff] [blame] | 355 | struct delete_class { |
| 356 | template <typename Iterator, typename Exception, typename Context> |
| 357 | x3::error_handler_result on_error(Iterator&, Iterator const&, Exception const&, Context const& context) |
| 358 | { |
| 359 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 360 | if (parserContext.m_errorMsg.empty()) |
| 361 | parserContext.m_errorMsg = "Couldn't parse create/delete command."; |
| 362 | return x3::error_handler_result::rethrow; |
| 363 | } |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 364 | }; |
| 365 | |
Václav Kubernát | 0b0272f | 2018-06-13 14:13:08 +0200 | [diff] [blame] | 366 | struct leaf_path_class { |
| 367 | template <typename T, typename Iterator, typename Context> |
Václav Kubernát | 7707cae | 2020-01-16 12:04:53 +0100 | [diff] [blame] | 368 | 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] | 369 | { |
| 370 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 371 | try { |
Václav Kubernát | 7707cae | 2020-01-16 12:04:53 +0100 | [diff] [blame] | 372 | auto lastNode = parserContext.currentSchemaPath().m_nodes.back(); |
| 373 | auto leaf = boost::get<leaf_>(lastNode.m_suffix); |
| 374 | auto location = pathWithoutLastNode(parserContext.currentSchemaPath()); |
| 375 | ModuleNodePair node{lastNode.m_prefix.flat_map([](const auto& it) { return boost::optional<std::string>{it.m_name}; }), leaf.m_name}; |
| 376 | |
| 377 | parserContext.m_tmpListKeyLeafPath.m_location = location; |
| 378 | parserContext.m_tmpListKeyLeafPath.m_node = node; |
| 379 | |
Václav Kubernát | 0b0272f | 2018-06-13 14:13:08 +0200 | [diff] [blame] | 380 | } catch (boost::bad_get&) { |
| 381 | parserContext.m_errorMsg = "This is not a path to leaf."; |
| 382 | _pass(context) = false; |
| 383 | } |
| 384 | } |
| 385 | }; |
| 386 | |
Václav Kubernát | eeb3884 | 2019-03-20 19:46:05 +0100 | [diff] [blame] | 387 | // This handler only checks if the module exists |
| 388 | // It doesn't set any ParserContext flags (except the error message) |
| 389 | struct data_module_prefix_class { |
| 390 | template <typename T, typename Iterator, typename Context> |
| 391 | void on_success(Iterator const&, Iterator const&, T& ast, Context const& context) |
| 392 | { |
| 393 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 394 | const auto& schema = parserContext.m_schema; |
| 395 | |
Václav Kubernát | 72749c6 | 2020-01-03 16:47:34 +0100 | [diff] [blame] | 396 | if (!schema.isModule(parserContext.currentSchemaPath(), ast.m_name)) { |
Václav Kubernát | eeb3884 | 2019-03-20 19:46:05 +0100 | [diff] [blame] | 397 | parserContext.m_errorMsg = "Invalid module name."; |
| 398 | _pass(context) = false; |
| 399 | } |
| 400 | } |
| 401 | }; |
| 402 | |
Václav Kubernát | ebca255 | 2018-06-08 19:06:02 +0200 | [diff] [blame] | 403 | struct leaf_data_class { |
Václav Kubernát | 0b0272f | 2018-06-13 14:13:08 +0200 | [diff] [blame] | 404 | template <typename Iterator, typename Exception, typename Context> |
| 405 | x3::error_handler_result on_error(Iterator&, Iterator const&, Exception const&, Context const& context) |
| 406 | { |
| 407 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 408 | auto& schema = parserContext.m_schema; |
| 409 | if (parserContext.m_errorMsg.empty()) { |
Václav Kubernát | 7707cae | 2020-01-16 12:04:53 +0100 | [diff] [blame] | 410 | parserContext.m_errorMsg = "leaf data type mismatch: Expected " + |
| 411 | 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] | 412 | return x3::error_handler_result::fail; |
| 413 | } |
| 414 | return x3::error_handler_result::rethrow; |
| 415 | } |
Václav Kubernát | ebca255 | 2018-06-08 19:06:02 +0200 | [diff] [blame] | 416 | }; |
| 417 | |
Václav Kubernát | 90de950 | 2019-11-20 17:19:44 +0100 | [diff] [blame] | 418 | template <yang::LeafDataTypes TYPE> |
Václav Kubernát | ebca255 | 2018-06-08 19:06:02 +0200 | [diff] [blame] | 419 | struct leaf_data_base_class { |
| 420 | yang::LeafDataTypes m_type; |
| 421 | |
Ivona Oboňová | 88c78ca | 2019-07-02 18:40:07 +0200 | [diff] [blame] | 422 | leaf_data_base_class() |
| 423 | : m_type(TYPE) |
Václav Kubernát | ebca255 | 2018-06-08 19:06:02 +0200 | [diff] [blame] | 424 | { |
| 425 | } |
| 426 | |
| 427 | template <typename T, typename Iterator, typename Context> |
| 428 | void on_success(Iterator const&, Iterator const&, T&, Context const& context) |
| 429 | { |
| 430 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 431 | auto& schema = parserContext.m_schema; |
| 432 | |
Václav Kubernát | 7707cae | 2020-01-16 12:04:53 +0100 | [diff] [blame] | 433 | 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] | 434 | if (type == yang::LeafDataTypes::LeafRef) { |
Václav Kubernát | 7707cae | 2020-01-16 12:04:53 +0100 | [diff] [blame] | 435 | 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] | 436 | } |
Václav Kubernát | 7707cae | 2020-01-16 12:04:53 +0100 | [diff] [blame] | 437 | |
Václav Kubernát | 6a8d1d9 | 2019-04-24 20:30:36 +0200 | [diff] [blame] | 438 | if (type != m_type) { |
Václav Kubernát | ebca255 | 2018-06-08 19:06:02 +0200 | [diff] [blame] | 439 | _pass(context) = false; |
| 440 | } |
| 441 | } |
| 442 | }; |
| 443 | |
Ivona Oboňová | 88c78ca | 2019-07-02 18:40:07 +0200 | [diff] [blame] | 444 | struct leaf_data_binary_data_class; |
| 445 | |
| 446 | struct leaf_data_enum_class : leaf_data_base_class<yang::LeafDataTypes::Enum> { |
| 447 | using leaf_data_base_class::leaf_data_base_class; |
Václav Kubernát | ebca255 | 2018-06-08 19:06:02 +0200 | [diff] [blame] | 448 | |
| 449 | template <typename T, typename Iterator, typename Context> |
| 450 | void on_success(Iterator const& start, Iterator const& end, T& ast, Context const& context) |
| 451 | { |
| 452 | leaf_data_base_class::on_success(start, end, ast, context); |
Václav Kubernát | eeb3884 | 2019-03-20 19:46:05 +0100 | [diff] [blame] | 453 | // Base class on_success cannot return for us, so we check if it failed the parser. |
| 454 | if (_pass(context) == false) |
| 455 | return; |
Václav Kubernát | ebca255 | 2018-06-08 19:06:02 +0200 | [diff] [blame] | 456 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 457 | auto& schema = parserContext.m_schema; |
| 458 | |
Václav Kubernát | 7707cae | 2020-01-16 12:04:53 +0100 | [diff] [blame] | 459 | 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] | 460 | _pass(context) = false; |
Václav Kubernát | 777704d | 2020-01-15 18:48:06 +0100 | [diff] [blame] | 461 | 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] | 462 | 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] | 463 | parserContext.m_errorMsg += " " + it; |
| 464 | } |
Václav Kubernát | ebca255 | 2018-06-08 19:06:02 +0200 | [diff] [blame] | 465 | } |
| 466 | } |
| 467 | }; |
| 468 | |
Václav Kubernát | eeb3884 | 2019-03-20 19:46:05 +0100 | [diff] [blame] | 469 | struct leaf_data_identityRef_data_class; |
| 470 | |
Ivona Oboňová | 88c78ca | 2019-07-02 18:40:07 +0200 | [diff] [blame] | 471 | struct leaf_data_identityRef_class : leaf_data_base_class<yang::LeafDataTypes::IdentityRef> { |
| 472 | using leaf_data_base_class::leaf_data_base_class; |
Václav Kubernát | eeb3884 | 2019-03-20 19:46:05 +0100 | [diff] [blame] | 473 | |
| 474 | template <typename T, typename Iterator, typename Context> |
| 475 | void on_success(Iterator const& start, Iterator const& end, T& ast, Context const& context) |
| 476 | { |
| 477 | // FIXME: can I reuse leaf_data_enum_class somehow..? |
| 478 | leaf_data_base_class::on_success(start, end, ast, context); |
| 479 | // Base class on_success cannot return for us, so we check if it failed the parser. |
| 480 | if (_pass(context) == false) |
| 481 | return; |
| 482 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 483 | auto& schema = parserContext.m_schema; |
Václav Kubernát | eeb3884 | 2019-03-20 19:46:05 +0100 | [diff] [blame] | 484 | |
| 485 | ModuleValuePair pair; |
| 486 | if (ast.m_prefix) { |
| 487 | pair.first = ast.m_prefix.get().m_name; |
| 488 | } |
| 489 | pair.second = ast.m_value; |
| 490 | |
Václav Kubernát | 7707cae | 2020-01-16 12:04:53 +0100 | [diff] [blame] | 491 | 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] | 492 | _pass(context) = false; |
| 493 | } |
| 494 | } |
| 495 | }; |
| 496 | |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 497 | struct set_class { |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 498 | template <typename Iterator, typename Exception, typename Context> |
| 499 | x3::error_handler_result on_error(Iterator&, Iterator const&, Exception const& x, Context const& context) |
| 500 | { |
| 501 | auto& parserContext = x3::get<parser_context_tag>(context); |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame] | 502 | if (parserContext.m_errorMsg.empty()) |
| 503 | parserContext.m_errorMsg = "Expected " + x.which() + " here:"; |
| 504 | return x3::error_handler_result::rethrow; |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 505 | } |
| 506 | }; |
| 507 | |
Václav Kubernát | 812ee28 | 2018-08-30 17:10:03 +0200 | [diff] [blame] | 508 | struct commit_class; |
| 509 | |
Václav Kubernát | 054cc99 | 2019-02-21 14:23:52 +0100 | [diff] [blame] | 510 | struct help_class; |
| 511 | |
Václav Kubernát | b6ff0b6 | 2018-08-30 16:14:53 +0200 | [diff] [blame] | 512 | struct get_class; |
| 513 | |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 514 | struct command_class { |
| 515 | template <typename Iterator, typename Exception, typename Context> |
| 516 | x3::error_handler_result on_error(Iterator&, Iterator const&, Exception const& x, Context const& context) |
| 517 | { |
| 518 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 519 | 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] | 520 | if (parserContext.m_errorMsg.empty()) { |
| 521 | parserContext.m_errorMsg = "Unknown command."; |
| 522 | } |
| 523 | error_handler(x.where(), parserContext.m_errorMsg); |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 524 | return x3::error_handler_result::fail; |
| 525 | } |
| 526 | }; |
Václav Kubernát | 5c75b25 | 2018-10-10 18:33:47 +0200 | [diff] [blame] | 527 | |
Václav Kubernát | 4108e0d | 2018-10-29 13:32:22 +0100 | [diff] [blame] | 528 | struct initializePath_class { |
Václav Kubernát | 5c75b25 | 2018-10-10 18:33:47 +0200 | [diff] [blame] | 529 | template <typename T, typename Iterator, typename Context> |
| 530 | void on_success(Iterator const&, Iterator const&, T&, Context const& context) |
| 531 | { |
| 532 | auto& parserContext = x3::get<parser_context_tag>(context); |
Václav Kubernát | 72749c6 | 2020-01-03 16:47:34 +0100 | [diff] [blame] | 533 | parserContext.resetPath(); |
Václav Kubernát | 5c75b25 | 2018-10-10 18:33:47 +0200 | [diff] [blame] | 534 | parserContext.m_tmpListKeys.clear(); |
| 535 | parserContext.m_tmpListName.clear(); |
Václav Kubernát | 4108e0d | 2018-10-29 13:32:22 +0100 | [diff] [blame] | 536 | parserContext.m_suggestions.clear(); |
Václav Kubernát | 5c75b25 | 2018-10-10 18:33:47 +0200 | [diff] [blame] | 537 | } |
| 538 | }; |
Václav Kubernát | d6fd249 | 2018-11-19 15:11:16 +0100 | [diff] [blame] | 539 | |
| 540 | struct trailingSlash_class; |
Václav Kubernát | 4108e0d | 2018-10-29 13:32:22 +0100 | [diff] [blame] | 541 | |
| 542 | struct createPathSuggestions_class { |
| 543 | template <typename T, typename Iterator, typename Context> |
| 544 | void on_success(Iterator const& begin, Iterator const&, T&, Context const& context) |
| 545 | { |
| 546 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 547 | const auto& schema = parserContext.m_schema; |
| 548 | |
| 549 | parserContext.m_completionIterator = begin; |
Václav Kubernát | 72749c6 | 2020-01-03 16:47:34 +0100 | [diff] [blame] | 550 | auto suggestions = schema.childNodes(parserContext.currentSchemaPath(), Recursion::NonRecursive); |
Václav Kubernát | e69133a | 2019-11-01 19:01:34 +0100 | [diff] [blame] | 551 | std::set<std::string> suffixesAdded; |
| 552 | std::transform(suggestions.begin(), suggestions.end(), |
| 553 | std::inserter(suffixesAdded, suffixesAdded.end()), |
| 554 | [&parserContext, &schema] (auto it) { |
| 555 | ModuleNodePair node; |
| 556 | if (auto colonPos = it.find(":"); colonPos != it.npos) { |
| 557 | node.first = it.substr(0, colonPos); |
| 558 | node.second = it.substr(colonPos + 1, node.second.npos); |
| 559 | } else { |
| 560 | node.first = boost::none; |
| 561 | node.second = it; |
| 562 | } |
| 563 | |
Václav Kubernát | 72749c6 | 2020-01-03 16:47:34 +0100 | [diff] [blame] | 564 | if (schema.isLeaf(parserContext.currentSchemaPath(), node)) { |
Václav Kubernát | e69133a | 2019-11-01 19:01:34 +0100 | [diff] [blame] | 565 | return it + " "; |
| 566 | } |
Václav Kubernát | 72749c6 | 2020-01-03 16:47:34 +0100 | [diff] [blame] | 567 | if (schema.isContainer(parserContext.currentSchemaPath(), node)) { |
Václav Kubernát | e69133a | 2019-11-01 19:01:34 +0100 | [diff] [blame] | 568 | return it + "/"; |
| 569 | } |
Václav Kubernát | 72749c6 | 2020-01-03 16:47:34 +0100 | [diff] [blame] | 570 | if (schema.isList(parserContext.currentSchemaPath(), node)) { |
Václav Kubernát | e69133a | 2019-11-01 19:01:34 +0100 | [diff] [blame] | 571 | return it + "["; |
| 572 | } |
| 573 | return it; |
| 574 | }); |
| 575 | parserContext.m_suggestions = suffixesAdded; |
Václav Kubernát | 4108e0d | 2018-10-29 13:32:22 +0100 | [diff] [blame] | 576 | } |
| 577 | }; |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 578 | |
| 579 | std::set<std::string> generateMissingKeyCompletionSet(std::set<std::string> keysNeeded, std::set<std::string> currentSet); |
| 580 | |
| 581 | struct createKeySuggestions_class { |
| 582 | template <typename T, typename Iterator, typename Context> |
| 583 | void on_success(Iterator const& begin, Iterator const&, T&, Context const& context) |
| 584 | { |
| 585 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 586 | const auto& schema = parserContext.m_schema; |
| 587 | |
| 588 | parserContext.m_completionIterator = begin; |
| 589 | |
Václav Kubernát | 72749c6 | 2020-01-03 16:47:34 +0100 | [diff] [blame] | 590 | 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] | 591 | parserContext.m_suggestions = generateMissingKeyCompletionSet(keysNeeded, parserContext.m_tmpListKeys); |
| 592 | } |
| 593 | }; |
| 594 | |
| 595 | struct suggestKeysEnd_class { |
| 596 | template <typename T, typename Iterator, typename Context> |
| 597 | void on_success(Iterator const& begin, Iterator const&, T&, Context const& context) |
| 598 | { |
| 599 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 600 | const auto& schema = parserContext.m_schema; |
| 601 | |
| 602 | parserContext.m_completionIterator = begin; |
Václav Kubernát | 72749c6 | 2020-01-03 16:47:34 +0100 | [diff] [blame] | 603 | 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] | 604 | if (generateMissingKeyCompletionSet(keysNeeded, parserContext.m_tmpListKeys).empty()) { |
| 605 | parserContext.m_suggestions = {"]/"}; |
| 606 | } else { |
Václav Kubernát | 4c32548 | 2019-04-11 17:51:55 +0200 | [diff] [blame] | 607 | parserContext.m_suggestions = {"]["}; |
Václav Kubernát | 329c6c3 | 2019-02-06 16:41:53 +0100 | [diff] [blame] | 608 | } |
| 609 | } |
| 610 | }; |
Václav Kubernát | 5727242 | 2019-02-08 12:48:24 +0100 | [diff] [blame] | 611 | |
| 612 | struct commandNamesVisitor { |
| 613 | template <typename T> |
| 614 | auto operator()(boost::type<T>) |
| 615 | { |
| 616 | return T::name; |
| 617 | } |
| 618 | }; |
| 619 | |
| 620 | struct createCommandSuggestions_class { |
| 621 | template <typename T, typename Iterator, typename Context> |
| 622 | void on_success(Iterator const& begin, Iterator const&, T&, Context const& context) |
| 623 | { |
| 624 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 625 | parserContext.m_completionIterator = begin; |
| 626 | |
| 627 | parserContext.m_suggestions.clear(); |
| 628 | boost::mpl::for_each<CommandTypes, boost::type<boost::mpl::_>>([&parserContext](auto cmd) { |
| 629 | parserContext.m_suggestions.emplace(commandNamesVisitor()(cmd)); |
| 630 | }); |
| 631 | } |
| 632 | }; |
Václav Kubernát | ac035d6 | 2019-02-18 10:59:08 +0100 | [diff] [blame] | 633 | |
| 634 | struct completing_class { |
| 635 | template <typename T, typename Iterator, typename Context> |
| 636 | void on_success(Iterator const&, Iterator const&, T&, Context const& context) |
| 637 | { |
| 638 | auto& parserContext = x3::get<parser_context_tag>(context); |
| 639 | |
| 640 | if (!parserContext.m_completing) |
| 641 | _pass(context) = false; |
| 642 | } |
| 643 | }; |
Václav Kubernát | 989b5de | 2019-02-20 16:28:35 +0100 | [diff] [blame] | 644 | |
| 645 | struct createEnumSuggestions_class { |
| 646 | template <typename T, typename Iterator, typename Context> |
| 647 | void on_success(Iterator const& begin, Iterator const&, T&, Context const& context) |
| 648 | { |
| 649 | auto& parserContext = x3::get<parser_context_tag>(context); |
Václav Kubernát | 989b5de | 2019-02-20 16:28:35 +0100 | [diff] [blame] | 650 | const Schema& schema = parserContext.m_schema; |
| 651 | |
Václav Kubernát | 7707cae | 2020-01-16 12:04:53 +0100 | [diff] [blame] | 652 | parserContext.m_completionIterator = begin; |
Václav Kubernát | 989b5de | 2019-02-20 16:28:35 +0100 | [diff] [blame] | 653 | |
Václav Kubernát | eeb3884 | 2019-03-20 19:46:05 +0100 | [diff] [blame] | 654 | // Only generate completions if the type is enum so that we don't |
| 655 | // overwrite some other completions. |
Václav Kubernát | 7707cae | 2020-01-16 12:04:53 +0100 | [diff] [blame] | 656 | if (schema.leafType(parserContext.m_tmpListKeyLeafPath.m_location, parserContext.m_tmpListKeyLeafPath.m_node) == yang::LeafDataTypes::Enum) |
| 657 | parserContext.m_suggestions = schema.enumValues(parserContext.m_tmpListKeyLeafPath.m_location, parserContext.m_tmpListKeyLeafPath.m_node); |
Václav Kubernát | eeb3884 | 2019-03-20 19:46:05 +0100 | [diff] [blame] | 658 | } |
| 659 | }; |
| 660 | |
| 661 | // FIXME: can I reuse createEnumSuggestions? |
| 662 | struct createIdentitySuggestions_class { |
| 663 | template <typename T, typename Iterator, typename Context> |
| 664 | void on_success(Iterator const& begin, Iterator const&, T&, Context const& context) |
| 665 | { |
| 666 | auto& parserContext = x3::get<parser_context_tag>(context); |
Václav Kubernát | eeb3884 | 2019-03-20 19:46:05 +0100 | [diff] [blame] | 667 | const Schema& schema = parserContext.m_schema; |
| 668 | |
Václav Kubernát | 7707cae | 2020-01-16 12:04:53 +0100 | [diff] [blame] | 669 | parserContext.m_completionIterator = begin; |
Václav Kubernát | eeb3884 | 2019-03-20 19:46:05 +0100 | [diff] [blame] | 670 | |
| 671 | // Only generate completions if the type is identityref so that we |
| 672 | // don't overwrite some other completions. |
Václav Kubernát | 7707cae | 2020-01-16 12:04:53 +0100 | [diff] [blame] | 673 | if (schema.leafType(parserContext.m_tmpListKeyLeafPath.m_location, parserContext.m_tmpListKeyLeafPath.m_node) == yang::LeafDataTypes::IdentityRef) |
| 674 | parserContext.m_suggestions = schema.validIdentities(parserContext.m_tmpListKeyLeafPath.m_location, parserContext.m_tmpListKeyLeafPath.m_node, Prefixes::WhenNeeded); |
Václav Kubernát | 989b5de | 2019-02-20 16:28:35 +0100 | [diff] [blame] | 675 | } |
| 676 | }; |