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" |
Michal Vasko | afac782 | 2020-10-20 14:22:26 +0200 | [diff] [blame] | 24 | #include "in.h" |
Radek Krejci | 47fab89 | 2020-11-05 17:02:41 +0100 | [diff] [blame] | 25 | #include "log.h" |
Radek Krejci | ca376bd | 2020-06-11 16:04:06 +0200 | [diff] [blame] | 26 | #include "parser_schema.h" |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 27 | #include "path.h" |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 28 | #include "schema_compile.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 29 | #include "tree.h" |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 30 | #include "tree_schema.h" |
| 31 | #include "tree_schema_internal.h" |
| 32 | |
| 33 | static LY_ERR |
| 34 | lysp_stmt_validate_value(struct lys_parser_ctx *ctx, enum yang_arg val_type, const char *val) |
| 35 | { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 36 | uint8_t prefix = 0; |
| 37 | ly_bool first = 1; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 38 | uint32_t c; |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 39 | size_t utf8_char_len; |
| 40 | |
| 41 | while (*val) { |
| 42 | LY_CHECK_ERR_RET(ly_getutf8(&val, &c, &utf8_char_len), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 43 | LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, (val)[-utf8_char_len]), LY_EVALID); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 44 | |
| 45 | switch (val_type) { |
| 46 | case Y_IDENTIF_ARG: |
| 47 | LY_CHECK_RET(lysp_check_identifierchar(ctx, c, first, NULL)); |
| 48 | break; |
| 49 | case Y_PREF_IDENTIF_ARG: |
| 50 | LY_CHECK_RET(lysp_check_identifierchar(ctx, c, first, &prefix)); |
| 51 | break; |
| 52 | case Y_STR_ARG: |
| 53 | case Y_MAYBE_STR_ARG: |
| 54 | LY_CHECK_RET(lysp_check_stringchar(ctx, c)); |
| 55 | break; |
| 56 | } |
| 57 | first = 0; |
| 58 | } |
| 59 | |
| 60 | return LY_SUCCESS; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * @brief Parse extension instance. |
| 65 | * |
| 66 | * @param[in] ctx yang parser context for logging. |
| 67 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 68 | * @param[in] ext_name Extension instance substatement name (keyword). |
| 69 | * @param[in] ext_name_len Extension instance substatement name length. |
| 70 | * @param[in] insubstmt Type of the keyword this extension instance is a substatement of. |
| 71 | * @param[in] insubstmt_index Index of the keyword instance this extension instance is a substatement of. |
| 72 | * @param[in,out] exts Extension instances to add to. |
| 73 | * |
| 74 | * @return LY_ERR values. |
| 75 | */ |
| 76 | static LY_ERR |
| 77 | lysp_stmt_ext(struct lys_parser_ctx *ctx, const struct lysp_stmt *stmt, LYEXT_SUBSTMT insubstmt, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 78 | LY_ARRAY_COUNT_TYPE insubstmt_index, struct lysp_ext_instance **exts) |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 79 | { |
| 80 | struct lysp_ext_instance *e; |
| 81 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 82 | LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *exts, e, LY_EMEM); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 83 | |
| 84 | /* store name and insubstmt info */ |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 85 | LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), stmt->stmt, 0, &e->name)); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 86 | e->insubstmt = insubstmt; |
| 87 | e->insubstmt_index = insubstmt_index; |
| 88 | /* TODO (duplicate) e->child = stmt->child; */ |
| 89 | |
| 90 | /* get optional argument */ |
| 91 | if (stmt->arg) { |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 92 | 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] | 93 | } |
| 94 | |
| 95 | return LY_SUCCESS; |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * @brief Parse a generic text field without specific constraints. Those are contact, organization, |
| 100 | * description, etc... |
| 101 | * |
| 102 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 103 | * @param[in] stmt Statement structure. |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 104 | * @param[in] substmt Type of this substatement. |
| 105 | * @param[in] substmt_index Index of this substatement. |
| 106 | * @param[in,out] value Place to store the parsed value. |
| 107 | * @param[in] arg Type of the YANG keyword argument (of the value). |
| 108 | * @param[in,out] exts Extension instances to add to. |
| 109 | * |
| 110 | * @return LY_ERR values. |
| 111 | */ |
| 112 | static LY_ERR |
| 113 | lysp_stmt_text_field(struct lys_parser_ctx *ctx, const struct lysp_stmt *stmt, LYEXT_SUBSTMT substmt, uint32_t substmt_index, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 114 | const char **value, enum yang_arg arg, struct lysp_ext_instance **exts) |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 115 | { |
| 116 | const struct lysp_stmt *child; |
| 117 | |
| 118 | if (*value) { |
| 119 | LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, lyext_substmt2str(substmt)); |
| 120 | return LY_EVALID; |
| 121 | } |
| 122 | |
| 123 | LY_CHECK_RET(lysp_stmt_validate_value(ctx, arg, stmt->arg)); |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 124 | LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), stmt->arg, 0, value)); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 125 | |
| 126 | for (child = stmt->child; child; child = child->next) { |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 127 | struct ly_in *in; |
| 128 | LY_CHECK_RET(ly_in_new_memory(child->stmt, &in)); |
| 129 | enum ly_stmt kw = lysp_match_kw(NULL, in); |
| 130 | ly_in_free(in, 0); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 131 | |
| 132 | switch (kw) { |
| 133 | case LY_STMT_EXTENSION_INSTANCE: |
| 134 | LY_CHECK_RET(lysp_stmt_ext(ctx, child, substmt, substmt_index, exts)); |
| 135 | break; |
| 136 | default: |
| 137 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), lyext_substmt2str(substmt)); |
| 138 | return LY_EVALID; |
| 139 | } |
| 140 | } |
| 141 | return LY_SUCCESS; |
| 142 | } |
| 143 | |
| 144 | /** |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 145 | * @brief Parse a qname that can have more instances such as if-feature. |
| 146 | * |
| 147 | * @param[in] ctx yang parser context for logging. |
| 148 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 149 | * @param[in] substmt Type of this substatement. |
| 150 | * @param[in,out] qnames Parsed qnames to add to. |
| 151 | * @param[in] arg Type of the expected argument. |
| 152 | * @param[in,out] exts Extension instances to add to. |
| 153 | * |
| 154 | * @return LY_ERR values. |
| 155 | */ |
| 156 | static LY_ERR |
| 157 | lysp_stmt_qnames(struct lys_parser_ctx *ctx, const struct lysp_stmt *stmt, LYEXT_SUBSTMT substmt, |
| 158 | struct lysp_qname **qnames, enum yang_arg arg, struct lysp_ext_instance **exts) |
| 159 | { |
| 160 | struct lysp_qname *item; |
| 161 | const struct lysp_stmt *child; |
| 162 | |
| 163 | LY_CHECK_RET(lysp_stmt_validate_value(ctx, arg, stmt->arg)); |
| 164 | |
| 165 | /* allocate new pointer */ |
| 166 | LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *qnames, item, LY_EMEM); |
| 167 | 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] | 168 | item->mod = ctx->parsed_mod; |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 169 | |
| 170 | for (child = stmt->child; child; child = child->next) { |
| 171 | struct ly_in *in; |
| 172 | LY_CHECK_RET(ly_in_new_memory(child->stmt, &in)); |
| 173 | enum ly_stmt kw = lysp_match_kw(NULL, in); |
| 174 | ly_in_free(in, 0); |
| 175 | |
| 176 | switch (kw) { |
| 177 | case LY_STMT_EXTENSION_INSTANCE: |
| 178 | LY_CHECK_RET(lysp_stmt_ext(ctx, child, substmt, LY_ARRAY_COUNT(*qnames) - 1, exts)); |
| 179 | break; |
| 180 | default: |
| 181 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), lyext_substmt2str(substmt)); |
| 182 | return LY_EVALID; |
| 183 | } |
| 184 | } |
| 185 | return LY_SUCCESS; |
| 186 | } |
| 187 | |
| 188 | /** |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 189 | * @brief Parse a generic text field that can have more instances such as base. |
| 190 | * |
| 191 | * @param[in] ctx yang parser context for logging. |
| 192 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 193 | * @param[in] substmt Type of this substatement. |
| 194 | * @param[in,out] texts Parsed values to add to. |
| 195 | * @param[in] arg Type of the expected argument. |
| 196 | * @param[in,out] exts Extension instances to add to. |
| 197 | * |
| 198 | * @return LY_ERR values. |
| 199 | */ |
| 200 | static LY_ERR |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 201 | lysp_stmt_text_fields(struct lys_parser_ctx *ctx, const struct lysp_stmt *stmt, LYEXT_SUBSTMT substmt, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 202 | const char ***texts, enum yang_arg arg, struct lysp_ext_instance **exts) |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 203 | { |
| 204 | const char **item; |
| 205 | const struct lysp_stmt *child; |
| 206 | |
| 207 | LY_CHECK_RET(lysp_stmt_validate_value(ctx, arg, stmt->arg)); |
| 208 | |
| 209 | /* allocate new pointer */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 210 | LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *texts, item, LY_EMEM); |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 211 | LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), stmt->arg, 0, item)); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 212 | |
| 213 | for (child = stmt->child; child; child = child->next) { |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 214 | struct ly_in *in; |
| 215 | LY_CHECK_RET(ly_in_new_memory(child->stmt, &in)); |
| 216 | enum ly_stmt kw = lysp_match_kw(NULL, in); |
| 217 | ly_in_free(in, 0); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 218 | |
| 219 | switch (kw) { |
| 220 | case LY_STMT_EXTENSION_INSTANCE: |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 221 | LY_CHECK_RET(lysp_stmt_ext(ctx, child, substmt, LY_ARRAY_COUNT(*texts) - 1, exts)); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 222 | break; |
| 223 | default: |
| 224 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), lyext_substmt2str(substmt)); |
| 225 | return LY_EVALID; |
| 226 | } |
| 227 | } |
| 228 | return LY_SUCCESS; |
| 229 | } |
| 230 | |
| 231 | /** |
| 232 | * @brief Parse the status statement. |
| 233 | * |
| 234 | * @param[in] ctx yang parser context for logging. |
| 235 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 236 | * @param[in,out] flags Flags to add to. |
| 237 | * @param[in,out] exts Extension instances to add to. |
| 238 | * |
| 239 | * @return LY_ERR values. |
| 240 | */ |
| 241 | static LY_ERR |
| 242 | lysp_stmt_status(struct lys_parser_ctx *ctx, const struct lysp_stmt *stmt, uint16_t *flags, struct lysp_ext_instance **exts) |
| 243 | { |
| 244 | size_t arg_len; |
| 245 | const struct lysp_stmt *child; |
| 246 | |
| 247 | if (*flags & LYS_STATUS_MASK) { |
| 248 | LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "status"); |
| 249 | return LY_EVALID; |
| 250 | } |
| 251 | |
| 252 | LY_CHECK_RET(lysp_stmt_validate_value(ctx, Y_STR_ARG, stmt->arg)); |
| 253 | arg_len = strlen(stmt->arg); |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame^] | 254 | 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] | 255 | *flags |= LYS_STATUS_CURR; |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame^] | 256 | } 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] | 257 | *flags |= LYS_STATUS_DEPRC; |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame^] | 258 | } 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] | 259 | *flags |= LYS_STATUS_OBSLT; |
| 260 | } else { |
| 261 | LOGVAL_PARSER(ctx, LY_VCODE_INVAL, arg_len, stmt->arg, "status"); |
| 262 | return LY_EVALID; |
| 263 | } |
| 264 | |
| 265 | for (child = stmt->child; child; child = child->next) { |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 266 | struct ly_in *in; |
| 267 | LY_CHECK_RET(ly_in_new_memory(child->stmt, &in)); |
| 268 | enum ly_stmt kw = lysp_match_kw(NULL, in); |
| 269 | ly_in_free(in, 0); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 270 | |
| 271 | switch (kw) { |
| 272 | case LY_STMT_EXTENSION_INSTANCE: |
| 273 | LY_CHECK_RET(lysp_stmt_ext(ctx, child, LYEXT_SUBSTMT_STATUS, 0, exts)); |
| 274 | break; |
| 275 | default: |
| 276 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "status"); |
| 277 | return LY_EVALID; |
| 278 | } |
| 279 | } |
| 280 | return LY_SUCCESS; |
| 281 | } |
| 282 | |
| 283 | /** |
| 284 | * @brief Parse a restriction such as range or length. |
| 285 | * |
| 286 | * @param[in] ctx yang parser context for logging. |
| 287 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 288 | * @param[in] restr_kw Type of this particular restriction. |
| 289 | * @param[in,out] exts Extension instances to add to. |
| 290 | * |
| 291 | * @return LY_ERR values. |
| 292 | */ |
| 293 | static LY_ERR |
| 294 | lysp_stmt_restr(struct lys_parser_ctx *ctx, const struct lysp_stmt *stmt, enum ly_stmt restr_kw, struct lysp_restr *restr) |
| 295 | { |
| 296 | const struct lysp_stmt *child; |
| 297 | |
| 298 | 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] | 299 | 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] | 300 | restr->arg.mod = ctx->parsed_mod; |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 301 | |
| 302 | for (child = stmt->child; child; child = child->next) { |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 303 | struct ly_in *in; |
| 304 | LY_CHECK_RET(ly_in_new_memory(child->stmt, &in)); |
| 305 | enum ly_stmt kw = lysp_match_kw(NULL, in); |
| 306 | ly_in_free(in, 0); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 307 | |
| 308 | switch (kw) { |
| 309 | case LY_STMT_DESCRIPTION: |
| 310 | LY_CHECK_RET(lysp_stmt_text_field(ctx, child, LYEXT_SUBSTMT_DESCRIPTION, 0, &restr->dsc, Y_STR_ARG, &restr->exts)); |
| 311 | break; |
| 312 | case LY_STMT_REFERENCE: |
| 313 | LY_CHECK_RET(lysp_stmt_text_field(ctx, child, LYEXT_SUBSTMT_REFERENCE, 0, &restr->ref, Y_STR_ARG, &restr->exts)); |
| 314 | break; |
| 315 | case LY_STMT_ERROR_APP_TAG: |
| 316 | LY_CHECK_RET(lysp_stmt_text_field(ctx, child, LYEXT_SUBSTMT_ERRTAG, 0, &restr->eapptag, Y_STR_ARG, &restr->exts)); |
| 317 | break; |
| 318 | case LY_STMT_ERROR_MESSAGE: |
| 319 | LY_CHECK_RET(lysp_stmt_text_field(ctx, child, LYEXT_SUBSTMT_ERRMSG, 0, &restr->emsg, Y_STR_ARG, &restr->exts)); |
| 320 | break; |
| 321 | case LY_STMT_EXTENSION_INSTANCE: |
| 322 | LY_CHECK_RET(lysp_stmt_ext(ctx, child, LYEXT_SUBSTMT_SELF, 0, &restr->exts)); |
| 323 | break; |
| 324 | default: |
| 325 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(restr_kw)); |
| 326 | return LY_EVALID; |
| 327 | } |
| 328 | } |
| 329 | return LY_SUCCESS; |
| 330 | } |
| 331 | |
| 332 | /** |
| 333 | * @brief Parse a restriction that can have more instances such as must. |
| 334 | * |
| 335 | * @param[in] ctx yang parser context for logging. |
| 336 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 337 | * @param[in] restr_kw Type of this particular restriction. |
| 338 | * @param[in,out] restrs Restrictions to add to. |
| 339 | * |
| 340 | * @return LY_ERR values. |
| 341 | */ |
| 342 | static LY_ERR |
| 343 | lysp_stmt_restrs(struct lys_parser_ctx *ctx, const struct lysp_stmt *stmt, enum ly_stmt restr_kw, struct lysp_restr **restrs) |
| 344 | { |
| 345 | struct lysp_restr *restr; |
| 346 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 347 | LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *restrs, restr, LY_EMEM); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 348 | return lysp_stmt_restr(ctx, stmt, restr_kw, restr); |
| 349 | } |
| 350 | |
| 351 | /** |
| 352 | * @brief Parse the value or position statement. Substatement of type enum statement. |
| 353 | * |
| 354 | * @param[in] ctx yang parser context for logging. |
| 355 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 356 | * @param[in] val_kw Type of this particular keyword. |
| 357 | * @param[in,out] value Value to write to. |
| 358 | * @param[in,out] flags Flags to write to. |
| 359 | * @param[in,out] exts Extension instances to add to. |
| 360 | * |
| 361 | * @return LY_ERR values. |
| 362 | */ |
| 363 | static LY_ERR |
| 364 | lysp_stmt_type_enum_value_pos(struct lys_parser_ctx *ctx, const struct lysp_stmt *stmt, enum ly_stmt val_kw, int64_t *value, uint16_t *flags, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 365 | struct lysp_ext_instance **exts) |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 366 | { |
| 367 | size_t arg_len; |
| 368 | char *ptr = NULL; |
| 369 | long int num = 0; |
| 370 | unsigned long int unum = 0; |
| 371 | struct lysp_stmt *child; |
| 372 | |
| 373 | if (*flags & LYS_SET_VALUE) { |
| 374 | LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(val_kw)); |
| 375 | return LY_EVALID; |
| 376 | } |
| 377 | *flags |= LYS_SET_VALUE; |
| 378 | |
| 379 | LY_CHECK_RET(lysp_stmt_validate_value(ctx, Y_STR_ARG, stmt->arg)); |
| 380 | |
| 381 | arg_len = strlen(stmt->arg); |
| 382 | if (!arg_len || (stmt->arg[0] == '+') || ((stmt->arg[0] == '0') && (arg_len > 1)) || ((val_kw == LY_STMT_POSITION) && !strncmp(stmt->arg, "-0", 2))) { |
| 383 | LOGVAL_PARSER(ctx, LY_VCODE_INVAL, arg_len, stmt->arg, ly_stmt2str(val_kw)); |
| 384 | goto error; |
| 385 | } |
| 386 | |
| 387 | errno = 0; |
| 388 | if (val_kw == LY_STMT_VALUE) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame^] | 389 | num = strtol(stmt->arg, &ptr, LY_BASE_DEC); |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 390 | if ((num < INT64_C(-2147483648)) || (num > INT64_C(2147483647))) { |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 391 | LOGVAL_PARSER(ctx, LY_VCODE_INVAL, arg_len, stmt->arg, ly_stmt2str(val_kw)); |
| 392 | goto error; |
| 393 | } |
| 394 | } else { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame^] | 395 | unum = strtoul(stmt->arg, &ptr, LY_BASE_DEC); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 396 | if (unum > UINT64_C(4294967295)) { |
| 397 | LOGVAL_PARSER(ctx, LY_VCODE_INVAL, arg_len, stmt->arg, ly_stmt2str(val_kw)); |
| 398 | goto error; |
| 399 | } |
| 400 | } |
| 401 | /* we have not parsed the whole argument */ |
| 402 | if ((size_t)(ptr - stmt->arg) != arg_len) { |
| 403 | LOGVAL_PARSER(ctx, LY_VCODE_INVAL, arg_len, stmt->arg, ly_stmt2str(val_kw)); |
| 404 | goto error; |
| 405 | } |
| 406 | if (errno == ERANGE) { |
| 407 | LOGVAL_PARSER(ctx, LY_VCODE_OOB, arg_len, stmt->arg, ly_stmt2str(val_kw)); |
| 408 | goto error; |
| 409 | } |
| 410 | if (val_kw == LY_STMT_VALUE) { |
| 411 | *value = num; |
| 412 | } else { |
| 413 | *value = unum; |
| 414 | } |
| 415 | |
| 416 | for (child = stmt->child; child; child = child->next) { |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 417 | struct ly_in *in; |
| 418 | LY_CHECK_RET(ly_in_new_memory(child->stmt, &in)); |
| 419 | enum ly_stmt kw = lysp_match_kw(NULL, in); |
| 420 | ly_in_free(in, 0); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 421 | |
| 422 | switch (kw) { |
| 423 | case LY_STMT_EXTENSION_INSTANCE: |
| 424 | LY_CHECK_RET(lysp_stmt_ext(ctx, child, val_kw == LY_STMT_VALUE ? LYEXT_SUBSTMT_VALUE : LYEXT_SUBSTMT_POSITION, 0, exts)); |
| 425 | break; |
| 426 | default: |
| 427 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(val_kw)); |
| 428 | return LY_EVALID; |
| 429 | } |
| 430 | } |
| 431 | return LY_SUCCESS; |
| 432 | |
| 433 | error: |
| 434 | return LY_EVALID; |
| 435 | } |
| 436 | |
| 437 | /** |
| 438 | * @brief Parse the enum or bit statement. Substatement of type statement. |
| 439 | * |
| 440 | * @param[in] ctx yang parser context for logging. |
| 441 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 442 | * @param[in] enum_kw Type of this particular keyword. |
| 443 | * @param[in,out] enums Enums or bits to add to. |
| 444 | * |
| 445 | * @return LY_ERR values. |
| 446 | */ |
| 447 | static LY_ERR |
| 448 | lysp_stmt_type_enum(struct lys_parser_ctx *ctx, const struct lysp_stmt *stmt, enum ly_stmt enum_kw, struct lysp_type_enum **enums) |
| 449 | { |
| 450 | struct lysp_type_enum *enm; |
| 451 | const struct lysp_stmt *child; |
| 452 | |
| 453 | LY_CHECK_RET(lysp_stmt_validate_value(ctx, enum_kw == LY_STMT_ENUM ? Y_STR_ARG : Y_IDENTIF_ARG, stmt->arg)); |
| 454 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 455 | LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *enums, enm, LY_EMEM); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 456 | |
| 457 | if (enum_kw == LY_STMT_ENUM) { |
| 458 | LY_CHECK_RET(lysp_check_enum_name(ctx, stmt->arg, strlen(stmt->arg))); |
| 459 | } /* else nothing specific for YANG_BIT */ |
| 460 | |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 461 | LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), stmt->arg, 0, &enm->name)); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 462 | CHECK_UNIQUENESS(ctx, *enums, name, ly_stmt2str(enum_kw), enm->name); |
| 463 | |
| 464 | for (child = stmt->child; child; child = child->next) { |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 465 | struct ly_in *in; |
| 466 | LY_CHECK_RET(ly_in_new_memory(child->stmt, &in)); |
| 467 | enum ly_stmt kw = lysp_match_kw(NULL, in); |
| 468 | ly_in_free(in, 0); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 469 | |
| 470 | switch (kw) { |
| 471 | case LY_STMT_DESCRIPTION: |
| 472 | LY_CHECK_RET(lysp_stmt_text_field(ctx, child, LYEXT_SUBSTMT_DESCRIPTION, 0, &enm->dsc, Y_STR_ARG, &enm->exts)); |
| 473 | break; |
| 474 | case LY_STMT_IF_FEATURE: |
| 475 | PARSER_CHECK_STMTVER2_RET(ctx, "if-feature", ly_stmt2str(enum_kw)); |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 476 | LY_CHECK_RET(lysp_stmt_qnames(ctx, child, LYEXT_SUBSTMT_IFFEATURE, &enm->iffeatures, Y_STR_ARG, &enm->exts)); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 477 | break; |
| 478 | case LY_STMT_REFERENCE: |
| 479 | LY_CHECK_RET(lysp_stmt_text_field(ctx, child, LYEXT_SUBSTMT_REFERENCE, 0, &enm->ref, Y_STR_ARG, &enm->exts)); |
| 480 | break; |
| 481 | case LY_STMT_STATUS: |
| 482 | LY_CHECK_RET(lysp_stmt_status(ctx, child, &enm->flags, &enm->exts)); |
| 483 | break; |
| 484 | case LY_STMT_VALUE: |
| 485 | LY_CHECK_ERR_RET(enum_kw == LY_STMT_BIT, LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 486 | ly_stmt2str(enum_kw)), LY_EVALID); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 487 | LY_CHECK_RET(lysp_stmt_type_enum_value_pos(ctx, child, kw, &enm->value, &enm->flags, &enm->exts)); |
| 488 | break; |
| 489 | case LY_STMT_POSITION: |
| 490 | LY_CHECK_ERR_RET(enum_kw == LY_STMT_ENUM, LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 491 | ly_stmt2str(enum_kw)), LY_EVALID); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 492 | LY_CHECK_RET(lysp_stmt_type_enum_value_pos(ctx, child, kw, &enm->value, &enm->flags, &enm->exts)); |
| 493 | break; |
| 494 | case LY_STMT_EXTENSION_INSTANCE: |
| 495 | LY_CHECK_RET(lysp_stmt_ext(ctx, child, LYEXT_SUBSTMT_SELF, 0, &enm->exts)); |
| 496 | break; |
| 497 | default: |
| 498 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(enum_kw)); |
| 499 | return LY_EVALID; |
| 500 | } |
| 501 | } |
| 502 | return LY_SUCCESS; |
| 503 | } |
| 504 | |
| 505 | /** |
| 506 | * @brief Parse the fraction-digits statement. Substatement of type statement. |
| 507 | * |
| 508 | * @param[in] ctx yang parser context for logging. |
| 509 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 510 | * @param[in,out] fracdig Value to write to. |
| 511 | * @param[in,out] exts Extension instances to add to. |
| 512 | * |
| 513 | * @return LY_ERR values. |
| 514 | */ |
| 515 | static LY_ERR |
| 516 | lysp_stmt_type_fracdigits(struct lys_parser_ctx *ctx, const struct lysp_stmt *stmt, uint8_t *fracdig, struct lysp_ext_instance **exts) |
| 517 | { |
| 518 | char *ptr; |
| 519 | size_t arg_len; |
| 520 | unsigned long int num; |
| 521 | const struct lysp_stmt *child; |
| 522 | |
| 523 | if (*fracdig) { |
| 524 | LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "fraction-digits"); |
| 525 | return LY_EVALID; |
| 526 | } |
| 527 | |
| 528 | LY_CHECK_RET(lysp_stmt_validate_value(ctx, Y_STR_ARG, stmt->arg)); |
| 529 | arg_len = strlen(stmt->arg); |
| 530 | if (!arg_len || (stmt->arg[0] == '0') || !isdigit(stmt->arg[0])) { |
| 531 | LOGVAL_PARSER(ctx, LY_VCODE_INVAL, arg_len, stmt->arg, "fraction-digits"); |
| 532 | return LY_EVALID; |
| 533 | } |
| 534 | |
| 535 | errno = 0; |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame^] | 536 | num = strtoul(stmt->arg, &ptr, LY_BASE_DEC); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 537 | /* we have not parsed the whole argument */ |
| 538 | if ((size_t)(ptr - stmt->arg) != arg_len) { |
| 539 | LOGVAL_PARSER(ctx, LY_VCODE_INVAL, arg_len, stmt->arg, "fraction-digits"); |
| 540 | return LY_EVALID; |
| 541 | } |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame^] | 542 | if ((errno == ERANGE) || (num > LY_TYPE_DEC64_FD_MAX)) { |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 543 | LOGVAL_PARSER(ctx, LY_VCODE_OOB, arg_len, stmt->arg, "fraction-digits"); |
| 544 | return LY_EVALID; |
| 545 | } |
| 546 | *fracdig = num; |
| 547 | |
| 548 | for (child = stmt->child; child; child = child->next) { |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 549 | struct ly_in *in; |
| 550 | LY_CHECK_RET(ly_in_new_memory(child->stmt, &in)); |
| 551 | enum ly_stmt kw = lysp_match_kw(NULL, in); |
| 552 | ly_in_free(in, 0); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 553 | |
| 554 | switch (kw) { |
| 555 | case LY_STMT_EXTENSION_INSTANCE: |
| 556 | LY_CHECK_RET(lysp_stmt_ext(ctx, child, LYEXT_SUBSTMT_FRACDIGITS, 0, exts)); |
| 557 | break; |
| 558 | default: |
| 559 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "fraction-digits"); |
| 560 | return LY_EVALID; |
| 561 | } |
| 562 | } |
| 563 | return LY_SUCCESS; |
| 564 | } |
| 565 | |
| 566 | /** |
| 567 | * @brief Parse the require-instance statement. Substatement of type statement. |
| 568 | * |
| 569 | * @param[in] ctx yang parser context for logging. |
| 570 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 571 | * @param[in,out] reqinst Value to write to. |
| 572 | * @param[in,out] flags Flags to write to. |
| 573 | * @param[in,out] exts Extension instances to add to. |
| 574 | * |
| 575 | * @return LY_ERR values. |
| 576 | */ |
| 577 | static LY_ERR |
| 578 | 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] | 579 | struct lysp_ext_instance **exts) |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 580 | { |
| 581 | size_t arg_len; |
| 582 | struct lysp_stmt *child; |
| 583 | |
| 584 | if (*flags & LYS_SET_REQINST) { |
| 585 | LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "require-instance"); |
| 586 | return LY_EVALID; |
| 587 | } |
| 588 | *flags |= LYS_SET_REQINST; |
| 589 | |
| 590 | LY_CHECK_RET(lysp_stmt_validate_value(ctx, Y_STR_ARG, stmt->arg)); |
| 591 | arg_len = strlen(stmt->arg); |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame^] | 592 | 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] | 593 | *reqinst = 1; |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame^] | 594 | } 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] | 595 | LOGVAL_PARSER(ctx, LY_VCODE_INVAL, arg_len, stmt->arg, "require-instance"); |
| 596 | return LY_EVALID; |
| 597 | } |
| 598 | |
| 599 | for (child = stmt->child; child; child = child->next) { |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 600 | struct ly_in *in; |
| 601 | LY_CHECK_RET(ly_in_new_memory(child->stmt, &in)); |
| 602 | enum ly_stmt kw = lysp_match_kw(NULL, in); |
| 603 | ly_in_free(in, 0); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 604 | |
| 605 | switch (kw) { |
| 606 | case LY_STMT_EXTENSION_INSTANCE: |
| 607 | LY_CHECK_RET(lysp_stmt_ext(ctx, child, LYEXT_SUBSTMT_REQINSTANCE, 0, exts)); |
| 608 | break; |
| 609 | default: |
| 610 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "require-instance"); |
| 611 | return LY_EVALID; |
| 612 | } |
| 613 | } |
| 614 | return LY_SUCCESS; |
| 615 | } |
| 616 | |
| 617 | /** |
| 618 | * @brief Parse the modifier statement. Substatement of type pattern statement. |
| 619 | * |
| 620 | * @param[in] ctx yang parser context for logging. |
| 621 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 622 | * @param[in,out] pat Value to write to. |
| 623 | * @param[in,out] exts Extension instances to add to. |
| 624 | * |
| 625 | * @return LY_ERR values. |
| 626 | */ |
| 627 | static LY_ERR |
| 628 | lysp_stmt_type_pattern_modifier(struct lys_parser_ctx *ctx, const struct lysp_stmt *stmt, const char **pat, struct lysp_ext_instance **exts) |
| 629 | { |
| 630 | size_t arg_len; |
| 631 | char *buf; |
| 632 | const struct lysp_stmt *child; |
| 633 | |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame^] | 634 | if ((*pat)[0] == LYSP_RESTR_PATTERN_NACK) { |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 635 | LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "modifier"); |
| 636 | return LY_EVALID; |
| 637 | } |
| 638 | |
| 639 | LY_CHECK_RET(lysp_stmt_validate_value(ctx, Y_STR_ARG, stmt->arg)); |
| 640 | arg_len = strlen(stmt->arg); |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame^] | 641 | 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] | 642 | LOGVAL_PARSER(ctx, LY_VCODE_INVAL, arg_len, stmt->arg, "modifier"); |
| 643 | return LY_EVALID; |
| 644 | } |
| 645 | |
| 646 | /* replace the value in the dictionary */ |
| 647 | buf = malloc(strlen(*pat) + 1); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 648 | LY_CHECK_ERR_RET(!buf, LOGMEM(PARSER_CTX(ctx)), LY_EMEM); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 649 | strcpy(buf, *pat); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 650 | lydict_remove(PARSER_CTX(ctx), *pat); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 651 | |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame^] | 652 | assert(buf[0] == LYSP_RESTR_PATTERN_ACK); |
| 653 | buf[0] = LYSP_RESTR_PATTERN_NACK; |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 654 | LY_CHECK_RET(lydict_insert_zc(PARSER_CTX(ctx), buf, pat)); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 655 | |
| 656 | for (child = stmt->child; child; child = child->next) { |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 657 | struct ly_in *in; |
| 658 | LY_CHECK_RET(ly_in_new_memory(child->stmt, &in)); |
| 659 | enum ly_stmt kw = lysp_match_kw(NULL, in); |
| 660 | ly_in_free(in, 0); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 661 | |
| 662 | switch (kw) { |
| 663 | case LY_STMT_EXTENSION_INSTANCE: |
| 664 | LY_CHECK_RET(lysp_stmt_ext(ctx, child, LYEXT_SUBSTMT_MODIFIER, 0, exts)); |
| 665 | break; |
| 666 | default: |
| 667 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "modifier"); |
| 668 | return LY_EVALID; |
| 669 | } |
| 670 | } |
| 671 | return LY_SUCCESS; |
| 672 | } |
| 673 | |
| 674 | /** |
| 675 | * @brief Parse the pattern statement. Substatement of type statement. |
| 676 | * |
| 677 | * @param[in] ctx yang parser context for logging. |
| 678 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 679 | * @param[in,out] patterns Restrictions to add to. |
| 680 | * |
| 681 | * @return LY_ERR values. |
| 682 | */ |
| 683 | static LY_ERR |
| 684 | lysp_stmt_type_pattern(struct lys_parser_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_restr **patterns) |
| 685 | { |
| 686 | char *buf; |
| 687 | size_t arg_len; |
| 688 | const struct lysp_stmt *child; |
| 689 | struct lysp_restr *restr; |
| 690 | |
| 691 | 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] | 692 | LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *patterns, restr, LY_EMEM); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 693 | arg_len = strlen(stmt->arg); |
| 694 | |
| 695 | /* add special meaning first byte */ |
| 696 | buf = malloc(arg_len + 2); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 697 | LY_CHECK_ERR_RET(!buf, LOGMEM(PARSER_CTX(ctx)), LY_EMEM); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 698 | memmove(buf + 1, stmt->arg, arg_len); |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame^] | 699 | buf[0] = LYSP_RESTR_PATTERN_ACK; /* pattern's default regular-match flag */ |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 700 | buf[arg_len + 1] = '\0'; /* terminating NULL byte */ |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 701 | 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] | 702 | restr->arg.mod = ctx->parsed_mod; |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 703 | |
| 704 | for (child = stmt->child; child; child = child->next) { |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 705 | struct ly_in *in; |
| 706 | LY_CHECK_RET(ly_in_new_memory(child->stmt, &in)); |
| 707 | enum ly_stmt kw = lysp_match_kw(NULL, in); |
| 708 | ly_in_free(in, 0); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 709 | |
| 710 | switch (kw) { |
| 711 | case LY_STMT_DESCRIPTION: |
| 712 | LY_CHECK_RET(lysp_stmt_text_field(ctx, child, LYEXT_SUBSTMT_DESCRIPTION, 0, &restr->dsc, Y_STR_ARG, &restr->exts)); |
| 713 | break; |
| 714 | case LY_STMT_REFERENCE: |
| 715 | LY_CHECK_RET(lysp_stmt_text_field(ctx, child, LYEXT_SUBSTMT_REFERENCE, 0, &restr->ref, Y_STR_ARG, &restr->exts)); |
| 716 | break; |
| 717 | case LY_STMT_ERROR_APP_TAG: |
| 718 | LY_CHECK_RET(lysp_stmt_text_field(ctx, child, LYEXT_SUBSTMT_ERRTAG, 0, &restr->eapptag, Y_STR_ARG, &restr->exts)); |
| 719 | break; |
| 720 | case LY_STMT_ERROR_MESSAGE: |
| 721 | LY_CHECK_RET(lysp_stmt_text_field(ctx, child, LYEXT_SUBSTMT_ERRMSG, 0, &restr->emsg, Y_STR_ARG, &restr->exts)); |
| 722 | break; |
| 723 | case LY_STMT_MODIFIER: |
| 724 | PARSER_CHECK_STMTVER2_RET(ctx, "modifier", "pattern"); |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 725 | 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] | 726 | break; |
| 727 | case LY_STMT_EXTENSION_INSTANCE: |
| 728 | LY_CHECK_RET(lysp_stmt_ext(ctx, child, LYEXT_SUBSTMT_SELF, 0, &restr->exts)); |
| 729 | break; |
| 730 | default: |
| 731 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "pattern"); |
| 732 | return LY_EVALID; |
| 733 | } |
| 734 | } |
| 735 | return LY_SUCCESS; |
| 736 | } |
| 737 | |
| 738 | /** |
| 739 | * @brief Parse the type statement. |
| 740 | * |
| 741 | * @param[in] ctx yang parser context for logging. |
| 742 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 743 | * @param[in,out] type Type to wrote to. |
| 744 | * |
| 745 | * @return LY_ERR values. |
| 746 | */ |
| 747 | static LY_ERR |
| 748 | lysp_stmt_type(struct lys_parser_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_type *type) |
| 749 | { |
| 750 | struct lysp_type *nest_type; |
| 751 | const struct lysp_stmt *child; |
Radek Krejci | b124784 | 2020-07-02 16:22:38 +0200 | [diff] [blame] | 752 | const char *str_path = NULL; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 753 | LY_ERR ret; |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 754 | |
| 755 | if (type->name) { |
| 756 | LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "type"); |
| 757 | return LY_EVALID; |
| 758 | } |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 759 | 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] | 760 | type->pmod = ctx->parsed_mod; |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 761 | |
| 762 | for (child = stmt->child; child; child = child->next) { |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 763 | struct ly_in *in; |
| 764 | LY_CHECK_RET(ly_in_new_memory(child->stmt, &in)); |
| 765 | enum ly_stmt kw = lysp_match_kw(NULL, in); |
| 766 | ly_in_free(in, 0); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 767 | |
| 768 | switch (kw) { |
| 769 | case LY_STMT_BASE: |
| 770 | LY_CHECK_RET(lysp_stmt_text_fields(ctx, child, LYEXT_SUBSTMT_BASE, &type->bases, Y_PREF_IDENTIF_ARG, &type->exts)); |
| 771 | type->flags |= LYS_SET_BASE; |
| 772 | break; |
| 773 | case LY_STMT_BIT: |
| 774 | LY_CHECK_RET(lysp_stmt_type_enum(ctx, child, kw, &type->bits)); |
| 775 | type->flags |= LYS_SET_BIT; |
| 776 | break; |
| 777 | case LY_STMT_ENUM: |
| 778 | LY_CHECK_RET(lysp_stmt_type_enum(ctx, child, kw, &type->enums)); |
| 779 | type->flags |= LYS_SET_ENUM; |
| 780 | break; |
| 781 | case LY_STMT_FRACTION_DIGITS: |
| 782 | LY_CHECK_RET(lysp_stmt_type_fracdigits(ctx, child, &type->fraction_digits, &type->exts)); |
| 783 | type->flags |= LYS_SET_FRDIGITS; |
| 784 | break; |
| 785 | case LY_STMT_LENGTH: |
| 786 | if (type->length) { |
| 787 | LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw)); |
| 788 | return LY_EVALID; |
| 789 | } |
| 790 | type->length = calloc(1, sizeof *type->length); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 791 | LY_CHECK_ERR_RET(!type->length, LOGMEM(PARSER_CTX(ctx)), LY_EMEM); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 792 | |
| 793 | LY_CHECK_RET(lysp_stmt_restr(ctx, child, kw, type->length)); |
| 794 | type->flags |= LYS_SET_LENGTH; |
| 795 | break; |
| 796 | case LY_STMT_PATH: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 797 | LY_CHECK_RET(lysp_stmt_text_field(ctx, child, LYEXT_SUBSTMT_PATH, 0, &str_path, Y_STR_ARG, &type->exts)); |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 798 | ret = ly_path_parse(PARSER_CTX(ctx), NULL, str_path, 0, LY_PATH_BEGIN_EITHER, LY_PATH_LREF_TRUE, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 799 | LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_LEAFREF, &type->path); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 800 | lydict_remove(PARSER_CTX(ctx), str_path); |
| 801 | LY_CHECK_RET(ret); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 802 | type->flags |= LYS_SET_PATH; |
| 803 | break; |
| 804 | case LY_STMT_PATTERN: |
| 805 | LY_CHECK_RET(lysp_stmt_type_pattern(ctx, child, &type->patterns)); |
| 806 | type->flags |= LYS_SET_PATTERN; |
| 807 | break; |
| 808 | case LY_STMT_RANGE: |
| 809 | if (type->range) { |
| 810 | LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw)); |
| 811 | return LY_EVALID; |
| 812 | } |
| 813 | type->range = calloc(1, sizeof *type->range); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 814 | LY_CHECK_ERR_RET(!type->range, LOGMEM(PARSER_CTX(ctx)), LY_EMEM); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 815 | |
| 816 | LY_CHECK_RET(lysp_stmt_restr(ctx, child, kw, type->range)); |
| 817 | type->flags |= LYS_SET_RANGE; |
| 818 | break; |
| 819 | case LY_STMT_REQUIRE_INSTANCE: |
| 820 | LY_CHECK_RET(lysp_stmt_type_reqinstance(ctx, child, &type->require_instance, &type->flags, &type->exts)); |
| 821 | /* LYS_SET_REQINST checked and set inside parse_type_reqinstance() */ |
| 822 | break; |
| 823 | case LY_STMT_TYPE: |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 824 | 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] | 825 | LY_CHECK_RET(lysp_stmt_type(ctx, child, nest_type)); |
| 826 | type->flags |= LYS_SET_TYPE; |
| 827 | break; |
| 828 | case LY_STMT_EXTENSION_INSTANCE: |
| 829 | LY_CHECK_RET(lysp_stmt_ext(ctx, child, LYEXT_SUBSTMT_SELF, 0, &type->exts)); |
| 830 | break; |
| 831 | default: |
| 832 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "type"); |
| 833 | return LY_EVALID; |
| 834 | } |
| 835 | } |
| 836 | return LY_SUCCESS; |
| 837 | } |
| 838 | |
| 839 | LY_ERR |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 840 | lysp_stmt_parse(struct lysc_ctx *ctx, const struct lysp_stmt *stmt, enum ly_stmt kw, void **result, struct lysp_ext_instance **exts) |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 841 | { |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 842 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 843 | struct lys_yang_parser_ctx pctx = {0}; |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 844 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 845 | pctx.format = LYS_IN_YANG; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 846 | pctx.parsed_mod = ctx->pmod; |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 847 | pctx.pos_type = LY_VLOG_STR; |
| 848 | pctx.path = ctx->path; |
| 849 | |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 850 | switch (kw) { |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 851 | case LY_STMT_STATUS: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 852 | ret = lysp_stmt_status((struct lys_parser_ctx *)&pctx, stmt, *(uint16_t **)result, exts); |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 853 | break; |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 854 | case LY_STMT_TYPE: { |
| 855 | struct lysp_type *type; |
| 856 | type = calloc(1, sizeof *type); |
| 857 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 858 | ret = lysp_stmt_type((struct lys_parser_ctx *)&pctx, stmt, type); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 859 | (*result) = type; |
| 860 | break; |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 861 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 862 | default: |
| 863 | LOGINT(ctx->ctx); |
| 864 | return LY_EINT; |
| 865 | } |
| 866 | |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 867 | return ret; |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 868 | } |