Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1 | /** |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 2 | * @file parser_common.c |
| 3 | * @author Michal Vasko <mvasko@cesnet.cz> |
| 4 | * @brief libyang common parser functions. |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 5 | * |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 6 | * Copyright (c) 2015 - 2022 CESNET, z.s.p.o. |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 7 | * |
| 8 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 9 | * You may not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
| 11 | * |
| 12 | * https://opensource.org/licenses/BSD-3-Clause |
| 13 | */ |
| 14 | |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 15 | #define _GNU_SOURCE |
| 16 | #define _POSIX_C_SOURCE 200809L /* strdup, strndup */ |
| 17 | |
| 18 | #ifdef __APPLE__ |
| 19 | #define _DARWIN_C_SOURCE /* F_GETPATH */ |
| 20 | #endif |
| 21 | |
| 22 | #if defined (__NetBSD__) || defined (__OpenBSD__) |
| 23 | /* realpath */ |
| 24 | #define _XOPEN_SOURCE 1 |
| 25 | #define _XOPEN_SOURCE_EXTENDED 1 |
| 26 | #endif |
| 27 | |
| 28 | #include "parser_internal.h" |
| 29 | |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 30 | #include <assert.h> |
| 31 | #include <ctype.h> |
| 32 | #include <errno.h> |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 33 | #include <fcntl.h> |
| 34 | #include <limits.h> |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 35 | #include <stdint.h> |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 36 | #include <stdio.h> |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 37 | #include <stdlib.h> |
| 38 | #include <string.h> |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 39 | #include <unistd.h> |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 40 | |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 41 | #include "compat.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 42 | #include "dict.h" |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 43 | #include "in_internal.h" |
Radek Krejci | 47fab89 | 2020-11-05 17:02:41 +0100 | [diff] [blame] | 44 | #include "log.h" |
Michal Vasko | 8f702ee | 2024-02-20 15:44:24 +0100 | [diff] [blame] | 45 | #include "ly_common.h" |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 46 | #include "parser_data.h" |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 47 | #include "path.h" |
Michal Vasko | b475096 | 2022-10-06 15:33:35 +0200 | [diff] [blame] | 48 | #include "plugins_exts/metadata.h" |
Michal Vasko | 820efe8 | 2023-05-12 15:47:43 +0200 | [diff] [blame] | 49 | #include "schema_compile_node.h" |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 50 | #include "schema_features.h" |
Radek Krejci | 7711410 | 2021-03-10 15:21:57 +0100 | [diff] [blame] | 51 | #include "set.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 52 | #include "tree.h" |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 53 | #include "tree_data.h" |
| 54 | #include "tree_data_internal.h" |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 55 | #include "tree_schema.h" |
| 56 | #include "tree_schema_internal.h" |
| 57 | |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 58 | void |
| 59 | lyd_ctx_free(struct lyd_ctx *lydctx) |
| 60 | { |
| 61 | ly_set_erase(&lydctx->node_types, NULL); |
| 62 | ly_set_erase(&lydctx->meta_types, NULL); |
| 63 | ly_set_erase(&lydctx->node_when, NULL); |
Michal Vasko | 135719f | 2022-08-25 12:18:17 +0200 | [diff] [blame] | 64 | ly_set_erase(&lydctx->ext_node, free); |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 65 | ly_set_erase(&lydctx->ext_val, free); |
| 66 | } |
| 67 | |
| 68 | LY_ERR |
Michal Vasko | 820efe8 | 2023-05-12 15:47:43 +0200 | [diff] [blame] | 69 | lyd_parser_notif_eventtime_validate(const struct lyd_node *node) |
| 70 | { |
| 71 | LY_ERR rc = LY_SUCCESS; |
| 72 | struct ly_ctx *ctx = (struct ly_ctx *)LYD_CTX(node); |
| 73 | struct lysc_ctx cctx; |
| 74 | const struct lys_module *mod; |
| 75 | LY_ARRAY_COUNT_TYPE u; |
| 76 | struct ly_err_item *err = NULL; |
| 77 | struct lysp_type *type_p = NULL; |
| 78 | struct lysc_pattern **patterns = NULL; |
| 79 | const char *value; |
| 80 | |
| 81 | LYSC_CTX_INIT_CTX(cctx, ctx); |
| 82 | |
| 83 | /* get date-and-time parsed type */ |
| 84 | mod = ly_ctx_get_module_latest(ctx, "ietf-yang-types"); |
| 85 | assert(mod); |
| 86 | LY_ARRAY_FOR(mod->parsed->typedefs, u) { |
| 87 | if (!strcmp(mod->parsed->typedefs[u].name, "date-and-time")) { |
| 88 | type_p = &mod->parsed->typedefs[u].type; |
| 89 | break; |
| 90 | } |
| 91 | } |
| 92 | assert(type_p); |
| 93 | |
| 94 | /* compile patterns */ |
| 95 | assert(type_p->patterns); |
| 96 | LY_CHECK_GOTO(rc = lys_compile_type_patterns(&cctx, type_p->patterns, NULL, &patterns), cleanup); |
| 97 | |
| 98 | /* validate */ |
| 99 | value = lyd_get_value(node); |
| 100 | rc = lyplg_type_validate_patterns(patterns, value, strlen(value), &err); |
| 101 | |
| 102 | cleanup: |
| 103 | FREE_ARRAY(&cctx.free_ctx, patterns, lysc_pattern_free); |
| 104 | if (rc && err) { |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 105 | ly_err_print(ctx, err); |
Michal Vasko | 820efe8 | 2023-05-12 15:47:43 +0200 | [diff] [blame] | 106 | ly_err_free(err); |
| 107 | LOGVAL(ctx, LYVE_DATA, "Invalid \"eventTime\" in the notification."); |
| 108 | } |
| 109 | return rc; |
| 110 | } |
| 111 | |
| 112 | LY_ERR |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 113 | lyd_parser_find_operation(const struct lyd_node *parent, uint32_t int_opts, struct lyd_node **op) |
| 114 | { |
| 115 | const struct lyd_node *iter; |
| 116 | |
| 117 | *op = NULL; |
| 118 | |
| 119 | if (!parent) { |
| 120 | /* no parent, nothing to look for */ |
| 121 | return LY_SUCCESS; |
| 122 | } |
| 123 | |
| 124 | /* we need to find the operation node if it already exists */ |
| 125 | for (iter = parent; iter; iter = lyd_parent(iter)) { |
| 126 | if (iter->schema && (iter->schema->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF))) { |
| 127 | break; |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | if (!iter) { |
| 132 | /* no operation found */ |
| 133 | return LY_SUCCESS; |
| 134 | } |
| 135 | |
Michal Vasko | e3ed7dc | 2022-11-30 11:39:44 +0100 | [diff] [blame] | 136 | if (!(int_opts & LYD_INTOPT_ANY)) { |
| 137 | if (!(int_opts & (LYD_INTOPT_RPC | LYD_INTOPT_ACTION | LYD_INTOPT_NOTIF | LYD_INTOPT_REPLY))) { |
| 138 | LOGERR(LYD_CTX(parent), LY_EINVAL, "Invalid parent %s \"%s\" node when not parsing any operation.", |
| 139 | lys_nodetype2str(iter->schema->nodetype), iter->schema->name); |
| 140 | return LY_EINVAL; |
| 141 | } else if ((iter->schema->nodetype == LYS_RPC) && !(int_opts & (LYD_INTOPT_RPC | LYD_INTOPT_REPLY))) { |
| 142 | LOGERR(LYD_CTX(parent), LY_EINVAL, "Invalid parent RPC \"%s\" node when not parsing RPC nor rpc-reply.", |
| 143 | iter->schema->name); |
| 144 | return LY_EINVAL; |
| 145 | } else if ((iter->schema->nodetype == LYS_ACTION) && !(int_opts & (LYD_INTOPT_ACTION | LYD_INTOPT_REPLY))) { |
| 146 | LOGERR(LYD_CTX(parent), LY_EINVAL, "Invalid parent action \"%s\" node when not parsing action nor rpc-reply.", |
| 147 | iter->schema->name); |
| 148 | return LY_EINVAL; |
| 149 | } else if ((iter->schema->nodetype == LYS_NOTIF) && !(int_opts & LYD_INTOPT_NOTIF)) { |
| 150 | LOGERR(LYD_CTX(parent), LY_EINVAL, "Invalid parent notification \"%s\" node when not parsing a notification.", |
| 151 | iter->schema->name); |
| 152 | return LY_EINVAL; |
| 153 | } |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | *op = (struct lyd_node *)iter; |
| 157 | return LY_SUCCESS; |
| 158 | } |
| 159 | |
Michal Vasko | a878a89 | 2023-08-18 12:22:07 +0200 | [diff] [blame] | 160 | const struct lysc_node * |
| 161 | lyd_parser_node_schema(const struct lyd_node *node) |
| 162 | { |
| 163 | uint32_t i; |
| 164 | const struct lyd_node *iter; |
| 165 | const struct lysc_node *schema = NULL; |
| 166 | const struct lys_module *mod; |
| 167 | |
| 168 | if (!node) { |
| 169 | return NULL; |
| 170 | } else if (node->schema) { |
| 171 | /* simplest case */ |
| 172 | return node->schema; |
| 173 | } |
| 174 | |
| 175 | /* find the first schema node in the parsed nodes */ |
| 176 | i = ly_log_location_dnode_count(); |
| 177 | if (i) { |
| 178 | do { |
| 179 | --i; |
| 180 | if (ly_log_location_dnode(i)->schema) { |
| 181 | /* this node is processed */ |
| 182 | schema = ly_log_location_dnode(i)->schema; |
| 183 | ++i; |
| 184 | break; |
| 185 | } |
| 186 | } while (i); |
| 187 | } |
| 188 | |
| 189 | /* get schema node of an opaque node */ |
| 190 | do { |
| 191 | /* get next data node */ |
| 192 | if (i == ly_log_location_dnode_count()) { |
| 193 | iter = node; |
| 194 | } else { |
| 195 | iter = ly_log_location_dnode(i); |
| 196 | } |
| 197 | assert(!iter->schema); |
| 198 | |
| 199 | /* get module */ |
Michal Vasko | 420cc25 | 2023-08-24 08:14:24 +0200 | [diff] [blame] | 200 | mod = lyd_node_module(iter); |
Michal Vasko | a878a89 | 2023-08-18 12:22:07 +0200 | [diff] [blame] | 201 | if (!mod) { |
| 202 | /* unknown module, no schema node */ |
| 203 | schema = NULL; |
| 204 | break; |
| 205 | } |
| 206 | |
| 207 | /* get schema node */ |
| 208 | schema = lys_find_child(schema, mod, LYD_NAME(iter), 0, 0, 0); |
| 209 | |
| 210 | /* move to the descendant */ |
| 211 | ++i; |
| 212 | } while (schema && (iter != node)); |
| 213 | |
| 214 | return schema; |
| 215 | } |
| 216 | |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 217 | LY_ERR |
| 218 | lyd_parser_check_schema(struct lyd_ctx *lydctx, const struct lysc_node *snode) |
| 219 | { |
| 220 | LY_ERR rc = LY_SUCCESS; |
| 221 | |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 222 | LOG_LOCSET(snode, NULL); |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 223 | |
| 224 | if (lydctx->int_opts & LYD_INTOPT_ANY) { |
| 225 | /* nothing to check, everything is allowed */ |
| 226 | goto cleanup; |
| 227 | } |
| 228 | |
| 229 | if ((lydctx->parse_opts & LYD_PARSE_NO_STATE) && (snode->flags & LYS_CONFIG_R)) { |
| 230 | LOGVAL(lydctx->data_ctx->ctx, LY_VCODE_UNEXPNODE, "state", snode->name); |
| 231 | rc = LY_EVALID; |
| 232 | goto cleanup; |
| 233 | } |
| 234 | |
| 235 | if (snode->nodetype == LYS_RPC) { |
| 236 | if (lydctx->int_opts & (LYD_INTOPT_RPC | LYD_INTOPT_REPLY)) { |
| 237 | if (lydctx->op_node) { |
| 238 | goto error_node_dup; |
| 239 | } |
| 240 | } else { |
| 241 | goto error_node_inval; |
| 242 | } |
| 243 | } else if (snode->nodetype == LYS_ACTION) { |
| 244 | if (lydctx->int_opts & (LYD_INTOPT_ACTION | LYD_INTOPT_REPLY)) { |
| 245 | if (lydctx->op_node) { |
| 246 | goto error_node_dup; |
| 247 | } |
| 248 | } else { |
| 249 | goto error_node_inval; |
| 250 | } |
| 251 | } else if (snode->nodetype == LYS_NOTIF) { |
| 252 | if (lydctx->int_opts & LYD_INTOPT_NOTIF) { |
| 253 | if (lydctx->op_node) { |
| 254 | goto error_node_dup; |
| 255 | } |
| 256 | } else { |
| 257 | goto error_node_inval; |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | /* success */ |
| 262 | goto cleanup; |
| 263 | |
| 264 | error_node_dup: |
| 265 | LOGVAL(lydctx->data_ctx->ctx, LYVE_DATA, "Unexpected %s element \"%s\", %s \"%s\" already parsed.", |
| 266 | lys_nodetype2str(snode->nodetype), snode->name, lys_nodetype2str(lydctx->op_node->schema->nodetype), |
| 267 | lydctx->op_node->schema->name); |
| 268 | rc = LY_EVALID; |
| 269 | goto cleanup; |
| 270 | |
| 271 | error_node_inval: |
| 272 | LOGVAL(lydctx->data_ctx->ctx, LYVE_DATA, "Unexpected %s element \"%s\".", lys_nodetype2str(snode->nodetype), |
| 273 | snode->name); |
| 274 | rc = LY_EVALID; |
| 275 | |
| 276 | cleanup: |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 277 | LOG_LOCBACK(1, 0); |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 278 | return rc; |
| 279 | } |
| 280 | |
| 281 | LY_ERR |
| 282 | lyd_parser_create_term(struct lyd_ctx *lydctx, const struct lysc_node *schema, const void *value, size_t value_len, |
| 283 | ly_bool *dynamic, LY_VALUE_FORMAT format, void *prefix_data, uint32_t hints, struct lyd_node **node) |
| 284 | { |
Michal Vasko | 9dbb91d | 2023-01-30 13:59:22 +0100 | [diff] [blame] | 285 | LY_ERR r; |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 286 | ly_bool incomplete; |
Michal Vasko | a5c4fa4 | 2024-02-26 14:04:00 +0100 | [diff] [blame] | 287 | ly_bool store_only = (lydctx->parse_opts & LYD_PARSE_STORE_ONLY) == LYD_PARSE_STORE_ONLY ? 1 : 0; |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 288 | |
steweg | d4cde64 | 2024-02-21 08:34:16 +0100 | [diff] [blame] | 289 | if ((r = lyd_create_term(schema, value, value_len, 1, store_only, dynamic, format, prefix_data, |
| 290 | hints, &incomplete, node))) { |
Michal Vasko | 9dbb91d | 2023-01-30 13:59:22 +0100 | [diff] [blame] | 291 | if (lydctx->data_ctx->ctx != schema->module->ctx) { |
| 292 | /* move errors to the main context */ |
| 293 | ly_err_move(schema->module->ctx, (struct ly_ctx *)lydctx->data_ctx->ctx); |
| 294 | } |
| 295 | return r; |
| 296 | } |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 297 | |
| 298 | if (incomplete && !(lydctx->parse_opts & LYD_PARSE_ONLY)) { |
| 299 | LY_CHECK_RET(ly_set_add(&lydctx->node_types, *node, 1, NULL)); |
| 300 | } |
| 301 | return LY_SUCCESS; |
| 302 | } |
| 303 | |
| 304 | LY_ERR |
| 305 | lyd_parser_create_meta(struct lyd_ctx *lydctx, struct lyd_node *parent, struct lyd_meta **meta, const struct lys_module *mod, |
| 306 | const char *name, size_t name_len, const void *value, size_t value_len, ly_bool *dynamic, LY_VALUE_FORMAT format, |
| 307 | void *prefix_data, uint32_t hints, const struct lysc_node *ctx_node) |
| 308 | { |
Michal Vasko | dd03ff1 | 2023-09-11 10:30:48 +0200 | [diff] [blame] | 309 | LY_ERR rc = LY_SUCCESS; |
| 310 | char *dpath = NULL, *path = NULL; |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 311 | ly_bool incomplete; |
| 312 | struct lyd_meta *first = NULL; |
Michal Vasko | a5c4fa4 | 2024-02-26 14:04:00 +0100 | [diff] [blame] | 313 | ly_bool store_only = (lydctx->parse_opts & LYD_PARSE_STORE_ONLY) == LYD_PARSE_STORE_ONLY ? 1 : 0; |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 314 | |
| 315 | if (meta && *meta) { |
| 316 | /* remember the first metadata */ |
| 317 | first = *meta; |
| 318 | } |
| 319 | |
Michal Vasko | dd03ff1 | 2023-09-11 10:30:48 +0200 | [diff] [blame] | 320 | /* generate path to the metadata */ |
| 321 | LY_CHECK_RET(ly_vlog_build_data_path(lydctx->data_ctx->ctx, &dpath)); |
| 322 | if (asprintf(&path, "%s/@%s:%.*s", dpath, mod->name, (int)name_len, name) == -1) { |
| 323 | LOGMEM(lydctx->data_ctx->ctx); |
| 324 | rc = LY_EMEM; |
| 325 | goto cleanup; |
| 326 | } |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 327 | ly_log_location(NULL, NULL, path, NULL); |
Michal Vasko | dd03ff1 | 2023-09-11 10:30:48 +0200 | [diff] [blame] | 328 | |
steweg | d4cde64 | 2024-02-21 08:34:16 +0100 | [diff] [blame] | 329 | LY_CHECK_GOTO(rc = lyd_create_meta(parent, meta, mod, name, name_len, value, value_len, 1, store_only, dynamic, format, |
Michal Vasko | 989cdb4 | 2023-10-06 15:32:37 +0200 | [diff] [blame] | 330 | prefix_data, hints, ctx_node, 0, &incomplete), cleanup); |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 331 | |
| 332 | if (incomplete && !(lydctx->parse_opts & LYD_PARSE_ONLY)) { |
Michal Vasko | dd03ff1 | 2023-09-11 10:30:48 +0200 | [diff] [blame] | 333 | LY_CHECK_GOTO(rc = ly_set_add(&lydctx->meta_types, *meta, 1, NULL), cleanup); |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | if (first) { |
| 337 | /* always return the first metadata */ |
| 338 | *meta = first; |
| 339 | } |
| 340 | |
Michal Vasko | dd03ff1 | 2023-09-11 10:30:48 +0200 | [diff] [blame] | 341 | cleanup: |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 342 | ly_log_location_revert(0, 0, 1, 0); |
Michal Vasko | dd03ff1 | 2023-09-11 10:30:48 +0200 | [diff] [blame] | 343 | free(dpath); |
| 344 | free(path); |
| 345 | return rc; |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 346 | } |
| 347 | |
Michal Vasko | 95c6d5c | 2022-05-20 10:33:39 +0200 | [diff] [blame] | 348 | LY_ERR |
| 349 | lyd_parse_check_keys(struct lyd_node *node) |
| 350 | { |
| 351 | const struct lysc_node *skey = NULL; |
| 352 | const struct lyd_node *key; |
| 353 | |
| 354 | assert(node->schema->nodetype == LYS_LIST); |
| 355 | |
| 356 | key = lyd_child(node); |
| 357 | while ((skey = lys_getnext(skey, node->schema, NULL, 0)) && (skey->flags & LYS_KEY)) { |
| 358 | if (!key || (key->schema != skey)) { |
| 359 | LOGVAL(LYD_CTX(node), LY_VCODE_NOKEY, skey->name); |
| 360 | return LY_EVALID; |
| 361 | } |
| 362 | |
| 363 | key = key->next; |
| 364 | } |
| 365 | |
| 366 | return LY_SUCCESS; |
| 367 | } |
| 368 | |
| 369 | LY_ERR |
| 370 | lyd_parse_set_data_flags(struct lyd_node *node, struct lyd_meta **meta, struct lyd_ctx *lydctx, |
| 371 | struct lysc_ext_instance *ext) |
| 372 | { |
aPiecek | ed39cbe | 2023-11-06 14:20:27 +0100 | [diff] [blame] | 373 | struct lyd_meta *meta2, *prev_meta = NULL, *next_meta = NULL; |
Michal Vasko | 95c6d5c | 2022-05-20 10:33:39 +0200 | [diff] [blame] | 374 | struct lyd_ctx_ext_val *ext_val; |
| 375 | |
Michal Vasko | bc9f76f | 2022-12-01 10:31:38 +0100 | [diff] [blame] | 376 | if (lydctx->parse_opts & LYD_PARSE_NO_NEW) { |
| 377 | node->flags &= ~LYD_NEW; |
| 378 | } |
| 379 | |
Michal Vasko | 95c6d5c | 2022-05-20 10:33:39 +0200 | [diff] [blame] | 380 | if (lysc_has_when(node->schema)) { |
Michal Vasko | fbb48c2 | 2022-11-30 13:28:03 +0100 | [diff] [blame] | 381 | if (lydctx->parse_opts & LYD_PARSE_WHEN_TRUE) { |
| 382 | /* the condition was true before */ |
| 383 | node->flags |= LYD_WHEN_TRUE; |
| 384 | } |
Michal Vasko | 95c6d5c | 2022-05-20 10:33:39 +0200 | [diff] [blame] | 385 | if (!(lydctx->parse_opts & LYD_PARSE_ONLY)) { |
| 386 | /* remember we need to evaluate this node's when */ |
| 387 | LY_CHECK_RET(ly_set_add(&lydctx->node_when, node, 1, NULL)); |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | LY_LIST_FOR(*meta, meta2) { |
| 392 | if (!strcmp(meta2->name, "default") && !strcmp(meta2->annotation->module->name, "ietf-netconf-with-defaults") && |
| 393 | meta2->value.boolean) { |
| 394 | /* node is default according to the metadata */ |
| 395 | node->flags |= LYD_DEFAULT; |
| 396 | |
aPiecek | ed39cbe | 2023-11-06 14:20:27 +0100 | [diff] [blame] | 397 | next_meta = meta2->next; |
| 398 | |
Michal Vasko | 95c6d5c | 2022-05-20 10:33:39 +0200 | [diff] [blame] | 399 | /* delete the metadata */ |
aPiecek | ed39cbe | 2023-11-06 14:20:27 +0100 | [diff] [blame] | 400 | if (meta != &node->meta) { |
Michal Vasko | 95c6d5c | 2022-05-20 10:33:39 +0200 | [diff] [blame] | 401 | *meta = (*meta)->next; |
| 402 | } |
| 403 | lyd_free_meta_single(meta2); |
aPiecek | ed39cbe | 2023-11-06 14:20:27 +0100 | [diff] [blame] | 404 | if (prev_meta) { |
| 405 | prev_meta->next = next_meta; |
| 406 | } |
Michal Vasko | 4754d4a | 2022-12-01 10:11:21 +0100 | [diff] [blame] | 407 | |
| 408 | /* update dflt flag for all parent NP containers */ |
| 409 | lyd_cont_set_dflt(lyd_parent(node)); |
Michal Vasko | 95c6d5c | 2022-05-20 10:33:39 +0200 | [diff] [blame] | 410 | break; |
| 411 | } |
| 412 | |
| 413 | prev_meta = meta2; |
| 414 | } |
| 415 | |
| 416 | if (ext) { |
| 417 | /* parsed for an extension */ |
| 418 | node->flags |= LYD_EXT; |
| 419 | |
| 420 | if (!(lydctx->parse_opts & LYD_PARSE_ONLY)) { |
| 421 | /* rememeber for validation */ |
| 422 | ext_val = malloc(sizeof *ext_val); |
| 423 | LY_CHECK_ERR_RET(!ext_val, LOGMEM(LYD_CTX(node)), LY_EMEM); |
| 424 | ext_val->ext = ext; |
| 425 | ext_val->sibling = node; |
| 426 | LY_CHECK_RET(ly_set_add(&lydctx->ext_val, ext_val, 1, NULL)); |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | return LY_SUCCESS; |
| 431 | } |
| 432 | |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 433 | void |
| 434 | lys_parser_fill_filepath(struct ly_ctx *ctx, struct ly_in *in, const char **filepath) |
| 435 | { |
| 436 | char path[PATH_MAX]; |
| 437 | |
| 438 | #ifndef __APPLE__ |
| 439 | char proc_path[32]; |
| 440 | int len; |
| 441 | #endif |
| 442 | |
| 443 | LY_CHECK_ARG_RET(NULL, ctx, in, filepath, ); |
| 444 | if (*filepath) { |
| 445 | /* filepath already set */ |
| 446 | return; |
| 447 | } |
| 448 | |
| 449 | switch (in->type) { |
| 450 | case LY_IN_FILEPATH: |
| 451 | if (realpath(in->method.fpath.filepath, path) != NULL) { |
| 452 | lydict_insert(ctx, path, 0, filepath); |
| 453 | } else { |
| 454 | lydict_insert(ctx, in->method.fpath.filepath, 0, filepath); |
| 455 | } |
| 456 | |
| 457 | break; |
| 458 | case LY_IN_FD: |
| 459 | #ifdef __APPLE__ |
| 460 | if (fcntl(in->method.fd, F_GETPATH, path) != -1) { |
| 461 | lydict_insert(ctx, path, 0, filepath); |
| 462 | } |
| 463 | #elif defined _WIN32 |
| 464 | HANDLE h = _get_osfhandle(in->method.fd); |
| 465 | FILE_NAME_INFO info; |
Michal Vasko | 2bf4af4 | 2023-01-04 12:08:38 +0100 | [diff] [blame] | 466 | |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 467 | if (GetFileInformationByHandleEx(h, FileNameInfo, &info, sizeof info)) { |
| 468 | char *buf = calloc(info.FileNameLength + 1 /* trailing NULL */, MB_CUR_MAX); |
Michal Vasko | 2bf4af4 | 2023-01-04 12:08:38 +0100 | [diff] [blame] | 469 | |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 470 | len = wcstombs(buf, info.FileName, info.FileNameLength * MB_CUR_MAX); |
| 471 | lydict_insert(ctx, buf, len, filepath); |
| 472 | } |
| 473 | #else |
| 474 | /* get URI if there is /proc */ |
| 475 | sprintf(proc_path, "/proc/self/fd/%d", in->method.fd); |
| 476 | if ((len = readlink(proc_path, path, PATH_MAX - 1)) > 0) { |
| 477 | lydict_insert(ctx, path, len, filepath); |
| 478 | } |
| 479 | #endif |
| 480 | break; |
| 481 | case LY_IN_MEMORY: |
| 482 | case LY_IN_FILE: |
| 483 | /* nothing to do */ |
| 484 | break; |
| 485 | default: |
| 486 | LOGINT(ctx); |
| 487 | break; |
| 488 | } |
| 489 | } |
| 490 | |
Michal Vasko | d0625d7 | 2022-10-06 15:02:50 +0200 | [diff] [blame] | 491 | static LY_ERR lysp_stmt_container(struct lysp_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_node *parent, |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 492 | struct lysp_node **siblings); |
Michal Vasko | d0625d7 | 2022-10-06 15:02:50 +0200 | [diff] [blame] | 493 | static LY_ERR lysp_stmt_choice(struct lysp_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_node *parent, |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 494 | struct lysp_node **siblings); |
Michal Vasko | d0625d7 | 2022-10-06 15:02:50 +0200 | [diff] [blame] | 495 | static LY_ERR lysp_stmt_case(struct lysp_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_node *parent, |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 496 | struct lysp_node **siblings); |
Michal Vasko | d0625d7 | 2022-10-06 15:02:50 +0200 | [diff] [blame] | 497 | static LY_ERR lysp_stmt_uses(struct lysp_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_node *parent, |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 498 | struct lysp_node **siblings); |
Michal Vasko | d0625d7 | 2022-10-06 15:02:50 +0200 | [diff] [blame] | 499 | static LY_ERR lysp_stmt_grouping(struct lysp_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_node *parent, |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 500 | struct lysp_node_grp **groupings); |
Michal Vasko | d0625d7 | 2022-10-06 15:02:50 +0200 | [diff] [blame] | 501 | static LY_ERR lysp_stmt_list(struct lysp_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_node *parent, |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 502 | struct lysp_node **siblings); |
| 503 | |
Michal Vasko | 5d10b9b | 2022-12-14 12:14:20 +0100 | [diff] [blame] | 504 | /** |
| 505 | * @brief Validate stmt string value. |
| 506 | * |
| 507 | * @param[in] ctx Parser context. |
| 508 | * @param[in] val_type String value type. |
| 509 | * @param[in] val Value to validate. |
| 510 | * @return LY_ERR value. |
| 511 | */ |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 512 | static LY_ERR |
Michal Vasko | d0625d7 | 2022-10-06 15:02:50 +0200 | [diff] [blame] | 513 | lysp_stmt_validate_value(struct lysp_ctx *ctx, enum yang_arg val_type, const char *val) |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 514 | { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 515 | uint8_t prefix = 0; |
| 516 | ly_bool first = 1; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 517 | uint32_t c; |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 518 | size_t utf8_char_len; |
| 519 | |
Michal Vasko | cfa1a96 | 2023-02-27 09:28:45 +0100 | [diff] [blame] | 520 | if (!val) { |
| 521 | if (val_type == Y_MAYBE_STR_ARG) { |
| 522 | /* fine */ |
| 523 | return LY_SUCCESS; |
| 524 | } |
| 525 | |
| 526 | LOGVAL_PARSER(ctx, LYVE_SYNTAX, "Missing an expected string."); |
| 527 | return LY_EVALID; |
| 528 | } |
| 529 | |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 530 | while (*val) { |
| 531 | LY_CHECK_ERR_RET(ly_getutf8(&val, &c, &utf8_char_len), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 532 | LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, (val)[-utf8_char_len]), LY_EVALID); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 533 | |
| 534 | switch (val_type) { |
| 535 | case Y_IDENTIF_ARG: |
| 536 | LY_CHECK_RET(lysp_check_identifierchar(ctx, c, first, NULL)); |
| 537 | break; |
| 538 | case Y_PREF_IDENTIF_ARG: |
| 539 | LY_CHECK_RET(lysp_check_identifierchar(ctx, c, first, &prefix)); |
| 540 | break; |
| 541 | case Y_STR_ARG: |
| 542 | case Y_MAYBE_STR_ARG: |
| 543 | LY_CHECK_RET(lysp_check_stringchar(ctx, c)); |
| 544 | break; |
| 545 | } |
| 546 | first = 0; |
| 547 | } |
| 548 | |
| 549 | return LY_SUCCESS; |
| 550 | } |
| 551 | |
| 552 | /** |
Michal Vasko | 5d10b9b | 2022-12-14 12:14:20 +0100 | [diff] [blame] | 553 | * @brief Duplicate statement siblings, recursively. |
| 554 | * |
| 555 | * @param[in] ctx Parser context. |
| 556 | * @param[in] stmt Statement to duplicate. |
| 557 | * @param[out] first First duplicated statement, the rest follow. |
| 558 | * @return LY_ERR value. |
| 559 | */ |
| 560 | static LY_ERR |
| 561 | lysp_stmt_dup(struct lysp_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_stmt **first) |
| 562 | { |
| 563 | struct lysp_stmt *child, *last = NULL; |
| 564 | |
| 565 | LY_LIST_FOR(stmt, stmt) { |
| 566 | child = calloc(1, sizeof *child); |
| 567 | LY_CHECK_ERR_RET(!child, LOGMEM(PARSER_CTX(ctx)), LY_EMEM); |
| 568 | |
| 569 | if (last) { |
| 570 | last->next = child; |
| 571 | } else { |
| 572 | assert(!*first); |
| 573 | *first = child; |
| 574 | } |
| 575 | last = child; |
| 576 | |
| 577 | LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), stmt->stmt, 0, &child->stmt)); |
| 578 | LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), stmt->arg, 0, &child->arg)); |
| 579 | child->format = stmt->format; |
| 580 | LY_CHECK_RET(ly_dup_prefix_data(PARSER_CTX(ctx), stmt->format, stmt->prefix_data, &child->prefix_data)); |
| 581 | child->flags = stmt->flags; |
| 582 | child->kw = stmt->kw; |
| 583 | |
| 584 | /* recursively */ |
| 585 | LY_CHECK_RET(lysp_stmt_dup(ctx, stmt->child, &child->child)); |
| 586 | } |
| 587 | |
| 588 | return LY_SUCCESS; |
| 589 | } |
| 590 | |
| 591 | /** |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 592 | * @brief Parse extension instance. |
| 593 | * |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 594 | * @param[in] ctx parser context. |
| 595 | * @param[in] stmt Source statement data from the parsed extension instance. |
Radek Krejci | fc596f9 | 2021-02-26 22:40:26 +0100 | [diff] [blame] | 596 | * @param[in] insubstmt The statement this extension instance is a substatement of. |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 597 | * @param[in] insubstmt_index Index of the keyword instance this extension instance is a substatement of. |
| 598 | * @param[in,out] exts Extension instances to add to. |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 599 | * @return LY_ERR values. |
| 600 | */ |
| 601 | static LY_ERR |
Michal Vasko | d0625d7 | 2022-10-06 15:02:50 +0200 | [diff] [blame] | 602 | lysp_stmt_ext(struct lysp_ctx *ctx, const struct lysp_stmt *stmt, enum ly_stmt insubstmt, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 603 | LY_ARRAY_COUNT_TYPE insubstmt_index, struct lysp_ext_instance **exts) |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 604 | { |
| 605 | struct lysp_ext_instance *e; |
| 606 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 607 | LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *exts, e, LY_EMEM); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 608 | |
| 609 | /* store name and insubstmt info */ |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 610 | LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), stmt->stmt, 0, &e->name)); |
Radek Krejci | ab43086 | 2021-03-02 20:13:40 +0100 | [diff] [blame] | 611 | e->parent_stmt = insubstmt; |
| 612 | e->parent_stmt_index = insubstmt_index; |
aPiecek | 60d9d67 | 2021-04-27 15:49:57 +0200 | [diff] [blame] | 613 | e->parsed = NULL; |
Michal Vasko | 5d10b9b | 2022-12-14 12:14:20 +0100 | [diff] [blame] | 614 | LY_CHECK_RET(lysp_stmt_dup(ctx, stmt->child, &e->child)); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 615 | |
| 616 | /* get optional argument */ |
| 617 | if (stmt->arg) { |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 618 | LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), stmt->arg, 0, &e->argument)); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 619 | } |
| 620 | |
| 621 | return LY_SUCCESS; |
| 622 | } |
| 623 | |
| 624 | /** |
| 625 | * @brief Parse a generic text field without specific constraints. Those are contact, organization, |
| 626 | * description, etc... |
| 627 | * |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 628 | * @param[in] ctx parser context. |
| 629 | * @param[in] stmt Source statement data from the parsed extension instance. |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 630 | * @param[in] substmt_index Index of this substatement. |
| 631 | * @param[in,out] value Place to store the parsed value. |
| 632 | * @param[in] arg Type of the YANG keyword argument (of the value). |
| 633 | * @param[in,out] exts Extension instances to add to. |
| 634 | * |
| 635 | * @return LY_ERR values. |
| 636 | */ |
| 637 | static LY_ERR |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 638 | lysp_stmt_text_field(struct lysp_ctx *ctx, const struct lysp_stmt *stmt, uint32_t substmt_index, const char **value, |
| 639 | enum yang_arg arg, struct lysp_ext_instance **exts) |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 640 | { |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 641 | if (*value) { |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 642 | LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, lyplg_ext_stmt2str(stmt->kw)); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 643 | return LY_EVALID; |
| 644 | } |
| 645 | |
| 646 | LY_CHECK_RET(lysp_stmt_validate_value(ctx, arg, stmt->arg)); |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 647 | LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), stmt->arg, 0, value)); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 648 | |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 649 | for (const struct lysp_stmt *child = stmt->child; child; child = child->next) { |
| 650 | switch (child->kw) { |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 651 | case LY_STMT_EXTENSION_INSTANCE: |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 652 | LY_CHECK_RET(lysp_stmt_ext(ctx, child, stmt->kw, substmt_index, exts)); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 653 | break; |
| 654 | default: |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 655 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(child->kw), lyplg_ext_stmt2str(stmt->kw)); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 656 | return LY_EVALID; |
| 657 | } |
| 658 | } |
| 659 | return LY_SUCCESS; |
| 660 | } |
| 661 | |
| 662 | /** |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 663 | * @brief Parse a qname that can have more instances such as if-feature. |
| 664 | * |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 665 | * @param[in] ctx parser context. |
| 666 | * @param[in] stmt Source statement data from the parsed extension instance. |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 667 | * @param[in,out] qnames Parsed qnames to add to. |
| 668 | * @param[in] arg Type of the expected argument. |
| 669 | * @param[in,out] exts Extension instances to add to. |
| 670 | * |
| 671 | * @return LY_ERR values. |
| 672 | */ |
| 673 | static LY_ERR |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 674 | lysp_stmt_qnames(struct lysp_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_qname **qnames, enum yang_arg arg, |
| 675 | struct lysp_ext_instance **exts) |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 676 | { |
| 677 | struct lysp_qname *item; |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 678 | |
| 679 | LY_CHECK_RET(lysp_stmt_validate_value(ctx, arg, stmt->arg)); |
| 680 | |
| 681 | /* allocate new pointer */ |
| 682 | LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *qnames, item, LY_EMEM); |
| 683 | LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), stmt->arg, 0, &item->str)); |
Michal Vasko | 8a67eff | 2021-12-07 14:04:47 +0100 | [diff] [blame] | 684 | item->mod = PARSER_CUR_PMOD(ctx); |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 685 | |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 686 | for (const struct lysp_stmt *child = stmt->child; child; child = child->next) { |
| 687 | switch (child->kw) { |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 688 | case LY_STMT_EXTENSION_INSTANCE: |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 689 | LY_CHECK_RET(lysp_stmt_ext(ctx, child, stmt->kw, LY_ARRAY_COUNT(*qnames) - 1, exts)); |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 690 | break; |
| 691 | default: |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 692 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(child->kw), lyplg_ext_stmt2str(stmt->kw)); |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 693 | return LY_EVALID; |
| 694 | } |
| 695 | } |
| 696 | return LY_SUCCESS; |
| 697 | } |
| 698 | |
| 699 | /** |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 700 | * @brief Parse a generic text field that can have more instances such as base. |
| 701 | * |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 702 | * @param[in] ctx parser context. |
| 703 | * @param[in] stmt Source statement data from the parsed extension instance. |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 704 | * @param[in,out] texts Parsed values to add to. |
| 705 | * @param[in] arg Type of the expected argument. |
| 706 | * @param[in,out] exts Extension instances to add to. |
| 707 | * |
| 708 | * @return LY_ERR values. |
| 709 | */ |
| 710 | static LY_ERR |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 711 | lysp_stmt_text_fields(struct lysp_ctx *ctx, const struct lysp_stmt *stmt, const char ***texts, enum yang_arg arg, |
| 712 | struct lysp_ext_instance **exts) |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 713 | { |
| 714 | const char **item; |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 715 | |
| 716 | LY_CHECK_RET(lysp_stmt_validate_value(ctx, arg, stmt->arg)); |
| 717 | |
| 718 | /* allocate new pointer */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 719 | LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *texts, item, LY_EMEM); |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 720 | LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), stmt->arg, 0, item)); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 721 | |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 722 | for (const struct lysp_stmt *child = stmt->child; child; child = child->next) { |
| 723 | switch (child->kw) { |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 724 | case LY_STMT_EXTENSION_INSTANCE: |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 725 | LY_CHECK_RET(lysp_stmt_ext(ctx, child, stmt->kw, LY_ARRAY_COUNT(*texts) - 1, exts)); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 726 | break; |
| 727 | default: |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 728 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(child->kw), lyplg_ext_stmt2str(stmt->kw)); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 729 | return LY_EVALID; |
| 730 | } |
| 731 | } |
| 732 | return LY_SUCCESS; |
| 733 | } |
| 734 | |
| 735 | /** |
| 736 | * @brief Parse the status statement. |
| 737 | * |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 738 | * @param[in] ctx parser context. |
| 739 | * @param[in] stmt Source statement data from the parsed extension instance. |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 740 | * @param[in,out] flags Flags to add to. |
| 741 | * @param[in,out] exts Extension instances to add to. |
| 742 | * |
| 743 | * @return LY_ERR values. |
| 744 | */ |
| 745 | static LY_ERR |
Michal Vasko | d0625d7 | 2022-10-06 15:02:50 +0200 | [diff] [blame] | 746 | lysp_stmt_status(struct lysp_ctx *ctx, const struct lysp_stmt *stmt, uint16_t *flags, struct lysp_ext_instance **exts) |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 747 | { |
Michal Vasko | 7b3a00e | 2023-08-09 11:58:03 +0200 | [diff] [blame] | 748 | int arg_len; |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 749 | |
| 750 | if (*flags & LYS_STATUS_MASK) { |
| 751 | LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "status"); |
| 752 | return LY_EVALID; |
| 753 | } |
| 754 | |
| 755 | LY_CHECK_RET(lysp_stmt_validate_value(ctx, Y_STR_ARG, stmt->arg)); |
| 756 | arg_len = strlen(stmt->arg); |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 757 | if ((arg_len == ly_strlen_const("current")) && !strncmp(stmt->arg, "current", arg_len)) { |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 758 | *flags |= LYS_STATUS_CURR; |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 759 | } else if ((arg_len == ly_strlen_const("deprecated")) && !strncmp(stmt->arg, "deprecated", arg_len)) { |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 760 | *flags |= LYS_STATUS_DEPRC; |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 761 | } else if ((arg_len == ly_strlen_const("obsolete")) && !strncmp(stmt->arg, "obsolete", arg_len)) { |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 762 | *flags |= LYS_STATUS_OBSLT; |
| 763 | } else { |
| 764 | LOGVAL_PARSER(ctx, LY_VCODE_INVAL, arg_len, stmt->arg, "status"); |
| 765 | return LY_EVALID; |
| 766 | } |
| 767 | |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 768 | for (const struct lysp_stmt *child = stmt->child; child; child = child->next) { |
| 769 | switch (child->kw) { |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 770 | case LY_STMT_EXTENSION_INSTANCE: |
Radek Krejci | fc596f9 | 2021-02-26 22:40:26 +0100 | [diff] [blame] | 771 | LY_CHECK_RET(lysp_stmt_ext(ctx, child, LY_STMT_STATUS, 0, exts)); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 772 | break; |
| 773 | default: |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 774 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(child->kw), "status"); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 775 | return LY_EVALID; |
| 776 | } |
| 777 | } |
| 778 | return LY_SUCCESS; |
| 779 | } |
| 780 | |
| 781 | /** |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 782 | * @brief Parse the when statement. |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 783 | * |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 784 | * @param[in] ctx parser context. |
| 785 | * @param[in] stmt Source statement data from the parsed extension instance. |
| 786 | * @param[in,out] when_p When pointer to parse to. |
| 787 | * |
| 788 | * @return LY_ERR values. |
| 789 | */ |
| 790 | static LY_ERR |
Michal Vasko | d0625d7 | 2022-10-06 15:02:50 +0200 | [diff] [blame] | 791 | lysp_stmt_when(struct lysp_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_when **when_p) |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 792 | { |
| 793 | LY_ERR ret = LY_SUCCESS; |
| 794 | struct lysp_when *when; |
| 795 | |
| 796 | if (*when_p) { |
| 797 | LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "when"); |
| 798 | return LY_EVALID; |
| 799 | } |
| 800 | |
| 801 | LY_CHECK_RET(lysp_stmt_validate_value(ctx, Y_STR_ARG, stmt->arg)); |
| 802 | |
| 803 | when = calloc(1, sizeof *when); |
| 804 | LY_CHECK_ERR_RET(!when, LOGMEM(PARSER_CTX(ctx)), LY_EMEM); |
| 805 | *when_p = when; |
| 806 | |
| 807 | LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), stmt->arg, 0, &when->cond)); |
| 808 | |
| 809 | for (const struct lysp_stmt *child = stmt->child; child; child = child->next) { |
| 810 | switch (child->kw) { |
| 811 | case LY_STMT_DESCRIPTION: |
| 812 | LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &when->dsc, Y_STR_ARG, &when->exts)); |
| 813 | break; |
| 814 | case LY_STMT_REFERENCE: |
| 815 | LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &when->ref, Y_STR_ARG, &when->exts)); |
| 816 | break; |
| 817 | case LY_STMT_EXTENSION_INSTANCE: |
Radek Krejci | 39b7fc2 | 2021-02-26 23:29:18 +0100 | [diff] [blame] | 818 | LY_CHECK_RET(lysp_stmt_ext(ctx, child, LY_STMT_WHEN, 0, &when->exts)); |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 819 | break; |
| 820 | default: |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 821 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(child->kw), "when"); |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 822 | return LY_EVALID; |
| 823 | } |
| 824 | } |
| 825 | return ret; |
| 826 | } |
| 827 | |
| 828 | /** |
| 829 | * @brief Parse the config statement. |
| 830 | * |
| 831 | * @param[in] ctx parser context. |
| 832 | * @param[in] stmt Source statement data from the parsed extension instance. |
| 833 | * @param[in,out] flags Flags to add to. |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 834 | * @param[in,out] exts Extension instances to add to. |
| 835 | * |
| 836 | * @return LY_ERR values. |
| 837 | */ |
| 838 | static LY_ERR |
Michal Vasko | d0625d7 | 2022-10-06 15:02:50 +0200 | [diff] [blame] | 839 | lysp_stmt_config(struct lysp_ctx *ctx, const struct lysp_stmt *stmt, uint16_t *flags, struct lysp_ext_instance **exts) |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 840 | { |
Michal Vasko | 7b3a00e | 2023-08-09 11:58:03 +0200 | [diff] [blame] | 841 | int arg_len; |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 842 | |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 843 | if (*flags & LYS_CONFIG_MASK) { |
| 844 | LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "config"); |
| 845 | return LY_EVALID; |
| 846 | } |
| 847 | |
| 848 | LY_CHECK_RET(lysp_stmt_validate_value(ctx, Y_STR_ARG, stmt->arg)); |
| 849 | arg_len = strlen(stmt->arg); |
| 850 | if ((arg_len == ly_strlen_const("true")) && !strncmp(stmt->arg, "true", arg_len)) { |
| 851 | *flags |= LYS_CONFIG_W; |
| 852 | } else if ((arg_len == ly_strlen_const("false")) && !strncmp(stmt->arg, "false", arg_len)) { |
| 853 | *flags |= LYS_CONFIG_R; |
| 854 | } else { |
| 855 | LOGVAL_PARSER(ctx, LY_VCODE_INVAL, arg_len, stmt->arg, "config"); |
| 856 | return LY_EVALID; |
| 857 | } |
| 858 | |
| 859 | for (const struct lysp_stmt *child = stmt->child; child; child = child->next) { |
| 860 | switch (child->kw) { |
| 861 | case LY_STMT_EXTENSION_INSTANCE: |
Radek Krejci | fc596f9 | 2021-02-26 22:40:26 +0100 | [diff] [blame] | 862 | LY_CHECK_RET(lysp_stmt_ext(ctx, child, LY_STMT_CONFIG, 0, exts)); |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 863 | break; |
| 864 | default: |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 865 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(child->kw), "config"); |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 866 | return LY_EVALID; |
| 867 | } |
| 868 | } |
| 869 | |
| 870 | return LY_SUCCESS; |
| 871 | } |
| 872 | |
| 873 | /** |
| 874 | * @brief Parse the mandatory statement. |
| 875 | * |
| 876 | * @param[in] ctx parser context. |
| 877 | * @param[in] stmt Source statement data from the parsed extension instance. |
| 878 | * @param[in,out] flags Flags to add to. |
| 879 | * @param[in,out] exts Extension instances to add to. |
| 880 | * |
| 881 | * @return LY_ERR values. |
| 882 | */ |
| 883 | static LY_ERR |
Michal Vasko | d0625d7 | 2022-10-06 15:02:50 +0200 | [diff] [blame] | 884 | lysp_stmt_mandatory(struct lysp_ctx *ctx, const struct lysp_stmt *stmt, uint16_t *flags, |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 885 | struct lysp_ext_instance **exts) |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 886 | { |
Michal Vasko | 7b3a00e | 2023-08-09 11:58:03 +0200 | [diff] [blame] | 887 | int arg_len; |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 888 | |
| 889 | if (*flags & LYS_MAND_MASK) { |
| 890 | LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "mandatory"); |
| 891 | return LY_EVALID; |
| 892 | } |
| 893 | |
| 894 | LY_CHECK_RET(lysp_stmt_validate_value(ctx, Y_STR_ARG, stmt->arg)); |
| 895 | arg_len = strlen(stmt->arg); |
| 896 | if ((arg_len == ly_strlen_const("true")) && !strncmp(stmt->arg, "true", arg_len)) { |
| 897 | *flags |= LYS_MAND_TRUE; |
| 898 | } else if ((arg_len == ly_strlen_const("false")) && !strncmp(stmt->arg, "false", arg_len)) { |
| 899 | *flags |= LYS_MAND_FALSE; |
| 900 | } else { |
| 901 | LOGVAL_PARSER(ctx, LY_VCODE_INVAL, arg_len, stmt->arg, "mandatory"); |
| 902 | return LY_EVALID; |
| 903 | } |
| 904 | |
| 905 | for (const struct lysp_stmt *child = stmt->child; child; child = child->next) { |
| 906 | switch (child->kw) { |
| 907 | case LY_STMT_EXTENSION_INSTANCE: |
Radek Krejci | fc596f9 | 2021-02-26 22:40:26 +0100 | [diff] [blame] | 908 | LY_CHECK_RET(lysp_stmt_ext(ctx, child, LY_STMT_MANDATORY, 0, exts)); |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 909 | break; |
| 910 | default: |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 911 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(child->kw), "mandatory"); |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 912 | return LY_EVALID; |
| 913 | } |
| 914 | } |
| 915 | |
| 916 | return LY_SUCCESS; |
| 917 | } |
| 918 | |
| 919 | /** |
| 920 | * @brief Parse a restriction such as range or length. |
| 921 | * |
| 922 | * @param[in] ctx parser context. |
| 923 | * @param[in] stmt Source statement data from the parsed extension instance. |
| 924 | * @param[in,out] exts Extension instances to add to. |
| 925 | * |
| 926 | * @return LY_ERR values. |
| 927 | */ |
| 928 | static LY_ERR |
Michal Vasko | d0625d7 | 2022-10-06 15:02:50 +0200 | [diff] [blame] | 929 | lysp_stmt_restr(struct lysp_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_restr *restr) |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 930 | { |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 931 | LY_CHECK_RET(lysp_stmt_validate_value(ctx, Y_STR_ARG, stmt->arg)); |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 932 | LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), stmt->arg, 0, &restr->arg.str)); |
Michal Vasko | 8a67eff | 2021-12-07 14:04:47 +0100 | [diff] [blame] | 933 | restr->arg.mod = PARSER_CUR_PMOD(ctx); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 934 | |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 935 | for (const struct lysp_stmt *child = stmt->child; child; child = child->next) { |
| 936 | switch (child->kw) { |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 937 | case LY_STMT_DESCRIPTION: |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 938 | LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &restr->dsc, Y_STR_ARG, &restr->exts)); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 939 | break; |
| 940 | case LY_STMT_REFERENCE: |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 941 | LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &restr->ref, Y_STR_ARG, &restr->exts)); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 942 | break; |
| 943 | case LY_STMT_ERROR_APP_TAG: |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 944 | LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &restr->eapptag, Y_STR_ARG, &restr->exts)); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 945 | break; |
| 946 | case LY_STMT_ERROR_MESSAGE: |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 947 | LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &restr->emsg, Y_STR_ARG, &restr->exts)); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 948 | break; |
| 949 | case LY_STMT_EXTENSION_INSTANCE: |
Radek Krejci | 39b7fc2 | 2021-02-26 23:29:18 +0100 | [diff] [blame] | 950 | LY_CHECK_RET(lysp_stmt_ext(ctx, child, stmt->kw, 0, &restr->exts)); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 951 | break; |
| 952 | default: |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 953 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(child->kw), lyplg_ext_stmt2str(stmt->kw)); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 954 | return LY_EVALID; |
| 955 | } |
| 956 | } |
| 957 | return LY_SUCCESS; |
| 958 | } |
| 959 | |
| 960 | /** |
| 961 | * @brief Parse a restriction that can have more instances such as must. |
| 962 | * |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 963 | * @param[in] ctx parser context. |
| 964 | * @param[in] stmt Source statement data from the parsed extension instance. |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 965 | * @param[in,out] restrs Restrictions to add to. |
| 966 | * |
| 967 | * @return LY_ERR values. |
| 968 | */ |
| 969 | static LY_ERR |
Michal Vasko | d0625d7 | 2022-10-06 15:02:50 +0200 | [diff] [blame] | 970 | lysp_stmt_restrs(struct lysp_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_restr **restrs) |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 971 | { |
| 972 | struct lysp_restr *restr; |
| 973 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 974 | LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *restrs, restr, LY_EMEM); |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 975 | return lysp_stmt_restr(ctx, stmt, restr); |
| 976 | } |
| 977 | |
| 978 | /** |
| 979 | * @brief Parse the anydata or anyxml statement. |
| 980 | * |
| 981 | * @param[in] ctx parser context. |
| 982 | * @param[in] stmt Source statement data from the parsed extension instance. |
| 983 | * @param[in,out] siblings Siblings to add to. |
| 984 | * |
| 985 | * @return LY_ERR values. |
| 986 | */ |
| 987 | static LY_ERR |
Michal Vasko | d0625d7 | 2022-10-06 15:02:50 +0200 | [diff] [blame] | 988 | lysp_stmt_any(struct lysp_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_node *parent, struct lysp_node **siblings) |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 989 | { |
| 990 | struct lysp_node_anydata *any; |
| 991 | |
| 992 | LY_CHECK_RET(lysp_stmt_validate_value(ctx, Y_IDENTIF_ARG, stmt->arg)); |
| 993 | |
| 994 | /* create new structure and insert into siblings */ |
| 995 | LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, any, next, LY_EMEM); |
| 996 | |
| 997 | any->nodetype = stmt->kw == LY_STMT_ANYDATA ? LYS_ANYDATA : LYS_ANYXML; |
| 998 | any->parent = parent; |
| 999 | |
| 1000 | LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), stmt->arg, 0, &any->name)); |
| 1001 | |
| 1002 | /* parse substatements */ |
| 1003 | for (const struct lysp_stmt *child = stmt->child; child; child = child->next) { |
| 1004 | switch (child->kw) { |
| 1005 | case LY_STMT_CONFIG: |
| 1006 | LY_CHECK_RET(lysp_stmt_config(ctx, child, &any->flags, &any->exts)); |
| 1007 | break; |
| 1008 | case LY_STMT_DESCRIPTION: |
| 1009 | LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &any->dsc, Y_STR_ARG, &any->exts)); |
| 1010 | break; |
| 1011 | case LY_STMT_IF_FEATURE: |
| 1012 | LY_CHECK_RET(lysp_stmt_qnames(ctx, child, &any->iffeatures, Y_STR_ARG, &any->exts)); |
| 1013 | break; |
| 1014 | case LY_STMT_MANDATORY: |
| 1015 | LY_CHECK_RET(lysp_stmt_mandatory(ctx, child, &any->flags, &any->exts)); |
| 1016 | break; |
| 1017 | case LY_STMT_MUST: |
| 1018 | LY_CHECK_RET(lysp_stmt_restrs(ctx, child, &any->musts)); |
| 1019 | break; |
| 1020 | case LY_STMT_REFERENCE: |
| 1021 | LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &any->ref, Y_STR_ARG, &any->exts)); |
| 1022 | break; |
| 1023 | case LY_STMT_STATUS: |
| 1024 | LY_CHECK_RET(lysp_stmt_status(ctx, child, &any->flags, &any->exts)); |
| 1025 | break; |
| 1026 | case LY_STMT_WHEN: |
| 1027 | LY_CHECK_RET(lysp_stmt_when(ctx, child, &any->when)); |
| 1028 | break; |
| 1029 | case LY_STMT_EXTENSION_INSTANCE: |
Radek Krejci | 39b7fc2 | 2021-02-26 23:29:18 +0100 | [diff] [blame] | 1030 | LY_CHECK_RET(lysp_stmt_ext(ctx, child, stmt->kw, 0, &any->exts)); |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 1031 | break; |
| 1032 | default: |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1033 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(child->kw), |
| 1034 | (any->nodetype & LYS_ANYDATA) == LYS_ANYDATA ? lyplg_ext_stmt2str(LY_STMT_ANYDATA) : lyplg_ext_stmt2str(LY_STMT_ANYXML)); |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 1035 | return LY_EVALID; |
| 1036 | } |
| 1037 | } |
| 1038 | |
| 1039 | return LY_SUCCESS; |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1040 | } |
| 1041 | |
| 1042 | /** |
| 1043 | * @brief Parse the value or position statement. Substatement of type enum statement. |
| 1044 | * |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 1045 | * @param[in] ctx parser context. |
| 1046 | * @param[in] stmt Source statement data from the parsed extension instance. |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1047 | * @param[in,out] value Value to write to. |
| 1048 | * @param[in,out] flags Flags to write to. |
| 1049 | * @param[in,out] exts Extension instances to add to. |
| 1050 | * |
| 1051 | * @return LY_ERR values. |
| 1052 | */ |
| 1053 | static LY_ERR |
Michal Vasko | d0625d7 | 2022-10-06 15:02:50 +0200 | [diff] [blame] | 1054 | lysp_stmt_type_enum_value_pos(struct lysp_ctx *ctx, const struct lysp_stmt *stmt, int64_t *value, uint16_t *flags, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 1055 | struct lysp_ext_instance **exts) |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1056 | { |
Michal Vasko | 7b3a00e | 2023-08-09 11:58:03 +0200 | [diff] [blame] | 1057 | int arg_len; |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1058 | char *ptr = NULL; |
Michal Vasko | 2bf4af4 | 2023-01-04 12:08:38 +0100 | [diff] [blame] | 1059 | long long num = 0; |
| 1060 | unsigned long long unum = 0; |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1061 | |
| 1062 | if (*flags & LYS_SET_VALUE) { |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1063 | LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, lyplg_ext_stmt2str(stmt->kw)); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1064 | return LY_EVALID; |
| 1065 | } |
| 1066 | *flags |= LYS_SET_VALUE; |
| 1067 | |
| 1068 | LY_CHECK_RET(lysp_stmt_validate_value(ctx, Y_STR_ARG, stmt->arg)); |
| 1069 | |
| 1070 | arg_len = strlen(stmt->arg); |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 1071 | if (!arg_len || (stmt->arg[0] == '+') || ((stmt->arg[0] == '0') && (arg_len > 1)) || |
| 1072 | ((stmt->kw == LY_STMT_POSITION) && !strncmp(stmt->arg, "-0", 2))) { |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1073 | LOGVAL_PARSER(ctx, LY_VCODE_INVAL, arg_len, stmt->arg, lyplg_ext_stmt2str(stmt->kw)); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1074 | goto error; |
| 1075 | } |
| 1076 | |
| 1077 | errno = 0; |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 1078 | if (stmt->kw == LY_STMT_VALUE) { |
Michal Vasko | 73d77ab | 2021-07-23 12:45:55 +0200 | [diff] [blame] | 1079 | num = strtoll(stmt->arg, &ptr, LY_BASE_DEC); |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1080 | if ((num < INT64_C(-2147483648)) || (num > INT64_C(2147483647))) { |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1081 | LOGVAL_PARSER(ctx, LY_VCODE_INVAL, arg_len, stmt->arg, lyplg_ext_stmt2str(stmt->kw)); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1082 | goto error; |
| 1083 | } |
| 1084 | } else { |
Michal Vasko | 73d77ab | 2021-07-23 12:45:55 +0200 | [diff] [blame] | 1085 | unum = strtoull(stmt->arg, &ptr, LY_BASE_DEC); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1086 | if (unum > UINT64_C(4294967295)) { |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1087 | LOGVAL_PARSER(ctx, LY_VCODE_INVAL, arg_len, stmt->arg, lyplg_ext_stmt2str(stmt->kw)); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1088 | goto error; |
| 1089 | } |
| 1090 | } |
| 1091 | /* we have not parsed the whole argument */ |
Michal Vasko | 7b3a00e | 2023-08-09 11:58:03 +0200 | [diff] [blame] | 1092 | if (ptr - stmt->arg != arg_len) { |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1093 | LOGVAL_PARSER(ctx, LY_VCODE_INVAL, arg_len, stmt->arg, lyplg_ext_stmt2str(stmt->kw)); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1094 | goto error; |
| 1095 | } |
| 1096 | if (errno == ERANGE) { |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1097 | LOGVAL_PARSER(ctx, LY_VCODE_OOB, arg_len, stmt->arg, lyplg_ext_stmt2str(stmt->kw)); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1098 | goto error; |
| 1099 | } |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 1100 | if (stmt->kw == LY_STMT_VALUE) { |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1101 | *value = num; |
| 1102 | } else { |
| 1103 | *value = unum; |
| 1104 | } |
| 1105 | |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 1106 | for (const struct lysp_stmt *child = stmt->child; child; child = child->next) { |
| 1107 | switch (child->kw) { |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1108 | case LY_STMT_EXTENSION_INSTANCE: |
Radek Krejci | fc596f9 | 2021-02-26 22:40:26 +0100 | [diff] [blame] | 1109 | LY_CHECK_RET(lysp_stmt_ext(ctx, child, stmt->kw == LY_STMT_VALUE ? LY_STMT_VALUE : LY_STMT_POSITION, 0, exts)); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1110 | break; |
| 1111 | default: |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1112 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(child->kw), lyplg_ext_stmt2str(stmt->kw)); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1113 | return LY_EVALID; |
| 1114 | } |
| 1115 | } |
| 1116 | return LY_SUCCESS; |
| 1117 | |
| 1118 | error: |
| 1119 | return LY_EVALID; |
| 1120 | } |
| 1121 | |
| 1122 | /** |
| 1123 | * @brief Parse the enum or bit statement. Substatement of type statement. |
| 1124 | * |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 1125 | * @param[in] ctx parser context. |
| 1126 | * @param[in] stmt Source statement data from the parsed extension instance. |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1127 | * @param[in,out] enums Enums or bits to add to. |
| 1128 | * |
| 1129 | * @return LY_ERR values. |
| 1130 | */ |
| 1131 | static LY_ERR |
Michal Vasko | d0625d7 | 2022-10-06 15:02:50 +0200 | [diff] [blame] | 1132 | lysp_stmt_type_enum(struct lysp_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_type_enum **enums) |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1133 | { |
| 1134 | struct lysp_type_enum *enm; |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1135 | |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 1136 | LY_CHECK_RET(lysp_stmt_validate_value(ctx, stmt->kw == LY_STMT_ENUM ? Y_STR_ARG : Y_IDENTIF_ARG, stmt->arg)); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1137 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1138 | LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *enums, enm, LY_EMEM); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1139 | |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 1140 | if (stmt->kw == LY_STMT_ENUM) { |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1141 | LY_CHECK_RET(lysp_check_enum_name(ctx, stmt->arg, strlen(stmt->arg))); |
| 1142 | } /* else nothing specific for YANG_BIT */ |
| 1143 | |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 1144 | LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), stmt->arg, 0, &enm->name)); |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1145 | CHECK_UNIQUENESS(ctx, *enums, name, lyplg_ext_stmt2str(stmt->kw), enm->name); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1146 | |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 1147 | for (const struct lysp_stmt *child = stmt->child; child; child = child->next) { |
| 1148 | switch (child->kw) { |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1149 | case LY_STMT_DESCRIPTION: |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 1150 | LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &enm->dsc, Y_STR_ARG, &enm->exts)); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1151 | break; |
| 1152 | case LY_STMT_IF_FEATURE: |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1153 | PARSER_CHECK_STMTVER2_RET(ctx, "if-feature", lyplg_ext_stmt2str(stmt->kw)); |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 1154 | LY_CHECK_RET(lysp_stmt_qnames(ctx, child, &enm->iffeatures, Y_STR_ARG, &enm->exts)); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1155 | break; |
| 1156 | case LY_STMT_REFERENCE: |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 1157 | LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &enm->ref, Y_STR_ARG, &enm->exts)); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1158 | break; |
| 1159 | case LY_STMT_STATUS: |
| 1160 | LY_CHECK_RET(lysp_stmt_status(ctx, child, &enm->flags, &enm->exts)); |
| 1161 | break; |
| 1162 | case LY_STMT_VALUE: |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1163 | LY_CHECK_ERR_RET(stmt->kw == LY_STMT_BIT, LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(child->kw), |
| 1164 | lyplg_ext_stmt2str(stmt->kw)), LY_EVALID); |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 1165 | LY_CHECK_RET(lysp_stmt_type_enum_value_pos(ctx, child, &enm->value, &enm->flags, &enm->exts)); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1166 | break; |
| 1167 | case LY_STMT_POSITION: |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1168 | LY_CHECK_ERR_RET(stmt->kw == LY_STMT_ENUM, LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(child->kw), |
| 1169 | lyplg_ext_stmt2str(stmt->kw)), LY_EVALID); |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 1170 | LY_CHECK_RET(lysp_stmt_type_enum_value_pos(ctx, child, &enm->value, &enm->flags, &enm->exts)); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1171 | break; |
| 1172 | case LY_STMT_EXTENSION_INSTANCE: |
Radek Krejci | 39b7fc2 | 2021-02-26 23:29:18 +0100 | [diff] [blame] | 1173 | LY_CHECK_RET(lysp_stmt_ext(ctx, child, stmt->kw, 0, &enm->exts)); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1174 | break; |
| 1175 | default: |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1176 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(child->kw), lyplg_ext_stmt2str(stmt->kw)); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1177 | return LY_EVALID; |
| 1178 | } |
| 1179 | } |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1180 | |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1181 | return LY_SUCCESS; |
| 1182 | } |
| 1183 | |
| 1184 | /** |
| 1185 | * @brief Parse the fraction-digits statement. Substatement of type statement. |
| 1186 | * |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 1187 | * @param[in] ctx parser context. |
| 1188 | * @param[in] stmt Source statement data from the parsed extension instance. |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1189 | * @param[in,out] fracdig Value to write to. |
| 1190 | * @param[in,out] exts Extension instances to add to. |
| 1191 | * |
| 1192 | * @return LY_ERR values. |
| 1193 | */ |
| 1194 | static LY_ERR |
Michal Vasko | d0625d7 | 2022-10-06 15:02:50 +0200 | [diff] [blame] | 1195 | lysp_stmt_type_fracdigits(struct lysp_ctx *ctx, const struct lysp_stmt *stmt, uint8_t *fracdig, |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 1196 | struct lysp_ext_instance **exts) |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1197 | { |
| 1198 | char *ptr; |
Michal Vasko | 7b3a00e | 2023-08-09 11:58:03 +0200 | [diff] [blame] | 1199 | int arg_len; |
Michal Vasko | 2bf4af4 | 2023-01-04 12:08:38 +0100 | [diff] [blame] | 1200 | unsigned long long num; |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1201 | |
| 1202 | if (*fracdig) { |
| 1203 | LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "fraction-digits"); |
| 1204 | return LY_EVALID; |
| 1205 | } |
| 1206 | |
| 1207 | LY_CHECK_RET(lysp_stmt_validate_value(ctx, Y_STR_ARG, stmt->arg)); |
| 1208 | arg_len = strlen(stmt->arg); |
| 1209 | if (!arg_len || (stmt->arg[0] == '0') || !isdigit(stmt->arg[0])) { |
| 1210 | LOGVAL_PARSER(ctx, LY_VCODE_INVAL, arg_len, stmt->arg, "fraction-digits"); |
| 1211 | return LY_EVALID; |
| 1212 | } |
| 1213 | |
| 1214 | errno = 0; |
Michal Vasko | 73d77ab | 2021-07-23 12:45:55 +0200 | [diff] [blame] | 1215 | num = strtoull(stmt->arg, &ptr, LY_BASE_DEC); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1216 | /* we have not parsed the whole argument */ |
Michal Vasko | 7b3a00e | 2023-08-09 11:58:03 +0200 | [diff] [blame] | 1217 | if (ptr - stmt->arg != arg_len) { |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1218 | LOGVAL_PARSER(ctx, LY_VCODE_INVAL, arg_len, stmt->arg, "fraction-digits"); |
| 1219 | return LY_EVALID; |
| 1220 | } |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1221 | if ((errno == ERANGE) || (num > LY_TYPE_DEC64_FD_MAX)) { |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1222 | LOGVAL_PARSER(ctx, LY_VCODE_OOB, arg_len, stmt->arg, "fraction-digits"); |
| 1223 | return LY_EVALID; |
| 1224 | } |
| 1225 | *fracdig = num; |
| 1226 | |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 1227 | for (const struct lysp_stmt *child = stmt->child; child; child = child->next) { |
| 1228 | switch (child->kw) { |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1229 | case LY_STMT_EXTENSION_INSTANCE: |
Radek Krejci | fc596f9 | 2021-02-26 22:40:26 +0100 | [diff] [blame] | 1230 | LY_CHECK_RET(lysp_stmt_ext(ctx, child, LY_STMT_FRACTION_DIGITS, 0, exts)); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1231 | break; |
| 1232 | default: |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1233 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(child->kw), "fraction-digits"); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1234 | return LY_EVALID; |
| 1235 | } |
| 1236 | } |
| 1237 | return LY_SUCCESS; |
| 1238 | } |
| 1239 | |
| 1240 | /** |
| 1241 | * @brief Parse the require-instance statement. Substatement of type statement. |
| 1242 | * |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 1243 | * @param[in] ctx parser context. |
| 1244 | * @param[in] stmt Source statement data from the parsed extension instance. |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1245 | * @param[in,out] reqinst Value to write to. |
| 1246 | * @param[in,out] flags Flags to write to. |
| 1247 | * @param[in,out] exts Extension instances to add to. |
| 1248 | * |
| 1249 | * @return LY_ERR values. |
| 1250 | */ |
| 1251 | static LY_ERR |
Michal Vasko | d0625d7 | 2022-10-06 15:02:50 +0200 | [diff] [blame] | 1252 | lysp_stmt_type_reqinstance(struct lysp_ctx *ctx, const struct lysp_stmt *stmt, uint8_t *reqinst, uint16_t *flags, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 1253 | struct lysp_ext_instance **exts) |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1254 | { |
Michal Vasko | 7b3a00e | 2023-08-09 11:58:03 +0200 | [diff] [blame] | 1255 | int arg_len; |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1256 | |
| 1257 | if (*flags & LYS_SET_REQINST) { |
| 1258 | LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "require-instance"); |
| 1259 | return LY_EVALID; |
| 1260 | } |
| 1261 | *flags |= LYS_SET_REQINST; |
| 1262 | |
| 1263 | LY_CHECK_RET(lysp_stmt_validate_value(ctx, Y_STR_ARG, stmt->arg)); |
| 1264 | arg_len = strlen(stmt->arg); |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1265 | if ((arg_len == ly_strlen_const("true")) && !strncmp(stmt->arg, "true", arg_len)) { |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1266 | *reqinst = 1; |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1267 | } else if ((arg_len != ly_strlen_const("false")) || strncmp(stmt->arg, "false", arg_len)) { |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1268 | LOGVAL_PARSER(ctx, LY_VCODE_INVAL, arg_len, stmt->arg, "require-instance"); |
| 1269 | return LY_EVALID; |
| 1270 | } |
| 1271 | |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 1272 | for (const struct lysp_stmt *child = stmt->child; child; child = child->next) { |
| 1273 | switch (child->kw) { |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1274 | case LY_STMT_EXTENSION_INSTANCE: |
Radek Krejci | fc596f9 | 2021-02-26 22:40:26 +0100 | [diff] [blame] | 1275 | LY_CHECK_RET(lysp_stmt_ext(ctx, child, LY_STMT_REQUIRE_INSTANCE, 0, exts)); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1276 | break; |
| 1277 | default: |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1278 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(child->kw), "require-instance"); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1279 | return LY_EVALID; |
| 1280 | } |
| 1281 | } |
| 1282 | return LY_SUCCESS; |
| 1283 | } |
| 1284 | |
| 1285 | /** |
| 1286 | * @brief Parse the modifier statement. Substatement of type pattern statement. |
| 1287 | * |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 1288 | * @param[in] ctx parser context. |
| 1289 | * @param[in] stmt Source statement data from the parsed extension instance. |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1290 | * @param[in,out] pat Value to write to. |
| 1291 | * @param[in,out] exts Extension instances to add to. |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1292 | * @return LY_ERR values. |
| 1293 | */ |
| 1294 | static LY_ERR |
Michal Vasko | d0625d7 | 2022-10-06 15:02:50 +0200 | [diff] [blame] | 1295 | lysp_stmt_type_pattern_modifier(struct lysp_ctx *ctx, const struct lysp_stmt *stmt, const char **pat, |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 1296 | struct lysp_ext_instance **exts) |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1297 | { |
Michal Vasko | 7b3a00e | 2023-08-09 11:58:03 +0200 | [diff] [blame] | 1298 | int arg_len; |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1299 | char *buf; |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1300 | |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1301 | if ((*pat)[0] == LYSP_RESTR_PATTERN_NACK) { |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1302 | LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "modifier"); |
| 1303 | return LY_EVALID; |
| 1304 | } |
| 1305 | |
| 1306 | LY_CHECK_RET(lysp_stmt_validate_value(ctx, Y_STR_ARG, stmt->arg)); |
| 1307 | arg_len = strlen(stmt->arg); |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1308 | if ((arg_len != ly_strlen_const("invert-match")) || strncmp(stmt->arg, "invert-match", arg_len)) { |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1309 | LOGVAL_PARSER(ctx, LY_VCODE_INVAL, arg_len, stmt->arg, "modifier"); |
| 1310 | return LY_EVALID; |
| 1311 | } |
| 1312 | |
| 1313 | /* replace the value in the dictionary */ |
| 1314 | buf = malloc(strlen(*pat) + 1); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1315 | LY_CHECK_ERR_RET(!buf, LOGMEM(PARSER_CTX(ctx)), LY_EMEM); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1316 | strcpy(buf, *pat); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1317 | lydict_remove(PARSER_CTX(ctx), *pat); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1318 | |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1319 | assert(buf[0] == LYSP_RESTR_PATTERN_ACK); |
| 1320 | buf[0] = LYSP_RESTR_PATTERN_NACK; |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 1321 | LY_CHECK_RET(lydict_insert_zc(PARSER_CTX(ctx), buf, pat)); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1322 | |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 1323 | for (const struct lysp_stmt *child = stmt->child; child; child = child->next) { |
| 1324 | switch (child->kw) { |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1325 | case LY_STMT_EXTENSION_INSTANCE: |
Radek Krejci | fc596f9 | 2021-02-26 22:40:26 +0100 | [diff] [blame] | 1326 | LY_CHECK_RET(lysp_stmt_ext(ctx, child, LY_STMT_MODIFIER, 0, exts)); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1327 | break; |
| 1328 | default: |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1329 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(child->kw), "modifier"); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1330 | return LY_EVALID; |
| 1331 | } |
| 1332 | } |
| 1333 | return LY_SUCCESS; |
| 1334 | } |
| 1335 | |
| 1336 | /** |
| 1337 | * @brief Parse the pattern statement. Substatement of type statement. |
| 1338 | * |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 1339 | * @param[in] ctx parser context. |
| 1340 | * @param[in] stmt Source statement data from the parsed extension instance. |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1341 | * @param[in,out] patterns Restrictions to add to. |
| 1342 | * |
| 1343 | * @return LY_ERR values. |
| 1344 | */ |
| 1345 | static LY_ERR |
Michal Vasko | d0625d7 | 2022-10-06 15:02:50 +0200 | [diff] [blame] | 1346 | lysp_stmt_type_pattern(struct lysp_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_restr **patterns) |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1347 | { |
| 1348 | char *buf; |
| 1349 | size_t arg_len; |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1350 | struct lysp_restr *restr; |
| 1351 | |
| 1352 | LY_CHECK_RET(lysp_stmt_validate_value(ctx, Y_STR_ARG, stmt->arg)); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1353 | LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *patterns, restr, LY_EMEM); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1354 | arg_len = strlen(stmt->arg); |
| 1355 | |
| 1356 | /* add special meaning first byte */ |
| 1357 | buf = malloc(arg_len + 2); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1358 | LY_CHECK_ERR_RET(!buf, LOGMEM(PARSER_CTX(ctx)), LY_EMEM); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1359 | memmove(buf + 1, stmt->arg, arg_len); |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1360 | buf[0] = LYSP_RESTR_PATTERN_ACK; /* pattern's default regular-match flag */ |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1361 | buf[arg_len + 1] = '\0'; /* terminating NULL byte */ |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 1362 | LY_CHECK_RET(lydict_insert_zc(PARSER_CTX(ctx), buf, &restr->arg.str)); |
Michal Vasko | 8a67eff | 2021-12-07 14:04:47 +0100 | [diff] [blame] | 1363 | restr->arg.mod = PARSER_CUR_PMOD(ctx); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1364 | |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 1365 | for (const struct lysp_stmt *child = stmt->child; child; child = child->next) { |
| 1366 | switch (child->kw) { |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1367 | case LY_STMT_DESCRIPTION: |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 1368 | LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &restr->dsc, Y_STR_ARG, &restr->exts)); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1369 | break; |
| 1370 | case LY_STMT_REFERENCE: |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 1371 | LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &restr->ref, Y_STR_ARG, &restr->exts)); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1372 | break; |
| 1373 | case LY_STMT_ERROR_APP_TAG: |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 1374 | LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &restr->eapptag, Y_STR_ARG, &restr->exts)); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1375 | break; |
| 1376 | case LY_STMT_ERROR_MESSAGE: |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 1377 | LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &restr->emsg, Y_STR_ARG, &restr->exts)); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1378 | break; |
| 1379 | case LY_STMT_MODIFIER: |
| 1380 | PARSER_CHECK_STMTVER2_RET(ctx, "modifier", "pattern"); |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 1381 | LY_CHECK_RET(lysp_stmt_type_pattern_modifier(ctx, child, &restr->arg.str, &restr->exts)); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1382 | break; |
| 1383 | case LY_STMT_EXTENSION_INSTANCE: |
Radek Krejci | 39b7fc2 | 2021-02-26 23:29:18 +0100 | [diff] [blame] | 1384 | LY_CHECK_RET(lysp_stmt_ext(ctx, child, LY_STMT_PATTERN, 0, &restr->exts)); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1385 | break; |
| 1386 | default: |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1387 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(child->kw), "pattern"); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1388 | return LY_EVALID; |
| 1389 | } |
| 1390 | } |
| 1391 | return LY_SUCCESS; |
| 1392 | } |
| 1393 | |
| 1394 | /** |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1395 | * @brief Parse the deviate statement. Substatement of deviation statement. |
| 1396 | * |
| 1397 | * @param[in] ctx parser context. |
| 1398 | * @param[in] stmt Source statement data from the parsed extension instance. |
| 1399 | * @param[in,out] devs Array of deviates to add to. |
| 1400 | * @param[in,out] exts Extension instances to add to. |
| 1401 | * @return LY_ERR values. |
| 1402 | */ |
| 1403 | static LY_ERR |
| 1404 | lysp_stmt_deviate(struct lysp_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_deviate **devs, struct lysp_ext_instance **exts) |
| 1405 | { |
| 1406 | (void)stmt; |
| 1407 | (void)devs; |
| 1408 | (void)exts; |
| 1409 | |
| 1410 | /* TODO */ |
| 1411 | LOGERR(PARSER_CTX(ctx), LY_EINVAL, "Extension instance \"deviate\" substatement is not supported."); |
| 1412 | return LY_EINVAL; |
| 1413 | } |
| 1414 | |
| 1415 | /** |
| 1416 | * @brief Parse the deviation statement. |
| 1417 | * |
| 1418 | * @param[in] ctx parser context. |
| 1419 | * @param[in] stmt Source statement data from the parsed extension instance. |
| 1420 | * @param[in,out] deviations Array of deviations to add to. |
| 1421 | * @return LY_ERR values. |
| 1422 | */ |
| 1423 | static LY_ERR |
| 1424 | lysp_stmt_deviation(struct lysp_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_deviation **deviations) |
| 1425 | { |
| 1426 | struct lysp_deviation *dev; |
| 1427 | |
| 1428 | LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *deviations, dev, LY_EMEM); |
| 1429 | |
| 1430 | /* store nodeid */ |
| 1431 | LY_CHECK_RET(lysp_stmt_validate_value(ctx, Y_STR_ARG, stmt->arg)); |
| 1432 | LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), stmt->arg, 0, &dev->nodeid)); |
| 1433 | |
| 1434 | /* parse substatements */ |
| 1435 | for (const struct lysp_stmt *child = stmt->child; child; child = child->next) { |
| 1436 | switch (child->kw) { |
| 1437 | case LY_STMT_DESCRIPTION: |
| 1438 | LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &dev->dsc, Y_STR_ARG, &dev->exts)); |
| 1439 | break; |
| 1440 | case LY_STMT_DEVIATE: |
| 1441 | LY_CHECK_RET(lysp_stmt_deviate(ctx, child, &dev->deviates, &dev->exts)); |
| 1442 | break; |
| 1443 | case LY_STMT_REFERENCE: |
| 1444 | LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &dev->ref, Y_STR_ARG, &dev->exts)); |
| 1445 | break; |
| 1446 | case LY_STMT_EXTENSION_INSTANCE: |
| 1447 | LY_CHECK_RET(lysp_stmt_ext(ctx, child, stmt->kw, 0, &dev->exts)); |
| 1448 | break; |
| 1449 | default: |
| 1450 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(child->kw), lyplg_ext_stmt2str(LY_STMT_DEVIATION)); |
| 1451 | return LY_EVALID; |
| 1452 | } |
| 1453 | } |
| 1454 | |
| 1455 | return LY_SUCCESS; |
| 1456 | } |
| 1457 | |
| 1458 | /** |
| 1459 | * @brief Parse the yang-version statement. |
| 1460 | * |
| 1461 | * @param[in] ctx parser context. |
| 1462 | * @param[in] stmt Source statement data from the parsed extension instance. |
| 1463 | * @param[out] version Version to write to. |
| 1464 | * @param[in,out] exts Extension instances to add to. |
| 1465 | * @return LY_ERR values. |
| 1466 | */ |
| 1467 | static LY_ERR |
| 1468 | lysp_stmt_yangver(struct lysp_ctx *ctx, const struct lysp_stmt *stmt, uint8_t *version, struct lysp_ext_instance **exts) |
| 1469 | { |
| 1470 | if (*version) { |
| 1471 | LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "yin-element"); |
| 1472 | return LY_EVALID; |
| 1473 | } |
| 1474 | |
| 1475 | /* store flag */ |
| 1476 | if (!strcmp(stmt->arg, "1")) { |
| 1477 | *version = LYS_VERSION_1_0; |
| 1478 | } else if (!strcmp(stmt->arg, "1.1")) { |
| 1479 | *version = LYS_VERSION_1_1; |
| 1480 | } else { |
Michal Vasko | 7b3a00e | 2023-08-09 11:58:03 +0200 | [diff] [blame] | 1481 | LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)strlen(stmt->arg), stmt->arg, "yang-version"); |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1482 | return LY_EVALID; |
| 1483 | } |
| 1484 | |
| 1485 | /* parse substatements */ |
| 1486 | for (const struct lysp_stmt *child = stmt->child; child; child = child->next) { |
| 1487 | switch (child->kw) { |
| 1488 | case LY_STMT_EXTENSION_INSTANCE: |
| 1489 | LY_CHECK_RET(lysp_stmt_ext(ctx, child, stmt->kw, 0, exts)); |
| 1490 | break; |
| 1491 | default: |
| 1492 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(child->kw), lyplg_ext_stmt2str(LY_STMT_YANG_VERSION)); |
| 1493 | return LY_EVALID; |
| 1494 | } |
| 1495 | } |
| 1496 | |
| 1497 | return LY_SUCCESS; |
| 1498 | } |
| 1499 | |
| 1500 | /** |
| 1501 | * @brief Parse the module statement. |
| 1502 | * |
| 1503 | * @param[in] ctx parser context. |
| 1504 | * @param[in] stmt Source statement data from the parsed extension instance. |
| 1505 | * @param[in,out] mod Module to fill. |
| 1506 | * @return LY_ERR values. |
| 1507 | */ |
| 1508 | static LY_ERR |
| 1509 | lysp_stmt_module(struct lysp_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_module *mod) |
| 1510 | { |
| 1511 | (void)stmt; |
| 1512 | (void)mod; |
| 1513 | |
| 1514 | /* TODO */ |
| 1515 | LOGERR(PARSER_CTX(ctx), LY_EINVAL, "Extension instance \"module\" substatement is not supported."); |
| 1516 | return LY_EINVAL; |
| 1517 | } |
| 1518 | |
| 1519 | /** |
| 1520 | * @brief Parse the submodule statement. |
| 1521 | * |
| 1522 | * @param[in] ctx parser context. |
| 1523 | * @param[in] stmt Source statement data from the parsed extension instance. |
| 1524 | * @param[in,out] submod Module to fill. |
| 1525 | * @return LY_ERR values. |
| 1526 | */ |
| 1527 | static LY_ERR |
| 1528 | lysp_stmt_submodule(struct lysp_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_submodule *submod) |
| 1529 | { |
| 1530 | (void)stmt; |
| 1531 | (void)submod; |
| 1532 | |
| 1533 | /* TODO */ |
| 1534 | LOGERR(PARSER_CTX(ctx), LY_EINVAL, "Extension instance \"submodule\" substatement is not supported."); |
| 1535 | return LY_EINVAL; |
| 1536 | } |
| 1537 | |
| 1538 | /** |
| 1539 | * @brief Parse the yin-element statement. Substatement of argument statement. |
| 1540 | * |
| 1541 | * @param[in] ctx parser context. |
| 1542 | * @param[in] stmt Source statement data from the parsed extension instance. |
| 1543 | * @param[in,out] flags Flags to write to. |
| 1544 | * @param[in,out] exts Extension instances to add to. |
| 1545 | * @return LY_ERR values. |
| 1546 | */ |
| 1547 | static LY_ERR |
| 1548 | lysp_stmt_yinelem(struct lysp_ctx *ctx, const struct lysp_stmt *stmt, uint16_t *flags, struct lysp_ext_instance **exts) |
| 1549 | { |
| 1550 | if (*flags & LYS_YINELEM_MASK) { |
| 1551 | LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "yin-element"); |
| 1552 | return LY_EVALID; |
| 1553 | } |
| 1554 | |
| 1555 | /* store flag */ |
| 1556 | if (!strcmp(stmt->arg, "true")) { |
| 1557 | *flags |= LYS_YINELEM_TRUE; |
| 1558 | } else if (!strcmp(stmt->arg, "false")) { |
| 1559 | *flags |= LYS_YINELEM_FALSE; |
| 1560 | } else { |
Michal Vasko | 7b3a00e | 2023-08-09 11:58:03 +0200 | [diff] [blame] | 1561 | LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)strlen(stmt->arg), stmt->arg, "yin-element"); |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1562 | return LY_EVALID; |
| 1563 | } |
| 1564 | |
| 1565 | /* parse substatements */ |
| 1566 | for (const struct lysp_stmt *child = stmt->child; child; child = child->next) { |
| 1567 | switch (child->kw) { |
| 1568 | case LY_STMT_EXTENSION_INSTANCE: |
| 1569 | LY_CHECK_RET(lysp_stmt_ext(ctx, child, stmt->kw, 0, exts)); |
| 1570 | break; |
| 1571 | default: |
| 1572 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(child->kw), lyplg_ext_stmt2str(LY_STMT_YIN_ELEMENT)); |
| 1573 | return LY_EVALID; |
| 1574 | } |
| 1575 | } |
| 1576 | |
| 1577 | return LY_SUCCESS; |
| 1578 | } |
| 1579 | |
| 1580 | /** |
| 1581 | * @brief Parse the argument statement. Substatement of extension statement. |
| 1582 | * |
| 1583 | * @param[in] ctx parser context. |
| 1584 | * @param[in] stmt Source statement data from the parsed extension instance. |
| 1585 | * @param[in,out] ex Extension to fill. |
| 1586 | * @return LY_ERR values. |
| 1587 | */ |
| 1588 | static LY_ERR |
| 1589 | lysp_stmt_argument(struct lysp_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_ext *ex) |
| 1590 | { |
| 1591 | if (ex->argname) { |
| 1592 | LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "argument"); |
| 1593 | return LY_EVALID; |
| 1594 | } |
| 1595 | |
| 1596 | /* store argument name */ |
| 1597 | LY_CHECK_RET(lysp_stmt_validate_value(ctx, Y_PREF_IDENTIF_ARG, stmt->arg)); |
| 1598 | LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), stmt->arg, 0, &ex->argname)); |
| 1599 | |
| 1600 | /* parse substatements */ |
| 1601 | for (const struct lysp_stmt *child = stmt->child; child; child = child->next) { |
| 1602 | switch (child->kw) { |
| 1603 | case LY_STMT_YIN_ELEMENT: |
| 1604 | LY_CHECK_RET(lysp_stmt_yinelem(ctx, child, &ex->flags, &ex->exts)); |
| 1605 | break; |
| 1606 | case LY_STMT_EXTENSION_INSTANCE: |
| 1607 | LY_CHECK_RET(lysp_stmt_ext(ctx, child, stmt->kw, 0, &ex->exts)); |
| 1608 | break; |
| 1609 | default: |
| 1610 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(child->kw), lyplg_ext_stmt2str(LY_STMT_ARGUMENT)); |
| 1611 | return LY_EVALID; |
| 1612 | } |
| 1613 | } |
| 1614 | |
| 1615 | return LY_SUCCESS; |
| 1616 | } |
| 1617 | |
| 1618 | /** |
| 1619 | * @brief Parse the extension statement. |
| 1620 | * |
| 1621 | * @param[in] ctx parser context. |
| 1622 | * @param[in] stmt Source statement data from the parsed extension instance. |
| 1623 | * @param[in,out] extensions Array of extensions to add to. |
| 1624 | * @return LY_ERR values. |
| 1625 | */ |
| 1626 | static LY_ERR |
| 1627 | lysp_stmt_extension(struct lysp_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_ext **extensions) |
| 1628 | { |
| 1629 | struct lysp_ext *ex; |
| 1630 | |
| 1631 | LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *extensions, ex, LY_EMEM); |
| 1632 | |
| 1633 | /* store name */ |
| 1634 | LY_CHECK_RET(lysp_stmt_validate_value(ctx, Y_IDENTIF_ARG, stmt->arg)); |
| 1635 | LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), stmt->arg, 0, &ex->name)); |
| 1636 | |
| 1637 | /* parse substatements */ |
| 1638 | for (const struct lysp_stmt *child = stmt->child; child; child = child->next) { |
| 1639 | switch (child->kw) { |
| 1640 | case LY_STMT_DESCRIPTION: |
| 1641 | LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &ex->dsc, Y_STR_ARG, &ex->exts)); |
| 1642 | break; |
| 1643 | case LY_STMT_REFERENCE: |
| 1644 | LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &ex->ref, Y_STR_ARG, &ex->exts)); |
| 1645 | break; |
| 1646 | case LY_STMT_STATUS: |
| 1647 | LY_CHECK_RET(lysp_stmt_status(ctx, child, &ex->flags, &ex->exts)); |
| 1648 | break; |
| 1649 | case LY_STMT_ARGUMENT: |
| 1650 | LY_CHECK_RET(lysp_stmt_argument(ctx, child, ex)); |
| 1651 | break; |
| 1652 | case LY_STMT_EXTENSION_INSTANCE: |
| 1653 | LY_CHECK_RET(lysp_stmt_ext(ctx, child, stmt->kw, 0, &ex->exts)); |
| 1654 | break; |
| 1655 | default: |
| 1656 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(child->kw), lyplg_ext_stmt2str(LY_STMT_EXTENSION)); |
| 1657 | return LY_EVALID; |
| 1658 | } |
| 1659 | } |
| 1660 | |
| 1661 | return LY_SUCCESS; |
| 1662 | } |
| 1663 | |
| 1664 | /** |
| 1665 | * @brief Parse the feature statement. |
| 1666 | * |
| 1667 | * @param[in] ctx parser context. |
| 1668 | * @param[in] stmt Source statement data from the parsed extension instance. |
| 1669 | * @param[in,out] features Array of features to add to. |
| 1670 | * @return LY_ERR values. |
| 1671 | */ |
| 1672 | static LY_ERR |
| 1673 | lysp_stmt_feature(struct lysp_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_feature **features) |
| 1674 | { |
| 1675 | struct lysp_feature *feat; |
| 1676 | |
| 1677 | LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *features, feat, LY_EMEM); |
| 1678 | |
| 1679 | /* store name */ |
| 1680 | LY_CHECK_RET(lysp_stmt_validate_value(ctx, Y_IDENTIF_ARG, stmt->arg)); |
| 1681 | LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), stmt->arg, 0, &feat->name)); |
| 1682 | |
| 1683 | /* parse substatements */ |
| 1684 | for (const struct lysp_stmt *child = stmt->child; child; child = child->next) { |
| 1685 | switch (child->kw) { |
| 1686 | case LY_STMT_DESCRIPTION: |
| 1687 | LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &feat->dsc, Y_STR_ARG, &feat->exts)); |
| 1688 | break; |
| 1689 | case LY_STMT_IF_FEATURE: |
| 1690 | LY_CHECK_RET(lysp_stmt_qnames(ctx, child, &feat->iffeatures, Y_STR_ARG, &feat->exts)); |
| 1691 | break; |
| 1692 | case LY_STMT_REFERENCE: |
| 1693 | LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &feat->ref, Y_STR_ARG, &feat->exts)); |
| 1694 | break; |
| 1695 | case LY_STMT_STATUS: |
| 1696 | LY_CHECK_RET(lysp_stmt_status(ctx, child, &feat->flags, &feat->exts)); |
| 1697 | break; |
| 1698 | case LY_STMT_EXTENSION_INSTANCE: |
| 1699 | LY_CHECK_RET(lysp_stmt_ext(ctx, child, stmt->kw, 0, &feat->exts)); |
| 1700 | break; |
| 1701 | default: |
| 1702 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(child->kw), lyplg_ext_stmt2str(LY_STMT_FEATURE)); |
| 1703 | return LY_EVALID; |
| 1704 | } |
| 1705 | } |
| 1706 | |
| 1707 | return LY_SUCCESS; |
| 1708 | } |
| 1709 | |
| 1710 | /** |
| 1711 | * @brief Parse the identity statement. |
| 1712 | * |
| 1713 | * @param[in] ctx parser context. |
| 1714 | * @param[in] stmt Source statement data from the parsed extension instance. |
| 1715 | * @param[in,out] identities Array of identities to add to. |
| 1716 | * @return LY_ERR values. |
| 1717 | */ |
| 1718 | static LY_ERR |
| 1719 | lysp_stmt_identity(struct lysp_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_ident **identities) |
| 1720 | { |
| 1721 | struct lysp_ident *ident; |
| 1722 | |
| 1723 | LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *identities, ident, LY_EMEM); |
| 1724 | |
| 1725 | /* store name */ |
| 1726 | LY_CHECK_RET(lysp_stmt_validate_value(ctx, Y_IDENTIF_ARG, stmt->arg)); |
| 1727 | LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), stmt->arg, 0, &ident->name)); |
| 1728 | |
| 1729 | /* parse substatements */ |
| 1730 | for (const struct lysp_stmt *child = stmt->child; child; child = child->next) { |
| 1731 | switch (child->kw) { |
| 1732 | case LY_STMT_DESCRIPTION: |
| 1733 | LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &ident->dsc, Y_STR_ARG, &ident->exts)); |
| 1734 | break; |
| 1735 | case LY_STMT_IF_FEATURE: |
| 1736 | LY_CHECK_RET(lysp_stmt_qnames(ctx, child, &ident->iffeatures, Y_STR_ARG, &ident->exts)); |
| 1737 | break; |
| 1738 | case LY_STMT_REFERENCE: |
| 1739 | LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &ident->ref, Y_STR_ARG, &ident->exts)); |
| 1740 | break; |
| 1741 | case LY_STMT_STATUS: |
| 1742 | LY_CHECK_RET(lysp_stmt_status(ctx, child, &ident->flags, &ident->exts)); |
| 1743 | break; |
| 1744 | case LY_STMT_BASE: |
| 1745 | LY_CHECK_RET(lysp_stmt_text_fields(ctx, child, &ident->bases, Y_PREF_IDENTIF_ARG, &ident->exts)); |
| 1746 | break; |
| 1747 | case LY_STMT_EXTENSION_INSTANCE: |
| 1748 | LY_CHECK_RET(lysp_stmt_ext(ctx, child, stmt->kw, 0, &ident->exts)); |
| 1749 | break; |
| 1750 | default: |
| 1751 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(child->kw), lyplg_ext_stmt2str(LY_STMT_IDENTITY)); |
| 1752 | return LY_EVALID; |
| 1753 | } |
| 1754 | } |
| 1755 | |
| 1756 | return LY_SUCCESS; |
| 1757 | } |
| 1758 | |
| 1759 | /** |
| 1760 | * @brief Parse the import statement. |
| 1761 | * |
| 1762 | * @param[in] ctx parser context. |
| 1763 | * @param[in] stmt Source statement data from the parsed extension instance. |
| 1764 | * @param[in,out] imports Array of imports to add to. |
| 1765 | * @return LY_ERR values. |
| 1766 | */ |
| 1767 | static LY_ERR |
| 1768 | lysp_stmt_import(struct lysp_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_import **imports) |
| 1769 | { |
| 1770 | struct lysp_import *imp; |
| 1771 | const char *str = NULL; |
| 1772 | |
| 1773 | LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *imports, imp, LY_EMEM); |
| 1774 | |
| 1775 | /* store name */ |
| 1776 | LY_CHECK_RET(lysp_stmt_validate_value(ctx, Y_IDENTIF_ARG, stmt->arg)); |
| 1777 | LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), stmt->arg, 0, &imp->name)); |
| 1778 | |
| 1779 | /* parse substatements */ |
| 1780 | for (const struct lysp_stmt *child = stmt->child; child; child = child->next) { |
| 1781 | switch (child->kw) { |
| 1782 | case LY_STMT_PREFIX: |
| 1783 | LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &imp->prefix, Y_IDENTIF_ARG, &imp->exts)); |
| 1784 | LY_CHECK_RET(lysp_check_prefix(ctx, *imports, NULL, &imp->prefix), LY_EVALID); |
| 1785 | break; |
| 1786 | case LY_STMT_DESCRIPTION: |
| 1787 | LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &imp->dsc, Y_STR_ARG, &imp->exts)); |
| 1788 | break; |
| 1789 | case LY_STMT_REFERENCE: |
| 1790 | LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &imp->ref, Y_STR_ARG, &imp->exts)); |
| 1791 | break; |
| 1792 | case LY_STMT_REVISION_DATE: |
| 1793 | LY_CHECK_RET(lysp_stmt_text_field(ctx, stmt, 0, &str, Y_STR_ARG, &imp->exts)); |
| 1794 | strcpy(imp->rev, str); |
| 1795 | lydict_remove(PARSER_CTX(ctx), str); |
| 1796 | LY_CHECK_RET(lysp_check_date(ctx, imp->rev, LY_REV_SIZE - 1, "revision-date")); |
| 1797 | break; |
| 1798 | case LY_STMT_EXTENSION_INSTANCE: |
| 1799 | LY_CHECK_RET(lysp_stmt_ext(ctx, child, stmt->kw, 0, &imp->exts)); |
| 1800 | break; |
| 1801 | default: |
| 1802 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(child->kw), lyplg_ext_stmt2str(LY_STMT_IMPORT)); |
| 1803 | return LY_EVALID; |
| 1804 | } |
| 1805 | } |
| 1806 | |
| 1807 | return LY_SUCCESS; |
| 1808 | } |
| 1809 | |
| 1810 | /** |
| 1811 | * @brief Parse the include statement. |
| 1812 | * |
| 1813 | * @param[in] ctx parser context. |
| 1814 | * @param[in] stmt Source statement data from the parsed extension instance. |
| 1815 | * @param[in,out] includes Array of identities to add to. |
| 1816 | * @return LY_ERR values. |
| 1817 | */ |
| 1818 | static LY_ERR |
| 1819 | lysp_stmt_include(struct lysp_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_include **includes) |
| 1820 | { |
| 1821 | struct lysp_include *inc; |
| 1822 | const char *str = NULL; |
| 1823 | |
| 1824 | LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *includes, inc, LY_EMEM); |
| 1825 | |
| 1826 | /* store name */ |
| 1827 | LY_CHECK_RET(lysp_stmt_validate_value(ctx, Y_IDENTIF_ARG, stmt->arg)); |
| 1828 | LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), stmt->arg, 0, &inc->name)); |
| 1829 | |
| 1830 | /* parse substatements */ |
| 1831 | for (const struct lysp_stmt *child = stmt->child; child; child = child->next) { |
| 1832 | switch (child->kw) { |
| 1833 | case LY_STMT_DESCRIPTION: |
| 1834 | LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &inc->dsc, Y_STR_ARG, &inc->exts)); |
| 1835 | break; |
| 1836 | case LY_STMT_REFERENCE: |
| 1837 | LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &inc->ref, Y_STR_ARG, &inc->exts)); |
| 1838 | break; |
| 1839 | case LY_STMT_REVISION_DATE: |
| 1840 | LY_CHECK_RET(lysp_stmt_text_field(ctx, stmt, 0, &str, Y_STR_ARG, &inc->exts)); |
| 1841 | strcpy(inc->rev, str); |
| 1842 | lydict_remove(PARSER_CTX(ctx), str); |
| 1843 | LY_CHECK_RET(lysp_check_date(ctx, inc->rev, LY_REV_SIZE - 1, "revision-date")); |
| 1844 | break; |
| 1845 | case LY_STMT_EXTENSION_INSTANCE: |
| 1846 | LY_CHECK_RET(lysp_stmt_ext(ctx, child, stmt->kw, 0, &inc->exts)); |
| 1847 | break; |
| 1848 | default: |
| 1849 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(child->kw), lyplg_ext_stmt2str(LY_STMT_INCLUDE)); |
| 1850 | return LY_EVALID; |
| 1851 | } |
| 1852 | } |
| 1853 | |
| 1854 | return LY_SUCCESS; |
| 1855 | } |
| 1856 | |
| 1857 | /** |
| 1858 | * @brief Parse the revision statement. |
| 1859 | * |
| 1860 | * @param[in] ctx parser context. |
| 1861 | * @param[in] stmt Source statement data from the parsed extension instance. |
| 1862 | * @param[in,out] includes Array of identities to add to. |
| 1863 | * @return LY_ERR values. |
| 1864 | */ |
| 1865 | static LY_ERR |
| 1866 | lysp_stmt_revision(struct lysp_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_revision **revs) |
| 1867 | { |
| 1868 | struct lysp_revision *rev; |
| 1869 | |
| 1870 | LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *revs, rev, LY_EMEM); |
| 1871 | |
| 1872 | /* store date */ |
Michal Vasko | b425bb3 | 2022-11-08 10:49:36 +0100 | [diff] [blame] | 1873 | LY_CHECK_RET(lysp_check_date(ctx, stmt->arg, strlen(stmt->arg), "revision")); |
Michal Vasko | d5562c7 | 2022-11-08 11:30:50 +0100 | [diff] [blame] | 1874 | strncpy(rev->date, stmt->arg, LY_REV_SIZE - 1); |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1875 | |
| 1876 | /* parse substatements */ |
| 1877 | for (const struct lysp_stmt *child = stmt->child; child; child = child->next) { |
| 1878 | switch (child->kw) { |
| 1879 | case LY_STMT_DESCRIPTION: |
| 1880 | LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &rev->dsc, Y_STR_ARG, &rev->exts)); |
| 1881 | break; |
| 1882 | case LY_STMT_REFERENCE: |
| 1883 | LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &rev->ref, Y_STR_ARG, &rev->exts)); |
| 1884 | break; |
| 1885 | case LY_STMT_EXTENSION_INSTANCE: |
| 1886 | LY_CHECK_RET(lysp_stmt_ext(ctx, child, stmt->kw, 0, &rev->exts)); |
| 1887 | break; |
| 1888 | default: |
| 1889 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(child->kw), lyplg_ext_stmt2str(LY_STMT_REVISION)); |
| 1890 | return LY_EVALID; |
| 1891 | } |
| 1892 | } |
| 1893 | |
| 1894 | return LY_SUCCESS; |
| 1895 | } |
| 1896 | |
| 1897 | /** |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1898 | * @brief Parse the type statement. |
| 1899 | * |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 1900 | * @param[in] ctx parser context. |
| 1901 | * @param[in] stmt Source statement data from the parsed extension instance. |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1902 | * @param[in,out] type Type to wrote to. |
| 1903 | * |
| 1904 | * @return LY_ERR values. |
| 1905 | */ |
| 1906 | static LY_ERR |
Michal Vasko | d0625d7 | 2022-10-06 15:02:50 +0200 | [diff] [blame] | 1907 | lysp_stmt_type(struct lysp_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_type *type) |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1908 | { |
| 1909 | struct lysp_type *nest_type; |
Radek Krejci | b124784 | 2020-07-02 16:22:38 +0200 | [diff] [blame] | 1910 | const char *str_path = NULL; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1911 | LY_ERR ret; |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1912 | |
| 1913 | if (type->name) { |
| 1914 | LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "type"); |
| 1915 | return LY_EVALID; |
| 1916 | } |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 1917 | |
| 1918 | LY_CHECK_RET(lysp_stmt_validate_value(ctx, Y_PREF_IDENTIF_ARG, stmt->arg)); |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 1919 | LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), stmt->arg, 0, &type->name)); |
Michal Vasko | 8a67eff | 2021-12-07 14:04:47 +0100 | [diff] [blame] | 1920 | type->pmod = PARSER_CUR_PMOD(ctx); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1921 | |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 1922 | for (const struct lysp_stmt *child = stmt->child; child; child = child->next) { |
| 1923 | switch (child->kw) { |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1924 | case LY_STMT_BASE: |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 1925 | LY_CHECK_RET(lysp_stmt_text_fields(ctx, child, &type->bases, Y_PREF_IDENTIF_ARG, &type->exts)); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1926 | type->flags |= LYS_SET_BASE; |
| 1927 | break; |
| 1928 | case LY_STMT_BIT: |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 1929 | LY_CHECK_RET(lysp_stmt_type_enum(ctx, child, &type->bits)); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1930 | type->flags |= LYS_SET_BIT; |
| 1931 | break; |
| 1932 | case LY_STMT_ENUM: |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 1933 | LY_CHECK_RET(lysp_stmt_type_enum(ctx, child, &type->enums)); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1934 | type->flags |= LYS_SET_ENUM; |
| 1935 | break; |
| 1936 | case LY_STMT_FRACTION_DIGITS: |
| 1937 | LY_CHECK_RET(lysp_stmt_type_fracdigits(ctx, child, &type->fraction_digits, &type->exts)); |
| 1938 | type->flags |= LYS_SET_FRDIGITS; |
| 1939 | break; |
| 1940 | case LY_STMT_LENGTH: |
| 1941 | if (type->length) { |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1942 | LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, lyplg_ext_stmt2str(child->kw)); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1943 | return LY_EVALID; |
| 1944 | } |
| 1945 | type->length = calloc(1, sizeof *type->length); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1946 | LY_CHECK_ERR_RET(!type->length, LOGMEM(PARSER_CTX(ctx)), LY_EMEM); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1947 | |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 1948 | LY_CHECK_RET(lysp_stmt_restr(ctx, child, type->length)); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1949 | type->flags |= LYS_SET_LENGTH; |
| 1950 | break; |
| 1951 | case LY_STMT_PATH: |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 1952 | LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &str_path, Y_STR_ARG, &type->exts)); |
Michal Vasko | ed725d7 | 2021-06-23 12:03:45 +0200 | [diff] [blame] | 1953 | ret = ly_path_parse(PARSER_CTX(ctx), NULL, str_path, 0, 1, LY_PATH_BEGIN_EITHER, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1954 | LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_LEAFREF, &type->path); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1955 | lydict_remove(PARSER_CTX(ctx), str_path); |
| 1956 | LY_CHECK_RET(ret); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1957 | type->flags |= LYS_SET_PATH; |
| 1958 | break; |
| 1959 | case LY_STMT_PATTERN: |
| 1960 | LY_CHECK_RET(lysp_stmt_type_pattern(ctx, child, &type->patterns)); |
| 1961 | type->flags |= LYS_SET_PATTERN; |
| 1962 | break; |
| 1963 | case LY_STMT_RANGE: |
| 1964 | if (type->range) { |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1965 | LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, lyplg_ext_stmt2str(child->kw)); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1966 | return LY_EVALID; |
| 1967 | } |
| 1968 | type->range = calloc(1, sizeof *type->range); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1969 | LY_CHECK_ERR_RET(!type->range, LOGMEM(PARSER_CTX(ctx)), LY_EMEM); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1970 | |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 1971 | LY_CHECK_RET(lysp_stmt_restr(ctx, child, type->range)); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1972 | type->flags |= LYS_SET_RANGE; |
| 1973 | break; |
| 1974 | case LY_STMT_REQUIRE_INSTANCE: |
| 1975 | LY_CHECK_RET(lysp_stmt_type_reqinstance(ctx, child, &type->require_instance, &type->flags, &type->exts)); |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 1976 | /* LYS_SET_REQINST checked and set inside lysp_stmt_type_reqinstance() */ |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1977 | break; |
| 1978 | case LY_STMT_TYPE: |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1979 | LY_ARRAY_NEW_RET(PARSER_CTX(ctx), type->types, nest_type, LY_EMEM); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1980 | LY_CHECK_RET(lysp_stmt_type(ctx, child, nest_type)); |
| 1981 | type->flags |= LYS_SET_TYPE; |
| 1982 | break; |
| 1983 | case LY_STMT_EXTENSION_INSTANCE: |
Radek Krejci | 39b7fc2 | 2021-02-26 23:29:18 +0100 | [diff] [blame] | 1984 | LY_CHECK_RET(lysp_stmt_ext(ctx, child, LY_STMT_TYPE, 0, &type->exts)); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1985 | break; |
| 1986 | default: |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1987 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(child->kw), "type"); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1988 | return LY_EVALID; |
| 1989 | } |
| 1990 | } |
| 1991 | return LY_SUCCESS; |
| 1992 | } |
| 1993 | |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 1994 | /** |
| 1995 | * @brief Parse the leaf statement. |
| 1996 | * |
| 1997 | * @param[in] ctx parser context. |
| 1998 | * @param[in] stmt Source statement data from the parsed extension instance. |
| 1999 | * @param[in] parent Parent node to connect to (not into). |
| 2000 | * @param[in,out] siblings Siblings to add to. |
| 2001 | * |
| 2002 | * @return LY_ERR values. |
| 2003 | */ |
| 2004 | static LY_ERR |
Michal Vasko | d0625d7 | 2022-10-06 15:02:50 +0200 | [diff] [blame] | 2005 | lysp_stmt_leaf(struct lysp_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_node *parent, struct lysp_node **siblings) |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 2006 | { |
| 2007 | struct lysp_node_leaf *leaf; |
| 2008 | |
| 2009 | LY_CHECK_RET(lysp_stmt_validate_value(ctx, Y_IDENTIF_ARG, stmt->arg)); |
| 2010 | |
| 2011 | /* create new leaf structure */ |
| 2012 | LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, leaf, next, LY_EMEM); |
| 2013 | leaf->nodetype = LYS_LEAF; |
| 2014 | leaf->parent = parent; |
| 2015 | |
| 2016 | /* get name */ |
| 2017 | LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), stmt->arg, 0, &leaf->name)); |
| 2018 | |
| 2019 | /* parse substatements */ |
| 2020 | for (const struct lysp_stmt *child = stmt->child; child; child = child->next) { |
| 2021 | switch (child->kw) { |
| 2022 | case LY_STMT_CONFIG: |
| 2023 | LY_CHECK_RET(lysp_stmt_config(ctx, child, &leaf->flags, &leaf->exts)); |
| 2024 | break; |
| 2025 | case LY_STMT_DEFAULT: |
| 2026 | LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &leaf->dflt.str, Y_STR_ARG, &leaf->exts)); |
Michal Vasko | 8a67eff | 2021-12-07 14:04:47 +0100 | [diff] [blame] | 2027 | leaf->dflt.mod = PARSER_CUR_PMOD(ctx); |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 2028 | break; |
| 2029 | case LY_STMT_DESCRIPTION: |
| 2030 | LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &leaf->dsc, Y_STR_ARG, &leaf->exts)); |
| 2031 | break; |
| 2032 | case LY_STMT_IF_FEATURE: |
| 2033 | LY_CHECK_RET(lysp_stmt_qnames(ctx, child, &leaf->iffeatures, Y_STR_ARG, &leaf->exts)); |
| 2034 | break; |
| 2035 | case LY_STMT_MANDATORY: |
| 2036 | LY_CHECK_RET(lysp_stmt_mandatory(ctx, child, &leaf->flags, &leaf->exts)); |
| 2037 | break; |
| 2038 | case LY_STMT_MUST: |
| 2039 | LY_CHECK_RET(lysp_stmt_restrs(ctx, child, &leaf->musts)); |
| 2040 | break; |
| 2041 | case LY_STMT_REFERENCE: |
| 2042 | LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &leaf->ref, Y_STR_ARG, &leaf->exts)); |
| 2043 | break; |
| 2044 | case LY_STMT_STATUS: |
| 2045 | LY_CHECK_RET(lysp_stmt_status(ctx, child, &leaf->flags, &leaf->exts)); |
| 2046 | break; |
| 2047 | case LY_STMT_TYPE: |
| 2048 | LY_CHECK_RET(lysp_stmt_type(ctx, child, &leaf->type)); |
| 2049 | break; |
| 2050 | case LY_STMT_UNITS: |
| 2051 | LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &leaf->units, Y_STR_ARG, &leaf->exts)); |
| 2052 | break; |
| 2053 | case LY_STMT_WHEN: |
| 2054 | LY_CHECK_RET(lysp_stmt_when(ctx, child, &leaf->when)); |
| 2055 | break; |
| 2056 | case LY_STMT_EXTENSION_INSTANCE: |
Radek Krejci | 39b7fc2 | 2021-02-26 23:29:18 +0100 | [diff] [blame] | 2057 | LY_CHECK_RET(lysp_stmt_ext(ctx, child, LY_STMT_LEAF, 0, &leaf->exts)); |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 2058 | break; |
| 2059 | default: |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 2060 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(child->kw), "leaf"); |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 2061 | return LY_EVALID; |
| 2062 | } |
| 2063 | } |
| 2064 | |
| 2065 | /* mandatory substatements */ |
| 2066 | if (!leaf->type.name) { |
| 2067 | LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "leaf"); |
| 2068 | return LY_EVALID; |
| 2069 | } |
| 2070 | |
| 2071 | return LY_SUCCESS; |
| 2072 | } |
| 2073 | |
| 2074 | /** |
| 2075 | * @brief Parse the max-elements statement. |
| 2076 | * |
| 2077 | * @param[in] ctx parser context. |
| 2078 | * @param[in] stmt Source statement data from the parsed extension instance. |
| 2079 | * @param[in,out] max Value to write to. |
| 2080 | * @param[in,out] flags Flags to write to. |
| 2081 | * @param[in,out] exts Extension instances to add to. |
| 2082 | * |
| 2083 | * @return LY_ERR values. |
| 2084 | */ |
| 2085 | static LY_ERR |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 2086 | lysp_stmt_maxelements(struct lysp_ctx *ctx, const struct lysp_stmt *stmt, uint32_t *max, uint16_t *flags, |
| 2087 | struct lysp_ext_instance **exts) |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 2088 | { |
Michal Vasko | 7b3a00e | 2023-08-09 11:58:03 +0200 | [diff] [blame] | 2089 | int arg_len; |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 2090 | char *ptr; |
Michal Vasko | 2bf4af4 | 2023-01-04 12:08:38 +0100 | [diff] [blame] | 2091 | unsigned long long num; |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 2092 | |
| 2093 | if (*flags & LYS_SET_MAX) { |
| 2094 | LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "max-elements"); |
| 2095 | return LY_EVALID; |
| 2096 | } |
| 2097 | *flags |= LYS_SET_MAX; |
| 2098 | |
| 2099 | /* get value */ |
| 2100 | LY_CHECK_RET(lysp_stmt_validate_value(ctx, Y_STR_ARG, stmt->arg)); |
| 2101 | arg_len = strlen(stmt->arg); |
| 2102 | |
| 2103 | if (!arg_len || (stmt->arg[0] == '0') || ((stmt->arg[0] != 'u') && !isdigit(stmt->arg[0]))) { |
| 2104 | LOGVAL_PARSER(ctx, LY_VCODE_INVAL, arg_len, stmt->arg, "max-elements"); |
| 2105 | return LY_EVALID; |
| 2106 | } |
| 2107 | |
| 2108 | if ((arg_len != ly_strlen_const("unbounded")) || strncmp(stmt->arg, "unbounded", arg_len)) { |
| 2109 | errno = 0; |
Michal Vasko | 73d77ab | 2021-07-23 12:45:55 +0200 | [diff] [blame] | 2110 | num = strtoull(stmt->arg, &ptr, LY_BASE_DEC); |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 2111 | /* we have not parsed the whole argument */ |
Michal Vasko | 7b3a00e | 2023-08-09 11:58:03 +0200 | [diff] [blame] | 2112 | if (ptr - stmt->arg != arg_len) { |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 2113 | LOGVAL_PARSER(ctx, LY_VCODE_INVAL, arg_len, stmt->arg, "max-elements"); |
| 2114 | return LY_EVALID; |
| 2115 | } |
| 2116 | if ((errno == ERANGE) || (num > UINT32_MAX)) { |
| 2117 | LOGVAL_PARSER(ctx, LY_VCODE_OOB, arg_len, stmt->arg, "max-elements"); |
| 2118 | return LY_EVALID; |
| 2119 | } |
| 2120 | |
| 2121 | *max = num; |
| 2122 | } else { |
| 2123 | /* unbounded */ |
| 2124 | *max = 0; |
| 2125 | } |
| 2126 | |
| 2127 | for (const struct lysp_stmt *child = stmt->child; child; child = child->next) { |
| 2128 | switch (child->kw) { |
| 2129 | case LY_STMT_EXTENSION_INSTANCE: |
Radek Krejci | fc596f9 | 2021-02-26 22:40:26 +0100 | [diff] [blame] | 2130 | LY_CHECK_RET(lysp_stmt_ext(ctx, child, LY_STMT_MAX_ELEMENTS, 0, exts)); |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 2131 | break; |
| 2132 | default: |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 2133 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(child->kw), "max-elements"); |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 2134 | return LY_EVALID; |
| 2135 | } |
| 2136 | } |
| 2137 | |
| 2138 | return LY_SUCCESS; |
| 2139 | } |
| 2140 | |
| 2141 | /** |
| 2142 | * @brief Parse the min-elements statement. |
| 2143 | * |
| 2144 | * @param[in] ctx parser context. |
| 2145 | * @param[in] stmt Source statement data from the parsed extension instance. |
| 2146 | * @param[in,out] min Value to write to. |
| 2147 | * @param[in,out] flags Flags to write to. |
| 2148 | * @param[in,out] exts Extension instances to add to. |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 2149 | * @return LY_ERR values. |
| 2150 | */ |
| 2151 | static LY_ERR |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 2152 | lysp_stmt_minelements(struct lysp_ctx *ctx, const struct lysp_stmt *stmt, uint32_t *min, uint16_t *flags, |
| 2153 | struct lysp_ext_instance **exts) |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 2154 | { |
Michal Vasko | 7b3a00e | 2023-08-09 11:58:03 +0200 | [diff] [blame] | 2155 | int arg_len; |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 2156 | char *ptr; |
Michal Vasko | 2bf4af4 | 2023-01-04 12:08:38 +0100 | [diff] [blame] | 2157 | unsigned long long num; |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 2158 | |
| 2159 | if (*flags & LYS_SET_MIN) { |
| 2160 | LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "min-elements"); |
| 2161 | return LY_EVALID; |
| 2162 | } |
| 2163 | *flags |= LYS_SET_MIN; |
| 2164 | |
| 2165 | /* get value */ |
| 2166 | LY_CHECK_RET(lysp_stmt_validate_value(ctx, Y_STR_ARG, stmt->arg)); |
| 2167 | arg_len = strlen(stmt->arg); |
| 2168 | |
| 2169 | if (!arg_len || !isdigit(stmt->arg[0]) || ((stmt->arg[0] == '0') && (arg_len > 1))) { |
| 2170 | LOGVAL_PARSER(ctx, LY_VCODE_INVAL, arg_len, stmt->arg, "min-elements"); |
| 2171 | return LY_EVALID; |
| 2172 | } |
| 2173 | |
| 2174 | errno = 0; |
Michal Vasko | 73d77ab | 2021-07-23 12:45:55 +0200 | [diff] [blame] | 2175 | num = strtoull(stmt->arg, &ptr, LY_BASE_DEC); |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 2176 | /* we have not parsed the whole argument */ |
Michal Vasko | 7b3a00e | 2023-08-09 11:58:03 +0200 | [diff] [blame] | 2177 | if (ptr - stmt->arg != arg_len) { |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 2178 | LOGVAL_PARSER(ctx, LY_VCODE_INVAL, arg_len, stmt->arg, "min-elements"); |
| 2179 | return LY_EVALID; |
| 2180 | } |
| 2181 | if ((errno == ERANGE) || (num > UINT32_MAX)) { |
| 2182 | LOGVAL_PARSER(ctx, LY_VCODE_OOB, arg_len, stmt->arg, "min-elements"); |
| 2183 | return LY_EVALID; |
| 2184 | } |
| 2185 | *min = num; |
| 2186 | |
| 2187 | for (const struct lysp_stmt *child = stmt->child; child; child = child->next) { |
| 2188 | switch (child->kw) { |
| 2189 | case LY_STMT_EXTENSION_INSTANCE: |
Radek Krejci | fc596f9 | 2021-02-26 22:40:26 +0100 | [diff] [blame] | 2190 | LY_CHECK_RET(lysp_stmt_ext(ctx, child, LY_STMT_MIN_ELEMENTS, 0, exts)); |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 2191 | break; |
| 2192 | default: |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 2193 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(child->kw), "min-elements"); |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 2194 | return LY_EVALID; |
| 2195 | } |
| 2196 | } |
| 2197 | |
| 2198 | return LY_SUCCESS; |
| 2199 | } |
| 2200 | |
| 2201 | /** |
| 2202 | * @brief Parse the ordered-by statement. |
| 2203 | * |
| 2204 | * @param[in] ctx parser context. |
| 2205 | * @param[in] stmt Source statement data from the parsed extension instance. |
| 2206 | * @param[in,out] flags Flags to write to. |
| 2207 | * @param[in,out] exts Extension instances to add to. |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 2208 | * @return LY_ERR values. |
| 2209 | */ |
| 2210 | static LY_ERR |
Michal Vasko | d0625d7 | 2022-10-06 15:02:50 +0200 | [diff] [blame] | 2211 | lysp_stmt_orderedby(struct lysp_ctx *ctx, const struct lysp_stmt *stmt, uint16_t *flags, struct lysp_ext_instance **exts) |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 2212 | { |
Michal Vasko | 7b3a00e | 2023-08-09 11:58:03 +0200 | [diff] [blame] | 2213 | int arg_len; |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 2214 | |
| 2215 | if (*flags & LYS_ORDBY_MASK) { |
| 2216 | LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "ordered-by"); |
| 2217 | return LY_EVALID; |
| 2218 | } |
| 2219 | |
| 2220 | /* get value */ |
| 2221 | LY_CHECK_RET(lysp_stmt_validate_value(ctx, Y_STR_ARG, stmt->arg)); |
| 2222 | arg_len = strlen(stmt->arg); |
| 2223 | if ((arg_len == ly_strlen_const("system")) && !strncmp(stmt->arg, "system", arg_len)) { |
| 2224 | *flags |= LYS_MAND_TRUE; |
| 2225 | } else if ((arg_len == ly_strlen_const("user")) && !strncmp(stmt->arg, "user", arg_len)) { |
| 2226 | *flags |= LYS_MAND_FALSE; |
| 2227 | } else { |
| 2228 | LOGVAL_PARSER(ctx, LY_VCODE_INVAL, arg_len, stmt->arg, "ordered-by"); |
| 2229 | return LY_EVALID; |
| 2230 | } |
| 2231 | |
| 2232 | for (const struct lysp_stmt *child = stmt->child; child; child = child->next) { |
| 2233 | switch (child->kw) { |
| 2234 | case LY_STMT_EXTENSION_INSTANCE: |
Radek Krejci | fc596f9 | 2021-02-26 22:40:26 +0100 | [diff] [blame] | 2235 | LY_CHECK_RET(lysp_stmt_ext(ctx, child, LY_STMT_ORDERED_BY, 0, exts)); |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 2236 | break; |
| 2237 | default: |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 2238 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(child->kw), "ordered-by"); |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 2239 | return LY_EVALID; |
| 2240 | } |
| 2241 | } |
| 2242 | |
| 2243 | return LY_SUCCESS; |
| 2244 | } |
| 2245 | |
| 2246 | /** |
| 2247 | * @brief Parse the leaf-list statement. |
| 2248 | * |
| 2249 | * @param[in] ctx parser context. |
| 2250 | * @param[in] stmt Source statement data from the parsed extension instance. |
| 2251 | * @param[in] parent Parent node to connect to (not into). |
| 2252 | * @param[in,out] siblings Siblings to add to. |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 2253 | * @return LY_ERR values. |
| 2254 | */ |
| 2255 | static LY_ERR |
Michal Vasko | d0625d7 | 2022-10-06 15:02:50 +0200 | [diff] [blame] | 2256 | lysp_stmt_leaflist(struct lysp_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_node *parent, |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 2257 | struct lysp_node **siblings) |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 2258 | { |
| 2259 | struct lysp_node_leaflist *llist; |
| 2260 | |
| 2261 | LY_CHECK_RET(lysp_stmt_validate_value(ctx, Y_IDENTIF_ARG, stmt->arg)); |
| 2262 | |
| 2263 | /* create new leaf-list structure */ |
| 2264 | LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, llist, next, LY_EMEM); |
| 2265 | llist->nodetype = LYS_LEAFLIST; |
| 2266 | llist->parent = parent; |
| 2267 | |
| 2268 | /* get name */ |
| 2269 | LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), stmt->arg, 0, &llist->name)); |
| 2270 | |
| 2271 | /* parse substatements */ |
| 2272 | for (const struct lysp_stmt *child = stmt->child; child; child = child->next) { |
| 2273 | switch (child->kw) { |
| 2274 | case LY_STMT_CONFIG: |
| 2275 | LY_CHECK_RET(lysp_stmt_config(ctx, child, &llist->flags, &llist->exts)); |
| 2276 | break; |
| 2277 | case LY_STMT_DEFAULT: |
| 2278 | PARSER_CHECK_STMTVER2_RET(ctx, "default", "leaf-list"); |
| 2279 | LY_CHECK_RET(lysp_stmt_qnames(ctx, child, &llist->dflts, Y_STR_ARG, &llist->exts)); |
| 2280 | break; |
| 2281 | case LY_STMT_DESCRIPTION: |
| 2282 | LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &llist->dsc, Y_STR_ARG, &llist->exts)); |
| 2283 | break; |
| 2284 | case LY_STMT_IF_FEATURE: |
| 2285 | LY_CHECK_RET(lysp_stmt_qnames(ctx, child, &llist->iffeatures, Y_STR_ARG, &llist->exts)); |
| 2286 | break; |
| 2287 | case LY_STMT_MAX_ELEMENTS: |
| 2288 | LY_CHECK_RET(lysp_stmt_maxelements(ctx, child, &llist->max, &llist->flags, &llist->exts)); |
| 2289 | break; |
| 2290 | case LY_STMT_MIN_ELEMENTS: |
| 2291 | LY_CHECK_RET(lysp_stmt_minelements(ctx, child, &llist->min, &llist->flags, &llist->exts)); |
| 2292 | break; |
| 2293 | case LY_STMT_MUST: |
| 2294 | LY_CHECK_RET(lysp_stmt_restrs(ctx, child, &llist->musts)); |
| 2295 | break; |
| 2296 | case LY_STMT_ORDERED_BY: |
| 2297 | LY_CHECK_RET(lysp_stmt_orderedby(ctx, child, &llist->flags, &llist->exts)); |
| 2298 | break; |
| 2299 | case LY_STMT_REFERENCE: |
| 2300 | LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &llist->ref, Y_STR_ARG, &llist->exts)); |
| 2301 | break; |
| 2302 | case LY_STMT_STATUS: |
| 2303 | LY_CHECK_RET(lysp_stmt_status(ctx, child, &llist->flags, &llist->exts)); |
| 2304 | break; |
| 2305 | case LY_STMT_TYPE: |
| 2306 | LY_CHECK_RET(lysp_stmt_type(ctx, child, &llist->type)); |
| 2307 | break; |
| 2308 | case LY_STMT_UNITS: |
| 2309 | LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &llist->units, Y_STR_ARG, &llist->exts)); |
| 2310 | break; |
| 2311 | case LY_STMT_WHEN: |
| 2312 | LY_CHECK_RET(lysp_stmt_when(ctx, child, &llist->when)); |
| 2313 | break; |
| 2314 | case LY_STMT_EXTENSION_INSTANCE: |
Radek Krejci | 39b7fc2 | 2021-02-26 23:29:18 +0100 | [diff] [blame] | 2315 | LY_CHECK_RET(lysp_stmt_ext(ctx, child, LY_STMT_LEAF_LIST, 0, &llist->exts)); |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 2316 | break; |
| 2317 | default: |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 2318 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(child->kw), "llist"); |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 2319 | return LY_EVALID; |
| 2320 | } |
| 2321 | } |
| 2322 | |
| 2323 | /* mandatory substatements */ |
| 2324 | if (!llist->type.name) { |
| 2325 | LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "leaf-list"); |
| 2326 | return LY_EVALID; |
| 2327 | } |
| 2328 | |
| 2329 | return LY_SUCCESS; |
| 2330 | } |
| 2331 | |
| 2332 | /** |
| 2333 | * @brief Parse the refine statement. |
| 2334 | * |
| 2335 | * @param[in] ctx parser context. |
| 2336 | * @param[in] stmt Source statement data from the parsed extension instance. |
| 2337 | * @param[in,out] refines Refines to add to. |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 2338 | * @return LY_ERR values. |
| 2339 | */ |
| 2340 | static LY_ERR |
Michal Vasko | d0625d7 | 2022-10-06 15:02:50 +0200 | [diff] [blame] | 2341 | lysp_stmt_refine(struct lysp_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_refine **refines) |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 2342 | { |
| 2343 | struct lysp_refine *rf; |
| 2344 | |
| 2345 | LY_CHECK_RET(lysp_stmt_validate_value(ctx, Y_STR_ARG, stmt->arg)); |
| 2346 | |
| 2347 | LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *refines, rf, LY_EMEM); |
| 2348 | |
| 2349 | LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), stmt->arg, 0, &rf->nodeid)); |
| 2350 | |
| 2351 | for (const struct lysp_stmt *child = stmt->child; child; child = child->next) { |
| 2352 | switch (child->kw) { |
| 2353 | case LY_STMT_CONFIG: |
| 2354 | LY_CHECK_RET(lysp_stmt_config(ctx, child, &rf->flags, &rf->exts)); |
| 2355 | break; |
| 2356 | case LY_STMT_DEFAULT: |
| 2357 | LY_CHECK_RET(lysp_stmt_qnames(ctx, child, &rf->dflts, Y_STR_ARG, &rf->exts)); |
| 2358 | break; |
| 2359 | case LY_STMT_DESCRIPTION: |
| 2360 | LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &rf->dsc, Y_STR_ARG, &rf->exts)); |
| 2361 | break; |
| 2362 | case LY_STMT_IF_FEATURE: |
| 2363 | PARSER_CHECK_STMTVER2_RET(ctx, "if-feature", "refine"); |
| 2364 | LY_CHECK_RET(lysp_stmt_qnames(ctx, child, &rf->iffeatures, Y_STR_ARG, &rf->exts)); |
| 2365 | break; |
| 2366 | case LY_STMT_MAX_ELEMENTS: |
| 2367 | LY_CHECK_RET(lysp_stmt_maxelements(ctx, child, &rf->max, &rf->flags, &rf->exts)); |
| 2368 | break; |
| 2369 | case LY_STMT_MIN_ELEMENTS: |
| 2370 | LY_CHECK_RET(lysp_stmt_minelements(ctx, child, &rf->min, &rf->flags, &rf->exts)); |
| 2371 | break; |
| 2372 | case LY_STMT_MUST: |
| 2373 | LY_CHECK_RET(lysp_stmt_restrs(ctx, child, &rf->musts)); |
| 2374 | break; |
| 2375 | case LY_STMT_MANDATORY: |
| 2376 | LY_CHECK_RET(lysp_stmt_mandatory(ctx, child, &rf->flags, &rf->exts)); |
| 2377 | break; |
| 2378 | case LY_STMT_REFERENCE: |
| 2379 | LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &rf->ref, Y_STR_ARG, &rf->exts)); |
| 2380 | break; |
| 2381 | case LY_STMT_PRESENCE: |
| 2382 | LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &rf->presence, Y_STR_ARG, &rf->exts)); |
| 2383 | break; |
| 2384 | case LY_STMT_EXTENSION_INSTANCE: |
Radek Krejci | 39b7fc2 | 2021-02-26 23:29:18 +0100 | [diff] [blame] | 2385 | LY_CHECK_RET(lysp_stmt_ext(ctx, child, LY_STMT_REFINE, 0, &rf->exts)); |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 2386 | break; |
| 2387 | default: |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 2388 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(child->kw), "refine"); |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 2389 | return LY_EVALID; |
| 2390 | } |
| 2391 | } |
| 2392 | |
| 2393 | return LY_SUCCESS; |
| 2394 | } |
| 2395 | |
| 2396 | /** |
| 2397 | * @brief Parse the typedef statement. |
| 2398 | * |
| 2399 | * @param[in] ctx parser context. |
| 2400 | * @param[in] stmt Source statement data from the parsed extension instance. |
| 2401 | * @param[in] parent Parent node to connect to (not into). |
| 2402 | * @param[in,out] typedefs Typedefs to add to. |
| 2403 | * |
| 2404 | * @return LY_ERR values. |
| 2405 | */ |
| 2406 | static LY_ERR |
Michal Vasko | d0625d7 | 2022-10-06 15:02:50 +0200 | [diff] [blame] | 2407 | lysp_stmt_typedef(struct lysp_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_node *parent, |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 2408 | struct lysp_tpdf **typedefs) |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 2409 | { |
| 2410 | struct lysp_tpdf *tpdf; |
| 2411 | |
| 2412 | LY_CHECK_RET(lysp_stmt_validate_value(ctx, Y_IDENTIF_ARG, stmt->arg)); |
| 2413 | |
| 2414 | LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *typedefs, tpdf, LY_EMEM); |
| 2415 | |
| 2416 | LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), stmt->arg, 0, &tpdf->name)); |
| 2417 | |
| 2418 | /* parse substatements */ |
| 2419 | for (const struct lysp_stmt *child = stmt->child; child; child = child->next) { |
| 2420 | switch (child->kw) { |
| 2421 | case LY_STMT_DEFAULT: |
| 2422 | LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &tpdf->dflt.str, Y_STR_ARG, &tpdf->exts)); |
Michal Vasko | 8a67eff | 2021-12-07 14:04:47 +0100 | [diff] [blame] | 2423 | tpdf->dflt.mod = PARSER_CUR_PMOD(ctx); |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 2424 | break; |
| 2425 | case LY_STMT_DESCRIPTION: |
| 2426 | LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &tpdf->dsc, Y_STR_ARG, &tpdf->exts)); |
| 2427 | break; |
| 2428 | case LY_STMT_REFERENCE: |
| 2429 | LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &tpdf->ref, Y_STR_ARG, &tpdf->exts)); |
| 2430 | break; |
| 2431 | case LY_STMT_STATUS: |
| 2432 | LY_CHECK_RET(lysp_stmt_status(ctx, child, &tpdf->flags, &tpdf->exts)); |
| 2433 | break; |
| 2434 | case LY_STMT_TYPE: |
| 2435 | LY_CHECK_RET(lysp_stmt_type(ctx, child, &tpdf->type)); |
| 2436 | break; |
| 2437 | case LY_STMT_UNITS: |
| 2438 | LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &tpdf->units, Y_STR_ARG, &tpdf->exts)); |
| 2439 | break; |
| 2440 | case LY_STMT_EXTENSION_INSTANCE: |
Radek Krejci | 39b7fc2 | 2021-02-26 23:29:18 +0100 | [diff] [blame] | 2441 | LY_CHECK_RET(lysp_stmt_ext(ctx, child, LY_STMT_TYPEDEF, 0, &tpdf->exts)); |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 2442 | break; |
| 2443 | default: |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 2444 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(child->kw), "typedef"); |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 2445 | return LY_EVALID; |
| 2446 | } |
| 2447 | } |
| 2448 | |
| 2449 | /* mandatory substatements */ |
| 2450 | if (!tpdf->type.name) { |
| 2451 | LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "typedef"); |
| 2452 | return LY_EVALID; |
| 2453 | } |
| 2454 | |
| 2455 | /* store data for collision check */ |
| 2456 | if (parent && !(parent->nodetype & (LYS_GROUPING | LYS_RPC | LYS_ACTION | LYS_INPUT | LYS_OUTPUT | LYS_NOTIF))) { |
| 2457 | LY_CHECK_RET(ly_set_add(&ctx->tpdfs_nodes, parent, 0, NULL)); |
| 2458 | } |
| 2459 | |
| 2460 | return LY_SUCCESS; |
| 2461 | } |
| 2462 | |
| 2463 | /** |
| 2464 | * @brief Parse the input or output statement. |
| 2465 | * |
| 2466 | * @param[in] ctx parser context. |
| 2467 | * @param[in] stmt Source statement data from the parsed extension instance. |
| 2468 | * @param[in] parent Parent node to connect to (not into). |
| 2469 | * @param[in,out] inout_p Input/output pointer to write to. |
| 2470 | * |
| 2471 | * @return LY_ERR values. |
| 2472 | */ |
| 2473 | static LY_ERR |
Michal Vasko | d0625d7 | 2022-10-06 15:02:50 +0200 | [diff] [blame] | 2474 | lysp_stmt_inout(struct lysp_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_node *parent, |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 2475 | struct lysp_node_action_inout *inout_p) |
| 2476 | { |
| 2477 | if (inout_p->nodetype) { |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 2478 | LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, lyplg_ext_stmt2str(stmt->kw)); |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 2479 | return LY_EVALID; |
| 2480 | } |
| 2481 | |
| 2482 | /* initiate structure */ |
| 2483 | LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), stmt->kw == LY_STMT_INPUT ? "input" : "output", 0, &inout_p->name)); |
| 2484 | inout_p->nodetype = stmt->kw == LY_STMT_INPUT ? LYS_INPUT : LYS_OUTPUT; |
| 2485 | inout_p->parent = parent; |
| 2486 | |
| 2487 | /* parse substatements */ |
| 2488 | for (const struct lysp_stmt *child = stmt->child; child; child = child->next) { |
| 2489 | switch (child->kw) { |
| 2490 | case LY_STMT_ANYDATA: |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 2491 | PARSER_CHECK_STMTVER2_RET(ctx, "anydata", lyplg_ext_stmt2str(stmt->kw)); |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 2492 | /* fall through */ |
| 2493 | case LY_STMT_ANYXML: |
| 2494 | LY_CHECK_RET(lysp_stmt_any(ctx, child, &inout_p->node, &inout_p->child)); |
| 2495 | break; |
| 2496 | case LY_STMT_CHOICE: |
| 2497 | LY_CHECK_RET(lysp_stmt_choice(ctx, child, &inout_p->node, &inout_p->child)); |
| 2498 | break; |
| 2499 | case LY_STMT_CONTAINER: |
| 2500 | LY_CHECK_RET(lysp_stmt_container(ctx, child, &inout_p->node, &inout_p->child)); |
| 2501 | break; |
| 2502 | case LY_STMT_LEAF: |
| 2503 | LY_CHECK_RET(lysp_stmt_leaf(ctx, child, &inout_p->node, &inout_p->child)); |
| 2504 | break; |
| 2505 | case LY_STMT_LEAF_LIST: |
| 2506 | LY_CHECK_RET(lysp_stmt_leaflist(ctx, child, &inout_p->node, &inout_p->child)); |
| 2507 | break; |
| 2508 | case LY_STMT_LIST: |
| 2509 | LY_CHECK_RET(lysp_stmt_list(ctx, child, &inout_p->node, &inout_p->child)); |
| 2510 | break; |
| 2511 | case LY_STMT_USES: |
| 2512 | LY_CHECK_RET(lysp_stmt_uses(ctx, child, &inout_p->node, &inout_p->child)); |
| 2513 | break; |
| 2514 | case LY_STMT_TYPEDEF: |
| 2515 | LY_CHECK_RET(lysp_stmt_typedef(ctx, child, &inout_p->node, &inout_p->typedefs)); |
| 2516 | break; |
| 2517 | case LY_STMT_MUST: |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 2518 | PARSER_CHECK_STMTVER2_RET(ctx, "must", lyplg_ext_stmt2str(stmt->kw)); |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 2519 | LY_CHECK_RET(lysp_stmt_restrs(ctx, child, &inout_p->musts)); |
| 2520 | break; |
| 2521 | case LY_STMT_GROUPING: |
| 2522 | LY_CHECK_RET(lysp_stmt_grouping(ctx, child, &inout_p->node, &inout_p->groupings)); |
| 2523 | break; |
| 2524 | case LY_STMT_EXTENSION_INSTANCE: |
Radek Krejci | 39b7fc2 | 2021-02-26 23:29:18 +0100 | [diff] [blame] | 2525 | LY_CHECK_RET(lysp_stmt_ext(ctx, child, stmt->kw, 0, &inout_p->exts)); |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 2526 | break; |
| 2527 | default: |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 2528 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(child->kw), lyplg_ext_stmt2str(stmt->kw)); |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 2529 | return LY_EVALID; |
| 2530 | } |
| 2531 | } |
| 2532 | |
| 2533 | if (!inout_p->child) { |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 2534 | LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "data-def-stmt", lyplg_ext_stmt2str(stmt->kw)); |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 2535 | return LY_EVALID; |
| 2536 | } |
| 2537 | |
| 2538 | return LY_SUCCESS; |
| 2539 | } |
| 2540 | |
| 2541 | /** |
| 2542 | * @brief Parse the action statement. |
| 2543 | * |
| 2544 | * @param[in] ctx parser context. |
| 2545 | * @param[in] stmt Source statement data from the parsed extension instance. |
| 2546 | * @param[in] parent Parent node to connect to (not into). |
| 2547 | * @param[in,out] actions Actions to add to. |
| 2548 | * |
| 2549 | * @return LY_ERR values. |
| 2550 | */ |
| 2551 | static LY_ERR |
Michal Vasko | d0625d7 | 2022-10-06 15:02:50 +0200 | [diff] [blame] | 2552 | lysp_stmt_action(struct lysp_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_node *parent, |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 2553 | struct lysp_node_action **actions) |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 2554 | { |
| 2555 | struct lysp_node_action *act; |
| 2556 | |
| 2557 | LY_CHECK_RET(lysp_stmt_validate_value(ctx, Y_IDENTIF_ARG, stmt->arg)); |
| 2558 | |
| 2559 | LY_LIST_NEW_RET(PARSER_CTX(ctx), actions, act, next, LY_EMEM); |
| 2560 | |
| 2561 | LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), stmt->arg, 0, &act->name)); |
| 2562 | act->nodetype = parent ? LYS_ACTION : LYS_RPC; |
| 2563 | act->parent = parent; |
| 2564 | |
| 2565 | for (const struct lysp_stmt *child = stmt->child; child; child = child->next) { |
| 2566 | switch (child->kw) { |
| 2567 | case LY_STMT_DESCRIPTION: |
| 2568 | LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &act->dsc, Y_STR_ARG, &act->exts)); |
| 2569 | break; |
| 2570 | case LY_STMT_IF_FEATURE: |
| 2571 | LY_CHECK_RET(lysp_stmt_qnames(ctx, child, &act->iffeatures, Y_STR_ARG, &act->exts)); |
| 2572 | break; |
| 2573 | case LY_STMT_REFERENCE: |
| 2574 | LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &act->ref, Y_STR_ARG, &act->exts)); |
| 2575 | break; |
| 2576 | case LY_STMT_STATUS: |
| 2577 | LY_CHECK_RET(lysp_stmt_status(ctx, child, &act->flags, &act->exts)); |
| 2578 | break; |
| 2579 | |
| 2580 | case LY_STMT_INPUT: |
| 2581 | LY_CHECK_RET(lysp_stmt_inout(ctx, child, &act->node, &act->input)); |
| 2582 | break; |
| 2583 | case LY_STMT_OUTPUT: |
| 2584 | LY_CHECK_RET(lysp_stmt_inout(ctx, child, &act->node, &act->output)); |
| 2585 | break; |
| 2586 | |
| 2587 | case LY_STMT_TYPEDEF: |
| 2588 | LY_CHECK_RET(lysp_stmt_typedef(ctx, child, &act->node, &act->typedefs)); |
| 2589 | break; |
| 2590 | case LY_STMT_GROUPING: |
| 2591 | LY_CHECK_RET(lysp_stmt_grouping(ctx, child, &act->node, &act->groupings)); |
| 2592 | break; |
| 2593 | case LY_STMT_EXTENSION_INSTANCE: |
Radek Krejci | 39b7fc2 | 2021-02-26 23:29:18 +0100 | [diff] [blame] | 2594 | LY_CHECK_RET(lysp_stmt_ext(ctx, child, parent ? LY_STMT_ACTION : LY_STMT_RPC, 0, &act->exts)); |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 2595 | break; |
| 2596 | default: |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 2597 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(child->kw), parent ? "action" : "rpc"); |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 2598 | return LY_EVALID; |
| 2599 | } |
| 2600 | } |
| 2601 | |
| 2602 | /* always initialize inout, they are technically present (needed for later deviations/refines) */ |
| 2603 | if (!act->input.nodetype) { |
| 2604 | act->input.nodetype = LYS_INPUT; |
| 2605 | act->input.parent = &act->node; |
| 2606 | LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), "input", 0, &act->input.name)); |
| 2607 | } |
| 2608 | if (!act->output.nodetype) { |
| 2609 | act->output.nodetype = LYS_OUTPUT; |
| 2610 | act->output.parent = &act->node; |
| 2611 | LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), "output", 0, &act->output.name)); |
| 2612 | } |
| 2613 | |
| 2614 | return LY_SUCCESS; |
| 2615 | } |
| 2616 | |
| 2617 | /** |
| 2618 | * @brief Parse the notification statement. |
| 2619 | * |
| 2620 | * @param[in] ctx parser context. |
| 2621 | * @param[in] stmt Source statement data from the parsed extension instance. |
| 2622 | * @param[in] parent Parent node to connect to (not into). |
| 2623 | * @param[in,out] notifs Notifications to add to. |
| 2624 | * |
| 2625 | * @return LY_ERR values. |
| 2626 | */ |
| 2627 | static LY_ERR |
Michal Vasko | d0625d7 | 2022-10-06 15:02:50 +0200 | [diff] [blame] | 2628 | lysp_stmt_notif(struct lysp_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_node *parent, |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 2629 | struct lysp_node_notif **notifs) |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 2630 | { |
| 2631 | struct lysp_node_notif *notif; |
| 2632 | |
| 2633 | LY_CHECK_RET(lysp_stmt_validate_value(ctx, Y_IDENTIF_ARG, stmt->arg)); |
| 2634 | |
| 2635 | LY_LIST_NEW_RET(PARSER_CTX(ctx), notifs, notif, next, LY_EMEM); |
| 2636 | |
| 2637 | LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), stmt->arg, 0, ¬if->name)); |
| 2638 | notif->nodetype = LYS_NOTIF; |
| 2639 | notif->parent = parent; |
| 2640 | |
| 2641 | for (const struct lysp_stmt *child = stmt->child; child; child = child->next) { |
| 2642 | switch (child->kw) { |
| 2643 | case LY_STMT_DESCRIPTION: |
| 2644 | LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, ¬if->dsc, Y_STR_ARG, ¬if->exts)); |
| 2645 | break; |
| 2646 | case LY_STMT_IF_FEATURE: |
| 2647 | LY_CHECK_RET(lysp_stmt_qnames(ctx, child, ¬if->iffeatures, Y_STR_ARG, ¬if->exts)); |
| 2648 | break; |
| 2649 | case LY_STMT_REFERENCE: |
| 2650 | LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, ¬if->ref, Y_STR_ARG, ¬if->exts)); |
| 2651 | break; |
| 2652 | case LY_STMT_STATUS: |
| 2653 | LY_CHECK_RET(lysp_stmt_status(ctx, child, ¬if->flags, ¬if->exts)); |
| 2654 | break; |
| 2655 | |
| 2656 | case LY_STMT_ANYDATA: |
| 2657 | PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "notification"); |
| 2658 | /* fall through */ |
| 2659 | case LY_STMT_ANYXML: |
| 2660 | LY_CHECK_RET(lysp_stmt_any(ctx, child, ¬if->node, ¬if->child)); |
| 2661 | break; |
| 2662 | case LY_STMT_CHOICE: |
| 2663 | LY_CHECK_RET(lysp_stmt_case(ctx, child, ¬if->node, ¬if->child)); |
| 2664 | break; |
| 2665 | case LY_STMT_CONTAINER: |
| 2666 | LY_CHECK_RET(lysp_stmt_container(ctx, child, ¬if->node, ¬if->child)); |
| 2667 | break; |
| 2668 | case LY_STMT_LEAF: |
| 2669 | LY_CHECK_RET(lysp_stmt_leaf(ctx, child, ¬if->node, ¬if->child)); |
| 2670 | break; |
| 2671 | case LY_STMT_LEAF_LIST: |
| 2672 | LY_CHECK_RET(lysp_stmt_leaflist(ctx, child, ¬if->node, ¬if->child)); |
| 2673 | break; |
| 2674 | case LY_STMT_LIST: |
| 2675 | LY_CHECK_RET(lysp_stmt_list(ctx, child, ¬if->node, ¬if->child)); |
| 2676 | break; |
| 2677 | case LY_STMT_USES: |
| 2678 | LY_CHECK_RET(lysp_stmt_uses(ctx, child, ¬if->node, ¬if->child)); |
| 2679 | break; |
| 2680 | |
| 2681 | case LY_STMT_MUST: |
| 2682 | PARSER_CHECK_STMTVER2_RET(ctx, "must", "notification"); |
| 2683 | LY_CHECK_RET(lysp_stmt_restrs(ctx, child, ¬if->musts)); |
| 2684 | break; |
| 2685 | case LY_STMT_TYPEDEF: |
| 2686 | LY_CHECK_RET(lysp_stmt_typedef(ctx, child, ¬if->node, ¬if->typedefs)); |
| 2687 | break; |
| 2688 | case LY_STMT_GROUPING: |
| 2689 | LY_CHECK_RET(lysp_stmt_grouping(ctx, child, ¬if->node, ¬if->groupings)); |
| 2690 | break; |
| 2691 | case LY_STMT_EXTENSION_INSTANCE: |
Radek Krejci | 39b7fc2 | 2021-02-26 23:29:18 +0100 | [diff] [blame] | 2692 | LY_CHECK_RET(lysp_stmt_ext(ctx, child, LY_STMT_NOTIFICATION, 0, ¬if->exts)); |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 2693 | break; |
| 2694 | default: |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 2695 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(child->kw), "notification"); |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 2696 | return LY_EVALID; |
| 2697 | } |
| 2698 | } |
| 2699 | |
| 2700 | return LY_SUCCESS; |
| 2701 | } |
| 2702 | |
| 2703 | /** |
| 2704 | * @brief Parse the grouping statement. |
| 2705 | * |
| 2706 | * @param[in] ctx parser context. |
| 2707 | * @param[in] stmt Source statement data from the parsed extension instance. |
| 2708 | * @param[in] parent Parent node to connect to (not into). |
| 2709 | * @param[in,out] groupings Groupings to add to. |
| 2710 | * |
| 2711 | * @return LY_ERR values. |
| 2712 | */ |
| 2713 | static LY_ERR |
Michal Vasko | d0625d7 | 2022-10-06 15:02:50 +0200 | [diff] [blame] | 2714 | lysp_stmt_grouping(struct lysp_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_node *parent, |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 2715 | struct lysp_node_grp **groupings) |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 2716 | { |
| 2717 | struct lysp_node_grp *grp; |
| 2718 | |
| 2719 | LY_CHECK_RET(lysp_stmt_validate_value(ctx, Y_IDENTIF_ARG, stmt->arg)); |
| 2720 | |
| 2721 | LY_LIST_NEW_RET(PARSER_CTX(ctx), groupings, grp, next, LY_EMEM); |
| 2722 | |
| 2723 | LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), stmt->arg, 0, &grp->name)); |
| 2724 | grp->nodetype = LYS_GROUPING; |
| 2725 | grp->parent = parent; |
| 2726 | |
| 2727 | for (const struct lysp_stmt *child = stmt->child; child; child = child->next) { |
| 2728 | switch (child->kw) { |
| 2729 | case LY_STMT_DESCRIPTION: |
| 2730 | LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &grp->dsc, Y_STR_ARG, &grp->exts)); |
| 2731 | break; |
| 2732 | case LY_STMT_REFERENCE: |
| 2733 | LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &grp->ref, Y_STR_ARG, &grp->exts)); |
| 2734 | break; |
| 2735 | case LY_STMT_STATUS: |
| 2736 | LY_CHECK_RET(lysp_stmt_status(ctx, child, &grp->flags, &grp->exts)); |
| 2737 | break; |
| 2738 | |
| 2739 | case LY_STMT_ANYDATA: |
| 2740 | PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "grouping"); |
| 2741 | /* fall through */ |
| 2742 | case LY_STMT_ANYXML: |
| 2743 | LY_CHECK_RET(lysp_stmt_any(ctx, child, &grp->node, &grp->child)); |
| 2744 | break; |
| 2745 | case LY_STMT_CHOICE: |
| 2746 | LY_CHECK_RET(lysp_stmt_choice(ctx, child, &grp->node, &grp->child)); |
| 2747 | break; |
| 2748 | case LY_STMT_CONTAINER: |
| 2749 | LY_CHECK_RET(lysp_stmt_container(ctx, child, &grp->node, &grp->child)); |
| 2750 | break; |
| 2751 | case LY_STMT_LEAF: |
| 2752 | LY_CHECK_RET(lysp_stmt_leaf(ctx, child, &grp->node, &grp->child)); |
| 2753 | break; |
| 2754 | case LY_STMT_LEAF_LIST: |
| 2755 | LY_CHECK_RET(lysp_stmt_leaflist(ctx, child, &grp->node, &grp->child)); |
| 2756 | break; |
| 2757 | case LY_STMT_LIST: |
| 2758 | LY_CHECK_RET(lysp_stmt_list(ctx, child, &grp->node, &grp->child)); |
| 2759 | break; |
| 2760 | case LY_STMT_USES: |
| 2761 | LY_CHECK_RET(lysp_stmt_uses(ctx, child, &grp->node, &grp->child)); |
| 2762 | break; |
| 2763 | |
| 2764 | case LY_STMT_TYPEDEF: |
| 2765 | LY_CHECK_RET(lysp_stmt_typedef(ctx, child, &grp->node, &grp->typedefs)); |
| 2766 | break; |
| 2767 | case LY_STMT_ACTION: |
| 2768 | PARSER_CHECK_STMTVER2_RET(ctx, "action", "grouping"); |
| 2769 | LY_CHECK_RET(lysp_stmt_action(ctx, child, &grp->node, &grp->actions)); |
| 2770 | break; |
| 2771 | case LY_STMT_GROUPING: |
| 2772 | LY_CHECK_RET(lysp_stmt_grouping(ctx, child, &grp->node, &grp->groupings)); |
| 2773 | break; |
| 2774 | case LY_STMT_NOTIFICATION: |
| 2775 | PARSER_CHECK_STMTVER2_RET(ctx, "notification", "grouping"); |
| 2776 | LY_CHECK_RET(lysp_stmt_notif(ctx, child, &grp->node, &grp->notifs)); |
| 2777 | break; |
| 2778 | case LY_STMT_EXTENSION_INSTANCE: |
Radek Krejci | 39b7fc2 | 2021-02-26 23:29:18 +0100 | [diff] [blame] | 2779 | LY_CHECK_RET(lysp_stmt_ext(ctx, child, LY_STMT_GROUPING, 0, &grp->exts)); |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 2780 | break; |
| 2781 | default: |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 2782 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(child->kw), "grouping"); |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 2783 | return LY_EVALID; |
| 2784 | } |
| 2785 | } |
| 2786 | |
| 2787 | return LY_SUCCESS; |
| 2788 | } |
| 2789 | |
| 2790 | /** |
| 2791 | * @brief Parse the augment statement. |
| 2792 | * |
| 2793 | * @param[in] ctx parser context. |
| 2794 | * @param[in] stmt Source statement data from the parsed extension instance. |
| 2795 | * @param[in] parent Parent node to connect to (not into). |
| 2796 | * @param[in,out] augments Augments to add to. |
| 2797 | * |
| 2798 | * @return LY_ERR values. |
| 2799 | */ |
| 2800 | static LY_ERR |
Michal Vasko | d0625d7 | 2022-10-06 15:02:50 +0200 | [diff] [blame] | 2801 | lysp_stmt_augment(struct lysp_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_node *parent, |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 2802 | struct lysp_node_augment **augments) |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 2803 | { |
| 2804 | struct lysp_node_augment *aug; |
| 2805 | |
| 2806 | LY_CHECK_RET(lysp_stmt_validate_value(ctx, Y_STR_ARG, stmt->arg)); |
| 2807 | |
| 2808 | LY_LIST_NEW_RET(PARSER_CTX(ctx), augments, aug, next, LY_EMEM); |
| 2809 | |
| 2810 | LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), stmt->arg, 0, &aug->nodeid)); |
| 2811 | aug->nodetype = LYS_AUGMENT; |
| 2812 | aug->parent = parent; |
| 2813 | |
| 2814 | for (const struct lysp_stmt *child = stmt->child; child; child = child->next) { |
| 2815 | switch (child->kw) { |
| 2816 | case LY_STMT_DESCRIPTION: |
| 2817 | LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &aug->dsc, Y_STR_ARG, &aug->exts)); |
| 2818 | break; |
| 2819 | case LY_STMT_IF_FEATURE: |
| 2820 | LY_CHECK_RET(lysp_stmt_qnames(ctx, child, &aug->iffeatures, Y_STR_ARG, &aug->exts)); |
| 2821 | break; |
| 2822 | case LY_STMT_REFERENCE: |
| 2823 | LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &aug->ref, Y_STR_ARG, &aug->exts)); |
| 2824 | break; |
| 2825 | case LY_STMT_STATUS: |
| 2826 | LY_CHECK_RET(lysp_stmt_status(ctx, child, &aug->flags, &aug->exts)); |
| 2827 | break; |
| 2828 | case LY_STMT_WHEN: |
| 2829 | LY_CHECK_RET(lysp_stmt_when(ctx, child, &aug->when)); |
| 2830 | break; |
| 2831 | |
| 2832 | case LY_STMT_ANYDATA: |
| 2833 | PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "augment"); |
| 2834 | /* fall through */ |
| 2835 | case LY_STMT_ANYXML: |
| 2836 | LY_CHECK_RET(lysp_stmt_any(ctx, child, &aug->node, &aug->child)); |
| 2837 | break; |
| 2838 | case LY_STMT_CASE: |
| 2839 | LY_CHECK_RET(lysp_stmt_case(ctx, child, &aug->node, &aug->child)); |
| 2840 | break; |
| 2841 | case LY_STMT_CHOICE: |
| 2842 | LY_CHECK_RET(lysp_stmt_choice(ctx, child, &aug->node, &aug->child)); |
| 2843 | break; |
| 2844 | case LY_STMT_CONTAINER: |
| 2845 | LY_CHECK_RET(lysp_stmt_container(ctx, child, &aug->node, &aug->child)); |
| 2846 | break; |
| 2847 | case LY_STMT_LEAF: |
| 2848 | LY_CHECK_RET(lysp_stmt_leaf(ctx, child, &aug->node, &aug->child)); |
| 2849 | break; |
| 2850 | case LY_STMT_LEAF_LIST: |
| 2851 | LY_CHECK_RET(lysp_stmt_leaflist(ctx, child, &aug->node, &aug->child)); |
| 2852 | break; |
| 2853 | case LY_STMT_LIST: |
| 2854 | LY_CHECK_RET(lysp_stmt_list(ctx, child, &aug->node, &aug->child)); |
| 2855 | break; |
| 2856 | case LY_STMT_USES: |
| 2857 | LY_CHECK_RET(lysp_stmt_uses(ctx, child, &aug->node, &aug->child)); |
| 2858 | break; |
| 2859 | |
| 2860 | case LY_STMT_ACTION: |
| 2861 | PARSER_CHECK_STMTVER2_RET(ctx, "action", "augment"); |
| 2862 | LY_CHECK_RET(lysp_stmt_action(ctx, child, &aug->node, &aug->actions)); |
| 2863 | break; |
| 2864 | case LY_STMT_NOTIFICATION: |
| 2865 | PARSER_CHECK_STMTVER2_RET(ctx, "notification", "augment"); |
| 2866 | LY_CHECK_RET(lysp_stmt_notif(ctx, child, &aug->node, &aug->notifs)); |
| 2867 | break; |
| 2868 | case LY_STMT_EXTENSION_INSTANCE: |
Radek Krejci | 39b7fc2 | 2021-02-26 23:29:18 +0100 | [diff] [blame] | 2869 | LY_CHECK_RET(lysp_stmt_ext(ctx, child, LY_STMT_AUGMENT, 0, &aug->exts)); |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 2870 | break; |
| 2871 | default: |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 2872 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(child->kw), "augment"); |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 2873 | return LY_EVALID; |
| 2874 | } |
| 2875 | } |
| 2876 | |
| 2877 | return LY_SUCCESS; |
| 2878 | } |
| 2879 | |
| 2880 | /** |
| 2881 | * @brief Parse the uses statement. |
| 2882 | * |
| 2883 | * @param[in] ctx parser context. |
| 2884 | * @param[in] stmt Source statement data from the parsed extension instance. |
| 2885 | * @param[in] parent Parent node to connect to (not into). |
| 2886 | * @param[in,out] siblings Siblings to add to. |
| 2887 | * |
| 2888 | * @return LY_ERR values. |
| 2889 | */ |
| 2890 | static LY_ERR |
Michal Vasko | d0625d7 | 2022-10-06 15:02:50 +0200 | [diff] [blame] | 2891 | lysp_stmt_uses(struct lysp_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_node *parent, |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 2892 | struct lysp_node **siblings) |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 2893 | { |
| 2894 | struct lysp_node_uses *uses; |
| 2895 | |
| 2896 | LY_CHECK_RET(lysp_stmt_validate_value(ctx, Y_PREF_IDENTIF_ARG, stmt->arg)); |
| 2897 | |
| 2898 | /* create uses structure */ |
| 2899 | LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, uses, next, LY_EMEM); |
| 2900 | |
| 2901 | LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), stmt->arg, 0, &uses->name)); |
| 2902 | uses->nodetype = LYS_USES; |
| 2903 | uses->parent = parent; |
| 2904 | |
| 2905 | /* parse substatements */ |
| 2906 | for (const struct lysp_stmt *child = stmt->child; child; child = child->next) { |
| 2907 | switch (child->kw) { |
| 2908 | case LY_STMT_DESCRIPTION: |
| 2909 | LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &uses->dsc, Y_STR_ARG, &uses->exts)); |
| 2910 | break; |
| 2911 | case LY_STMT_IF_FEATURE: |
| 2912 | LY_CHECK_RET(lysp_stmt_qnames(ctx, child, &uses->iffeatures, Y_STR_ARG, &uses->exts)); |
| 2913 | break; |
| 2914 | case LY_STMT_REFERENCE: |
| 2915 | LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &uses->ref, Y_STR_ARG, &uses->exts)); |
| 2916 | break; |
| 2917 | case LY_STMT_STATUS: |
| 2918 | LY_CHECK_RET(lysp_stmt_status(ctx, child, &uses->flags, &uses->exts)); |
| 2919 | break; |
| 2920 | case LY_STMT_WHEN: |
| 2921 | LY_CHECK_RET(lysp_stmt_when(ctx, child, &uses->when)); |
| 2922 | break; |
| 2923 | |
| 2924 | case LY_STMT_REFINE: |
| 2925 | LY_CHECK_RET(lysp_stmt_refine(ctx, child, &uses->refines)); |
| 2926 | break; |
| 2927 | case LY_STMT_AUGMENT: |
| 2928 | LY_CHECK_RET(lysp_stmt_augment(ctx, child, &uses->node, &uses->augments)); |
| 2929 | break; |
| 2930 | case LY_STMT_EXTENSION_INSTANCE: |
Radek Krejci | 39b7fc2 | 2021-02-26 23:29:18 +0100 | [diff] [blame] | 2931 | LY_CHECK_RET(lysp_stmt_ext(ctx, child, LY_STMT_USES, 0, &uses->exts)); |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 2932 | break; |
| 2933 | default: |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 2934 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(child->kw), "uses"); |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 2935 | return LY_EVALID; |
| 2936 | } |
| 2937 | } |
| 2938 | |
| 2939 | return LY_SUCCESS; |
| 2940 | } |
| 2941 | |
| 2942 | /** |
| 2943 | * @brief Parse the case statement. |
| 2944 | * |
| 2945 | * @param[in] ctx parser context. |
| 2946 | * @param[in] stmt Source statement data from the parsed extension instance. |
| 2947 | * @param[in] parent Parent node to connect to (not into). |
| 2948 | * @param[in,out] siblings Siblings to add to. |
| 2949 | * |
| 2950 | * @return LY_ERR values. |
| 2951 | */ |
| 2952 | static LY_ERR |
Michal Vasko | d0625d7 | 2022-10-06 15:02:50 +0200 | [diff] [blame] | 2953 | lysp_stmt_case(struct lysp_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_node *parent, |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 2954 | struct lysp_node **siblings) |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 2955 | { |
| 2956 | struct lysp_node_case *cas; |
| 2957 | |
| 2958 | LY_CHECK_RET(lysp_stmt_validate_value(ctx, Y_IDENTIF_ARG, stmt->arg)); |
| 2959 | |
| 2960 | /* create new case structure */ |
| 2961 | LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, cas, next, LY_EMEM); |
| 2962 | |
| 2963 | LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), stmt->arg, 0, &cas->name)); |
| 2964 | cas->nodetype = LYS_CASE; |
| 2965 | cas->parent = parent; |
| 2966 | |
| 2967 | /* parse substatements */ |
| 2968 | for (const struct lysp_stmt *child = stmt->child; child; child = child->next) { |
| 2969 | switch (child->kw) { |
| 2970 | case LY_STMT_DESCRIPTION: |
| 2971 | LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &cas->dsc, Y_STR_ARG, &cas->exts)); |
| 2972 | break; |
| 2973 | case LY_STMT_IF_FEATURE: |
| 2974 | LY_CHECK_RET(lysp_stmt_qnames(ctx, child, &cas->iffeatures, Y_STR_ARG, &cas->exts)); |
| 2975 | break; |
| 2976 | case LY_STMT_REFERENCE: |
| 2977 | LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &cas->ref, Y_STR_ARG, &cas->exts)); |
| 2978 | break; |
| 2979 | case LY_STMT_STATUS: |
| 2980 | LY_CHECK_RET(lysp_stmt_status(ctx, child, &cas->flags, &cas->exts)); |
| 2981 | break; |
| 2982 | case LY_STMT_WHEN: |
| 2983 | LY_CHECK_RET(lysp_stmt_when(ctx, child, &cas->when)); |
| 2984 | break; |
| 2985 | |
| 2986 | case LY_STMT_ANYDATA: |
| 2987 | PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "case"); |
| 2988 | /* fall through */ |
| 2989 | case LY_STMT_ANYXML: |
| 2990 | LY_CHECK_RET(lysp_stmt_any(ctx, child, &cas->node, &cas->child)); |
| 2991 | break; |
| 2992 | case LY_STMT_CHOICE: |
| 2993 | LY_CHECK_RET(lysp_stmt_choice(ctx, child, &cas->node, &cas->child)); |
| 2994 | break; |
| 2995 | case LY_STMT_CONTAINER: |
| 2996 | LY_CHECK_RET(lysp_stmt_container(ctx, child, &cas->node, &cas->child)); |
| 2997 | break; |
| 2998 | case LY_STMT_LEAF: |
| 2999 | LY_CHECK_RET(lysp_stmt_leaf(ctx, child, &cas->node, &cas->child)); |
| 3000 | break; |
| 3001 | case LY_STMT_LEAF_LIST: |
| 3002 | LY_CHECK_RET(lysp_stmt_leaflist(ctx, child, &cas->node, &cas->child)); |
| 3003 | break; |
| 3004 | case LY_STMT_LIST: |
| 3005 | LY_CHECK_RET(lysp_stmt_list(ctx, child, &cas->node, &cas->child)); |
| 3006 | break; |
| 3007 | case LY_STMT_USES: |
| 3008 | LY_CHECK_RET(lysp_stmt_uses(ctx, child, &cas->node, &cas->child)); |
| 3009 | break; |
| 3010 | case LY_STMT_EXTENSION_INSTANCE: |
Radek Krejci | 39b7fc2 | 2021-02-26 23:29:18 +0100 | [diff] [blame] | 3011 | LY_CHECK_RET(lysp_stmt_ext(ctx, child, LY_STMT_CASE, 0, &cas->exts)); |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 3012 | break; |
| 3013 | default: |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 3014 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(child->kw), "case"); |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 3015 | return LY_EVALID; |
| 3016 | } |
| 3017 | } |
| 3018 | return LY_SUCCESS; |
| 3019 | } |
| 3020 | |
| 3021 | /** |
| 3022 | * @brief Parse the choice statement. |
| 3023 | * |
| 3024 | * @param[in] ctx parser context. |
| 3025 | * @param[in] stmt Source statement data from the parsed extension instance. |
| 3026 | * @param[in] parent Parent node to connect to (not into). |
| 3027 | * @param[in,out] siblings Siblings to add to. |
| 3028 | * |
| 3029 | * @return LY_ERR values. |
| 3030 | */ |
| 3031 | static LY_ERR |
Michal Vasko | d0625d7 | 2022-10-06 15:02:50 +0200 | [diff] [blame] | 3032 | lysp_stmt_choice(struct lysp_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_node *parent, |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 3033 | struct lysp_node **siblings) |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 3034 | { |
| 3035 | struct lysp_node_choice *choice; |
| 3036 | |
| 3037 | LY_CHECK_RET(lysp_stmt_validate_value(ctx, Y_IDENTIF_ARG, stmt->arg)); |
| 3038 | |
| 3039 | /* create new choice structure */ |
| 3040 | LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, choice, next, LY_EMEM); |
| 3041 | |
| 3042 | LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), stmt->arg, 0, &choice->name)); |
| 3043 | choice->nodetype = LYS_CHOICE; |
| 3044 | choice->parent = parent; |
| 3045 | |
| 3046 | /* parse substatements */ |
| 3047 | for (const struct lysp_stmt *child = stmt->child; child; child = child->next) { |
| 3048 | switch (child->kw) { |
| 3049 | case LY_STMT_CONFIG: |
| 3050 | LY_CHECK_RET(lysp_stmt_config(ctx, child, &choice->flags, &choice->exts)); |
| 3051 | break; |
| 3052 | case LY_STMT_DESCRIPTION: |
| 3053 | LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &choice->dsc, Y_STR_ARG, &choice->exts)); |
| 3054 | break; |
| 3055 | case LY_STMT_IF_FEATURE: |
| 3056 | LY_CHECK_RET(lysp_stmt_qnames(ctx, child, &choice->iffeatures, Y_STR_ARG, &choice->exts)); |
| 3057 | break; |
| 3058 | case LY_STMT_MANDATORY: |
| 3059 | LY_CHECK_RET(lysp_stmt_mandatory(ctx, child, &choice->flags, &choice->exts)); |
| 3060 | break; |
| 3061 | case LY_STMT_REFERENCE: |
| 3062 | LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &choice->ref, Y_STR_ARG, &choice->exts)); |
| 3063 | break; |
| 3064 | case LY_STMT_STATUS: |
| 3065 | LY_CHECK_RET(lysp_stmt_status(ctx, child, &choice->flags, &choice->exts)); |
| 3066 | break; |
| 3067 | case LY_STMT_WHEN: |
| 3068 | LY_CHECK_RET(lysp_stmt_when(ctx, child, &choice->when)); |
| 3069 | break; |
| 3070 | case LY_STMT_DEFAULT: |
| 3071 | LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &choice->dflt.str, Y_PREF_IDENTIF_ARG, &choice->exts)); |
Michal Vasko | 8a67eff | 2021-12-07 14:04:47 +0100 | [diff] [blame] | 3072 | choice->dflt.mod = PARSER_CUR_PMOD(ctx); |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 3073 | break; |
| 3074 | case LY_STMT_ANYDATA: |
| 3075 | PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "choice"); |
| 3076 | /* fall through */ |
| 3077 | case LY_STMT_ANYXML: |
| 3078 | LY_CHECK_RET(lysp_stmt_any(ctx, child, &choice->node, &choice->child)); |
| 3079 | break; |
| 3080 | case LY_STMT_CASE: |
| 3081 | LY_CHECK_RET(lysp_stmt_case(ctx, child, &choice->node, &choice->child)); |
| 3082 | break; |
| 3083 | case LY_STMT_CHOICE: |
| 3084 | PARSER_CHECK_STMTVER2_RET(ctx, "choice", "choice"); |
| 3085 | LY_CHECK_RET(lysp_stmt_choice(ctx, child, &choice->node, &choice->child)); |
| 3086 | break; |
| 3087 | case LY_STMT_CONTAINER: |
| 3088 | LY_CHECK_RET(lysp_stmt_container(ctx, child, &choice->node, &choice->child)); |
| 3089 | break; |
| 3090 | case LY_STMT_LEAF: |
| 3091 | LY_CHECK_RET(lysp_stmt_leaf(ctx, child, &choice->node, &choice->child)); |
| 3092 | break; |
| 3093 | case LY_STMT_LEAF_LIST: |
| 3094 | LY_CHECK_RET(lysp_stmt_leaflist(ctx, child, &choice->node, &choice->child)); |
| 3095 | break; |
| 3096 | case LY_STMT_LIST: |
| 3097 | LY_CHECK_RET(lysp_stmt_list(ctx, child, &choice->node, &choice->child)); |
| 3098 | break; |
| 3099 | case LY_STMT_EXTENSION_INSTANCE: |
Radek Krejci | 39b7fc2 | 2021-02-26 23:29:18 +0100 | [diff] [blame] | 3100 | LY_CHECK_RET(lysp_stmt_ext(ctx, child, LY_STMT_CHOICE, 0, &choice->exts)); |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 3101 | break; |
| 3102 | default: |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 3103 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(child->kw), "choice"); |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 3104 | return LY_EVALID; |
| 3105 | } |
| 3106 | } |
| 3107 | return LY_SUCCESS; |
| 3108 | } |
| 3109 | |
| 3110 | /** |
| 3111 | * @brief Parse the container statement. |
| 3112 | * |
| 3113 | * @param[in] ctx parser context. |
| 3114 | * @param[in] stmt Source statement data from the parsed extension instance. |
| 3115 | * @param[in] parent Parent node to connect to (not into). |
| 3116 | * @param[in,out] siblings Siblings to add to. |
| 3117 | * |
| 3118 | * @return LY_ERR values. |
| 3119 | */ |
| 3120 | static LY_ERR |
Michal Vasko | d0625d7 | 2022-10-06 15:02:50 +0200 | [diff] [blame] | 3121 | lysp_stmt_container(struct lysp_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_node *parent, |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 3122 | struct lysp_node **siblings) |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 3123 | { |
| 3124 | struct lysp_node_container *cont; |
| 3125 | |
| 3126 | LY_CHECK_RET(lysp_stmt_validate_value(ctx, Y_IDENTIF_ARG, stmt->arg)); |
| 3127 | |
| 3128 | /* create new container structure */ |
| 3129 | LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, cont, next, LY_EMEM); |
| 3130 | |
| 3131 | LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), stmt->arg, 0, &cont->name)); |
| 3132 | cont->nodetype = LYS_CONTAINER; |
| 3133 | cont->parent = parent; |
| 3134 | |
| 3135 | /* parse substatements */ |
| 3136 | for (const struct lysp_stmt *child = stmt->child; child; child = child->next) { |
| 3137 | switch (child->kw) { |
| 3138 | case LY_STMT_CONFIG: |
| 3139 | LY_CHECK_RET(lysp_stmt_config(ctx, child, &cont->flags, &cont->exts)); |
| 3140 | break; |
| 3141 | case LY_STMT_DESCRIPTION: |
| 3142 | LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &cont->dsc, Y_STR_ARG, &cont->exts)); |
| 3143 | break; |
| 3144 | case LY_STMT_IF_FEATURE: |
| 3145 | LY_CHECK_RET(lysp_stmt_qnames(ctx, child, &cont->iffeatures, Y_STR_ARG, &cont->exts)); |
| 3146 | break; |
| 3147 | case LY_STMT_REFERENCE: |
| 3148 | LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &cont->ref, Y_STR_ARG, &cont->exts)); |
| 3149 | break; |
| 3150 | case LY_STMT_STATUS: |
| 3151 | LY_CHECK_RET(lysp_stmt_status(ctx, child, &cont->flags, &cont->exts)); |
| 3152 | break; |
| 3153 | case LY_STMT_WHEN: |
| 3154 | LY_CHECK_RET(lysp_stmt_when(ctx, child, &cont->when)); |
| 3155 | break; |
| 3156 | case LY_STMT_PRESENCE: |
| 3157 | LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &cont->presence, Y_STR_ARG, &cont->exts)); |
| 3158 | break; |
| 3159 | case LY_STMT_ANYDATA: |
| 3160 | PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "container"); |
| 3161 | /* fall through */ |
| 3162 | case LY_STMT_ANYXML: |
| 3163 | LY_CHECK_RET(lysp_stmt_any(ctx, child, &cont->node, &cont->child)); |
| 3164 | break; |
| 3165 | case LY_STMT_CHOICE: |
| 3166 | LY_CHECK_RET(lysp_stmt_choice(ctx, child, &cont->node, &cont->child)); |
| 3167 | break; |
| 3168 | case LY_STMT_CONTAINER: |
| 3169 | LY_CHECK_RET(lysp_stmt_container(ctx, child, &cont->node, &cont->child)); |
| 3170 | break; |
| 3171 | case LY_STMT_LEAF: |
| 3172 | LY_CHECK_RET(lysp_stmt_leaf(ctx, child, &cont->node, &cont->child)); |
| 3173 | break; |
| 3174 | case LY_STMT_LEAF_LIST: |
| 3175 | LY_CHECK_RET(lysp_stmt_leaflist(ctx, child, &cont->node, &cont->child)); |
| 3176 | break; |
| 3177 | case LY_STMT_LIST: |
| 3178 | LY_CHECK_RET(lysp_stmt_list(ctx, child, &cont->node, &cont->child)); |
| 3179 | break; |
| 3180 | case LY_STMT_USES: |
| 3181 | LY_CHECK_RET(lysp_stmt_uses(ctx, child, &cont->node, &cont->child)); |
| 3182 | break; |
| 3183 | |
| 3184 | case LY_STMT_TYPEDEF: |
| 3185 | LY_CHECK_RET(lysp_stmt_typedef(ctx, child, &cont->node, &cont->typedefs)); |
| 3186 | break; |
| 3187 | case LY_STMT_MUST: |
| 3188 | LY_CHECK_RET(lysp_stmt_restrs(ctx, child, &cont->musts)); |
| 3189 | break; |
| 3190 | case LY_STMT_ACTION: |
| 3191 | PARSER_CHECK_STMTVER2_RET(ctx, "action", "container"); |
| 3192 | LY_CHECK_RET(lysp_stmt_action(ctx, child, &cont->node, &cont->actions)); |
| 3193 | break; |
| 3194 | case LY_STMT_GROUPING: |
| 3195 | LY_CHECK_RET(lysp_stmt_grouping(ctx, child, &cont->node, &cont->groupings)); |
| 3196 | break; |
| 3197 | case LY_STMT_NOTIFICATION: |
| 3198 | PARSER_CHECK_STMTVER2_RET(ctx, "notification", "container"); |
| 3199 | LY_CHECK_RET(lysp_stmt_notif(ctx, child, &cont->node, &cont->notifs)); |
| 3200 | break; |
| 3201 | case LY_STMT_EXTENSION_INSTANCE: |
Radek Krejci | 39b7fc2 | 2021-02-26 23:29:18 +0100 | [diff] [blame] | 3202 | LY_CHECK_RET(lysp_stmt_ext(ctx, child, LY_STMT_CONTAINER, 0, &cont->exts)); |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 3203 | break; |
| 3204 | default: |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 3205 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(child->kw), "container"); |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 3206 | return LY_EVALID; |
| 3207 | } |
| 3208 | } |
| 3209 | |
| 3210 | return LY_SUCCESS; |
| 3211 | } |
| 3212 | |
| 3213 | /** |
| 3214 | * @brief Parse the list statement. |
| 3215 | * |
| 3216 | * @param[in] ctx parser context. |
| 3217 | * @param[in] stmt Source statement data from the parsed extension instance. |
| 3218 | * @param[in] parent Parent node to connect to (not into). |
| 3219 | * @param[in,out] siblings Siblings to add to. |
| 3220 | * |
| 3221 | * @return LY_ERR values. |
| 3222 | */ |
| 3223 | static LY_ERR |
Michal Vasko | d0625d7 | 2022-10-06 15:02:50 +0200 | [diff] [blame] | 3224 | lysp_stmt_list(struct lysp_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_node *parent, |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 3225 | struct lysp_node **siblings) |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 3226 | { |
| 3227 | struct lysp_node_list *list; |
| 3228 | |
| 3229 | LY_CHECK_RET(lysp_stmt_validate_value(ctx, Y_IDENTIF_ARG, stmt->arg)); |
| 3230 | |
| 3231 | /* create new list structure */ |
| 3232 | LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, list, next, LY_EMEM); |
| 3233 | |
| 3234 | LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), stmt->arg, 0, &list->name)); |
| 3235 | list->nodetype = LYS_LIST; |
| 3236 | list->parent = parent; |
| 3237 | |
| 3238 | /* parse substatements */ |
| 3239 | for (const struct lysp_stmt *child = stmt->child; child; child = child->next) { |
| 3240 | switch (child->kw) { |
| 3241 | case LY_STMT_CONFIG: |
| 3242 | LY_CHECK_RET(lysp_stmt_config(ctx, child, &list->flags, &list->exts)); |
| 3243 | break; |
| 3244 | case LY_STMT_DESCRIPTION: |
| 3245 | LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &list->dsc, Y_STR_ARG, &list->exts)); |
| 3246 | break; |
| 3247 | case LY_STMT_IF_FEATURE: |
| 3248 | LY_CHECK_RET(lysp_stmt_qnames(ctx, child, &list->iffeatures, Y_STR_ARG, &list->exts)); |
| 3249 | break; |
| 3250 | case LY_STMT_REFERENCE: |
| 3251 | LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &list->ref, Y_STR_ARG, &list->exts)); |
| 3252 | break; |
| 3253 | case LY_STMT_STATUS: |
| 3254 | LY_CHECK_RET(lysp_stmt_status(ctx, child, &list->flags, &list->exts)); |
| 3255 | break; |
| 3256 | case LY_STMT_WHEN: |
| 3257 | LY_CHECK_RET(lysp_stmt_when(ctx, child, &list->when)); |
| 3258 | break; |
| 3259 | case LY_STMT_KEY: |
| 3260 | LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &list->key, Y_STR_ARG, &list->exts)); |
| 3261 | break; |
| 3262 | case LY_STMT_MAX_ELEMENTS: |
| 3263 | LY_CHECK_RET(lysp_stmt_maxelements(ctx, child, &list->max, &list->flags, &list->exts)); |
| 3264 | break; |
| 3265 | case LY_STMT_MIN_ELEMENTS: |
| 3266 | LY_CHECK_RET(lysp_stmt_minelements(ctx, child, &list->min, &list->flags, &list->exts)); |
| 3267 | break; |
| 3268 | case LY_STMT_ORDERED_BY: |
| 3269 | LY_CHECK_RET(lysp_stmt_orderedby(ctx, child, &list->flags, &list->exts)); |
| 3270 | break; |
| 3271 | case LY_STMT_UNIQUE: |
| 3272 | LY_CHECK_RET(lysp_stmt_qnames(ctx, child, &list->uniques, Y_STR_ARG, &list->exts)); |
| 3273 | break; |
| 3274 | |
| 3275 | case LY_STMT_ANYDATA: |
| 3276 | PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "list"); |
| 3277 | /* fall through */ |
| 3278 | case LY_STMT_ANYXML: |
| 3279 | LY_CHECK_RET(lysp_stmt_any(ctx, child, &list->node, &list->child)); |
| 3280 | break; |
| 3281 | case LY_STMT_CHOICE: |
| 3282 | LY_CHECK_RET(lysp_stmt_choice(ctx, child, &list->node, &list->child)); |
| 3283 | break; |
| 3284 | case LY_STMT_CONTAINER: |
| 3285 | LY_CHECK_RET(lysp_stmt_container(ctx, child, &list->node, &list->child)); |
| 3286 | break; |
| 3287 | case LY_STMT_LEAF: |
| 3288 | LY_CHECK_RET(lysp_stmt_leaf(ctx, child, &list->node, &list->child)); |
| 3289 | break; |
| 3290 | case LY_STMT_LEAF_LIST: |
| 3291 | LY_CHECK_RET(lysp_stmt_leaflist(ctx, child, &list->node, &list->child)); |
| 3292 | break; |
| 3293 | case LY_STMT_LIST: |
| 3294 | LY_CHECK_RET(lysp_stmt_list(ctx, child, &list->node, &list->child)); |
| 3295 | break; |
| 3296 | case LY_STMT_USES: |
| 3297 | LY_CHECK_RET(lysp_stmt_uses(ctx, child, &list->node, &list->child)); |
| 3298 | break; |
| 3299 | |
| 3300 | case LY_STMT_TYPEDEF: |
| 3301 | LY_CHECK_RET(lysp_stmt_typedef(ctx, child, &list->node, &list->typedefs)); |
| 3302 | break; |
| 3303 | case LY_STMT_MUST: |
| 3304 | LY_CHECK_RET(lysp_stmt_restrs(ctx, child, &list->musts)); |
| 3305 | break; |
| 3306 | case LY_STMT_ACTION: |
| 3307 | PARSER_CHECK_STMTVER2_RET(ctx, "action", "list"); |
| 3308 | LY_CHECK_RET(lysp_stmt_action(ctx, child, &list->node, &list->actions)); |
| 3309 | break; |
| 3310 | case LY_STMT_GROUPING: |
| 3311 | LY_CHECK_RET(lysp_stmt_grouping(ctx, child, &list->node, &list->groupings)); |
| 3312 | break; |
| 3313 | case LY_STMT_NOTIFICATION: |
| 3314 | PARSER_CHECK_STMTVER2_RET(ctx, "notification", "list"); |
| 3315 | LY_CHECK_RET(lysp_stmt_notif(ctx, child, &list->node, &list->notifs)); |
| 3316 | break; |
| 3317 | case LY_STMT_EXTENSION_INSTANCE: |
Radek Krejci | 39b7fc2 | 2021-02-26 23:29:18 +0100 | [diff] [blame] | 3318 | LY_CHECK_RET(lysp_stmt_ext(ctx, child, LY_STMT_LIST, 0, &list->exts)); |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 3319 | break; |
| 3320 | default: |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 3321 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(child->kw), "list"); |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 3322 | return LY_EVALID; |
| 3323 | } |
| 3324 | } |
| 3325 | |
| 3326 | return LY_SUCCESS; |
| 3327 | } |
| 3328 | |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 3329 | /** |
| 3330 | * @brief Parse generic statement structure into a specific parsed-schema structure. |
| 3331 | * |
| 3332 | * @param[in] pctx Parse context of the @p stmt being processed. |
| 3333 | * @param[in] stmt Generic statement structure to process. |
| 3334 | * @param[out] result Specific parsed-schema structure for the given statement. For the specific type for the particular statement, check the function code. |
| 3335 | * @param[in,out] exts [sized array](@ref sizedarrays) For extension instances in case of statements that do not store extension instances in their own list. |
| 3336 | * @return LY_ERR value. |
| 3337 | */ |
| 3338 | static LY_ERR |
| 3339 | lysp_stmt_parse(struct lysp_ctx *pctx, const struct lysp_stmt *stmt, void **result, struct lysp_ext_instance **exts) |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 3340 | { |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 3341 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 3342 | uint16_t flags; |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 3343 | |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 3344 | switch (stmt->kw) { |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 3345 | case LY_STMT_NOTIFICATION: |
| 3346 | ret = lysp_stmt_notif(pctx, stmt, NULL, (struct lysp_node_notif **)result); |
| 3347 | break; |
| 3348 | case LY_STMT_INPUT: |
| 3349 | case LY_STMT_OUTPUT: { |
| 3350 | struct lysp_node_action_inout *inout; |
| 3351 | |
| 3352 | *result = inout = calloc(1, sizeof *inout); |
| 3353 | LY_CHECK_ERR_RET(!inout, LOGMEM(PARSER_CTX(pctx)), LY_EMEM); |
| 3354 | ret = lysp_stmt_inout(pctx, stmt, NULL, inout); |
| 3355 | break; |
| 3356 | } |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 3357 | case LY_STMT_ACTION: |
| 3358 | case LY_STMT_RPC: |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 3359 | ret = lysp_stmt_action(pctx, stmt, NULL, (struct lysp_node_action **)result); |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 3360 | break; |
| 3361 | case LY_STMT_ANYDATA: |
| 3362 | case LY_STMT_ANYXML: |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 3363 | ret = lysp_stmt_any(pctx, stmt, NULL, (struct lysp_node **)result); |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 3364 | break; |
| 3365 | case LY_STMT_AUGMENT: |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 3366 | ret = lysp_stmt_augment(pctx, stmt, NULL, (struct lysp_node_augment **)result); |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 3367 | break; |
| 3368 | case LY_STMT_CASE: |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 3369 | ret = lysp_stmt_case(pctx, stmt, NULL, (struct lysp_node **)result); |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 3370 | break; |
| 3371 | case LY_STMT_CHOICE: |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 3372 | ret = lysp_stmt_choice(pctx, stmt, NULL, (struct lysp_node **)result); |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 3373 | break; |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 3374 | case LY_STMT_CONTAINER: |
| 3375 | ret = lysp_stmt_container(pctx, stmt, NULL, (struct lysp_node **)result); |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 3376 | break; |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 3377 | case LY_STMT_GROUPING: |
| 3378 | ret = lysp_stmt_grouping(pctx, stmt, NULL, (struct lysp_node_grp **)result); |
| 3379 | break; |
| 3380 | case LY_STMT_LEAF: |
| 3381 | ret = lysp_stmt_leaf(pctx, stmt, NULL, (struct lysp_node **)result); |
| 3382 | break; |
| 3383 | case LY_STMT_LEAF_LIST: |
| 3384 | ret = lysp_stmt_leaflist(pctx, stmt, NULL, (struct lysp_node **)result); |
| 3385 | break; |
| 3386 | case LY_STMT_LIST: |
| 3387 | ret = lysp_stmt_list(pctx, stmt, NULL, (struct lysp_node **)result); |
| 3388 | break; |
| 3389 | case LY_STMT_USES: |
| 3390 | ret = lysp_stmt_uses(pctx, stmt, NULL, (struct lysp_node **)result); |
| 3391 | break; |
| 3392 | case LY_STMT_BASE: |
| 3393 | ret = lysp_stmt_text_fields(pctx, stmt, (const char ***)result, Y_PREF_IDENTIF_ARG, exts); |
| 3394 | break; |
| 3395 | case LY_STMT_ARGUMENT: |
| 3396 | case LY_STMT_BELONGS_TO: |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 3397 | case LY_STMT_CONTACT: |
| 3398 | case LY_STMT_DESCRIPTION: |
| 3399 | case LY_STMT_ERROR_APP_TAG: |
| 3400 | case LY_STMT_ERROR_MESSAGE: |
| 3401 | case LY_STMT_KEY: |
| 3402 | case LY_STMT_NAMESPACE: |
| 3403 | case LY_STMT_ORGANIZATION: |
| 3404 | case LY_STMT_PRESENCE: |
| 3405 | case LY_STMT_REFERENCE: |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 3406 | case LY_STMT_REVISION_DATE: |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 3407 | case LY_STMT_UNITS: |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 3408 | ret = lysp_stmt_text_field(pctx, stmt, 0, (const char **)result, Y_STR_ARG, exts); |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 3409 | break; |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 3410 | case LY_STMT_BIT: |
| 3411 | case LY_STMT_ENUM: |
| 3412 | ret = lysp_stmt_type_enum(pctx, stmt, (struct lysp_type_enum **)result); |
| 3413 | break; |
| 3414 | case LY_STMT_CONFIG: |
| 3415 | assert(*result); |
| 3416 | ret = lysp_stmt_config(pctx, stmt, *(uint16_t **)result, exts); |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 3417 | break; |
| 3418 | case LY_STMT_DEFAULT: |
| 3419 | case LY_STMT_IF_FEATURE: |
| 3420 | case LY_STMT_UNIQUE: |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 3421 | ret = lysp_stmt_qnames(pctx, stmt, (struct lysp_qname **)result, Y_STR_ARG, exts); |
| 3422 | break; |
| 3423 | case LY_STMT_DEVIATE: |
| 3424 | ret = lysp_stmt_deviate(pctx, stmt, (struct lysp_deviate **)result, exts); |
| 3425 | break; |
| 3426 | case LY_STMT_DEVIATION: |
| 3427 | ret = lysp_stmt_deviation(pctx, stmt, (struct lysp_deviation **)result); |
| 3428 | break; |
| 3429 | case LY_STMT_EXTENSION: |
| 3430 | ret = lysp_stmt_extension(pctx, stmt, (struct lysp_ext **)result); |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 3431 | break; |
| 3432 | case LY_STMT_EXTENSION_INSTANCE: |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 3433 | ret = lysp_stmt_ext(pctx, stmt, LY_STMT_EXTENSION_INSTANCE, 0, (struct lysp_ext_instance **)result); |
| 3434 | break; |
| 3435 | case LY_STMT_FEATURE: |
| 3436 | ret = lysp_stmt_feature(pctx, stmt, (struct lysp_feature **)result); |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 3437 | break; |
| 3438 | case LY_STMT_FRACTION_DIGITS: |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 3439 | ret = lysp_stmt_type_fracdigits(pctx, stmt, *(uint8_t **)result, exts); |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 3440 | break; |
| 3441 | case LY_STMT_LENGTH: |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 3442 | case LY_STMT_RANGE: { |
| 3443 | struct lysp_restr *restr; |
| 3444 | |
| 3445 | *result = restr = calloc(1, sizeof *restr); |
| 3446 | LY_CHECK_ERR_RET(!restr, LOGMEM(PARSER_CTX(pctx)), LY_EMEM); |
| 3447 | |
| 3448 | ret = lysp_stmt_restr(pctx, stmt, restr); |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 3449 | break; |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 3450 | } |
| 3451 | case LY_STMT_MUST: |
| 3452 | ret = lysp_stmt_restrs(pctx, stmt, (struct lysp_restr **)result); |
| 3453 | break; |
| 3454 | case LY_STMT_IDENTITY: |
| 3455 | ret = lysp_stmt_identity(pctx, stmt, (struct lysp_ident **)result); |
| 3456 | break; |
| 3457 | case LY_STMT_IMPORT: |
| 3458 | ret = lysp_stmt_import(pctx, stmt, (struct lysp_import **)result); |
| 3459 | break; |
| 3460 | case LY_STMT_INCLUDE: |
| 3461 | ret = lysp_stmt_include(pctx, stmt, (struct lysp_include **)result); |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 3462 | break; |
| 3463 | case LY_STMT_MANDATORY: |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 3464 | ret = lysp_stmt_mandatory(pctx, stmt, *(uint16_t **)result, exts); |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 3465 | break; |
| 3466 | case LY_STMT_MAX_ELEMENTS: |
| 3467 | flags = 0; |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 3468 | ret = lysp_stmt_maxelements(pctx, stmt, *(uint32_t **)result, &flags, exts); |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 3469 | break; |
| 3470 | case LY_STMT_MIN_ELEMENTS: |
| 3471 | flags = 0; |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 3472 | ret = lysp_stmt_minelements(pctx, stmt, *(uint32_t **)result, &flags, exts); |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 3473 | break; |
| 3474 | case LY_STMT_MODIFIER: |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 3475 | ret = lysp_stmt_type_pattern_modifier(pctx, stmt, (const char **)result, exts); |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 3476 | break; |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 3477 | case LY_STMT_MODULE: { |
| 3478 | struct lysp_module *mod; |
| 3479 | |
| 3480 | *result = mod = calloc(1, sizeof *mod); |
| 3481 | LY_CHECK_ERR_RET(!mod, LOGMEM(PARSER_CTX(pctx)), LY_EMEM); |
| 3482 | ret = lysp_stmt_module(pctx, stmt, mod); |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 3483 | break; |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 3484 | } |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 3485 | case LY_STMT_ORDERED_BY: |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 3486 | ret = lysp_stmt_orderedby(pctx, stmt, *(uint16_t **)result, exts); |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 3487 | break; |
| 3488 | case LY_STMT_PATH: { |
| 3489 | const char *str_path = NULL; |
| 3490 | |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 3491 | LY_CHECK_RET(lysp_stmt_text_field(pctx, stmt, 0, &str_path, Y_STR_ARG, exts)); |
| 3492 | ret = ly_path_parse(PARSER_CTX(pctx), NULL, str_path, 0, 1, LY_PATH_BEGIN_EITHER, |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 3493 | LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_LEAFREF, (struct lyxp_expr **)result); |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 3494 | lydict_remove(PARSER_CTX(pctx), str_path); |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 3495 | break; |
| 3496 | } |
| 3497 | case LY_STMT_PATTERN: |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 3498 | ret = lysp_stmt_type_pattern(pctx, stmt, (struct lysp_restr **)result); |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 3499 | break; |
| 3500 | case LY_STMT_POSITION: |
| 3501 | case LY_STMT_VALUE: |
| 3502 | flags = 0; |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 3503 | ret = lysp_stmt_type_enum_value_pos(pctx, stmt, *(int64_t **)result, &flags, exts); |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 3504 | break; |
| 3505 | case LY_STMT_PREFIX: |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 3506 | ret = lysp_stmt_text_field(pctx, stmt, 0, (const char **)result, Y_IDENTIF_ARG, exts); |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 3507 | break; |
| 3508 | case LY_STMT_REFINE: |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 3509 | ret = lysp_stmt_refine(pctx, stmt, (struct lysp_refine **)result); |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 3510 | break; |
| 3511 | case LY_STMT_REQUIRE_INSTANCE: |
| 3512 | flags = 0; |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 3513 | ret = lysp_stmt_type_reqinstance(pctx, stmt, *(uint8_t **)result, &flags, exts); |
| 3514 | break; |
| 3515 | case LY_STMT_REVISION: |
| 3516 | ret = lysp_stmt_revision(pctx, stmt, (struct lysp_revision **)result); |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 3517 | break; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3518 | case LY_STMT_STATUS: |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 3519 | ret = lysp_stmt_status(pctx, stmt, (uint16_t *)result, exts); |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 3520 | break; |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 3521 | case LY_STMT_SUBMODULE: { |
| 3522 | struct lysp_submodule *submod; |
| 3523 | |
| 3524 | *result = submod = calloc(1, sizeof *submod); |
| 3525 | LY_CHECK_ERR_RET(!submod, LOGMEM(PARSER_CTX(pctx)), LY_EMEM); |
| 3526 | ret = lysp_stmt_submodule(pctx, stmt, submod); |
| 3527 | break; |
| 3528 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 3529 | case LY_STMT_TYPE: { |
| 3530 | struct lysp_type *type; |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 3531 | |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 3532 | *result = type = calloc(1, sizeof *type); |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 3533 | LY_CHECK_ERR_RET(!type, LOGMEM(PARSER_CTX(pctx)), LY_EMEM); |
| 3534 | ret = lysp_stmt_type(pctx, stmt, type); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 3535 | break; |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 3536 | } |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 3537 | case LY_STMT_TYPEDEF: |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 3538 | ret = lysp_stmt_typedef(pctx, stmt, NULL, (struct lysp_tpdf **)result); |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 3539 | break; |
| 3540 | case LY_STMT_WHEN: |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 3541 | ret = lysp_stmt_when(pctx, stmt, (struct lysp_when **)result); |
| 3542 | break; |
| 3543 | case LY_STMT_YANG_VERSION: |
| 3544 | ret = lysp_stmt_yangver(pctx, stmt, *(uint8_t **)result, exts); |
| 3545 | break; |
| 3546 | case LY_STMT_YIN_ELEMENT: |
| 3547 | ret = lysp_stmt_yinelem(pctx, stmt, *(uint16_t **)result, exts); |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 3548 | break; |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 3549 | default: |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 3550 | LOGINT(PARSER_CTX(pctx)); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 3551 | return LY_EINT; |
| 3552 | } |
| 3553 | |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 3554 | return ret; |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 3555 | } |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 3556 | |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 3557 | LY_ERR |
| 3558 | lys_parse_ext_instance_stmt(struct lysp_ctx *pctx, struct lysp_ext_substmt *substmt, struct lysp_stmt *stmt) |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 3559 | { |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 3560 | LY_ERR rc = LY_SUCCESS; |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 3561 | |
Michal Vasko | a6bab1e | 2024-09-05 12:24:20 +0200 | [diff] [blame] | 3562 | if (!substmt->storage_p) { |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 3563 | /* nothing to parse, ignored */ |
| 3564 | goto cleanup; |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 3565 | } |
| 3566 | |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 3567 | switch (stmt->kw) { |
| 3568 | case LY_STMT_NOTIFICATION: |
| 3569 | case LY_STMT_INPUT: |
| 3570 | case LY_STMT_OUTPUT: |
| 3571 | case LY_STMT_ACTION: |
| 3572 | case LY_STMT_RPC: |
| 3573 | case LY_STMT_ANYDATA: |
| 3574 | case LY_STMT_ANYXML: |
| 3575 | case LY_STMT_AUGMENT: |
| 3576 | case LY_STMT_CASE: |
| 3577 | case LY_STMT_CHOICE: |
| 3578 | case LY_STMT_CONTAINER: |
| 3579 | case LY_STMT_GROUPING: |
| 3580 | case LY_STMT_LEAF: |
| 3581 | case LY_STMT_LEAF_LIST: |
| 3582 | case LY_STMT_LIST: |
| 3583 | case LY_STMT_USES: { |
| 3584 | struct lysp_node **pnodes_p, *pnode = NULL; |
| 3585 | |
| 3586 | /* parse the node */ |
| 3587 | LY_CHECK_GOTO(rc = lysp_stmt_parse(pctx, stmt, (void **)&pnode, NULL), cleanup); |
| 3588 | |
| 3589 | /* usually is a linked-list of all the parsed schema nodes */ |
Michal Vasko | a6bab1e | 2024-09-05 12:24:20 +0200 | [diff] [blame] | 3590 | pnodes_p = (struct lysp_node **)substmt->storage_p; |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 3591 | while (*pnodes_p) { |
| 3592 | pnodes_p = &(*pnodes_p)->next; |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 3593 | } |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 3594 | *pnodes_p = pnode; |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 3595 | |
| 3596 | break; |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 3597 | } |
| 3598 | case LY_STMT_BASE: |
| 3599 | case LY_STMT_BIT: |
| 3600 | case LY_STMT_DEFAULT: |
| 3601 | case LY_STMT_DEVIATE: |
| 3602 | case LY_STMT_DEVIATION: |
| 3603 | case LY_STMT_ENUM: |
| 3604 | case LY_STMT_EXTENSION: |
| 3605 | case LY_STMT_EXTENSION_INSTANCE: |
| 3606 | case LY_STMT_FEATURE: |
| 3607 | case LY_STMT_IDENTITY: |
| 3608 | case LY_STMT_IF_FEATURE: |
| 3609 | case LY_STMT_IMPORT: |
| 3610 | case LY_STMT_INCLUDE: |
| 3611 | case LY_STMT_MUST: |
| 3612 | case LY_STMT_PATTERN: |
| 3613 | case LY_STMT_REFINE: |
| 3614 | case LY_STMT_REVISION: |
| 3615 | case LY_STMT_TYPEDEF: |
| 3616 | case LY_STMT_UNIQUE: |
| 3617 | /* parse, sized array */ |
Michal Vasko | a6bab1e | 2024-09-05 12:24:20 +0200 | [diff] [blame] | 3618 | LY_CHECK_GOTO(rc = lysp_stmt_parse(pctx, stmt, substmt->storage_p, NULL), cleanup); |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 3619 | break; |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 3620 | |
| 3621 | case LY_STMT_ARGUMENT: |
| 3622 | case LY_STMT_BELONGS_TO: |
| 3623 | case LY_STMT_CONTACT: |
| 3624 | case LY_STMT_DESCRIPTION: |
| 3625 | case LY_STMT_ERROR_APP_TAG: |
| 3626 | case LY_STMT_ERROR_MESSAGE: |
| 3627 | case LY_STMT_FRACTION_DIGITS: |
| 3628 | case LY_STMT_KEY: |
| 3629 | case LY_STMT_LENGTH: |
| 3630 | case LY_STMT_MANDATORY: |
| 3631 | case LY_STMT_MAX_ELEMENTS: |
| 3632 | case LY_STMT_MIN_ELEMENTS: |
| 3633 | case LY_STMT_MODIFIER: |
| 3634 | case LY_STMT_MODULE: |
| 3635 | case LY_STMT_NAMESPACE: |
| 3636 | case LY_STMT_ORGANIZATION: |
| 3637 | case LY_STMT_PATH: |
| 3638 | case LY_STMT_POSITION: |
| 3639 | case LY_STMT_PREFIX: |
| 3640 | case LY_STMT_PRESENCE: |
| 3641 | case LY_STMT_RANGE: |
| 3642 | case LY_STMT_REFERENCE: |
| 3643 | case LY_STMT_REQUIRE_INSTANCE: |
| 3644 | case LY_STMT_REVISION_DATE: |
| 3645 | case LY_STMT_SUBMODULE: |
| 3646 | case LY_STMT_TYPE: |
| 3647 | case LY_STMT_UNITS: |
| 3648 | case LY_STMT_VALUE: |
| 3649 | case LY_STMT_WHEN: |
| 3650 | case LY_STMT_YANG_VERSION: |
| 3651 | case LY_STMT_YIN_ELEMENT: |
| 3652 | /* single item */ |
Michal Vasko | a6bab1e | 2024-09-05 12:24:20 +0200 | [diff] [blame] | 3653 | if (*substmt->storage_p) { |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 3654 | LOGVAL(PARSER_CTX(pctx), LY_VCODE_DUPSTMT, stmt->stmt); |
| 3655 | rc = LY_EVALID; |
| 3656 | goto cleanup; |
| 3657 | } |
| 3658 | |
| 3659 | /* parse */ |
Michal Vasko | a6bab1e | 2024-09-05 12:24:20 +0200 | [diff] [blame] | 3660 | LY_CHECK_GOTO(rc = lysp_stmt_parse(pctx, stmt, substmt->storage_p, NULL), cleanup); |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 3661 | break; |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 3662 | |
| 3663 | case LY_STMT_CONFIG: |
| 3664 | /* single item */ |
Michal Vasko | a6bab1e | 2024-09-05 12:24:20 +0200 | [diff] [blame] | 3665 | if ((*(uint16_t *)substmt->storage_p) & LYS_CONFIG_MASK) { |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 3666 | LOGVAL(PARSER_CTX(pctx), LY_VCODE_DUPSTMT, stmt->stmt); |
| 3667 | rc = LY_EVALID; |
| 3668 | goto cleanup; |
| 3669 | } |
| 3670 | |
| 3671 | /* parse */ |
Michal Vasko | a6bab1e | 2024-09-05 12:24:20 +0200 | [diff] [blame] | 3672 | LY_CHECK_GOTO(rc = lysp_stmt_parse(pctx, stmt, substmt->storage_p, NULL), cleanup); |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 3673 | break; |
| 3674 | |
| 3675 | case LY_STMT_ORDERED_BY: |
| 3676 | /* single item */ |
Michal Vasko | a6bab1e | 2024-09-05 12:24:20 +0200 | [diff] [blame] | 3677 | if ((*(uint16_t *)substmt->storage_p) & LYS_ORDBY_MASK) { |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 3678 | LOGVAL(PARSER_CTX(pctx), LY_VCODE_DUPSTMT, stmt->stmt); |
| 3679 | rc = LY_EVALID; |
| 3680 | goto cleanup; |
| 3681 | } |
| 3682 | |
| 3683 | /* parse */ |
Michal Vasko | a6bab1e | 2024-09-05 12:24:20 +0200 | [diff] [blame] | 3684 | LY_CHECK_GOTO(rc = lysp_stmt_parse(pctx, stmt, substmt->storage_p, NULL), cleanup); |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 3685 | break; |
| 3686 | |
| 3687 | case LY_STMT_STATUS: |
| 3688 | /* single item */ |
Michal Vasko | a6bab1e | 2024-09-05 12:24:20 +0200 | [diff] [blame] | 3689 | if ((*(uint16_t *)substmt->storage_p) & LYS_STATUS_MASK) { |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 3690 | LOGVAL(PARSER_CTX(pctx), LY_VCODE_DUPSTMT, stmt->stmt); |
| 3691 | rc = LY_EVALID; |
| 3692 | goto cleanup; |
| 3693 | } |
| 3694 | |
| 3695 | /* parse */ |
Michal Vasko | a6bab1e | 2024-09-05 12:24:20 +0200 | [diff] [blame] | 3696 | LY_CHECK_GOTO(rc = lysp_stmt_parse(pctx, stmt, substmt->storage_p, NULL), cleanup); |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 3697 | break; |
| 3698 | |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 3699 | default: |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 3700 | LOGINT(PARSER_CTX(pctx)); |
| 3701 | rc = LY_EINT; |
| 3702 | goto cleanup; |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 3703 | } |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 3704 | |
| 3705 | cleanup: |
| 3706 | return rc; |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 3707 | } |