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 | |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 41 | #include "common.h" |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 42 | #include "compat.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 43 | #include "dict.h" |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 44 | #include "in_internal.h" |
Radek Krejci | 47fab89 | 2020-11-05 17:02:41 +0100 | [diff] [blame] | 45 | #include "log.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" |
Radek Krejci | 7711410 | 2021-03-10 15:21:57 +0100 | [diff] [blame] | 48 | #include "set.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 49 | #include "tree.h" |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 50 | #include "tree_data.h" |
| 51 | #include "tree_data_internal.h" |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 52 | #include "tree_schema.h" |
| 53 | #include "tree_schema_internal.h" |
| 54 | |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 55 | void |
| 56 | lyd_ctx_free(struct lyd_ctx *lydctx) |
| 57 | { |
| 58 | ly_set_erase(&lydctx->node_types, NULL); |
| 59 | ly_set_erase(&lydctx->meta_types, NULL); |
| 60 | ly_set_erase(&lydctx->node_when, NULL); |
Michal Vasko | 135719f | 2022-08-25 12:18:17 +0200 | [diff] [blame^] | 61 | ly_set_erase(&lydctx->ext_node, free); |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 62 | ly_set_erase(&lydctx->ext_val, free); |
| 63 | } |
| 64 | |
| 65 | LY_ERR |
| 66 | lyd_parser_find_operation(const struct lyd_node *parent, uint32_t int_opts, struct lyd_node **op) |
| 67 | { |
| 68 | const struct lyd_node *iter; |
| 69 | |
| 70 | *op = NULL; |
| 71 | |
| 72 | if (!parent) { |
| 73 | /* no parent, nothing to look for */ |
| 74 | return LY_SUCCESS; |
| 75 | } |
| 76 | |
| 77 | /* we need to find the operation node if it already exists */ |
| 78 | for (iter = parent; iter; iter = lyd_parent(iter)) { |
| 79 | if (iter->schema && (iter->schema->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF))) { |
| 80 | break; |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | if (!iter) { |
| 85 | /* no operation found */ |
| 86 | return LY_SUCCESS; |
| 87 | } |
| 88 | |
| 89 | if (!(int_opts & (LYD_INTOPT_RPC | LYD_INTOPT_ACTION | LYD_INTOPT_NOTIF | LYD_INTOPT_REPLY))) { |
| 90 | LOGERR(LYD_CTX(parent), LY_EINVAL, "Invalid parent %s \"%s\" node when not parsing any operation.", |
| 91 | lys_nodetype2str(iter->schema->nodetype), iter->schema->name); |
| 92 | return LY_EINVAL; |
| 93 | } else if ((iter->schema->nodetype == LYS_RPC) && !(int_opts & (LYD_INTOPT_RPC | LYD_INTOPT_REPLY))) { |
| 94 | LOGERR(LYD_CTX(parent), LY_EINVAL, "Invalid parent RPC \"%s\" node when not parsing RPC nor rpc-reply.", |
| 95 | iter->schema->name); |
| 96 | return LY_EINVAL; |
| 97 | } else if ((iter->schema->nodetype == LYS_ACTION) && !(int_opts & (LYD_INTOPT_ACTION | LYD_INTOPT_REPLY))) { |
| 98 | LOGERR(LYD_CTX(parent), LY_EINVAL, "Invalid parent action \"%s\" node when not parsing action nor rpc-reply.", |
| 99 | iter->schema->name); |
| 100 | return LY_EINVAL; |
| 101 | } else if ((iter->schema->nodetype == LYS_NOTIF) && !(int_opts & LYD_INTOPT_NOTIF)) { |
| 102 | LOGERR(LYD_CTX(parent), LY_EINVAL, "Invalid parent notification \"%s\" node when not parsing a notification.", |
| 103 | iter->schema->name); |
| 104 | return LY_EINVAL; |
| 105 | } |
| 106 | |
| 107 | *op = (struct lyd_node *)iter; |
| 108 | return LY_SUCCESS; |
| 109 | } |
| 110 | |
| 111 | LY_ERR |
| 112 | lyd_parser_check_schema(struct lyd_ctx *lydctx, const struct lysc_node *snode) |
| 113 | { |
| 114 | LY_ERR rc = LY_SUCCESS; |
| 115 | |
| 116 | LOG_LOCSET(snode, NULL, NULL, NULL); |
| 117 | |
| 118 | if (lydctx->int_opts & LYD_INTOPT_ANY) { |
| 119 | /* nothing to check, everything is allowed */ |
| 120 | goto cleanup; |
| 121 | } |
| 122 | |
| 123 | if ((lydctx->parse_opts & LYD_PARSE_NO_STATE) && (snode->flags & LYS_CONFIG_R)) { |
| 124 | LOGVAL(lydctx->data_ctx->ctx, LY_VCODE_UNEXPNODE, "state", snode->name); |
| 125 | rc = LY_EVALID; |
| 126 | goto cleanup; |
| 127 | } |
| 128 | |
| 129 | if (snode->nodetype == LYS_RPC) { |
| 130 | if (lydctx->int_opts & (LYD_INTOPT_RPC | LYD_INTOPT_REPLY)) { |
| 131 | if (lydctx->op_node) { |
| 132 | goto error_node_dup; |
| 133 | } |
| 134 | } else { |
| 135 | goto error_node_inval; |
| 136 | } |
| 137 | } else if (snode->nodetype == LYS_ACTION) { |
| 138 | if (lydctx->int_opts & (LYD_INTOPT_ACTION | LYD_INTOPT_REPLY)) { |
| 139 | if (lydctx->op_node) { |
| 140 | goto error_node_dup; |
| 141 | } |
| 142 | } else { |
| 143 | goto error_node_inval; |
| 144 | } |
| 145 | } else if (snode->nodetype == LYS_NOTIF) { |
| 146 | if (lydctx->int_opts & LYD_INTOPT_NOTIF) { |
| 147 | if (lydctx->op_node) { |
| 148 | goto error_node_dup; |
| 149 | } |
| 150 | } else { |
| 151 | goto error_node_inval; |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | /* success */ |
| 156 | goto cleanup; |
| 157 | |
| 158 | error_node_dup: |
| 159 | LOGVAL(lydctx->data_ctx->ctx, LYVE_DATA, "Unexpected %s element \"%s\", %s \"%s\" already parsed.", |
| 160 | lys_nodetype2str(snode->nodetype), snode->name, lys_nodetype2str(lydctx->op_node->schema->nodetype), |
| 161 | lydctx->op_node->schema->name); |
| 162 | rc = LY_EVALID; |
| 163 | goto cleanup; |
| 164 | |
| 165 | error_node_inval: |
| 166 | LOGVAL(lydctx->data_ctx->ctx, LYVE_DATA, "Unexpected %s element \"%s\".", lys_nodetype2str(snode->nodetype), |
| 167 | snode->name); |
| 168 | rc = LY_EVALID; |
| 169 | |
| 170 | cleanup: |
| 171 | LOG_LOCBACK(1, 0, 0, 0); |
| 172 | return rc; |
| 173 | } |
| 174 | |
| 175 | LY_ERR |
| 176 | lyd_parser_create_term(struct lyd_ctx *lydctx, const struct lysc_node *schema, const void *value, size_t value_len, |
| 177 | ly_bool *dynamic, LY_VALUE_FORMAT format, void *prefix_data, uint32_t hints, struct lyd_node **node) |
| 178 | { |
| 179 | ly_bool incomplete; |
| 180 | |
| 181 | LY_CHECK_RET(lyd_create_term(schema, value, value_len, dynamic, format, prefix_data, hints, &incomplete, node)); |
| 182 | |
| 183 | if (incomplete && !(lydctx->parse_opts & LYD_PARSE_ONLY)) { |
| 184 | LY_CHECK_RET(ly_set_add(&lydctx->node_types, *node, 1, NULL)); |
| 185 | } |
| 186 | return LY_SUCCESS; |
| 187 | } |
| 188 | |
| 189 | LY_ERR |
| 190 | lyd_parser_create_meta(struct lyd_ctx *lydctx, struct lyd_node *parent, struct lyd_meta **meta, const struct lys_module *mod, |
| 191 | const char *name, size_t name_len, const void *value, size_t value_len, ly_bool *dynamic, LY_VALUE_FORMAT format, |
| 192 | void *prefix_data, uint32_t hints, const struct lysc_node *ctx_node) |
| 193 | { |
| 194 | ly_bool incomplete; |
| 195 | struct lyd_meta *first = NULL; |
| 196 | |
| 197 | if (meta && *meta) { |
| 198 | /* remember the first metadata */ |
| 199 | first = *meta; |
| 200 | } |
| 201 | |
| 202 | LY_CHECK_RET(lyd_create_meta(parent, meta, mod, name, name_len, value, value_len, dynamic, format, prefix_data, |
| 203 | hints, ctx_node, 0, &incomplete)); |
| 204 | |
| 205 | if (incomplete && !(lydctx->parse_opts & LYD_PARSE_ONLY)) { |
| 206 | LY_CHECK_RET(ly_set_add(&lydctx->meta_types, *meta, 1, NULL)); |
| 207 | } |
| 208 | |
| 209 | if (first) { |
| 210 | /* always return the first metadata */ |
| 211 | *meta = first; |
| 212 | } |
| 213 | |
| 214 | return LY_SUCCESS; |
| 215 | } |
| 216 | |
Michal Vasko | 95c6d5c | 2022-05-20 10:33:39 +0200 | [diff] [blame] | 217 | LY_ERR |
| 218 | lyd_parse_check_keys(struct lyd_node *node) |
| 219 | { |
| 220 | const struct lysc_node *skey = NULL; |
| 221 | const struct lyd_node *key; |
| 222 | |
| 223 | assert(node->schema->nodetype == LYS_LIST); |
| 224 | |
| 225 | key = lyd_child(node); |
| 226 | while ((skey = lys_getnext(skey, node->schema, NULL, 0)) && (skey->flags & LYS_KEY)) { |
| 227 | if (!key || (key->schema != skey)) { |
| 228 | LOGVAL(LYD_CTX(node), LY_VCODE_NOKEY, skey->name); |
| 229 | return LY_EVALID; |
| 230 | } |
| 231 | |
| 232 | key = key->next; |
| 233 | } |
| 234 | |
| 235 | return LY_SUCCESS; |
| 236 | } |
| 237 | |
| 238 | LY_ERR |
| 239 | lyd_parse_set_data_flags(struct lyd_node *node, struct lyd_meta **meta, struct lyd_ctx *lydctx, |
| 240 | struct lysc_ext_instance *ext) |
| 241 | { |
| 242 | struct lyd_meta *meta2, *prev_meta = NULL; |
| 243 | struct lyd_ctx_ext_val *ext_val; |
| 244 | |
| 245 | if (lysc_has_when(node->schema)) { |
| 246 | if (!(lydctx->parse_opts & LYD_PARSE_ONLY)) { |
| 247 | /* remember we need to evaluate this node's when */ |
| 248 | LY_CHECK_RET(ly_set_add(&lydctx->node_when, node, 1, NULL)); |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | LY_LIST_FOR(*meta, meta2) { |
| 253 | if (!strcmp(meta2->name, "default") && !strcmp(meta2->annotation->module->name, "ietf-netconf-with-defaults") && |
| 254 | meta2->value.boolean) { |
| 255 | /* node is default according to the metadata */ |
| 256 | node->flags |= LYD_DEFAULT; |
| 257 | |
| 258 | /* delete the metadata */ |
| 259 | if (prev_meta) { |
| 260 | prev_meta->next = meta2->next; |
| 261 | } else { |
| 262 | *meta = (*meta)->next; |
| 263 | } |
| 264 | lyd_free_meta_single(meta2); |
| 265 | break; |
| 266 | } |
| 267 | |
| 268 | prev_meta = meta2; |
| 269 | } |
| 270 | |
| 271 | if (ext) { |
| 272 | /* parsed for an extension */ |
| 273 | node->flags |= LYD_EXT; |
| 274 | |
| 275 | if (!(lydctx->parse_opts & LYD_PARSE_ONLY)) { |
| 276 | /* rememeber for validation */ |
| 277 | ext_val = malloc(sizeof *ext_val); |
| 278 | LY_CHECK_ERR_RET(!ext_val, LOGMEM(LYD_CTX(node)), LY_EMEM); |
| 279 | ext_val->ext = ext; |
| 280 | ext_val->sibling = node; |
| 281 | LY_CHECK_RET(ly_set_add(&lydctx->ext_val, ext_val, 1, NULL)); |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | return LY_SUCCESS; |
| 286 | } |
| 287 | |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 288 | static LY_ERR lysp_stmt_container(struct lys_parser_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_node *parent, |
| 289 | struct lysp_node **siblings); |
| 290 | static LY_ERR lysp_stmt_choice(struct lys_parser_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_node *parent, |
| 291 | struct lysp_node **siblings); |
| 292 | static LY_ERR lysp_stmt_case(struct lys_parser_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_node *parent, |
| 293 | struct lysp_node **siblings); |
| 294 | static LY_ERR lysp_stmt_uses(struct lys_parser_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_node *parent, |
| 295 | struct lysp_node **siblings); |
| 296 | static LY_ERR lysp_stmt_grouping(struct lys_parser_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_node *parent, |
| 297 | struct lysp_node_grp **groupings); |
| 298 | static LY_ERR lysp_stmt_list(struct lys_parser_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_node *parent, |
| 299 | struct lysp_node **siblings); |
| 300 | |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 301 | static LY_ERR |
| 302 | lysp_stmt_validate_value(struct lys_parser_ctx *ctx, enum yang_arg val_type, const char *val) |
| 303 | { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 304 | uint8_t prefix = 0; |
| 305 | ly_bool first = 1; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 306 | uint32_t c; |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 307 | size_t utf8_char_len; |
| 308 | |
| 309 | while (*val) { |
| 310 | LY_CHECK_ERR_RET(ly_getutf8(&val, &c, &utf8_char_len), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 311 | LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, (val)[-utf8_char_len]), LY_EVALID); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 312 | |
| 313 | switch (val_type) { |
| 314 | case Y_IDENTIF_ARG: |
| 315 | LY_CHECK_RET(lysp_check_identifierchar(ctx, c, first, NULL)); |
| 316 | break; |
| 317 | case Y_PREF_IDENTIF_ARG: |
| 318 | LY_CHECK_RET(lysp_check_identifierchar(ctx, c, first, &prefix)); |
| 319 | break; |
| 320 | case Y_STR_ARG: |
| 321 | case Y_MAYBE_STR_ARG: |
| 322 | LY_CHECK_RET(lysp_check_stringchar(ctx, c)); |
| 323 | break; |
| 324 | } |
| 325 | first = 0; |
| 326 | } |
| 327 | |
| 328 | return LY_SUCCESS; |
| 329 | } |
| 330 | |
| 331 | /** |
| 332 | * @brief Parse extension instance. |
| 333 | * |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 334 | * @param[in] ctx parser context. |
| 335 | * @param[in] stmt Source statement data from the parsed extension instance. |
Radek Krejci | fc596f9 | 2021-02-26 22:40:26 +0100 | [diff] [blame] | 336 | * @param[in] insubstmt The statement this extension instance is a substatement of. |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 337 | * @param[in] insubstmt_index Index of the keyword instance this extension instance is a substatement of. |
| 338 | * @param[in,out] exts Extension instances to add to. |
| 339 | * |
| 340 | * @return LY_ERR values. |
| 341 | */ |
| 342 | static LY_ERR |
Radek Krejci | fc596f9 | 2021-02-26 22:40:26 +0100 | [diff] [blame] | 343 | lysp_stmt_ext(struct lys_parser_ctx *ctx, const struct lysp_stmt *stmt, enum ly_stmt insubstmt, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 344 | LY_ARRAY_COUNT_TYPE insubstmt_index, struct lysp_ext_instance **exts) |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 345 | { |
| 346 | struct lysp_ext_instance *e; |
| 347 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 348 | LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *exts, e, LY_EMEM); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 349 | |
| 350 | /* store name and insubstmt info */ |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 351 | 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] | 352 | e->parent_stmt = insubstmt; |
| 353 | e->parent_stmt_index = insubstmt_index; |
aPiecek | 60d9d67 | 2021-04-27 15:49:57 +0200 | [diff] [blame] | 354 | e->parsed = NULL; |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 355 | /* TODO (duplicate) e->child = stmt->child; */ |
| 356 | |
| 357 | /* get optional argument */ |
| 358 | if (stmt->arg) { |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 359 | 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] | 360 | } |
| 361 | |
| 362 | return LY_SUCCESS; |
| 363 | } |
| 364 | |
| 365 | /** |
| 366 | * @brief Parse a generic text field without specific constraints. Those are contact, organization, |
| 367 | * description, etc... |
| 368 | * |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 369 | * @param[in] ctx parser context. |
| 370 | * @param[in] stmt Source statement data from the parsed extension instance. |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 371 | * @param[in] substmt_index Index of this substatement. |
| 372 | * @param[in,out] value Place to store the parsed value. |
| 373 | * @param[in] arg Type of the YANG keyword argument (of the value). |
| 374 | * @param[in,out] exts Extension instances to add to. |
| 375 | * |
| 376 | * @return LY_ERR values. |
| 377 | */ |
| 378 | static LY_ERR |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 379 | lysp_stmt_text_field(struct lys_parser_ctx *ctx, const struct lysp_stmt *stmt, uint32_t substmt_index, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 380 | const char **value, enum yang_arg arg, struct lysp_ext_instance **exts) |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 381 | { |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 382 | if (*value) { |
Radek Krejci | 3972b33 | 2021-03-02 16:34:31 +0100 | [diff] [blame] | 383 | LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(stmt->kw)); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 384 | return LY_EVALID; |
| 385 | } |
| 386 | |
| 387 | LY_CHECK_RET(lysp_stmt_validate_value(ctx, arg, stmt->arg)); |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 388 | LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), stmt->arg, 0, value)); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 389 | |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 390 | for (const struct lysp_stmt *child = stmt->child; child; child = child->next) { |
| 391 | switch (child->kw) { |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 392 | case LY_STMT_EXTENSION_INSTANCE: |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 393 | 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] | 394 | break; |
| 395 | default: |
Radek Krejci | 3972b33 | 2021-03-02 16:34:31 +0100 | [diff] [blame] | 396 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(child->kw), ly_stmt2str(stmt->kw)); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 397 | return LY_EVALID; |
| 398 | } |
| 399 | } |
| 400 | return LY_SUCCESS; |
| 401 | } |
| 402 | |
| 403 | /** |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 404 | * @brief Parse a qname that can have more instances such as if-feature. |
| 405 | * |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 406 | * @param[in] ctx parser context. |
| 407 | * @param[in] stmt Source statement data from the parsed extension instance. |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 408 | * @param[in,out] qnames Parsed qnames to add to. |
| 409 | * @param[in] arg Type of the expected argument. |
| 410 | * @param[in,out] exts Extension instances to add to. |
| 411 | * |
| 412 | * @return LY_ERR values. |
| 413 | */ |
| 414 | static LY_ERR |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 415 | lysp_stmt_qnames(struct lys_parser_ctx *ctx, const struct lysp_stmt *stmt, |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 416 | struct lysp_qname **qnames, enum yang_arg arg, struct lysp_ext_instance **exts) |
| 417 | { |
| 418 | struct lysp_qname *item; |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 419 | |
| 420 | LY_CHECK_RET(lysp_stmt_validate_value(ctx, arg, stmt->arg)); |
| 421 | |
| 422 | /* allocate new pointer */ |
| 423 | LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *qnames, item, LY_EMEM); |
| 424 | 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] | 425 | item->mod = PARSER_CUR_PMOD(ctx); |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 426 | |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 427 | for (const struct lysp_stmt *child = stmt->child; child; child = child->next) { |
| 428 | switch (child->kw) { |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 429 | case LY_STMT_EXTENSION_INSTANCE: |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 430 | 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] | 431 | break; |
| 432 | default: |
Radek Krejci | 3972b33 | 2021-03-02 16:34:31 +0100 | [diff] [blame] | 433 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(child->kw), ly_stmt2str(stmt->kw)); |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 434 | return LY_EVALID; |
| 435 | } |
| 436 | } |
| 437 | return LY_SUCCESS; |
| 438 | } |
| 439 | |
| 440 | /** |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 441 | * @brief Parse a generic text field that can have more instances such as base. |
| 442 | * |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 443 | * @param[in] ctx parser context. |
| 444 | * @param[in] stmt Source statement data from the parsed extension instance. |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 445 | * @param[in,out] texts Parsed values to add to. |
| 446 | * @param[in] arg Type of the expected argument. |
| 447 | * @param[in,out] exts Extension instances to add to. |
| 448 | * |
| 449 | * @return LY_ERR values. |
| 450 | */ |
| 451 | static LY_ERR |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 452 | lysp_stmt_text_fields(struct lys_parser_ctx *ctx, const struct lysp_stmt *stmt, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 453 | const char ***texts, enum yang_arg arg, struct lysp_ext_instance **exts) |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 454 | { |
| 455 | const char **item; |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 456 | |
| 457 | LY_CHECK_RET(lysp_stmt_validate_value(ctx, arg, stmt->arg)); |
| 458 | |
| 459 | /* allocate new pointer */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 460 | LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *texts, item, LY_EMEM); |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 461 | LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), stmt->arg, 0, item)); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 462 | |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 463 | for (const struct lysp_stmt *child = stmt->child; child; child = child->next) { |
| 464 | switch (child->kw) { |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 465 | case LY_STMT_EXTENSION_INSTANCE: |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 466 | 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] | 467 | break; |
| 468 | default: |
Radek Krejci | 3972b33 | 2021-03-02 16:34:31 +0100 | [diff] [blame] | 469 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(child->kw), ly_stmt2str(stmt->kw)); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 470 | return LY_EVALID; |
| 471 | } |
| 472 | } |
| 473 | return LY_SUCCESS; |
| 474 | } |
| 475 | |
| 476 | /** |
| 477 | * @brief Parse the status statement. |
| 478 | * |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 479 | * @param[in] ctx parser context. |
| 480 | * @param[in] stmt Source statement data from the parsed extension instance. |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 481 | * @param[in,out] flags Flags to add to. |
| 482 | * @param[in,out] exts Extension instances to add to. |
| 483 | * |
| 484 | * @return LY_ERR values. |
| 485 | */ |
| 486 | static LY_ERR |
| 487 | lysp_stmt_status(struct lys_parser_ctx *ctx, const struct lysp_stmt *stmt, uint16_t *flags, struct lysp_ext_instance **exts) |
| 488 | { |
| 489 | size_t arg_len; |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 490 | |
| 491 | if (*flags & LYS_STATUS_MASK) { |
| 492 | LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "status"); |
| 493 | return LY_EVALID; |
| 494 | } |
| 495 | |
| 496 | LY_CHECK_RET(lysp_stmt_validate_value(ctx, Y_STR_ARG, stmt->arg)); |
| 497 | arg_len = strlen(stmt->arg); |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 498 | 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] | 499 | *flags |= LYS_STATUS_CURR; |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 500 | } 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] | 501 | *flags |= LYS_STATUS_DEPRC; |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 502 | } 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] | 503 | *flags |= LYS_STATUS_OBSLT; |
| 504 | } else { |
| 505 | LOGVAL_PARSER(ctx, LY_VCODE_INVAL, arg_len, stmt->arg, "status"); |
| 506 | return LY_EVALID; |
| 507 | } |
| 508 | |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 509 | for (const struct lysp_stmt *child = stmt->child; child; child = child->next) { |
| 510 | switch (child->kw) { |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 511 | case LY_STMT_EXTENSION_INSTANCE: |
Radek Krejci | fc596f9 | 2021-02-26 22:40:26 +0100 | [diff] [blame] | 512 | 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] | 513 | break; |
| 514 | default: |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 515 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(child->kw), "status"); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 516 | return LY_EVALID; |
| 517 | } |
| 518 | } |
| 519 | return LY_SUCCESS; |
| 520 | } |
| 521 | |
| 522 | /** |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 523 | * @brief Parse the when statement. |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 524 | * |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 525 | * @param[in] ctx parser context. |
| 526 | * @param[in] stmt Source statement data from the parsed extension instance. |
| 527 | * @param[in,out] when_p When pointer to parse to. |
| 528 | * |
| 529 | * @return LY_ERR values. |
| 530 | */ |
| 531 | static LY_ERR |
| 532 | lysp_stmt_when(struct lys_parser_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_when **when_p) |
| 533 | { |
| 534 | LY_ERR ret = LY_SUCCESS; |
| 535 | struct lysp_when *when; |
| 536 | |
| 537 | if (*when_p) { |
| 538 | LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "when"); |
| 539 | return LY_EVALID; |
| 540 | } |
| 541 | |
| 542 | LY_CHECK_RET(lysp_stmt_validate_value(ctx, Y_STR_ARG, stmt->arg)); |
| 543 | |
| 544 | when = calloc(1, sizeof *when); |
| 545 | LY_CHECK_ERR_RET(!when, LOGMEM(PARSER_CTX(ctx)), LY_EMEM); |
| 546 | *when_p = when; |
| 547 | |
| 548 | LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), stmt->arg, 0, &when->cond)); |
| 549 | |
| 550 | for (const struct lysp_stmt *child = stmt->child; child; child = child->next) { |
| 551 | switch (child->kw) { |
| 552 | case LY_STMT_DESCRIPTION: |
| 553 | LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &when->dsc, Y_STR_ARG, &when->exts)); |
| 554 | break; |
| 555 | case LY_STMT_REFERENCE: |
| 556 | LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &when->ref, Y_STR_ARG, &when->exts)); |
| 557 | break; |
| 558 | case LY_STMT_EXTENSION_INSTANCE: |
Radek Krejci | 39b7fc2 | 2021-02-26 23:29:18 +0100 | [diff] [blame] | 559 | 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] | 560 | break; |
| 561 | default: |
| 562 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(child->kw), "when"); |
| 563 | return LY_EVALID; |
| 564 | } |
| 565 | } |
| 566 | return ret; |
| 567 | } |
| 568 | |
| 569 | /** |
| 570 | * @brief Parse the config statement. |
| 571 | * |
| 572 | * @param[in] ctx parser context. |
| 573 | * @param[in] stmt Source statement data from the parsed extension instance. |
| 574 | * @param[in,out] flags Flags to add to. |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 575 | * @param[in,out] exts Extension instances to add to. |
| 576 | * |
| 577 | * @return LY_ERR values. |
| 578 | */ |
| 579 | static LY_ERR |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 580 | lysp_stmt_config(struct lys_parser_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] | 581 | { |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 582 | size_t arg_len; |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 583 | |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 584 | if (*flags & LYS_CONFIG_MASK) { |
| 585 | LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "config"); |
| 586 | return LY_EVALID; |
| 587 | } |
| 588 | |
| 589 | LY_CHECK_RET(lysp_stmt_validate_value(ctx, Y_STR_ARG, stmt->arg)); |
| 590 | arg_len = strlen(stmt->arg); |
| 591 | if ((arg_len == ly_strlen_const("true")) && !strncmp(stmt->arg, "true", arg_len)) { |
| 592 | *flags |= LYS_CONFIG_W; |
| 593 | } else if ((arg_len == ly_strlen_const("false")) && !strncmp(stmt->arg, "false", arg_len)) { |
| 594 | *flags |= LYS_CONFIG_R; |
| 595 | } else { |
| 596 | LOGVAL_PARSER(ctx, LY_VCODE_INVAL, arg_len, stmt->arg, "config"); |
| 597 | return LY_EVALID; |
| 598 | } |
| 599 | |
| 600 | for (const struct lysp_stmt *child = stmt->child; child; child = child->next) { |
| 601 | switch (child->kw) { |
| 602 | case LY_STMT_EXTENSION_INSTANCE: |
Radek Krejci | fc596f9 | 2021-02-26 22:40:26 +0100 | [diff] [blame] | 603 | 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] | 604 | break; |
| 605 | default: |
| 606 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(child->kw), "config"); |
| 607 | return LY_EVALID; |
| 608 | } |
| 609 | } |
| 610 | |
| 611 | return LY_SUCCESS; |
| 612 | } |
| 613 | |
| 614 | /** |
| 615 | * @brief Parse the mandatory statement. |
| 616 | * |
| 617 | * @param[in] ctx parser context. |
| 618 | * @param[in] stmt Source statement data from the parsed extension instance. |
| 619 | * @param[in,out] flags Flags to add to. |
| 620 | * @param[in,out] exts Extension instances to add to. |
| 621 | * |
| 622 | * @return LY_ERR values. |
| 623 | */ |
| 624 | static LY_ERR |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 625 | lysp_stmt_mandatory(struct lys_parser_ctx *ctx, const struct lysp_stmt *stmt, uint16_t *flags, |
| 626 | struct lysp_ext_instance **exts) |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 627 | { |
| 628 | size_t arg_len; |
| 629 | |
| 630 | if (*flags & LYS_MAND_MASK) { |
| 631 | LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "mandatory"); |
| 632 | return LY_EVALID; |
| 633 | } |
| 634 | |
| 635 | LY_CHECK_RET(lysp_stmt_validate_value(ctx, Y_STR_ARG, stmt->arg)); |
| 636 | arg_len = strlen(stmt->arg); |
| 637 | if ((arg_len == ly_strlen_const("true")) && !strncmp(stmt->arg, "true", arg_len)) { |
| 638 | *flags |= LYS_MAND_TRUE; |
| 639 | } else if ((arg_len == ly_strlen_const("false")) && !strncmp(stmt->arg, "false", arg_len)) { |
| 640 | *flags |= LYS_MAND_FALSE; |
| 641 | } else { |
| 642 | LOGVAL_PARSER(ctx, LY_VCODE_INVAL, arg_len, stmt->arg, "mandatory"); |
| 643 | return LY_EVALID; |
| 644 | } |
| 645 | |
| 646 | for (const struct lysp_stmt *child = stmt->child; child; child = child->next) { |
| 647 | switch (child->kw) { |
| 648 | case LY_STMT_EXTENSION_INSTANCE: |
Radek Krejci | fc596f9 | 2021-02-26 22:40:26 +0100 | [diff] [blame] | 649 | 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] | 650 | break; |
| 651 | default: |
| 652 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(child->kw), "mandatory"); |
| 653 | return LY_EVALID; |
| 654 | } |
| 655 | } |
| 656 | |
| 657 | return LY_SUCCESS; |
| 658 | } |
| 659 | |
| 660 | /** |
| 661 | * @brief Parse a restriction such as range or length. |
| 662 | * |
| 663 | * @param[in] ctx parser context. |
| 664 | * @param[in] stmt Source statement data from the parsed extension instance. |
| 665 | * @param[in,out] exts Extension instances to add to. |
| 666 | * |
| 667 | * @return LY_ERR values. |
| 668 | */ |
| 669 | static LY_ERR |
| 670 | lysp_stmt_restr(struct lys_parser_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_restr *restr) |
| 671 | { |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 672 | 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] | 673 | 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] | 674 | restr->arg.mod = PARSER_CUR_PMOD(ctx); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 675 | |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 676 | for (const struct lysp_stmt *child = stmt->child; child; child = child->next) { |
| 677 | switch (child->kw) { |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 678 | case LY_STMT_DESCRIPTION: |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 679 | 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] | 680 | break; |
| 681 | case LY_STMT_REFERENCE: |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 682 | 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] | 683 | break; |
| 684 | case LY_STMT_ERROR_APP_TAG: |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 685 | 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] | 686 | break; |
| 687 | case LY_STMT_ERROR_MESSAGE: |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 688 | 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] | 689 | break; |
| 690 | case LY_STMT_EXTENSION_INSTANCE: |
Radek Krejci | 39b7fc2 | 2021-02-26 23:29:18 +0100 | [diff] [blame] | 691 | 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] | 692 | break; |
| 693 | default: |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 694 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(child->kw), ly_stmt2str(stmt->kw)); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 695 | return LY_EVALID; |
| 696 | } |
| 697 | } |
| 698 | return LY_SUCCESS; |
| 699 | } |
| 700 | |
| 701 | /** |
| 702 | * @brief Parse a restriction that can have more instances such as must. |
| 703 | * |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 704 | * @param[in] ctx parser context. |
| 705 | * @param[in] stmt Source statement data from the parsed extension instance. |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 706 | * @param[in,out] restrs Restrictions to add to. |
| 707 | * |
| 708 | * @return LY_ERR values. |
| 709 | */ |
| 710 | static LY_ERR |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 711 | lysp_stmt_restrs(struct lys_parser_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_restr **restrs) |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 712 | { |
| 713 | struct lysp_restr *restr; |
| 714 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 715 | LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *restrs, restr, LY_EMEM); |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 716 | return lysp_stmt_restr(ctx, stmt, restr); |
| 717 | } |
| 718 | |
| 719 | /** |
| 720 | * @brief Parse the anydata or anyxml statement. |
| 721 | * |
| 722 | * @param[in] ctx parser context. |
| 723 | * @param[in] stmt Source statement data from the parsed extension instance. |
| 724 | * @param[in,out] siblings Siblings to add to. |
| 725 | * |
| 726 | * @return LY_ERR values. |
| 727 | */ |
| 728 | static LY_ERR |
| 729 | lysp_stmt_any(struct lys_parser_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_node *parent, struct lysp_node **siblings) |
| 730 | { |
| 731 | struct lysp_node_anydata *any; |
| 732 | |
| 733 | LY_CHECK_RET(lysp_stmt_validate_value(ctx, Y_IDENTIF_ARG, stmt->arg)); |
| 734 | |
| 735 | /* create new structure and insert into siblings */ |
| 736 | LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, any, next, LY_EMEM); |
| 737 | |
| 738 | any->nodetype = stmt->kw == LY_STMT_ANYDATA ? LYS_ANYDATA : LYS_ANYXML; |
| 739 | any->parent = parent; |
| 740 | |
| 741 | LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), stmt->arg, 0, &any->name)); |
| 742 | |
| 743 | /* parse substatements */ |
| 744 | for (const struct lysp_stmt *child = stmt->child; child; child = child->next) { |
| 745 | switch (child->kw) { |
| 746 | case LY_STMT_CONFIG: |
| 747 | LY_CHECK_RET(lysp_stmt_config(ctx, child, &any->flags, &any->exts)); |
| 748 | break; |
| 749 | case LY_STMT_DESCRIPTION: |
| 750 | LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &any->dsc, Y_STR_ARG, &any->exts)); |
| 751 | break; |
| 752 | case LY_STMT_IF_FEATURE: |
| 753 | LY_CHECK_RET(lysp_stmt_qnames(ctx, child, &any->iffeatures, Y_STR_ARG, &any->exts)); |
| 754 | break; |
| 755 | case LY_STMT_MANDATORY: |
| 756 | LY_CHECK_RET(lysp_stmt_mandatory(ctx, child, &any->flags, &any->exts)); |
| 757 | break; |
| 758 | case LY_STMT_MUST: |
| 759 | LY_CHECK_RET(lysp_stmt_restrs(ctx, child, &any->musts)); |
| 760 | break; |
| 761 | case LY_STMT_REFERENCE: |
| 762 | LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &any->ref, Y_STR_ARG, &any->exts)); |
| 763 | break; |
| 764 | case LY_STMT_STATUS: |
| 765 | LY_CHECK_RET(lysp_stmt_status(ctx, child, &any->flags, &any->exts)); |
| 766 | break; |
| 767 | case LY_STMT_WHEN: |
| 768 | LY_CHECK_RET(lysp_stmt_when(ctx, child, &any->when)); |
| 769 | break; |
| 770 | case LY_STMT_EXTENSION_INSTANCE: |
Radek Krejci | 39b7fc2 | 2021-02-26 23:29:18 +0100 | [diff] [blame] | 771 | 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] | 772 | break; |
| 773 | default: |
| 774 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(child->kw), |
| 775 | (any->nodetype & LYS_ANYDATA) == LYS_ANYDATA ? ly_stmt2str(LY_STMT_ANYDATA) : ly_stmt2str(LY_STMT_ANYXML)); |
| 776 | return LY_EVALID; |
| 777 | } |
| 778 | } |
| 779 | |
| 780 | return LY_SUCCESS; |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 781 | } |
| 782 | |
| 783 | /** |
| 784 | * @brief Parse the value or position statement. Substatement of type enum statement. |
| 785 | * |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 786 | * @param[in] ctx parser context. |
| 787 | * @param[in] stmt Source statement data from the parsed extension instance. |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 788 | * @param[in,out] value Value to write to. |
| 789 | * @param[in,out] flags Flags to write to. |
| 790 | * @param[in,out] exts Extension instances to add to. |
| 791 | * |
| 792 | * @return LY_ERR values. |
| 793 | */ |
| 794 | static LY_ERR |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 795 | lysp_stmt_type_enum_value_pos(struct lys_parser_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] | 796 | struct lysp_ext_instance **exts) |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 797 | { |
| 798 | size_t arg_len; |
| 799 | char *ptr = NULL; |
Michal Vasko | 73d77ab | 2021-07-23 12:45:55 +0200 | [diff] [blame] | 800 | long long int num = 0; |
| 801 | unsigned long long int unum = 0; |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 802 | |
| 803 | if (*flags & LYS_SET_VALUE) { |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 804 | LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(stmt->kw)); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 805 | return LY_EVALID; |
| 806 | } |
| 807 | *flags |= LYS_SET_VALUE; |
| 808 | |
| 809 | LY_CHECK_RET(lysp_stmt_validate_value(ctx, Y_STR_ARG, stmt->arg)); |
| 810 | |
| 811 | arg_len = strlen(stmt->arg); |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 812 | if (!arg_len || (stmt->arg[0] == '+') || ((stmt->arg[0] == '0') && (arg_len > 1)) || |
| 813 | ((stmt->kw == LY_STMT_POSITION) && !strncmp(stmt->arg, "-0", 2))) { |
| 814 | LOGVAL_PARSER(ctx, LY_VCODE_INVAL, arg_len, stmt->arg, ly_stmt2str(stmt->kw)); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 815 | goto error; |
| 816 | } |
| 817 | |
| 818 | errno = 0; |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 819 | if (stmt->kw == LY_STMT_VALUE) { |
Michal Vasko | 73d77ab | 2021-07-23 12:45:55 +0200 | [diff] [blame] | 820 | num = strtoll(stmt->arg, &ptr, LY_BASE_DEC); |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 821 | if ((num < INT64_C(-2147483648)) || (num > INT64_C(2147483647))) { |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 822 | LOGVAL_PARSER(ctx, LY_VCODE_INVAL, arg_len, stmt->arg, ly_stmt2str(stmt->kw)); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 823 | goto error; |
| 824 | } |
| 825 | } else { |
Michal Vasko | 73d77ab | 2021-07-23 12:45:55 +0200 | [diff] [blame] | 826 | unum = strtoull(stmt->arg, &ptr, LY_BASE_DEC); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 827 | if (unum > UINT64_C(4294967295)) { |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 828 | LOGVAL_PARSER(ctx, LY_VCODE_INVAL, arg_len, stmt->arg, ly_stmt2str(stmt->kw)); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 829 | goto error; |
| 830 | } |
| 831 | } |
| 832 | /* we have not parsed the whole argument */ |
| 833 | if ((size_t)(ptr - stmt->arg) != arg_len) { |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 834 | LOGVAL_PARSER(ctx, LY_VCODE_INVAL, arg_len, stmt->arg, ly_stmt2str(stmt->kw)); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 835 | goto error; |
| 836 | } |
| 837 | if (errno == ERANGE) { |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 838 | LOGVAL_PARSER(ctx, LY_VCODE_OOB, arg_len, stmt->arg, ly_stmt2str(stmt->kw)); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 839 | goto error; |
| 840 | } |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 841 | if (stmt->kw == LY_STMT_VALUE) { |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 842 | *value = num; |
| 843 | } else { |
| 844 | *value = unum; |
| 845 | } |
| 846 | |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 847 | for (const struct lysp_stmt *child = stmt->child; child; child = child->next) { |
| 848 | switch (child->kw) { |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 849 | case LY_STMT_EXTENSION_INSTANCE: |
Radek Krejci | fc596f9 | 2021-02-26 22:40:26 +0100 | [diff] [blame] | 850 | 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] | 851 | break; |
| 852 | default: |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 853 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(child->kw), ly_stmt2str(stmt->kw)); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 854 | return LY_EVALID; |
| 855 | } |
| 856 | } |
| 857 | return LY_SUCCESS; |
| 858 | |
| 859 | error: |
| 860 | return LY_EVALID; |
| 861 | } |
| 862 | |
| 863 | /** |
| 864 | * @brief Parse the enum or bit statement. Substatement of type statement. |
| 865 | * |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 866 | * @param[in] ctx parser context. |
| 867 | * @param[in] stmt Source statement data from the parsed extension instance. |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 868 | * @param[in,out] enums Enums or bits to add to. |
| 869 | * |
| 870 | * @return LY_ERR values. |
| 871 | */ |
| 872 | static LY_ERR |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 873 | lysp_stmt_type_enum(struct lys_parser_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_type_enum **enums) |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 874 | { |
| 875 | struct lysp_type_enum *enm; |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 876 | |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 877 | 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] | 878 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 879 | LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *enums, enm, LY_EMEM); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 880 | |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 881 | if (stmt->kw == LY_STMT_ENUM) { |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 882 | LY_CHECK_RET(lysp_check_enum_name(ctx, stmt->arg, strlen(stmt->arg))); |
| 883 | } /* else nothing specific for YANG_BIT */ |
| 884 | |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 885 | LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), stmt->arg, 0, &enm->name)); |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 886 | CHECK_UNIQUENESS(ctx, *enums, name, ly_stmt2str(stmt->kw), enm->name); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 887 | |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 888 | for (const struct lysp_stmt *child = stmt->child; child; child = child->next) { |
| 889 | switch (child->kw) { |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 890 | case LY_STMT_DESCRIPTION: |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 891 | 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] | 892 | break; |
| 893 | case LY_STMT_IF_FEATURE: |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 894 | PARSER_CHECK_STMTVER2_RET(ctx, "if-feature", ly_stmt2str(stmt->kw)); |
| 895 | 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] | 896 | break; |
| 897 | case LY_STMT_REFERENCE: |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 898 | 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] | 899 | break; |
| 900 | case LY_STMT_STATUS: |
| 901 | LY_CHECK_RET(lysp_stmt_status(ctx, child, &enm->flags, &enm->exts)); |
| 902 | break; |
| 903 | case LY_STMT_VALUE: |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 904 | LY_CHECK_ERR_RET(stmt->kw == LY_STMT_BIT, LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(child->kw), |
| 905 | ly_stmt2str(stmt->kw)), LY_EVALID); |
| 906 | 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] | 907 | break; |
| 908 | case LY_STMT_POSITION: |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 909 | LY_CHECK_ERR_RET(stmt->kw == LY_STMT_ENUM, LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(child->kw), |
| 910 | ly_stmt2str(stmt->kw)), LY_EVALID); |
| 911 | 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] | 912 | break; |
| 913 | case LY_STMT_EXTENSION_INSTANCE: |
Radek Krejci | 39b7fc2 | 2021-02-26 23:29:18 +0100 | [diff] [blame] | 914 | 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] | 915 | break; |
| 916 | default: |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 917 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(child->kw), ly_stmt2str(stmt->kw)); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 918 | return LY_EVALID; |
| 919 | } |
| 920 | } |
| 921 | return LY_SUCCESS; |
| 922 | } |
| 923 | |
| 924 | /** |
| 925 | * @brief Parse the fraction-digits statement. Substatement of type statement. |
| 926 | * |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 927 | * @param[in] ctx parser context. |
| 928 | * @param[in] stmt Source statement data from the parsed extension instance. |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 929 | * @param[in,out] fracdig Value to write to. |
| 930 | * @param[in,out] exts Extension instances to add to. |
| 931 | * |
| 932 | * @return LY_ERR values. |
| 933 | */ |
| 934 | static LY_ERR |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 935 | lysp_stmt_type_fracdigits(struct lys_parser_ctx *ctx, const struct lysp_stmt *stmt, uint8_t *fracdig, |
| 936 | struct lysp_ext_instance **exts) |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 937 | { |
| 938 | char *ptr; |
| 939 | size_t arg_len; |
Michal Vasko | 73d77ab | 2021-07-23 12:45:55 +0200 | [diff] [blame] | 940 | unsigned long long int num; |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 941 | |
| 942 | if (*fracdig) { |
| 943 | LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "fraction-digits"); |
| 944 | return LY_EVALID; |
| 945 | } |
| 946 | |
| 947 | LY_CHECK_RET(lysp_stmt_validate_value(ctx, Y_STR_ARG, stmt->arg)); |
| 948 | arg_len = strlen(stmt->arg); |
| 949 | if (!arg_len || (stmt->arg[0] == '0') || !isdigit(stmt->arg[0])) { |
| 950 | LOGVAL_PARSER(ctx, LY_VCODE_INVAL, arg_len, stmt->arg, "fraction-digits"); |
| 951 | return LY_EVALID; |
| 952 | } |
| 953 | |
| 954 | errno = 0; |
Michal Vasko | 73d77ab | 2021-07-23 12:45:55 +0200 | [diff] [blame] | 955 | num = strtoull(stmt->arg, &ptr, LY_BASE_DEC); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 956 | /* we have not parsed the whole argument */ |
| 957 | if ((size_t)(ptr - stmt->arg) != arg_len) { |
| 958 | LOGVAL_PARSER(ctx, LY_VCODE_INVAL, arg_len, stmt->arg, "fraction-digits"); |
| 959 | return LY_EVALID; |
| 960 | } |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 961 | if ((errno == ERANGE) || (num > LY_TYPE_DEC64_FD_MAX)) { |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 962 | LOGVAL_PARSER(ctx, LY_VCODE_OOB, arg_len, stmt->arg, "fraction-digits"); |
| 963 | return LY_EVALID; |
| 964 | } |
| 965 | *fracdig = num; |
| 966 | |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 967 | for (const struct lysp_stmt *child = stmt->child; child; child = child->next) { |
| 968 | switch (child->kw) { |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 969 | case LY_STMT_EXTENSION_INSTANCE: |
Radek Krejci | fc596f9 | 2021-02-26 22:40:26 +0100 | [diff] [blame] | 970 | 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] | 971 | break; |
| 972 | default: |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 973 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(child->kw), "fraction-digits"); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 974 | return LY_EVALID; |
| 975 | } |
| 976 | } |
| 977 | return LY_SUCCESS; |
| 978 | } |
| 979 | |
| 980 | /** |
| 981 | * @brief Parse the require-instance statement. Substatement of type statement. |
| 982 | * |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 983 | * @param[in] ctx parser context. |
| 984 | * @param[in] stmt Source statement data from the parsed extension instance. |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 985 | * @param[in,out] reqinst Value to write to. |
| 986 | * @param[in,out] flags Flags to write to. |
| 987 | * @param[in,out] exts Extension instances to add to. |
| 988 | * |
| 989 | * @return LY_ERR values. |
| 990 | */ |
| 991 | static LY_ERR |
| 992 | lysp_stmt_type_reqinstance(struct lys_parser_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] | 993 | struct lysp_ext_instance **exts) |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 994 | { |
| 995 | size_t arg_len; |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 996 | |
| 997 | if (*flags & LYS_SET_REQINST) { |
| 998 | LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "require-instance"); |
| 999 | return LY_EVALID; |
| 1000 | } |
| 1001 | *flags |= LYS_SET_REQINST; |
| 1002 | |
| 1003 | LY_CHECK_RET(lysp_stmt_validate_value(ctx, Y_STR_ARG, stmt->arg)); |
| 1004 | arg_len = strlen(stmt->arg); |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1005 | 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] | 1006 | *reqinst = 1; |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1007 | } 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] | 1008 | LOGVAL_PARSER(ctx, LY_VCODE_INVAL, arg_len, stmt->arg, "require-instance"); |
| 1009 | return LY_EVALID; |
| 1010 | } |
| 1011 | |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 1012 | for (const struct lysp_stmt *child = stmt->child; child; child = child->next) { |
| 1013 | switch (child->kw) { |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1014 | case LY_STMT_EXTENSION_INSTANCE: |
Radek Krejci | fc596f9 | 2021-02-26 22:40:26 +0100 | [diff] [blame] | 1015 | 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] | 1016 | break; |
| 1017 | default: |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 1018 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(child->kw), "require-instance"); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1019 | return LY_EVALID; |
| 1020 | } |
| 1021 | } |
| 1022 | return LY_SUCCESS; |
| 1023 | } |
| 1024 | |
| 1025 | /** |
| 1026 | * @brief Parse the modifier statement. Substatement of type pattern statement. |
| 1027 | * |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 1028 | * @param[in] ctx parser context. |
| 1029 | * @param[in] stmt Source statement data from the parsed extension instance. |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1030 | * @param[in,out] pat Value to write to. |
| 1031 | * @param[in,out] exts Extension instances to add to. |
| 1032 | * |
| 1033 | * @return LY_ERR values. |
| 1034 | */ |
| 1035 | static LY_ERR |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 1036 | lysp_stmt_type_pattern_modifier(struct lys_parser_ctx *ctx, const struct lysp_stmt *stmt, const char **pat, |
| 1037 | struct lysp_ext_instance **exts) |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1038 | { |
| 1039 | size_t arg_len; |
| 1040 | char *buf; |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1041 | |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1042 | if ((*pat)[0] == LYSP_RESTR_PATTERN_NACK) { |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1043 | LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "modifier"); |
| 1044 | return LY_EVALID; |
| 1045 | } |
| 1046 | |
| 1047 | LY_CHECK_RET(lysp_stmt_validate_value(ctx, Y_STR_ARG, stmt->arg)); |
| 1048 | arg_len = strlen(stmt->arg); |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1049 | 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] | 1050 | LOGVAL_PARSER(ctx, LY_VCODE_INVAL, arg_len, stmt->arg, "modifier"); |
| 1051 | return LY_EVALID; |
| 1052 | } |
| 1053 | |
| 1054 | /* replace the value in the dictionary */ |
| 1055 | buf = malloc(strlen(*pat) + 1); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1056 | LY_CHECK_ERR_RET(!buf, LOGMEM(PARSER_CTX(ctx)), LY_EMEM); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1057 | strcpy(buf, *pat); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1058 | lydict_remove(PARSER_CTX(ctx), *pat); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1059 | |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1060 | assert(buf[0] == LYSP_RESTR_PATTERN_ACK); |
| 1061 | buf[0] = LYSP_RESTR_PATTERN_NACK; |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 1062 | LY_CHECK_RET(lydict_insert_zc(PARSER_CTX(ctx), buf, pat)); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1063 | |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 1064 | for (const struct lysp_stmt *child = stmt->child; child; child = child->next) { |
| 1065 | switch (child->kw) { |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1066 | case LY_STMT_EXTENSION_INSTANCE: |
Radek Krejci | fc596f9 | 2021-02-26 22:40:26 +0100 | [diff] [blame] | 1067 | 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] | 1068 | break; |
| 1069 | default: |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 1070 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(child->kw), "modifier"); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1071 | return LY_EVALID; |
| 1072 | } |
| 1073 | } |
| 1074 | return LY_SUCCESS; |
| 1075 | } |
| 1076 | |
| 1077 | /** |
| 1078 | * @brief Parse the pattern statement. Substatement of type statement. |
| 1079 | * |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 1080 | * @param[in] ctx parser context. |
| 1081 | * @param[in] stmt Source statement data from the parsed extension instance. |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1082 | * @param[in,out] patterns Restrictions to add to. |
| 1083 | * |
| 1084 | * @return LY_ERR values. |
| 1085 | */ |
| 1086 | static LY_ERR |
| 1087 | lysp_stmt_type_pattern(struct lys_parser_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_restr **patterns) |
| 1088 | { |
| 1089 | char *buf; |
| 1090 | size_t arg_len; |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1091 | struct lysp_restr *restr; |
| 1092 | |
| 1093 | 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] | 1094 | LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *patterns, restr, LY_EMEM); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1095 | arg_len = strlen(stmt->arg); |
| 1096 | |
| 1097 | /* add special meaning first byte */ |
| 1098 | buf = malloc(arg_len + 2); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1099 | LY_CHECK_ERR_RET(!buf, LOGMEM(PARSER_CTX(ctx)), LY_EMEM); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1100 | memmove(buf + 1, stmt->arg, arg_len); |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1101 | buf[0] = LYSP_RESTR_PATTERN_ACK; /* pattern's default regular-match flag */ |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1102 | buf[arg_len + 1] = '\0'; /* terminating NULL byte */ |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 1103 | 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] | 1104 | restr->arg.mod = PARSER_CUR_PMOD(ctx); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1105 | |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 1106 | for (const struct lysp_stmt *child = stmt->child; child; child = child->next) { |
| 1107 | switch (child->kw) { |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1108 | case LY_STMT_DESCRIPTION: |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 1109 | 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] | 1110 | break; |
| 1111 | case LY_STMT_REFERENCE: |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 1112 | 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] | 1113 | break; |
| 1114 | case LY_STMT_ERROR_APP_TAG: |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 1115 | 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] | 1116 | break; |
| 1117 | case LY_STMT_ERROR_MESSAGE: |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 1118 | 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] | 1119 | break; |
| 1120 | case LY_STMT_MODIFIER: |
| 1121 | PARSER_CHECK_STMTVER2_RET(ctx, "modifier", "pattern"); |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 1122 | 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] | 1123 | break; |
| 1124 | case LY_STMT_EXTENSION_INSTANCE: |
Radek Krejci | 39b7fc2 | 2021-02-26 23:29:18 +0100 | [diff] [blame] | 1125 | 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] | 1126 | break; |
| 1127 | default: |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 1128 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(child->kw), "pattern"); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1129 | return LY_EVALID; |
| 1130 | } |
| 1131 | } |
| 1132 | return LY_SUCCESS; |
| 1133 | } |
| 1134 | |
| 1135 | /** |
| 1136 | * @brief Parse the type statement. |
| 1137 | * |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 1138 | * @param[in] ctx parser context. |
| 1139 | * @param[in] stmt Source statement data from the parsed extension instance. |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1140 | * @param[in,out] type Type to wrote to. |
| 1141 | * |
| 1142 | * @return LY_ERR values. |
| 1143 | */ |
| 1144 | static LY_ERR |
| 1145 | lysp_stmt_type(struct lys_parser_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_type *type) |
| 1146 | { |
| 1147 | struct lysp_type *nest_type; |
Radek Krejci | b124784 | 2020-07-02 16:22:38 +0200 | [diff] [blame] | 1148 | const char *str_path = NULL; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1149 | LY_ERR ret; |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1150 | |
| 1151 | if (type->name) { |
| 1152 | LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "type"); |
| 1153 | return LY_EVALID; |
| 1154 | } |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 1155 | |
| 1156 | 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] | 1157 | 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] | 1158 | type->pmod = PARSER_CUR_PMOD(ctx); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1159 | |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 1160 | for (const struct lysp_stmt *child = stmt->child; child; child = child->next) { |
| 1161 | switch (child->kw) { |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1162 | case LY_STMT_BASE: |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 1163 | 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] | 1164 | type->flags |= LYS_SET_BASE; |
| 1165 | break; |
| 1166 | case LY_STMT_BIT: |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 1167 | LY_CHECK_RET(lysp_stmt_type_enum(ctx, child, &type->bits)); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1168 | type->flags |= LYS_SET_BIT; |
| 1169 | break; |
| 1170 | case LY_STMT_ENUM: |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 1171 | LY_CHECK_RET(lysp_stmt_type_enum(ctx, child, &type->enums)); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1172 | type->flags |= LYS_SET_ENUM; |
| 1173 | break; |
| 1174 | case LY_STMT_FRACTION_DIGITS: |
| 1175 | LY_CHECK_RET(lysp_stmt_type_fracdigits(ctx, child, &type->fraction_digits, &type->exts)); |
| 1176 | type->flags |= LYS_SET_FRDIGITS; |
| 1177 | break; |
| 1178 | case LY_STMT_LENGTH: |
| 1179 | if (type->length) { |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 1180 | LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(child->kw)); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1181 | return LY_EVALID; |
| 1182 | } |
| 1183 | type->length = calloc(1, sizeof *type->length); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1184 | LY_CHECK_ERR_RET(!type->length, LOGMEM(PARSER_CTX(ctx)), LY_EMEM); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1185 | |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 1186 | LY_CHECK_RET(lysp_stmt_restr(ctx, child, type->length)); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1187 | type->flags |= LYS_SET_LENGTH; |
| 1188 | break; |
| 1189 | case LY_STMT_PATH: |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 1190 | 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] | 1191 | 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] | 1192 | LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_LEAFREF, &type->path); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1193 | lydict_remove(PARSER_CTX(ctx), str_path); |
| 1194 | LY_CHECK_RET(ret); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1195 | type->flags |= LYS_SET_PATH; |
| 1196 | break; |
| 1197 | case LY_STMT_PATTERN: |
| 1198 | LY_CHECK_RET(lysp_stmt_type_pattern(ctx, child, &type->patterns)); |
| 1199 | type->flags |= LYS_SET_PATTERN; |
| 1200 | break; |
| 1201 | case LY_STMT_RANGE: |
| 1202 | if (type->range) { |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 1203 | LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(child->kw)); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1204 | return LY_EVALID; |
| 1205 | } |
| 1206 | type->range = calloc(1, sizeof *type->range); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1207 | LY_CHECK_ERR_RET(!type->range, LOGMEM(PARSER_CTX(ctx)), LY_EMEM); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1208 | |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 1209 | LY_CHECK_RET(lysp_stmt_restr(ctx, child, type->range)); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1210 | type->flags |= LYS_SET_RANGE; |
| 1211 | break; |
| 1212 | case LY_STMT_REQUIRE_INSTANCE: |
| 1213 | 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] | 1214 | /* LYS_SET_REQINST checked and set inside lysp_stmt_type_reqinstance() */ |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1215 | break; |
| 1216 | case LY_STMT_TYPE: |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1217 | 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] | 1218 | LY_CHECK_RET(lysp_stmt_type(ctx, child, nest_type)); |
| 1219 | type->flags |= LYS_SET_TYPE; |
| 1220 | break; |
| 1221 | case LY_STMT_EXTENSION_INSTANCE: |
Radek Krejci | 39b7fc2 | 2021-02-26 23:29:18 +0100 | [diff] [blame] | 1222 | 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] | 1223 | break; |
| 1224 | default: |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 1225 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(child->kw), "type"); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1226 | return LY_EVALID; |
| 1227 | } |
| 1228 | } |
| 1229 | return LY_SUCCESS; |
| 1230 | } |
| 1231 | |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 1232 | /** |
| 1233 | * @brief Parse the leaf statement. |
| 1234 | * |
| 1235 | * @param[in] ctx parser context. |
| 1236 | * @param[in] stmt Source statement data from the parsed extension instance. |
| 1237 | * @param[in] parent Parent node to connect to (not into). |
| 1238 | * @param[in,out] siblings Siblings to add to. |
| 1239 | * |
| 1240 | * @return LY_ERR values. |
| 1241 | */ |
| 1242 | static LY_ERR |
| 1243 | lysp_stmt_leaf(struct lys_parser_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_node *parent, struct lysp_node **siblings) |
| 1244 | { |
| 1245 | struct lysp_node_leaf *leaf; |
| 1246 | |
| 1247 | LY_CHECK_RET(lysp_stmt_validate_value(ctx, Y_IDENTIF_ARG, stmt->arg)); |
| 1248 | |
| 1249 | /* create new leaf structure */ |
| 1250 | LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, leaf, next, LY_EMEM); |
| 1251 | leaf->nodetype = LYS_LEAF; |
| 1252 | leaf->parent = parent; |
| 1253 | |
| 1254 | /* get name */ |
| 1255 | LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), stmt->arg, 0, &leaf->name)); |
| 1256 | |
| 1257 | /* parse substatements */ |
| 1258 | for (const struct lysp_stmt *child = stmt->child; child; child = child->next) { |
| 1259 | switch (child->kw) { |
| 1260 | case LY_STMT_CONFIG: |
| 1261 | LY_CHECK_RET(lysp_stmt_config(ctx, child, &leaf->flags, &leaf->exts)); |
| 1262 | break; |
| 1263 | case LY_STMT_DEFAULT: |
| 1264 | 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] | 1265 | leaf->dflt.mod = PARSER_CUR_PMOD(ctx); |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 1266 | break; |
| 1267 | case LY_STMT_DESCRIPTION: |
| 1268 | LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &leaf->dsc, Y_STR_ARG, &leaf->exts)); |
| 1269 | break; |
| 1270 | case LY_STMT_IF_FEATURE: |
| 1271 | LY_CHECK_RET(lysp_stmt_qnames(ctx, child, &leaf->iffeatures, Y_STR_ARG, &leaf->exts)); |
| 1272 | break; |
| 1273 | case LY_STMT_MANDATORY: |
| 1274 | LY_CHECK_RET(lysp_stmt_mandatory(ctx, child, &leaf->flags, &leaf->exts)); |
| 1275 | break; |
| 1276 | case LY_STMT_MUST: |
| 1277 | LY_CHECK_RET(lysp_stmt_restrs(ctx, child, &leaf->musts)); |
| 1278 | break; |
| 1279 | case LY_STMT_REFERENCE: |
| 1280 | LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &leaf->ref, Y_STR_ARG, &leaf->exts)); |
| 1281 | break; |
| 1282 | case LY_STMT_STATUS: |
| 1283 | LY_CHECK_RET(lysp_stmt_status(ctx, child, &leaf->flags, &leaf->exts)); |
| 1284 | break; |
| 1285 | case LY_STMT_TYPE: |
| 1286 | LY_CHECK_RET(lysp_stmt_type(ctx, child, &leaf->type)); |
| 1287 | break; |
| 1288 | case LY_STMT_UNITS: |
| 1289 | LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &leaf->units, Y_STR_ARG, &leaf->exts)); |
| 1290 | break; |
| 1291 | case LY_STMT_WHEN: |
| 1292 | LY_CHECK_RET(lysp_stmt_when(ctx, child, &leaf->when)); |
| 1293 | break; |
| 1294 | case LY_STMT_EXTENSION_INSTANCE: |
Radek Krejci | 39b7fc2 | 2021-02-26 23:29:18 +0100 | [diff] [blame] | 1295 | 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] | 1296 | break; |
| 1297 | default: |
| 1298 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(child->kw), "leaf"); |
| 1299 | return LY_EVALID; |
| 1300 | } |
| 1301 | } |
| 1302 | |
| 1303 | /* mandatory substatements */ |
| 1304 | if (!leaf->type.name) { |
| 1305 | LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "leaf"); |
| 1306 | return LY_EVALID; |
| 1307 | } |
| 1308 | |
| 1309 | return LY_SUCCESS; |
| 1310 | } |
| 1311 | |
| 1312 | /** |
| 1313 | * @brief Parse the max-elements statement. |
| 1314 | * |
| 1315 | * @param[in] ctx parser context. |
| 1316 | * @param[in] stmt Source statement data from the parsed extension instance. |
| 1317 | * @param[in,out] max Value to write to. |
| 1318 | * @param[in,out] flags Flags to write to. |
| 1319 | * @param[in,out] exts Extension instances to add to. |
| 1320 | * |
| 1321 | * @return LY_ERR values. |
| 1322 | */ |
| 1323 | static LY_ERR |
| 1324 | lysp_stmt_maxelements(struct lys_parser_ctx *ctx, const struct lysp_stmt *stmt, |
| 1325 | uint32_t *max, uint16_t *flags, struct lysp_ext_instance **exts) |
| 1326 | { |
| 1327 | size_t arg_len; |
| 1328 | char *ptr; |
Michal Vasko | 73d77ab | 2021-07-23 12:45:55 +0200 | [diff] [blame] | 1329 | unsigned long long int num; |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 1330 | |
| 1331 | if (*flags & LYS_SET_MAX) { |
| 1332 | LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "max-elements"); |
| 1333 | return LY_EVALID; |
| 1334 | } |
| 1335 | *flags |= LYS_SET_MAX; |
| 1336 | |
| 1337 | /* get value */ |
| 1338 | LY_CHECK_RET(lysp_stmt_validate_value(ctx, Y_STR_ARG, stmt->arg)); |
| 1339 | arg_len = strlen(stmt->arg); |
| 1340 | |
| 1341 | if (!arg_len || (stmt->arg[0] == '0') || ((stmt->arg[0] != 'u') && !isdigit(stmt->arg[0]))) { |
| 1342 | LOGVAL_PARSER(ctx, LY_VCODE_INVAL, arg_len, stmt->arg, "max-elements"); |
| 1343 | return LY_EVALID; |
| 1344 | } |
| 1345 | |
| 1346 | if ((arg_len != ly_strlen_const("unbounded")) || strncmp(stmt->arg, "unbounded", arg_len)) { |
| 1347 | errno = 0; |
Michal Vasko | 73d77ab | 2021-07-23 12:45:55 +0200 | [diff] [blame] | 1348 | num = strtoull(stmt->arg, &ptr, LY_BASE_DEC); |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 1349 | /* we have not parsed the whole argument */ |
| 1350 | if ((size_t)(ptr - stmt->arg) != arg_len) { |
| 1351 | LOGVAL_PARSER(ctx, LY_VCODE_INVAL, arg_len, stmt->arg, "max-elements"); |
| 1352 | return LY_EVALID; |
| 1353 | } |
| 1354 | if ((errno == ERANGE) || (num > UINT32_MAX)) { |
| 1355 | LOGVAL_PARSER(ctx, LY_VCODE_OOB, arg_len, stmt->arg, "max-elements"); |
| 1356 | return LY_EVALID; |
| 1357 | } |
| 1358 | |
| 1359 | *max = num; |
| 1360 | } else { |
| 1361 | /* unbounded */ |
| 1362 | *max = 0; |
| 1363 | } |
| 1364 | |
| 1365 | for (const struct lysp_stmt *child = stmt->child; child; child = child->next) { |
| 1366 | switch (child->kw) { |
| 1367 | case LY_STMT_EXTENSION_INSTANCE: |
Radek Krejci | fc596f9 | 2021-02-26 22:40:26 +0100 | [diff] [blame] | 1368 | 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] | 1369 | break; |
| 1370 | default: |
| 1371 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(child->kw), "max-elements"); |
| 1372 | return LY_EVALID; |
| 1373 | } |
| 1374 | } |
| 1375 | |
| 1376 | return LY_SUCCESS; |
| 1377 | } |
| 1378 | |
| 1379 | /** |
| 1380 | * @brief Parse the min-elements statement. |
| 1381 | * |
| 1382 | * @param[in] ctx parser context. |
| 1383 | * @param[in] stmt Source statement data from the parsed extension instance. |
| 1384 | * @param[in,out] min Value to write to. |
| 1385 | * @param[in,out] flags Flags to write to. |
| 1386 | * @param[in,out] exts Extension instances to add to. |
| 1387 | * |
| 1388 | * @return LY_ERR values. |
| 1389 | */ |
| 1390 | static LY_ERR |
| 1391 | lysp_stmt_minelements(struct lys_parser_ctx *ctx, const struct lysp_stmt *stmt, |
| 1392 | uint32_t *min, uint16_t *flags, struct lysp_ext_instance **exts) |
| 1393 | { |
| 1394 | size_t arg_len; |
| 1395 | char *ptr; |
Michal Vasko | 73d77ab | 2021-07-23 12:45:55 +0200 | [diff] [blame] | 1396 | unsigned long long int num; |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 1397 | |
| 1398 | if (*flags & LYS_SET_MIN) { |
| 1399 | LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "min-elements"); |
| 1400 | return LY_EVALID; |
| 1401 | } |
| 1402 | *flags |= LYS_SET_MIN; |
| 1403 | |
| 1404 | /* get value */ |
| 1405 | LY_CHECK_RET(lysp_stmt_validate_value(ctx, Y_STR_ARG, stmt->arg)); |
| 1406 | arg_len = strlen(stmt->arg); |
| 1407 | |
| 1408 | if (!arg_len || !isdigit(stmt->arg[0]) || ((stmt->arg[0] == '0') && (arg_len > 1))) { |
| 1409 | LOGVAL_PARSER(ctx, LY_VCODE_INVAL, arg_len, stmt->arg, "min-elements"); |
| 1410 | return LY_EVALID; |
| 1411 | } |
| 1412 | |
| 1413 | errno = 0; |
Michal Vasko | 73d77ab | 2021-07-23 12:45:55 +0200 | [diff] [blame] | 1414 | num = strtoull(stmt->arg, &ptr, LY_BASE_DEC); |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 1415 | /* we have not parsed the whole argument */ |
| 1416 | if ((size_t)(ptr - stmt->arg) != arg_len) { |
| 1417 | LOGVAL_PARSER(ctx, LY_VCODE_INVAL, arg_len, stmt->arg, "min-elements"); |
| 1418 | return LY_EVALID; |
| 1419 | } |
| 1420 | if ((errno == ERANGE) || (num > UINT32_MAX)) { |
| 1421 | LOGVAL_PARSER(ctx, LY_VCODE_OOB, arg_len, stmt->arg, "min-elements"); |
| 1422 | return LY_EVALID; |
| 1423 | } |
| 1424 | *min = num; |
| 1425 | |
| 1426 | for (const struct lysp_stmt *child = stmt->child; child; child = child->next) { |
| 1427 | switch (child->kw) { |
| 1428 | case LY_STMT_EXTENSION_INSTANCE: |
Radek Krejci | fc596f9 | 2021-02-26 22:40:26 +0100 | [diff] [blame] | 1429 | 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] | 1430 | break; |
| 1431 | default: |
| 1432 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(child->kw), "min-elements"); |
| 1433 | return LY_EVALID; |
| 1434 | } |
| 1435 | } |
| 1436 | |
| 1437 | return LY_SUCCESS; |
| 1438 | } |
| 1439 | |
| 1440 | /** |
| 1441 | * @brief Parse the ordered-by statement. |
| 1442 | * |
| 1443 | * @param[in] ctx parser context. |
| 1444 | * @param[in] stmt Source statement data from the parsed extension instance. |
| 1445 | * @param[in,out] flags Flags to write to. |
| 1446 | * @param[in,out] exts Extension instances to add to. |
| 1447 | * |
| 1448 | * @return LY_ERR values. |
| 1449 | */ |
| 1450 | static LY_ERR |
| 1451 | lysp_stmt_orderedby(struct lys_parser_ctx *ctx, const struct lysp_stmt *stmt, uint16_t *flags, struct lysp_ext_instance **exts) |
| 1452 | { |
| 1453 | size_t arg_len; |
| 1454 | |
| 1455 | if (*flags & LYS_ORDBY_MASK) { |
| 1456 | LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "ordered-by"); |
| 1457 | return LY_EVALID; |
| 1458 | } |
| 1459 | |
| 1460 | /* get value */ |
| 1461 | LY_CHECK_RET(lysp_stmt_validate_value(ctx, Y_STR_ARG, stmt->arg)); |
| 1462 | arg_len = strlen(stmt->arg); |
| 1463 | if ((arg_len == ly_strlen_const("system")) && !strncmp(stmt->arg, "system", arg_len)) { |
| 1464 | *flags |= LYS_MAND_TRUE; |
| 1465 | } else if ((arg_len == ly_strlen_const("user")) && !strncmp(stmt->arg, "user", arg_len)) { |
| 1466 | *flags |= LYS_MAND_FALSE; |
| 1467 | } else { |
| 1468 | LOGVAL_PARSER(ctx, LY_VCODE_INVAL, arg_len, stmt->arg, "ordered-by"); |
| 1469 | return LY_EVALID; |
| 1470 | } |
| 1471 | |
| 1472 | for (const struct lysp_stmt *child = stmt->child; child; child = child->next) { |
| 1473 | switch (child->kw) { |
| 1474 | case LY_STMT_EXTENSION_INSTANCE: |
Radek Krejci | fc596f9 | 2021-02-26 22:40:26 +0100 | [diff] [blame] | 1475 | 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] | 1476 | break; |
| 1477 | default: |
| 1478 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(child->kw), "ordered-by"); |
| 1479 | return LY_EVALID; |
| 1480 | } |
| 1481 | } |
| 1482 | |
| 1483 | return LY_SUCCESS; |
| 1484 | } |
| 1485 | |
| 1486 | /** |
| 1487 | * @brief Parse the leaf-list statement. |
| 1488 | * |
| 1489 | * @param[in] ctx parser context. |
| 1490 | * @param[in] stmt Source statement data from the parsed extension instance. |
| 1491 | * @param[in] parent Parent node to connect to (not into). |
| 1492 | * @param[in,out] siblings Siblings to add to. |
| 1493 | * |
| 1494 | * @return LY_ERR values. |
| 1495 | */ |
| 1496 | static LY_ERR |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 1497 | lysp_stmt_leaflist(struct lys_parser_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_node *parent, |
| 1498 | struct lysp_node **siblings) |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 1499 | { |
| 1500 | struct lysp_node_leaflist *llist; |
| 1501 | |
| 1502 | LY_CHECK_RET(lysp_stmt_validate_value(ctx, Y_IDENTIF_ARG, stmt->arg)); |
| 1503 | |
| 1504 | /* create new leaf-list structure */ |
| 1505 | LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, llist, next, LY_EMEM); |
| 1506 | llist->nodetype = LYS_LEAFLIST; |
| 1507 | llist->parent = parent; |
| 1508 | |
| 1509 | /* get name */ |
| 1510 | LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), stmt->arg, 0, &llist->name)); |
| 1511 | |
| 1512 | /* parse substatements */ |
| 1513 | for (const struct lysp_stmt *child = stmt->child; child; child = child->next) { |
| 1514 | switch (child->kw) { |
| 1515 | case LY_STMT_CONFIG: |
| 1516 | LY_CHECK_RET(lysp_stmt_config(ctx, child, &llist->flags, &llist->exts)); |
| 1517 | break; |
| 1518 | case LY_STMT_DEFAULT: |
| 1519 | PARSER_CHECK_STMTVER2_RET(ctx, "default", "leaf-list"); |
| 1520 | LY_CHECK_RET(lysp_stmt_qnames(ctx, child, &llist->dflts, Y_STR_ARG, &llist->exts)); |
| 1521 | break; |
| 1522 | case LY_STMT_DESCRIPTION: |
| 1523 | LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &llist->dsc, Y_STR_ARG, &llist->exts)); |
| 1524 | break; |
| 1525 | case LY_STMT_IF_FEATURE: |
| 1526 | LY_CHECK_RET(lysp_stmt_qnames(ctx, child, &llist->iffeatures, Y_STR_ARG, &llist->exts)); |
| 1527 | break; |
| 1528 | case LY_STMT_MAX_ELEMENTS: |
| 1529 | LY_CHECK_RET(lysp_stmt_maxelements(ctx, child, &llist->max, &llist->flags, &llist->exts)); |
| 1530 | break; |
| 1531 | case LY_STMT_MIN_ELEMENTS: |
| 1532 | LY_CHECK_RET(lysp_stmt_minelements(ctx, child, &llist->min, &llist->flags, &llist->exts)); |
| 1533 | break; |
| 1534 | case LY_STMT_MUST: |
| 1535 | LY_CHECK_RET(lysp_stmt_restrs(ctx, child, &llist->musts)); |
| 1536 | break; |
| 1537 | case LY_STMT_ORDERED_BY: |
| 1538 | LY_CHECK_RET(lysp_stmt_orderedby(ctx, child, &llist->flags, &llist->exts)); |
| 1539 | break; |
| 1540 | case LY_STMT_REFERENCE: |
| 1541 | LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &llist->ref, Y_STR_ARG, &llist->exts)); |
| 1542 | break; |
| 1543 | case LY_STMT_STATUS: |
| 1544 | LY_CHECK_RET(lysp_stmt_status(ctx, child, &llist->flags, &llist->exts)); |
| 1545 | break; |
| 1546 | case LY_STMT_TYPE: |
| 1547 | LY_CHECK_RET(lysp_stmt_type(ctx, child, &llist->type)); |
| 1548 | break; |
| 1549 | case LY_STMT_UNITS: |
| 1550 | LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &llist->units, Y_STR_ARG, &llist->exts)); |
| 1551 | break; |
| 1552 | case LY_STMT_WHEN: |
| 1553 | LY_CHECK_RET(lysp_stmt_when(ctx, child, &llist->when)); |
| 1554 | break; |
| 1555 | case LY_STMT_EXTENSION_INSTANCE: |
Radek Krejci | 39b7fc2 | 2021-02-26 23:29:18 +0100 | [diff] [blame] | 1556 | 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] | 1557 | break; |
| 1558 | default: |
| 1559 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(child->kw), "llist"); |
| 1560 | return LY_EVALID; |
| 1561 | } |
| 1562 | } |
| 1563 | |
| 1564 | /* mandatory substatements */ |
| 1565 | if (!llist->type.name) { |
| 1566 | LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "leaf-list"); |
| 1567 | return LY_EVALID; |
| 1568 | } |
| 1569 | |
| 1570 | return LY_SUCCESS; |
| 1571 | } |
| 1572 | |
| 1573 | /** |
| 1574 | * @brief Parse the refine statement. |
| 1575 | * |
| 1576 | * @param[in] ctx parser context. |
| 1577 | * @param[in] stmt Source statement data from the parsed extension instance. |
| 1578 | * @param[in,out] refines Refines to add to. |
| 1579 | * |
| 1580 | * @return LY_ERR values. |
| 1581 | */ |
| 1582 | static LY_ERR |
| 1583 | lysp_stmt_refine(struct lys_parser_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_refine **refines) |
| 1584 | { |
| 1585 | struct lysp_refine *rf; |
| 1586 | |
| 1587 | LY_CHECK_RET(lysp_stmt_validate_value(ctx, Y_STR_ARG, stmt->arg)); |
| 1588 | |
| 1589 | LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *refines, rf, LY_EMEM); |
| 1590 | |
| 1591 | LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), stmt->arg, 0, &rf->nodeid)); |
| 1592 | |
| 1593 | for (const struct lysp_stmt *child = stmt->child; child; child = child->next) { |
| 1594 | switch (child->kw) { |
| 1595 | case LY_STMT_CONFIG: |
| 1596 | LY_CHECK_RET(lysp_stmt_config(ctx, child, &rf->flags, &rf->exts)); |
| 1597 | break; |
| 1598 | case LY_STMT_DEFAULT: |
| 1599 | LY_CHECK_RET(lysp_stmt_qnames(ctx, child, &rf->dflts, Y_STR_ARG, &rf->exts)); |
| 1600 | break; |
| 1601 | case LY_STMT_DESCRIPTION: |
| 1602 | LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &rf->dsc, Y_STR_ARG, &rf->exts)); |
| 1603 | break; |
| 1604 | case LY_STMT_IF_FEATURE: |
| 1605 | PARSER_CHECK_STMTVER2_RET(ctx, "if-feature", "refine"); |
| 1606 | LY_CHECK_RET(lysp_stmt_qnames(ctx, child, &rf->iffeatures, Y_STR_ARG, &rf->exts)); |
| 1607 | break; |
| 1608 | case LY_STMT_MAX_ELEMENTS: |
| 1609 | LY_CHECK_RET(lysp_stmt_maxelements(ctx, child, &rf->max, &rf->flags, &rf->exts)); |
| 1610 | break; |
| 1611 | case LY_STMT_MIN_ELEMENTS: |
| 1612 | LY_CHECK_RET(lysp_stmt_minelements(ctx, child, &rf->min, &rf->flags, &rf->exts)); |
| 1613 | break; |
| 1614 | case LY_STMT_MUST: |
| 1615 | LY_CHECK_RET(lysp_stmt_restrs(ctx, child, &rf->musts)); |
| 1616 | break; |
| 1617 | case LY_STMT_MANDATORY: |
| 1618 | LY_CHECK_RET(lysp_stmt_mandatory(ctx, child, &rf->flags, &rf->exts)); |
| 1619 | break; |
| 1620 | case LY_STMT_REFERENCE: |
| 1621 | LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &rf->ref, Y_STR_ARG, &rf->exts)); |
| 1622 | break; |
| 1623 | case LY_STMT_PRESENCE: |
| 1624 | LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &rf->presence, Y_STR_ARG, &rf->exts)); |
| 1625 | break; |
| 1626 | case LY_STMT_EXTENSION_INSTANCE: |
Radek Krejci | 39b7fc2 | 2021-02-26 23:29:18 +0100 | [diff] [blame] | 1627 | 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] | 1628 | break; |
| 1629 | default: |
| 1630 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(child->kw), "refine"); |
| 1631 | return LY_EVALID; |
| 1632 | } |
| 1633 | } |
| 1634 | |
| 1635 | return LY_SUCCESS; |
| 1636 | } |
| 1637 | |
| 1638 | /** |
| 1639 | * @brief Parse the typedef statement. |
| 1640 | * |
| 1641 | * @param[in] ctx parser context. |
| 1642 | * @param[in] stmt Source statement data from the parsed extension instance. |
| 1643 | * @param[in] parent Parent node to connect to (not into). |
| 1644 | * @param[in,out] typedefs Typedefs to add to. |
| 1645 | * |
| 1646 | * @return LY_ERR values. |
| 1647 | */ |
| 1648 | static LY_ERR |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 1649 | lysp_stmt_typedef(struct lys_parser_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_node *parent, |
| 1650 | struct lysp_tpdf **typedefs) |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 1651 | { |
| 1652 | struct lysp_tpdf *tpdf; |
| 1653 | |
| 1654 | LY_CHECK_RET(lysp_stmt_validate_value(ctx, Y_IDENTIF_ARG, stmt->arg)); |
| 1655 | |
| 1656 | LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *typedefs, tpdf, LY_EMEM); |
| 1657 | |
| 1658 | LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), stmt->arg, 0, &tpdf->name)); |
| 1659 | |
| 1660 | /* parse substatements */ |
| 1661 | for (const struct lysp_stmt *child = stmt->child; child; child = child->next) { |
| 1662 | switch (child->kw) { |
| 1663 | case LY_STMT_DEFAULT: |
| 1664 | 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] | 1665 | tpdf->dflt.mod = PARSER_CUR_PMOD(ctx); |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 1666 | break; |
| 1667 | case LY_STMT_DESCRIPTION: |
| 1668 | LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &tpdf->dsc, Y_STR_ARG, &tpdf->exts)); |
| 1669 | break; |
| 1670 | case LY_STMT_REFERENCE: |
| 1671 | LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &tpdf->ref, Y_STR_ARG, &tpdf->exts)); |
| 1672 | break; |
| 1673 | case LY_STMT_STATUS: |
| 1674 | LY_CHECK_RET(lysp_stmt_status(ctx, child, &tpdf->flags, &tpdf->exts)); |
| 1675 | break; |
| 1676 | case LY_STMT_TYPE: |
| 1677 | LY_CHECK_RET(lysp_stmt_type(ctx, child, &tpdf->type)); |
| 1678 | break; |
| 1679 | case LY_STMT_UNITS: |
| 1680 | LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &tpdf->units, Y_STR_ARG, &tpdf->exts)); |
| 1681 | break; |
| 1682 | case LY_STMT_EXTENSION_INSTANCE: |
Radek Krejci | 39b7fc2 | 2021-02-26 23:29:18 +0100 | [diff] [blame] | 1683 | 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] | 1684 | break; |
| 1685 | default: |
| 1686 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(child->kw), "typedef"); |
| 1687 | return LY_EVALID; |
| 1688 | } |
| 1689 | } |
| 1690 | |
| 1691 | /* mandatory substatements */ |
| 1692 | if (!tpdf->type.name) { |
| 1693 | LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "typedef"); |
| 1694 | return LY_EVALID; |
| 1695 | } |
| 1696 | |
| 1697 | /* store data for collision check */ |
| 1698 | if (parent && !(parent->nodetype & (LYS_GROUPING | LYS_RPC | LYS_ACTION | LYS_INPUT | LYS_OUTPUT | LYS_NOTIF))) { |
| 1699 | LY_CHECK_RET(ly_set_add(&ctx->tpdfs_nodes, parent, 0, NULL)); |
| 1700 | } |
| 1701 | |
| 1702 | return LY_SUCCESS; |
| 1703 | } |
| 1704 | |
| 1705 | /** |
| 1706 | * @brief Parse the input or output statement. |
| 1707 | * |
| 1708 | * @param[in] ctx parser context. |
| 1709 | * @param[in] stmt Source statement data from the parsed extension instance. |
| 1710 | * @param[in] parent Parent node to connect to (not into). |
| 1711 | * @param[in,out] inout_p Input/output pointer to write to. |
| 1712 | * |
| 1713 | * @return LY_ERR values. |
| 1714 | */ |
| 1715 | static LY_ERR |
| 1716 | lysp_stmt_inout(struct lys_parser_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_node *parent, |
| 1717 | struct lysp_node_action_inout *inout_p) |
| 1718 | { |
| 1719 | if (inout_p->nodetype) { |
| 1720 | LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(stmt->kw)); |
| 1721 | return LY_EVALID; |
| 1722 | } |
| 1723 | |
| 1724 | /* initiate structure */ |
| 1725 | LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), stmt->kw == LY_STMT_INPUT ? "input" : "output", 0, &inout_p->name)); |
| 1726 | inout_p->nodetype = stmt->kw == LY_STMT_INPUT ? LYS_INPUT : LYS_OUTPUT; |
| 1727 | inout_p->parent = parent; |
| 1728 | |
| 1729 | /* parse substatements */ |
| 1730 | for (const struct lysp_stmt *child = stmt->child; child; child = child->next) { |
| 1731 | switch (child->kw) { |
| 1732 | case LY_STMT_ANYDATA: |
| 1733 | PARSER_CHECK_STMTVER2_RET(ctx, "anydata", ly_stmt2str(stmt->kw)); |
| 1734 | /* fall through */ |
| 1735 | case LY_STMT_ANYXML: |
| 1736 | LY_CHECK_RET(lysp_stmt_any(ctx, child, &inout_p->node, &inout_p->child)); |
| 1737 | break; |
| 1738 | case LY_STMT_CHOICE: |
| 1739 | LY_CHECK_RET(lysp_stmt_choice(ctx, child, &inout_p->node, &inout_p->child)); |
| 1740 | break; |
| 1741 | case LY_STMT_CONTAINER: |
| 1742 | LY_CHECK_RET(lysp_stmt_container(ctx, child, &inout_p->node, &inout_p->child)); |
| 1743 | break; |
| 1744 | case LY_STMT_LEAF: |
| 1745 | LY_CHECK_RET(lysp_stmt_leaf(ctx, child, &inout_p->node, &inout_p->child)); |
| 1746 | break; |
| 1747 | case LY_STMT_LEAF_LIST: |
| 1748 | LY_CHECK_RET(lysp_stmt_leaflist(ctx, child, &inout_p->node, &inout_p->child)); |
| 1749 | break; |
| 1750 | case LY_STMT_LIST: |
| 1751 | LY_CHECK_RET(lysp_stmt_list(ctx, child, &inout_p->node, &inout_p->child)); |
| 1752 | break; |
| 1753 | case LY_STMT_USES: |
| 1754 | LY_CHECK_RET(lysp_stmt_uses(ctx, child, &inout_p->node, &inout_p->child)); |
| 1755 | break; |
| 1756 | case LY_STMT_TYPEDEF: |
| 1757 | LY_CHECK_RET(lysp_stmt_typedef(ctx, child, &inout_p->node, &inout_p->typedefs)); |
| 1758 | break; |
| 1759 | case LY_STMT_MUST: |
| 1760 | PARSER_CHECK_STMTVER2_RET(ctx, "must", ly_stmt2str(stmt->kw)); |
| 1761 | LY_CHECK_RET(lysp_stmt_restrs(ctx, child, &inout_p->musts)); |
| 1762 | break; |
| 1763 | case LY_STMT_GROUPING: |
| 1764 | LY_CHECK_RET(lysp_stmt_grouping(ctx, child, &inout_p->node, &inout_p->groupings)); |
| 1765 | break; |
| 1766 | case LY_STMT_EXTENSION_INSTANCE: |
Radek Krejci | 39b7fc2 | 2021-02-26 23:29:18 +0100 | [diff] [blame] | 1767 | 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] | 1768 | break; |
| 1769 | default: |
| 1770 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(child->kw), ly_stmt2str(stmt->kw)); |
| 1771 | return LY_EVALID; |
| 1772 | } |
| 1773 | } |
| 1774 | |
| 1775 | if (!inout_p->child) { |
| 1776 | LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "data-def-stmt", ly_stmt2str(stmt->kw)); |
| 1777 | return LY_EVALID; |
| 1778 | } |
| 1779 | |
| 1780 | return LY_SUCCESS; |
| 1781 | } |
| 1782 | |
| 1783 | /** |
| 1784 | * @brief Parse the action statement. |
| 1785 | * |
| 1786 | * @param[in] ctx parser context. |
| 1787 | * @param[in] stmt Source statement data from the parsed extension instance. |
| 1788 | * @param[in] parent Parent node to connect to (not into). |
| 1789 | * @param[in,out] actions Actions to add to. |
| 1790 | * |
| 1791 | * @return LY_ERR values. |
| 1792 | */ |
| 1793 | static LY_ERR |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 1794 | lysp_stmt_action(struct lys_parser_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_node *parent, |
| 1795 | struct lysp_node_action **actions) |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 1796 | { |
| 1797 | struct lysp_node_action *act; |
| 1798 | |
| 1799 | LY_CHECK_RET(lysp_stmt_validate_value(ctx, Y_IDENTIF_ARG, stmt->arg)); |
| 1800 | |
| 1801 | LY_LIST_NEW_RET(PARSER_CTX(ctx), actions, act, next, LY_EMEM); |
| 1802 | |
| 1803 | LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), stmt->arg, 0, &act->name)); |
| 1804 | act->nodetype = parent ? LYS_ACTION : LYS_RPC; |
| 1805 | act->parent = parent; |
| 1806 | |
| 1807 | for (const struct lysp_stmt *child = stmt->child; child; child = child->next) { |
| 1808 | switch (child->kw) { |
| 1809 | case LY_STMT_DESCRIPTION: |
| 1810 | LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &act->dsc, Y_STR_ARG, &act->exts)); |
| 1811 | break; |
| 1812 | case LY_STMT_IF_FEATURE: |
| 1813 | LY_CHECK_RET(lysp_stmt_qnames(ctx, child, &act->iffeatures, Y_STR_ARG, &act->exts)); |
| 1814 | break; |
| 1815 | case LY_STMT_REFERENCE: |
| 1816 | LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &act->ref, Y_STR_ARG, &act->exts)); |
| 1817 | break; |
| 1818 | case LY_STMT_STATUS: |
| 1819 | LY_CHECK_RET(lysp_stmt_status(ctx, child, &act->flags, &act->exts)); |
| 1820 | break; |
| 1821 | |
| 1822 | case LY_STMT_INPUT: |
| 1823 | LY_CHECK_RET(lysp_stmt_inout(ctx, child, &act->node, &act->input)); |
| 1824 | break; |
| 1825 | case LY_STMT_OUTPUT: |
| 1826 | LY_CHECK_RET(lysp_stmt_inout(ctx, child, &act->node, &act->output)); |
| 1827 | break; |
| 1828 | |
| 1829 | case LY_STMT_TYPEDEF: |
| 1830 | LY_CHECK_RET(lysp_stmt_typedef(ctx, child, &act->node, &act->typedefs)); |
| 1831 | break; |
| 1832 | case LY_STMT_GROUPING: |
| 1833 | LY_CHECK_RET(lysp_stmt_grouping(ctx, child, &act->node, &act->groupings)); |
| 1834 | break; |
| 1835 | case LY_STMT_EXTENSION_INSTANCE: |
Radek Krejci | 39b7fc2 | 2021-02-26 23:29:18 +0100 | [diff] [blame] | 1836 | 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] | 1837 | break; |
| 1838 | default: |
| 1839 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(child->kw), parent ? "action" : "rpc"); |
| 1840 | return LY_EVALID; |
| 1841 | } |
| 1842 | } |
| 1843 | |
| 1844 | /* always initialize inout, they are technically present (needed for later deviations/refines) */ |
| 1845 | if (!act->input.nodetype) { |
| 1846 | act->input.nodetype = LYS_INPUT; |
| 1847 | act->input.parent = &act->node; |
| 1848 | LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), "input", 0, &act->input.name)); |
| 1849 | } |
| 1850 | if (!act->output.nodetype) { |
| 1851 | act->output.nodetype = LYS_OUTPUT; |
| 1852 | act->output.parent = &act->node; |
| 1853 | LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), "output", 0, &act->output.name)); |
| 1854 | } |
| 1855 | |
| 1856 | return LY_SUCCESS; |
| 1857 | } |
| 1858 | |
| 1859 | /** |
| 1860 | * @brief Parse the notification statement. |
| 1861 | * |
| 1862 | * @param[in] ctx parser context. |
| 1863 | * @param[in] stmt Source statement data from the parsed extension instance. |
| 1864 | * @param[in] parent Parent node to connect to (not into). |
| 1865 | * @param[in,out] notifs Notifications to add to. |
| 1866 | * |
| 1867 | * @return LY_ERR values. |
| 1868 | */ |
| 1869 | static LY_ERR |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 1870 | lysp_stmt_notif(struct lys_parser_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_node *parent, |
| 1871 | struct lysp_node_notif **notifs) |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 1872 | { |
| 1873 | struct lysp_node_notif *notif; |
| 1874 | |
| 1875 | LY_CHECK_RET(lysp_stmt_validate_value(ctx, Y_IDENTIF_ARG, stmt->arg)); |
| 1876 | |
| 1877 | LY_LIST_NEW_RET(PARSER_CTX(ctx), notifs, notif, next, LY_EMEM); |
| 1878 | |
| 1879 | LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), stmt->arg, 0, ¬if->name)); |
| 1880 | notif->nodetype = LYS_NOTIF; |
| 1881 | notif->parent = parent; |
| 1882 | |
| 1883 | for (const struct lysp_stmt *child = stmt->child; child; child = child->next) { |
| 1884 | switch (child->kw) { |
| 1885 | case LY_STMT_DESCRIPTION: |
| 1886 | LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, ¬if->dsc, Y_STR_ARG, ¬if->exts)); |
| 1887 | break; |
| 1888 | case LY_STMT_IF_FEATURE: |
| 1889 | LY_CHECK_RET(lysp_stmt_qnames(ctx, child, ¬if->iffeatures, Y_STR_ARG, ¬if->exts)); |
| 1890 | break; |
| 1891 | case LY_STMT_REFERENCE: |
| 1892 | LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, ¬if->ref, Y_STR_ARG, ¬if->exts)); |
| 1893 | break; |
| 1894 | case LY_STMT_STATUS: |
| 1895 | LY_CHECK_RET(lysp_stmt_status(ctx, child, ¬if->flags, ¬if->exts)); |
| 1896 | break; |
| 1897 | |
| 1898 | case LY_STMT_ANYDATA: |
| 1899 | PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "notification"); |
| 1900 | /* fall through */ |
| 1901 | case LY_STMT_ANYXML: |
| 1902 | LY_CHECK_RET(lysp_stmt_any(ctx, child, ¬if->node, ¬if->child)); |
| 1903 | break; |
| 1904 | case LY_STMT_CHOICE: |
| 1905 | LY_CHECK_RET(lysp_stmt_case(ctx, child, ¬if->node, ¬if->child)); |
| 1906 | break; |
| 1907 | case LY_STMT_CONTAINER: |
| 1908 | LY_CHECK_RET(lysp_stmt_container(ctx, child, ¬if->node, ¬if->child)); |
| 1909 | break; |
| 1910 | case LY_STMT_LEAF: |
| 1911 | LY_CHECK_RET(lysp_stmt_leaf(ctx, child, ¬if->node, ¬if->child)); |
| 1912 | break; |
| 1913 | case LY_STMT_LEAF_LIST: |
| 1914 | LY_CHECK_RET(lysp_stmt_leaflist(ctx, child, ¬if->node, ¬if->child)); |
| 1915 | break; |
| 1916 | case LY_STMT_LIST: |
| 1917 | LY_CHECK_RET(lysp_stmt_list(ctx, child, ¬if->node, ¬if->child)); |
| 1918 | break; |
| 1919 | case LY_STMT_USES: |
| 1920 | LY_CHECK_RET(lysp_stmt_uses(ctx, child, ¬if->node, ¬if->child)); |
| 1921 | break; |
| 1922 | |
| 1923 | case LY_STMT_MUST: |
| 1924 | PARSER_CHECK_STMTVER2_RET(ctx, "must", "notification"); |
| 1925 | LY_CHECK_RET(lysp_stmt_restrs(ctx, child, ¬if->musts)); |
| 1926 | break; |
| 1927 | case LY_STMT_TYPEDEF: |
| 1928 | LY_CHECK_RET(lysp_stmt_typedef(ctx, child, ¬if->node, ¬if->typedefs)); |
| 1929 | break; |
| 1930 | case LY_STMT_GROUPING: |
| 1931 | LY_CHECK_RET(lysp_stmt_grouping(ctx, child, ¬if->node, ¬if->groupings)); |
| 1932 | break; |
| 1933 | case LY_STMT_EXTENSION_INSTANCE: |
Radek Krejci | 39b7fc2 | 2021-02-26 23:29:18 +0100 | [diff] [blame] | 1934 | 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] | 1935 | break; |
| 1936 | default: |
| 1937 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(child->kw), "notification"); |
| 1938 | return LY_EVALID; |
| 1939 | } |
| 1940 | } |
| 1941 | |
| 1942 | return LY_SUCCESS; |
| 1943 | } |
| 1944 | |
| 1945 | /** |
| 1946 | * @brief Parse the grouping statement. |
| 1947 | * |
| 1948 | * @param[in] ctx parser context. |
| 1949 | * @param[in] stmt Source statement data from the parsed extension instance. |
| 1950 | * @param[in] parent Parent node to connect to (not into). |
| 1951 | * @param[in,out] groupings Groupings to add to. |
| 1952 | * |
| 1953 | * @return LY_ERR values. |
| 1954 | */ |
| 1955 | static LY_ERR |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 1956 | lysp_stmt_grouping(struct lys_parser_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_node *parent, |
| 1957 | struct lysp_node_grp **groupings) |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 1958 | { |
| 1959 | struct lysp_node_grp *grp; |
| 1960 | |
| 1961 | LY_CHECK_RET(lysp_stmt_validate_value(ctx, Y_IDENTIF_ARG, stmt->arg)); |
| 1962 | |
| 1963 | LY_LIST_NEW_RET(PARSER_CTX(ctx), groupings, grp, next, LY_EMEM); |
| 1964 | |
| 1965 | LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), stmt->arg, 0, &grp->name)); |
| 1966 | grp->nodetype = LYS_GROUPING; |
| 1967 | grp->parent = parent; |
| 1968 | |
| 1969 | for (const struct lysp_stmt *child = stmt->child; child; child = child->next) { |
| 1970 | switch (child->kw) { |
| 1971 | case LY_STMT_DESCRIPTION: |
| 1972 | LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &grp->dsc, Y_STR_ARG, &grp->exts)); |
| 1973 | break; |
| 1974 | case LY_STMT_REFERENCE: |
| 1975 | LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &grp->ref, Y_STR_ARG, &grp->exts)); |
| 1976 | break; |
| 1977 | case LY_STMT_STATUS: |
| 1978 | LY_CHECK_RET(lysp_stmt_status(ctx, child, &grp->flags, &grp->exts)); |
| 1979 | break; |
| 1980 | |
| 1981 | case LY_STMT_ANYDATA: |
| 1982 | PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "grouping"); |
| 1983 | /* fall through */ |
| 1984 | case LY_STMT_ANYXML: |
| 1985 | LY_CHECK_RET(lysp_stmt_any(ctx, child, &grp->node, &grp->child)); |
| 1986 | break; |
| 1987 | case LY_STMT_CHOICE: |
| 1988 | LY_CHECK_RET(lysp_stmt_choice(ctx, child, &grp->node, &grp->child)); |
| 1989 | break; |
| 1990 | case LY_STMT_CONTAINER: |
| 1991 | LY_CHECK_RET(lysp_stmt_container(ctx, child, &grp->node, &grp->child)); |
| 1992 | break; |
| 1993 | case LY_STMT_LEAF: |
| 1994 | LY_CHECK_RET(lysp_stmt_leaf(ctx, child, &grp->node, &grp->child)); |
| 1995 | break; |
| 1996 | case LY_STMT_LEAF_LIST: |
| 1997 | LY_CHECK_RET(lysp_stmt_leaflist(ctx, child, &grp->node, &grp->child)); |
| 1998 | break; |
| 1999 | case LY_STMT_LIST: |
| 2000 | LY_CHECK_RET(lysp_stmt_list(ctx, child, &grp->node, &grp->child)); |
| 2001 | break; |
| 2002 | case LY_STMT_USES: |
| 2003 | LY_CHECK_RET(lysp_stmt_uses(ctx, child, &grp->node, &grp->child)); |
| 2004 | break; |
| 2005 | |
| 2006 | case LY_STMT_TYPEDEF: |
| 2007 | LY_CHECK_RET(lysp_stmt_typedef(ctx, child, &grp->node, &grp->typedefs)); |
| 2008 | break; |
| 2009 | case LY_STMT_ACTION: |
| 2010 | PARSER_CHECK_STMTVER2_RET(ctx, "action", "grouping"); |
| 2011 | LY_CHECK_RET(lysp_stmt_action(ctx, child, &grp->node, &grp->actions)); |
| 2012 | break; |
| 2013 | case LY_STMT_GROUPING: |
| 2014 | LY_CHECK_RET(lysp_stmt_grouping(ctx, child, &grp->node, &grp->groupings)); |
| 2015 | break; |
| 2016 | case LY_STMT_NOTIFICATION: |
| 2017 | PARSER_CHECK_STMTVER2_RET(ctx, "notification", "grouping"); |
| 2018 | LY_CHECK_RET(lysp_stmt_notif(ctx, child, &grp->node, &grp->notifs)); |
| 2019 | break; |
| 2020 | case LY_STMT_EXTENSION_INSTANCE: |
Radek Krejci | 39b7fc2 | 2021-02-26 23:29:18 +0100 | [diff] [blame] | 2021 | 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] | 2022 | break; |
| 2023 | default: |
| 2024 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(child->kw), "grouping"); |
| 2025 | return LY_EVALID; |
| 2026 | } |
| 2027 | } |
| 2028 | |
| 2029 | return LY_SUCCESS; |
| 2030 | } |
| 2031 | |
| 2032 | /** |
| 2033 | * @brief Parse the augment statement. |
| 2034 | * |
| 2035 | * @param[in] ctx parser context. |
| 2036 | * @param[in] stmt Source statement data from the parsed extension instance. |
| 2037 | * @param[in] parent Parent node to connect to (not into). |
| 2038 | * @param[in,out] augments Augments to add to. |
| 2039 | * |
| 2040 | * @return LY_ERR values. |
| 2041 | */ |
| 2042 | static LY_ERR |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 2043 | lysp_stmt_augment(struct lys_parser_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_node *parent, |
| 2044 | struct lysp_node_augment **augments) |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 2045 | { |
| 2046 | struct lysp_node_augment *aug; |
| 2047 | |
| 2048 | LY_CHECK_RET(lysp_stmt_validate_value(ctx, Y_STR_ARG, stmt->arg)); |
| 2049 | |
| 2050 | LY_LIST_NEW_RET(PARSER_CTX(ctx), augments, aug, next, LY_EMEM); |
| 2051 | |
| 2052 | LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), stmt->arg, 0, &aug->nodeid)); |
| 2053 | aug->nodetype = LYS_AUGMENT; |
| 2054 | aug->parent = parent; |
| 2055 | |
| 2056 | for (const struct lysp_stmt *child = stmt->child; child; child = child->next) { |
| 2057 | switch (child->kw) { |
| 2058 | case LY_STMT_DESCRIPTION: |
| 2059 | LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &aug->dsc, Y_STR_ARG, &aug->exts)); |
| 2060 | break; |
| 2061 | case LY_STMT_IF_FEATURE: |
| 2062 | LY_CHECK_RET(lysp_stmt_qnames(ctx, child, &aug->iffeatures, Y_STR_ARG, &aug->exts)); |
| 2063 | break; |
| 2064 | case LY_STMT_REFERENCE: |
| 2065 | LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &aug->ref, Y_STR_ARG, &aug->exts)); |
| 2066 | break; |
| 2067 | case LY_STMT_STATUS: |
| 2068 | LY_CHECK_RET(lysp_stmt_status(ctx, child, &aug->flags, &aug->exts)); |
| 2069 | break; |
| 2070 | case LY_STMT_WHEN: |
| 2071 | LY_CHECK_RET(lysp_stmt_when(ctx, child, &aug->when)); |
| 2072 | break; |
| 2073 | |
| 2074 | case LY_STMT_ANYDATA: |
| 2075 | PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "augment"); |
| 2076 | /* fall through */ |
| 2077 | case LY_STMT_ANYXML: |
| 2078 | LY_CHECK_RET(lysp_stmt_any(ctx, child, &aug->node, &aug->child)); |
| 2079 | break; |
| 2080 | case LY_STMT_CASE: |
| 2081 | LY_CHECK_RET(lysp_stmt_case(ctx, child, &aug->node, &aug->child)); |
| 2082 | break; |
| 2083 | case LY_STMT_CHOICE: |
| 2084 | LY_CHECK_RET(lysp_stmt_choice(ctx, child, &aug->node, &aug->child)); |
| 2085 | break; |
| 2086 | case LY_STMT_CONTAINER: |
| 2087 | LY_CHECK_RET(lysp_stmt_container(ctx, child, &aug->node, &aug->child)); |
| 2088 | break; |
| 2089 | case LY_STMT_LEAF: |
| 2090 | LY_CHECK_RET(lysp_stmt_leaf(ctx, child, &aug->node, &aug->child)); |
| 2091 | break; |
| 2092 | case LY_STMT_LEAF_LIST: |
| 2093 | LY_CHECK_RET(lysp_stmt_leaflist(ctx, child, &aug->node, &aug->child)); |
| 2094 | break; |
| 2095 | case LY_STMT_LIST: |
| 2096 | LY_CHECK_RET(lysp_stmt_list(ctx, child, &aug->node, &aug->child)); |
| 2097 | break; |
| 2098 | case LY_STMT_USES: |
| 2099 | LY_CHECK_RET(lysp_stmt_uses(ctx, child, &aug->node, &aug->child)); |
| 2100 | break; |
| 2101 | |
| 2102 | case LY_STMT_ACTION: |
| 2103 | PARSER_CHECK_STMTVER2_RET(ctx, "action", "augment"); |
| 2104 | LY_CHECK_RET(lysp_stmt_action(ctx, child, &aug->node, &aug->actions)); |
| 2105 | break; |
| 2106 | case LY_STMT_NOTIFICATION: |
| 2107 | PARSER_CHECK_STMTVER2_RET(ctx, "notification", "augment"); |
| 2108 | LY_CHECK_RET(lysp_stmt_notif(ctx, child, &aug->node, &aug->notifs)); |
| 2109 | break; |
| 2110 | case LY_STMT_EXTENSION_INSTANCE: |
Radek Krejci | 39b7fc2 | 2021-02-26 23:29:18 +0100 | [diff] [blame] | 2111 | 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] | 2112 | break; |
| 2113 | default: |
| 2114 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(child->kw), "augment"); |
| 2115 | return LY_EVALID; |
| 2116 | } |
| 2117 | } |
| 2118 | |
| 2119 | return LY_SUCCESS; |
| 2120 | } |
| 2121 | |
| 2122 | /** |
| 2123 | * @brief Parse the uses statement. |
| 2124 | * |
| 2125 | * @param[in] ctx parser context. |
| 2126 | * @param[in] stmt Source statement data from the parsed extension instance. |
| 2127 | * @param[in] parent Parent node to connect to (not into). |
| 2128 | * @param[in,out] siblings Siblings to add to. |
| 2129 | * |
| 2130 | * @return LY_ERR values. |
| 2131 | */ |
| 2132 | static LY_ERR |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 2133 | lysp_stmt_uses(struct lys_parser_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_node *parent, |
| 2134 | struct lysp_node **siblings) |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 2135 | { |
| 2136 | struct lysp_node_uses *uses; |
| 2137 | |
| 2138 | LY_CHECK_RET(lysp_stmt_validate_value(ctx, Y_PREF_IDENTIF_ARG, stmt->arg)); |
| 2139 | |
| 2140 | /* create uses structure */ |
| 2141 | LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, uses, next, LY_EMEM); |
| 2142 | |
| 2143 | LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), stmt->arg, 0, &uses->name)); |
| 2144 | uses->nodetype = LYS_USES; |
| 2145 | uses->parent = parent; |
| 2146 | |
| 2147 | /* parse substatements */ |
| 2148 | for (const struct lysp_stmt *child = stmt->child; child; child = child->next) { |
| 2149 | switch (child->kw) { |
| 2150 | case LY_STMT_DESCRIPTION: |
| 2151 | LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &uses->dsc, Y_STR_ARG, &uses->exts)); |
| 2152 | break; |
| 2153 | case LY_STMT_IF_FEATURE: |
| 2154 | LY_CHECK_RET(lysp_stmt_qnames(ctx, child, &uses->iffeatures, Y_STR_ARG, &uses->exts)); |
| 2155 | break; |
| 2156 | case LY_STMT_REFERENCE: |
| 2157 | LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &uses->ref, Y_STR_ARG, &uses->exts)); |
| 2158 | break; |
| 2159 | case LY_STMT_STATUS: |
| 2160 | LY_CHECK_RET(lysp_stmt_status(ctx, child, &uses->flags, &uses->exts)); |
| 2161 | break; |
| 2162 | case LY_STMT_WHEN: |
| 2163 | LY_CHECK_RET(lysp_stmt_when(ctx, child, &uses->when)); |
| 2164 | break; |
| 2165 | |
| 2166 | case LY_STMT_REFINE: |
| 2167 | LY_CHECK_RET(lysp_stmt_refine(ctx, child, &uses->refines)); |
| 2168 | break; |
| 2169 | case LY_STMT_AUGMENT: |
| 2170 | LY_CHECK_RET(lysp_stmt_augment(ctx, child, &uses->node, &uses->augments)); |
| 2171 | break; |
| 2172 | case LY_STMT_EXTENSION_INSTANCE: |
Radek Krejci | 39b7fc2 | 2021-02-26 23:29:18 +0100 | [diff] [blame] | 2173 | 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] | 2174 | break; |
| 2175 | default: |
| 2176 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(child->kw), "uses"); |
| 2177 | return LY_EVALID; |
| 2178 | } |
| 2179 | } |
| 2180 | |
| 2181 | return LY_SUCCESS; |
| 2182 | } |
| 2183 | |
| 2184 | /** |
| 2185 | * @brief Parse the case statement. |
| 2186 | * |
| 2187 | * @param[in] ctx parser context. |
| 2188 | * @param[in] stmt Source statement data from the parsed extension instance. |
| 2189 | * @param[in] parent Parent node to connect to (not into). |
| 2190 | * @param[in,out] siblings Siblings to add to. |
| 2191 | * |
| 2192 | * @return LY_ERR values. |
| 2193 | */ |
| 2194 | static LY_ERR |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 2195 | lysp_stmt_case(struct lys_parser_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_node *parent, |
| 2196 | struct lysp_node **siblings) |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 2197 | { |
| 2198 | struct lysp_node_case *cas; |
| 2199 | |
| 2200 | LY_CHECK_RET(lysp_stmt_validate_value(ctx, Y_IDENTIF_ARG, stmt->arg)); |
| 2201 | |
| 2202 | /* create new case structure */ |
| 2203 | LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, cas, next, LY_EMEM); |
| 2204 | |
| 2205 | LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), stmt->arg, 0, &cas->name)); |
| 2206 | cas->nodetype = LYS_CASE; |
| 2207 | cas->parent = parent; |
| 2208 | |
| 2209 | /* parse substatements */ |
| 2210 | for (const struct lysp_stmt *child = stmt->child; child; child = child->next) { |
| 2211 | switch (child->kw) { |
| 2212 | case LY_STMT_DESCRIPTION: |
| 2213 | LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &cas->dsc, Y_STR_ARG, &cas->exts)); |
| 2214 | break; |
| 2215 | case LY_STMT_IF_FEATURE: |
| 2216 | LY_CHECK_RET(lysp_stmt_qnames(ctx, child, &cas->iffeatures, Y_STR_ARG, &cas->exts)); |
| 2217 | break; |
| 2218 | case LY_STMT_REFERENCE: |
| 2219 | LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &cas->ref, Y_STR_ARG, &cas->exts)); |
| 2220 | break; |
| 2221 | case LY_STMT_STATUS: |
| 2222 | LY_CHECK_RET(lysp_stmt_status(ctx, child, &cas->flags, &cas->exts)); |
| 2223 | break; |
| 2224 | case LY_STMT_WHEN: |
| 2225 | LY_CHECK_RET(lysp_stmt_when(ctx, child, &cas->when)); |
| 2226 | break; |
| 2227 | |
| 2228 | case LY_STMT_ANYDATA: |
| 2229 | PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "case"); |
| 2230 | /* fall through */ |
| 2231 | case LY_STMT_ANYXML: |
| 2232 | LY_CHECK_RET(lysp_stmt_any(ctx, child, &cas->node, &cas->child)); |
| 2233 | break; |
| 2234 | case LY_STMT_CHOICE: |
| 2235 | LY_CHECK_RET(lysp_stmt_choice(ctx, child, &cas->node, &cas->child)); |
| 2236 | break; |
| 2237 | case LY_STMT_CONTAINER: |
| 2238 | LY_CHECK_RET(lysp_stmt_container(ctx, child, &cas->node, &cas->child)); |
| 2239 | break; |
| 2240 | case LY_STMT_LEAF: |
| 2241 | LY_CHECK_RET(lysp_stmt_leaf(ctx, child, &cas->node, &cas->child)); |
| 2242 | break; |
| 2243 | case LY_STMT_LEAF_LIST: |
| 2244 | LY_CHECK_RET(lysp_stmt_leaflist(ctx, child, &cas->node, &cas->child)); |
| 2245 | break; |
| 2246 | case LY_STMT_LIST: |
| 2247 | LY_CHECK_RET(lysp_stmt_list(ctx, child, &cas->node, &cas->child)); |
| 2248 | break; |
| 2249 | case LY_STMT_USES: |
| 2250 | LY_CHECK_RET(lysp_stmt_uses(ctx, child, &cas->node, &cas->child)); |
| 2251 | break; |
| 2252 | case LY_STMT_EXTENSION_INSTANCE: |
Radek Krejci | 39b7fc2 | 2021-02-26 23:29:18 +0100 | [diff] [blame] | 2253 | 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] | 2254 | break; |
| 2255 | default: |
| 2256 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(child->kw), "case"); |
| 2257 | return LY_EVALID; |
| 2258 | } |
| 2259 | } |
| 2260 | return LY_SUCCESS; |
| 2261 | } |
| 2262 | |
| 2263 | /** |
| 2264 | * @brief Parse the choice statement. |
| 2265 | * |
| 2266 | * @param[in] ctx parser context. |
| 2267 | * @param[in] stmt Source statement data from the parsed extension instance. |
| 2268 | * @param[in] parent Parent node to connect to (not into). |
| 2269 | * @param[in,out] siblings Siblings to add to. |
| 2270 | * |
| 2271 | * @return LY_ERR values. |
| 2272 | */ |
| 2273 | static LY_ERR |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 2274 | lysp_stmt_choice(struct lys_parser_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_node *parent, |
| 2275 | struct lysp_node **siblings) |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 2276 | { |
| 2277 | struct lysp_node_choice *choice; |
| 2278 | |
| 2279 | LY_CHECK_RET(lysp_stmt_validate_value(ctx, Y_IDENTIF_ARG, stmt->arg)); |
| 2280 | |
| 2281 | /* create new choice structure */ |
| 2282 | LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, choice, next, LY_EMEM); |
| 2283 | |
| 2284 | LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), stmt->arg, 0, &choice->name)); |
| 2285 | choice->nodetype = LYS_CHOICE; |
| 2286 | choice->parent = parent; |
| 2287 | |
| 2288 | /* parse substatements */ |
| 2289 | for (const struct lysp_stmt *child = stmt->child; child; child = child->next) { |
| 2290 | switch (child->kw) { |
| 2291 | case LY_STMT_CONFIG: |
| 2292 | LY_CHECK_RET(lysp_stmt_config(ctx, child, &choice->flags, &choice->exts)); |
| 2293 | break; |
| 2294 | case LY_STMT_DESCRIPTION: |
| 2295 | LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &choice->dsc, Y_STR_ARG, &choice->exts)); |
| 2296 | break; |
| 2297 | case LY_STMT_IF_FEATURE: |
| 2298 | LY_CHECK_RET(lysp_stmt_qnames(ctx, child, &choice->iffeatures, Y_STR_ARG, &choice->exts)); |
| 2299 | break; |
| 2300 | case LY_STMT_MANDATORY: |
| 2301 | LY_CHECK_RET(lysp_stmt_mandatory(ctx, child, &choice->flags, &choice->exts)); |
| 2302 | break; |
| 2303 | case LY_STMT_REFERENCE: |
| 2304 | LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &choice->ref, Y_STR_ARG, &choice->exts)); |
| 2305 | break; |
| 2306 | case LY_STMT_STATUS: |
| 2307 | LY_CHECK_RET(lysp_stmt_status(ctx, child, &choice->flags, &choice->exts)); |
| 2308 | break; |
| 2309 | case LY_STMT_WHEN: |
| 2310 | LY_CHECK_RET(lysp_stmt_when(ctx, child, &choice->when)); |
| 2311 | break; |
| 2312 | case LY_STMT_DEFAULT: |
| 2313 | 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] | 2314 | choice->dflt.mod = PARSER_CUR_PMOD(ctx); |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 2315 | break; |
| 2316 | case LY_STMT_ANYDATA: |
| 2317 | PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "choice"); |
| 2318 | /* fall through */ |
| 2319 | case LY_STMT_ANYXML: |
| 2320 | LY_CHECK_RET(lysp_stmt_any(ctx, child, &choice->node, &choice->child)); |
| 2321 | break; |
| 2322 | case LY_STMT_CASE: |
| 2323 | LY_CHECK_RET(lysp_stmt_case(ctx, child, &choice->node, &choice->child)); |
| 2324 | break; |
| 2325 | case LY_STMT_CHOICE: |
| 2326 | PARSER_CHECK_STMTVER2_RET(ctx, "choice", "choice"); |
| 2327 | LY_CHECK_RET(lysp_stmt_choice(ctx, child, &choice->node, &choice->child)); |
| 2328 | break; |
| 2329 | case LY_STMT_CONTAINER: |
| 2330 | LY_CHECK_RET(lysp_stmt_container(ctx, child, &choice->node, &choice->child)); |
| 2331 | break; |
| 2332 | case LY_STMT_LEAF: |
| 2333 | LY_CHECK_RET(lysp_stmt_leaf(ctx, child, &choice->node, &choice->child)); |
| 2334 | break; |
| 2335 | case LY_STMT_LEAF_LIST: |
| 2336 | LY_CHECK_RET(lysp_stmt_leaflist(ctx, child, &choice->node, &choice->child)); |
| 2337 | break; |
| 2338 | case LY_STMT_LIST: |
| 2339 | LY_CHECK_RET(lysp_stmt_list(ctx, child, &choice->node, &choice->child)); |
| 2340 | break; |
| 2341 | case LY_STMT_EXTENSION_INSTANCE: |
Radek Krejci | 39b7fc2 | 2021-02-26 23:29:18 +0100 | [diff] [blame] | 2342 | 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] | 2343 | break; |
| 2344 | default: |
| 2345 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(child->kw), "choice"); |
| 2346 | return LY_EVALID; |
| 2347 | } |
| 2348 | } |
| 2349 | return LY_SUCCESS; |
| 2350 | } |
| 2351 | |
| 2352 | /** |
| 2353 | * @brief Parse the container statement. |
| 2354 | * |
| 2355 | * @param[in] ctx parser context. |
| 2356 | * @param[in] stmt Source statement data from the parsed extension instance. |
| 2357 | * @param[in] parent Parent node to connect to (not into). |
| 2358 | * @param[in,out] siblings Siblings to add to. |
| 2359 | * |
| 2360 | * @return LY_ERR values. |
| 2361 | */ |
| 2362 | static LY_ERR |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 2363 | lysp_stmt_container(struct lys_parser_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_node *parent, |
| 2364 | struct lysp_node **siblings) |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 2365 | { |
| 2366 | struct lysp_node_container *cont; |
| 2367 | |
| 2368 | LY_CHECK_RET(lysp_stmt_validate_value(ctx, Y_IDENTIF_ARG, stmt->arg)); |
| 2369 | |
| 2370 | /* create new container structure */ |
| 2371 | LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, cont, next, LY_EMEM); |
| 2372 | |
| 2373 | LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), stmt->arg, 0, &cont->name)); |
| 2374 | cont->nodetype = LYS_CONTAINER; |
| 2375 | cont->parent = parent; |
| 2376 | |
| 2377 | /* parse substatements */ |
| 2378 | for (const struct lysp_stmt *child = stmt->child; child; child = child->next) { |
| 2379 | switch (child->kw) { |
| 2380 | case LY_STMT_CONFIG: |
| 2381 | LY_CHECK_RET(lysp_stmt_config(ctx, child, &cont->flags, &cont->exts)); |
| 2382 | break; |
| 2383 | case LY_STMT_DESCRIPTION: |
| 2384 | LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &cont->dsc, Y_STR_ARG, &cont->exts)); |
| 2385 | break; |
| 2386 | case LY_STMT_IF_FEATURE: |
| 2387 | LY_CHECK_RET(lysp_stmt_qnames(ctx, child, &cont->iffeatures, Y_STR_ARG, &cont->exts)); |
| 2388 | break; |
| 2389 | case LY_STMT_REFERENCE: |
| 2390 | LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &cont->ref, Y_STR_ARG, &cont->exts)); |
| 2391 | break; |
| 2392 | case LY_STMT_STATUS: |
| 2393 | LY_CHECK_RET(lysp_stmt_status(ctx, child, &cont->flags, &cont->exts)); |
| 2394 | break; |
| 2395 | case LY_STMT_WHEN: |
| 2396 | LY_CHECK_RET(lysp_stmt_when(ctx, child, &cont->when)); |
| 2397 | break; |
| 2398 | case LY_STMT_PRESENCE: |
| 2399 | LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &cont->presence, Y_STR_ARG, &cont->exts)); |
| 2400 | break; |
| 2401 | case LY_STMT_ANYDATA: |
| 2402 | PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "container"); |
| 2403 | /* fall through */ |
| 2404 | case LY_STMT_ANYXML: |
| 2405 | LY_CHECK_RET(lysp_stmt_any(ctx, child, &cont->node, &cont->child)); |
| 2406 | break; |
| 2407 | case LY_STMT_CHOICE: |
| 2408 | LY_CHECK_RET(lysp_stmt_choice(ctx, child, &cont->node, &cont->child)); |
| 2409 | break; |
| 2410 | case LY_STMT_CONTAINER: |
| 2411 | LY_CHECK_RET(lysp_stmt_container(ctx, child, &cont->node, &cont->child)); |
| 2412 | break; |
| 2413 | case LY_STMT_LEAF: |
| 2414 | LY_CHECK_RET(lysp_stmt_leaf(ctx, child, &cont->node, &cont->child)); |
| 2415 | break; |
| 2416 | case LY_STMT_LEAF_LIST: |
| 2417 | LY_CHECK_RET(lysp_stmt_leaflist(ctx, child, &cont->node, &cont->child)); |
| 2418 | break; |
| 2419 | case LY_STMT_LIST: |
| 2420 | LY_CHECK_RET(lysp_stmt_list(ctx, child, &cont->node, &cont->child)); |
| 2421 | break; |
| 2422 | case LY_STMT_USES: |
| 2423 | LY_CHECK_RET(lysp_stmt_uses(ctx, child, &cont->node, &cont->child)); |
| 2424 | break; |
| 2425 | |
| 2426 | case LY_STMT_TYPEDEF: |
| 2427 | LY_CHECK_RET(lysp_stmt_typedef(ctx, child, &cont->node, &cont->typedefs)); |
| 2428 | break; |
| 2429 | case LY_STMT_MUST: |
| 2430 | LY_CHECK_RET(lysp_stmt_restrs(ctx, child, &cont->musts)); |
| 2431 | break; |
| 2432 | case LY_STMT_ACTION: |
| 2433 | PARSER_CHECK_STMTVER2_RET(ctx, "action", "container"); |
| 2434 | LY_CHECK_RET(lysp_stmt_action(ctx, child, &cont->node, &cont->actions)); |
| 2435 | break; |
| 2436 | case LY_STMT_GROUPING: |
| 2437 | LY_CHECK_RET(lysp_stmt_grouping(ctx, child, &cont->node, &cont->groupings)); |
| 2438 | break; |
| 2439 | case LY_STMT_NOTIFICATION: |
| 2440 | PARSER_CHECK_STMTVER2_RET(ctx, "notification", "container"); |
| 2441 | LY_CHECK_RET(lysp_stmt_notif(ctx, child, &cont->node, &cont->notifs)); |
| 2442 | break; |
| 2443 | case LY_STMT_EXTENSION_INSTANCE: |
Radek Krejci | 39b7fc2 | 2021-02-26 23:29:18 +0100 | [diff] [blame] | 2444 | 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] | 2445 | break; |
| 2446 | default: |
| 2447 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(child->kw), "container"); |
| 2448 | return LY_EVALID; |
| 2449 | } |
| 2450 | } |
| 2451 | |
| 2452 | return LY_SUCCESS; |
| 2453 | } |
| 2454 | |
| 2455 | /** |
| 2456 | * @brief Parse the list statement. |
| 2457 | * |
| 2458 | * @param[in] ctx parser context. |
| 2459 | * @param[in] stmt Source statement data from the parsed extension instance. |
| 2460 | * @param[in] parent Parent node to connect to (not into). |
| 2461 | * @param[in,out] siblings Siblings to add to. |
| 2462 | * |
| 2463 | * @return LY_ERR values. |
| 2464 | */ |
| 2465 | static LY_ERR |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 2466 | lysp_stmt_list(struct lys_parser_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_node *parent, |
| 2467 | struct lysp_node **siblings) |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 2468 | { |
| 2469 | struct lysp_node_list *list; |
| 2470 | |
| 2471 | LY_CHECK_RET(lysp_stmt_validate_value(ctx, Y_IDENTIF_ARG, stmt->arg)); |
| 2472 | |
| 2473 | /* create new list structure */ |
| 2474 | LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, list, next, LY_EMEM); |
| 2475 | |
| 2476 | LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), stmt->arg, 0, &list->name)); |
| 2477 | list->nodetype = LYS_LIST; |
| 2478 | list->parent = parent; |
| 2479 | |
| 2480 | /* parse substatements */ |
| 2481 | for (const struct lysp_stmt *child = stmt->child; child; child = child->next) { |
| 2482 | switch (child->kw) { |
| 2483 | case LY_STMT_CONFIG: |
| 2484 | LY_CHECK_RET(lysp_stmt_config(ctx, child, &list->flags, &list->exts)); |
| 2485 | break; |
| 2486 | case LY_STMT_DESCRIPTION: |
| 2487 | LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &list->dsc, Y_STR_ARG, &list->exts)); |
| 2488 | break; |
| 2489 | case LY_STMT_IF_FEATURE: |
| 2490 | LY_CHECK_RET(lysp_stmt_qnames(ctx, child, &list->iffeatures, Y_STR_ARG, &list->exts)); |
| 2491 | break; |
| 2492 | case LY_STMT_REFERENCE: |
| 2493 | LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &list->ref, Y_STR_ARG, &list->exts)); |
| 2494 | break; |
| 2495 | case LY_STMT_STATUS: |
| 2496 | LY_CHECK_RET(lysp_stmt_status(ctx, child, &list->flags, &list->exts)); |
| 2497 | break; |
| 2498 | case LY_STMT_WHEN: |
| 2499 | LY_CHECK_RET(lysp_stmt_when(ctx, child, &list->when)); |
| 2500 | break; |
| 2501 | case LY_STMT_KEY: |
| 2502 | LY_CHECK_RET(lysp_stmt_text_field(ctx, child, 0, &list->key, Y_STR_ARG, &list->exts)); |
| 2503 | break; |
| 2504 | case LY_STMT_MAX_ELEMENTS: |
| 2505 | LY_CHECK_RET(lysp_stmt_maxelements(ctx, child, &list->max, &list->flags, &list->exts)); |
| 2506 | break; |
| 2507 | case LY_STMT_MIN_ELEMENTS: |
| 2508 | LY_CHECK_RET(lysp_stmt_minelements(ctx, child, &list->min, &list->flags, &list->exts)); |
| 2509 | break; |
| 2510 | case LY_STMT_ORDERED_BY: |
| 2511 | LY_CHECK_RET(lysp_stmt_orderedby(ctx, child, &list->flags, &list->exts)); |
| 2512 | break; |
| 2513 | case LY_STMT_UNIQUE: |
| 2514 | LY_CHECK_RET(lysp_stmt_qnames(ctx, child, &list->uniques, Y_STR_ARG, &list->exts)); |
| 2515 | break; |
| 2516 | |
| 2517 | case LY_STMT_ANYDATA: |
| 2518 | PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "list"); |
| 2519 | /* fall through */ |
| 2520 | case LY_STMT_ANYXML: |
| 2521 | LY_CHECK_RET(lysp_stmt_any(ctx, child, &list->node, &list->child)); |
| 2522 | break; |
| 2523 | case LY_STMT_CHOICE: |
| 2524 | LY_CHECK_RET(lysp_stmt_choice(ctx, child, &list->node, &list->child)); |
| 2525 | break; |
| 2526 | case LY_STMT_CONTAINER: |
| 2527 | LY_CHECK_RET(lysp_stmt_container(ctx, child, &list->node, &list->child)); |
| 2528 | break; |
| 2529 | case LY_STMT_LEAF: |
| 2530 | LY_CHECK_RET(lysp_stmt_leaf(ctx, child, &list->node, &list->child)); |
| 2531 | break; |
| 2532 | case LY_STMT_LEAF_LIST: |
| 2533 | LY_CHECK_RET(lysp_stmt_leaflist(ctx, child, &list->node, &list->child)); |
| 2534 | break; |
| 2535 | case LY_STMT_LIST: |
| 2536 | LY_CHECK_RET(lysp_stmt_list(ctx, child, &list->node, &list->child)); |
| 2537 | break; |
| 2538 | case LY_STMT_USES: |
| 2539 | LY_CHECK_RET(lysp_stmt_uses(ctx, child, &list->node, &list->child)); |
| 2540 | break; |
| 2541 | |
| 2542 | case LY_STMT_TYPEDEF: |
| 2543 | LY_CHECK_RET(lysp_stmt_typedef(ctx, child, &list->node, &list->typedefs)); |
| 2544 | break; |
| 2545 | case LY_STMT_MUST: |
| 2546 | LY_CHECK_RET(lysp_stmt_restrs(ctx, child, &list->musts)); |
| 2547 | break; |
| 2548 | case LY_STMT_ACTION: |
| 2549 | PARSER_CHECK_STMTVER2_RET(ctx, "action", "list"); |
| 2550 | LY_CHECK_RET(lysp_stmt_action(ctx, child, &list->node, &list->actions)); |
| 2551 | break; |
| 2552 | case LY_STMT_GROUPING: |
| 2553 | LY_CHECK_RET(lysp_stmt_grouping(ctx, child, &list->node, &list->groupings)); |
| 2554 | break; |
| 2555 | case LY_STMT_NOTIFICATION: |
| 2556 | PARSER_CHECK_STMTVER2_RET(ctx, "notification", "list"); |
| 2557 | LY_CHECK_RET(lysp_stmt_notif(ctx, child, &list->node, &list->notifs)); |
| 2558 | break; |
| 2559 | case LY_STMT_EXTENSION_INSTANCE: |
Radek Krejci | 39b7fc2 | 2021-02-26 23:29:18 +0100 | [diff] [blame] | 2560 | 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] | 2561 | break; |
| 2562 | default: |
| 2563 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(child->kw), "list"); |
| 2564 | return LY_EVALID; |
| 2565 | } |
| 2566 | } |
| 2567 | |
| 2568 | return LY_SUCCESS; |
| 2569 | } |
| 2570 | |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 2571 | LY_ERR |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 2572 | lysp_stmt_parse(struct lysc_ctx *ctx, const struct lysp_stmt *stmt, void **result, struct lysp_ext_instance **exts) |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 2573 | { |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 2574 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 2575 | uint16_t flags; |
| 2576 | struct lys_parser_ctx pctx = {0}; |
Michal Vasko | 8a67eff | 2021-12-07 14:04:47 +0100 | [diff] [blame] | 2577 | struct ly_set pmods = {0}; |
| 2578 | void *objs; |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 2579 | |
Michal Vasko | 8a67eff | 2021-12-07 14:04:47 +0100 | [diff] [blame] | 2580 | /* local context */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 2581 | pctx.format = LYS_IN_YANG; |
Michal Vasko | 8a67eff | 2021-12-07 14:04:47 +0100 | [diff] [blame] | 2582 | pctx.parsed_mods = &pmods; |
| 2583 | objs = &ctx->pmod; |
| 2584 | pmods.objs = objs; |
| 2585 | pmods.count = 1; |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 2586 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 2587 | LOG_LOCSET(NULL, NULL, ctx->path, NULL); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 2588 | |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 2589 | switch (stmt->kw) { |
| 2590 | case LY_STMT_ACTION: |
| 2591 | case LY_STMT_RPC: |
| 2592 | ret = lysp_stmt_action(&pctx, stmt, NULL, (struct lysp_node_action **)result); |
| 2593 | break; |
| 2594 | case LY_STMT_ANYDATA: |
| 2595 | case LY_STMT_ANYXML: |
| 2596 | ret = lysp_stmt_any(&pctx, stmt, NULL, (struct lysp_node **)result); |
| 2597 | break; |
| 2598 | case LY_STMT_AUGMENT: |
| 2599 | ret = lysp_stmt_augment(&pctx, stmt, NULL, (struct lysp_node_augment **)result); |
| 2600 | break; |
| 2601 | case LY_STMT_BASE: |
| 2602 | ret = lysp_stmt_text_fields(&pctx, stmt, (const char ***)result, Y_PREF_IDENTIF_ARG, exts); |
| 2603 | break; |
| 2604 | case LY_STMT_BIT: |
| 2605 | case LY_STMT_ENUM: |
| 2606 | ret = lysp_stmt_type_enum(&pctx, stmt, (struct lysp_type_enum **)result); |
| 2607 | break; |
| 2608 | case LY_STMT_CASE: |
| 2609 | ret = lysp_stmt_case(&pctx, stmt, NULL, (struct lysp_node **)result); |
| 2610 | break; |
| 2611 | case LY_STMT_CHOICE: |
| 2612 | ret = lysp_stmt_choice(&pctx, stmt, NULL, (struct lysp_node **)result); |
| 2613 | break; |
| 2614 | case LY_STMT_CONFIG: |
Michal Vasko | 39ed7a2 | 2022-02-21 08:34:18 +0100 | [diff] [blame] | 2615 | assert(*result); |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 2616 | ret = lysp_stmt_config(&pctx, stmt, *(uint16_t **)result, exts); |
| 2617 | break; |
| 2618 | case LY_STMT_CONTACT: |
| 2619 | case LY_STMT_DESCRIPTION: |
| 2620 | case LY_STMT_ERROR_APP_TAG: |
| 2621 | case LY_STMT_ERROR_MESSAGE: |
| 2622 | case LY_STMT_KEY: |
| 2623 | case LY_STMT_NAMESPACE: |
| 2624 | case LY_STMT_ORGANIZATION: |
| 2625 | case LY_STMT_PRESENCE: |
| 2626 | case LY_STMT_REFERENCE: |
| 2627 | case LY_STMT_UNITS: |
| 2628 | ret = lysp_stmt_text_field(&pctx, stmt, 0, (const char **)result, Y_STR_ARG, exts); |
| 2629 | break; |
| 2630 | case LY_STMT_CONTAINER: |
| 2631 | ret = lysp_stmt_container(&pctx, stmt, NULL, (struct lysp_node **)result); |
| 2632 | break; |
| 2633 | case LY_STMT_DEFAULT: |
| 2634 | case LY_STMT_IF_FEATURE: |
| 2635 | case LY_STMT_UNIQUE: |
| 2636 | ret = lysp_stmt_qnames(&pctx, stmt, (struct lysp_qname **)result, Y_STR_ARG, exts); |
| 2637 | break; |
| 2638 | case LY_STMT_EXTENSION_INSTANCE: |
Radek Krejci | 39b7fc2 | 2021-02-26 23:29:18 +0100 | [diff] [blame] | 2639 | ret = lysp_stmt_ext(&pctx, stmt, LY_STMT_EXTENSION_INSTANCE, 0, exts); |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 2640 | break; |
| 2641 | case LY_STMT_FRACTION_DIGITS: |
| 2642 | ret = lysp_stmt_type_fracdigits(&pctx, stmt, *(uint8_t **)result, exts); |
| 2643 | break; |
| 2644 | case LY_STMT_GROUPING: |
| 2645 | ret = lysp_stmt_grouping(&pctx, stmt, NULL, (struct lysp_node_grp **)result); |
| 2646 | break; |
| 2647 | case LY_STMT_INPUT: |
| 2648 | case LY_STMT_OUTPUT: { |
| 2649 | struct lysp_node_action_inout *inout; |
| 2650 | |
| 2651 | *result = inout = calloc(1, sizeof *inout); |
| 2652 | LY_CHECK_ERR_RET(!inout, LOGMEM(ctx->ctx), LY_EMEM); |
| 2653 | ret = lysp_stmt_inout(&pctx, stmt, NULL, inout); |
| 2654 | break; |
| 2655 | } |
| 2656 | case LY_STMT_LEAF: |
| 2657 | ret = lysp_stmt_leaf(&pctx, stmt, NULL, (struct lysp_node **)result); |
| 2658 | break; |
| 2659 | case LY_STMT_LEAF_LIST: |
| 2660 | ret = lysp_stmt_leaflist(&pctx, stmt, NULL, (struct lysp_node **)result); |
| 2661 | break; |
| 2662 | case LY_STMT_LENGTH: |
| 2663 | case LY_STMT_MUST: |
| 2664 | case LY_STMT_RANGE: |
| 2665 | ret = lysp_stmt_restrs(&pctx, stmt, (struct lysp_restr **)result); |
| 2666 | break; |
| 2667 | case LY_STMT_LIST: |
| 2668 | ret = lysp_stmt_list(&pctx, stmt, NULL, (struct lysp_node **)result); |
| 2669 | break; |
| 2670 | case LY_STMT_MANDATORY: |
| 2671 | ret = lysp_stmt_mandatory(&pctx, stmt, *(uint16_t **)result, exts); |
| 2672 | break; |
| 2673 | case LY_STMT_MAX_ELEMENTS: |
| 2674 | flags = 0; |
| 2675 | ret = lysp_stmt_maxelements(&pctx, stmt, *(uint32_t **)result, &flags, exts); |
| 2676 | break; |
| 2677 | case LY_STMT_MIN_ELEMENTS: |
| 2678 | flags = 0; |
| 2679 | ret = lysp_stmt_minelements(&pctx, stmt, *(uint32_t **)result, &flags, exts); |
| 2680 | break; |
| 2681 | case LY_STMT_MODIFIER: |
| 2682 | ret = lysp_stmt_type_pattern_modifier(&pctx, stmt, (const char **)result, exts); |
| 2683 | break; |
| 2684 | case LY_STMT_NOTIFICATION: |
| 2685 | ret = lysp_stmt_notif(&pctx, stmt, NULL, (struct lysp_node_notif **)result); |
| 2686 | break; |
| 2687 | case LY_STMT_ORDERED_BY: |
| 2688 | ret = lysp_stmt_orderedby(&pctx, stmt, *(uint16_t **)result, exts); |
| 2689 | break; |
| 2690 | case LY_STMT_PATH: { |
| 2691 | const char *str_path = NULL; |
| 2692 | |
| 2693 | LY_CHECK_RET(lysp_stmt_text_field(&pctx, stmt, 0, &str_path, Y_STR_ARG, exts)); |
Michal Vasko | ed725d7 | 2021-06-23 12:03:45 +0200 | [diff] [blame] | 2694 | ret = ly_path_parse(ctx->ctx, NULL, str_path, 0, 1, LY_PATH_BEGIN_EITHER, |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 2695 | LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_LEAFREF, (struct lyxp_expr **)result); |
| 2696 | lydict_remove(ctx->ctx, str_path); |
| 2697 | break; |
| 2698 | } |
| 2699 | case LY_STMT_PATTERN: |
| 2700 | ret = lysp_stmt_type_pattern(&pctx, stmt, (struct lysp_restr **)result); |
| 2701 | break; |
| 2702 | case LY_STMT_POSITION: |
| 2703 | case LY_STMT_VALUE: |
| 2704 | flags = 0; |
| 2705 | ret = lysp_stmt_type_enum_value_pos(&pctx, stmt, *(int64_t **)result, &flags, exts); |
| 2706 | break; |
| 2707 | case LY_STMT_PREFIX: |
| 2708 | ret = lysp_stmt_text_field(&pctx, stmt, 0, (const char **)result, Y_IDENTIF_ARG, exts); |
| 2709 | break; |
| 2710 | case LY_STMT_REFINE: |
| 2711 | ret = lysp_stmt_refine(&pctx, stmt, (struct lysp_refine **)result); |
| 2712 | break; |
| 2713 | case LY_STMT_REQUIRE_INSTANCE: |
| 2714 | flags = 0; |
| 2715 | ret = lysp_stmt_type_reqinstance(&pctx, stmt, *(uint8_t **)result, &flags, exts); |
| 2716 | break; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2717 | case LY_STMT_STATUS: |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 2718 | ret = lysp_stmt_status(&pctx, stmt, *(uint16_t **)result, exts); |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 2719 | break; |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 2720 | case LY_STMT_TYPE: { |
| 2721 | struct lysp_type *type; |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 2722 | |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 2723 | *result = type = calloc(1, sizeof *type); |
| 2724 | LY_CHECK_ERR_RET(!type, LOGMEM(ctx->ctx), LY_EMEM); |
| 2725 | ret = lysp_stmt_type(&pctx, stmt, type); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 2726 | break; |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 2727 | } |
Radek Krejci | 76c8c4e | 2021-02-17 10:16:48 +0100 | [diff] [blame] | 2728 | case LY_STMT_TYPEDEF: |
| 2729 | ret = lysp_stmt_typedef(&pctx, stmt, NULL, (struct lysp_tpdf **)result); |
| 2730 | break; |
| 2731 | case LY_STMT_USES: |
| 2732 | ret = lysp_stmt_uses(&pctx, stmt, NULL, (struct lysp_node **)result); |
| 2733 | break; |
| 2734 | case LY_STMT_WHEN: |
| 2735 | ret = lysp_stmt_when(&pctx, stmt, (struct lysp_when **)result); |
| 2736 | break; |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 2737 | default: |
| 2738 | LOGINT(ctx->ctx); |
| 2739 | return LY_EINT; |
| 2740 | } |
| 2741 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 2742 | LOG_LOCBACK(0, 0, 1, 0); |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 2743 | return ret; |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 2744 | } |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 2745 | |
| 2746 | void |
| 2747 | lys_parser_fill_filepath(struct ly_ctx *ctx, struct ly_in *in, const char **filepath) |
| 2748 | { |
| 2749 | char path[PATH_MAX]; |
| 2750 | |
| 2751 | #ifndef __APPLE__ |
| 2752 | char proc_path[32]; |
| 2753 | int len; |
| 2754 | #endif |
| 2755 | |
| 2756 | LY_CHECK_ARG_RET(NULL, ctx, in, filepath, ); |
| 2757 | if (*filepath) { |
| 2758 | /* filepath already set */ |
| 2759 | return; |
| 2760 | } |
| 2761 | |
| 2762 | switch (in->type) { |
| 2763 | case LY_IN_FILEPATH: |
| 2764 | if (realpath(in->method.fpath.filepath, path) != NULL) { |
| 2765 | lydict_insert(ctx, path, 0, filepath); |
| 2766 | } else { |
| 2767 | lydict_insert(ctx, in->method.fpath.filepath, 0, filepath); |
| 2768 | } |
| 2769 | |
| 2770 | break; |
| 2771 | case LY_IN_FD: |
| 2772 | #ifdef __APPLE__ |
| 2773 | if (fcntl(in->method.fd, F_GETPATH, path) != -1) { |
| 2774 | lydict_insert(ctx, path, 0, filepath); |
| 2775 | } |
| 2776 | #elif defined _WIN32 |
| 2777 | HANDLE h = _get_osfhandle(in->method.fd); |
| 2778 | FILE_NAME_INFO info; |
| 2779 | if (GetFileInformationByHandleEx(h, FileNameInfo, &info, sizeof info)) { |
| 2780 | char *buf = calloc(info.FileNameLength + 1 /* trailing NULL */, MB_CUR_MAX); |
| 2781 | len = wcstombs(buf, info.FileName, info.FileNameLength * MB_CUR_MAX); |
| 2782 | lydict_insert(ctx, buf, len, filepath); |
| 2783 | } |
| 2784 | #else |
| 2785 | /* get URI if there is /proc */ |
| 2786 | sprintf(proc_path, "/proc/self/fd/%d", in->method.fd); |
| 2787 | if ((len = readlink(proc_path, path, PATH_MAX - 1)) > 0) { |
| 2788 | lydict_insert(ctx, path, len, filepath); |
| 2789 | } |
| 2790 | #endif |
| 2791 | break; |
| 2792 | case LY_IN_MEMORY: |
| 2793 | case LY_IN_FILE: |
| 2794 | /* nothing to do */ |
| 2795 | break; |
| 2796 | default: |
| 2797 | LOGINT(ctx); |
| 2798 | break; |
| 2799 | } |
| 2800 | } |