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