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