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