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