Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @file parser_yang.c |
| 3 | * @author Michal Vasko <mvasko@cesnet.cz> |
| 4 | * @brief YANG parser |
| 5 | * |
| 6 | * Copyright (c) 2018 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 | */ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 14 | #include "parser_internal.h" |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 15 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 16 | #include <assert.h> |
| 17 | #include <ctype.h> |
| 18 | #include <errno.h> |
| 19 | #include <stdint.h> |
| 20 | #include <stdio.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <string.h> |
| 23 | |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 24 | #include "common.h" |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 25 | #include "context.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 26 | #include "dict.h" |
Michal Vasko | afac782 | 2020-10-20 14:22:26 +0200 | [diff] [blame] | 27 | #include "in_internal.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 28 | #include "log.h" |
Radek Krejci | ca376bd | 2020-06-11 16:04:06 +0200 | [diff] [blame] | 29 | #include "parser_schema.h" |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 30 | #include "path.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 31 | #include "set.h" |
| 32 | #include "tree.h" |
| 33 | #include "tree_schema.h" |
Radek Krejci | 70853c5 | 2018-10-15 14:46:16 +0200 | [diff] [blame] | 34 | #include "tree_schema_internal.h" |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 35 | |
Radek Krejci | ceaf212 | 2019-01-02 15:03:26 +0100 | [diff] [blame] | 36 | /** |
| 37 | * @brief Insert WORD into the libyang context's dictionary and store as TARGET. |
| 38 | * @param[in] CTX yang parser context to access libyang context. |
| 39 | * @param[in] BUF buffer in case the word is not a constant and can be inserted directly (zero-copy) |
| 40 | * @param[out] TARGET variable where to store the pointer to the inserted value. |
| 41 | * @param[in] WORD string to store. |
| 42 | * @param[in] LEN length of the string in WORD to store. |
| 43 | */ |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 44 | #define INSERT_WORD_RET(CTX, BUF, TARGET, WORD, LEN) \ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 45 | if (BUF) {LY_CHECK_RET(lydict_insert_zc(PARSER_CTX(CTX), WORD, &(TARGET)));}\ |
| 46 | else {LY_CHECK_RET(lydict_insert(PARSER_CTX(CTX), LEN ? WORD : "", LEN, &(TARGET)));} |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 47 | |
Radek Krejci | ceaf212 | 2019-01-02 15:03:26 +0100 | [diff] [blame] | 48 | /** |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 49 | * @brief Read from the IN structure COUNT items. Also updates the indent value in yang parser context |
Radek Krejci | ceaf212 | 2019-01-02 15:03:26 +0100 | [diff] [blame] | 50 | * @param[in] CTX yang parser context to update its indent value. |
Radek Krejci | ceaf212 | 2019-01-02 15:03:26 +0100 | [diff] [blame] | 51 | * @param[in] COUNT number of items for which the DATA pointer is supposed to move on. |
| 52 | */ |
Radek Krejci | d54412f | 2020-12-17 20:25:35 +0100 | [diff] [blame] | 53 | #define MOVE_INPUT(CTX, COUNT) ly_in_skip((CTX)->in, COUNT);(CTX)->indent+=COUNT |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 54 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 55 | /** |
| 56 | * @brief Loop through all substatements providing, return if there are none. |
| 57 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 58 | * @param[in] CTX yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 59 | * @param[out] KW YANG keyword read. |
| 60 | * @param[out] WORD Pointer to the keyword itself. |
| 61 | * @param[out] WORD_LEN Length of the keyword. |
| 62 | * @param[out] ERR Variable for error storing. |
| 63 | * |
| 64 | * @return In case there are no substatements or a fatal error encountered. |
| 65 | */ |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 66 | #define YANG_READ_SUBSTMT_FOR(CTX, KW, WORD, WORD_LEN, ERR, CHECKGOTO) \ |
| 67 | LY_CHECK_RET(get_keyword(CTX, &KW, &WORD, &WORD_LEN)); \ |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 68 | if (KW == LY_STMT_SYNTAX_SEMICOLON) { \ |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 69 | CHECKGOTO; \ |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 70 | return LY_SUCCESS; \ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 71 | } \ |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 72 | if (KW != LY_STMT_SYNTAX_LEFT_BRACE) { \ |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 73 | LOGVAL_PARSER(CTX, LYVE_SYNTAX_YANG, "Invalid keyword \"%s\", expected \";\" or \"{\".", ly_stmt2str(KW)); \ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 74 | return LY_EVALID; \ |
| 75 | } \ |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 76 | for (ERR = get_keyword(CTX, &KW, &WORD, &WORD_LEN); \ |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 77 | !ERR && (KW != LY_STMT_SYNTAX_RIGHT_BRACE); \ |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 78 | ERR = get_keyword(CTX, &KW, &WORD, &WORD_LEN)) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 79 | |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 80 | LY_ERR parse_container(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 81 | struct lysp_node **siblings); |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 82 | LY_ERR parse_uses(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings); |
| 83 | LY_ERR parse_choice(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings); |
| 84 | LY_ERR parse_case(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings); |
| 85 | LY_ERR parse_list(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings); |
| 86 | LY_ERR parse_grouping(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_grp **groupings); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 87 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 88 | /** |
| 89 | * @brief Add another character to dynamic buffer, a low-level function. |
| 90 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 91 | * Enlarge if needed. Updates \p input as well as \p buf_used. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 92 | * |
Radek Krejci | 404251e | 2018-10-09 12:06:44 +0200 | [diff] [blame] | 93 | * @param[in] ctx libyang context for logging. |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 94 | * @param[in,out] in Input structure. |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 95 | * @param[in] len Number of bytes to get from the input string and copy into the buffer. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 96 | * @param[in,out] buf Buffer to use, can be moved by realloc(). |
| 97 | * @param[in,out] buf_len Current size of the buffer. |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 98 | * @param[in,out] buf_used Currently used characters of the buffer. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 99 | * |
| 100 | * @return LY_ERR values. |
| 101 | */ |
Radek Krejci | 2d7a47b | 2019-05-16 13:34:10 +0200 | [diff] [blame] | 102 | LY_ERR |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 103 | buf_add_char(struct ly_ctx *ctx, struct ly_in *in, size_t len, char **buf, size_t *buf_len, size_t *buf_used) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 104 | { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 105 | #define BUF_STEP 16; |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 106 | if (*buf_len <= (*buf_used) + len) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 107 | *buf_len += BUF_STEP; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 108 | *buf = ly_realloc(*buf, *buf_len); |
| 109 | LY_CHECK_ERR_RET(!*buf, LOGMEM(ctx), LY_EMEM); |
| 110 | } |
Radek Krejci | c091739 | 2019-04-10 13:04:04 +0200 | [diff] [blame] | 111 | if (*buf_used) { |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 112 | ly_in_read(in, &(*buf)[*buf_used], len); |
Radek Krejci | c091739 | 2019-04-10 13:04:04 +0200 | [diff] [blame] | 113 | } else { |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 114 | ly_in_read(in, *buf, len); |
Radek Krejci | c091739 | 2019-04-10 13:04:04 +0200 | [diff] [blame] | 115 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 116 | |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 117 | (*buf_used) += len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 118 | return LY_SUCCESS; |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 119 | #undef BUF_STEP |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 120 | } |
| 121 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 122 | /** |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 123 | * @brief Store a single UTF8 character. It depends whether in a dynamically-allocated buffer or just as a pointer to the data. |
| 124 | * |
| 125 | * @param[in] ctx yang parser context for logging. |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 126 | * @param[in] arg Type of the input string to select method of checking character validity. |
| 127 | * @param[in,out] word_p Word pointer. If buffer (\p word_b) was not yet needed, it is just a pointer to the first |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 128 | * stored character. If buffer was needed (\p word_b is non-NULL or \p need_buf is set), it is pointing to the buffer. |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 129 | * @param[in,out] word_len Current length of the word pointed to by \p word_p. |
| 130 | * @param[in,out] word_b Word buffer. Is kept NULL as long as it is not requested (word is a substring of the data). |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 131 | * @param[in,out] buf_len Current length of \p word_b. |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 132 | * @param[in] need_buf Flag if the dynamically allocated buffer is required. |
David Sedlák | 40bb13b | 2019-07-10 14:34:18 +0200 | [diff] [blame] | 133 | * @param[in,out] prefix Storage for internally used flag in case of possible prefixed identifiers: |
| 134 | * 0 - colon not yet found (no prefix) |
| 135 | * 1 - \p c is the colon character |
| 136 | * 2 - prefix already processed, now processing the identifier |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 137 | * |
| 138 | * @return LY_ERR values. |
| 139 | */ |
Radek Krejci | 2d7a47b | 2019-05-16 13:34:10 +0200 | [diff] [blame] | 140 | LY_ERR |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 141 | buf_store_char(struct lys_yang_parser_ctx *ctx, enum yang_arg arg, char **word_p, size_t *word_len, |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 142 | char **word_b, size_t *buf_len, ly_bool need_buf, uint8_t *prefix) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 143 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 144 | uint32_t c; |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 145 | size_t len; |
| 146 | |
Radek Krejci | f29b7c3 | 2019-04-09 16:17:49 +0200 | [diff] [blame] | 147 | /* check valid combination of input paremeters - if need_buf specified, word_b must be provided */ |
| 148 | assert(!need_buf || (need_buf && word_b)); |
| 149 | |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 150 | /* get UTF8 code point (and number of bytes coding the character) */ |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 151 | LY_CHECK_ERR_RET(ly_getutf8(&ctx->in->current, &c, &len), |
| 152 | LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, ctx->in->current[-len]), LY_EVALID); |
| 153 | ctx->in->current -= len; |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 154 | if (c == '\n') { |
| 155 | ctx->indent = 0; |
Radek Krejci | dd713ce | 2021-01-04 23:12:12 +0100 | [diff] [blame] | 156 | LY_IN_NEW_LINE(ctx->in); |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 157 | } else { |
| 158 | /* note - even the multibyte character is count as 1 */ |
| 159 | ++ctx->indent; |
| 160 | } |
| 161 | |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 162 | /* check character validity */ |
| 163 | switch (arg) { |
| 164 | case Y_IDENTIF_ARG: |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 165 | LY_CHECK_RET(lysp_check_identifierchar((struct lys_parser_ctx *)ctx, c, !(*word_len), NULL)); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 166 | break; |
| 167 | case Y_PREF_IDENTIF_ARG: |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 168 | LY_CHECK_RET(lysp_check_identifierchar((struct lys_parser_ctx *)ctx, c, !(*word_len), prefix)); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 169 | break; |
| 170 | case Y_STR_ARG: |
| 171 | case Y_MAYBE_STR_ARG: |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 172 | LY_CHECK_RET(lysp_check_stringchar((struct lys_parser_ctx *)ctx, c)); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 173 | break; |
| 174 | } |
| 175 | |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 176 | if (word_b && *word_b) { |
| 177 | /* add another character into buffer */ |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 178 | if (buf_add_char(PARSER_CTX(ctx), ctx->in, len, word_b, buf_len, word_len)) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 179 | return LY_EMEM; |
| 180 | } |
| 181 | |
| 182 | /* in case of realloc */ |
| 183 | *word_p = *word_b; |
Radek Krejci | f29b7c3 | 2019-04-09 16:17:49 +0200 | [diff] [blame] | 184 | } else if (word_b && need_buf) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 185 | /* first time we need a buffer, copy everything read up to now */ |
| 186 | if (*word_len) { |
| 187 | *word_b = malloc(*word_len); |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 188 | LY_CHECK_ERR_RET(!*word_b, LOGMEM(PARSER_CTX(ctx)), LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 189 | *buf_len = *word_len; |
| 190 | memcpy(*word_b, *word_p, *word_len); |
| 191 | } |
| 192 | |
| 193 | /* add this new character into buffer */ |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 194 | if (buf_add_char(PARSER_CTX(ctx), ctx->in, len, word_b, buf_len, word_len)) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 195 | return LY_EMEM; |
| 196 | } |
| 197 | |
| 198 | /* in case of realloc */ |
| 199 | *word_p = *word_b; |
| 200 | } else { |
| 201 | /* just remember the first character pointer */ |
| 202 | if (!*word_p) { |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 203 | *word_p = (char *)ctx->in->current; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 204 | } |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 205 | /* ... and update the word's length */ |
| 206 | (*word_len) += len; |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 207 | ly_in_skip(ctx->in, len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | return LY_SUCCESS; |
| 211 | } |
| 212 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 213 | /** |
| 214 | * @brief Skip YANG comment in data. |
| 215 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 216 | * @param[in] ctx yang parser context for logging. |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 217 | * @param[in] comment Type of the comment to process: |
| 218 | * 1 for a one-line comment, |
| 219 | * 2 for a block comment. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 220 | * @return LY_ERR values. |
| 221 | */ |
Radek Krejci | 2d7a47b | 2019-05-16 13:34:10 +0200 | [diff] [blame] | 222 | LY_ERR |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 223 | skip_comment(struct lys_yang_parser_ctx *ctx, uint8_t comment) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 224 | { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 225 | /* internal statuses: */ |
| 226 | #define COMMENT_NO 0 /* comment ended */ |
| 227 | #define COMMENT_LINE 1 /* in line comment */ |
| 228 | #define COMMENT_BLOCK 2 /* in block comment */ |
| 229 | #define COMMENT_BLOCK_END 3 /* in block comment with last read character '*' */ |
| 230 | |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 231 | while (ctx->in->current[0] && comment) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 232 | switch (comment) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 233 | case COMMENT_LINE: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 234 | if (ctx->in->current[0] == '\n') { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 235 | comment = COMMENT_NO; |
Radek Krejci | d54412f | 2020-12-17 20:25:35 +0100 | [diff] [blame] | 236 | LY_IN_NEW_LINE(ctx->in); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 237 | } |
| 238 | break; |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 239 | case COMMENT_BLOCK: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 240 | if (ctx->in->current[0] == '*') { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 241 | comment = COMMENT_BLOCK_END; |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 242 | } else if (ctx->in->current[0] == '\n') { |
Radek Krejci | d54412f | 2020-12-17 20:25:35 +0100 | [diff] [blame] | 243 | LY_IN_NEW_LINE(ctx->in); |
Radek Krejci | 15c80ca | 2018-10-09 11:01:31 +0200 | [diff] [blame] | 244 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 245 | break; |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 246 | case COMMENT_BLOCK_END: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 247 | if (ctx->in->current[0] == '/') { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 248 | comment = COMMENT_NO; |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 249 | } else if (ctx->in->current[0] != '*') { |
| 250 | if (ctx->in->current[0] == '\n') { |
Radek Krejci | d54412f | 2020-12-17 20:25:35 +0100 | [diff] [blame] | 251 | LY_IN_NEW_LINE(ctx->in); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 252 | } |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 253 | comment = COMMENT_BLOCK; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 254 | } |
| 255 | break; |
| 256 | default: |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 257 | LOGINT_RET(PARSER_CTX(ctx)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 258 | } |
| 259 | |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 260 | if (ctx->in->current[0] == '\n') { |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 261 | ctx->indent = 0; |
| 262 | } else { |
| 263 | ++ctx->indent; |
| 264 | } |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 265 | ++ctx->in->current; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 266 | } |
| 267 | |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 268 | if (!ctx->in->current[0] && (comment >= COMMENT_BLOCK)) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 269 | LOGVAL_PARSER(ctx, LYVE_SYNTAX, "Unexpected end-of-input, non-terminated comment."); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 270 | return LY_EVALID; |
| 271 | } |
| 272 | |
| 273 | return LY_SUCCESS; |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 274 | |
| 275 | #undef COMMENT_NO |
| 276 | #undef COMMENT_LINE |
| 277 | #undef COMMENT_BLOCK |
| 278 | #undef COMMENT_BLOCK_END |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 279 | } |
| 280 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 281 | /** |
| 282 | * @brief Read a quoted string from data. |
| 283 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 284 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 285 | * @param[in] arg Type of YANG keyword argument expected. |
| 286 | * @param[out] word_p Pointer to the read quoted string. |
| 287 | * @param[out] word_b Pointer to a dynamically-allocated buffer holding the read quoted string. If not needed, |
| 288 | * set to NULL. Otherwise equal to \p word_p. |
| 289 | * @param[out] word_len Length of the read quoted string. |
| 290 | * @param[out] buf_len Length of the dynamically-allocated buffer \p word_b. |
| 291 | * @param[in] indent Current indent (number of YANG spaces). Needed for correct multi-line string |
| 292 | * indenation in the final quoted string. |
| 293 | * |
| 294 | * @return LY_ERR values. |
| 295 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 296 | static LY_ERR |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 297 | read_qstring(struct lys_yang_parser_ctx *ctx, enum yang_arg arg, char **word_p, char **word_b, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 298 | size_t *word_len, size_t *buf_len) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 299 | { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 300 | /* string parsing status: */ |
| 301 | #define STRING_ENDED 0 /* string ended */ |
| 302 | #define STRING_SINGLE_QUOTED 1 /* string with ' */ |
| 303 | #define STRING_DOUBLE_QUOTED 2 /* string with " */ |
| 304 | #define STRING_DOUBLE_QUOTED_ESCAPED 3 /* string with " with last character \ */ |
| 305 | #define STRING_PAUSED_NEXTSTRING 4 /* string finished, now skipping whitespaces looking for + */ |
| 306 | #define STRING_PAUSED_CONTINUE 5 /* string continues after +, skipping whitespaces */ |
| 307 | |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 308 | uint8_t string; |
| 309 | uint64_t block_indent = 0, current_indent = 0; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 310 | ly_bool need_buf = 0; |
| 311 | uint8_t prefix = 0; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 312 | const char *c; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 313 | uint64_t trailing_ws = 0; /* current number of stored trailing whitespace characters */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 314 | |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 315 | if (ctx->in->current[0] == '\"') { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 316 | string = STRING_DOUBLE_QUOTED; |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 317 | current_indent = block_indent = ctx->indent + 1; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 318 | } else { |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 319 | assert(ctx->in->current[0] == '\''); |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 320 | string = STRING_SINGLE_QUOTED; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 321 | } |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 322 | MOVE_INPUT(ctx, 1); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 323 | |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 324 | while (ctx->in->current[0] && string) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 325 | switch (string) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 326 | case STRING_SINGLE_QUOTED: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 327 | switch (ctx->in->current[0]) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 328 | case '\'': |
| 329 | /* string may be finished, but check for + */ |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 330 | string = STRING_PAUSED_NEXTSTRING; |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 331 | MOVE_INPUT(ctx, 1); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 332 | break; |
| 333 | default: |
| 334 | /* check and store character */ |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 335 | LY_CHECK_RET(buf_store_char(ctx, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 336 | break; |
| 337 | } |
| 338 | break; |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 339 | case STRING_DOUBLE_QUOTED: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 340 | switch (ctx->in->current[0]) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 341 | case '\"': |
| 342 | /* string may be finished, but check for + */ |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 343 | string = STRING_PAUSED_NEXTSTRING; |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 344 | MOVE_INPUT(ctx, 1); |
Radek Krejci | ff13cd1 | 2019-10-25 15:34:24 +0200 | [diff] [blame] | 345 | trailing_ws = 0; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 346 | break; |
| 347 | case '\\': |
| 348 | /* special character following */ |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 349 | string = STRING_DOUBLE_QUOTED_ESCAPED; |
Radek Krejci | ff13cd1 | 2019-10-25 15:34:24 +0200 | [diff] [blame] | 350 | |
| 351 | /* the backslash sequence is substituted, so we will need a buffer to store the result */ |
| 352 | need_buf = 1; |
| 353 | |
| 354 | /* move forward to the escaped character */ |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 355 | ++ctx->in->current; |
Radek Krejci | ff13cd1 | 2019-10-25 15:34:24 +0200 | [diff] [blame] | 356 | |
| 357 | /* note that the trailing whitespaces are supposed to be trimmed before substitution of |
| 358 | * backslash-escaped characters (RFC 7950, 6.1.3), so we have to zero the trailing whitespaces counter */ |
| 359 | trailing_ws = 0; |
| 360 | |
| 361 | /* since the backslash-escaped character is handled as first non-whitespace character, stop eating indentation */ |
| 362 | current_indent = block_indent; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 363 | break; |
| 364 | case ' ': |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 365 | if (current_indent < block_indent) { |
| 366 | ++current_indent; |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 367 | MOVE_INPUT(ctx, 1); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 368 | } else { |
Michal Vasko | 90edde4 | 2019-11-25 15:25:07 +0100 | [diff] [blame] | 369 | /* check and store whitespace character */ |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 370 | LY_CHECK_RET(buf_store_char(ctx, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix)); |
Michal Vasko | 90edde4 | 2019-11-25 15:25:07 +0100 | [diff] [blame] | 371 | trailing_ws++; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 372 | } |
| 373 | break; |
| 374 | case '\t': |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 375 | if (current_indent < block_indent) { |
| 376 | assert(need_buf); |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 377 | current_indent += Y_TAB_SPACES; |
| 378 | ctx->indent += Y_TAB_SPACES; |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 379 | for ( ; current_indent > block_indent; --current_indent, --ctx->indent) { |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 380 | /* store leftover spaces from the tab */ |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 381 | c = ctx->in->current; |
| 382 | ctx->in->current = " "; |
| 383 | LY_CHECK_RET(buf_store_char(ctx, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix)); |
| 384 | ctx->in->current = c; |
Michal Vasko | 90edde4 | 2019-11-25 15:25:07 +0100 | [diff] [blame] | 385 | trailing_ws++; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 386 | } |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 387 | ++ctx->in->current; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 388 | } else { |
Michal Vasko | 90edde4 | 2019-11-25 15:25:07 +0100 | [diff] [blame] | 389 | /* check and store whitespace character */ |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 390 | LY_CHECK_RET(buf_store_char(ctx, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix)); |
Michal Vasko | 90edde4 | 2019-11-25 15:25:07 +0100 | [diff] [blame] | 391 | trailing_ws++; |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 392 | /* additional characters for indentation - only 1 was count in buf_store_char */ |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 393 | ctx->indent += Y_TAB_SPACES - 1; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 394 | } |
| 395 | break; |
| 396 | case '\n': |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 397 | if (block_indent) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 398 | /* we will be removing the indents so we need our own buffer */ |
| 399 | need_buf = 1; |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 400 | |
| 401 | /* remove trailing tabs and spaces */ |
Radek Krejci | ff13cd1 | 2019-10-25 15:34:24 +0200 | [diff] [blame] | 402 | (*word_len) = *word_len - trailing_ws; |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 403 | |
Radek Krejci | ff13cd1 | 2019-10-25 15:34:24 +0200 | [diff] [blame] | 404 | /* restart indentation */ |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 405 | current_indent = 0; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | /* check and store character */ |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 409 | LY_CHECK_RET(buf_store_char(ctx, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix)); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 410 | |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 411 | /* reset context indentation counter for possible string after this one */ |
| 412 | ctx->indent = 0; |
Radek Krejci | ff13cd1 | 2019-10-25 15:34:24 +0200 | [diff] [blame] | 413 | trailing_ws = 0; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 414 | break; |
| 415 | default: |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 416 | /* first non-whitespace character, stop eating indentation */ |
| 417 | current_indent = block_indent; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 418 | |
| 419 | /* check and store character */ |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 420 | LY_CHECK_RET(buf_store_char(ctx, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix)); |
Radek Krejci | ff13cd1 | 2019-10-25 15:34:24 +0200 | [diff] [blame] | 421 | trailing_ws = 0; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 422 | break; |
| 423 | } |
| 424 | break; |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 425 | case STRING_DOUBLE_QUOTED_ESCAPED: |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 426 | /* string encoded characters */ |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 427 | c = ctx->in->current; |
| 428 | switch (ctx->in->current[0]) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 429 | case 'n': |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 430 | ctx->in->current = "\n"; |
Radek Krejci | dd713ce | 2021-01-04 23:12:12 +0100 | [diff] [blame] | 431 | /* fix false newline count in buf_store_char() */ |
| 432 | ctx->in->line--; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 433 | break; |
| 434 | case 't': |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 435 | ctx->in->current = "\t"; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 436 | break; |
| 437 | case '\"': |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 438 | case '\\': |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 439 | /* ok as is */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 440 | break; |
| 441 | default: |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 442 | LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Double-quoted string unknown special character '\\%c'.", |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 443 | ctx->in->current[0]); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 444 | return LY_EVALID; |
| 445 | } |
| 446 | |
| 447 | /* check and store character */ |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 448 | LY_CHECK_RET(buf_store_char(ctx, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 449 | |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 450 | string = STRING_DOUBLE_QUOTED; |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 451 | ctx->in->current = c + 1; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 452 | break; |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 453 | case STRING_PAUSED_NEXTSTRING: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 454 | switch (ctx->in->current[0]) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 455 | case '+': |
| 456 | /* string continues */ |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 457 | string = STRING_PAUSED_CONTINUE; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 458 | need_buf = 1; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 459 | break; |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 460 | case '\n': |
Radek Krejci | dd713ce | 2021-01-04 23:12:12 +0100 | [diff] [blame] | 461 | LY_IN_NEW_LINE(ctx->in); |
Radek Krejci | cb3e647 | 2021-01-06 08:19:01 +0100 | [diff] [blame] | 462 | /* fall through */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 463 | case ' ': |
| 464 | case '\t': |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 465 | /* just skip */ |
| 466 | break; |
| 467 | default: |
| 468 | /* string is finished */ |
| 469 | goto string_end; |
| 470 | } |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 471 | MOVE_INPUT(ctx, 1); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 472 | break; |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 473 | case STRING_PAUSED_CONTINUE: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 474 | switch (ctx->in->current[0]) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 475 | case '\n': |
Radek Krejci | dd713ce | 2021-01-04 23:12:12 +0100 | [diff] [blame] | 476 | LY_IN_NEW_LINE(ctx->in); |
Radek Krejci | cb3e647 | 2021-01-06 08:19:01 +0100 | [diff] [blame] | 477 | /* fall through */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 478 | case ' ': |
| 479 | case '\t': |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 480 | /* skip */ |
| 481 | break; |
| 482 | case '\'': |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 483 | string = STRING_SINGLE_QUOTED; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 484 | break; |
| 485 | case '\"': |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 486 | string = STRING_DOUBLE_QUOTED; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 487 | break; |
| 488 | default: |
| 489 | /* it must be quoted again */ |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 490 | LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Both string parts divided by '+' must be quoted."); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 491 | return LY_EVALID; |
| 492 | } |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 493 | MOVE_INPUT(ctx, 1); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 494 | break; |
| 495 | default: |
| 496 | return LY_EINT; |
| 497 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | string_end: |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 501 | if ((arg <= Y_PREF_IDENTIF_ARG) && !(*word_len)) { |
Radek Krejci | 4e199f5 | 2019-05-28 09:09:28 +0200 | [diff] [blame] | 502 | /* empty identifier */ |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 503 | LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Statement argument is required."); |
Radek Krejci | 4e199f5 | 2019-05-28 09:09:28 +0200 | [diff] [blame] | 504 | return LY_EVALID; |
| 505 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 506 | return LY_SUCCESS; |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 507 | |
| 508 | #undef STRING_ENDED |
| 509 | #undef STRING_SINGLE_QUOTED |
| 510 | #undef STRING_DOUBLE_QUOTED |
| 511 | #undef STRING_DOUBLE_QUOTED_ESCAPED |
| 512 | #undef STRING_PAUSED_NEXTSTRING |
| 513 | #undef STRING_PAUSED_CONTINUE |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 514 | } |
| 515 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 516 | /** |
| 517 | * @brief Get another YANG string from the raw data. |
| 518 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 519 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 520 | * @param[in] arg Type of YANG keyword argument expected. |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 521 | * @param[out] flags optional output argument to get flag of the argument's quoting (LYS_*QOUTED - see [schema node flags](@ref snodeflags)) |
Michal Vasko | 2ca70f5 | 2018-09-27 11:04:51 +0200 | [diff] [blame] | 522 | * @param[out] word_p Pointer to the read string. Can return NULL if \p arg is #Y_MAYBE_STR_ARG. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 523 | * @param[out] word_b Pointer to a dynamically-allocated buffer holding the read string. If not needed, |
| 524 | * set to NULL. Otherwise equal to \p word_p. |
| 525 | * @param[out] word_len Length of the read string. |
| 526 | * |
| 527 | * @return LY_ERR values. |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 528 | */ |
Radek Krejci | 2d7a47b | 2019-05-16 13:34:10 +0200 | [diff] [blame] | 529 | LY_ERR |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 530 | get_argument(struct lys_yang_parser_ctx *ctx, enum yang_arg arg, uint16_t *flags, char **word_p, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 531 | char **word_b, size_t *word_len) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 532 | { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 533 | size_t buf_len = 0; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 534 | uint8_t prefix = 0; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 535 | |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 536 | /* word buffer - dynamically allocated */ |
| 537 | *word_b = NULL; |
| 538 | |
| 539 | /* word pointer - just a pointer to data */ |
| 540 | *word_p = NULL; |
| 541 | |
| 542 | *word_len = 0; |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 543 | while (ctx->in->current[0]) { |
| 544 | switch (ctx->in->current[0]) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 545 | case '\'': |
| 546 | case '\"': |
| 547 | if (*word_len) { |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 548 | /* invalid - quotes cannot be in unquoted string and only optsep, ; or { can follow it */ |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 549 | LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, 1, ctx->in->current, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 550 | "unquoted string character, optsep, semicolon or opening brace"); |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 551 | return LY_EVALID; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 552 | } |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 553 | if (flags) { |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 554 | (*flags) |= ctx->in->current[0] == '\'' ? LYS_SINGLEQUOTED : LYS_DOUBLEQUOTED; |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 555 | } |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 556 | LY_CHECK_RET(read_qstring(ctx, arg, word_p, word_b, word_len, &buf_len)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 557 | goto str_end; |
| 558 | case '/': |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 559 | if (ctx->in->current[1] == '/') { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 560 | /* one-line comment */ |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 561 | MOVE_INPUT(ctx, 2); |
| 562 | LY_CHECK_RET(skip_comment(ctx, 1)); |
| 563 | } else if (ctx->in->current[1] == '*') { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 564 | /* block comment */ |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 565 | MOVE_INPUT(ctx, 2); |
| 566 | LY_CHECK_RET(skip_comment(ctx, 2)); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 567 | } else { |
| 568 | /* not a comment after all */ |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 569 | LY_CHECK_RET(buf_store_char(ctx, arg, word_p, word_len, word_b, &buf_len, 0, &prefix)); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 570 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 571 | break; |
| 572 | case ' ': |
| 573 | if (*word_len) { |
| 574 | /* word is finished */ |
| 575 | goto str_end; |
| 576 | } |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 577 | MOVE_INPUT(ctx, 1); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 578 | break; |
| 579 | case '\t': |
| 580 | if (*word_len) { |
| 581 | /* word is finished */ |
| 582 | goto str_end; |
| 583 | } |
| 584 | /* tabs count for 8 spaces */ |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 585 | ctx->indent += Y_TAB_SPACES; |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 586 | |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 587 | ++ctx->in->current; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 588 | break; |
| 589 | case '\n': |
| 590 | if (*word_len) { |
| 591 | /* word is finished */ |
| 592 | goto str_end; |
| 593 | } |
Radek Krejci | dd713ce | 2021-01-04 23:12:12 +0100 | [diff] [blame] | 594 | LY_IN_NEW_LINE(ctx->in); |
Radek Krejci | d54412f | 2020-12-17 20:25:35 +0100 | [diff] [blame] | 595 | MOVE_INPUT(ctx, 1); |
| 596 | |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 597 | /* reset indent */ |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 598 | ctx->indent = 0; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 599 | break; |
| 600 | case ';': |
| 601 | case '{': |
| 602 | if (*word_len || (arg == Y_MAYBE_STR_ARG)) { |
| 603 | /* word is finished */ |
| 604 | goto str_end; |
| 605 | } |
| 606 | |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 607 | LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, 1, ctx->in->current, "an argument"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 608 | return LY_EVALID; |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 609 | case '}': |
| 610 | /* invalid - braces cannot be in unquoted string (opening braces terminates the string and can follow it) */ |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 611 | LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, 1, ctx->in->current, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 612 | "unquoted string character, optsep, semicolon or opening brace"); |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 613 | return LY_EVALID; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 614 | default: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 615 | LY_CHECK_RET(buf_store_char(ctx, arg, word_p, word_len, word_b, &buf_len, 0, &prefix)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 616 | break; |
| 617 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 618 | } |
| 619 | |
Radek Krejci | 0a1d0d4 | 2019-05-16 15:14:51 +0200 | [diff] [blame] | 620 | /* unexpected end of loop */ |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 621 | LOGVAL_PARSER(ctx, LY_VCODE_EOF); |
Radek Krejci | 0a1d0d4 | 2019-05-16 15:14:51 +0200 | [diff] [blame] | 622 | return LY_EVALID; |
| 623 | |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 624 | str_end: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 625 | /* terminating NULL byte for buf */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 626 | if (*word_b) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 627 | (*word_b) = ly_realloc(*word_b, (*word_len) + 1); |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 628 | LY_CHECK_ERR_RET(!(*word_b), LOGMEM(PARSER_CTX(ctx)), LY_EMEM); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 629 | (*word_b)[*word_len] = '\0'; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 630 | *word_p = *word_b; |
| 631 | } |
| 632 | |
| 633 | return LY_SUCCESS; |
| 634 | } |
| 635 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 636 | /** |
| 637 | * @brief Get another YANG keyword from the raw data. |
| 638 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 639 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 640 | * @param[out] kw YANG keyword read. |
| 641 | * @param[out] word_p Pointer to the keyword in the data. Useful for extension instances. |
| 642 | * @param[out] word_len Length of the keyword in the data. Useful for extension instances. |
| 643 | * |
| 644 | * @return LY_ERR values. |
| 645 | */ |
Radek Krejci | 2d7a47b | 2019-05-16 13:34:10 +0200 | [diff] [blame] | 646 | LY_ERR |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 647 | get_keyword(struct lys_yang_parser_ctx *ctx, enum ly_stmt *kw, char **word_p, size_t *word_len) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 648 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 649 | uint8_t prefix; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 650 | const char *word_start; |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 651 | size_t len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 652 | |
| 653 | if (word_p) { |
| 654 | *word_p = NULL; |
| 655 | *word_len = 0; |
| 656 | } |
| 657 | |
| 658 | /* first skip "optsep", comments */ |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 659 | while (ctx->in->current[0]) { |
| 660 | switch (ctx->in->current[0]) { |
Radek Krejci | dcc7b32 | 2018-10-11 14:24:02 +0200 | [diff] [blame] | 661 | case '/': |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 662 | if (ctx->in->current[1] == '/') { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 663 | /* one-line comment */ |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 664 | MOVE_INPUT(ctx, 2); |
| 665 | LY_CHECK_RET(skip_comment(ctx, 1)); |
| 666 | } else if (ctx->in->current[1] == '*') { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 667 | /* block comment */ |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 668 | MOVE_INPUT(ctx, 2); |
| 669 | LY_CHECK_RET(skip_comment(ctx, 2)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 670 | } else { |
Radek Krejci | dcc7b32 | 2018-10-11 14:24:02 +0200 | [diff] [blame] | 671 | /* error - not a comment after all, keyword cannot start with slash */ |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 672 | LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid identifier first character '/'."); |
Radek Krejci | dcc7b32 | 2018-10-11 14:24:02 +0200 | [diff] [blame] | 673 | return LY_EVALID; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 674 | } |
Radek Krejci | 1302828 | 2019-06-11 14:56:48 +0200 | [diff] [blame] | 675 | continue; |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 676 | case '\n': |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 677 | /* skip whitespaces (optsep) */ |
Radek Krejci | dd713ce | 2021-01-04 23:12:12 +0100 | [diff] [blame] | 678 | LY_IN_NEW_LINE(ctx->in); |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 679 | ctx->indent = 0; |
| 680 | break; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 681 | case ' ': |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 682 | /* skip whitespaces (optsep) */ |
| 683 | ++ctx->indent; |
| 684 | break; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 685 | case '\t': |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 686 | /* skip whitespaces (optsep) */ |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 687 | ctx->indent += Y_TAB_SPACES; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 688 | break; |
| 689 | default: |
| 690 | /* either a keyword start or an invalid character */ |
| 691 | goto keyword_start; |
| 692 | } |
| 693 | |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 694 | ly_in_skip(ctx->in, 1); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 695 | } |
| 696 | |
| 697 | keyword_start: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 698 | word_start = ctx->in->current; |
Radek Krejci | d54412f | 2020-12-17 20:25:35 +0100 | [diff] [blame] | 699 | *kw = lysp_match_kw(ctx->in, &ctx->indent); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 700 | |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 701 | if ((*kw == LY_STMT_SYNTAX_SEMICOLON) || (*kw == LY_STMT_SYNTAX_LEFT_BRACE) || (*kw == LY_STMT_SYNTAX_RIGHT_BRACE)) { |
Radek Krejci | 626df48 | 2018-10-11 15:06:31 +0200 | [diff] [blame] | 702 | goto success; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 703 | } |
| 704 | |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 705 | if (*kw != LY_STMT_NONE) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 706 | /* make sure we have the whole keyword */ |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 707 | switch (ctx->in->current[0]) { |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 708 | case '\n': |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 709 | case '\t': |
Radek Krejci | abdd806 | 2019-06-11 16:44:19 +0200 | [diff] [blame] | 710 | case ' ': |
| 711 | /* mandatory "sep" is just checked, not eaten so nothing in the context is updated */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 712 | break; |
Radek Krejci | 156ccaf | 2018-10-15 15:49:17 +0200 | [diff] [blame] | 713 | case ':': |
| 714 | /* keyword is not actually a keyword, but prefix of an extension. |
| 715 | * To avoid repeated check of the prefix syntax, move to the point where the colon was read |
| 716 | * and we will be checking the keyword (extension instance) itself */ |
| 717 | prefix = 1; |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 718 | MOVE_INPUT(ctx, 1); |
Radek Krejci | 156ccaf | 2018-10-15 15:49:17 +0200 | [diff] [blame] | 719 | goto extension; |
Radek Krejci | dcc7b32 | 2018-10-11 14:24:02 +0200 | [diff] [blame] | 720 | case '{': |
| 721 | /* allowed only for input and output statements which can be without arguments */ |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 722 | if ((*kw == LY_STMT_INPUT) || (*kw == LY_STMT_OUTPUT)) { |
Radek Krejci | dcc7b32 | 2018-10-11 14:24:02 +0200 | [diff] [blame] | 723 | break; |
| 724 | } |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 725 | /* fall through */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 726 | default: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 727 | MOVE_INPUT(ctx, 1); |
| 728 | LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, (int)(ctx->in->current - word_start), word_start, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 729 | "a keyword followed by a separator"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 730 | return LY_EVALID; |
| 731 | } |
| 732 | } else { |
| 733 | /* still can be an extension */ |
| 734 | prefix = 0; |
Radek Krejci | 156ccaf | 2018-10-15 15:49:17 +0200 | [diff] [blame] | 735 | extension: |
Radek Krejci | d54412f | 2020-12-17 20:25:35 +0100 | [diff] [blame] | 736 | while (ctx->in->current[0] && (ctx->in->current[0] != ' ') && (ctx->in->current[0] != '\t') && |
| 737 | (ctx->in->current[0] != '\n') && (ctx->in->current[0] != '{') && (ctx->in->current[0] != ';')) { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 738 | uint32_t c = 0; |
| 739 | |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 740 | LY_CHECK_ERR_RET(ly_getutf8(&ctx->in->current, &c, &len), |
| 741 | LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, ctx->in->current[-len]), LY_EVALID); |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 742 | ++ctx->indent; |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 743 | /* check character validity */ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 744 | LY_CHECK_RET(lysp_check_identifierchar((struct lys_parser_ctx *)ctx, c, |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 745 | ctx->in->current - len == word_start ? 1 : 0, &prefix)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 746 | } |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 747 | if (!ctx->in->current[0]) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 748 | LOGVAL_PARSER(ctx, LY_VCODE_EOF); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 749 | return LY_EVALID; |
| 750 | } |
| 751 | |
| 752 | /* prefix is mandatory for extension instances */ |
Radek Krejci | dcc7b32 | 2018-10-11 14:24:02 +0200 | [diff] [blame] | 753 | if (prefix != 2) { |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 754 | LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, (int)(ctx->in->current - word_start), word_start, "a keyword"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 755 | return LY_EVALID; |
| 756 | } |
| 757 | |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 758 | *kw = LY_STMT_EXTENSION_INSTANCE; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 759 | } |
Radek Krejci | 626df48 | 2018-10-11 15:06:31 +0200 | [diff] [blame] | 760 | success: |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 761 | if (word_p) { |
| 762 | *word_p = (char *)word_start; |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 763 | *word_len = ctx->in->current - word_start; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 764 | } |
| 765 | |
| 766 | return LY_SUCCESS; |
| 767 | } |
| 768 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 769 | /** |
| 770 | * @brief Parse extension instance substatements. |
| 771 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 772 | * @param[in] ctx yang parser context for logging. |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 773 | * @param[in] kw Statement keyword value matching @p word value. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 774 | * @param[in] word Extension instance substatement name (keyword). |
| 775 | * @param[in] word_len Extension instance substatement name length. |
| 776 | * @param[in,out] child Children of this extension instance to add to. |
| 777 | * |
| 778 | * @return LY_ERR values. |
| 779 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 780 | static LY_ERR |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 781 | parse_ext_substmt(struct lys_yang_parser_ctx *ctx, enum ly_stmt kw, char *word, size_t word_len, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 782 | struct lysp_stmt **child) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 783 | { |
| 784 | char *buf; |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 785 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 786 | enum ly_stmt child_kw; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 787 | struct lysp_stmt *stmt, *par_child; |
| 788 | |
| 789 | stmt = calloc(1, sizeof *stmt); |
| 790 | LY_CHECK_ERR_RET(!stmt, LOGMEM(NULL), LY_EMEM); |
| 791 | |
Radek Krejci | bb9b198 | 2019-04-08 14:24:59 +0200 | [diff] [blame] | 792 | /* insert into parent statements */ |
| 793 | if (!*child) { |
| 794 | *child = stmt; |
| 795 | } else { |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 796 | for (par_child = *child; par_child->next; par_child = par_child->next) {} |
Radek Krejci | bb9b198 | 2019-04-08 14:24:59 +0200 | [diff] [blame] | 797 | par_child->next = stmt; |
| 798 | } |
| 799 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 800 | LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), word, word_len, &stmt->stmt)); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 801 | stmt->kw = kw; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 802 | |
| 803 | /* get optional argument */ |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 804 | LY_CHECK_RET(get_argument(ctx, Y_MAYBE_STR_ARG, &stmt->flags, &word, &buf, &word_len)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 805 | |
Radek Krejci | 0ae092d | 2018-09-20 16:43:19 +0200 | [diff] [blame] | 806 | if (word) { |
| 807 | if (buf) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 808 | LY_CHECK_RET(lydict_insert_zc(PARSER_CTX(ctx), word, &stmt->arg)); |
Radek Krejci | 0ae092d | 2018-09-20 16:43:19 +0200 | [diff] [blame] | 809 | } else { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 810 | LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), word, word_len, &stmt->arg)); |
Radek Krejci | 0ae092d | 2018-09-20 16:43:19 +0200 | [diff] [blame] | 811 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 812 | } |
| 813 | |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 814 | YANG_READ_SUBSTMT_FOR(ctx, child_kw, word, word_len, ret, ) { |
| 815 | LY_CHECK_RET(parse_ext_substmt(ctx, child_kw, word, word_len, &stmt->child)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 816 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 817 | return ret; |
| 818 | } |
| 819 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 820 | /** |
| 821 | * @brief Parse extension instance. |
| 822 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 823 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 824 | * @param[in] ext_name Extension instance substatement name (keyword). |
| 825 | * @param[in] ext_name_len Extension instance substatement name length. |
| 826 | * @param[in] insubstmt Type of the keyword this extension instance is a substatement of. |
| 827 | * @param[in] insubstmt_index Index of the keyword instance this extension instance is a substatement of. |
| 828 | * @param[in,out] exts Extension instances to add to. |
| 829 | * |
| 830 | * @return LY_ERR values. |
| 831 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 832 | static LY_ERR |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 833 | parse_ext(struct lys_yang_parser_ctx *ctx, const char *ext_name, size_t ext_name_len, LYEXT_SUBSTMT insubstmt, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 834 | LY_ARRAY_COUNT_TYPE insubstmt_index, struct lysp_ext_instance **exts) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 835 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 836 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 837 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 838 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 839 | struct lysp_ext_instance *e; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 840 | enum ly_stmt kw; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 841 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 842 | LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *exts, e, LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 843 | |
| 844 | /* store name and insubstmt info */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 845 | LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), ext_name, ext_name_len, &e->name)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 846 | e->insubstmt = insubstmt; |
| 847 | e->insubstmt_index = insubstmt_index; |
| 848 | |
| 849 | /* get optional argument */ |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 850 | LY_CHECK_RET(get_argument(ctx, Y_MAYBE_STR_ARG, NULL, &word, &buf, &word_len)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 851 | |
Radek Krejci | 0ae092d | 2018-09-20 16:43:19 +0200 | [diff] [blame] | 852 | if (word) { |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 853 | INSERT_WORD_RET(ctx, buf, e->argument, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 854 | } |
| 855 | |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 856 | YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) { |
| 857 | LY_CHECK_RET(parse_ext_substmt(ctx, kw, word, word_len, &e->child)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 858 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 859 | return ret; |
| 860 | } |
| 861 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 862 | /** |
| 863 | * @brief Parse a generic text field without specific constraints. Those are contact, organization, |
| 864 | * description, etc... |
| 865 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 866 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 867 | * @param[in] substmt Type of this substatement. |
| 868 | * @param[in] substmt_index Index of this substatement. |
| 869 | * @param[in,out] value Place to store the parsed value. |
| 870 | * @param[in] arg Type of the YANG keyword argument (of the value). |
| 871 | * @param[in,out] exts Extension instances to add to. |
| 872 | * |
| 873 | * @return LY_ERR values. |
| 874 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 875 | static LY_ERR |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 876 | parse_text_field(struct lys_yang_parser_ctx *ctx, LYEXT_SUBSTMT substmt, uint32_t substmt_index, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 877 | const char **value, enum yang_arg arg, struct lysp_ext_instance **exts) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 878 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 879 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 880 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 881 | size_t word_len; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 882 | enum ly_stmt kw; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 883 | |
| 884 | if (*value) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 885 | LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, lyext_substmt2str(substmt)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 886 | return LY_EVALID; |
| 887 | } |
| 888 | |
| 889 | /* get value */ |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 890 | LY_CHECK_RET(get_argument(ctx, arg, NULL, &word, &buf, &word_len)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 891 | |
| 892 | /* store value and spend buf if allocated */ |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 893 | INSERT_WORD_RET(ctx, buf, *value, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 894 | |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 895 | YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 896 | switch (kw) { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 897 | case LY_STMT_EXTENSION_INSTANCE: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 898 | LY_CHECK_RET(parse_ext(ctx, word, word_len, substmt, substmt_index, exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 899 | break; |
| 900 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 901 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), lyext_substmt2str(substmt)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 902 | return LY_EVALID; |
| 903 | } |
| 904 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 905 | return ret; |
| 906 | } |
| 907 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 908 | /** |
| 909 | * @brief Parse the yang-version statement. |
| 910 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 911 | * @param[in] ctx yang parser context for logging. |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 912 | * @param[out] version Storage for the parsed information. |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 913 | * @param[in,out] exts Extension instances to add to. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 914 | * |
| 915 | * @return LY_ERR values. |
| 916 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 917 | static LY_ERR |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 918 | parse_yangversion(struct lys_yang_parser_ctx *ctx, uint8_t *version, struct lysp_ext_instance **exts) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 919 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 920 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 921 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 922 | size_t word_len; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 923 | enum ly_stmt kw; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 924 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 925 | if (*version) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 926 | LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "yang-version"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 927 | return LY_EVALID; |
| 928 | } |
| 929 | |
| 930 | /* get value */ |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 931 | LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 932 | |
Radek Krejci | 96e48da | 2020-09-04 13:18:06 +0200 | [diff] [blame] | 933 | if ((word_len == 1) && !strncmp(word, "1", word_len)) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 934 | *version = LYS_VERSION_1_0; |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 935 | } else if ((word_len == ly_strlen_const("1.1")) && !strncmp(word, "1.1", word_len)) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 936 | *version = LYS_VERSION_1_1; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 937 | } else { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 938 | LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "yang-version"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 939 | free(buf); |
| 940 | return LY_EVALID; |
| 941 | } |
| 942 | free(buf); |
| 943 | |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 944 | YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 945 | switch (kw) { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 946 | case LY_STMT_EXTENSION_INSTANCE: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 947 | LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_VERSION, 0, exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 948 | break; |
| 949 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 950 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "yang-version"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 951 | return LY_EVALID; |
| 952 | } |
| 953 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 954 | return ret; |
| 955 | } |
| 956 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 957 | /** |
| 958 | * @brief Parse the belongs-to statement. |
| 959 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 960 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 961 | * @param[in,out] prefix Place to store the parsed belongs-to prefix value. |
| 962 | * @param[in,out] exts Extension instances to add to. |
| 963 | * |
| 964 | * @return LY_ERR values. |
| 965 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 966 | static LY_ERR |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 967 | parse_belongsto(struct lys_yang_parser_ctx *ctx, const char **prefix, struct lysp_ext_instance **exts) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 968 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 969 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 970 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 971 | size_t word_len; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 972 | enum ly_stmt kw; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 973 | |
Michal Vasko | c3781c3 | 2020-10-06 14:04:08 +0200 | [diff] [blame] | 974 | if (*prefix) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 975 | LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "belongs-to"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 976 | return LY_EVALID; |
| 977 | } |
| 978 | |
Michal Vasko | c3781c3 | 2020-10-06 14:04:08 +0200 | [diff] [blame] | 979 | /* get value, it must match the main module */ |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 980 | LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len)); |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 981 | if (ly_strncmp(ctx->parsed_mod->mod->name, word, word_len)) { |
Michal Vasko | c3781c3 | 2020-10-06 14:04:08 +0200 | [diff] [blame] | 982 | LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Submodule \"belongs-to\" value \"%.*s\" does not match its module name \"%s\".", |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 983 | (int)word_len, word, ctx->parsed_mod->mod->name); |
Michal Vasko | c3781c3 | 2020-10-06 14:04:08 +0200 | [diff] [blame] | 984 | free(buf); |
| 985 | return LY_EVALID; |
| 986 | } |
| 987 | free(buf); |
Radek Krejci | f09e4e8 | 2019-06-14 15:08:11 +0200 | [diff] [blame] | 988 | |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 989 | YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 990 | switch (kw) { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 991 | case LY_STMT_PREFIX: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 992 | LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_PREFIX, 0, prefix, Y_IDENTIF_ARG, exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 993 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 994 | case LY_STMT_EXTENSION_INSTANCE: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 995 | LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_BELONGSTO, 0, exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 996 | break; |
| 997 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 998 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "belongs-to"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 999 | return LY_EVALID; |
| 1000 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1001 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1002 | LY_CHECK_RET(ret); |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 1003 | checks: |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1004 | /* mandatory substatements */ |
| 1005 | if (!*prefix) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 1006 | LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "belongs-to"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1007 | return LY_EVALID; |
| 1008 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1009 | return ret; |
| 1010 | } |
| 1011 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1012 | /** |
| 1013 | * @brief Parse the revision-date statement. |
| 1014 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1015 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1016 | * @param[in,out] rev Array to store the parsed value in. |
| 1017 | * @param[in,out] exts Extension instances to add to. |
| 1018 | * |
| 1019 | * @return LY_ERR values. |
| 1020 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1021 | static LY_ERR |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1022 | parse_revisiondate(struct lys_yang_parser_ctx *ctx, char *rev, struct lysp_ext_instance **exts) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1023 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1024 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1025 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 1026 | size_t word_len; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1027 | enum ly_stmt kw; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1028 | |
| 1029 | if (rev[0]) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 1030 | LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "revision-date"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1031 | return LY_EVALID; |
| 1032 | } |
| 1033 | |
| 1034 | /* get value */ |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1035 | LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1036 | |
| 1037 | /* check value */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1038 | if (lysp_check_date((struct lys_parser_ctx *)ctx, word, word_len, "revision-date")) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1039 | free(buf); |
| 1040 | return LY_EVALID; |
| 1041 | } |
| 1042 | |
| 1043 | /* store value and spend buf if allocated */ |
| 1044 | strncpy(rev, word, word_len); |
| 1045 | free(buf); |
| 1046 | |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1047 | YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1048 | switch (kw) { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1049 | case LY_STMT_EXTENSION_INSTANCE: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1050 | LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_REVISIONDATE, 0, exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1051 | break; |
| 1052 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 1053 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "revision-date"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1054 | return LY_EVALID; |
| 1055 | } |
| 1056 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1057 | return ret; |
| 1058 | } |
| 1059 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1060 | /** |
| 1061 | * @brief Parse the include statement. |
| 1062 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1063 | * @param[in] ctx yang parser context for logging. |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1064 | * @param[in] module_name Name of the module to check name collisions. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1065 | * @param[in,out] includes Parsed includes to add to. |
| 1066 | * |
| 1067 | * @return LY_ERR values. |
| 1068 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1069 | static LY_ERR |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1070 | parse_include(struct lys_yang_parser_ctx *ctx, const char *module_name, struct lysp_include **includes) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1071 | { |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 1072 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1073 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 1074 | size_t word_len; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1075 | enum ly_stmt kw; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1076 | struct lysp_include *inc; |
| 1077 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 1078 | LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *includes, inc, LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1079 | |
| 1080 | /* get value */ |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1081 | LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1082 | |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 1083 | INSERT_WORD_RET(ctx, buf, inc->name, word, word_len); |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 1084 | |
Radek Krejci | faa1eac | 2018-10-30 14:34:55 +0100 | [diff] [blame] | 1085 | /* submodules share the namespace with the module names, so there must not be |
| 1086 | * a module of the same name in the context, no need for revision matching */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 1087 | if (!strcmp(module_name, inc->name) || ly_ctx_get_module_latest(PARSER_CTX(ctx), inc->name)) { |
Radek Krejci | 854e155 | 2020-12-21 15:05:23 +0100 | [diff] [blame] | 1088 | LOGVAL_PARSER(ctx, LY_VCODE_NAME2_COL, "module", "submodule", inc->name); |
Radek Krejci | faa1eac | 2018-10-30 14:34:55 +0100 | [diff] [blame] | 1089 | return LY_EVALID; |
| 1090 | } |
| 1091 | |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1092 | YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1093 | switch (kw) { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1094 | case LY_STMT_DESCRIPTION: |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1095 | PARSER_CHECK_STMTVER2_RET(ctx, "description", "include"); |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1096 | LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, &inc->dsc, Y_STR_ARG, &inc->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1097 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1098 | case LY_STMT_REFERENCE: |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1099 | PARSER_CHECK_STMTVER2_RET(ctx, "reference", "include"); |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1100 | LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_REFERENCE, 0, &inc->ref, Y_STR_ARG, &inc->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1101 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1102 | case LY_STMT_REVISION_DATE: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1103 | LY_CHECK_RET(parse_revisiondate(ctx, inc->rev, &inc->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1104 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1105 | case LY_STMT_EXTENSION_INSTANCE: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1106 | LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &inc->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1107 | break; |
| 1108 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 1109 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "include"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1110 | return LY_EVALID; |
| 1111 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1112 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1113 | return ret; |
| 1114 | } |
| 1115 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1116 | /** |
| 1117 | * @brief Parse the import statement. |
| 1118 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1119 | * @param[in] ctx yang parser context for logging. |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1120 | * @param[in] module_prefix Prefix of the module to check prefix collisions. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1121 | * @param[in,out] imports Parsed imports to add to. |
| 1122 | * |
| 1123 | * @return LY_ERR values. |
| 1124 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1125 | static LY_ERR |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1126 | parse_import(struct lys_yang_parser_ctx *ctx, const char *module_prefix, struct lysp_import **imports) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1127 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1128 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1129 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 1130 | size_t word_len; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1131 | enum ly_stmt kw; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1132 | struct lysp_import *imp; |
| 1133 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 1134 | LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *imports, imp, LY_EVALID); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1135 | |
| 1136 | /* get value */ |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1137 | LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len)); |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 1138 | INSERT_WORD_RET(ctx, buf, imp->name, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1139 | |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1140 | YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1141 | switch (kw) { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1142 | case LY_STMT_PREFIX: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1143 | LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_PREFIX, 0, &imp->prefix, Y_IDENTIF_ARG, &imp->exts)); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1144 | LY_CHECK_RET(lysp_check_prefix((struct lys_parser_ctx *)ctx, *imports, module_prefix, &imp->prefix), LY_EVALID); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1145 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1146 | case LY_STMT_DESCRIPTION: |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1147 | PARSER_CHECK_STMTVER2_RET(ctx, "description", "import"); |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1148 | LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, &imp->dsc, Y_STR_ARG, &imp->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1149 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1150 | case LY_STMT_REFERENCE: |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1151 | PARSER_CHECK_STMTVER2_RET(ctx, "reference", "import"); |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1152 | LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_REFERENCE, 0, &imp->ref, Y_STR_ARG, &imp->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1153 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1154 | case LY_STMT_REVISION_DATE: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1155 | LY_CHECK_RET(parse_revisiondate(ctx, imp->rev, &imp->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1156 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1157 | case LY_STMT_EXTENSION_INSTANCE: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1158 | LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &imp->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1159 | break; |
| 1160 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 1161 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "import"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1162 | return LY_EVALID; |
| 1163 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1164 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1165 | LY_CHECK_RET(ret); |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 1166 | checks: |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1167 | /* mandatory substatements */ |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 1168 | LY_CHECK_ERR_RET(!imp->prefix, LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "import"), LY_EVALID); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1169 | |
| 1170 | return ret; |
| 1171 | } |
| 1172 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1173 | /** |
| 1174 | * @brief Parse the revision statement. |
| 1175 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1176 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1177 | * @param[in,out] revs Parsed revisions to add to. |
| 1178 | * |
| 1179 | * @return LY_ERR values. |
| 1180 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1181 | static LY_ERR |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1182 | parse_revision(struct lys_yang_parser_ctx *ctx, struct lysp_revision **revs) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1183 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1184 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1185 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 1186 | size_t word_len; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1187 | enum ly_stmt kw; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1188 | struct lysp_revision *rev; |
| 1189 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 1190 | LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *revs, rev, LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1191 | |
| 1192 | /* get value */ |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1193 | LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1194 | |
| 1195 | /* check value */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1196 | if (lysp_check_date((struct lys_parser_ctx *)ctx, word, word_len, "revision")) { |
David Sedlák | 68ef3dc | 2019-07-22 13:40:19 +0200 | [diff] [blame] | 1197 | free(buf); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1198 | return LY_EVALID; |
| 1199 | } |
| 1200 | |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 1201 | strncpy(rev->date, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1202 | free(buf); |
| 1203 | |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1204 | YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1205 | switch (kw) { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1206 | case LY_STMT_DESCRIPTION: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1207 | LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, &rev->dsc, Y_STR_ARG, &rev->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1208 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1209 | case LY_STMT_REFERENCE: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1210 | LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_REFERENCE, 0, &rev->ref, Y_STR_ARG, &rev->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1211 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1212 | case LY_STMT_EXTENSION_INSTANCE: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1213 | LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &rev->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1214 | break; |
| 1215 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 1216 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "revision"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1217 | return LY_EVALID; |
| 1218 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1219 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1220 | return ret; |
| 1221 | } |
| 1222 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1223 | /** |
| 1224 | * @brief Parse a generic text field that can have more instances such as base. |
| 1225 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1226 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1227 | * @param[in] substmt Type of this substatement. |
| 1228 | * @param[in,out] texts Parsed values to add to. |
| 1229 | * @param[in] arg Type of the expected argument. |
| 1230 | * @param[in,out] exts Extension instances to add to. |
| 1231 | * |
| 1232 | * @return LY_ERR values. |
| 1233 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1234 | static LY_ERR |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1235 | parse_text_fields(struct lys_yang_parser_ctx *ctx, LYEXT_SUBSTMT substmt, const char ***texts, enum yang_arg arg, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 1236 | struct lysp_ext_instance **exts) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1237 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1238 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1239 | char *buf, *word; |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 1240 | const char **item; |
| 1241 | size_t word_len; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1242 | enum ly_stmt kw; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1243 | |
| 1244 | /* allocate new pointer */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 1245 | LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *texts, item, LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1246 | |
| 1247 | /* get value */ |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1248 | LY_CHECK_RET(get_argument(ctx, arg, NULL, &word, &buf, &word_len)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1249 | |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 1250 | INSERT_WORD_RET(ctx, buf, *item, word, word_len); |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1251 | YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1252 | switch (kw) { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1253 | case LY_STMT_EXTENSION_INSTANCE: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1254 | LY_CHECK_RET(parse_ext(ctx, word, word_len, substmt, LY_ARRAY_COUNT(*texts) - 1, exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1255 | break; |
| 1256 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 1257 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), lyext_substmt2str(substmt)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1258 | return LY_EVALID; |
| 1259 | } |
| 1260 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1261 | return ret; |
| 1262 | } |
| 1263 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1264 | /** |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 1265 | * @brief Parse a generic text field that can have more instances such as base. |
| 1266 | * |
| 1267 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 1268 | * @param[in] substmt Type of this substatement. |
| 1269 | * @param[in,out] qnames Parsed qnames to add to. |
| 1270 | * @param[in] arg Type of the expected argument. |
| 1271 | * @param[in,out] exts Extension instances to add to. |
| 1272 | * |
| 1273 | * @return LY_ERR values. |
| 1274 | */ |
| 1275 | static LY_ERR |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1276 | parse_qnames(struct lys_yang_parser_ctx *ctx, LYEXT_SUBSTMT substmt, struct lysp_qname **qnames, |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 1277 | enum yang_arg arg, struct lysp_ext_instance **exts) |
| 1278 | { |
| 1279 | LY_ERR ret = LY_SUCCESS; |
| 1280 | char *buf, *word; |
| 1281 | struct lysp_qname *item; |
| 1282 | size_t word_len; |
| 1283 | enum ly_stmt kw; |
| 1284 | |
| 1285 | /* allocate new pointer */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 1286 | LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *qnames, item, LY_EMEM); |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 1287 | |
| 1288 | /* get value */ |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1289 | LY_CHECK_RET(get_argument(ctx, arg, NULL, &word, &buf, &word_len)); |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 1290 | |
| 1291 | INSERT_WORD_RET(ctx, buf, item->str, word, word_len); |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 1292 | item->mod = ctx->parsed_mod; |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1293 | YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) { |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 1294 | switch (kw) { |
| 1295 | case LY_STMT_EXTENSION_INSTANCE: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1296 | LY_CHECK_RET(parse_ext(ctx, word, word_len, substmt, LY_ARRAY_COUNT(*qnames) - 1, exts)); |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 1297 | break; |
| 1298 | default: |
| 1299 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), lyext_substmt2str(substmt)); |
| 1300 | return LY_EVALID; |
| 1301 | } |
| 1302 | } |
| 1303 | return ret; |
| 1304 | } |
| 1305 | |
| 1306 | /** |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1307 | * @brief Parse the config statement. |
| 1308 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1309 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1310 | * @param[in,out] flags Flags to add to. |
| 1311 | * @param[in,out] exts Extension instances to add to. |
| 1312 | * |
| 1313 | * @return LY_ERR values. |
| 1314 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1315 | static LY_ERR |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1316 | parse_config(struct lys_yang_parser_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1317 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1318 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1319 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 1320 | size_t word_len; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1321 | enum ly_stmt kw; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1322 | |
| 1323 | if (*flags & LYS_CONFIG_MASK) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 1324 | LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "config"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1325 | return LY_EVALID; |
| 1326 | } |
| 1327 | |
| 1328 | /* get value */ |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1329 | LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1330 | |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1331 | if ((word_len == ly_strlen_const("true")) && !strncmp(word, "true", word_len)) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1332 | *flags |= LYS_CONFIG_W; |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1333 | } else if ((word_len == ly_strlen_const("false")) && !strncmp(word, "false", word_len)) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1334 | *flags |= LYS_CONFIG_R; |
| 1335 | } else { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 1336 | LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "config"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1337 | free(buf); |
| 1338 | return LY_EVALID; |
| 1339 | } |
| 1340 | free(buf); |
| 1341 | |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1342 | YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1343 | switch (kw) { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1344 | case LY_STMT_EXTENSION_INSTANCE: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1345 | LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_CONFIG, 0, exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1346 | break; |
| 1347 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 1348 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "config"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1349 | return LY_EVALID; |
| 1350 | } |
| 1351 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1352 | return ret; |
| 1353 | } |
| 1354 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1355 | /** |
| 1356 | * @brief Parse the mandatory statement. |
| 1357 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1358 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1359 | * @param[in,out] flags Flags to add to. |
| 1360 | * @param[in,out] exts Extension instances to add to. |
| 1361 | * |
| 1362 | * @return LY_ERR values. |
| 1363 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1364 | static LY_ERR |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1365 | parse_mandatory(struct lys_yang_parser_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1366 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1367 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1368 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 1369 | size_t word_len; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1370 | enum ly_stmt kw; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1371 | |
| 1372 | if (*flags & LYS_MAND_MASK) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 1373 | LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "mandatory"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1374 | return LY_EVALID; |
| 1375 | } |
| 1376 | |
| 1377 | /* get value */ |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1378 | LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1379 | |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1380 | if ((word_len == ly_strlen_const("true")) && !strncmp(word, "true", word_len)) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1381 | *flags |= LYS_MAND_TRUE; |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1382 | } else if ((word_len == ly_strlen_const("false")) && !strncmp(word, "false", word_len)) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1383 | *flags |= LYS_MAND_FALSE; |
| 1384 | } else { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 1385 | LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "mandatory"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1386 | free(buf); |
| 1387 | return LY_EVALID; |
| 1388 | } |
| 1389 | free(buf); |
| 1390 | |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1391 | YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1392 | switch (kw) { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1393 | case LY_STMT_EXTENSION_INSTANCE: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1394 | LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_MANDATORY, 0, exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1395 | break; |
| 1396 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 1397 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "mandatory"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1398 | return LY_EVALID; |
| 1399 | } |
| 1400 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1401 | return ret; |
| 1402 | } |
| 1403 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1404 | /** |
| 1405 | * @brief Parse a restriction such as range or length. |
| 1406 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1407 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1408 | * @param[in] restr_kw Type of this particular restriction. |
| 1409 | * @param[in,out] exts Extension instances to add to. |
| 1410 | * |
| 1411 | * @return LY_ERR values. |
| 1412 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1413 | static LY_ERR |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1414 | parse_restr(struct lys_yang_parser_ctx *ctx, enum ly_stmt restr_kw, struct lysp_restr *restr) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1415 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1416 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1417 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 1418 | size_t word_len; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1419 | enum ly_stmt kw; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1420 | |
| 1421 | /* get value */ |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1422 | LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1423 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1424 | CHECK_NONEMPTY(ctx, word_len, ly_stmt2str(restr_kw)); |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 1425 | INSERT_WORD_RET(ctx, buf, restr->arg.str, word, word_len); |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 1426 | restr->arg.mod = ctx->parsed_mod; |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1427 | YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1428 | switch (kw) { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1429 | case LY_STMT_DESCRIPTION: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1430 | LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, &restr->dsc, Y_STR_ARG, &restr->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1431 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1432 | case LY_STMT_REFERENCE: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1433 | LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_REFERENCE, 0, &restr->ref, Y_STR_ARG, &restr->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1434 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1435 | case LY_STMT_ERROR_APP_TAG: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1436 | LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_ERRTAG, 0, &restr->eapptag, Y_STR_ARG, &restr->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1437 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1438 | case LY_STMT_ERROR_MESSAGE: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1439 | LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_ERRMSG, 0, &restr->emsg, Y_STR_ARG, &restr->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1440 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1441 | case LY_STMT_EXTENSION_INSTANCE: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1442 | LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &restr->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1443 | break; |
| 1444 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 1445 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(restr_kw)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1446 | return LY_EVALID; |
| 1447 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1448 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1449 | return ret; |
| 1450 | } |
| 1451 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1452 | /** |
| 1453 | * @brief Parse a restriction that can have more instances such as must. |
| 1454 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1455 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1456 | * @param[in] restr_kw Type of this particular restriction. |
| 1457 | * @param[in,out] restrs Restrictions to add to. |
| 1458 | * |
| 1459 | * @return LY_ERR values. |
| 1460 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1461 | static LY_ERR |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1462 | parse_restrs(struct lys_yang_parser_ctx *ctx, enum ly_stmt restr_kw, struct lysp_restr **restrs) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1463 | { |
| 1464 | struct lysp_restr *restr; |
| 1465 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 1466 | LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *restrs, restr, LY_EMEM); |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1467 | return parse_restr(ctx, restr_kw, restr); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1468 | } |
| 1469 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1470 | /** |
| 1471 | * @brief Parse the status statement. |
| 1472 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1473 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1474 | * @param[in,out] flags Flags to add to. |
| 1475 | * @param[in,out] exts Extension instances to add to. |
| 1476 | * |
| 1477 | * @return LY_ERR values. |
| 1478 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1479 | static LY_ERR |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1480 | parse_status(struct lys_yang_parser_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1481 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1482 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1483 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 1484 | size_t word_len; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1485 | enum ly_stmt kw; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1486 | |
| 1487 | if (*flags & LYS_STATUS_MASK) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 1488 | LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "status"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1489 | return LY_EVALID; |
| 1490 | } |
| 1491 | |
| 1492 | /* get value */ |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1493 | LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1494 | |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1495 | if ((word_len == ly_strlen_const("current")) && !strncmp(word, "current", word_len)) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1496 | *flags |= LYS_STATUS_CURR; |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1497 | } else if ((word_len == ly_strlen_const("deprecated")) && !strncmp(word, "deprecated", word_len)) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1498 | *flags |= LYS_STATUS_DEPRC; |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1499 | } else if ((word_len == ly_strlen_const("obsolete")) && !strncmp(word, "obsolete", word_len)) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1500 | *flags |= LYS_STATUS_OBSLT; |
| 1501 | } else { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 1502 | LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "status"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1503 | free(buf); |
| 1504 | return LY_EVALID; |
| 1505 | } |
| 1506 | free(buf); |
| 1507 | |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1508 | YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1509 | switch (kw) { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1510 | case LY_STMT_EXTENSION_INSTANCE: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1511 | LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_STATUS, 0, exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1512 | break; |
| 1513 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 1514 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "status"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1515 | return LY_EVALID; |
| 1516 | } |
| 1517 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1518 | return ret; |
| 1519 | } |
| 1520 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1521 | /** |
| 1522 | * @brief Parse the when statement. |
| 1523 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1524 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1525 | * @param[in,out] when_p When pointer to parse to. |
| 1526 | * |
| 1527 | * @return LY_ERR values. |
| 1528 | */ |
Radek Krejci | f09e4e8 | 2019-06-14 15:08:11 +0200 | [diff] [blame] | 1529 | LY_ERR |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1530 | parse_when(struct lys_yang_parser_ctx *ctx, struct lysp_when **when_p) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1531 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1532 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1533 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 1534 | size_t word_len; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1535 | enum ly_stmt kw; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1536 | struct lysp_when *when; |
| 1537 | |
| 1538 | if (*when_p) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 1539 | LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "when"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1540 | return LY_EVALID; |
| 1541 | } |
| 1542 | |
| 1543 | when = calloc(1, sizeof *when); |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 1544 | LY_CHECK_ERR_RET(!when, LOGMEM(PARSER_CTX(ctx)), LY_EMEM); |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 1545 | *when_p = when; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1546 | |
| 1547 | /* get value */ |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1548 | LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len)); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1549 | CHECK_NONEMPTY(ctx, word_len, "when"); |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 1550 | INSERT_WORD_RET(ctx, buf, when->cond, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1551 | |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1552 | YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1553 | switch (kw) { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1554 | case LY_STMT_DESCRIPTION: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1555 | LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, &when->dsc, Y_STR_ARG, &when->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1556 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1557 | case LY_STMT_REFERENCE: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1558 | LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_REFERENCE, 0, &when->ref, Y_STR_ARG, &when->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1559 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1560 | case LY_STMT_EXTENSION_INSTANCE: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1561 | LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &when->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1562 | break; |
| 1563 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 1564 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "when"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1565 | return LY_EVALID; |
| 1566 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1567 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1568 | return ret; |
| 1569 | } |
| 1570 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1571 | /** |
| 1572 | * @brief Parse the anydata or anyxml statement. |
| 1573 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1574 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1575 | * @param[in] kw Type of this particular keyword. |
| 1576 | * @param[in,out] siblings Siblings to add to. |
| 1577 | * |
| 1578 | * @return LY_ERR values. |
| 1579 | */ |
Radek Krejci | 2d7a47b | 2019-05-16 13:34:10 +0200 | [diff] [blame] | 1580 | LY_ERR |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1581 | parse_any(struct lys_yang_parser_ctx *ctx, enum ly_stmt kw, struct lysp_node *parent, struct lysp_node **siblings) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1582 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1583 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1584 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 1585 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1586 | struct lysp_node_anydata *any; |
| 1587 | |
David Sedlák | 60adc09 | 2019-08-06 15:57:02 +0200 | [diff] [blame] | 1588 | /* create new structure and insert into siblings */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 1589 | LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, any, next, LY_EMEM); |
David Sedlák | 60adc09 | 2019-08-06 15:57:02 +0200 | [diff] [blame] | 1590 | |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1591 | any->nodetype = kw == LY_STMT_ANYDATA ? LYS_ANYDATA : LYS_ANYXML; |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1592 | any->parent = parent; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1593 | |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1594 | /* get name */ |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1595 | LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len)); |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 1596 | INSERT_WORD_RET(ctx, buf, any->name, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1597 | |
| 1598 | /* parse substatements */ |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1599 | YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1600 | switch (kw) { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1601 | case LY_STMT_CONFIG: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1602 | LY_CHECK_RET(parse_config(ctx, &any->flags, &any->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1603 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1604 | case LY_STMT_DESCRIPTION: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1605 | LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, &any->dsc, Y_STR_ARG, &any->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1606 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1607 | case LY_STMT_IF_FEATURE: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1608 | LY_CHECK_RET(parse_qnames(ctx, LYEXT_SUBSTMT_IFFEATURE, &any->iffeatures, Y_STR_ARG, &any->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1609 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1610 | case LY_STMT_MANDATORY: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1611 | LY_CHECK_RET(parse_mandatory(ctx, &any->flags, &any->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1612 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1613 | case LY_STMT_MUST: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1614 | LY_CHECK_RET(parse_restrs(ctx, kw, &any->musts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1615 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1616 | case LY_STMT_REFERENCE: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1617 | LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_REFERENCE, 0, &any->ref, Y_STR_ARG, &any->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1618 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1619 | case LY_STMT_STATUS: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1620 | LY_CHECK_RET(parse_status(ctx, &any->flags, &any->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1621 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1622 | case LY_STMT_WHEN: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1623 | LY_CHECK_RET(parse_when(ctx, &any->when)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1624 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1625 | case LY_STMT_EXTENSION_INSTANCE: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1626 | LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &any->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1627 | break; |
| 1628 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 1629 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 1630 | (any->nodetype & LYS_ANYDATA) == LYS_ANYDATA ? ly_stmt2str(LY_STMT_ANYDATA) : ly_stmt2str(LY_STMT_ANYXML)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1631 | return LY_EVALID; |
| 1632 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1633 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1634 | return ret; |
| 1635 | } |
| 1636 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1637 | /** |
| 1638 | * @brief Parse the value or position statement. Substatement of type enum statement. |
| 1639 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1640 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1641 | * @param[in] val_kw Type of this particular keyword. |
| 1642 | * @param[in,out] value Value to write to. |
| 1643 | * @param[in,out] flags Flags to write to. |
| 1644 | * @param[in,out] exts Extension instances to add to. |
| 1645 | * |
| 1646 | * @return LY_ERR values. |
| 1647 | */ |
David Sedlák | d6ce6d7 | 2019-07-16 17:30:18 +0200 | [diff] [blame] | 1648 | LY_ERR |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1649 | parse_type_enum_value_pos(struct lys_yang_parser_ctx *ctx, enum ly_stmt val_kw, int64_t *value, uint16_t *flags, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 1650 | struct lysp_ext_instance **exts) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1651 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1652 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1653 | char *buf, *word, *ptr; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 1654 | size_t word_len; |
Radek Krejci | 4ad42aa | 2019-07-23 16:55:58 +0200 | [diff] [blame] | 1655 | long int num = 0; |
| 1656 | unsigned long int unum = 0; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1657 | enum ly_stmt kw; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1658 | |
| 1659 | if (*flags & LYS_SET_VALUE) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 1660 | LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(val_kw)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1661 | return LY_EVALID; |
| 1662 | } |
| 1663 | *flags |= LYS_SET_VALUE; |
| 1664 | |
| 1665 | /* get value */ |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1666 | LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1667 | |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1668 | if (!word_len || (word[0] == '+') || ((word[0] == '0') && (word_len > 1)) || ((val_kw == LY_STMT_POSITION) && !strncmp(word, "-0", 2))) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 1669 | LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw)); |
Radek Krejci | 8b76466 | 2018-11-14 14:15:13 +0100 | [diff] [blame] | 1670 | goto error; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1671 | } |
| 1672 | |
| 1673 | errno = 0; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1674 | if (val_kw == LY_STMT_VALUE) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1675 | num = strtol(word, &ptr, LY_BASE_DEC); |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1676 | if ((num < INT64_C(-2147483648)) || (num > INT64_C(2147483647))) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 1677 | LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw)); |
Radek Krejci | 8b76466 | 2018-11-14 14:15:13 +0100 | [diff] [blame] | 1678 | goto error; |
| 1679 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1680 | } else { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1681 | unum = strtoul(word, &ptr, LY_BASE_DEC); |
Radek Krejci | 8b76466 | 2018-11-14 14:15:13 +0100 | [diff] [blame] | 1682 | if (unum > UINT64_C(4294967295)) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 1683 | LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw)); |
Radek Krejci | 8b76466 | 2018-11-14 14:15:13 +0100 | [diff] [blame] | 1684 | goto error; |
| 1685 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1686 | } |
| 1687 | /* we have not parsed the whole argument */ |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 1688 | if ((size_t)(ptr - word) != word_len) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 1689 | LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw)); |
Radek Krejci | 8b76466 | 2018-11-14 14:15:13 +0100 | [diff] [blame] | 1690 | goto error; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1691 | } |
| 1692 | if (errno == ERANGE) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 1693 | LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, ly_stmt2str(val_kw)); |
Radek Krejci | 8b76466 | 2018-11-14 14:15:13 +0100 | [diff] [blame] | 1694 | goto error; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1695 | } |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1696 | if (val_kw == LY_STMT_VALUE) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1697 | *value = num; |
| 1698 | } else { |
| 1699 | *value = unum; |
| 1700 | } |
| 1701 | free(buf); |
| 1702 | |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1703 | YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1704 | switch (kw) { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1705 | case LY_STMT_EXTENSION_INSTANCE: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1706 | LY_CHECK_RET(parse_ext(ctx, word, word_len, val_kw == LY_STMT_VALUE ? LYEXT_SUBSTMT_VALUE : LYEXT_SUBSTMT_POSITION, 0, exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1707 | break; |
| 1708 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 1709 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(val_kw)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1710 | return LY_EVALID; |
| 1711 | } |
| 1712 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1713 | return ret; |
Radek Krejci | 8b76466 | 2018-11-14 14:15:13 +0100 | [diff] [blame] | 1714 | |
| 1715 | error: |
| 1716 | free(buf); |
| 1717 | return LY_EVALID; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1718 | } |
| 1719 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1720 | /** |
| 1721 | * @brief Parse the enum or bit statement. Substatement of type statement. |
| 1722 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1723 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1724 | * @param[in] enum_kw Type of this particular keyword. |
| 1725 | * @param[in,out] enums Enums or bits to add to. |
| 1726 | * |
| 1727 | * @return LY_ERR values. |
| 1728 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1729 | static LY_ERR |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1730 | parse_type_enum(struct lys_yang_parser_ctx *ctx, enum ly_stmt enum_kw, struct lysp_type_enum **enums) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1731 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1732 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1733 | char *buf, *word; |
David Sedlák | 6544c18 | 2019-07-12 13:17:33 +0200 | [diff] [blame] | 1734 | size_t word_len; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1735 | enum ly_stmt kw; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1736 | struct lysp_type_enum *enm; |
| 1737 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 1738 | LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *enums, enm, LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1739 | |
| 1740 | /* get value */ |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1741 | LY_CHECK_RET(get_argument(ctx, enum_kw == LY_STMT_ENUM ? Y_STR_ARG : Y_IDENTIF_ARG, NULL, &word, &buf, &word_len)); |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1742 | if (enum_kw == LY_STMT_ENUM) { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1743 | ret = lysp_check_enum_name((struct lys_parser_ctx *)ctx, (const char *)word, word_len); |
David Sedlák | 6544c18 | 2019-07-12 13:17:33 +0200 | [diff] [blame] | 1744 | LY_CHECK_ERR_RET(ret, free(buf), ret); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1745 | } /* else nothing specific for YANG_BIT */ |
Radek Krejci | 8b76466 | 2018-11-14 14:15:13 +0100 | [diff] [blame] | 1746 | |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 1747 | INSERT_WORD_RET(ctx, buf, enm->name, word, word_len); |
Radek Krejci | 8b76466 | 2018-11-14 14:15:13 +0100 | [diff] [blame] | 1748 | CHECK_UNIQUENESS(ctx, *enums, name, ly_stmt2str(enum_kw), enm->name); |
| 1749 | |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1750 | YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1751 | switch (kw) { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1752 | case LY_STMT_DESCRIPTION: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1753 | LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, &enm->dsc, Y_STR_ARG, &enm->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1754 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1755 | case LY_STMT_IF_FEATURE: |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 1756 | PARSER_CHECK_STMTVER2_RET(ctx, "if-feature", ly_stmt2str(enum_kw)); |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1757 | LY_CHECK_RET(parse_qnames(ctx, LYEXT_SUBSTMT_IFFEATURE, &enm->iffeatures, Y_STR_ARG, &enm->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1758 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1759 | case LY_STMT_REFERENCE: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1760 | LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_REFERENCE, 0, &enm->ref, Y_STR_ARG, &enm->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1761 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1762 | case LY_STMT_STATUS: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1763 | LY_CHECK_RET(parse_status(ctx, &enm->flags, &enm->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1764 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1765 | case LY_STMT_VALUE: |
| 1766 | 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] | 1767 | ly_stmt2str(enum_kw)), LY_EVALID); |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1768 | LY_CHECK_RET(parse_type_enum_value_pos(ctx, kw, &enm->value, &enm->flags, &enm->exts)); |
David Sedlák | 9fb515f | 2019-07-11 10:33:58 +0200 | [diff] [blame] | 1769 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1770 | case LY_STMT_POSITION: |
| 1771 | 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] | 1772 | ly_stmt2str(enum_kw)), LY_EVALID); |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1773 | LY_CHECK_RET(parse_type_enum_value_pos(ctx, kw, &enm->value, &enm->flags, &enm->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1774 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1775 | case LY_STMT_EXTENSION_INSTANCE: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1776 | LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &enm->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1777 | break; |
| 1778 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 1779 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(enum_kw)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1780 | return LY_EVALID; |
| 1781 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1782 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1783 | return ret; |
| 1784 | } |
| 1785 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1786 | /** |
| 1787 | * @brief Parse the fraction-digits statement. Substatement of type statement. |
| 1788 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1789 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1790 | * @param[in,out] fracdig Value to write to. |
| 1791 | * @param[in,out] exts Extension instances to add to. |
| 1792 | * |
| 1793 | * @return LY_ERR values. |
| 1794 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1795 | static LY_ERR |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1796 | parse_type_fracdigits(struct lys_yang_parser_ctx *ctx, uint8_t *fracdig, struct lysp_ext_instance **exts) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1797 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1798 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1799 | char *buf, *word, *ptr; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 1800 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1801 | unsigned long int num; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1802 | enum ly_stmt kw; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1803 | |
| 1804 | if (*fracdig) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 1805 | LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "fraction-digits"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1806 | return LY_EVALID; |
| 1807 | } |
| 1808 | |
| 1809 | /* get value */ |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1810 | LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1811 | |
| 1812 | if (!word_len || (word[0] == '0') || !isdigit(word[0])) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 1813 | LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "fraction-digits"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1814 | free(buf); |
| 1815 | return LY_EVALID; |
| 1816 | } |
| 1817 | |
| 1818 | errno = 0; |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1819 | num = strtoul(word, &ptr, LY_BASE_DEC); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1820 | /* we have not parsed the whole argument */ |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 1821 | if ((size_t)(ptr - word) != word_len) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 1822 | LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "fraction-digits"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1823 | free(buf); |
| 1824 | return LY_EVALID; |
| 1825 | } |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1826 | if ((errno == ERANGE) || (num > LY_TYPE_DEC64_FD_MAX)) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 1827 | LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, "fraction-digits"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1828 | free(buf); |
| 1829 | return LY_EVALID; |
| 1830 | } |
| 1831 | *fracdig = num; |
| 1832 | free(buf); |
| 1833 | |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1834 | YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1835 | switch (kw) { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1836 | case LY_STMT_EXTENSION_INSTANCE: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1837 | LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_FRACDIGITS, 0, exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1838 | break; |
| 1839 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 1840 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "fraction-digits"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1841 | return LY_EVALID; |
| 1842 | } |
| 1843 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1844 | return ret; |
| 1845 | } |
| 1846 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1847 | /** |
| 1848 | * @brief Parse the require-instance statement. Substatement of type statement. |
| 1849 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1850 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1851 | * @param[in,out] reqinst Value to write to. |
| 1852 | * @param[in,out] flags Flags to write to. |
| 1853 | * @param[in,out] exts Extension instances to add to. |
| 1854 | * |
| 1855 | * @return LY_ERR values. |
| 1856 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1857 | static LY_ERR |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1858 | parse_type_reqinstance(struct lys_yang_parser_ctx *ctx, uint8_t *reqinst, uint16_t *flags, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 1859 | struct lysp_ext_instance **exts) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1860 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1861 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1862 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 1863 | size_t word_len; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1864 | enum ly_stmt kw; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1865 | |
| 1866 | if (*flags & LYS_SET_REQINST) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 1867 | LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "require-instance"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1868 | return LY_EVALID; |
| 1869 | } |
| 1870 | *flags |= LYS_SET_REQINST; |
| 1871 | |
| 1872 | /* get value */ |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1873 | LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1874 | |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1875 | if ((word_len == ly_strlen_const("true")) && !strncmp(word, "true", word_len)) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1876 | *reqinst = 1; |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1877 | } else if ((word_len != ly_strlen_const("false")) || strncmp(word, "false", word_len)) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 1878 | LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "require-instance"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1879 | free(buf); |
| 1880 | return LY_EVALID; |
| 1881 | } |
| 1882 | free(buf); |
| 1883 | |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1884 | YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1885 | switch (kw) { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1886 | case LY_STMT_EXTENSION_INSTANCE: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1887 | LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_REQINSTANCE, 0, exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1888 | break; |
| 1889 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 1890 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "require-instance"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1891 | return LY_EVALID; |
| 1892 | } |
| 1893 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1894 | return ret; |
| 1895 | } |
| 1896 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1897 | /** |
| 1898 | * @brief Parse the modifier statement. Substatement of type pattern statement. |
| 1899 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1900 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1901 | * @param[in,out] pat Value to write to. |
| 1902 | * @param[in,out] exts Extension instances to add to. |
| 1903 | * |
| 1904 | * @return LY_ERR values. |
| 1905 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1906 | static LY_ERR |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1907 | parse_type_pattern_modifier(struct lys_yang_parser_ctx *ctx, const char **pat, struct lysp_ext_instance **exts) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1908 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1909 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1910 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 1911 | size_t word_len; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1912 | enum ly_stmt kw; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1913 | |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1914 | if ((*pat)[0] == LYSP_RESTR_PATTERN_NACK) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 1915 | LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "modifier"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1916 | return LY_EVALID; |
| 1917 | } |
| 1918 | |
| 1919 | /* get value */ |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1920 | LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1921 | |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1922 | if ((word_len != ly_strlen_const("invert-match")) || strncmp(word, "invert-match", word_len)) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 1923 | LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "modifier"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1924 | free(buf); |
| 1925 | return LY_EVALID; |
| 1926 | } |
| 1927 | free(buf); |
| 1928 | |
| 1929 | /* replace the value in the dictionary */ |
| 1930 | buf = malloc(strlen(*pat) + 1); |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 1931 | LY_CHECK_ERR_RET(!buf, LOGMEM(PARSER_CTX(ctx)), LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1932 | strcpy(buf, *pat); |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 1933 | lydict_remove(PARSER_CTX(ctx), *pat); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1934 | |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1935 | assert(buf[0] == LYSP_RESTR_PATTERN_ACK); |
| 1936 | buf[0] = LYSP_RESTR_PATTERN_NACK; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 1937 | LY_CHECK_RET(lydict_insert_zc(PARSER_CTX(ctx), buf, pat)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1938 | |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1939 | YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1940 | switch (kw) { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1941 | case LY_STMT_EXTENSION_INSTANCE: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1942 | LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_MODIFIER, 0, exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1943 | break; |
| 1944 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 1945 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "modifier"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1946 | return LY_EVALID; |
| 1947 | } |
| 1948 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1949 | return ret; |
| 1950 | } |
| 1951 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1952 | /** |
| 1953 | * @brief Parse the pattern statement. Substatement of type statement. |
| 1954 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1955 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1956 | * @param[in,out] patterns Restrictions to add to. |
| 1957 | * |
| 1958 | * @return LY_ERR values. |
| 1959 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1960 | static LY_ERR |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1961 | parse_type_pattern(struct lys_yang_parser_ctx *ctx, struct lysp_restr **patterns) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1962 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1963 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1964 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 1965 | size_t word_len; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1966 | enum ly_stmt kw; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1967 | struct lysp_restr *restr; |
| 1968 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 1969 | LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *patterns, restr, LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1970 | |
| 1971 | /* get value */ |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1972 | LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1973 | |
| 1974 | /* add special meaning first byte */ |
| 1975 | if (buf) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1976 | buf = ly_realloc(buf, word_len + 2); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1977 | word = buf; |
| 1978 | } else { |
| 1979 | buf = malloc(word_len + 2); |
| 1980 | } |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 1981 | LY_CHECK_ERR_RET(!buf, LOGMEM(PARSER_CTX(ctx)), LY_EMEM); |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1982 | memmove(buf + 1, word, word_len); |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1983 | buf[0] = LYSP_RESTR_PATTERN_ACK; /* pattern's default regular-match flag */ |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1984 | buf[word_len + 1] = '\0'; /* terminating NULL byte */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 1985 | LY_CHECK_RET(lydict_insert_zc(PARSER_CTX(ctx), buf, &restr->arg.str)); |
| 1986 | restr->arg.mod = ctx->parsed_mod; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1987 | |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1988 | YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1989 | switch (kw) { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1990 | case LY_STMT_DESCRIPTION: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1991 | LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, &restr->dsc, Y_STR_ARG, &restr->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1992 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1993 | case LY_STMT_REFERENCE: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1994 | LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_REFERENCE, 0, &restr->ref, Y_STR_ARG, &restr->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1995 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1996 | case LY_STMT_ERROR_APP_TAG: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1997 | LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_ERRTAG, 0, &restr->eapptag, Y_STR_ARG, &restr->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1998 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1999 | case LY_STMT_ERROR_MESSAGE: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2000 | LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_ERRMSG, 0, &restr->emsg, Y_STR_ARG, &restr->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2001 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2002 | case LY_STMT_MODIFIER: |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 2003 | PARSER_CHECK_STMTVER2_RET(ctx, "modifier", "pattern"); |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2004 | LY_CHECK_RET(parse_type_pattern_modifier(ctx, &restr->arg.str, &restr->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2005 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2006 | case LY_STMT_EXTENSION_INSTANCE: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2007 | LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &restr->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2008 | break; |
| 2009 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 2010 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "pattern"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2011 | return LY_EVALID; |
| 2012 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2013 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2014 | return ret; |
| 2015 | } |
| 2016 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2017 | /** |
| 2018 | * @brief Parse the type statement. |
| 2019 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2020 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2021 | * @param[in,out] type Type to wrote to. |
| 2022 | * |
| 2023 | * @return LY_ERR values. |
| 2024 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2025 | static LY_ERR |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2026 | parse_type(struct lys_yang_parser_ctx *ctx, struct lysp_type *type) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2027 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2028 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2029 | char *buf, *word; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2030 | const char *str_path = NULL; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 2031 | size_t word_len; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2032 | enum ly_stmt kw; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2033 | struct lysp_type *nest_type; |
| 2034 | |
| 2035 | if (type->name) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 2036 | LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "type"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2037 | return LY_EVALID; |
| 2038 | } |
| 2039 | |
| 2040 | /* get value */ |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2041 | LY_CHECK_RET(get_argument(ctx, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &word_len)); |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 2042 | INSERT_WORD_RET(ctx, buf, type->name, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2043 | |
Michal Vasko | e9c050f | 2020-10-06 14:01:23 +0200 | [diff] [blame] | 2044 | /* set module */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 2045 | type->pmod = ctx->parsed_mod; |
Michal Vasko | e9c050f | 2020-10-06 14:01:23 +0200 | [diff] [blame] | 2046 | |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2047 | YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2048 | switch (kw) { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2049 | case LY_STMT_BASE: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2050 | LY_CHECK_RET(parse_text_fields(ctx, LYEXT_SUBSTMT_BASE, &type->bases, Y_PREF_IDENTIF_ARG, &type->exts)); |
Radek Krejci | d505e3d | 2018-11-13 09:04:17 +0100 | [diff] [blame] | 2051 | type->flags |= LYS_SET_BASE; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2052 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2053 | case LY_STMT_BIT: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2054 | LY_CHECK_RET(parse_type_enum(ctx, kw, &type->bits)); |
Radek Krejci | d505e3d | 2018-11-13 09:04:17 +0100 | [diff] [blame] | 2055 | type->flags |= LYS_SET_BIT; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2056 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2057 | case LY_STMT_ENUM: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2058 | LY_CHECK_RET(parse_type_enum(ctx, kw, &type->enums)); |
Radek Krejci | d505e3d | 2018-11-13 09:04:17 +0100 | [diff] [blame] | 2059 | type->flags |= LYS_SET_ENUM; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2060 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2061 | case LY_STMT_FRACTION_DIGITS: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2062 | LY_CHECK_RET(parse_type_fracdigits(ctx, &type->fraction_digits, &type->exts)); |
Radek Krejci | d505e3d | 2018-11-13 09:04:17 +0100 | [diff] [blame] | 2063 | type->flags |= LYS_SET_FRDIGITS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2064 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2065 | case LY_STMT_LENGTH: |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2066 | if (type->length) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 2067 | LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2068 | return LY_EVALID; |
| 2069 | } |
| 2070 | type->length = calloc(1, sizeof *type->length); |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 2071 | LY_CHECK_ERR_RET(!type->length, LOGMEM(PARSER_CTX(ctx)), LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2072 | |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2073 | LY_CHECK_RET(parse_restr(ctx, kw, type->length)); |
Radek Krejci | d505e3d | 2018-11-13 09:04:17 +0100 | [diff] [blame] | 2074 | type->flags |= LYS_SET_LENGTH; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2075 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2076 | case LY_STMT_PATH: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2077 | if (type->path) { |
| 2078 | LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, lyext_substmt2str(LYEXT_SUBSTMT_PATH)); |
| 2079 | return LY_EVALID; |
| 2080 | } |
| 2081 | |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2082 | LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_PATH, 0, &str_path, Y_STR_ARG, &type->exts)); |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 2083 | 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] | 2084 | LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_LEAFREF, &type->path); |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 2085 | lydict_remove(PARSER_CTX(ctx), str_path); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2086 | LY_CHECK_RET(ret); |
Radek Krejci | d505e3d | 2018-11-13 09:04:17 +0100 | [diff] [blame] | 2087 | type->flags |= LYS_SET_PATH; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2088 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2089 | case LY_STMT_PATTERN: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2090 | LY_CHECK_RET(parse_type_pattern(ctx, &type->patterns)); |
Radek Krejci | d505e3d | 2018-11-13 09:04:17 +0100 | [diff] [blame] | 2091 | type->flags |= LYS_SET_PATTERN; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2092 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2093 | case LY_STMT_RANGE: |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2094 | if (type->range) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 2095 | LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2096 | return LY_EVALID; |
| 2097 | } |
| 2098 | type->range = calloc(1, sizeof *type->range); |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 2099 | LY_CHECK_ERR_RET(!type->range, LOGMEM(PARSER_CTX(ctx)), LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2100 | |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2101 | LY_CHECK_RET(parse_restr(ctx, kw, type->range)); |
Radek Krejci | d505e3d | 2018-11-13 09:04:17 +0100 | [diff] [blame] | 2102 | type->flags |= LYS_SET_RANGE; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2103 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2104 | case LY_STMT_REQUIRE_INSTANCE: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2105 | LY_CHECK_RET(parse_type_reqinstance(ctx, &type->require_instance, &type->flags, &type->exts)); |
Radek Krejci | d505e3d | 2018-11-13 09:04:17 +0100 | [diff] [blame] | 2106 | /* LYS_SET_REQINST checked and set inside parse_type_reqinstance() */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2107 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2108 | case LY_STMT_TYPE: |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 2109 | LY_ARRAY_NEW_RET(PARSER_CTX(ctx), type->types, nest_type, LY_EMEM); |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2110 | LY_CHECK_RET(parse_type(ctx, nest_type)); |
Radek Krejci | d505e3d | 2018-11-13 09:04:17 +0100 | [diff] [blame] | 2111 | type->flags |= LYS_SET_TYPE; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2112 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2113 | case LY_STMT_EXTENSION_INSTANCE: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2114 | LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &type->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2115 | break; |
| 2116 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 2117 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "type"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2118 | return LY_EVALID; |
| 2119 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2120 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2121 | return ret; |
| 2122 | } |
| 2123 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2124 | /** |
| 2125 | * @brief Parse the leaf statement. |
| 2126 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2127 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2128 | * @param[in,out] siblings Siblings to add to. |
| 2129 | * |
| 2130 | * @return LY_ERR values. |
| 2131 | */ |
Radek Krejci | 2d7a47b | 2019-05-16 13:34:10 +0200 | [diff] [blame] | 2132 | LY_ERR |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2133 | parse_leaf(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2134 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2135 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2136 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 2137 | size_t word_len; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2138 | enum ly_stmt kw; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2139 | struct lysp_node_leaf *leaf; |
| 2140 | |
David Sedlák | 60adc09 | 2019-08-06 15:57:02 +0200 | [diff] [blame] | 2141 | /* create new leaf structure */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 2142 | LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, leaf, next, LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2143 | leaf->nodetype = LYS_LEAF; |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2144 | leaf->parent = parent; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2145 | |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2146 | /* get name */ |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2147 | LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len)); |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 2148 | INSERT_WORD_RET(ctx, buf, leaf->name, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2149 | |
| 2150 | /* parse substatements */ |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2151 | YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2152 | switch (kw) { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2153 | case LY_STMT_CONFIG: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2154 | LY_CHECK_RET(parse_config(ctx, &leaf->flags, &leaf->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2155 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2156 | case LY_STMT_DEFAULT: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2157 | LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_DEFAULT, 0, &leaf->dflt.str, Y_STR_ARG, &leaf->exts)); |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 2158 | leaf->dflt.mod = ctx->parsed_mod; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2159 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2160 | case LY_STMT_DESCRIPTION: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2161 | LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, &leaf->dsc, Y_STR_ARG, &leaf->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2162 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2163 | case LY_STMT_IF_FEATURE: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2164 | LY_CHECK_RET(parse_qnames(ctx, LYEXT_SUBSTMT_IFFEATURE, &leaf->iffeatures, Y_STR_ARG, &leaf->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2165 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2166 | case LY_STMT_MANDATORY: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2167 | LY_CHECK_RET(parse_mandatory(ctx, &leaf->flags, &leaf->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2168 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2169 | case LY_STMT_MUST: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2170 | LY_CHECK_RET(parse_restrs(ctx, kw, &leaf->musts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2171 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2172 | case LY_STMT_REFERENCE: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2173 | LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_REFERENCE, 0, &leaf->ref, Y_STR_ARG, &leaf->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2174 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2175 | case LY_STMT_STATUS: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2176 | LY_CHECK_RET(parse_status(ctx, &leaf->flags, &leaf->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2177 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2178 | case LY_STMT_TYPE: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2179 | LY_CHECK_RET(parse_type(ctx, &leaf->type)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2180 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2181 | case LY_STMT_UNITS: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2182 | LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_UNITS, 0, &leaf->units, Y_STR_ARG, &leaf->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2183 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2184 | case LY_STMT_WHEN: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2185 | LY_CHECK_RET(parse_when(ctx, &leaf->when)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2186 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2187 | case LY_STMT_EXTENSION_INSTANCE: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2188 | LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &leaf->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2189 | break; |
| 2190 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 2191 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "leaf"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2192 | return LY_EVALID; |
| 2193 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2194 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 2195 | LY_CHECK_RET(ret); |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 2196 | checks: |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2197 | /* mandatory substatements */ |
| 2198 | if (!leaf->type.name) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 2199 | LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "leaf"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2200 | return LY_EVALID; |
| 2201 | } |
| 2202 | |
| 2203 | return ret; |
| 2204 | } |
| 2205 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2206 | /** |
| 2207 | * @brief Parse the max-elements statement. |
| 2208 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2209 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2210 | * @param[in,out] max Value to write to. |
| 2211 | * @param[in,out] flags Flags to write to. |
| 2212 | * @param[in,out] exts Extension instances to add to. |
| 2213 | * |
| 2214 | * @return LY_ERR values. |
| 2215 | */ |
Radek Krejci | 2d7a47b | 2019-05-16 13:34:10 +0200 | [diff] [blame] | 2216 | LY_ERR |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2217 | parse_maxelements(struct lys_yang_parser_ctx *ctx, uint32_t *max, uint16_t *flags, struct lysp_ext_instance **exts) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2218 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2219 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2220 | char *buf, *word, *ptr; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 2221 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2222 | unsigned long int num; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2223 | enum ly_stmt kw; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2224 | |
| 2225 | if (*flags & LYS_SET_MAX) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 2226 | LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "max-elements"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2227 | return LY_EVALID; |
| 2228 | } |
| 2229 | *flags |= LYS_SET_MAX; |
| 2230 | |
| 2231 | /* get value */ |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2232 | LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2233 | |
| 2234 | if (!word_len || (word[0] == '0') || ((word[0] != 'u') && !isdigit(word[0]))) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 2235 | LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "max-elements"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2236 | free(buf); |
| 2237 | return LY_EVALID; |
| 2238 | } |
| 2239 | |
Radek Krejci | 7f9b651 | 2019-09-18 13:11:09 +0200 | [diff] [blame] | 2240 | if (ly_strncmp("unbounded", word, word_len)) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2241 | errno = 0; |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 2242 | num = strtoul(word, &ptr, LY_BASE_DEC); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2243 | /* we have not parsed the whole argument */ |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 2244 | if ((size_t)(ptr - word) != word_len) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 2245 | LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "max-elements"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2246 | free(buf); |
| 2247 | return LY_EVALID; |
| 2248 | } |
| 2249 | if ((errno == ERANGE) || (num > UINT32_MAX)) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 2250 | LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, "max-elements"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2251 | free(buf); |
| 2252 | return LY_EVALID; |
| 2253 | } |
| 2254 | |
| 2255 | *max = num; |
| 2256 | } |
| 2257 | free(buf); |
| 2258 | |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2259 | YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2260 | switch (kw) { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2261 | case LY_STMT_EXTENSION_INSTANCE: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2262 | LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_MAX, 0, exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2263 | break; |
| 2264 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 2265 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "max-elements"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2266 | return LY_EVALID; |
| 2267 | } |
| 2268 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2269 | return ret; |
| 2270 | } |
| 2271 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2272 | /** |
| 2273 | * @brief Parse the min-elements statement. |
| 2274 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2275 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2276 | * @param[in,out] min Value to write to. |
| 2277 | * @param[in,out] flags Flags to write to. |
| 2278 | * @param[in,out] exts Extension instances to add to. |
| 2279 | * |
| 2280 | * @return LY_ERR values. |
| 2281 | */ |
Radek Krejci | 2d7a47b | 2019-05-16 13:34:10 +0200 | [diff] [blame] | 2282 | LY_ERR |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2283 | parse_minelements(struct lys_yang_parser_ctx *ctx, uint32_t *min, uint16_t *flags, struct lysp_ext_instance **exts) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2284 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2285 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2286 | char *buf, *word, *ptr; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 2287 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2288 | unsigned long int num; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2289 | enum ly_stmt kw; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2290 | |
| 2291 | if (*flags & LYS_SET_MIN) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 2292 | LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "min-elements"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2293 | return LY_EVALID; |
| 2294 | } |
| 2295 | *flags |= LYS_SET_MIN; |
| 2296 | |
| 2297 | /* get value */ |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2298 | LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2299 | |
| 2300 | if (!word_len || !isdigit(word[0]) || ((word[0] == '0') && (word_len > 1))) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 2301 | LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "min-elements"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2302 | free(buf); |
| 2303 | return LY_EVALID; |
| 2304 | } |
| 2305 | |
| 2306 | errno = 0; |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 2307 | num = strtoul(word, &ptr, LY_BASE_DEC); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2308 | /* we have not parsed the whole argument */ |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 2309 | if ((size_t)(ptr - word) != word_len) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 2310 | LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "min-elements"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2311 | free(buf); |
| 2312 | return LY_EVALID; |
| 2313 | } |
| 2314 | if ((errno == ERANGE) || (num > UINT32_MAX)) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 2315 | LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, "min-elements"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2316 | free(buf); |
| 2317 | return LY_EVALID; |
| 2318 | } |
| 2319 | *min = num; |
| 2320 | free(buf); |
| 2321 | |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2322 | YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2323 | switch (kw) { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2324 | case LY_STMT_EXTENSION_INSTANCE: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2325 | LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_MIN, 0, exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2326 | break; |
| 2327 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 2328 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "min-elements"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2329 | return LY_EVALID; |
| 2330 | } |
| 2331 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2332 | return ret; |
| 2333 | } |
| 2334 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2335 | /** |
| 2336 | * @brief Parse the ordered-by statement. |
| 2337 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2338 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2339 | * @param[in,out] flags Flags to write to. |
| 2340 | * @param[in,out] exts Extension instances to add to. |
| 2341 | * |
| 2342 | * @return LY_ERR values. |
| 2343 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2344 | static LY_ERR |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2345 | parse_orderedby(struct lys_yang_parser_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2346 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2347 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2348 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 2349 | size_t word_len; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2350 | enum ly_stmt kw; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2351 | |
| 2352 | if (*flags & LYS_ORDBY_MASK) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 2353 | LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "ordered-by"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2354 | return LY_EVALID; |
| 2355 | } |
| 2356 | |
| 2357 | /* get value */ |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2358 | LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2359 | |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 2360 | if ((word_len == ly_strlen_const("system")) && !strncmp(word, "system", word_len)) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2361 | *flags |= LYS_ORDBY_SYSTEM; |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 2362 | } else if ((word_len == ly_strlen_const("user")) && !strncmp(word, "user", word_len)) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2363 | *flags |= LYS_ORDBY_USER; |
| 2364 | } else { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 2365 | LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "ordered-by"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2366 | free(buf); |
| 2367 | return LY_EVALID; |
| 2368 | } |
| 2369 | free(buf); |
| 2370 | |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2371 | YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2372 | switch (kw) { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2373 | case LY_STMT_EXTENSION_INSTANCE: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2374 | LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_ORDEREDBY, 0, exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2375 | break; |
| 2376 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 2377 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "ordered-by"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2378 | return LY_EVALID; |
| 2379 | } |
| 2380 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2381 | return ret; |
| 2382 | } |
| 2383 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2384 | /** |
| 2385 | * @brief Parse the leaf-list statement. |
| 2386 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2387 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2388 | * @param[in,out] siblings Siblings to add to. |
| 2389 | * |
| 2390 | * @return LY_ERR values. |
| 2391 | */ |
Radek Krejci | 2d7a47b | 2019-05-16 13:34:10 +0200 | [diff] [blame] | 2392 | LY_ERR |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2393 | parse_leaflist(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2394 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2395 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2396 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 2397 | size_t word_len; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2398 | enum ly_stmt kw; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2399 | struct lysp_node_leaflist *llist; |
| 2400 | |
David Sedlák | 60adc09 | 2019-08-06 15:57:02 +0200 | [diff] [blame] | 2401 | /* create new leaf-list structure */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 2402 | LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, llist, next, LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2403 | llist->nodetype = LYS_LEAFLIST; |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2404 | llist->parent = parent; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2405 | |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2406 | /* get name */ |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2407 | LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len)); |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 2408 | INSERT_WORD_RET(ctx, buf, llist->name, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2409 | |
| 2410 | /* parse substatements */ |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2411 | YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2412 | switch (kw) { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2413 | case LY_STMT_CONFIG: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2414 | LY_CHECK_RET(parse_config(ctx, &llist->flags, &llist->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2415 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2416 | case LY_STMT_DEFAULT: |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 2417 | PARSER_CHECK_STMTVER2_RET(ctx, "default", "leaf-list"); |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2418 | LY_CHECK_RET(parse_qnames(ctx, LYEXT_SUBSTMT_DEFAULT, &llist->dflts, Y_STR_ARG, &llist->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2419 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2420 | case LY_STMT_DESCRIPTION: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2421 | LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, &llist->dsc, Y_STR_ARG, &llist->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2422 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2423 | case LY_STMT_IF_FEATURE: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2424 | LY_CHECK_RET(parse_qnames(ctx, LYEXT_SUBSTMT_IFFEATURE, &llist->iffeatures, Y_STR_ARG, &llist->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2425 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2426 | case LY_STMT_MAX_ELEMENTS: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2427 | LY_CHECK_RET(parse_maxelements(ctx, &llist->max, &llist->flags, &llist->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2428 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2429 | case LY_STMT_MIN_ELEMENTS: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2430 | LY_CHECK_RET(parse_minelements(ctx, &llist->min, &llist->flags, &llist->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2431 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2432 | case LY_STMT_MUST: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2433 | LY_CHECK_RET(parse_restrs(ctx, kw, &llist->musts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2434 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2435 | case LY_STMT_ORDERED_BY: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2436 | LY_CHECK_RET(parse_orderedby(ctx, &llist->flags, &llist->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2437 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2438 | case LY_STMT_REFERENCE: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2439 | LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_REFERENCE, 0, &llist->ref, Y_STR_ARG, &llist->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2440 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2441 | case LY_STMT_STATUS: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2442 | LY_CHECK_RET(parse_status(ctx, &llist->flags, &llist->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2443 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2444 | case LY_STMT_TYPE: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2445 | LY_CHECK_RET(parse_type(ctx, &llist->type)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2446 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2447 | case LY_STMT_UNITS: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2448 | LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_UNITS, 0, &llist->units, Y_STR_ARG, &llist->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2449 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2450 | case LY_STMT_WHEN: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2451 | LY_CHECK_RET(parse_when(ctx, &llist->when)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2452 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2453 | case LY_STMT_EXTENSION_INSTANCE: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2454 | LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &llist->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2455 | break; |
| 2456 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 2457 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "llist"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2458 | return LY_EVALID; |
| 2459 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2460 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 2461 | LY_CHECK_RET(ret); |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 2462 | checks: |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2463 | /* mandatory substatements */ |
| 2464 | if (!llist->type.name) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 2465 | LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "leaf-list"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2466 | return LY_EVALID; |
| 2467 | } |
| 2468 | |
| 2469 | return ret; |
| 2470 | } |
| 2471 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2472 | /** |
| 2473 | * @brief Parse the refine statement. |
| 2474 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2475 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2476 | * @param[in,out] refines Refines to add to. |
| 2477 | * |
| 2478 | * @return LY_ERR values. |
| 2479 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2480 | static LY_ERR |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2481 | parse_refine(struct lys_yang_parser_ctx *ctx, struct lysp_refine **refines) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2482 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2483 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2484 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 2485 | size_t word_len; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2486 | enum ly_stmt kw; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2487 | struct lysp_refine *rf; |
| 2488 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 2489 | LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *refines, rf, LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2490 | |
| 2491 | /* get value */ |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2492 | LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len)); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 2493 | CHECK_NONEMPTY(ctx, word_len, "refine"); |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 2494 | INSERT_WORD_RET(ctx, buf, rf->nodeid, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2495 | |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2496 | YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2497 | switch (kw) { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2498 | case LY_STMT_CONFIG: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2499 | LY_CHECK_RET(parse_config(ctx, &rf->flags, &rf->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2500 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2501 | case LY_STMT_DEFAULT: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2502 | LY_CHECK_RET(parse_qnames(ctx, LYEXT_SUBSTMT_DEFAULT, &rf->dflts, Y_STR_ARG, &rf->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2503 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2504 | case LY_STMT_DESCRIPTION: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2505 | LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, &rf->dsc, Y_STR_ARG, &rf->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2506 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2507 | case LY_STMT_IF_FEATURE: |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 2508 | PARSER_CHECK_STMTVER2_RET(ctx, "if-feature", "refine"); |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2509 | LY_CHECK_RET(parse_qnames(ctx, LYEXT_SUBSTMT_IFFEATURE, &rf->iffeatures, Y_STR_ARG, &rf->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2510 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2511 | case LY_STMT_MAX_ELEMENTS: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2512 | LY_CHECK_RET(parse_maxelements(ctx, &rf->max, &rf->flags, &rf->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2513 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2514 | case LY_STMT_MIN_ELEMENTS: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2515 | LY_CHECK_RET(parse_minelements(ctx, &rf->min, &rf->flags, &rf->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2516 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2517 | case LY_STMT_MUST: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2518 | LY_CHECK_RET(parse_restrs(ctx, kw, &rf->musts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2519 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2520 | case LY_STMT_MANDATORY: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2521 | LY_CHECK_RET(parse_mandatory(ctx, &rf->flags, &rf->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2522 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2523 | case LY_STMT_REFERENCE: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2524 | LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_REFERENCE, 0, &rf->ref, Y_STR_ARG, &rf->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2525 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2526 | case LY_STMT_PRESENCE: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2527 | LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_PRESENCE, 0, &rf->presence, Y_STR_ARG, &rf->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2528 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2529 | case LY_STMT_EXTENSION_INSTANCE: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2530 | LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &rf->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2531 | break; |
| 2532 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 2533 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "refine"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2534 | return LY_EVALID; |
| 2535 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2536 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2537 | return ret; |
| 2538 | } |
| 2539 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2540 | /** |
| 2541 | * @brief Parse the typedef statement. |
| 2542 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2543 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2544 | * @param[in,out] typedefs Typedefs to add to. |
| 2545 | * |
| 2546 | * @return LY_ERR values. |
| 2547 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2548 | static LY_ERR |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2549 | parse_typedef(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_tpdf **typedefs) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2550 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2551 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2552 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 2553 | size_t word_len; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2554 | enum ly_stmt kw; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2555 | struct lysp_tpdf *tpdf; |
| 2556 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 2557 | LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *typedefs, tpdf, LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2558 | |
| 2559 | /* get value */ |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2560 | LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len)); |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 2561 | INSERT_WORD_RET(ctx, buf, tpdf->name, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2562 | |
| 2563 | /* parse substatements */ |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2564 | YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2565 | switch (kw) { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2566 | case LY_STMT_DEFAULT: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2567 | LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_DEFAULT, 0, &tpdf->dflt.str, Y_STR_ARG, &tpdf->exts)); |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 2568 | tpdf->dflt.mod = ctx->parsed_mod; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2569 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2570 | case LY_STMT_DESCRIPTION: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2571 | LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, &tpdf->dsc, Y_STR_ARG, &tpdf->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2572 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2573 | case LY_STMT_REFERENCE: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2574 | LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_REFERENCE, 0, &tpdf->ref, Y_STR_ARG, &tpdf->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2575 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2576 | case LY_STMT_STATUS: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2577 | LY_CHECK_RET(parse_status(ctx, &tpdf->flags, &tpdf->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2578 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2579 | case LY_STMT_TYPE: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2580 | LY_CHECK_RET(parse_type(ctx, &tpdf->type)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2581 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2582 | case LY_STMT_UNITS: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2583 | LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_UNITS, 0, &tpdf->units, Y_STR_ARG, &tpdf->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2584 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2585 | case LY_STMT_EXTENSION_INSTANCE: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2586 | LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &tpdf->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2587 | break; |
| 2588 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 2589 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "typedef"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2590 | return LY_EVALID; |
| 2591 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2592 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 2593 | LY_CHECK_RET(ret); |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 2594 | checks: |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2595 | /* mandatory substatements */ |
| 2596 | if (!tpdf->type.name) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 2597 | LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "typedef"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2598 | return LY_EVALID; |
| 2599 | } |
| 2600 | |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 2601 | /* store data for collision check */ |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 2602 | if (parent && !(parent->nodetype & (LYS_GROUPING | LYS_RPC | LYS_ACTION | LYS_INPUT | LYS_OUTPUT | LYS_NOTIF))) { |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 2603 | LY_CHECK_RET(ly_set_add(&ctx->tpdfs_nodes, parent, 0, NULL)); |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 2604 | } |
| 2605 | |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2606 | return ret; |
| 2607 | } |
| 2608 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2609 | /** |
| 2610 | * @brief Parse the input or output statement. |
| 2611 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2612 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2613 | * @param[in] kw Type of this particular keyword |
| 2614 | * @param[in,out] inout_p Input/output pointer to write to. |
| 2615 | * |
| 2616 | * @return LY_ERR values. |
| 2617 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2618 | static LY_ERR |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2619 | parse_inout(struct lys_yang_parser_ctx *ctx, enum ly_stmt inout_kw, struct lysp_node *parent, |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 2620 | struct lysp_action_inout *inout_p) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2621 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2622 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2623 | char *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 2624 | size_t word_len; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2625 | enum ly_stmt kw; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2626 | |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 2627 | if (inout_p->nodetype) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 2628 | LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(inout_kw)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2629 | return LY_EVALID; |
| 2630 | } |
| 2631 | |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 2632 | /* initiate structure */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2633 | inout_p->nodetype = &((struct lysp_action *)parent)->input == inout_p ? LYS_INPUT : LYS_OUTPUT; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 2634 | inout_p->parent = parent; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2635 | |
| 2636 | /* parse substatements */ |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2637 | YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2638 | switch (kw) { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2639 | case LY_STMT_ANYDATA: |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 2640 | PARSER_CHECK_STMTVER2_RET(ctx, "anydata", ly_stmt2str(inout_kw)); |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 2641 | /* fall through */ |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2642 | case LY_STMT_ANYXML: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2643 | LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)inout_p, &inout_p->data)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2644 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2645 | case LY_STMT_CHOICE: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2646 | LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)inout_p, &inout_p->data)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2647 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2648 | case LY_STMT_CONTAINER: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2649 | LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)inout_p, &inout_p->data)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2650 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2651 | case LY_STMT_LEAF: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2652 | LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)inout_p, &inout_p->data)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2653 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2654 | case LY_STMT_LEAF_LIST: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2655 | LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)inout_p, &inout_p->data)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2656 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2657 | case LY_STMT_LIST: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2658 | LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)inout_p, &inout_p->data)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2659 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2660 | case LY_STMT_USES: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2661 | LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)inout_p, &inout_p->data)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2662 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2663 | case LY_STMT_TYPEDEF: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2664 | LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)inout_p, &inout_p->typedefs)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2665 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2666 | case LY_STMT_MUST: |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 2667 | PARSER_CHECK_STMTVER2_RET(ctx, "must", ly_stmt2str(inout_kw)); |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2668 | LY_CHECK_RET(parse_restrs(ctx, kw, &inout_p->musts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2669 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2670 | case LY_STMT_GROUPING: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2671 | LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)inout_p, &inout_p->groupings)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2672 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2673 | case LY_STMT_EXTENSION_INSTANCE: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2674 | LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &inout_p->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2675 | break; |
| 2676 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 2677 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(inout_kw)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2678 | return LY_EVALID; |
| 2679 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2680 | } |
Radek Krejci | 7fc6829 | 2019-06-12 13:51:09 +0200 | [diff] [blame] | 2681 | LY_CHECK_RET(ret); |
Michal Vasko | b83af8a | 2020-01-06 09:49:22 +0100 | [diff] [blame] | 2682 | |
Radek Krejci | 7fc6829 | 2019-06-12 13:51:09 +0200 | [diff] [blame] | 2683 | checks: |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 2684 | /* finalize parent pointers to the reallocated items */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 2685 | LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, inout_p->groupings, NULL, NULL, NULL)); |
Radek Krejci | 7fc6829 | 2019-06-12 13:51:09 +0200 | [diff] [blame] | 2686 | |
Michal Vasko | b83af8a | 2020-01-06 09:49:22 +0100 | [diff] [blame] | 2687 | if (!inout_p->data) { |
| 2688 | LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "data-def-stmt", ly_stmt2str(inout_kw)); |
| 2689 | return LY_EVALID; |
| 2690 | } |
| 2691 | |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2692 | return ret; |
| 2693 | } |
| 2694 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2695 | /** |
| 2696 | * @brief Parse the action statement. |
| 2697 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2698 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2699 | * @param[in,out] actions Actions to add to. |
| 2700 | * |
| 2701 | * @return LY_ERR values. |
| 2702 | */ |
Radek Krejci | 2d7a47b | 2019-05-16 13:34:10 +0200 | [diff] [blame] | 2703 | LY_ERR |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2704 | parse_action(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_action **actions) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2705 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2706 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2707 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 2708 | size_t word_len; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2709 | enum ly_stmt kw; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2710 | struct lysp_action *act; |
| 2711 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 2712 | LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *actions, act, LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2713 | |
| 2714 | /* get value */ |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2715 | LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len)); |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 2716 | INSERT_WORD_RET(ctx, buf, act->name, word, word_len); |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 2717 | act->nodetype = parent ? LYS_ACTION : LYS_RPC; |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2718 | act->parent = parent; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2719 | |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2720 | YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2721 | switch (kw) { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2722 | case LY_STMT_DESCRIPTION: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2723 | LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, &act->dsc, Y_STR_ARG, &act->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2724 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2725 | case LY_STMT_IF_FEATURE: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2726 | LY_CHECK_RET(parse_qnames(ctx, LYEXT_SUBSTMT_IFFEATURE, &act->iffeatures, Y_STR_ARG, &act->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2727 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2728 | case LY_STMT_REFERENCE: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2729 | LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_REFERENCE, 0, &act->ref, Y_STR_ARG, &act->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2730 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2731 | case LY_STMT_STATUS: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2732 | LY_CHECK_RET(parse_status(ctx, &act->flags, &act->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2733 | break; |
| 2734 | |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2735 | case LY_STMT_INPUT: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2736 | LY_CHECK_RET(parse_inout(ctx, kw, (struct lysp_node *)act, &act->input)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2737 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2738 | case LY_STMT_OUTPUT: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2739 | LY_CHECK_RET(parse_inout(ctx, kw, (struct lysp_node *)act, &act->output)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2740 | break; |
| 2741 | |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2742 | case LY_STMT_TYPEDEF: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2743 | LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)act, &act->typedefs)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2744 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2745 | case LY_STMT_GROUPING: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2746 | LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)act, &act->groupings)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2747 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2748 | case LY_STMT_EXTENSION_INSTANCE: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2749 | LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &act->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2750 | break; |
| 2751 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 2752 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), parent ? "action" : "rpc"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2753 | return LY_EVALID; |
| 2754 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2755 | } |
Radek Krejci | 7fc6829 | 2019-06-12 13:51:09 +0200 | [diff] [blame] | 2756 | LY_CHECK_RET(ret); |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 2757 | |
| 2758 | /* always initialize inout, they are technically present (needed for later deviations/refines) */ |
| 2759 | if (!act->input.nodetype) { |
| 2760 | act->input.nodetype = LYS_INPUT; |
| 2761 | act->input.parent = (struct lysp_node *)act; |
| 2762 | } |
| 2763 | if (!act->output.nodetype) { |
| 2764 | act->output.nodetype = LYS_OUTPUT; |
| 2765 | act->output.parent = (struct lysp_node *)act; |
| 2766 | } |
| 2767 | |
Radek Krejci | 7fc6829 | 2019-06-12 13:51:09 +0200 | [diff] [blame] | 2768 | checks: |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 2769 | /* finalize parent pointers to the reallocated items */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 2770 | LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, act->groupings, NULL, NULL, NULL)); |
Radek Krejci | 7fc6829 | 2019-06-12 13:51:09 +0200 | [diff] [blame] | 2771 | |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2772 | return ret; |
| 2773 | } |
| 2774 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2775 | /** |
| 2776 | * @brief Parse the notification statement. |
| 2777 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2778 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2779 | * @param[in,out] notifs Notifications to add to. |
| 2780 | * |
| 2781 | * @return LY_ERR values. |
| 2782 | */ |
Radek Krejci | 2d7a47b | 2019-05-16 13:34:10 +0200 | [diff] [blame] | 2783 | LY_ERR |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2784 | parse_notif(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_notif **notifs) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2785 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2786 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2787 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 2788 | size_t word_len; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2789 | enum ly_stmt kw; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2790 | struct lysp_notif *notif; |
| 2791 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 2792 | LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *notifs, notif, LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2793 | |
| 2794 | /* get value */ |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2795 | LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len)); |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 2796 | INSERT_WORD_RET(ctx, buf, notif->name, word, word_len); |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2797 | notif->nodetype = LYS_NOTIF; |
| 2798 | notif->parent = parent; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2799 | |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2800 | YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2801 | switch (kw) { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2802 | case LY_STMT_DESCRIPTION: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2803 | LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, ¬if->dsc, Y_STR_ARG, ¬if->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2804 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2805 | case LY_STMT_IF_FEATURE: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2806 | LY_CHECK_RET(parse_qnames(ctx, LYEXT_SUBSTMT_IFFEATURE, ¬if->iffeatures, Y_STR_ARG, ¬if->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2807 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2808 | case LY_STMT_REFERENCE: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2809 | LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_REFERENCE, 0, ¬if->ref, Y_STR_ARG, ¬if->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2810 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2811 | case LY_STMT_STATUS: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2812 | LY_CHECK_RET(parse_status(ctx, ¬if->flags, ¬if->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2813 | break; |
| 2814 | |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2815 | case LY_STMT_ANYDATA: |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 2816 | PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "notification"); |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 2817 | /* fall through */ |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2818 | case LY_STMT_ANYXML: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2819 | LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)notif, ¬if->data)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2820 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2821 | case LY_STMT_CHOICE: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2822 | LY_CHECK_RET(parse_case(ctx, (struct lysp_node *)notif, ¬if->data)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2823 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2824 | case LY_STMT_CONTAINER: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2825 | LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)notif, ¬if->data)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2826 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2827 | case LY_STMT_LEAF: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2828 | LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)notif, ¬if->data)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2829 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2830 | case LY_STMT_LEAF_LIST: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2831 | LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)notif, ¬if->data)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2832 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2833 | case LY_STMT_LIST: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2834 | LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)notif, ¬if->data)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2835 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2836 | case LY_STMT_USES: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2837 | LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)notif, ¬if->data)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2838 | break; |
| 2839 | |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2840 | case LY_STMT_MUST: |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 2841 | PARSER_CHECK_STMTVER2_RET(ctx, "must", "notification"); |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2842 | LY_CHECK_RET(parse_restrs(ctx, kw, ¬if->musts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2843 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2844 | case LY_STMT_TYPEDEF: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2845 | LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)notif, ¬if->typedefs)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2846 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2847 | case LY_STMT_GROUPING: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2848 | LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)notif, ¬if->groupings)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2849 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2850 | case LY_STMT_EXTENSION_INSTANCE: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2851 | LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, ¬if->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2852 | break; |
| 2853 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 2854 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "notification"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2855 | return LY_EVALID; |
| 2856 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2857 | } |
Radek Krejci | 7fc6829 | 2019-06-12 13:51:09 +0200 | [diff] [blame] | 2858 | LY_CHECK_RET(ret); |
| 2859 | checks: |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 2860 | /* finalize parent pointers to the reallocated items */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 2861 | LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, notif->groupings, NULL, NULL, NULL)); |
Radek Krejci | 7fc6829 | 2019-06-12 13:51:09 +0200 | [diff] [blame] | 2862 | |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2863 | return ret; |
| 2864 | } |
| 2865 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2866 | /** |
| 2867 | * @brief Parse the grouping statement. |
| 2868 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2869 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2870 | * @param[in,out] groupings Groupings to add to. |
| 2871 | * |
| 2872 | * @return LY_ERR values. |
| 2873 | */ |
Radek Krejci | 2d7a47b | 2019-05-16 13:34:10 +0200 | [diff] [blame] | 2874 | LY_ERR |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2875 | parse_grouping(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_grp **groupings) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2876 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2877 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2878 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 2879 | size_t word_len; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2880 | enum ly_stmt kw; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2881 | struct lysp_grp *grp; |
| 2882 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 2883 | LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *groupings, grp, LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2884 | |
| 2885 | /* get value */ |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2886 | LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len)); |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 2887 | INSERT_WORD_RET(ctx, buf, grp->name, word, word_len); |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2888 | grp->nodetype = LYS_GROUPING; |
| 2889 | grp->parent = parent; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2890 | |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2891 | YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2892 | switch (kw) { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2893 | case LY_STMT_DESCRIPTION: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2894 | LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, &grp->dsc, Y_STR_ARG, &grp->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2895 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2896 | case LY_STMT_REFERENCE: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2897 | LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_REFERENCE, 0, &grp->ref, Y_STR_ARG, &grp->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2898 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2899 | case LY_STMT_STATUS: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2900 | LY_CHECK_RET(parse_status(ctx, &grp->flags, &grp->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2901 | break; |
| 2902 | |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2903 | case LY_STMT_ANYDATA: |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 2904 | PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "grouping"); |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 2905 | /* fall through */ |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2906 | case LY_STMT_ANYXML: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2907 | LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)grp, &grp->data)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2908 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2909 | case LY_STMT_CHOICE: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2910 | LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)grp, &grp->data)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2911 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2912 | case LY_STMT_CONTAINER: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2913 | LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)grp, &grp->data)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2914 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2915 | case LY_STMT_LEAF: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2916 | LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)grp, &grp->data)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2917 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2918 | case LY_STMT_LEAF_LIST: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2919 | LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)grp, &grp->data)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2920 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2921 | case LY_STMT_LIST: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2922 | LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)grp, &grp->data)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2923 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2924 | case LY_STMT_USES: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2925 | LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)grp, &grp->data)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2926 | break; |
| 2927 | |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2928 | case LY_STMT_TYPEDEF: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2929 | LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)grp, &grp->typedefs)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2930 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2931 | case LY_STMT_ACTION: |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 2932 | PARSER_CHECK_STMTVER2_RET(ctx, "action", "grouping"); |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2933 | LY_CHECK_RET(parse_action(ctx, (struct lysp_node *)grp, &grp->actions)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2934 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2935 | case LY_STMT_GROUPING: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2936 | LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)grp, &grp->groupings)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2937 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2938 | case LY_STMT_NOTIFICATION: |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 2939 | PARSER_CHECK_STMTVER2_RET(ctx, "notification", "grouping"); |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2940 | LY_CHECK_RET(parse_notif(ctx, (struct lysp_node *)grp, &grp->notifs)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2941 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2942 | case LY_STMT_EXTENSION_INSTANCE: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2943 | LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &grp->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2944 | break; |
| 2945 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 2946 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "grouping"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2947 | return LY_EVALID; |
| 2948 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2949 | } |
Radek Krejci | 7fc6829 | 2019-06-12 13:51:09 +0200 | [diff] [blame] | 2950 | LY_CHECK_RET(ret); |
| 2951 | checks: |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 2952 | /* finalize parent pointers to the reallocated items */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 2953 | LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, grp->groupings, NULL, grp->actions, grp->notifs)); |
Radek Krejci | 7fc6829 | 2019-06-12 13:51:09 +0200 | [diff] [blame] | 2954 | |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2955 | return ret; |
| 2956 | } |
| 2957 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2958 | /** |
David Sedlák | 0d6de5a | 2019-07-22 13:25:44 +0200 | [diff] [blame] | 2959 | * @brief Parse the augment statement. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2960 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2961 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2962 | * @param[in,out] augments Augments to add to. |
| 2963 | * |
| 2964 | * @return LY_ERR values. |
| 2965 | */ |
Radek Krejci | 2d7a47b | 2019-05-16 13:34:10 +0200 | [diff] [blame] | 2966 | LY_ERR |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2967 | parse_augment(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_augment **augments) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2968 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2969 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2970 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 2971 | size_t word_len; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2972 | enum ly_stmt kw; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2973 | struct lysp_augment *aug; |
| 2974 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 2975 | LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *augments, aug, LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2976 | |
| 2977 | /* get value */ |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2978 | LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len)); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 2979 | CHECK_NONEMPTY(ctx, word_len, "augment"); |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 2980 | INSERT_WORD_RET(ctx, buf, aug->nodeid, word, word_len); |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2981 | aug->nodetype = LYS_AUGMENT; |
| 2982 | aug->parent = parent; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2983 | |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2984 | YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2985 | switch (kw) { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2986 | case LY_STMT_DESCRIPTION: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2987 | LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, &aug->dsc, Y_STR_ARG, &aug->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2988 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2989 | case LY_STMT_IF_FEATURE: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2990 | LY_CHECK_RET(parse_qnames(ctx, LYEXT_SUBSTMT_IFFEATURE, &aug->iffeatures, Y_STR_ARG, &aug->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2991 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2992 | case LY_STMT_REFERENCE: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2993 | LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_REFERENCE, 0, &aug->ref, Y_STR_ARG, &aug->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2994 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2995 | case LY_STMT_STATUS: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2996 | LY_CHECK_RET(parse_status(ctx, &aug->flags, &aug->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2997 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 2998 | case LY_STMT_WHEN: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 2999 | LY_CHECK_RET(parse_when(ctx, &aug->when)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3000 | break; |
| 3001 | |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3002 | case LY_STMT_ANYDATA: |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 3003 | PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "augment"); |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 3004 | /* fall through */ |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3005 | case LY_STMT_ANYXML: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3006 | LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)aug, &aug->child)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3007 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3008 | case LY_STMT_CASE: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3009 | LY_CHECK_RET(parse_case(ctx, (struct lysp_node *)aug, &aug->child)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3010 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3011 | case LY_STMT_CHOICE: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3012 | LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)aug, &aug->child)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3013 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3014 | case LY_STMT_CONTAINER: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3015 | LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)aug, &aug->child)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3016 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3017 | case LY_STMT_LEAF: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3018 | LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)aug, &aug->child)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3019 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3020 | case LY_STMT_LEAF_LIST: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3021 | LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)aug, &aug->child)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3022 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3023 | case LY_STMT_LIST: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3024 | LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)aug, &aug->child)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3025 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3026 | case LY_STMT_USES: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3027 | LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)aug, &aug->child)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3028 | break; |
| 3029 | |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3030 | case LY_STMT_ACTION: |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 3031 | PARSER_CHECK_STMTVER2_RET(ctx, "action", "augment"); |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3032 | LY_CHECK_RET(parse_action(ctx, (struct lysp_node *)aug, &aug->actions)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3033 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3034 | case LY_STMT_NOTIFICATION: |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 3035 | PARSER_CHECK_STMTVER2_RET(ctx, "notification", "augment"); |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3036 | LY_CHECK_RET(parse_notif(ctx, (struct lysp_node *)aug, &aug->notifs)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3037 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3038 | case LY_STMT_EXTENSION_INSTANCE: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3039 | LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &aug->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3040 | break; |
| 3041 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 3042 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "augment"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3043 | return LY_EVALID; |
| 3044 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3045 | } |
Radek Krejci | 7fc6829 | 2019-06-12 13:51:09 +0200 | [diff] [blame] | 3046 | LY_CHECK_RET(ret); |
| 3047 | checks: |
| 3048 | /* finalize parent pointers to the reallocated items */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 3049 | LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, NULL, NULL, aug->actions, aug->notifs)); |
Radek Krejci | 7fc6829 | 2019-06-12 13:51:09 +0200 | [diff] [blame] | 3050 | |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3051 | return ret; |
| 3052 | } |
| 3053 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3054 | /** |
| 3055 | * @brief Parse the uses statement. |
| 3056 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3057 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3058 | * @param[in,out] siblings Siblings to add to. |
| 3059 | * |
| 3060 | * @return LY_ERR values. |
| 3061 | */ |
Radek Krejci | 2d7a47b | 2019-05-16 13:34:10 +0200 | [diff] [blame] | 3062 | LY_ERR |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3063 | parse_uses(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3064 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3065 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3066 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 3067 | size_t word_len; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3068 | enum ly_stmt kw; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3069 | struct lysp_node_uses *uses; |
| 3070 | |
David Sedlák | 60adc09 | 2019-08-06 15:57:02 +0200 | [diff] [blame] | 3071 | /* create uses structure */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 3072 | LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, uses, next, LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3073 | uses->nodetype = LYS_USES; |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3074 | uses->parent = parent; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3075 | |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3076 | /* get name */ |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3077 | LY_CHECK_RET(get_argument(ctx, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &word_len)); |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 3078 | INSERT_WORD_RET(ctx, buf, uses->name, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3079 | |
| 3080 | /* parse substatements */ |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3081 | YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3082 | switch (kw) { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3083 | case LY_STMT_DESCRIPTION: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3084 | LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, &uses->dsc, Y_STR_ARG, &uses->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3085 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3086 | case LY_STMT_IF_FEATURE: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3087 | LY_CHECK_RET(parse_qnames(ctx, LYEXT_SUBSTMT_IFFEATURE, &uses->iffeatures, Y_STR_ARG, &uses->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3088 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3089 | case LY_STMT_REFERENCE: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3090 | LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_REFERENCE, 0, &uses->ref, Y_STR_ARG, &uses->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3091 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3092 | case LY_STMT_STATUS: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3093 | LY_CHECK_RET(parse_status(ctx, &uses->flags, &uses->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3094 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3095 | case LY_STMT_WHEN: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3096 | LY_CHECK_RET(parse_when(ctx, &uses->when)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3097 | break; |
| 3098 | |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3099 | case LY_STMT_REFINE: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3100 | LY_CHECK_RET(parse_refine(ctx, &uses->refines)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3101 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3102 | case LY_STMT_AUGMENT: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3103 | LY_CHECK_RET(parse_augment(ctx, (struct lysp_node *)uses, &uses->augments)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3104 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3105 | case LY_STMT_EXTENSION_INSTANCE: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3106 | LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &uses->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3107 | break; |
| 3108 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 3109 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "uses"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3110 | return LY_EVALID; |
| 3111 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3112 | } |
Radek Krejci | 7fc6829 | 2019-06-12 13:51:09 +0200 | [diff] [blame] | 3113 | checks: |
| 3114 | /* finalize parent pointers to the reallocated items */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 3115 | LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, NULL, uses->augments, NULL, NULL)); |
Radek Krejci | 7fc6829 | 2019-06-12 13:51:09 +0200 | [diff] [blame] | 3116 | |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3117 | return ret; |
| 3118 | } |
| 3119 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3120 | /** |
| 3121 | * @brief Parse the case statement. |
| 3122 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3123 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3124 | * @param[in,out] siblings Siblings to add to. |
| 3125 | * |
| 3126 | * @return LY_ERR values. |
| 3127 | */ |
Radek Krejci | 2d7a47b | 2019-05-16 13:34:10 +0200 | [diff] [blame] | 3128 | LY_ERR |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3129 | parse_case(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3130 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3131 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3132 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 3133 | size_t word_len; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3134 | enum ly_stmt kw; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3135 | struct lysp_node_case *cas; |
| 3136 | |
David Sedlák | 60adc09 | 2019-08-06 15:57:02 +0200 | [diff] [blame] | 3137 | /* create new case structure */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 3138 | LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, cas, next, LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3139 | cas->nodetype = LYS_CASE; |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3140 | cas->parent = parent; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3141 | |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3142 | /* get name */ |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3143 | LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len)); |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 3144 | INSERT_WORD_RET(ctx, buf, cas->name, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3145 | |
| 3146 | /* parse substatements */ |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3147 | YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3148 | switch (kw) { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3149 | case LY_STMT_DESCRIPTION: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3150 | LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, &cas->dsc, Y_STR_ARG, &cas->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3151 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3152 | case LY_STMT_IF_FEATURE: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3153 | LY_CHECK_RET(parse_qnames(ctx, LYEXT_SUBSTMT_IFFEATURE, &cas->iffeatures, Y_STR_ARG, &cas->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3154 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3155 | case LY_STMT_REFERENCE: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3156 | LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_REFERENCE, 0, &cas->ref, Y_STR_ARG, &cas->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3157 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3158 | case LY_STMT_STATUS: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3159 | LY_CHECK_RET(parse_status(ctx, &cas->flags, &cas->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3160 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3161 | case LY_STMT_WHEN: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3162 | LY_CHECK_RET(parse_when(ctx, &cas->when)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3163 | break; |
| 3164 | |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3165 | case LY_STMT_ANYDATA: |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 3166 | PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "case"); |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 3167 | /* fall through */ |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3168 | case LY_STMT_ANYXML: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3169 | LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)cas, &cas->child)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3170 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3171 | case LY_STMT_CHOICE: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3172 | LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)cas, &cas->child)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3173 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3174 | case LY_STMT_CONTAINER: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3175 | LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)cas, &cas->child)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3176 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3177 | case LY_STMT_LEAF: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3178 | LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)cas, &cas->child)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3179 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3180 | case LY_STMT_LEAF_LIST: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3181 | LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)cas, &cas->child)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3182 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3183 | case LY_STMT_LIST: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3184 | LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)cas, &cas->child)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3185 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3186 | case LY_STMT_USES: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3187 | LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)cas, &cas->child)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3188 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3189 | case LY_STMT_EXTENSION_INSTANCE: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3190 | LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &cas->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3191 | break; |
| 3192 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 3193 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "case"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3194 | return LY_EVALID; |
| 3195 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3196 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3197 | return ret; |
| 3198 | } |
| 3199 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3200 | /** |
| 3201 | * @brief Parse the choice statement. |
| 3202 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3203 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3204 | * @param[in,out] siblings Siblings to add to. |
| 3205 | * |
| 3206 | * @return LY_ERR values. |
| 3207 | */ |
Radek Krejci | 2d7a47b | 2019-05-16 13:34:10 +0200 | [diff] [blame] | 3208 | LY_ERR |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3209 | parse_choice(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3210 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3211 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3212 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 3213 | size_t word_len; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3214 | enum ly_stmt kw; |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3215 | struct lysp_node_choice *choice; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3216 | |
David Sedlák | 60adc09 | 2019-08-06 15:57:02 +0200 | [diff] [blame] | 3217 | /* create new choice structure */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 3218 | LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, choice, next, LY_EMEM); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3219 | choice->nodetype = LYS_CHOICE; |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3220 | choice->parent = parent; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3221 | |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3222 | /* get name */ |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3223 | LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len)); |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 3224 | INSERT_WORD_RET(ctx, buf, choice->name, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3225 | |
| 3226 | /* parse substatements */ |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3227 | YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3228 | switch (kw) { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3229 | case LY_STMT_CONFIG: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3230 | LY_CHECK_RET(parse_config(ctx, &choice->flags, &choice->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3231 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3232 | case LY_STMT_DESCRIPTION: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3233 | LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, &choice->dsc, Y_STR_ARG, &choice->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3234 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3235 | case LY_STMT_IF_FEATURE: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3236 | LY_CHECK_RET(parse_qnames(ctx, LYEXT_SUBSTMT_IFFEATURE, &choice->iffeatures, Y_STR_ARG, &choice->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3237 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3238 | case LY_STMT_MANDATORY: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3239 | LY_CHECK_RET(parse_mandatory(ctx, &choice->flags, &choice->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3240 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3241 | case LY_STMT_REFERENCE: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3242 | LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_REFERENCE, 0, &choice->ref, Y_STR_ARG, &choice->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3243 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3244 | case LY_STMT_STATUS: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3245 | LY_CHECK_RET(parse_status(ctx, &choice->flags, &choice->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3246 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3247 | case LY_STMT_WHEN: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3248 | LY_CHECK_RET(parse_when(ctx, &choice->when)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3249 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3250 | case LY_STMT_DEFAULT: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3251 | LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_DEFAULT, 0, &choice->dflt.str, Y_PREF_IDENTIF_ARG, |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 3252 | &choice->exts)); |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 3253 | choice->dflt.mod = ctx->parsed_mod; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3254 | break; |
| 3255 | |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3256 | case LY_STMT_ANYDATA: |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 3257 | PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "choice"); |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 3258 | /* fall through */ |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3259 | case LY_STMT_ANYXML: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3260 | LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)choice, &choice->child)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3261 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3262 | case LY_STMT_CASE: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3263 | LY_CHECK_RET(parse_case(ctx, (struct lysp_node *)choice, &choice->child)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3264 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3265 | case LY_STMT_CHOICE: |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 3266 | PARSER_CHECK_STMTVER2_RET(ctx, "choice", "choice"); |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3267 | LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)choice, &choice->child)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3268 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3269 | case LY_STMT_CONTAINER: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3270 | LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)choice, &choice->child)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3271 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3272 | case LY_STMT_LEAF: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3273 | LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)choice, &choice->child)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3274 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3275 | case LY_STMT_LEAF_LIST: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3276 | LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)choice, &choice->child)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3277 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3278 | case LY_STMT_LIST: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3279 | LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)choice, &choice->child)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3280 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3281 | case LY_STMT_EXTENSION_INSTANCE: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3282 | LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &choice->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3283 | break; |
| 3284 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 3285 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "choice"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3286 | return LY_EVALID; |
| 3287 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3288 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3289 | return ret; |
| 3290 | } |
| 3291 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3292 | /** |
| 3293 | * @brief Parse the container statement. |
| 3294 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3295 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3296 | * @param[in,out] siblings Siblings to add to. |
| 3297 | * |
| 3298 | * @return LY_ERR values. |
| 3299 | */ |
Radek Krejci | 2d7a47b | 2019-05-16 13:34:10 +0200 | [diff] [blame] | 3300 | LY_ERR |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3301 | parse_container(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3302 | { |
| 3303 | LY_ERR ret = 0; |
| 3304 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 3305 | size_t word_len; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3306 | enum ly_stmt kw; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3307 | struct lysp_node_container *cont; |
| 3308 | |
David Sedlák | 60adc09 | 2019-08-06 15:57:02 +0200 | [diff] [blame] | 3309 | /* create new container structure */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 3310 | LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, cont, next, LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3311 | cont->nodetype = LYS_CONTAINER; |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3312 | cont->parent = parent; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3313 | |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3314 | /* get name */ |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3315 | LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len)); |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 3316 | INSERT_WORD_RET(ctx, buf, cont->name, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3317 | |
| 3318 | /* parse substatements */ |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3319 | YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3320 | switch (kw) { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3321 | case LY_STMT_CONFIG: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3322 | LY_CHECK_RET(parse_config(ctx, &cont->flags, &cont->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3323 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3324 | case LY_STMT_DESCRIPTION: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3325 | LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, &cont->dsc, Y_STR_ARG, &cont->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3326 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3327 | case LY_STMT_IF_FEATURE: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3328 | LY_CHECK_RET(parse_qnames(ctx, LYEXT_SUBSTMT_IFFEATURE, &cont->iffeatures, Y_STR_ARG, &cont->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3329 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3330 | case LY_STMT_REFERENCE: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3331 | LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_REFERENCE, 0, &cont->ref, Y_STR_ARG, &cont->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3332 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3333 | case LY_STMT_STATUS: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3334 | LY_CHECK_RET(parse_status(ctx, &cont->flags, &cont->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3335 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3336 | case LY_STMT_WHEN: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3337 | LY_CHECK_RET(parse_when(ctx, &cont->when)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3338 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3339 | case LY_STMT_PRESENCE: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3340 | LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_PRESENCE, 0, &cont->presence, Y_STR_ARG, &cont->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3341 | break; |
| 3342 | |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3343 | case LY_STMT_ANYDATA: |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 3344 | PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "container"); |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 3345 | /* fall through */ |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3346 | case LY_STMT_ANYXML: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3347 | LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)cont, &cont->child)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3348 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3349 | case LY_STMT_CHOICE: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3350 | LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)cont, &cont->child)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3351 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3352 | case LY_STMT_CONTAINER: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3353 | LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)cont, &cont->child)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3354 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3355 | case LY_STMT_LEAF: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3356 | LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)cont, &cont->child)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3357 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3358 | case LY_STMT_LEAF_LIST: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3359 | LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)cont, &cont->child)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3360 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3361 | case LY_STMT_LIST: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3362 | LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)cont, &cont->child)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3363 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3364 | case LY_STMT_USES: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3365 | LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)cont, &cont->child)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3366 | break; |
| 3367 | |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3368 | case LY_STMT_TYPEDEF: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3369 | LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)cont, &cont->typedefs)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3370 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3371 | case LY_STMT_MUST: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3372 | LY_CHECK_RET(parse_restrs(ctx, kw, &cont->musts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3373 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3374 | case LY_STMT_ACTION: |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 3375 | PARSER_CHECK_STMTVER2_RET(ctx, "action", "container"); |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3376 | LY_CHECK_RET(parse_action(ctx, (struct lysp_node *)cont, &cont->actions)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3377 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3378 | case LY_STMT_GROUPING: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3379 | LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)cont, &cont->groupings)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3380 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3381 | case LY_STMT_NOTIFICATION: |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 3382 | PARSER_CHECK_STMTVER2_RET(ctx, "notification", "container"); |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3383 | LY_CHECK_RET(parse_notif(ctx, (struct lysp_node *)cont, &cont->notifs)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3384 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3385 | case LY_STMT_EXTENSION_INSTANCE: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3386 | LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &cont->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3387 | break; |
| 3388 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 3389 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "container"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3390 | return LY_EVALID; |
| 3391 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3392 | } |
Radek Krejci | 7fc6829 | 2019-06-12 13:51:09 +0200 | [diff] [blame] | 3393 | checks: |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 3394 | /* finalize parent pointers to the reallocated items */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 3395 | LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, cont->groupings, NULL, cont->actions, cont->notifs)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3396 | return ret; |
| 3397 | } |
| 3398 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3399 | /** |
| 3400 | * @brief Parse the list statement. |
| 3401 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3402 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3403 | * @param[in,out] siblings Siblings to add to. |
| 3404 | * |
| 3405 | * @return LY_ERR values. |
| 3406 | */ |
Radek Krejci | 2d7a47b | 2019-05-16 13:34:10 +0200 | [diff] [blame] | 3407 | LY_ERR |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3408 | parse_list(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3409 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3410 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3411 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 3412 | size_t word_len; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3413 | enum ly_stmt kw; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3414 | struct lysp_node_list *list; |
| 3415 | |
David Sedlák | 60adc09 | 2019-08-06 15:57:02 +0200 | [diff] [blame] | 3416 | /* create new list structure */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 3417 | LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, list, next, LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3418 | list->nodetype = LYS_LIST; |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3419 | list->parent = parent; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3420 | |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3421 | /* get name */ |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3422 | LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len)); |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 3423 | INSERT_WORD_RET(ctx, buf, list->name, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3424 | |
| 3425 | /* parse substatements */ |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3426 | YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3427 | switch (kw) { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3428 | case LY_STMT_CONFIG: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3429 | LY_CHECK_RET(parse_config(ctx, &list->flags, &list->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3430 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3431 | case LY_STMT_DESCRIPTION: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3432 | LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, &list->dsc, Y_STR_ARG, &list->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3433 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3434 | case LY_STMT_IF_FEATURE: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3435 | LY_CHECK_RET(parse_qnames(ctx, LYEXT_SUBSTMT_IFFEATURE, &list->iffeatures, Y_STR_ARG, &list->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3436 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3437 | case LY_STMT_REFERENCE: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3438 | LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_REFERENCE, 0, &list->ref, Y_STR_ARG, &list->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3439 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3440 | case LY_STMT_STATUS: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3441 | LY_CHECK_RET(parse_status(ctx, &list->flags, &list->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3442 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3443 | case LY_STMT_WHEN: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3444 | LY_CHECK_RET(parse_when(ctx, &list->when)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3445 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3446 | case LY_STMT_KEY: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3447 | LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_KEY, 0, &list->key, Y_STR_ARG, &list->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3448 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3449 | case LY_STMT_MAX_ELEMENTS: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3450 | LY_CHECK_RET(parse_maxelements(ctx, &list->max, &list->flags, &list->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3451 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3452 | case LY_STMT_MIN_ELEMENTS: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3453 | LY_CHECK_RET(parse_minelements(ctx, &list->min, &list->flags, &list->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3454 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3455 | case LY_STMT_ORDERED_BY: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3456 | LY_CHECK_RET(parse_orderedby(ctx, &list->flags, &list->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3457 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3458 | case LY_STMT_UNIQUE: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3459 | LY_CHECK_RET(parse_qnames(ctx, LYEXT_SUBSTMT_UNIQUE, &list->uniques, Y_STR_ARG, &list->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3460 | break; |
| 3461 | |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3462 | case LY_STMT_ANYDATA: |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 3463 | PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "list"); |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 3464 | /* fall through */ |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3465 | case LY_STMT_ANYXML: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3466 | LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)list, &list->child)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3467 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3468 | case LY_STMT_CHOICE: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3469 | LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)list, &list->child)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3470 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3471 | case LY_STMT_CONTAINER: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3472 | LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)list, &list->child)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3473 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3474 | case LY_STMT_LEAF: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3475 | LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)list, &list->child)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3476 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3477 | case LY_STMT_LEAF_LIST: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3478 | LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)list, &list->child)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3479 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3480 | case LY_STMT_LIST: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3481 | LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)list, &list->child)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3482 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3483 | case LY_STMT_USES: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3484 | LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)list, &list->child)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3485 | break; |
| 3486 | |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3487 | case LY_STMT_TYPEDEF: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3488 | LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)list, &list->typedefs)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3489 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3490 | case LY_STMT_MUST: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3491 | LY_CHECK_RET(parse_restrs(ctx, kw, &list->musts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3492 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3493 | case LY_STMT_ACTION: |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 3494 | PARSER_CHECK_STMTVER2_RET(ctx, "action", "list"); |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3495 | LY_CHECK_RET(parse_action(ctx, (struct lysp_node *)list, &list->actions)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3496 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3497 | case LY_STMT_GROUPING: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3498 | LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)list, &list->groupings)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3499 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3500 | case LY_STMT_NOTIFICATION: |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 3501 | PARSER_CHECK_STMTVER2_RET(ctx, "notification", "list"); |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3502 | LY_CHECK_RET(parse_notif(ctx, (struct lysp_node *)list, &list->notifs)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3503 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3504 | case LY_STMT_EXTENSION_INSTANCE: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3505 | LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &list->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3506 | break; |
| 3507 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 3508 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "list"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3509 | return LY_EVALID; |
| 3510 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3511 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3512 | LY_CHECK_RET(ret); |
| 3513 | checks: |
Radek Krejci | 7fc6829 | 2019-06-12 13:51:09 +0200 | [diff] [blame] | 3514 | /* finalize parent pointers to the reallocated items */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 3515 | LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, list->groupings, NULL, list->actions, list->notifs)); |
Radek Krejci | 7fc6829 | 2019-06-12 13:51:09 +0200 | [diff] [blame] | 3516 | |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3517 | return ret; |
| 3518 | } |
| 3519 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3520 | /** |
| 3521 | * @brief Parse the yin-element statement. |
| 3522 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3523 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3524 | * @param[in,out] flags Flags to write to. |
| 3525 | * @param[in,out] exts Extension instances to add to. |
| 3526 | * |
| 3527 | * @return LY_ERR values. |
| 3528 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3529 | static LY_ERR |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3530 | parse_yinelement(struct lys_yang_parser_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3531 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3532 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3533 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 3534 | size_t word_len; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3535 | enum ly_stmt kw; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3536 | |
| 3537 | if (*flags & LYS_YINELEM_MASK) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 3538 | LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "yin-element"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3539 | return LY_EVALID; |
| 3540 | } |
| 3541 | |
| 3542 | /* get value */ |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3543 | LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3544 | |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 3545 | if ((word_len == ly_strlen_const("true")) && !strncmp(word, "true", word_len)) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3546 | *flags |= LYS_YINELEM_TRUE; |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 3547 | } else if ((word_len == ly_strlen_const("false")) && !strncmp(word, "false", word_len)) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3548 | *flags |= LYS_YINELEM_FALSE; |
| 3549 | } else { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 3550 | LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "yin-element"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3551 | free(buf); |
| 3552 | return LY_EVALID; |
| 3553 | } |
| 3554 | free(buf); |
| 3555 | |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3556 | YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3557 | switch (kw) { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3558 | case LY_STMT_EXTENSION_INSTANCE: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3559 | LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_YINELEM, 0, exts)); |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 3560 | LY_CHECK_RET(ret); |
| 3561 | break; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3562 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 3563 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "yin-element"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3564 | return LY_EVALID; |
| 3565 | } |
| 3566 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3567 | return ret; |
| 3568 | } |
| 3569 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3570 | /** |
David Sedlák | 2444f8f | 2019-07-09 11:02:47 +0200 | [diff] [blame] | 3571 | * @brief Parse the argument statement. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3572 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3573 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3574 | * @param[in,out] argument Value to write to. |
| 3575 | * @param[in,out] flags Flags to write to. |
| 3576 | * @param[in,out] exts Extension instances to add to. |
| 3577 | * |
| 3578 | * @return LY_ERR values. |
| 3579 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3580 | static LY_ERR |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3581 | parse_argument(struct lys_yang_parser_ctx *ctx, const char **argument, uint16_t *flags, struct lysp_ext_instance **exts) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3582 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3583 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3584 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 3585 | size_t word_len; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3586 | enum ly_stmt kw; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3587 | |
| 3588 | if (*argument) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 3589 | LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "argument"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3590 | return LY_EVALID; |
| 3591 | } |
| 3592 | |
| 3593 | /* get value */ |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3594 | LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len)); |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 3595 | INSERT_WORD_RET(ctx, buf, *argument, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3596 | |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3597 | YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3598 | switch (kw) { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3599 | case LY_STMT_YIN_ELEMENT: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3600 | LY_CHECK_RET(parse_yinelement(ctx, flags, exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3601 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3602 | case LY_STMT_EXTENSION_INSTANCE: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3603 | LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_ARGUMENT, 0, exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3604 | break; |
| 3605 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 3606 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "argument"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3607 | return LY_EVALID; |
| 3608 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3609 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3610 | return ret; |
| 3611 | } |
| 3612 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3613 | /** |
| 3614 | * @brief Parse the extension statement. |
| 3615 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3616 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3617 | * @param[in,out] extensions Extensions to add to. |
| 3618 | * |
| 3619 | * @return LY_ERR values. |
| 3620 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3621 | static LY_ERR |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3622 | parse_extension(struct lys_yang_parser_ctx *ctx, struct lysp_ext **extensions) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3623 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3624 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3625 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 3626 | size_t word_len; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3627 | enum ly_stmt kw; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3628 | struct lysp_ext *ex; |
| 3629 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 3630 | LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *extensions, ex, LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3631 | |
| 3632 | /* get value */ |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3633 | LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len)); |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 3634 | INSERT_WORD_RET(ctx, buf, ex->name, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3635 | |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3636 | YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3637 | switch (kw) { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3638 | case LY_STMT_DESCRIPTION: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3639 | LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, &ex->dsc, Y_STR_ARG, &ex->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3640 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3641 | case LY_STMT_REFERENCE: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3642 | LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_REFERENCE, 0, &ex->ref, Y_STR_ARG, &ex->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3643 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3644 | case LY_STMT_STATUS: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3645 | LY_CHECK_RET(parse_status(ctx, &ex->flags, &ex->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3646 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3647 | case LY_STMT_ARGUMENT: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3648 | LY_CHECK_RET(parse_argument(ctx, &ex->argument, &ex->flags, &ex->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3649 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3650 | case LY_STMT_EXTENSION_INSTANCE: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3651 | LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &ex->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3652 | break; |
| 3653 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 3654 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "extension"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3655 | return LY_EVALID; |
| 3656 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3657 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3658 | return ret; |
| 3659 | } |
| 3660 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3661 | /** |
| 3662 | * @brief Parse the deviate statement. |
| 3663 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3664 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3665 | * @param[in,out] deviates Deviates to add to. |
| 3666 | * |
| 3667 | * @return LY_ERR values. |
| 3668 | */ |
Radek Krejci | 2d7a47b | 2019-05-16 13:34:10 +0200 | [diff] [blame] | 3669 | LY_ERR |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3670 | parse_deviate(struct lys_yang_parser_ctx *ctx, struct lysp_deviate **deviates) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3671 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3672 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3673 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 3674 | size_t word_len, dev_mod; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3675 | enum ly_stmt kw; |
David Sedlák | 60adc09 | 2019-08-06 15:57:02 +0200 | [diff] [blame] | 3676 | struct lysp_deviate *d; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3677 | struct lysp_deviate_add *d_add = NULL; |
| 3678 | struct lysp_deviate_rpl *d_rpl = NULL; |
| 3679 | struct lysp_deviate_del *d_del = NULL; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 3680 | const char **d_units = NULL; |
| 3681 | struct lysp_qname **d_uniques = NULL, **d_dflts = NULL; |
Radek Krejci | 4ad42aa | 2019-07-23 16:55:58 +0200 | [diff] [blame] | 3682 | struct lysp_restr **d_musts = NULL; |
| 3683 | uint16_t *d_flags = 0; |
| 3684 | uint32_t *d_min = 0, *d_max = 0; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3685 | |
| 3686 | /* get value */ |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3687 | LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3688 | |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 3689 | if ((word_len == ly_strlen_const("not-supported")) && !strncmp(word, "not-supported", word_len)) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3690 | dev_mod = LYS_DEV_NOT_SUPPORTED; |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 3691 | } else if ((word_len == ly_strlen_const("add")) && !strncmp(word, "add", word_len)) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3692 | dev_mod = LYS_DEV_ADD; |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 3693 | } else if ((word_len == ly_strlen_const("replace")) && !strncmp(word, "replace", word_len)) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3694 | dev_mod = LYS_DEV_REPLACE; |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 3695 | } else if ((word_len == ly_strlen_const("delete")) && !strncmp(word, "delete", word_len)) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3696 | dev_mod = LYS_DEV_DELETE; |
| 3697 | } else { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 3698 | LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "deviate"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3699 | free(buf); |
| 3700 | return LY_EVALID; |
| 3701 | } |
| 3702 | free(buf); |
| 3703 | |
| 3704 | /* create structure */ |
| 3705 | switch (dev_mod) { |
| 3706 | case LYS_DEV_NOT_SUPPORTED: |
| 3707 | d = calloc(1, sizeof *d); |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 3708 | LY_CHECK_ERR_RET(!d, LOGMEM(PARSER_CTX(ctx)), LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3709 | break; |
| 3710 | case LYS_DEV_ADD: |
| 3711 | d_add = calloc(1, sizeof *d_add); |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 3712 | LY_CHECK_ERR_RET(!d_add, LOGMEM(PARSER_CTX(ctx)), LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3713 | d = (struct lysp_deviate *)d_add; |
| 3714 | d_units = &d_add->units; |
| 3715 | d_uniques = &d_add->uniques; |
| 3716 | d_dflts = &d_add->dflts; |
| 3717 | d_musts = &d_add->musts; |
| 3718 | d_flags = &d_add->flags; |
| 3719 | d_min = &d_add->min; |
| 3720 | d_max = &d_add->max; |
| 3721 | break; |
| 3722 | case LYS_DEV_REPLACE: |
| 3723 | d_rpl = calloc(1, sizeof *d_rpl); |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 3724 | LY_CHECK_ERR_RET(!d_rpl, LOGMEM(PARSER_CTX(ctx)), LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3725 | d = (struct lysp_deviate *)d_rpl; |
| 3726 | d_units = &d_rpl->units; |
| 3727 | d_flags = &d_rpl->flags; |
| 3728 | d_min = &d_rpl->min; |
| 3729 | d_max = &d_rpl->max; |
| 3730 | break; |
| 3731 | case LYS_DEV_DELETE: |
| 3732 | d_del = calloc(1, sizeof *d_del); |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 3733 | LY_CHECK_ERR_RET(!d_del, LOGMEM(PARSER_CTX(ctx)), LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3734 | d = (struct lysp_deviate *)d_del; |
| 3735 | d_units = &d_del->units; |
| 3736 | d_uniques = &d_del->uniques; |
| 3737 | d_dflts = &d_del->dflts; |
| 3738 | d_musts = &d_del->musts; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3739 | break; |
| 3740 | default: |
| 3741 | assert(0); |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 3742 | LOGINT_RET(PARSER_CTX(ctx)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3743 | } |
| 3744 | d->mod = dev_mod; |
| 3745 | |
| 3746 | /* insert into siblings */ |
David Sedlák | 60adc09 | 2019-08-06 15:57:02 +0200 | [diff] [blame] | 3747 | LY_LIST_INSERT(deviates, d, next); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3748 | |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3749 | YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3750 | switch (kw) { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3751 | case LY_STMT_CONFIG: |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3752 | switch (dev_mod) { |
| 3753 | case LYS_DEV_NOT_SUPPORTED: |
Michal Vasko | 492bec7 | 2018-09-18 13:11:10 +0200 | [diff] [blame] | 3754 | case LYS_DEV_DELETE: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 3755 | LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3756 | return LY_EVALID; |
| 3757 | default: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3758 | LY_CHECK_RET(parse_config(ctx, d_flags, &d->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3759 | break; |
| 3760 | } |
| 3761 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3762 | case LY_STMT_DEFAULT: |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3763 | switch (dev_mod) { |
| 3764 | case LYS_DEV_NOT_SUPPORTED: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 3765 | LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3766 | return LY_EVALID; |
| 3767 | case LYS_DEV_REPLACE: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3768 | LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_DEFAULT, 0, &d_rpl->dflt.str, Y_STR_ARG, &d->exts)); |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 3769 | d_rpl->dflt.mod = ctx->parsed_mod; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3770 | break; |
| 3771 | default: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3772 | LY_CHECK_RET(parse_qnames(ctx, LYEXT_SUBSTMT_DEFAULT, d_dflts, Y_STR_ARG, &d->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3773 | break; |
| 3774 | } |
| 3775 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3776 | case LY_STMT_MANDATORY: |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3777 | switch (dev_mod) { |
| 3778 | case LYS_DEV_NOT_SUPPORTED: |
Michal Vasko | 492bec7 | 2018-09-18 13:11:10 +0200 | [diff] [blame] | 3779 | case LYS_DEV_DELETE: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 3780 | LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3781 | return LY_EVALID; |
| 3782 | default: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3783 | LY_CHECK_RET(parse_mandatory(ctx, d_flags, &d->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3784 | break; |
| 3785 | } |
| 3786 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3787 | case LY_STMT_MAX_ELEMENTS: |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3788 | switch (dev_mod) { |
| 3789 | case LYS_DEV_NOT_SUPPORTED: |
Michal Vasko | 492bec7 | 2018-09-18 13:11:10 +0200 | [diff] [blame] | 3790 | case LYS_DEV_DELETE: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 3791 | LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3792 | return LY_EVALID; |
| 3793 | default: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3794 | LY_CHECK_RET(parse_maxelements(ctx, d_max, d_flags, &d->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3795 | break; |
| 3796 | } |
| 3797 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3798 | case LY_STMT_MIN_ELEMENTS: |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3799 | switch (dev_mod) { |
| 3800 | case LYS_DEV_NOT_SUPPORTED: |
Michal Vasko | 492bec7 | 2018-09-18 13:11:10 +0200 | [diff] [blame] | 3801 | case LYS_DEV_DELETE: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 3802 | LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3803 | return LY_EVALID; |
| 3804 | default: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3805 | LY_CHECK_RET(parse_minelements(ctx, d_min, d_flags, &d->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3806 | break; |
| 3807 | } |
| 3808 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3809 | case LY_STMT_MUST: |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3810 | switch (dev_mod) { |
| 3811 | case LYS_DEV_NOT_SUPPORTED: |
Michal Vasko | 492bec7 | 2018-09-18 13:11:10 +0200 | [diff] [blame] | 3812 | case LYS_DEV_REPLACE: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 3813 | LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3814 | return LY_EVALID; |
| 3815 | default: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3816 | LY_CHECK_RET(parse_restrs(ctx, kw, d_musts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3817 | break; |
| 3818 | } |
| 3819 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3820 | case LY_STMT_TYPE: |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3821 | switch (dev_mod) { |
| 3822 | case LYS_DEV_NOT_SUPPORTED: |
| 3823 | case LYS_DEV_ADD: |
| 3824 | case LYS_DEV_DELETE: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 3825 | LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3826 | return LY_EVALID; |
| 3827 | default: |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 3828 | if (d_rpl->type) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 3829 | LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw)); |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 3830 | return LY_EVALID; |
| 3831 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3832 | d_rpl->type = calloc(1, sizeof *d_rpl->type); |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 3833 | LY_CHECK_ERR_RET(!d_rpl->type, LOGMEM(PARSER_CTX(ctx)), LY_EMEM); |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3834 | LY_CHECK_RET(parse_type(ctx, d_rpl->type)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3835 | break; |
| 3836 | } |
| 3837 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3838 | case LY_STMT_UNIQUE: |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3839 | switch (dev_mod) { |
| 3840 | case LYS_DEV_NOT_SUPPORTED: |
| 3841 | case LYS_DEV_REPLACE: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 3842 | LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3843 | return LY_EVALID; |
| 3844 | default: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3845 | LY_CHECK_RET(parse_qnames(ctx, LYEXT_SUBSTMT_UNIQUE, d_uniques, Y_STR_ARG, &d->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3846 | break; |
| 3847 | } |
| 3848 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3849 | case LY_STMT_UNITS: |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3850 | switch (dev_mod) { |
| 3851 | case LYS_DEV_NOT_SUPPORTED: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 3852 | LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3853 | return LY_EVALID; |
| 3854 | default: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3855 | LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_UNITS, 0, d_units, Y_STR_ARG, &d->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3856 | break; |
| 3857 | } |
| 3858 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3859 | case LY_STMT_EXTENSION_INSTANCE: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3860 | LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &d->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3861 | break; |
| 3862 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 3863 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "deviate"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3864 | return LY_EVALID; |
| 3865 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3866 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3867 | return ret; |
| 3868 | } |
| 3869 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3870 | /** |
| 3871 | * @brief Parse the deviation statement. |
| 3872 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3873 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3874 | * @param[in,out] deviations Deviations to add to. |
| 3875 | * |
| 3876 | * @return LY_ERR values. |
| 3877 | */ |
Radek Krejci | 2d7a47b | 2019-05-16 13:34:10 +0200 | [diff] [blame] | 3878 | LY_ERR |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3879 | parse_deviation(struct lys_yang_parser_ctx *ctx, struct lysp_deviation **deviations) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3880 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3881 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3882 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 3883 | size_t word_len; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3884 | enum ly_stmt kw; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3885 | struct lysp_deviation *dev; |
| 3886 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 3887 | LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *deviations, dev, LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3888 | |
| 3889 | /* get value */ |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3890 | LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len)); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 3891 | CHECK_NONEMPTY(ctx, word_len, "deviation"); |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 3892 | INSERT_WORD_RET(ctx, buf, dev->nodeid, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3893 | |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3894 | YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3895 | switch (kw) { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3896 | case LY_STMT_DESCRIPTION: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3897 | LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, &dev->dsc, Y_STR_ARG, &dev->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3898 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3899 | case LY_STMT_DEVIATE: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3900 | LY_CHECK_RET(parse_deviate(ctx, &dev->deviates)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3901 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3902 | case LY_STMT_REFERENCE: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3903 | LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_REFERENCE, 0, &dev->ref, Y_STR_ARG, &dev->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3904 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3905 | case LY_STMT_EXTENSION_INSTANCE: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3906 | LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &dev->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3907 | break; |
| 3908 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 3909 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "deviation"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3910 | return LY_EVALID; |
| 3911 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3912 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 3913 | LY_CHECK_RET(ret); |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 3914 | checks: |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3915 | /* mandatory substatements */ |
| 3916 | if (!dev->deviates) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 3917 | LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "deviate", "deviation"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3918 | return LY_EVALID; |
| 3919 | } |
| 3920 | |
| 3921 | return ret; |
| 3922 | } |
| 3923 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3924 | /** |
| 3925 | * @brief Parse the feature statement. |
| 3926 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3927 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3928 | * @param[in,out] features Features to add to. |
| 3929 | * |
| 3930 | * @return LY_ERR values. |
| 3931 | */ |
Radek Krejci | 2d7a47b | 2019-05-16 13:34:10 +0200 | [diff] [blame] | 3932 | LY_ERR |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3933 | parse_feature(struct lys_yang_parser_ctx *ctx, struct lysp_feature **features) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3934 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3935 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3936 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 3937 | size_t word_len; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3938 | enum ly_stmt kw; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3939 | struct lysp_feature *feat; |
| 3940 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 3941 | LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *features, feat, LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3942 | |
| 3943 | /* get value */ |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3944 | LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len)); |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 3945 | INSERT_WORD_RET(ctx, buf, feat->name, word, word_len); |
Radek Krejci | faa1eac | 2018-10-30 14:34:55 +0100 | [diff] [blame] | 3946 | |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3947 | YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3948 | switch (kw) { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3949 | case LY_STMT_DESCRIPTION: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3950 | LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, &feat->dsc, Y_STR_ARG, &feat->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3951 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3952 | case LY_STMT_IF_FEATURE: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3953 | LY_CHECK_RET(parse_qnames(ctx, LYEXT_SUBSTMT_IFFEATURE, &feat->iffeatures, Y_STR_ARG, &feat->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3954 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3955 | case LY_STMT_REFERENCE: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3956 | LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_REFERENCE, 0, &feat->ref, Y_STR_ARG, &feat->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3957 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3958 | case LY_STMT_STATUS: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3959 | LY_CHECK_RET(parse_status(ctx, &feat->flags, &feat->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3960 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3961 | case LY_STMT_EXTENSION_INSTANCE: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3962 | LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &feat->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3963 | break; |
| 3964 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 3965 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "feature"); |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 3966 | return LY_EVALID; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3967 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3968 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3969 | return ret; |
| 3970 | } |
| 3971 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3972 | /** |
| 3973 | * @brief Parse the identity statement. |
| 3974 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3975 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3976 | * @param[in,out] identities Identities to add to. |
| 3977 | * |
| 3978 | * @return LY_ERR values. |
| 3979 | */ |
Radek Krejci | 2d7a47b | 2019-05-16 13:34:10 +0200 | [diff] [blame] | 3980 | LY_ERR |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3981 | parse_identity(struct lys_yang_parser_ctx *ctx, struct lysp_ident **identities) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3982 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3983 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3984 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 3985 | size_t word_len; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3986 | enum ly_stmt kw; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3987 | struct lysp_ident *ident; |
| 3988 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 3989 | LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *identities, ident, LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3990 | |
| 3991 | /* get value */ |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3992 | LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len)); |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 3993 | INSERT_WORD_RET(ctx, buf, ident->name, word, word_len); |
Radek Krejci | faa1eac | 2018-10-30 14:34:55 +0100 | [diff] [blame] | 3994 | |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3995 | YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, ) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3996 | switch (kw) { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3997 | case LY_STMT_DESCRIPTION: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 3998 | LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, &ident->dsc, Y_STR_ARG, &ident->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3999 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 4000 | case LY_STMT_IF_FEATURE: |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 4001 | PARSER_CHECK_STMTVER2_RET(ctx, "if-feature", "identity"); |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 4002 | LY_CHECK_RET(parse_qnames(ctx, LYEXT_SUBSTMT_IFFEATURE, &ident->iffeatures, Y_STR_ARG, &ident->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4003 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 4004 | case LY_STMT_REFERENCE: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 4005 | LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_REFERENCE, 0, &ident->ref, Y_STR_ARG, &ident->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4006 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 4007 | case LY_STMT_STATUS: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 4008 | LY_CHECK_RET(parse_status(ctx, &ident->flags, &ident->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4009 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 4010 | case LY_STMT_BASE: |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 4011 | if (ident->bases && (ctx->parsed_mod->version < LYS_VERSION_1_1)) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 4012 | LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Identity can be derived from multiple base identities only in YANG 1.1 modules"); |
Radek Krejci | 1011365 | 2018-11-14 16:56:50 +0100 | [diff] [blame] | 4013 | return LY_EVALID; |
| 4014 | } |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 4015 | LY_CHECK_RET(parse_text_fields(ctx, LYEXT_SUBSTMT_BASE, &ident->bases, Y_PREF_IDENTIF_ARG, &ident->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4016 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 4017 | case LY_STMT_EXTENSION_INSTANCE: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 4018 | LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &ident->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4019 | break; |
| 4020 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 4021 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "identity"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4022 | return LY_EVALID; |
| 4023 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4024 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4025 | return ret; |
| 4026 | } |
| 4027 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 4028 | /** |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4029 | * @brief Parse module substatements. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 4030 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 4031 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 4032 | * @param[in,out] mod Module to write to. |
| 4033 | * |
| 4034 | * @return LY_ERR values. |
| 4035 | */ |
Radek Krejci | 2d7a47b | 2019-05-16 13:34:10 +0200 | [diff] [blame] | 4036 | LY_ERR |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 4037 | parse_module(struct lys_yang_parser_ctx *ctx, struct lysp_module *mod) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4038 | { |
| 4039 | LY_ERR ret = 0; |
| 4040 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 4041 | size_t word_len; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 4042 | enum ly_stmt kw, prev_kw = 0; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4043 | enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4044 | struct lysp_submodule *dup; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4045 | |
Michal Vasko | c3781c3 | 2020-10-06 14:04:08 +0200 | [diff] [blame] | 4046 | mod->is_submod = 0; |
| 4047 | |
| 4048 | /* module name */ |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 4049 | LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len)); |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 4050 | INSERT_WORD_RET(ctx, buf, mod->mod->name, word, word_len); |
Radek Krejci | faa1eac | 2018-10-30 14:34:55 +0100 | [diff] [blame] | 4051 | |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 4052 | YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4053 | |
Radek Krejci | e384647 | 2018-10-15 15:24:51 +0200 | [diff] [blame] | 4054 | #define CHECK_ORDER(SECTION) \ |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 4055 | if (mod_stmt > SECTION) {LOGVAL_PARSER(ctx, LY_VCODE_INORD, ly_stmt2str(kw), ly_stmt2str(prev_kw)); return LY_EVALID;}mod_stmt = SECTION |
Radek Krejci | e384647 | 2018-10-15 15:24:51 +0200 | [diff] [blame] | 4056 | |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4057 | switch (kw) { |
| 4058 | /* module header */ |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 4059 | case LY_STMT_NAMESPACE: |
| 4060 | case LY_STMT_PREFIX: |
Radek Krejci | e384647 | 2018-10-15 15:24:51 +0200 | [diff] [blame] | 4061 | CHECK_ORDER(Y_MOD_MODULE_HEADER); |
| 4062 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 4063 | case LY_STMT_YANG_VERSION: |
Radek Krejci | e384647 | 2018-10-15 15:24:51 +0200 | [diff] [blame] | 4064 | CHECK_ORDER(Y_MOD_MODULE_HEADER); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4065 | break; |
| 4066 | /* linkage */ |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 4067 | case LY_STMT_INCLUDE: |
| 4068 | case LY_STMT_IMPORT: |
Radek Krejci | e384647 | 2018-10-15 15:24:51 +0200 | [diff] [blame] | 4069 | CHECK_ORDER(Y_MOD_LINKAGE); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4070 | break; |
| 4071 | /* meta */ |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 4072 | case LY_STMT_ORGANIZATION: |
| 4073 | case LY_STMT_CONTACT: |
| 4074 | case LY_STMT_DESCRIPTION: |
| 4075 | case LY_STMT_REFERENCE: |
Radek Krejci | e384647 | 2018-10-15 15:24:51 +0200 | [diff] [blame] | 4076 | CHECK_ORDER(Y_MOD_META); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4077 | break; |
| 4078 | |
| 4079 | /* revision */ |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 4080 | case LY_STMT_REVISION: |
Radek Krejci | e384647 | 2018-10-15 15:24:51 +0200 | [diff] [blame] | 4081 | CHECK_ORDER(Y_MOD_REVISION); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4082 | break; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4083 | /* body */ |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 4084 | case LY_STMT_ANYDATA: |
| 4085 | case LY_STMT_ANYXML: |
| 4086 | case LY_STMT_AUGMENT: |
| 4087 | case LY_STMT_CHOICE: |
| 4088 | case LY_STMT_CONTAINER: |
| 4089 | case LY_STMT_DEVIATION: |
| 4090 | case LY_STMT_EXTENSION: |
| 4091 | case LY_STMT_FEATURE: |
| 4092 | case LY_STMT_GROUPING: |
| 4093 | case LY_STMT_IDENTITY: |
| 4094 | case LY_STMT_LEAF: |
| 4095 | case LY_STMT_LEAF_LIST: |
| 4096 | case LY_STMT_LIST: |
| 4097 | case LY_STMT_NOTIFICATION: |
| 4098 | case LY_STMT_RPC: |
| 4099 | case LY_STMT_TYPEDEF: |
| 4100 | case LY_STMT_USES: |
| 4101 | case LY_STMT_EXTENSION_INSTANCE: |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4102 | mod_stmt = Y_MOD_BODY; |
| 4103 | break; |
| 4104 | default: |
| 4105 | /* error handled in the next switch */ |
| 4106 | break; |
| 4107 | } |
Radek Krejci | e384647 | 2018-10-15 15:24:51 +0200 | [diff] [blame] | 4108 | #undef CHECK_ORDER |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4109 | |
Radek Krejci | e384647 | 2018-10-15 15:24:51 +0200 | [diff] [blame] | 4110 | prev_kw = kw; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4111 | switch (kw) { |
| 4112 | /* module header */ |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 4113 | case LY_STMT_YANG_VERSION: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 4114 | LY_CHECK_RET(parse_yangversion(ctx, &mod->version, &mod->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4115 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 4116 | case LY_STMT_NAMESPACE: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 4117 | LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_NAMESPACE, 0, &mod->mod->ns, Y_STR_ARG, &mod->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4118 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 4119 | case LY_STMT_PREFIX: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 4120 | LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_PREFIX, 0, &mod->mod->prefix, Y_IDENTIF_ARG, &mod->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4121 | break; |
| 4122 | |
| 4123 | /* linkage */ |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 4124 | case LY_STMT_INCLUDE: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 4125 | LY_CHECK_RET(parse_include(ctx, mod->mod->name, &mod->includes)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4126 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 4127 | case LY_STMT_IMPORT: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 4128 | LY_CHECK_RET(parse_import(ctx, mod->mod->prefix, &mod->imports)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4129 | break; |
| 4130 | |
| 4131 | /* meta */ |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 4132 | case LY_STMT_ORGANIZATION: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 4133 | LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_ORGANIZATION, 0, &mod->mod->org, Y_STR_ARG, &mod->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4134 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 4135 | case LY_STMT_CONTACT: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 4136 | LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_CONTACT, 0, &mod->mod->contact, Y_STR_ARG, &mod->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4137 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 4138 | case LY_STMT_DESCRIPTION: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 4139 | LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, &mod->mod->dsc, Y_STR_ARG, &mod->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4140 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 4141 | case LY_STMT_REFERENCE: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 4142 | LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_REFERENCE, 0, &mod->mod->ref, Y_STR_ARG, &mod->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4143 | break; |
| 4144 | |
| 4145 | /* revision */ |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 4146 | case LY_STMT_REVISION: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 4147 | LY_CHECK_RET(parse_revision(ctx, &mod->revs)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4148 | break; |
| 4149 | |
| 4150 | /* body */ |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 4151 | case LY_STMT_ANYDATA: |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 4152 | PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "module"); |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 4153 | /* fall through */ |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 4154 | case LY_STMT_ANYXML: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 4155 | LY_CHECK_RET(parse_any(ctx, kw, NULL, &mod->data)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4156 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 4157 | case LY_STMT_CHOICE: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 4158 | LY_CHECK_RET(parse_choice(ctx, NULL, &mod->data)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4159 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 4160 | case LY_STMT_CONTAINER: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 4161 | LY_CHECK_RET(parse_container(ctx, NULL, &mod->data)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4162 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 4163 | case LY_STMT_LEAF: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 4164 | LY_CHECK_RET(parse_leaf(ctx, NULL, &mod->data)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4165 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 4166 | case LY_STMT_LEAF_LIST: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 4167 | LY_CHECK_RET(parse_leaflist(ctx, NULL, &mod->data)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4168 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 4169 | case LY_STMT_LIST: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 4170 | LY_CHECK_RET(parse_list(ctx, NULL, &mod->data)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4171 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 4172 | case LY_STMT_USES: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 4173 | LY_CHECK_RET(parse_uses(ctx, NULL, &mod->data)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4174 | break; |
| 4175 | |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 4176 | case LY_STMT_AUGMENT: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 4177 | LY_CHECK_RET(parse_augment(ctx, NULL, &mod->augments)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4178 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 4179 | case LY_STMT_DEVIATION: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 4180 | LY_CHECK_RET(parse_deviation(ctx, &mod->deviations)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4181 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 4182 | case LY_STMT_EXTENSION: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 4183 | LY_CHECK_RET(parse_extension(ctx, &mod->extensions)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4184 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 4185 | case LY_STMT_FEATURE: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 4186 | LY_CHECK_RET(parse_feature(ctx, &mod->features)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4187 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 4188 | case LY_STMT_GROUPING: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 4189 | LY_CHECK_RET(parse_grouping(ctx, NULL, &mod->groupings)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4190 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 4191 | case LY_STMT_IDENTITY: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 4192 | LY_CHECK_RET(parse_identity(ctx, &mod->identities)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4193 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 4194 | case LY_STMT_NOTIFICATION: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 4195 | LY_CHECK_RET(parse_notif(ctx, NULL, &mod->notifs)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4196 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 4197 | case LY_STMT_RPC: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 4198 | LY_CHECK_RET(parse_action(ctx, NULL, &mod->rpcs)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4199 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 4200 | case LY_STMT_TYPEDEF: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 4201 | LY_CHECK_RET(parse_typedef(ctx, NULL, &mod->typedefs)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4202 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 4203 | case LY_STMT_EXTENSION_INSTANCE: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 4204 | LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &mod->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4205 | break; |
| 4206 | |
| 4207 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 4208 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "module"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4209 | return LY_EVALID; |
| 4210 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4211 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 4212 | LY_CHECK_RET(ret); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4213 | |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 4214 | checks: |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4215 | /* finalize parent pointers to the reallocated items */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 4216 | LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, mod->groupings, mod->augments, mod->rpcs, mod->notifs)); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4217 | |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4218 | /* mandatory substatements */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4219 | if (!mod->mod->ns) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 4220 | LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "namespace", "module"); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4221 | return LY_EVALID; |
| 4222 | } else if (!mod->mod->prefix) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 4223 | LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "module"); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4224 | return LY_EVALID; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4225 | } |
| 4226 | |
Radek Krejci | e9e987e | 2018-10-31 12:50:27 +0100 | [diff] [blame] | 4227 | /* submodules share the namespace with the module names, so there must not be |
| 4228 | * a submodule of the same name in the context, no need for revision matching */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 4229 | dup = ly_ctx_get_submodule(PARSER_CTX(ctx), NULL, mod->mod->name, NULL); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4230 | if (dup) { |
Radek Krejci | 854e155 | 2020-12-21 15:05:23 +0100 | [diff] [blame] | 4231 | LOGVAL_PARSER(ctx, LY_VCODE_NAME2_COL, "module", "submodule", mod->mod->name); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4232 | return LY_EVALID; |
| 4233 | } |
| 4234 | |
| 4235 | return ret; |
| 4236 | } |
| 4237 | |
| 4238 | /** |
| 4239 | * @brief Parse submodule substatements. |
| 4240 | * |
| 4241 | * @param[in] ctx yang parser context for logging. |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4242 | * @param[out] submod Parsed submodule structure. |
| 4243 | * |
| 4244 | * @return LY_ERR values. |
| 4245 | */ |
Radek Krejci | 2d7a47b | 2019-05-16 13:34:10 +0200 | [diff] [blame] | 4246 | LY_ERR |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 4247 | parse_submodule(struct lys_yang_parser_ctx *ctx, struct lysp_submodule *submod) |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4248 | { |
| 4249 | LY_ERR ret = 0; |
| 4250 | char *buf, *word; |
| 4251 | size_t word_len; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 4252 | enum ly_stmt kw, prev_kw = 0; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4253 | enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER; |
| 4254 | struct lysp_submodule *dup; |
| 4255 | |
Michal Vasko | c3781c3 | 2020-10-06 14:04:08 +0200 | [diff] [blame] | 4256 | submod->is_submod = 1; |
| 4257 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4258 | /* submodule name */ |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 4259 | LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len)); |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 4260 | INSERT_WORD_RET(ctx, buf, submod->name, word, word_len); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4261 | |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 4262 | YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, goto checks) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4263 | |
| 4264 | #define CHECK_ORDER(SECTION) \ |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 4265 | if (mod_stmt > SECTION) {LOGVAL_PARSER(ctx, LY_VCODE_INORD, ly_stmt2str(kw), ly_stmt2str(prev_kw)); return LY_EVALID;}mod_stmt = SECTION |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4266 | |
| 4267 | switch (kw) { |
| 4268 | /* module header */ |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 4269 | case LY_STMT_BELONGS_TO: |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4270 | CHECK_ORDER(Y_MOD_MODULE_HEADER); |
| 4271 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 4272 | case LY_STMT_YANG_VERSION: |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4273 | CHECK_ORDER(Y_MOD_MODULE_HEADER); |
| 4274 | break; |
| 4275 | /* linkage */ |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 4276 | case LY_STMT_INCLUDE: |
| 4277 | case LY_STMT_IMPORT: |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4278 | CHECK_ORDER(Y_MOD_LINKAGE); |
| 4279 | break; |
| 4280 | /* meta */ |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 4281 | case LY_STMT_ORGANIZATION: |
| 4282 | case LY_STMT_CONTACT: |
| 4283 | case LY_STMT_DESCRIPTION: |
| 4284 | case LY_STMT_REFERENCE: |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4285 | CHECK_ORDER(Y_MOD_META); |
| 4286 | break; |
| 4287 | |
| 4288 | /* revision */ |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 4289 | case LY_STMT_REVISION: |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4290 | CHECK_ORDER(Y_MOD_REVISION); |
| 4291 | break; |
| 4292 | /* body */ |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 4293 | case LY_STMT_ANYDATA: |
| 4294 | case LY_STMT_ANYXML: |
| 4295 | case LY_STMT_AUGMENT: |
| 4296 | case LY_STMT_CHOICE: |
| 4297 | case LY_STMT_CONTAINER: |
| 4298 | case LY_STMT_DEVIATION: |
| 4299 | case LY_STMT_EXTENSION: |
| 4300 | case LY_STMT_FEATURE: |
| 4301 | case LY_STMT_GROUPING: |
| 4302 | case LY_STMT_IDENTITY: |
| 4303 | case LY_STMT_LEAF: |
| 4304 | case LY_STMT_LEAF_LIST: |
| 4305 | case LY_STMT_LIST: |
| 4306 | case LY_STMT_NOTIFICATION: |
| 4307 | case LY_STMT_RPC: |
| 4308 | case LY_STMT_TYPEDEF: |
| 4309 | case LY_STMT_USES: |
| 4310 | case LY_STMT_EXTENSION_INSTANCE: |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4311 | mod_stmt = Y_MOD_BODY; |
| 4312 | break; |
| 4313 | default: |
| 4314 | /* error handled in the next switch */ |
| 4315 | break; |
| 4316 | } |
| 4317 | #undef CHECK_ORDER |
| 4318 | |
| 4319 | prev_kw = kw; |
| 4320 | switch (kw) { |
| 4321 | /* module header */ |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 4322 | case LY_STMT_YANG_VERSION: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 4323 | LY_CHECK_RET(parse_yangversion(ctx, &submod->version, &submod->exts)); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4324 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 4325 | case LY_STMT_BELONGS_TO: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 4326 | LY_CHECK_RET(parse_belongsto(ctx, &submod->prefix, &submod->exts)); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4327 | break; |
| 4328 | |
| 4329 | /* linkage */ |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 4330 | case LY_STMT_INCLUDE: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 4331 | LY_CHECK_RET(parse_include(ctx, submod->name, &submod->includes)); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4332 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 4333 | case LY_STMT_IMPORT: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 4334 | LY_CHECK_RET(parse_import(ctx, submod->prefix, &submod->imports)); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4335 | break; |
| 4336 | |
| 4337 | /* meta */ |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 4338 | case LY_STMT_ORGANIZATION: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 4339 | LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_ORGANIZATION, 0, &submod->org, Y_STR_ARG, &submod->exts)); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4340 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 4341 | case LY_STMT_CONTACT: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 4342 | LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_CONTACT, 0, &submod->contact, Y_STR_ARG, &submod->exts)); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4343 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 4344 | case LY_STMT_DESCRIPTION: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 4345 | LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, &submod->dsc, Y_STR_ARG, &submod->exts)); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4346 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 4347 | case LY_STMT_REFERENCE: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 4348 | LY_CHECK_RET(parse_text_field(ctx, LYEXT_SUBSTMT_REFERENCE, 0, &submod->ref, Y_STR_ARG, &submod->exts)); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4349 | break; |
| 4350 | |
| 4351 | /* revision */ |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 4352 | case LY_STMT_REVISION: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 4353 | LY_CHECK_RET(parse_revision(ctx, &submod->revs)); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4354 | break; |
| 4355 | |
| 4356 | /* body */ |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 4357 | case LY_STMT_ANYDATA: |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 4358 | PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "submodule"); |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 4359 | /* fall through */ |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 4360 | case LY_STMT_ANYXML: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 4361 | LY_CHECK_RET(parse_any(ctx, kw, NULL, &submod->data)); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4362 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 4363 | case LY_STMT_CHOICE: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 4364 | LY_CHECK_RET(parse_choice(ctx, NULL, &submod->data)); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4365 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 4366 | case LY_STMT_CONTAINER: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 4367 | LY_CHECK_RET(parse_container(ctx, NULL, &submod->data)); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4368 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 4369 | case LY_STMT_LEAF: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 4370 | LY_CHECK_RET(parse_leaf(ctx, NULL, &submod->data)); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4371 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 4372 | case LY_STMT_LEAF_LIST: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 4373 | LY_CHECK_RET(parse_leaflist(ctx, NULL, &submod->data)); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4374 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 4375 | case LY_STMT_LIST: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 4376 | LY_CHECK_RET(parse_list(ctx, NULL, &submod->data)); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4377 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 4378 | case LY_STMT_USES: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 4379 | LY_CHECK_RET(parse_uses(ctx, NULL, &submod->data)); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4380 | break; |
| 4381 | |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 4382 | case LY_STMT_AUGMENT: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 4383 | LY_CHECK_RET(parse_augment(ctx, NULL, &submod->augments)); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4384 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 4385 | case LY_STMT_DEVIATION: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 4386 | LY_CHECK_RET(parse_deviation(ctx, &submod->deviations)); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4387 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 4388 | case LY_STMT_EXTENSION: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 4389 | LY_CHECK_RET(parse_extension(ctx, &submod->extensions)); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4390 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 4391 | case LY_STMT_FEATURE: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 4392 | LY_CHECK_RET(parse_feature(ctx, &submod->features)); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4393 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 4394 | case LY_STMT_GROUPING: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 4395 | LY_CHECK_RET(parse_grouping(ctx, NULL, &submod->groupings)); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4396 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 4397 | case LY_STMT_IDENTITY: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 4398 | LY_CHECK_RET(parse_identity(ctx, &submod->identities)); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4399 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 4400 | case LY_STMT_NOTIFICATION: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 4401 | LY_CHECK_RET(parse_notif(ctx, NULL, &submod->notifs)); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4402 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 4403 | case LY_STMT_RPC: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 4404 | LY_CHECK_RET(parse_action(ctx, NULL, &submod->rpcs)); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4405 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 4406 | case LY_STMT_TYPEDEF: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 4407 | LY_CHECK_RET(parse_typedef(ctx, NULL, &submod->typedefs)); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4408 | break; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 4409 | case LY_STMT_EXTENSION_INSTANCE: |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 4410 | LY_CHECK_RET(parse_ext(ctx, word, word_len, LYEXT_SUBSTMT_SELF, 0, &submod->exts)); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4411 | break; |
| 4412 | |
| 4413 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 4414 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "submodule"); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4415 | return LY_EVALID; |
| 4416 | } |
| 4417 | } |
| 4418 | LY_CHECK_RET(ret); |
| 4419 | |
| 4420 | checks: |
| 4421 | /* finalize parent pointers to the reallocated items */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 4422 | LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, submod->groupings, submod->augments, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 4423 | submod->rpcs, submod->notifs)); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4424 | |
| 4425 | /* mandatory substatements */ |
Michal Vasko | c3781c3 | 2020-10-06 14:04:08 +0200 | [diff] [blame] | 4426 | if (!submod->prefix) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 4427 | LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "belongs-to", "submodule"); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4428 | return LY_EVALID; |
| 4429 | } |
| 4430 | |
| 4431 | /* submodules share the namespace with the module names, so there must not be |
| 4432 | * a submodule of the same name in the context, no need for revision matching */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 4433 | dup = ly_ctx_get_submodule(PARSER_CTX(ctx), NULL, submod->name, NULL); |
Michal Vasko | c3781c3 | 2020-10-06 14:04:08 +0200 | [diff] [blame] | 4434 | /* main modules may have different revisions */ |
| 4435 | if (dup && strcmp(dup->mod->name, submod->mod->name)) { |
Radek Krejci | 854e155 | 2020-12-21 15:05:23 +0100 | [diff] [blame] | 4436 | LOGVAL_PARSER(ctx, LY_VCODE_NAME_COL, "submodules", dup->name); |
Radek Krejci | e9e987e | 2018-10-31 12:50:27 +0100 | [diff] [blame] | 4437 | return LY_EVALID; |
| 4438 | } |
| 4439 | |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4440 | return ret; |
| 4441 | } |
| 4442 | |
Michal Vasko | 69e6bbb | 2020-11-04 17:11:46 +0100 | [diff] [blame] | 4443 | /** |
| 4444 | * @brief Skip any redundant characters, namely whitespaces and comments. |
| 4445 | * |
| 4446 | * @param[in] ctx Yang parser context. |
Michal Vasko | 69e6bbb | 2020-11-04 17:11:46 +0100 | [diff] [blame] | 4447 | * @return LY_SUCCESS on success. |
| 4448 | * @return LY_EVALID on invalid comment. |
| 4449 | */ |
| 4450 | static LY_ERR |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 4451 | skip_redundant_chars(struct lys_yang_parser_ctx *ctx) |
Michal Vasko | 69e6bbb | 2020-11-04 17:11:46 +0100 | [diff] [blame] | 4452 | { |
| 4453 | /* read some trailing spaces, new lines, or comments */ |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 4454 | while (ctx->in->current[0]) { |
| 4455 | if (!strncmp(ctx->in->current, "//", 2)) { |
Michal Vasko | 69e6bbb | 2020-11-04 17:11:46 +0100 | [diff] [blame] | 4456 | /* one-line comment */ |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 4457 | ly_in_skip(ctx->in, 2); |
| 4458 | LY_CHECK_RET(skip_comment(ctx, 1)); |
| 4459 | } else if (!strncmp(ctx->in->current, "/*", 2)) { |
Michal Vasko | 69e6bbb | 2020-11-04 17:11:46 +0100 | [diff] [blame] | 4460 | /* block comment */ |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 4461 | ly_in_skip(ctx->in, 2); |
| 4462 | LY_CHECK_RET(skip_comment(ctx, 2)); |
| 4463 | } else if (isspace(ctx->in->current[0])) { |
Michal Vasko | 69e6bbb | 2020-11-04 17:11:46 +0100 | [diff] [blame] | 4464 | /* whitespace */ |
Radek Krejci | dd713ce | 2021-01-04 23:12:12 +0100 | [diff] [blame] | 4465 | if (ctx->in->current[0] == '\n') { |
| 4466 | LY_IN_NEW_LINE(ctx->in); |
| 4467 | } |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 4468 | ly_in_skip(ctx->in, 1); |
Michal Vasko | 69e6bbb | 2020-11-04 17:11:46 +0100 | [diff] [blame] | 4469 | } else { |
| 4470 | break; |
| 4471 | } |
| 4472 | } |
| 4473 | |
| 4474 | return LY_SUCCESS; |
| 4475 | } |
| 4476 | |
Radek Krejci | d4557c6 | 2018-09-17 11:42:09 +0200 | [diff] [blame] | 4477 | LY_ERR |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 4478 | yang_parse_submodule(struct lys_yang_parser_ctx **context, struct ly_ctx *ly_ctx, struct lys_parser_ctx *main_ctx, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 4479 | struct ly_in *in, struct lysp_submodule **submod) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4480 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 4481 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 0a1d0d4 | 2019-05-16 15:14:51 +0200 | [diff] [blame] | 4482 | char *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 4483 | size_t word_len; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 4484 | enum ly_stmt kw; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4485 | struct lysp_submodule *mod_p = NULL; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4486 | |
David Sedlák | 1b62312 | 2019-08-05 15:27:49 +0200 | [diff] [blame] | 4487 | /* create context */ |
| 4488 | *context = calloc(1, sizeof **context); |
| 4489 | LY_CHECK_ERR_RET(!(*context), LOGMEM(ly_ctx), LY_EMEM); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 4490 | (*context)->format = LYS_IN_YANG; |
Michal Vasko | 405cc9e | 2020-12-01 12:01:27 +0100 | [diff] [blame] | 4491 | (*context)->unres = main_ctx->unres; |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 4492 | (*context)->in = in; |
David Sedlák | 1b62312 | 2019-08-05 15:27:49 +0200 | [diff] [blame] | 4493 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 4494 | mod_p = calloc(1, sizeof *mod_p); |
| 4495 | LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(ly_ctx); ret = LY_EMEM, cleanup); |
| 4496 | mod_p->mod = main_ctx->parsed_mod->mod; |
| 4497 | mod_p->parsing = 1; |
| 4498 | (*context)->parsed_mod = (struct lysp_module *)mod_p; |
| 4499 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 4500 | LOG_LOCINIT(NULL, NULL, NULL, in); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4501 | |
David Sedlák | 1b62312 | 2019-08-05 15:27:49 +0200 | [diff] [blame] | 4502 | /* map the typedefs and groupings list from main context to the submodule's context */ |
| 4503 | memcpy(&(*context)->tpdfs_nodes, &main_ctx->tpdfs_nodes, sizeof main_ctx->tpdfs_nodes); |
| 4504 | memcpy(&(*context)->grps_nodes, &main_ctx->grps_nodes, sizeof main_ctx->grps_nodes); |
| 4505 | |
Michal Vasko | 69e6bbb | 2020-11-04 17:11:46 +0100 | [diff] [blame] | 4506 | /* skip redundant but valid characters at the beginning */ |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 4507 | ret = skip_redundant_chars(*context); |
Michal Vasko | 69e6bbb | 2020-11-04 17:11:46 +0100 | [diff] [blame] | 4508 | LY_CHECK_GOTO(ret, cleanup); |
| 4509 | |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4510 | /* "module"/"submodule" */ |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 4511 | ret = get_keyword(*context, &kw, &word, &word_len); |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 4512 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4513 | |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 4514 | if (kw == LY_STMT_MODULE) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 4515 | LOGERR(ly_ctx, LY_EDENIED, "Input data contains module in situation when a submodule is expected."); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4516 | ret = LY_EINVAL; |
| 4517 | goto cleanup; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 4518 | } else if (kw != LY_STMT_SUBMODULE) { |
David Sedlák | 1538a84 | 2019-08-08 15:38:51 +0200 | [diff] [blame] | 4519 | LOGVAL_PARSER(*context, LY_VCODE_MOD_SUBOMD, ly_stmt2str(kw)); |
Radek Krejci | 40544fa | 2019-01-11 09:38:37 +0100 | [diff] [blame] | 4520 | ret = LY_EVALID; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 4521 | goto cleanup; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4522 | } |
| 4523 | |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4524 | /* substatements */ |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 4525 | ret = parse_submodule(*context, mod_p); |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 4526 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4527 | |
Michal Vasko | 69e6bbb | 2020-11-04 17:11:46 +0100 | [diff] [blame] | 4528 | /* skip redundant but valid characters at the end */ |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 4529 | ret = skip_redundant_chars(*context); |
Michal Vasko | 69e6bbb | 2020-11-04 17:11:46 +0100 | [diff] [blame] | 4530 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 4531 | if (in->current[0]) { |
| 4532 | LOGVAL_PARSER(*context, LY_VCODE_TRAILING_SUBMOD, 15, in->current, strlen(in->current) > 15 ? "..." : ""); |
Radek Krejci | 40544fa | 2019-01-11 09:38:37 +0100 | [diff] [blame] | 4533 | ret = LY_EVALID; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 4534 | goto cleanup; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4535 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4536 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4537 | mod_p->parsing = 0; |
| 4538 | *submod = mod_p; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4539 | |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 4540 | cleanup: |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 4541 | LOG_LOCBACK(0, 0, 0, 1); |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 4542 | if (ret) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 4543 | lysp_module_free((struct lysp_module *)mod_p); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 4544 | yang_parser_ctx_free(*context); |
David Sedlák | 1b62312 | 2019-08-05 15:27:49 +0200 | [diff] [blame] | 4545 | *context = NULL; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4546 | } |
| 4547 | |
| 4548 | return ret; |
| 4549 | } |
| 4550 | |
| 4551 | LY_ERR |
Michal Vasko | 405cc9e | 2020-12-01 12:01:27 +0100 | [diff] [blame] | 4552 | yang_parse_module(struct lys_yang_parser_ctx **context, struct ly_in *in, struct lys_module *mod, struct lys_glob_unres *unres) |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4553 | { |
| 4554 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 0a1d0d4 | 2019-05-16 15:14:51 +0200 | [diff] [blame] | 4555 | char *word; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4556 | size_t word_len; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 4557 | enum ly_stmt kw; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4558 | struct lysp_module *mod_p = NULL; |
| 4559 | |
David Sedlák | 1b62312 | 2019-08-05 15:27:49 +0200 | [diff] [blame] | 4560 | /* create context */ |
| 4561 | *context = calloc(1, sizeof **context); |
| 4562 | LY_CHECK_ERR_RET(!(*context), LOGMEM(mod->ctx), LY_EMEM); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 4563 | (*context)->format = LYS_IN_YANG; |
Michal Vasko | 405cc9e | 2020-12-01 12:01:27 +0100 | [diff] [blame] | 4564 | (*context)->unres = unres; |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 4565 | (*context)->in = in; |
David Sedlák | 1b62312 | 2019-08-05 15:27:49 +0200 | [diff] [blame] | 4566 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 4567 | mod_p = calloc(1, sizeof *mod_p); |
| 4568 | LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(mod->ctx), cleanup); |
| 4569 | mod_p->mod = mod; |
| 4570 | mod_p->parsing = 1; |
| 4571 | (*context)->parsed_mod = mod_p; |
| 4572 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 4573 | LOG_LOCINIT(NULL, NULL, NULL, in); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4574 | |
Michal Vasko | 69e6bbb | 2020-11-04 17:11:46 +0100 | [diff] [blame] | 4575 | /* skip redundant but valid characters at the beginning */ |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 4576 | ret = skip_redundant_chars(*context); |
Michal Vasko | 69e6bbb | 2020-11-04 17:11:46 +0100 | [diff] [blame] | 4577 | LY_CHECK_GOTO(ret, cleanup); |
| 4578 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4579 | /* "module"/"submodule" */ |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 4580 | ret = get_keyword(*context, &kw, &word, &word_len); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4581 | LY_CHECK_GOTO(ret, cleanup); |
| 4582 | |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 4583 | if (kw == LY_STMT_SUBMODULE) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 4584 | LOGERR(mod->ctx, LY_EDENIED, "Input data contains submodule which cannot be parsed directly without its main module."); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4585 | ret = LY_EINVAL; |
| 4586 | goto cleanup; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 4587 | } else if (kw != LY_STMT_MODULE) { |
David Sedlák | 1538a84 | 2019-08-08 15:38:51 +0200 | [diff] [blame] | 4588 | LOGVAL_PARSER((*context), LY_VCODE_MOD_SUBOMD, ly_stmt2str(kw)); |
Radek Krejci | 40544fa | 2019-01-11 09:38:37 +0100 | [diff] [blame] | 4589 | ret = LY_EVALID; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4590 | goto cleanup; |
| 4591 | } |
| 4592 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4593 | /* substatements */ |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 4594 | ret = parse_module(*context, mod_p); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4595 | LY_CHECK_GOTO(ret, cleanup); |
| 4596 | |
Michal Vasko | 69e6bbb | 2020-11-04 17:11:46 +0100 | [diff] [blame] | 4597 | /* skip redundant but valid characters at the end */ |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 4598 | ret = skip_redundant_chars(*context); |
Michal Vasko | 69e6bbb | 2020-11-04 17:11:46 +0100 | [diff] [blame] | 4599 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 4600 | if (in->current[0]) { |
| 4601 | LOGVAL_PARSER(*context, LY_VCODE_TRAILING_MOD, 15, in->current, strlen(in->current) > 15 ? "..." : ""); |
Radek Krejci | 40544fa | 2019-01-11 09:38:37 +0100 | [diff] [blame] | 4602 | ret = LY_EVALID; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4603 | goto cleanup; |
| 4604 | } |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4605 | |
| 4606 | mod_p->parsing = 0; |
| 4607 | mod->parsed = mod_p; |
| 4608 | |
| 4609 | cleanup: |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 4610 | LOG_LOCBACK(0, 0, 0, 1); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4611 | if (ret) { |
| 4612 | lysp_module_free(mod_p); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 4613 | yang_parser_ctx_free(*context); |
David Sedlák | 1b62312 | 2019-08-05 15:27:49 +0200 | [diff] [blame] | 4614 | *context = NULL; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 4615 | } |
| 4616 | |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4617 | return ret; |
| 4618 | } |