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 | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 14 | |
| 15 | #include "common.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 16 | |
| 17 | #include <assert.h> |
| 18 | #include <ctype.h> |
| 19 | #include <errno.h> |
| 20 | #include <stdint.h> |
| 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
| 24 | |
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" |
| 27 | #include "extensions.h" |
| 28 | #include "log.h" |
| 29 | #include "set.h" |
| 30 | #include "tree.h" |
| 31 | #include "tree_schema.h" |
Radek Krejci | 70853c5 | 2018-10-15 14:46:16 +0200 | [diff] [blame] | 32 | #include "tree_schema_internal.h" |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 33 | |
Radek Krejci | ceaf212 | 2019-01-02 15:03:26 +0100 | [diff] [blame] | 34 | /** |
Radek Krejci | ceaf212 | 2019-01-02 15:03:26 +0100 | [diff] [blame] | 35 | * @brief Insert WORD into the libyang context's dictionary and store as TARGET. |
| 36 | * @param[in] CTX yang parser context to access libyang context. |
| 37 | * @param[in] BUF buffer in case the word is not a constant and can be inserted directly (zero-copy) |
| 38 | * @param[out] TARGET variable where to store the pointer to the inserted value. |
| 39 | * @param[in] WORD string to store. |
| 40 | * @param[in] LEN length of the string in WORD to store. |
| 41 | */ |
Radek Krejci | 9fcacc1 | 2018-10-11 15:59:11 +0200 | [diff] [blame] | 42 | #define INSERT_WORD(CTX, BUF, TARGET, WORD, LEN) \ |
| 43 | if (BUF) {(TARGET) = lydict_insert_zc((CTX)->ctx, WORD);}\ |
Radek Krejci | f09e4e8 | 2019-06-14 15:08:11 +0200 | [diff] [blame] | 44 | else {(TARGET) = lydict_insert((CTX)->ctx, LEN ? WORD : "", LEN);} |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 45 | |
Radek Krejci | ceaf212 | 2019-01-02 15:03:26 +0100 | [diff] [blame] | 46 | /** |
| 47 | * @brief Move the DATA pointer by COUNT items. Also updates the indent value in yang parser context |
| 48 | * @param[in] CTX yang parser context to update its indent value. |
| 49 | * @param[in,out] DATA pointer to move |
| 50 | * @param[in] COUNT number of items for which the DATA pointer is supposed to move on. |
| 51 | */ |
Radek Krejci | 2b61048 | 2019-01-02 13:36:09 +0100 | [diff] [blame] | 52 | #define MOVE_INPUT(CTX, DATA, COUNT) (*(DATA))+=COUNT;(CTX)->indent+=COUNT |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 53 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 54 | /** |
| 55 | * @brief Loop through all substatements providing, return if there are none. |
| 56 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 57 | * @param[in] CTX yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 58 | * @param[in] DATA Raw data to read from. |
| 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 | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 66 | #define YANG_READ_SUBSTMT_FOR(CTX, DATA, KW, WORD, WORD_LEN, ERR, CHECKGOTO) \ |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 67 | LY_CHECK_RET(get_keyword(CTX, DATA, &KW, &WORD, &WORD_LEN)); \ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 68 | if (KW == YANG_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 | } \ |
| 72 | if (KW != YANG_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 | } \ |
| 76 | for (ERR = get_keyword(CTX, DATA, &KW, &WORD, &WORD_LEN); \ |
| 77 | !ERR && (KW != YANG_RIGHT_BRACE); \ |
| 78 | ERR = get_keyword(CTX, DATA, &KW, &WORD, &WORD_LEN)) |
| 79 | |
Radek Krejci | ceaf212 | 2019-01-02 15:03:26 +0100 | [diff] [blame] | 80 | /** |
| 81 | * @brief Check module version is at least 2 (YANG 1.1) because of the keyword presence. |
| 82 | * Logs error message and returns LY_EVALID in case of module in YANG version 1.0. |
| 83 | * @param[in] CTX yang parser context to get current module and for logging. |
| 84 | * @param[in] KW keyword allowed only in YANG version 1.1 (or later) - for logging. |
| 85 | * @param[in] PARENT parent statement where the KW is present - for logging. |
| 86 | */ |
| 87 | #define YANG_CHECK_STMTVER2_RET(CTX, KW, PARENT) \ |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 88 | if ((CTX)->mod_version < 2) {LOGVAL_PARSER((CTX), LY_VCODE_INCHILDSTMT2, KW, PARENT); return LY_EVALID;} |
Radek Krejci | 1011365 | 2018-11-14 16:56:50 +0100 | [diff] [blame] | 89 | |
Radek Krejci | 2d7a47b | 2019-05-16 13:34:10 +0200 | [diff] [blame] | 90 | LY_ERR parse_container(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings); |
| 91 | LY_ERR parse_uses(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings); |
| 92 | LY_ERR parse_choice(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings); |
| 93 | LY_ERR parse_case(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings); |
| 94 | LY_ERR parse_list(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings); |
| 95 | LY_ERR parse_grouping(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_grp **groupings); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 96 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 97 | /** |
| 98 | * @brief Add another character to dynamic buffer, a low-level function. |
| 99 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 100 | * Enlarge if needed. Updates \p input as well as \p buf_used. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 101 | * |
Radek Krejci | 404251e | 2018-10-09 12:06:44 +0200 | [diff] [blame] | 102 | * @param[in] ctx libyang context for logging. |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 103 | * @param[in, out] input Input string to process. |
| 104 | * @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] | 105 | * @param[in,out] buf Buffer to use, can be moved by realloc(). |
| 106 | * @param[in,out] buf_len Current size of the buffer. |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 107 | * @param[in,out] buf_used Currently used characters of the buffer. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 108 | * |
| 109 | * @return LY_ERR values. |
| 110 | */ |
Radek Krejci | 2d7a47b | 2019-05-16 13:34:10 +0200 | [diff] [blame] | 111 | LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 112 | buf_add_char(struct ly_ctx *ctx, const char **input, 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] | 113 | { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 114 | if (*buf_len <= (*buf_used) + len) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 115 | *buf_len += 16; |
| 116 | *buf = ly_realloc(*buf, *buf_len); |
| 117 | LY_CHECK_ERR_RET(!*buf, LOGMEM(ctx), LY_EMEM); |
| 118 | } |
Radek Krejci | c091739 | 2019-04-10 13:04:04 +0200 | [diff] [blame] | 119 | if (*buf_used) { |
| 120 | memcpy(&(*buf)[*buf_used], *input, len); |
| 121 | } else { |
| 122 | memcpy(*buf, *input, len); |
| 123 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 124 | |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 125 | (*buf_used) += len; |
| 126 | (*input) += len; |
| 127 | |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 128 | return LY_SUCCESS; |
| 129 | } |
| 130 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 131 | /** |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 132 | * @brief Store a single UTF8 character. It depends whether in a dynamically-allocated buffer or just as a pointer to the data. |
| 133 | * |
| 134 | * @param[in] ctx yang parser context for logging. |
| 135 | * @param[in,out] input Pointer to the string where to get the character to store. Automatically moved to the next character |
| 136 | * when function returns. |
| 137 | * @param[in] arg Type of the input string to select method of checking character validity. |
| 138 | * @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] | 139 | * 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] | 140 | * @param[in,out] word_len Current length of the word pointed to by \p word_p. |
| 141 | * @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] | 142 | * @param[in,out] buf_len Current length of \p word_b. |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 143 | * @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] | 144 | * @param[in,out] prefix Storage for internally used flag in case of possible prefixed identifiers: |
| 145 | * 0 - colon not yet found (no prefix) |
| 146 | * 1 - \p c is the colon character |
| 147 | * 2 - prefix already processed, now processing the identifier |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 148 | * |
| 149 | * @return LY_ERR values. |
| 150 | */ |
Radek Krejci | 2d7a47b | 2019-05-16 13:34:10 +0200 | [diff] [blame] | 151 | LY_ERR |
David Sedlák | 40bb13b | 2019-07-10 14:34:18 +0200 | [diff] [blame] | 152 | buf_store_char(struct lys_parser_ctx *ctx, const char **input, enum yang_arg arg, char **word_p, |
| 153 | size_t *word_len, char **word_b, size_t *buf_len, int need_buf, int *prefix) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 154 | { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 155 | unsigned int c; |
| 156 | size_t len; |
| 157 | |
Radek Krejci | f29b7c3 | 2019-04-09 16:17:49 +0200 | [diff] [blame] | 158 | /* check valid combination of input paremeters - if need_buf specified, word_b must be provided */ |
| 159 | assert(!need_buf || (need_buf && word_b)); |
| 160 | |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 161 | /* get UTF8 code point (and number of bytes coding the character) */ |
| 162 | LY_CHECK_ERR_RET(ly_getutf8(input, &c, &len), |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 163 | LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, (*input)[-len]), LY_EVALID); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 164 | (*input) -= len; |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 165 | if (c == '\n') { |
| 166 | ctx->indent = 0; |
| 167 | } else { |
| 168 | /* note - even the multibyte character is count as 1 */ |
| 169 | ++ctx->indent; |
| 170 | } |
| 171 | |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 172 | /* check character validity */ |
| 173 | switch (arg) { |
| 174 | case Y_IDENTIF_ARG: |
David Sedlák | 4a65053 | 2019-07-10 11:55:18 +0200 | [diff] [blame] | 175 | LY_CHECK_RET(lysp_check_identifierchar(ctx, c, !(*word_len), NULL)); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 176 | break; |
| 177 | case Y_PREF_IDENTIF_ARG: |
David Sedlák | 40bb13b | 2019-07-10 14:34:18 +0200 | [diff] [blame] | 178 | LY_CHECK_RET(lysp_check_identifierchar(ctx, c, !(*word_len), prefix)); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 179 | break; |
| 180 | case Y_STR_ARG: |
| 181 | case Y_MAYBE_STR_ARG: |
David Sedlák | 4a65053 | 2019-07-10 11:55:18 +0200 | [diff] [blame] | 182 | LY_CHECK_RET(lysp_check_stringchar(ctx, c)); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 183 | break; |
| 184 | } |
| 185 | |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 186 | if (word_b && *word_b) { |
| 187 | /* add another character into buffer */ |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 188 | if (buf_add_char(ctx->ctx, input, len, word_b, buf_len, word_len)) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 189 | return LY_EMEM; |
| 190 | } |
| 191 | |
| 192 | /* in case of realloc */ |
| 193 | *word_p = *word_b; |
Radek Krejci | f29b7c3 | 2019-04-09 16:17:49 +0200 | [diff] [blame] | 194 | } else if (word_b && need_buf) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 195 | /* first time we need a buffer, copy everything read up to now */ |
| 196 | if (*word_len) { |
| 197 | *word_b = malloc(*word_len); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 198 | LY_CHECK_ERR_RET(!*word_b, LOGMEM(ctx->ctx), LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 199 | *buf_len = *word_len; |
| 200 | memcpy(*word_b, *word_p, *word_len); |
| 201 | } |
| 202 | |
| 203 | /* add this new character into buffer */ |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 204 | if (buf_add_char(ctx->ctx, input, len, word_b, buf_len, word_len)) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 205 | return LY_EMEM; |
| 206 | } |
| 207 | |
| 208 | /* in case of realloc */ |
| 209 | *word_p = *word_b; |
| 210 | } else { |
| 211 | /* just remember the first character pointer */ |
| 212 | if (!*word_p) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 213 | *word_p = (char *)(*input); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 214 | } |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 215 | /* ... and update the word's length */ |
| 216 | (*word_len) += len; |
| 217 | (*input) += len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | return LY_SUCCESS; |
| 221 | } |
| 222 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 223 | /** |
| 224 | * @brief Skip YANG comment in data. |
| 225 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 226 | * @param[in] ctx yang parser context for logging. |
| 227 | * @param[in,out] data Data to read from, automatically moved after the comment. |
| 228 | * @param[in] comment Type of the comment to process: |
| 229 | * 1 for a one-line comment, |
| 230 | * 2 for a block comment. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 231 | * |
| 232 | * @return LY_ERR values. |
| 233 | */ |
Radek Krejci | 2d7a47b | 2019-05-16 13:34:10 +0200 | [diff] [blame] | 234 | LY_ERR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 235 | skip_comment(struct lys_parser_ctx *ctx, const char **data, int comment) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 236 | { |
Radek Krejci | 80dd33e | 2018-09-26 15:57:18 +0200 | [diff] [blame] | 237 | /* internal statuses: 0 - comment ended, |
| 238 | * 1 - in line comment, |
| 239 | * 2 - in block comment, |
| 240 | * 3 - in block comment with last read character '*' |
| 241 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 242 | while (**data && comment) { |
| 243 | switch (comment) { |
| 244 | case 1: |
| 245 | if (**data == '\n') { |
| 246 | comment = 0; |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 247 | ++ctx->line; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 248 | } |
| 249 | break; |
| 250 | case 2: |
| 251 | if (**data == '*') { |
Radek Krejci | 15c80ca | 2018-10-09 11:01:31 +0200 | [diff] [blame] | 252 | comment = 3; |
| 253 | } else if (**data == '\n') { |
| 254 | ++ctx->line; |
| 255 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 256 | break; |
| 257 | case 3: |
| 258 | if (**data == '/') { |
| 259 | comment = 0; |
Radek Krejci | 5b93049 | 2019-06-11 14:54:08 +0200 | [diff] [blame] | 260 | } else if (**data != '*') { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 261 | if (**data == '\n') { |
| 262 | ++ctx->line; |
| 263 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 264 | comment = 2; |
| 265 | } |
| 266 | break; |
| 267 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 268 | LOGINT_RET(ctx->ctx); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 269 | } |
| 270 | |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 271 | if (**data == '\n') { |
| 272 | ctx->indent = 0; |
| 273 | } else { |
| 274 | ++ctx->indent; |
| 275 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 276 | ++(*data); |
| 277 | } |
| 278 | |
| 279 | if (!**data && (comment > 1)) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 280 | LOGVAL_PARSER(ctx, LYVE_SYNTAX, "Unexpected end-of-input, non-terminated comment."); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 281 | return LY_EVALID; |
| 282 | } |
| 283 | |
| 284 | return LY_SUCCESS; |
| 285 | } |
| 286 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 287 | /** |
| 288 | * @brief Read a quoted string from data. |
| 289 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 290 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 291 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 292 | * @param[in] arg Type of YANG keyword argument expected. |
| 293 | * @param[out] word_p Pointer to the read quoted string. |
| 294 | * @param[out] word_b Pointer to a dynamically-allocated buffer holding the read quoted string. If not needed, |
| 295 | * set to NULL. Otherwise equal to \p word_p. |
| 296 | * @param[out] word_len Length of the read quoted string. |
| 297 | * @param[out] buf_len Length of the dynamically-allocated buffer \p word_b. |
| 298 | * @param[in] indent Current indent (number of YANG spaces). Needed for correct multi-line string |
| 299 | * indenation in the final quoted string. |
| 300 | * |
| 301 | * @return LY_ERR values. |
| 302 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 303 | static LY_ERR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 304 | read_qstring(struct lys_parser_ctx *ctx, const char **data, enum yang_arg arg, char **word_p, char **word_b, size_t *word_len, |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 305 | size_t *buf_len) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 306 | { |
| 307 | /* string: 0 - string ended, 1 - string with ', 2 - string with ", 3 - string with " with last character \, |
| 308 | * 4 - string finished, now skipping whitespaces looking for +, |
| 309 | * 5 - string continues after +, skipping whitespaces */ |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 310 | unsigned int string, block_indent = 0, current_indent = 0, need_buf = 0; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 311 | const char *c; |
David Sedlák | 40bb13b | 2019-07-10 14:34:18 +0200 | [diff] [blame] | 312 | int prefix = 0; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 313 | |
| 314 | if (**data == '\"') { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 315 | string = 2; |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 316 | current_indent = block_indent = ctx->indent + 1; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 317 | } else { |
| 318 | assert(**data == '\''); |
| 319 | string = 1; |
| 320 | } |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 321 | MOVE_INPUT(ctx, data, 1); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 322 | |
| 323 | while (**data && string) { |
| 324 | switch (string) { |
| 325 | case 1: |
| 326 | switch (**data) { |
| 327 | case '\'': |
| 328 | /* string may be finished, but check for + */ |
| 329 | string = 4; |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 330 | MOVE_INPUT(ctx, data, 1); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 331 | break; |
| 332 | default: |
| 333 | /* check and store character */ |
David Sedlák | 40bb13b | 2019-07-10 14:34:18 +0200 | [diff] [blame] | 334 | LY_CHECK_RET(buf_store_char(ctx, data, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 335 | break; |
| 336 | } |
| 337 | break; |
| 338 | case 2: |
| 339 | switch (**data) { |
| 340 | case '\"': |
| 341 | /* string may be finished, but check for + */ |
| 342 | string = 4; |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 343 | MOVE_INPUT(ctx, data, 1); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 344 | break; |
| 345 | case '\\': |
| 346 | /* special character following */ |
| 347 | string = 3; |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 348 | ++(*data); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 349 | break; |
| 350 | case ' ': |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 351 | if (current_indent < block_indent) { |
| 352 | ++current_indent; |
| 353 | MOVE_INPUT(ctx, data, 1); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 354 | } else { |
| 355 | /* check and store character */ |
David Sedlák | 40bb13b | 2019-07-10 14:34:18 +0200 | [diff] [blame] | 356 | LY_CHECK_RET(buf_store_char(ctx, data, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 357 | } |
| 358 | break; |
| 359 | case '\t': |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 360 | if (current_indent < block_indent) { |
| 361 | assert(need_buf); |
| 362 | current_indent += 8; |
| 363 | ctx->indent += 8; |
| 364 | for (; current_indent > block_indent; --current_indent, --ctx->indent) { |
| 365 | /* store leftover spaces from the tab */ |
| 366 | c = " "; |
David Sedlák | 40bb13b | 2019-07-10 14:34:18 +0200 | [diff] [blame] | 367 | LY_CHECK_RET(buf_store_char(ctx, &c, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 368 | } |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 369 | ++(*data); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 370 | } else { |
| 371 | /* check and store character */ |
David Sedlák | 40bb13b | 2019-07-10 14:34:18 +0200 | [diff] [blame] | 372 | LY_CHECK_RET(buf_store_char(ctx, data, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix)); |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 373 | /* additional characters for indentation - only 1 was count in buf_store_char */ |
| 374 | ctx->indent += 7; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 375 | } |
| 376 | break; |
| 377 | case '\n': |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 378 | if (block_indent) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 379 | /* we will be removing the indents so we need our own buffer */ |
| 380 | need_buf = 1; |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 381 | |
| 382 | /* remove trailing tabs and spaces */ |
| 383 | while ((*word_len) && ((*word_p)[(*word_len) - 1] == '\t' || (*word_p)[(*word_len) - 1] == ' ')) { |
| 384 | --(*word_len); |
| 385 | } |
| 386 | |
| 387 | /* start indentation */ |
| 388 | current_indent = 0; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | /* check and store character */ |
David Sedlák | 40bb13b | 2019-07-10 14:34:18 +0200 | [diff] [blame] | 392 | LY_CHECK_RET(buf_store_char(ctx, data, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix)); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 393 | |
| 394 | /* maintain line number */ |
| 395 | ++ctx->line; |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 396 | |
| 397 | /* reset context indentation counter for possible string after this one */ |
| 398 | ctx->indent = 0; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 399 | break; |
| 400 | default: |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 401 | /* first non-whitespace character, stop eating indentation */ |
| 402 | current_indent = block_indent; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 403 | |
| 404 | /* check and store character */ |
David Sedlák | 40bb13b | 2019-07-10 14:34:18 +0200 | [diff] [blame] | 405 | LY_CHECK_RET(buf_store_char(ctx, data, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 406 | break; |
| 407 | } |
| 408 | break; |
| 409 | case 3: |
| 410 | /* string encoded characters */ |
| 411 | switch (**data) { |
| 412 | case 'n': |
| 413 | c = "\n"; |
| 414 | break; |
| 415 | case 't': |
| 416 | c = "\t"; |
| 417 | break; |
| 418 | case '\"': |
| 419 | c = *data; |
| 420 | break; |
| 421 | case '\\': |
| 422 | c = *data; |
| 423 | break; |
| 424 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 425 | LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Double-quoted string unknown special character '\\%c'.", **data); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 426 | return LY_EVALID; |
| 427 | } |
| 428 | |
| 429 | /* check and store character */ |
David Sedlák | 40bb13b | 2019-07-10 14:34:18 +0200 | [diff] [blame] | 430 | LY_CHECK_RET(buf_store_char(ctx, &c, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 431 | |
| 432 | string = 2; |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 433 | ++(*data); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 434 | break; |
| 435 | case 4: |
| 436 | switch (**data) { |
| 437 | case '+': |
| 438 | /* string continues */ |
| 439 | string = 5; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 440 | need_buf = 1; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 441 | break; |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 442 | case '\n': |
| 443 | ++ctx->line; |
| 444 | /* fallthrough */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 445 | case ' ': |
| 446 | case '\t': |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 447 | /* just skip */ |
| 448 | break; |
| 449 | default: |
| 450 | /* string is finished */ |
| 451 | goto string_end; |
| 452 | } |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 453 | MOVE_INPUT(ctx, data, 1); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 454 | break; |
| 455 | case 5: |
| 456 | switch (**data) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 457 | case '\n': |
| 458 | ++ctx->line; |
| 459 | /* fallthrough */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 460 | case ' ': |
| 461 | case '\t': |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 462 | /* skip */ |
| 463 | break; |
| 464 | case '\'': |
| 465 | string = 1; |
| 466 | break; |
| 467 | case '\"': |
| 468 | string = 2; |
| 469 | break; |
| 470 | default: |
| 471 | /* it must be quoted again */ |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 472 | 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] | 473 | return LY_EVALID; |
| 474 | } |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 475 | MOVE_INPUT(ctx, data, 1); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 476 | break; |
| 477 | default: |
| 478 | return LY_EINT; |
| 479 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | string_end: |
Radek Krejci | 4e199f5 | 2019-05-28 09:09:28 +0200 | [diff] [blame] | 483 | if (arg <= Y_PREF_IDENTIF_ARG && !(*word_len)) { |
| 484 | /* empty identifier */ |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 485 | LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Statement argument is required."); |
Radek Krejci | 4e199f5 | 2019-05-28 09:09:28 +0200 | [diff] [blame] | 486 | return LY_EVALID; |
| 487 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 488 | return LY_SUCCESS; |
| 489 | } |
| 490 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 491 | /** |
| 492 | * @brief Get another YANG string from the raw data. |
| 493 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 494 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 495 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 496 | * @param[in] arg Type of YANG keyword argument expected. |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 497 | * @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] | 498 | * @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] | 499 | * @param[out] word_b Pointer to a dynamically-allocated buffer holding the read string. If not needed, |
| 500 | * set to NULL. Otherwise equal to \p word_p. |
| 501 | * @param[out] word_len Length of the read string. |
| 502 | * |
| 503 | * @return LY_ERR values. |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 504 | */ |
Radek Krejci | 2d7a47b | 2019-05-16 13:34:10 +0200 | [diff] [blame] | 505 | LY_ERR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 506 | get_argument(struct lys_parser_ctx *ctx, const char **data, enum yang_arg arg, |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 507 | uint16_t *flags, char **word_p, char **word_b, size_t *word_len) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 508 | { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 509 | size_t buf_len = 0; |
David Sedlák | 40bb13b | 2019-07-10 14:34:18 +0200 | [diff] [blame] | 510 | int prefix = 0; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 511 | /* word buffer - dynamically allocated */ |
| 512 | *word_b = NULL; |
| 513 | |
| 514 | /* word pointer - just a pointer to data */ |
| 515 | *word_p = NULL; |
| 516 | |
| 517 | *word_len = 0; |
| 518 | while (**data) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 519 | switch (**data) { |
| 520 | case '\'': |
| 521 | case '\"': |
| 522 | if (*word_len) { |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 523 | /* invalid - quotes cannot be in unquoted string and only optsep, ; or { can follow it */ |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 524 | LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, 1, *data, |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 525 | "unquoted string character, optsep, semicolon or opening brace"); |
| 526 | return LY_EVALID; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 527 | } |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 528 | if (flags) { |
| 529 | (*flags) |= (**data) == '\'' ? LYS_SINGLEQUOTED : LYS_DOUBLEQUOTED; |
| 530 | } |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 531 | LY_CHECK_RET(read_qstring(ctx, data, arg, word_p, word_b, word_len, &buf_len)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 532 | goto str_end; |
| 533 | case '/': |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 534 | if ((*data)[1] == '/') { |
| 535 | /* one-line comment */ |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 536 | MOVE_INPUT(ctx, data, 2); |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 537 | LY_CHECK_RET(skip_comment(ctx, data, 1)); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 538 | } else if ((*data)[1] == '*') { |
| 539 | /* block comment */ |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 540 | MOVE_INPUT(ctx, data, 2); |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 541 | LY_CHECK_RET(skip_comment(ctx, data, 2)); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 542 | } else { |
| 543 | /* not a comment after all */ |
David Sedlák | 40bb13b | 2019-07-10 14:34:18 +0200 | [diff] [blame] | 544 | LY_CHECK_RET(buf_store_char(ctx, data, arg, word_p, word_len, word_b, &buf_len, 0, &prefix)); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 545 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 546 | break; |
| 547 | case ' ': |
| 548 | if (*word_len) { |
| 549 | /* word is finished */ |
| 550 | goto str_end; |
| 551 | } |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 552 | MOVE_INPUT(ctx, data, 1); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 553 | break; |
| 554 | case '\t': |
| 555 | if (*word_len) { |
| 556 | /* word is finished */ |
| 557 | goto str_end; |
| 558 | } |
| 559 | /* tabs count for 8 spaces */ |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 560 | ctx->indent += 8; |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 561 | |
| 562 | ++(*data); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 563 | break; |
| 564 | case '\n': |
| 565 | if (*word_len) { |
| 566 | /* word is finished */ |
| 567 | goto str_end; |
| 568 | } |
| 569 | /* reset indent */ |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 570 | ctx->indent = 0; |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 571 | |
| 572 | /* track line numbers */ |
| 573 | ++ctx->line; |
| 574 | |
| 575 | ++(*data); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 576 | break; |
| 577 | case ';': |
| 578 | case '{': |
| 579 | if (*word_len || (arg == Y_MAYBE_STR_ARG)) { |
| 580 | /* word is finished */ |
| 581 | goto str_end; |
| 582 | } |
| 583 | |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 584 | LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, 1, *data, "an argument"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 585 | return LY_EVALID; |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 586 | case '}': |
| 587 | /* invalid - braces cannot be in unquoted string (opening braces terminates the string and can follow it) */ |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 588 | LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, 1, *data, |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 589 | "unquoted string character, optsep, semicolon or opening brace"); |
| 590 | return LY_EVALID; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 591 | default: |
David Sedlák | 40bb13b | 2019-07-10 14:34:18 +0200 | [diff] [blame] | 592 | LY_CHECK_RET(buf_store_char(ctx, data, arg, word_p, word_len, word_b, &buf_len, 0, &prefix)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 593 | break; |
| 594 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 595 | } |
| 596 | |
Radek Krejci | 0a1d0d4 | 2019-05-16 15:14:51 +0200 | [diff] [blame] | 597 | /* unexpected end of loop */ |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 598 | LOGVAL_PARSER(ctx, LY_VCODE_EOF); |
Radek Krejci | 0a1d0d4 | 2019-05-16 15:14:51 +0200 | [diff] [blame] | 599 | return LY_EVALID; |
| 600 | |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 601 | str_end: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 602 | /* terminating NULL byte for buf */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 603 | if (*word_b) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 604 | (*word_b) = ly_realloc(*word_b, (*word_len) + 1); |
| 605 | LY_CHECK_ERR_RET(!(*word_b), LOGMEM(ctx->ctx), LY_EMEM); |
| 606 | (*word_b)[*word_len] = '\0'; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 607 | *word_p = *word_b; |
| 608 | } |
| 609 | |
| 610 | return LY_SUCCESS; |
| 611 | } |
| 612 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 613 | /** |
| 614 | * @brief Get another YANG keyword from the raw data. |
| 615 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 616 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 617 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 618 | * @param[out] kw YANG keyword read. |
| 619 | * @param[out] word_p Pointer to the keyword in the data. Useful for extension instances. |
| 620 | * @param[out] word_len Length of the keyword in the data. Useful for extension instances. |
| 621 | * |
| 622 | * @return LY_ERR values. |
| 623 | */ |
Radek Krejci | 2d7a47b | 2019-05-16 13:34:10 +0200 | [diff] [blame] | 624 | LY_ERR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 625 | get_keyword(struct lys_parser_ctx *ctx, const char **data, enum yang_keyword *kw, char **word_p, size_t *word_len) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 626 | { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 627 | int prefix; |
| 628 | const char *word_start; |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 629 | unsigned int c; |
| 630 | size_t len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 631 | |
| 632 | if (word_p) { |
| 633 | *word_p = NULL; |
| 634 | *word_len = 0; |
| 635 | } |
| 636 | |
| 637 | /* first skip "optsep", comments */ |
| 638 | while (**data) { |
Radek Krejci | dcc7b32 | 2018-10-11 14:24:02 +0200 | [diff] [blame] | 639 | switch (**data) { |
| 640 | case '/': |
| 641 | if ((*data)[1] == '/') { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 642 | /* one-line comment */ |
Radek Krejci | dcc7b32 | 2018-10-11 14:24:02 +0200 | [diff] [blame] | 643 | MOVE_INPUT(ctx, data, 2); |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 644 | LY_CHECK_RET(skip_comment(ctx, data, 1)); |
Radek Krejci | dcc7b32 | 2018-10-11 14:24:02 +0200 | [diff] [blame] | 645 | } else if ((*data)[1] == '*') { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 646 | /* block comment */ |
Radek Krejci | dcc7b32 | 2018-10-11 14:24:02 +0200 | [diff] [blame] | 647 | MOVE_INPUT(ctx, data, 2); |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 648 | LY_CHECK_RET(skip_comment(ctx, data, 2)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 649 | } else { |
Radek Krejci | dcc7b32 | 2018-10-11 14:24:02 +0200 | [diff] [blame] | 650 | /* error - not a comment after all, keyword cannot start with slash */ |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 651 | LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid identifier first character '/'."); |
Radek Krejci | dcc7b32 | 2018-10-11 14:24:02 +0200 | [diff] [blame] | 652 | return LY_EVALID; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 653 | } |
Radek Krejci | 1302828 | 2019-06-11 14:56:48 +0200 | [diff] [blame] | 654 | continue; |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 655 | case '\n': |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 656 | /* skip whitespaces (optsep) */ |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 657 | ++ctx->line; |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 658 | ctx->indent = 0; |
| 659 | break; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 660 | case ' ': |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 661 | /* skip whitespaces (optsep) */ |
| 662 | ++ctx->indent; |
| 663 | break; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 664 | case '\t': |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 665 | /* skip whitespaces (optsep) */ |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 666 | ctx->indent += 8; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 667 | break; |
| 668 | default: |
| 669 | /* either a keyword start or an invalid character */ |
| 670 | goto keyword_start; |
| 671 | } |
| 672 | |
| 673 | ++(*data); |
| 674 | } |
| 675 | |
| 676 | keyword_start: |
| 677 | word_start = *data; |
David Sedlák | 5f8f033 | 2019-06-18 16:34:30 +0200 | [diff] [blame] | 678 | *kw = lysp_match_kw(ctx, data); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 679 | |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 680 | if (*kw == YANG_SEMICOLON || *kw == YANG_LEFT_BRACE || *kw == YANG_RIGHT_BRACE) { |
Radek Krejci | 626df48 | 2018-10-11 15:06:31 +0200 | [diff] [blame] | 681 | goto success; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 682 | } |
| 683 | |
| 684 | if (*kw != YANG_NONE) { |
| 685 | /* make sure we have the whole keyword */ |
| 686 | switch (**data) { |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 687 | case '\n': |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 688 | case '\t': |
Radek Krejci | abdd806 | 2019-06-11 16:44:19 +0200 | [diff] [blame] | 689 | case ' ': |
| 690 | /* 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] | 691 | break; |
Radek Krejci | 156ccaf | 2018-10-15 15:49:17 +0200 | [diff] [blame] | 692 | case ':': |
| 693 | /* keyword is not actually a keyword, but prefix of an extension. |
| 694 | * To avoid repeated check of the prefix syntax, move to the point where the colon was read |
| 695 | * and we will be checking the keyword (extension instance) itself */ |
| 696 | prefix = 1; |
| 697 | MOVE_INPUT(ctx, data, 1); |
| 698 | goto extension; |
Radek Krejci | dcc7b32 | 2018-10-11 14:24:02 +0200 | [diff] [blame] | 699 | case '{': |
| 700 | /* allowed only for input and output statements which can be without arguments */ |
| 701 | if (*kw == YANG_INPUT || *kw == YANG_OUTPUT) { |
| 702 | break; |
| 703 | } |
| 704 | /* fallthrough */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 705 | default: |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 706 | MOVE_INPUT(ctx, data, 1); |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 707 | LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, (int)(*data - word_start), word_start, |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 708 | "a keyword followed by a separator"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 709 | return LY_EVALID; |
| 710 | } |
| 711 | } else { |
| 712 | /* still can be an extension */ |
| 713 | prefix = 0; |
Radek Krejci | 156ccaf | 2018-10-15 15:49:17 +0200 | [diff] [blame] | 714 | extension: |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 715 | while (**data && (**data != ' ') && (**data != '\t') && (**data != '\n') && (**data != '{') && (**data != ';')) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 716 | LY_CHECK_ERR_RET(ly_getutf8(data, &c, &len), |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 717 | LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, (*data)[-len]), LY_EVALID); |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 718 | ++ctx->indent; |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 719 | /* check character validity */ |
David Sedlák | 4a65053 | 2019-07-10 11:55:18 +0200 | [diff] [blame] | 720 | LY_CHECK_RET(lysp_check_identifierchar(ctx, c, *data - len == word_start ? 1 : 0, &prefix)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 721 | } |
| 722 | if (!**data) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 723 | LOGVAL_PARSER(ctx, LY_VCODE_EOF); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 724 | return LY_EVALID; |
| 725 | } |
| 726 | |
| 727 | /* prefix is mandatory for extension instances */ |
Radek Krejci | dcc7b32 | 2018-10-11 14:24:02 +0200 | [diff] [blame] | 728 | if (prefix != 2) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 729 | LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, (int)(*data - word_start), word_start, "a keyword"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 730 | return LY_EVALID; |
| 731 | } |
| 732 | |
| 733 | *kw = YANG_CUSTOM; |
| 734 | } |
Radek Krejci | 626df48 | 2018-10-11 15:06:31 +0200 | [diff] [blame] | 735 | success: |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 736 | if (word_p) { |
| 737 | *word_p = (char *)word_start; |
| 738 | *word_len = *data - word_start; |
| 739 | } |
| 740 | |
| 741 | return LY_SUCCESS; |
| 742 | } |
| 743 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 744 | /** |
| 745 | * @brief Parse extension instance substatements. |
| 746 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 747 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 748 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 749 | * @param[in] word Extension instance substatement name (keyword). |
| 750 | * @param[in] word_len Extension instance substatement name length. |
| 751 | * @param[in,out] child Children of this extension instance to add to. |
| 752 | * |
| 753 | * @return LY_ERR values. |
| 754 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 755 | static LY_ERR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 756 | parse_ext_substmt(struct lys_parser_ctx *ctx, const char **data, char *word, size_t word_len, |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 757 | struct lysp_stmt **child) |
| 758 | { |
| 759 | char *buf; |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 760 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 761 | enum yang_keyword kw; |
| 762 | struct lysp_stmt *stmt, *par_child; |
| 763 | |
| 764 | stmt = calloc(1, sizeof *stmt); |
| 765 | LY_CHECK_ERR_RET(!stmt, LOGMEM(NULL), LY_EMEM); |
| 766 | |
Radek Krejci | bb9b198 | 2019-04-08 14:24:59 +0200 | [diff] [blame] | 767 | /* insert into parent statements */ |
| 768 | if (!*child) { |
| 769 | *child = stmt; |
| 770 | } else { |
| 771 | for (par_child = *child; par_child->next; par_child = par_child->next); |
| 772 | par_child->next = stmt; |
| 773 | } |
| 774 | |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 775 | stmt->stmt = lydict_insert(ctx->ctx, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 776 | |
| 777 | /* get optional argument */ |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 778 | LY_CHECK_RET(get_argument(ctx, data, Y_MAYBE_STR_ARG, &stmt->flags, &word, &buf, &word_len)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 779 | |
Radek Krejci | 0ae092d | 2018-09-20 16:43:19 +0200 | [diff] [blame] | 780 | if (word) { |
| 781 | if (buf) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 782 | stmt->arg = lydict_insert_zc(ctx->ctx, word); |
Radek Krejci | 0ae092d | 2018-09-20 16:43:19 +0200 | [diff] [blame] | 783 | } else { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 784 | stmt->arg = lydict_insert(ctx->ctx, word, word_len); |
Radek Krejci | 0ae092d | 2018-09-20 16:43:19 +0200 | [diff] [blame] | 785 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 786 | } |
| 787 | |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 788 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, ) { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 789 | LY_CHECK_RET(parse_ext_substmt(ctx, data, word, word_len, &stmt->child)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 790 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 791 | return ret; |
| 792 | } |
| 793 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 794 | /** |
| 795 | * @brief Parse extension instance. |
| 796 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 797 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 798 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 799 | * @param[in] ext_name Extension instance substatement name (keyword). |
| 800 | * @param[in] ext_name_len Extension instance substatement name length. |
| 801 | * @param[in] insubstmt Type of the keyword this extension instance is a substatement of. |
| 802 | * @param[in] insubstmt_index Index of the keyword instance this extension instance is a substatement of. |
| 803 | * @param[in,out] exts Extension instances to add to. |
| 804 | * |
| 805 | * @return LY_ERR values. |
| 806 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 807 | static LY_ERR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 808 | parse_ext(struct lys_parser_ctx *ctx, const char **data, const char *ext_name, int ext_name_len, LYEXT_SUBSTMT insubstmt, |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 809 | uint32_t insubstmt_index, struct lysp_ext_instance **exts) |
| 810 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 811 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 812 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 813 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 814 | struct lysp_ext_instance *e; |
| 815 | enum yang_keyword kw; |
| 816 | |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 817 | LY_ARRAY_NEW_RET(ctx->ctx, *exts, e, LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 818 | |
| 819 | /* store name and insubstmt info */ |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 820 | e->name = lydict_insert(ctx->ctx, ext_name, ext_name_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 821 | e->insubstmt = insubstmt; |
| 822 | e->insubstmt_index = insubstmt_index; |
| 823 | |
| 824 | /* get optional argument */ |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 825 | LY_CHECK_RET(get_argument(ctx, data, Y_MAYBE_STR_ARG, NULL, &word, &buf, &word_len)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 826 | |
Radek Krejci | 0ae092d | 2018-09-20 16:43:19 +0200 | [diff] [blame] | 827 | if (word) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 828 | INSERT_WORD(ctx, buf, e->argument, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 829 | } |
| 830 | |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 831 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 832 | LY_CHECK_RET(parse_ext_substmt(ctx, data, word, word_len, &e->child)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 833 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 834 | return ret; |
| 835 | } |
| 836 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 837 | /** |
| 838 | * @brief Parse a generic text field without specific constraints. Those are contact, organization, |
| 839 | * description, etc... |
| 840 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 841 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 842 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 843 | * @param[in] substmt Type of this substatement. |
| 844 | * @param[in] substmt_index Index of this substatement. |
| 845 | * @param[in,out] value Place to store the parsed value. |
| 846 | * @param[in] arg Type of the YANG keyword argument (of the value). |
| 847 | * @param[in,out] exts Extension instances to add to. |
| 848 | * |
| 849 | * @return LY_ERR values. |
| 850 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 851 | static LY_ERR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 852 | parse_text_field(struct lys_parser_ctx *ctx, const char **data, LYEXT_SUBSTMT substmt, uint32_t substmt_index, |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 853 | const char **value, enum yang_arg arg, struct lysp_ext_instance **exts) |
| 854 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 855 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 856 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 857 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 858 | enum yang_keyword kw; |
| 859 | |
| 860 | if (*value) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 861 | LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, lyext_substmt2str(substmt)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 862 | return LY_EVALID; |
| 863 | } |
| 864 | |
| 865 | /* get value */ |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 866 | LY_CHECK_RET(get_argument(ctx, data, arg, NULL, &word, &buf, &word_len)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 867 | |
| 868 | /* store value and spend buf if allocated */ |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 869 | INSERT_WORD(ctx, buf, *value, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 870 | |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 871 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 872 | switch (kw) { |
| 873 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 874 | LY_CHECK_RET(parse_ext(ctx, data, word, word_len, substmt, substmt_index, exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 875 | break; |
| 876 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 877 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), lyext_substmt2str(substmt)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 878 | return LY_EVALID; |
| 879 | } |
| 880 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 881 | return ret; |
| 882 | } |
| 883 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 884 | /** |
| 885 | * @brief Parse the yang-version statement. |
| 886 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 887 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 888 | * @param[in,out] data Data to read from, always moved to currently handled character. |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 889 | * @param[out] version Storage for the parsed information. |
| 890 | * @param[in, out] exts Extension instances to add to. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 891 | * |
| 892 | * @return LY_ERR values. |
| 893 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 894 | static LY_ERR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 895 | parse_yangversion(struct lys_parser_ctx *ctx, const char **data, uint8_t *version, struct lysp_ext_instance **exts) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 896 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 897 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 898 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 899 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 900 | enum yang_keyword kw; |
| 901 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 902 | if (*version) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 903 | LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "yang-version"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 904 | return LY_EVALID; |
| 905 | } |
| 906 | |
| 907 | /* get value */ |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 908 | LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 909 | |
| 910 | if ((word_len == 3) && !strncmp(word, "1.0", word_len)) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 911 | *version = LYS_VERSION_1_0; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 912 | } else if ((word_len == 3) && !strncmp(word, "1.1", word_len)) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 913 | *version = LYS_VERSION_1_1; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 914 | } else { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 915 | LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "yang-version"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 916 | free(buf); |
| 917 | return LY_EVALID; |
| 918 | } |
| 919 | free(buf); |
| 920 | |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 921 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 922 | switch (kw) { |
| 923 | case YANG_CUSTOM: |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 924 | LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_VERSION, 0, exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 925 | break; |
| 926 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 927 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "yang-version"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 928 | return LY_EVALID; |
| 929 | } |
| 930 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 931 | return ret; |
| 932 | } |
| 933 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 934 | /** |
| 935 | * @brief Parse the belongs-to statement. |
| 936 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 937 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 938 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 939 | * @param[in,out] belongsto Place to store the parsed value. |
| 940 | * @param[in,out] prefix Place to store the parsed belongs-to prefix value. |
| 941 | * @param[in,out] exts Extension instances to add to. |
| 942 | * |
| 943 | * @return LY_ERR values. |
| 944 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 945 | static LY_ERR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 946 | parse_belongsto(struct lys_parser_ctx *ctx, const char **data, const char **belongsto, const char **prefix, struct lysp_ext_instance **exts) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 947 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 948 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 949 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 950 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 951 | enum yang_keyword kw; |
| 952 | |
| 953 | if (*belongsto) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 954 | LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "belongs-to"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 955 | return LY_EVALID; |
| 956 | } |
| 957 | |
| 958 | /* get value */ |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 959 | LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 960 | |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 961 | INSERT_WORD(ctx, buf, *belongsto, word, word_len); |
Radek Krejci | f09e4e8 | 2019-06-14 15:08:11 +0200 | [diff] [blame] | 962 | |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 963 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 964 | switch (kw) { |
| 965 | case YANG_PREFIX: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 966 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_PREFIX, 0, prefix, Y_IDENTIF_ARG, exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 967 | break; |
| 968 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 969 | LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_BELONGSTO, 0, exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 970 | break; |
| 971 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 972 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "belongs-to"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 973 | return LY_EVALID; |
| 974 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 975 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 976 | LY_CHECK_RET(ret); |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 977 | checks: |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 978 | /* mandatory substatements */ |
| 979 | if (!*prefix) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 980 | LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "belongs-to"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 981 | return LY_EVALID; |
| 982 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 983 | return ret; |
| 984 | } |
| 985 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 986 | /** |
| 987 | * @brief Parse the revision-date statement. |
| 988 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 989 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 990 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 991 | * @param[in,out] rev Array to store the parsed value in. |
| 992 | * @param[in,out] exts Extension instances to add to. |
| 993 | * |
| 994 | * @return LY_ERR values. |
| 995 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 996 | static LY_ERR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 997 | parse_revisiondate(struct lys_parser_ctx *ctx, const char **data, char *rev, struct lysp_ext_instance **exts) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 998 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 999 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1000 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 1001 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1002 | enum yang_keyword kw; |
| 1003 | |
| 1004 | if (rev[0]) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 1005 | LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "revision-date"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1006 | return LY_EVALID; |
| 1007 | } |
| 1008 | |
| 1009 | /* get value */ |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 1010 | LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1011 | |
| 1012 | /* check value */ |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1013 | if (lysp_check_date(ctx, word, word_len, "revision-date")) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1014 | free(buf); |
| 1015 | return LY_EVALID; |
| 1016 | } |
| 1017 | |
| 1018 | /* store value and spend buf if allocated */ |
| 1019 | strncpy(rev, word, word_len); |
| 1020 | free(buf); |
| 1021 | |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 1022 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1023 | switch (kw) { |
| 1024 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1025 | LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_REVISIONDATE, 0, exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1026 | break; |
| 1027 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 1028 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "revision-date"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1029 | return LY_EVALID; |
| 1030 | } |
| 1031 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1032 | return ret; |
| 1033 | } |
| 1034 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1035 | /** |
| 1036 | * @brief Parse the include statement. |
| 1037 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1038 | * @param[in] ctx yang parser context for logging. |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1039 | * @param[in] module_name Name of the module to check name collisions. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1040 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 1041 | * @param[in,out] includes Parsed includes to add to. |
| 1042 | * |
| 1043 | * @return LY_ERR values. |
| 1044 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1045 | static LY_ERR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1046 | parse_include(struct lys_parser_ctx *ctx, const char *module_name, const char **data, struct lysp_include **includes) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1047 | { |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 1048 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1049 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 1050 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1051 | enum yang_keyword kw; |
| 1052 | struct lysp_include *inc; |
| 1053 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1054 | LY_ARRAY_NEW_RET(ctx->ctx, *includes, inc, LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1055 | |
| 1056 | /* get value */ |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 1057 | LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1058 | |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 1059 | INSERT_WORD(ctx, buf, inc->name, word, word_len); |
| 1060 | |
Radek Krejci | faa1eac | 2018-10-30 14:34:55 +0100 | [diff] [blame] | 1061 | /* submodules share the namespace with the module names, so there must not be |
| 1062 | * a module of the same name in the context, no need for revision matching */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1063 | if (!strcmp(module_name, inc->name) || ly_ctx_get_module_latest(ctx->ctx, inc->name)) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 1064 | LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Name collision between module and submodule of name \"%s\".", inc->name); |
Radek Krejci | faa1eac | 2018-10-30 14:34:55 +0100 | [diff] [blame] | 1065 | return LY_EVALID; |
| 1066 | } |
| 1067 | |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 1068 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1069 | switch (kw) { |
| 1070 | case YANG_DESCRIPTION: |
Radek Krejci | ceaf212 | 2019-01-02 15:03:26 +0100 | [diff] [blame] | 1071 | YANG_CHECK_STMTVER2_RET(ctx, "description", "include"); |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1072 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &inc->dsc, Y_STR_ARG, &inc->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1073 | break; |
| 1074 | case YANG_REFERENCE: |
Radek Krejci | ceaf212 | 2019-01-02 15:03:26 +0100 | [diff] [blame] | 1075 | YANG_CHECK_STMTVER2_RET(ctx, "reference", "include"); |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1076 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &inc->ref, Y_STR_ARG, &inc->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1077 | break; |
| 1078 | case YANG_REVISION_DATE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1079 | LY_CHECK_RET(parse_revisiondate(ctx, data, inc->rev, &inc->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1080 | break; |
| 1081 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1082 | LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &inc->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1083 | break; |
| 1084 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 1085 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "include"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1086 | return LY_EVALID; |
| 1087 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1088 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1089 | return ret; |
| 1090 | } |
| 1091 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1092 | /** |
| 1093 | * @brief Parse the import statement. |
| 1094 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1095 | * @param[in] ctx yang parser context for logging. |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1096 | * @param[in] module_prefix Prefix of the module to check prefix collisions. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1097 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 1098 | * @param[in,out] imports Parsed imports to add to. |
| 1099 | * |
| 1100 | * @return LY_ERR values. |
| 1101 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1102 | static LY_ERR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1103 | parse_import(struct lys_parser_ctx *ctx, const char *module_prefix, const char **data, struct lysp_import **imports) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1104 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1105 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1106 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 1107 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1108 | enum yang_keyword kw; |
| 1109 | struct lysp_import *imp; |
| 1110 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1111 | LY_ARRAY_NEW_RET(ctx->ctx, *imports, imp, LY_EVALID); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1112 | |
| 1113 | /* get value */ |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 1114 | LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len)); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1115 | INSERT_WORD(ctx, buf, imp->name, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1116 | |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 1117 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1118 | switch (kw) { |
| 1119 | case YANG_PREFIX: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1120 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_PREFIX, 0, &imp->prefix, Y_IDENTIF_ARG, &imp->exts)); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1121 | LY_CHECK_RET(lysp_check_prefix(ctx, *imports, module_prefix, &imp->prefix), LY_EVALID); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1122 | break; |
| 1123 | case YANG_DESCRIPTION: |
Radek Krejci | ceaf212 | 2019-01-02 15:03:26 +0100 | [diff] [blame] | 1124 | YANG_CHECK_STMTVER2_RET(ctx, "description", "import"); |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1125 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &imp->dsc, Y_STR_ARG, &imp->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1126 | break; |
| 1127 | case YANG_REFERENCE: |
Radek Krejci | ceaf212 | 2019-01-02 15:03:26 +0100 | [diff] [blame] | 1128 | YANG_CHECK_STMTVER2_RET(ctx, "reference", "import"); |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1129 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &imp->ref, Y_STR_ARG, &imp->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1130 | break; |
| 1131 | case YANG_REVISION_DATE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1132 | LY_CHECK_RET(parse_revisiondate(ctx, data, imp->rev, &imp->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1133 | break; |
| 1134 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1135 | LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &imp->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1136 | break; |
| 1137 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 1138 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "import"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1139 | return LY_EVALID; |
| 1140 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1141 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1142 | LY_CHECK_RET(ret); |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 1143 | checks: |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1144 | /* mandatory substatements */ |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 1145 | 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] | 1146 | |
| 1147 | return ret; |
| 1148 | } |
| 1149 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1150 | /** |
| 1151 | * @brief Parse the revision statement. |
| 1152 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1153 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1154 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 1155 | * @param[in,out] revs Parsed revisions to add to. |
| 1156 | * |
| 1157 | * @return LY_ERR values. |
| 1158 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1159 | static LY_ERR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1160 | parse_revision(struct lys_parser_ctx *ctx, const char **data, struct lysp_revision **revs) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1161 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1162 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1163 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 1164 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1165 | enum yang_keyword kw; |
| 1166 | struct lysp_revision *rev; |
| 1167 | |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 1168 | LY_ARRAY_NEW_RET(ctx->ctx, *revs, rev, LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1169 | |
| 1170 | /* get value */ |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 1171 | LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1172 | |
| 1173 | /* check value */ |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1174 | if (lysp_check_date(ctx, word, word_len, "revision")) { |
David Sedlák | 68ef3dc | 2019-07-22 13:40:19 +0200 | [diff] [blame] | 1175 | free(buf); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1176 | return LY_EVALID; |
| 1177 | } |
| 1178 | |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 1179 | strncpy(rev->date, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1180 | free(buf); |
| 1181 | |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 1182 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1183 | switch (kw) { |
| 1184 | case YANG_DESCRIPTION: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1185 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &rev->dsc, Y_STR_ARG, &rev->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1186 | break; |
| 1187 | case YANG_REFERENCE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1188 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &rev->ref, Y_STR_ARG, &rev->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1189 | break; |
| 1190 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1191 | LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &rev->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1192 | break; |
| 1193 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 1194 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "revision"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1195 | return LY_EVALID; |
| 1196 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1197 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1198 | return ret; |
| 1199 | } |
| 1200 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1201 | /** |
| 1202 | * @brief Parse a generic text field that can have more instances such as base. |
| 1203 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1204 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1205 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 1206 | * @param[in] substmt Type of this substatement. |
| 1207 | * @param[in,out] texts Parsed values to add to. |
| 1208 | * @param[in] arg Type of the expected argument. |
| 1209 | * @param[in,out] exts Extension instances to add to. |
| 1210 | * |
| 1211 | * @return LY_ERR values. |
| 1212 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1213 | static LY_ERR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1214 | parse_text_fields(struct lys_parser_ctx *ctx, const char **data, LYEXT_SUBSTMT substmt, const char ***texts, enum yang_arg arg, |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1215 | struct lysp_ext_instance **exts) |
| 1216 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1217 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1218 | char *buf, *word; |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 1219 | const char **item; |
| 1220 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1221 | enum yang_keyword kw; |
| 1222 | |
| 1223 | /* allocate new pointer */ |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 1224 | LY_ARRAY_NEW_RET(ctx->ctx, *texts, item, LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1225 | |
| 1226 | /* get value */ |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 1227 | LY_CHECK_RET(get_argument(ctx, data, arg, NULL, &word, &buf, &word_len)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1228 | |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 1229 | INSERT_WORD(ctx, buf, *item, word, word_len); |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 1230 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1231 | switch (kw) { |
| 1232 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1233 | LY_CHECK_RET(parse_ext(ctx, data, word, word_len, substmt, LY_ARRAY_SIZE(*texts) - 1, exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1234 | break; |
| 1235 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 1236 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), lyext_substmt2str(substmt)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1237 | return LY_EVALID; |
| 1238 | } |
| 1239 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1240 | return ret; |
| 1241 | } |
| 1242 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1243 | /** |
| 1244 | * @brief Parse the config statement. |
| 1245 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1246 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1247 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 1248 | * @param[in,out] flags Flags to add to. |
| 1249 | * @param[in,out] exts Extension instances to add to. |
| 1250 | * |
| 1251 | * @return LY_ERR values. |
| 1252 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1253 | static LY_ERR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1254 | parse_config(struct lys_parser_ctx *ctx, const char **data, uint16_t *flags, struct lysp_ext_instance **exts) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1255 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1256 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1257 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 1258 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1259 | enum yang_keyword kw; |
| 1260 | |
| 1261 | if (*flags & LYS_CONFIG_MASK) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 1262 | LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "config"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1263 | return LY_EVALID; |
| 1264 | } |
| 1265 | |
| 1266 | /* get value */ |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 1267 | LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1268 | |
| 1269 | if ((word_len == 4) && !strncmp(word, "true", word_len)) { |
| 1270 | *flags |= LYS_CONFIG_W; |
| 1271 | } else if ((word_len == 5) && !strncmp(word, "false", word_len)) { |
| 1272 | *flags |= LYS_CONFIG_R; |
| 1273 | } else { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 1274 | LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "config"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1275 | free(buf); |
| 1276 | return LY_EVALID; |
| 1277 | } |
| 1278 | free(buf); |
| 1279 | |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 1280 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1281 | switch (kw) { |
| 1282 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1283 | LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_CONFIG, 0, exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1284 | break; |
| 1285 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 1286 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "config"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1287 | return LY_EVALID; |
| 1288 | } |
| 1289 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1290 | return ret; |
| 1291 | } |
| 1292 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1293 | /** |
| 1294 | * @brief Parse the mandatory statement. |
| 1295 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1296 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1297 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 1298 | * @param[in,out] flags Flags to add to. |
| 1299 | * @param[in,out] exts Extension instances to add to. |
| 1300 | * |
| 1301 | * @return LY_ERR values. |
| 1302 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1303 | static LY_ERR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1304 | parse_mandatory(struct lys_parser_ctx *ctx, const char **data, uint16_t *flags, struct lysp_ext_instance **exts) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1305 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1306 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1307 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 1308 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1309 | enum yang_keyword kw; |
| 1310 | |
| 1311 | if (*flags & LYS_MAND_MASK) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 1312 | LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "mandatory"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1313 | return LY_EVALID; |
| 1314 | } |
| 1315 | |
| 1316 | /* get value */ |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 1317 | LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1318 | |
| 1319 | if ((word_len == 4) && !strncmp(word, "true", word_len)) { |
| 1320 | *flags |= LYS_MAND_TRUE; |
| 1321 | } else if ((word_len == 5) && !strncmp(word, "false", word_len)) { |
| 1322 | *flags |= LYS_MAND_FALSE; |
| 1323 | } else { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 1324 | LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "mandatory"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1325 | free(buf); |
| 1326 | return LY_EVALID; |
| 1327 | } |
| 1328 | free(buf); |
| 1329 | |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 1330 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1331 | switch (kw) { |
| 1332 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1333 | LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_MANDATORY, 0, exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1334 | break; |
| 1335 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 1336 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "mandatory"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1337 | return LY_EVALID; |
| 1338 | } |
| 1339 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1340 | return ret; |
| 1341 | } |
| 1342 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1343 | /** |
| 1344 | * @brief Parse a restriction such as range or length. |
| 1345 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1346 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1347 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 1348 | * @param[in] restr_kw Type of this particular restriction. |
| 1349 | * @param[in,out] exts Extension instances to add to. |
| 1350 | * |
| 1351 | * @return LY_ERR values. |
| 1352 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1353 | static LY_ERR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1354 | parse_restr(struct lys_parser_ctx *ctx, const char **data, enum yang_keyword restr_kw, struct lysp_restr *restr) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1355 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1356 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1357 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 1358 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1359 | enum yang_keyword kw; |
| 1360 | |
| 1361 | /* get value */ |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 1362 | LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1363 | |
David Sedlák | b9b892c | 2019-07-12 14:44:02 +0200 | [diff] [blame] | 1364 | YANG_CHECK_NONEMPTY(ctx, word_len, ly_stmt2str(restr_kw)); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1365 | INSERT_WORD(ctx, buf, restr->arg, word, word_len); |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 1366 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1367 | switch (kw) { |
| 1368 | case YANG_DESCRIPTION: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1369 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &restr->dsc, Y_STR_ARG, &restr->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1370 | break; |
| 1371 | case YANG_REFERENCE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1372 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &restr->ref, Y_STR_ARG, &restr->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1373 | break; |
| 1374 | case YANG_ERROR_APP_TAG: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1375 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_ERRTAG, 0, &restr->eapptag, Y_STR_ARG, &restr->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1376 | break; |
| 1377 | case YANG_ERROR_MESSAGE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1378 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_ERRMSG, 0, &restr->emsg, Y_STR_ARG, &restr->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1379 | break; |
| 1380 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1381 | LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &restr->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1382 | break; |
| 1383 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 1384 | 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] | 1385 | return LY_EVALID; |
| 1386 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1387 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1388 | return ret; |
| 1389 | } |
| 1390 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1391 | /** |
| 1392 | * @brief Parse a restriction that can have more instances such as must. |
| 1393 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1394 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1395 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 1396 | * @param[in] restr_kw Type of this particular restriction. |
| 1397 | * @param[in,out] restrs Restrictions to add to. |
| 1398 | * |
| 1399 | * @return LY_ERR values. |
| 1400 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1401 | static LY_ERR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1402 | parse_restrs(struct lys_parser_ctx *ctx, const char **data, enum yang_keyword restr_kw, struct lysp_restr **restrs) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1403 | { |
| 1404 | struct lysp_restr *restr; |
| 1405 | |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 1406 | LY_ARRAY_NEW_RET(ctx->ctx, *restrs, restr, LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1407 | return parse_restr(ctx, data, restr_kw, restr); |
| 1408 | } |
| 1409 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1410 | /** |
| 1411 | * @brief Parse the status statement. |
| 1412 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1413 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1414 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 1415 | * @param[in,out] flags Flags to add to. |
| 1416 | * @param[in,out] exts Extension instances to add to. |
| 1417 | * |
| 1418 | * @return LY_ERR values. |
| 1419 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1420 | static LY_ERR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1421 | parse_status(struct lys_parser_ctx *ctx, const char **data, uint16_t *flags, struct lysp_ext_instance **exts) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1422 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1423 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1424 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 1425 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1426 | enum yang_keyword kw; |
| 1427 | |
| 1428 | if (*flags & LYS_STATUS_MASK) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 1429 | LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "status"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1430 | return LY_EVALID; |
| 1431 | } |
| 1432 | |
| 1433 | /* get value */ |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 1434 | LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1435 | |
| 1436 | if ((word_len == 7) && !strncmp(word, "current", word_len)) { |
| 1437 | *flags |= LYS_STATUS_CURR; |
| 1438 | } else if ((word_len == 10) && !strncmp(word, "deprecated", word_len)) { |
| 1439 | *flags |= LYS_STATUS_DEPRC; |
| 1440 | } else if ((word_len == 8) && !strncmp(word, "obsolete", word_len)) { |
| 1441 | *flags |= LYS_STATUS_OBSLT; |
| 1442 | } else { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 1443 | LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "status"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1444 | free(buf); |
| 1445 | return LY_EVALID; |
| 1446 | } |
| 1447 | free(buf); |
| 1448 | |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 1449 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1450 | switch (kw) { |
| 1451 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1452 | LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_STATUS, 0, exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1453 | break; |
| 1454 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 1455 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "status"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1456 | return LY_EVALID; |
| 1457 | } |
| 1458 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1459 | return ret; |
| 1460 | } |
| 1461 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1462 | /** |
| 1463 | * @brief Parse the when statement. |
| 1464 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1465 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1466 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 1467 | * @param[in,out] when_p When pointer to parse to. |
| 1468 | * |
| 1469 | * @return LY_ERR values. |
| 1470 | */ |
Radek Krejci | f09e4e8 | 2019-06-14 15:08:11 +0200 | [diff] [blame] | 1471 | LY_ERR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1472 | parse_when(struct lys_parser_ctx *ctx, const char **data, struct lysp_when **when_p) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1473 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1474 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1475 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 1476 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1477 | enum yang_keyword kw; |
| 1478 | struct lysp_when *when; |
| 1479 | |
| 1480 | if (*when_p) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 1481 | LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "when"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1482 | return LY_EVALID; |
| 1483 | } |
| 1484 | |
| 1485 | when = calloc(1, sizeof *when); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1486 | LY_CHECK_ERR_RET(!when, LOGMEM(ctx->ctx), LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1487 | |
| 1488 | /* get value */ |
Radek Krejci | 2f54df5 | 2019-06-21 10:59:19 +0200 | [diff] [blame] | 1489 | LY_CHECK_ERR_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len), free(when), LY_EMEM); |
David Sedlák | b9b892c | 2019-07-12 14:44:02 +0200 | [diff] [blame] | 1490 | YANG_CHECK_NONEMPTY(ctx, word_len, "when"); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1491 | INSERT_WORD(ctx, buf, when->cond, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1492 | |
Radek Krejci | f09e4e8 | 2019-06-14 15:08:11 +0200 | [diff] [blame] | 1493 | *when_p = when; |
| 1494 | |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 1495 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1496 | switch (kw) { |
| 1497 | case YANG_DESCRIPTION: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1498 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &when->dsc, Y_STR_ARG, &when->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1499 | break; |
| 1500 | case YANG_REFERENCE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1501 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &when->ref, Y_STR_ARG, &when->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1502 | break; |
| 1503 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1504 | LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &when->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1505 | break; |
| 1506 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 1507 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "when"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1508 | return LY_EVALID; |
| 1509 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1510 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1511 | return ret; |
| 1512 | } |
| 1513 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1514 | /** |
| 1515 | * @brief Parse the anydata or anyxml statement. |
| 1516 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1517 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1518 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 1519 | * @param[in] kw Type of this particular keyword. |
| 1520 | * @param[in,out] siblings Siblings to add to. |
| 1521 | * |
| 1522 | * @return LY_ERR values. |
| 1523 | */ |
Radek Krejci | 2d7a47b | 2019-05-16 13:34:10 +0200 | [diff] [blame] | 1524 | LY_ERR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1525 | parse_any(struct lys_parser_ctx *ctx, const char **data, enum yang_keyword kw, struct lysp_node *parent, struct lysp_node **siblings) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1526 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1527 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1528 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 1529 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1530 | struct lysp_node *iter; |
| 1531 | struct lysp_node_anydata *any; |
| 1532 | |
| 1533 | /* create structure */ |
| 1534 | any = calloc(1, sizeof *any); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1535 | LY_CHECK_ERR_RET(!any, LOGMEM(ctx->ctx), LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1536 | any->nodetype = kw == YANG_ANYDATA ? LYS_ANYDATA : LYS_ANYXML; |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1537 | any->parent = parent; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1538 | |
| 1539 | /* insert into siblings */ |
| 1540 | if (!*siblings) { |
| 1541 | *siblings = (struct lysp_node *)any; |
| 1542 | } else { |
| 1543 | for (iter = *siblings; iter->next; iter = iter->next); |
| 1544 | iter->next = (struct lysp_node *)any; |
| 1545 | } |
| 1546 | |
| 1547 | /* get name */ |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 1548 | LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len)); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1549 | INSERT_WORD(ctx, buf, any->name, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1550 | |
| 1551 | /* parse substatements */ |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 1552 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1553 | switch (kw) { |
| 1554 | case YANG_CONFIG: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1555 | LY_CHECK_RET(parse_config(ctx, data, &any->flags, &any->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1556 | break; |
| 1557 | case YANG_DESCRIPTION: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1558 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &any->dsc, Y_STR_ARG, &any->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1559 | break; |
| 1560 | case YANG_IF_FEATURE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1561 | LY_CHECK_RET(parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &any->iffeatures, Y_STR_ARG, &any->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1562 | break; |
| 1563 | case YANG_MANDATORY: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1564 | LY_CHECK_RET(parse_mandatory(ctx, data, &any->flags, &any->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1565 | break; |
| 1566 | case YANG_MUST: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1567 | LY_CHECK_RET(parse_restrs(ctx, data, kw, &any->musts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1568 | break; |
| 1569 | case YANG_REFERENCE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1570 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &any->ref, Y_STR_ARG, &any->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1571 | break; |
| 1572 | case YANG_STATUS: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1573 | LY_CHECK_RET(parse_status(ctx, data, &any->flags, &any->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1574 | break; |
| 1575 | case YANG_WHEN: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1576 | LY_CHECK_RET(parse_when(ctx, data, &any->when)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1577 | break; |
| 1578 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1579 | LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &any->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1580 | break; |
| 1581 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 1582 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1583 | (any->nodetype & LYS_ANYDATA) == LYS_ANYDATA ? ly_stmt2str(YANG_ANYDATA) : ly_stmt2str(YANG_ANYXML)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1584 | return LY_EVALID; |
| 1585 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1586 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1587 | return ret; |
| 1588 | } |
| 1589 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1590 | /** |
| 1591 | * @brief Parse the value or position statement. Substatement of type enum statement. |
| 1592 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1593 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1594 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 1595 | * @param[in] val_kw Type of this particular keyword. |
| 1596 | * @param[in,out] value Value to write to. |
| 1597 | * @param[in,out] flags Flags to write to. |
| 1598 | * @param[in,out] exts Extension instances to add to. |
| 1599 | * |
| 1600 | * @return LY_ERR values. |
| 1601 | */ |
David Sedlák | d6ce6d7 | 2019-07-16 17:30:18 +0200 | [diff] [blame] | 1602 | LY_ERR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1603 | parse_type_enum_value_pos(struct lys_parser_ctx *ctx, const char **data, enum yang_keyword val_kw, int64_t *value, uint16_t *flags, |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1604 | struct lysp_ext_instance **exts) |
| 1605 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1606 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1607 | char *buf, *word, *ptr; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 1608 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1609 | long int num; |
| 1610 | unsigned long int unum; |
| 1611 | enum yang_keyword kw; |
| 1612 | |
| 1613 | if (*flags & LYS_SET_VALUE) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 1614 | LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(val_kw)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1615 | return LY_EVALID; |
| 1616 | } |
| 1617 | *flags |= LYS_SET_VALUE; |
| 1618 | |
| 1619 | /* get value */ |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 1620 | LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1621 | |
David Sedlák | d6ce6d7 | 2019-07-16 17:30:18 +0200 | [diff] [blame] | 1622 | if (!word_len || (word[0] == '+') || ((word[0] == '0') && (word_len > 1)) || ((val_kw == YANG_POSITION) && !strncmp(word, "-0", 2))) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 1623 | 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] | 1624 | goto error; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1625 | } |
| 1626 | |
| 1627 | errno = 0; |
| 1628 | if (val_kw == YANG_VALUE) { |
| 1629 | num = strtol(word, &ptr, 10); |
Radek Krejci | 8b76466 | 2018-11-14 14:15:13 +0100 | [diff] [blame] | 1630 | if (num < INT64_C(-2147483648) || num > INT64_C(2147483647)) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 1631 | 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] | 1632 | goto error; |
| 1633 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1634 | } else { |
| 1635 | unum = strtoul(word, &ptr, 10); |
Radek Krejci | 8b76466 | 2018-11-14 14:15:13 +0100 | [diff] [blame] | 1636 | if (unum > UINT64_C(4294967295)) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 1637 | 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] | 1638 | goto error; |
| 1639 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1640 | } |
| 1641 | /* we have not parsed the whole argument */ |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 1642 | if ((size_t)(ptr - word) != word_len) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 1643 | 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] | 1644 | goto error; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1645 | } |
| 1646 | if (errno == ERANGE) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 1647 | 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] | 1648 | goto error; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1649 | } |
| 1650 | if (val_kw == YANG_VALUE) { |
| 1651 | *value = num; |
| 1652 | } else { |
| 1653 | *value = unum; |
| 1654 | } |
| 1655 | free(buf); |
| 1656 | |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 1657 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1658 | switch (kw) { |
| 1659 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1660 | LY_CHECK_RET(parse_ext(ctx, data, word, word_len, val_kw == YANG_VALUE ? LYEXT_SUBSTMT_VALUE : LYEXT_SUBSTMT_POSITION, 0, exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1661 | break; |
| 1662 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 1663 | 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] | 1664 | return LY_EVALID; |
| 1665 | } |
| 1666 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1667 | return ret; |
Radek Krejci | 8b76466 | 2018-11-14 14:15:13 +0100 | [diff] [blame] | 1668 | |
| 1669 | error: |
| 1670 | free(buf); |
| 1671 | return LY_EVALID; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1672 | } |
| 1673 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1674 | /** |
| 1675 | * @brief Parse the enum or bit statement. Substatement of type statement. |
| 1676 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1677 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1678 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 1679 | * @param[in] enum_kw Type of this particular keyword. |
| 1680 | * @param[in,out] enums Enums or bits to add to. |
| 1681 | * |
| 1682 | * @return LY_ERR values. |
| 1683 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1684 | static LY_ERR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1685 | parse_type_enum(struct lys_parser_ctx *ctx, const char **data, enum yang_keyword enum_kw, struct lysp_type_enum **enums) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1686 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1687 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1688 | char *buf, *word; |
David Sedlák | 6544c18 | 2019-07-12 13:17:33 +0200 | [diff] [blame] | 1689 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1690 | enum yang_keyword kw; |
| 1691 | struct lysp_type_enum *enm; |
| 1692 | |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 1693 | LY_ARRAY_NEW_RET(ctx->ctx, *enums, enm, LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1694 | |
| 1695 | /* get value */ |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 1696 | LY_CHECK_RET(get_argument(ctx, data, enum_kw == YANG_ENUM ? Y_STR_ARG : Y_IDENTIF_ARG, NULL, &word, &buf, &word_len)); |
Radek Krejci | 8b76466 | 2018-11-14 14:15:13 +0100 | [diff] [blame] | 1697 | if (enum_kw == YANG_ENUM) { |
David Sedlák | 07869a5 | 2019-07-12 14:28:19 +0200 | [diff] [blame] | 1698 | ret = lysp_check_enum_name(ctx, (const char *)word, word_len); |
David Sedlák | 6544c18 | 2019-07-12 13:17:33 +0200 | [diff] [blame] | 1699 | LY_CHECK_ERR_RET(ret, free(buf), ret); |
Radek Krejci | 8b76466 | 2018-11-14 14:15:13 +0100 | [diff] [blame] | 1700 | } else { /* YANG_BIT */ |
| 1701 | |
| 1702 | } |
Radek Krejci | f09e4e8 | 2019-06-14 15:08:11 +0200 | [diff] [blame] | 1703 | if (enum_kw == YANG_ENUM) { |
David Sedlák | b9b892c | 2019-07-12 14:44:02 +0200 | [diff] [blame] | 1704 | YANG_CHECK_NONEMPTY(ctx, word_len, "enum"); |
Radek Krejci | f09e4e8 | 2019-06-14 15:08:11 +0200 | [diff] [blame] | 1705 | } |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1706 | INSERT_WORD(ctx, buf, enm->name, word, word_len); |
Radek Krejci | 8b76466 | 2018-11-14 14:15:13 +0100 | [diff] [blame] | 1707 | CHECK_UNIQUENESS(ctx, *enums, name, ly_stmt2str(enum_kw), enm->name); |
| 1708 | |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 1709 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1710 | switch (kw) { |
| 1711 | case YANG_DESCRIPTION: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1712 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &enm->dsc, Y_STR_ARG, &enm->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1713 | break; |
| 1714 | case YANG_IF_FEATURE: |
Radek Krejci | ceaf212 | 2019-01-02 15:03:26 +0100 | [diff] [blame] | 1715 | YANG_CHECK_STMTVER2_RET(ctx, "if-feature", ly_stmt2str(enum_kw)); |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1716 | LY_CHECK_RET(parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &enm->iffeatures, Y_STR_ARG, &enm->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1717 | break; |
| 1718 | case YANG_REFERENCE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1719 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &enm->ref, Y_STR_ARG, &enm->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1720 | break; |
| 1721 | case YANG_STATUS: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1722 | LY_CHECK_RET(parse_status(ctx, data, &enm->flags, &enm->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1723 | break; |
| 1724 | case YANG_VALUE: |
David Sedlák | 9fb515f | 2019-07-11 10:33:58 +0200 | [diff] [blame] | 1725 | LY_CHECK_ERR_RET(enum_kw == YANG_BIT, LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), |
| 1726 | ly_stmt2str(enum_kw)), LY_EVALID); |
| 1727 | LY_CHECK_RET(parse_type_enum_value_pos(ctx, data, kw, &enm->value, &enm->flags, &enm->exts)); |
| 1728 | break; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1729 | case YANG_POSITION: |
David Sedlák | 9fb515f | 2019-07-11 10:33:58 +0200 | [diff] [blame] | 1730 | LY_CHECK_ERR_RET(enum_kw == YANG_ENUM, LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), |
| 1731 | ly_stmt2str(enum_kw)), LY_EVALID); |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1732 | LY_CHECK_RET(parse_type_enum_value_pos(ctx, data, kw, &enm->value, &enm->flags, &enm->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1733 | break; |
| 1734 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1735 | LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &enm->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1736 | break; |
| 1737 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 1738 | 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] | 1739 | return LY_EVALID; |
| 1740 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1741 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1742 | return ret; |
| 1743 | } |
| 1744 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1745 | /** |
| 1746 | * @brief Parse the fraction-digits statement. Substatement of type statement. |
| 1747 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1748 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1749 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 1750 | * @param[in,out] fracdig Value to write to. |
| 1751 | * @param[in,out] exts Extension instances to add to. |
| 1752 | * |
| 1753 | * @return LY_ERR values. |
| 1754 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1755 | static LY_ERR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1756 | parse_type_fracdigits(struct lys_parser_ctx *ctx, const char **data, uint8_t *fracdig, struct lysp_ext_instance **exts) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1757 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1758 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1759 | char *buf, *word, *ptr; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 1760 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1761 | unsigned long int num; |
| 1762 | enum yang_keyword kw; |
| 1763 | |
| 1764 | if (*fracdig) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 1765 | LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "fraction-digits"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1766 | return LY_EVALID; |
| 1767 | } |
| 1768 | |
| 1769 | /* get value */ |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 1770 | LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1771 | |
| 1772 | if (!word_len || (word[0] == '0') || !isdigit(word[0])) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 1773 | LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "fraction-digits"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1774 | free(buf); |
| 1775 | return LY_EVALID; |
| 1776 | } |
| 1777 | |
| 1778 | errno = 0; |
| 1779 | num = strtoul(word, &ptr, 10); |
| 1780 | /* we have not parsed the whole argument */ |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 1781 | if ((size_t)(ptr - word) != word_len) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 1782 | LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "fraction-digits"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1783 | free(buf); |
| 1784 | return LY_EVALID; |
| 1785 | } |
| 1786 | if ((errno == ERANGE) || (num > 18)) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 1787 | LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, "fraction-digits"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1788 | free(buf); |
| 1789 | return LY_EVALID; |
| 1790 | } |
| 1791 | *fracdig = num; |
| 1792 | free(buf); |
| 1793 | |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 1794 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1795 | switch (kw) { |
| 1796 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1797 | LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_FRACDIGITS, 0, exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1798 | break; |
| 1799 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 1800 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "fraction-digits"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1801 | return LY_EVALID; |
| 1802 | } |
| 1803 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1804 | return ret; |
| 1805 | } |
| 1806 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1807 | /** |
| 1808 | * @brief Parse the require-instance statement. Substatement of type statement. |
| 1809 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1810 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1811 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 1812 | * @param[in,out] reqinst Value to write to. |
| 1813 | * @param[in,out] flags Flags to write to. |
| 1814 | * @param[in,out] exts Extension instances to add to. |
| 1815 | * |
| 1816 | * @return LY_ERR values. |
| 1817 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1818 | static LY_ERR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1819 | parse_type_reqinstance(struct lys_parser_ctx *ctx, const char **data, uint8_t *reqinst, uint16_t *flags, |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1820 | struct lysp_ext_instance **exts) |
| 1821 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1822 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1823 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 1824 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1825 | enum yang_keyword kw; |
| 1826 | |
| 1827 | if (*flags & LYS_SET_REQINST) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 1828 | LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "require-instance"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1829 | return LY_EVALID; |
| 1830 | } |
| 1831 | *flags |= LYS_SET_REQINST; |
| 1832 | |
| 1833 | /* get value */ |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 1834 | LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1835 | |
| 1836 | if ((word_len == 4) && !strncmp(word, "true", word_len)) { |
| 1837 | *reqinst = 1; |
| 1838 | } else if ((word_len != 5) || strncmp(word, "false", word_len)) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 1839 | LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "require-instance"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1840 | free(buf); |
| 1841 | return LY_EVALID; |
| 1842 | } |
| 1843 | free(buf); |
| 1844 | |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 1845 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1846 | switch (kw) { |
| 1847 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1848 | LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_REQINSTANCE, 0, exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1849 | break; |
| 1850 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 1851 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "require-instance"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1852 | return LY_EVALID; |
| 1853 | } |
| 1854 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1855 | return ret; |
| 1856 | } |
| 1857 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1858 | /** |
| 1859 | * @brief Parse the modifier statement. Substatement of type pattern statement. |
| 1860 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1861 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1862 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 1863 | * @param[in,out] pat Value to write to. |
| 1864 | * @param[in,out] exts Extension instances to add to. |
| 1865 | * |
| 1866 | * @return LY_ERR values. |
| 1867 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1868 | static LY_ERR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1869 | parse_type_pattern_modifier(struct lys_parser_ctx *ctx, const char **data, const char **pat, struct lysp_ext_instance **exts) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1870 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1871 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1872 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 1873 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1874 | enum yang_keyword kw; |
| 1875 | |
| 1876 | if ((*pat)[0] == 0x15) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 1877 | LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "modifier"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1878 | return LY_EVALID; |
| 1879 | } |
| 1880 | |
| 1881 | /* get value */ |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 1882 | LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1883 | |
| 1884 | if ((word_len != 12) || strncmp(word, "invert-match", word_len)) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 1885 | LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "modifier"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1886 | free(buf); |
| 1887 | return LY_EVALID; |
| 1888 | } |
| 1889 | free(buf); |
| 1890 | |
| 1891 | /* replace the value in the dictionary */ |
| 1892 | buf = malloc(strlen(*pat) + 1); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1893 | LY_CHECK_ERR_RET(!buf, LOGMEM(ctx->ctx), LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1894 | strcpy(buf, *pat); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1895 | lydict_remove(ctx->ctx, *pat); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1896 | |
| 1897 | assert(buf[0] == 0x06); |
| 1898 | buf[0] = 0x15; |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1899 | *pat = lydict_insert_zc(ctx->ctx, buf); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1900 | |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 1901 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1902 | switch (kw) { |
| 1903 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1904 | LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_MODIFIER, 0, exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1905 | break; |
| 1906 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 1907 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "modifier"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1908 | return LY_EVALID; |
| 1909 | } |
| 1910 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1911 | return ret; |
| 1912 | } |
| 1913 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1914 | /** |
| 1915 | * @brief Parse the pattern statement. Substatement of type statement. |
| 1916 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1917 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1918 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 1919 | * @param[in,out] patterns Restrictions to add to. |
| 1920 | * |
| 1921 | * @return LY_ERR values. |
| 1922 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1923 | static LY_ERR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1924 | parse_type_pattern(struct lys_parser_ctx *ctx, const char **data, struct lysp_restr **patterns) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1925 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1926 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1927 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 1928 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1929 | enum yang_keyword kw; |
| 1930 | struct lysp_restr *restr; |
| 1931 | |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 1932 | LY_ARRAY_NEW_RET(ctx->ctx, *patterns, restr, LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1933 | |
| 1934 | /* get value */ |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 1935 | LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1936 | |
| 1937 | /* add special meaning first byte */ |
| 1938 | if (buf) { |
| 1939 | buf = realloc(buf, word_len + 2); |
| 1940 | word = buf; |
| 1941 | } else { |
| 1942 | buf = malloc(word_len + 2); |
| 1943 | } |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1944 | LY_CHECK_ERR_RET(!buf, LOGMEM(ctx->ctx), LY_EMEM); |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1945 | memmove(buf + 1, word, word_len); |
| 1946 | buf[0] = 0x06; /* pattern's default regular-match flag */ |
| 1947 | buf[word_len + 1] = '\0'; /* terminating NULL byte */ |
| 1948 | restr->arg = lydict_insert_zc(ctx->ctx, buf); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1949 | |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 1950 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1951 | switch (kw) { |
| 1952 | case YANG_DESCRIPTION: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1953 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &restr->dsc, Y_STR_ARG, &restr->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1954 | break; |
| 1955 | case YANG_REFERENCE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1956 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &restr->ref, Y_STR_ARG, &restr->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1957 | break; |
| 1958 | case YANG_ERROR_APP_TAG: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1959 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_ERRTAG, 0, &restr->eapptag, Y_STR_ARG, &restr->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1960 | break; |
| 1961 | case YANG_ERROR_MESSAGE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1962 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_ERRMSG, 0, &restr->emsg, Y_STR_ARG, &restr->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1963 | break; |
| 1964 | case YANG_MODIFIER: |
Radek Krejci | ceaf212 | 2019-01-02 15:03:26 +0100 | [diff] [blame] | 1965 | YANG_CHECK_STMTVER2_RET(ctx, "modifier", "pattern"); |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1966 | LY_CHECK_RET(parse_type_pattern_modifier(ctx, data, &restr->arg, &restr->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1967 | break; |
| 1968 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1969 | LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &restr->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1970 | break; |
| 1971 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 1972 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "pattern"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1973 | return LY_EVALID; |
| 1974 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1975 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1976 | return ret; |
| 1977 | } |
| 1978 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1979 | /** |
| 1980 | * @brief Parse the type statement. |
| 1981 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1982 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1983 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 1984 | * @param[in,out] type Type to wrote to. |
| 1985 | * |
| 1986 | * @return LY_ERR values. |
| 1987 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1988 | static LY_ERR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1989 | parse_type(struct lys_parser_ctx *ctx, const char **data, struct lysp_type *type) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1990 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1991 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1992 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 1993 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1994 | enum yang_keyword kw; |
| 1995 | struct lysp_type *nest_type; |
| 1996 | |
| 1997 | if (type->name) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 1998 | LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "type"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1999 | return LY_EVALID; |
| 2000 | } |
| 2001 | |
| 2002 | /* get value */ |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 2003 | LY_CHECK_RET(get_argument(ctx, data, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &word_len)); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2004 | INSERT_WORD(ctx, buf, type->name, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2005 | |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 2006 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2007 | switch (kw) { |
| 2008 | case YANG_BASE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2009 | LY_CHECK_RET(parse_text_fields(ctx, data, LYEXT_SUBSTMT_BASE, &type->bases, Y_PREF_IDENTIF_ARG, &type->exts)); |
Radek Krejci | d505e3d | 2018-11-13 09:04:17 +0100 | [diff] [blame] | 2010 | type->flags |= LYS_SET_BASE; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2011 | break; |
| 2012 | case YANG_BIT: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2013 | LY_CHECK_RET(parse_type_enum(ctx, data, kw, &type->bits)); |
Radek Krejci | d505e3d | 2018-11-13 09:04:17 +0100 | [diff] [blame] | 2014 | type->flags |= LYS_SET_BIT; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2015 | break; |
| 2016 | case YANG_ENUM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2017 | LY_CHECK_RET(parse_type_enum(ctx, data, kw, &type->enums)); |
Radek Krejci | d505e3d | 2018-11-13 09:04:17 +0100 | [diff] [blame] | 2018 | type->flags |= LYS_SET_ENUM; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2019 | break; |
| 2020 | case YANG_FRACTION_DIGITS: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2021 | LY_CHECK_RET(parse_type_fracdigits(ctx, data, &type->fraction_digits, &type->exts)); |
Radek Krejci | d505e3d | 2018-11-13 09:04:17 +0100 | [diff] [blame] | 2022 | type->flags |= LYS_SET_FRDIGITS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2023 | break; |
| 2024 | case YANG_LENGTH: |
| 2025 | if (type->length) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 2026 | LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2027 | return LY_EVALID; |
| 2028 | } |
| 2029 | type->length = calloc(1, sizeof *type->length); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2030 | LY_CHECK_ERR_RET(!type->length, LOGMEM(ctx->ctx), LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2031 | |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2032 | LY_CHECK_RET(parse_restr(ctx, data, kw, type->length)); |
Radek Krejci | d505e3d | 2018-11-13 09:04:17 +0100 | [diff] [blame] | 2033 | type->flags |= LYS_SET_LENGTH; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2034 | break; |
| 2035 | case YANG_PATH: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2036 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_PATH, 0, &type->path, Y_STR_ARG, &type->exts)); |
Radek Krejci | d505e3d | 2018-11-13 09:04:17 +0100 | [diff] [blame] | 2037 | type->flags |= LYS_SET_PATH; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2038 | break; |
| 2039 | case YANG_PATTERN: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2040 | LY_CHECK_RET(parse_type_pattern(ctx, data, &type->patterns)); |
Radek Krejci | d505e3d | 2018-11-13 09:04:17 +0100 | [diff] [blame] | 2041 | type->flags |= LYS_SET_PATTERN; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2042 | break; |
| 2043 | case YANG_RANGE: |
| 2044 | if (type->range) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 2045 | LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2046 | return LY_EVALID; |
| 2047 | } |
| 2048 | type->range = calloc(1, sizeof *type->range); |
David Sedlák | 7a8b247 | 2019-07-11 15:08:34 +0200 | [diff] [blame] | 2049 | LY_CHECK_ERR_RET(!type->range, LOGMEM(ctx->ctx), LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2050 | |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2051 | LY_CHECK_RET(parse_restr(ctx, data, kw, type->range)); |
Radek Krejci | d505e3d | 2018-11-13 09:04:17 +0100 | [diff] [blame] | 2052 | type->flags |= LYS_SET_RANGE; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2053 | break; |
| 2054 | case YANG_REQUIRE_INSTANCE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2055 | LY_CHECK_RET(parse_type_reqinstance(ctx, data, &type->require_instance, &type->flags, &type->exts)); |
Radek Krejci | d505e3d | 2018-11-13 09:04:17 +0100 | [diff] [blame] | 2056 | /* LYS_SET_REQINST checked and set inside parse_type_reqinstance() */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2057 | break; |
| 2058 | case YANG_TYPE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2059 | LY_ARRAY_NEW_RET(ctx->ctx, type->types, nest_type, LY_EMEM); |
| 2060 | LY_CHECK_RET(parse_type(ctx, data, nest_type)); |
Radek Krejci | d505e3d | 2018-11-13 09:04:17 +0100 | [diff] [blame] | 2061 | type->flags |= LYS_SET_TYPE; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2062 | break; |
| 2063 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2064 | LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &type->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2065 | break; |
| 2066 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 2067 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "type"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2068 | return LY_EVALID; |
| 2069 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2070 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2071 | return ret; |
| 2072 | } |
| 2073 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2074 | /** |
| 2075 | * @brief Parse the leaf statement. |
| 2076 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2077 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2078 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 2079 | * @param[in,out] siblings Siblings to add to. |
| 2080 | * |
| 2081 | * @return LY_ERR values. |
| 2082 | */ |
Radek Krejci | 2d7a47b | 2019-05-16 13:34:10 +0200 | [diff] [blame] | 2083 | LY_ERR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 2084 | parse_leaf(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2085 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2086 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2087 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 2088 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2089 | enum yang_keyword kw; |
| 2090 | struct lysp_node *iter; |
| 2091 | struct lysp_node_leaf *leaf; |
| 2092 | |
| 2093 | /* create structure */ |
| 2094 | leaf = calloc(1, sizeof *leaf); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2095 | LY_CHECK_ERR_RET(!leaf, LOGMEM(ctx->ctx), LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2096 | leaf->nodetype = LYS_LEAF; |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2097 | leaf->parent = parent; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2098 | |
| 2099 | /* insert into siblings */ |
| 2100 | if (!*siblings) { |
| 2101 | *siblings = (struct lysp_node *)leaf; |
| 2102 | } else { |
| 2103 | for (iter = *siblings; iter->next; iter = iter->next); |
| 2104 | iter->next = (struct lysp_node *)leaf; |
| 2105 | } |
| 2106 | |
| 2107 | /* get name */ |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 2108 | LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len)); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2109 | INSERT_WORD(ctx, buf, leaf->name, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2110 | |
| 2111 | /* parse substatements */ |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 2112 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2113 | switch (kw) { |
| 2114 | case YANG_CONFIG: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2115 | LY_CHECK_RET(parse_config(ctx, data, &leaf->flags, &leaf->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2116 | break; |
| 2117 | case YANG_DEFAULT: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2118 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DEFAULT, 0, &leaf->dflt, Y_STR_ARG, &leaf->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2119 | break; |
| 2120 | case YANG_DESCRIPTION: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2121 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &leaf->dsc, Y_STR_ARG, &leaf->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2122 | break; |
| 2123 | case YANG_IF_FEATURE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2124 | LY_CHECK_RET(parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &leaf->iffeatures, Y_STR_ARG, &leaf->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2125 | break; |
| 2126 | case YANG_MANDATORY: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2127 | LY_CHECK_RET(parse_mandatory(ctx, data, &leaf->flags, &leaf->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2128 | break; |
| 2129 | case YANG_MUST: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2130 | LY_CHECK_RET(parse_restrs(ctx, data, kw, &leaf->musts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2131 | break; |
| 2132 | case YANG_REFERENCE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2133 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &leaf->ref, Y_STR_ARG, &leaf->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2134 | break; |
| 2135 | case YANG_STATUS: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2136 | LY_CHECK_RET(parse_status(ctx, data, &leaf->flags, &leaf->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2137 | break; |
| 2138 | case YANG_TYPE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2139 | LY_CHECK_RET(parse_type(ctx, data, &leaf->type)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2140 | break; |
| 2141 | case YANG_UNITS: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2142 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_UNITS, 0, &leaf->units, Y_STR_ARG, &leaf->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2143 | break; |
| 2144 | case YANG_WHEN: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2145 | LY_CHECK_RET(parse_when(ctx, data, &leaf->when)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2146 | break; |
| 2147 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2148 | LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &leaf->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2149 | break; |
| 2150 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 2151 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "leaf"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2152 | return LY_EVALID; |
| 2153 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2154 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 2155 | LY_CHECK_RET(ret); |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 2156 | checks: |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2157 | /* mandatory substatements */ |
| 2158 | if (!leaf->type.name) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 2159 | LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "leaf"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2160 | return LY_EVALID; |
| 2161 | } |
Radek Krejci | b1a5dcc | 2018-11-26 14:50:05 +0100 | [diff] [blame] | 2162 | if ((leaf->flags & LYS_MAND_TRUE) && (leaf->dflt)) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 2163 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMSCOMB, "mandatory", "default", "leaf"); |
Radek Krejci | b1a5dcc | 2018-11-26 14:50:05 +0100 | [diff] [blame] | 2164 | return LY_EVALID; |
| 2165 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2166 | |
| 2167 | return ret; |
| 2168 | } |
| 2169 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2170 | /** |
| 2171 | * @brief Parse the max-elements statement. |
| 2172 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2173 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2174 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 2175 | * @param[in,out] max Value to write to. |
| 2176 | * @param[in,out] flags Flags to write to. |
| 2177 | * @param[in,out] exts Extension instances to add to. |
| 2178 | * |
| 2179 | * @return LY_ERR values. |
| 2180 | */ |
Radek Krejci | 2d7a47b | 2019-05-16 13:34:10 +0200 | [diff] [blame] | 2181 | LY_ERR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 2182 | parse_maxelements(struct lys_parser_ctx *ctx, const char **data, uint32_t *max, uint16_t *flags, struct lysp_ext_instance **exts) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2183 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2184 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2185 | char *buf, *word, *ptr; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 2186 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2187 | unsigned long int num; |
| 2188 | enum yang_keyword kw; |
| 2189 | |
| 2190 | if (*flags & LYS_SET_MAX) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 2191 | LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "max-elements"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2192 | return LY_EVALID; |
| 2193 | } |
| 2194 | *flags |= LYS_SET_MAX; |
| 2195 | |
| 2196 | /* get value */ |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 2197 | LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2198 | |
| 2199 | 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] | 2200 | LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "max-elements"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2201 | free(buf); |
| 2202 | return LY_EVALID; |
| 2203 | } |
| 2204 | |
| 2205 | if (strncmp(word, "unbounded", word_len)) { |
| 2206 | errno = 0; |
| 2207 | num = strtoul(word, &ptr, 10); |
| 2208 | /* we have not parsed the whole argument */ |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 2209 | if ((size_t)(ptr - word) != word_len) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 2210 | LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "max-elements"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2211 | free(buf); |
| 2212 | return LY_EVALID; |
| 2213 | } |
| 2214 | if ((errno == ERANGE) || (num > UINT32_MAX)) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 2215 | LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, "max-elements"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2216 | free(buf); |
| 2217 | return LY_EVALID; |
| 2218 | } |
| 2219 | |
| 2220 | *max = num; |
| 2221 | } |
| 2222 | free(buf); |
| 2223 | |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 2224 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2225 | switch (kw) { |
| 2226 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2227 | LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_MAX, 0, exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2228 | break; |
| 2229 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 2230 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "max-elements"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2231 | return LY_EVALID; |
| 2232 | } |
| 2233 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2234 | return ret; |
| 2235 | } |
| 2236 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2237 | /** |
| 2238 | * @brief Parse the min-elements statement. |
| 2239 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2240 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2241 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 2242 | * @param[in,out] min Value to write to. |
| 2243 | * @param[in,out] flags Flags to write to. |
| 2244 | * @param[in,out] exts Extension instances to add to. |
| 2245 | * |
| 2246 | * @return LY_ERR values. |
| 2247 | */ |
Radek Krejci | 2d7a47b | 2019-05-16 13:34:10 +0200 | [diff] [blame] | 2248 | LY_ERR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 2249 | parse_minelements(struct lys_parser_ctx *ctx, const char **data, uint32_t *min, uint16_t *flags, struct lysp_ext_instance **exts) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2250 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2251 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2252 | char *buf, *word, *ptr; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 2253 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2254 | unsigned long int num; |
| 2255 | enum yang_keyword kw; |
| 2256 | |
| 2257 | if (*flags & LYS_SET_MIN) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 2258 | LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "min-elements"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2259 | return LY_EVALID; |
| 2260 | } |
| 2261 | *flags |= LYS_SET_MIN; |
| 2262 | |
| 2263 | /* get value */ |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 2264 | LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2265 | |
| 2266 | 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] | 2267 | LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "min-elements"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2268 | free(buf); |
| 2269 | return LY_EVALID; |
| 2270 | } |
| 2271 | |
| 2272 | errno = 0; |
| 2273 | num = strtoul(word, &ptr, 10); |
| 2274 | /* we have not parsed the whole argument */ |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 2275 | if ((size_t)(ptr - word) != word_len) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 2276 | LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "min-elements"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2277 | free(buf); |
| 2278 | return LY_EVALID; |
| 2279 | } |
| 2280 | if ((errno == ERANGE) || (num > UINT32_MAX)) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 2281 | LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, "min-elements"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2282 | free(buf); |
| 2283 | return LY_EVALID; |
| 2284 | } |
| 2285 | *min = num; |
| 2286 | free(buf); |
| 2287 | |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 2288 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2289 | switch (kw) { |
| 2290 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2291 | LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_MIN, 0, exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2292 | break; |
| 2293 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 2294 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "min-elements"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2295 | return LY_EVALID; |
| 2296 | } |
| 2297 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2298 | return ret; |
| 2299 | } |
| 2300 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2301 | /** |
| 2302 | * @brief Parse the ordered-by statement. |
| 2303 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2304 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2305 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 2306 | * @param[in,out] flags Flags to write to. |
| 2307 | * @param[in,out] exts Extension instances to add to. |
| 2308 | * |
| 2309 | * @return LY_ERR values. |
| 2310 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2311 | static LY_ERR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 2312 | parse_orderedby(struct lys_parser_ctx *ctx, const char **data, uint16_t *flags, struct lysp_ext_instance **exts) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2313 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2314 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2315 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 2316 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2317 | enum yang_keyword kw; |
| 2318 | |
| 2319 | if (*flags & LYS_ORDBY_MASK) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 2320 | LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "ordered-by"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2321 | return LY_EVALID; |
| 2322 | } |
| 2323 | |
| 2324 | /* get value */ |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 2325 | LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2326 | |
| 2327 | if ((word_len == 6) && !strncmp(word, "system", word_len)) { |
| 2328 | *flags |= LYS_ORDBY_SYSTEM; |
| 2329 | } else if ((word_len == 4) && !strncmp(word, "user", word_len)) { |
| 2330 | *flags |= LYS_ORDBY_USER; |
| 2331 | } else { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 2332 | LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "ordered-by"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2333 | free(buf); |
| 2334 | return LY_EVALID; |
| 2335 | } |
| 2336 | free(buf); |
| 2337 | |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 2338 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2339 | switch (kw) { |
| 2340 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2341 | LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_ORDEREDBY, 0, exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2342 | break; |
| 2343 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 2344 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "ordered-by"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2345 | return LY_EVALID; |
| 2346 | } |
| 2347 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2348 | return ret; |
| 2349 | } |
| 2350 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2351 | /** |
| 2352 | * @brief Parse the leaf-list statement. |
| 2353 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2354 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2355 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 2356 | * @param[in,out] siblings Siblings to add to. |
| 2357 | * |
| 2358 | * @return LY_ERR values. |
| 2359 | */ |
Radek Krejci | 2d7a47b | 2019-05-16 13:34:10 +0200 | [diff] [blame] | 2360 | LY_ERR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 2361 | parse_leaflist(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2362 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2363 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2364 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 2365 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2366 | enum yang_keyword kw; |
| 2367 | struct lysp_node *iter; |
| 2368 | struct lysp_node_leaflist *llist; |
| 2369 | |
| 2370 | /* create structure */ |
| 2371 | llist = calloc(1, sizeof *llist); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2372 | LY_CHECK_ERR_RET(!llist, LOGMEM(ctx->ctx), LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2373 | llist->nodetype = LYS_LEAFLIST; |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2374 | llist->parent = parent; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2375 | |
| 2376 | /* insert into siblings */ |
| 2377 | if (!*siblings) { |
| 2378 | *siblings = (struct lysp_node *)llist; |
| 2379 | } else { |
| 2380 | for (iter = *siblings; iter->next; iter = iter->next); |
| 2381 | iter->next = (struct lysp_node *)llist; |
| 2382 | } |
| 2383 | |
| 2384 | /* get name */ |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 2385 | LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len)); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2386 | INSERT_WORD(ctx, buf, llist->name, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2387 | |
| 2388 | /* parse substatements */ |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 2389 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2390 | switch (kw) { |
| 2391 | case YANG_CONFIG: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2392 | LY_CHECK_RET(parse_config(ctx, data, &llist->flags, &llist->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2393 | break; |
| 2394 | case YANG_DEFAULT: |
Radek Krejci | ceaf212 | 2019-01-02 15:03:26 +0100 | [diff] [blame] | 2395 | YANG_CHECK_STMTVER2_RET(ctx, "default", "leaf-list"); |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2396 | LY_CHECK_RET(parse_text_fields(ctx, data, LYEXT_SUBSTMT_DEFAULT, &llist->dflts, Y_STR_ARG, &llist->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2397 | break; |
| 2398 | case YANG_DESCRIPTION: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2399 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &llist->dsc, Y_STR_ARG, &llist->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2400 | break; |
| 2401 | case YANG_IF_FEATURE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2402 | LY_CHECK_RET(parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &llist->iffeatures, Y_STR_ARG, &llist->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2403 | break; |
| 2404 | case YANG_MAX_ELEMENTS: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2405 | LY_CHECK_RET(parse_maxelements(ctx, data, &llist->max, &llist->flags, &llist->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2406 | break; |
| 2407 | case YANG_MIN_ELEMENTS: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2408 | LY_CHECK_RET(parse_minelements(ctx, data, &llist->min, &llist->flags, &llist->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2409 | break; |
| 2410 | case YANG_MUST: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2411 | LY_CHECK_RET(parse_restrs(ctx, data, kw, &llist->musts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2412 | break; |
| 2413 | case YANG_ORDERED_BY: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2414 | LY_CHECK_RET(parse_orderedby(ctx, data, &llist->flags, &llist->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2415 | break; |
| 2416 | case YANG_REFERENCE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2417 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &llist->ref, Y_STR_ARG, &llist->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2418 | break; |
| 2419 | case YANG_STATUS: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2420 | LY_CHECK_RET(parse_status(ctx, data, &llist->flags, &llist->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2421 | break; |
| 2422 | case YANG_TYPE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2423 | LY_CHECK_RET(parse_type(ctx, data, &llist->type)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2424 | break; |
| 2425 | case YANG_UNITS: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2426 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_UNITS, 0, &llist->units, Y_STR_ARG, &llist->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2427 | break; |
| 2428 | case YANG_WHEN: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2429 | LY_CHECK_RET(parse_when(ctx, data, &llist->when)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2430 | break; |
| 2431 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2432 | LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &llist->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2433 | break; |
| 2434 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 2435 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "llist"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2436 | return LY_EVALID; |
| 2437 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2438 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 2439 | LY_CHECK_RET(ret); |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 2440 | checks: |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2441 | /* mandatory substatements */ |
| 2442 | if (!llist->type.name) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 2443 | LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "leaf-list"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2444 | return LY_EVALID; |
| 2445 | } |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 2446 | if ((llist->min) && (llist->dflts)) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 2447 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMSCOMB, "min-elements", "default", "leaf-list"); |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 2448 | return LY_EVALID; |
| 2449 | } |
Radek Krejci | df6cad1 | 2018-11-28 17:10:55 +0100 | [diff] [blame] | 2450 | if (llist->max && llist->min > llist->max) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 2451 | LOGVAL_PARSER(ctx, LYVE_SEMANTICS, |
Radek Krejci | df6cad1 | 2018-11-28 17:10:55 +0100 | [diff] [blame] | 2452 | "Invalid combination of min-elements and max-elements: min value %u is bigger than the max value %u.", |
| 2453 | llist->min, llist->max); |
| 2454 | return LY_EVALID; |
| 2455 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2456 | |
| 2457 | return ret; |
| 2458 | } |
| 2459 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2460 | /** |
| 2461 | * @brief Parse the refine statement. |
| 2462 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2463 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2464 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 2465 | * @param[in,out] refines Refines to add to. |
| 2466 | * |
| 2467 | * @return LY_ERR values. |
| 2468 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2469 | static LY_ERR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 2470 | parse_refine(struct lys_parser_ctx *ctx, const char **data, struct lysp_refine **refines) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2471 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2472 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2473 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 2474 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2475 | enum yang_keyword kw; |
| 2476 | struct lysp_refine *rf; |
| 2477 | |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 2478 | LY_ARRAY_NEW_RET(ctx->ctx, *refines, rf, LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2479 | |
| 2480 | /* get value */ |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 2481 | LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len)); |
David Sedlák | b9b892c | 2019-07-12 14:44:02 +0200 | [diff] [blame] | 2482 | YANG_CHECK_NONEMPTY(ctx, word_len, "refine"); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2483 | INSERT_WORD(ctx, buf, rf->nodeid, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2484 | |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 2485 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2486 | switch (kw) { |
| 2487 | case YANG_CONFIG: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2488 | LY_CHECK_RET(parse_config(ctx, data, &rf->flags, &rf->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2489 | break; |
| 2490 | case YANG_DEFAULT: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2491 | LY_CHECK_RET(parse_text_fields(ctx, data, LYEXT_SUBSTMT_DEFAULT, &rf->dflts, Y_STR_ARG, &rf->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2492 | break; |
| 2493 | case YANG_DESCRIPTION: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2494 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &rf->dsc, Y_STR_ARG, &rf->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2495 | break; |
| 2496 | case YANG_IF_FEATURE: |
Radek Krejci | ceaf212 | 2019-01-02 15:03:26 +0100 | [diff] [blame] | 2497 | YANG_CHECK_STMTVER2_RET(ctx, "if-feature", "refine"); |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2498 | LY_CHECK_RET(parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &rf->iffeatures, Y_STR_ARG, &rf->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2499 | break; |
| 2500 | case YANG_MAX_ELEMENTS: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2501 | LY_CHECK_RET(parse_maxelements(ctx, data, &rf->max, &rf->flags, &rf->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2502 | break; |
| 2503 | case YANG_MIN_ELEMENTS: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2504 | LY_CHECK_RET(parse_minelements(ctx, data, &rf->min, &rf->flags, &rf->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2505 | break; |
| 2506 | case YANG_MUST: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2507 | LY_CHECK_RET(parse_restrs(ctx, data, kw, &rf->musts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2508 | break; |
| 2509 | case YANG_MANDATORY: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2510 | LY_CHECK_RET(parse_mandatory(ctx, data, &rf->flags, &rf->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2511 | break; |
| 2512 | case YANG_REFERENCE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2513 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &rf->ref, Y_STR_ARG, &rf->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2514 | break; |
| 2515 | case YANG_PRESENCE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2516 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_PRESENCE, 0, &rf->presence, Y_STR_ARG, &rf->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2517 | break; |
| 2518 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2519 | LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &rf->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2520 | break; |
| 2521 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 2522 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "refine"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2523 | return LY_EVALID; |
| 2524 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2525 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2526 | return ret; |
| 2527 | } |
| 2528 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2529 | /** |
| 2530 | * @brief Parse the typedef statement. |
| 2531 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2532 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2533 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 2534 | * @param[in,out] typedefs Typedefs to add to. |
| 2535 | * |
| 2536 | * @return LY_ERR values. |
| 2537 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2538 | static LY_ERR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 2539 | parse_typedef(struct lys_parser_ctx *ctx, struct lysp_node *parent, const char **data, struct lysp_tpdf **typedefs) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2540 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2541 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2542 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 2543 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2544 | enum yang_keyword kw; |
| 2545 | struct lysp_tpdf *tpdf; |
| 2546 | |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 2547 | LY_ARRAY_NEW_RET(ctx->ctx, *typedefs, tpdf, LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2548 | |
| 2549 | /* get value */ |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 2550 | LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len)); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2551 | INSERT_WORD(ctx, buf, tpdf->name, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2552 | |
| 2553 | /* parse substatements */ |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 2554 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2555 | switch (kw) { |
| 2556 | case YANG_DEFAULT: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2557 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DEFAULT, 0, &tpdf->dflt, Y_STR_ARG, &tpdf->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2558 | break; |
| 2559 | case YANG_DESCRIPTION: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2560 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &tpdf->dsc, Y_STR_ARG, &tpdf->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2561 | break; |
| 2562 | case YANG_REFERENCE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2563 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &tpdf->ref, Y_STR_ARG, &tpdf->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2564 | break; |
| 2565 | case YANG_STATUS: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2566 | LY_CHECK_RET(parse_status(ctx, data, &tpdf->flags, &tpdf->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2567 | break; |
| 2568 | case YANG_TYPE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2569 | LY_CHECK_RET(parse_type(ctx, data, &tpdf->type)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2570 | break; |
| 2571 | case YANG_UNITS: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2572 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_UNITS, 0, &tpdf->units, Y_STR_ARG, &tpdf->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2573 | break; |
| 2574 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2575 | LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &tpdf->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2576 | break; |
| 2577 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 2578 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "typedef"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2579 | return LY_EVALID; |
| 2580 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2581 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 2582 | LY_CHECK_RET(ret); |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 2583 | checks: |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2584 | /* mandatory substatements */ |
| 2585 | if (!tpdf->type.name) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 2586 | LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "typedef"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2587 | return LY_EVALID; |
| 2588 | } |
| 2589 | |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 2590 | /* store data for collision check */ |
Radek Krejci | 7fc6829 | 2019-06-12 13:51:09 +0200 | [diff] [blame] | 2591 | if (parent && !(parent->nodetype & (LYS_GROUPING | LYS_ACTION | LYS_INOUT | LYS_NOTIF))) { |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 2592 | ly_set_add(&ctx->tpdfs_nodes, parent, 0); |
| 2593 | } |
| 2594 | |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2595 | return ret; |
| 2596 | } |
| 2597 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2598 | /** |
| 2599 | * @brief Parse the input or output statement. |
| 2600 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2601 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2602 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 2603 | * @param[in] kw Type of this particular keyword |
| 2604 | * @param[in,out] inout_p Input/output pointer to write to. |
| 2605 | * |
| 2606 | * @return LY_ERR values. |
| 2607 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2608 | static LY_ERR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 2609 | parse_inout(struct lys_parser_ctx *ctx, const char **data, enum yang_keyword inout_kw, struct lysp_node *parent, struct lysp_action_inout *inout_p) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2610 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2611 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2612 | char *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 2613 | size_t word_len; |
Radek Krejci | 1011365 | 2018-11-14 16:56:50 +0100 | [diff] [blame] | 2614 | enum yang_keyword kw; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2615 | |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 2616 | if (inout_p->nodetype) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 2617 | LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(inout_kw)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2618 | return LY_EVALID; |
| 2619 | } |
| 2620 | |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 2621 | /* initiate structure */ |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 2622 | 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] | 2623 | inout_p->parent = parent; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2624 | |
| 2625 | /* parse substatements */ |
Radek Krejci | 7fc6829 | 2019-06-12 13:51:09 +0200 | [diff] [blame] | 2626 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2627 | switch (kw) { |
| 2628 | case YANG_ANYDATA: |
Radek Krejci | ceaf212 | 2019-01-02 15:03:26 +0100 | [diff] [blame] | 2629 | YANG_CHECK_STMTVER2_RET(ctx, "anydata", ly_stmt2str(inout_kw)); |
Radek Krejci | 1011365 | 2018-11-14 16:56:50 +0100 | [diff] [blame] | 2630 | /* fall through */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2631 | case YANG_ANYXML: |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 2632 | LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)inout_p, &inout_p->data)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2633 | break; |
| 2634 | case YANG_CHOICE: |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 2635 | LY_CHECK_RET(parse_choice(ctx, data, (struct lysp_node*)inout_p, &inout_p->data)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2636 | break; |
| 2637 | case YANG_CONTAINER: |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 2638 | LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)inout_p, &inout_p->data)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2639 | break; |
| 2640 | case YANG_LEAF: |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 2641 | LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)inout_p, &inout_p->data)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2642 | break; |
| 2643 | case YANG_LEAF_LIST: |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 2644 | LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)inout_p, &inout_p->data)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2645 | break; |
| 2646 | case YANG_LIST: |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 2647 | LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)inout_p, &inout_p->data)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2648 | break; |
| 2649 | case YANG_USES: |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 2650 | LY_CHECK_RET(parse_uses(ctx, data, (struct lysp_node*)inout_p, &inout_p->data)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2651 | break; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2652 | case YANG_TYPEDEF: |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 2653 | LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node*)inout_p, data, &inout_p->typedefs)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2654 | break; |
| 2655 | case YANG_MUST: |
Radek Krejci | ceaf212 | 2019-01-02 15:03:26 +0100 | [diff] [blame] | 2656 | YANG_CHECK_STMTVER2_RET(ctx, "must", ly_stmt2str(inout_kw)); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 2657 | LY_CHECK_RET(parse_restrs(ctx, data, kw, &inout_p->musts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2658 | break; |
| 2659 | case YANG_GROUPING: |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 2660 | LY_CHECK_RET(parse_grouping(ctx, data, (struct lysp_node*)inout_p, &inout_p->groupings)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2661 | break; |
| 2662 | case YANG_CUSTOM: |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 2663 | LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &inout_p->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2664 | break; |
| 2665 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 2666 | 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] | 2667 | return LY_EVALID; |
| 2668 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2669 | } |
Radek Krejci | 7fc6829 | 2019-06-12 13:51:09 +0200 | [diff] [blame] | 2670 | LY_CHECK_RET(ret); |
| 2671 | checks: |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 2672 | /* finalize parent pointers to the reallocated items */ |
David Sedlák | d2ebe57 | 2019-07-22 12:53:14 +0200 | [diff] [blame] | 2673 | LY_CHECK_RET(lysp_parse_finalize_reallocated(ctx, inout_p->groupings, NULL, NULL, NULL)); |
Radek Krejci | 7fc6829 | 2019-06-12 13:51:09 +0200 | [diff] [blame] | 2674 | |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2675 | return ret; |
| 2676 | } |
| 2677 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2678 | /** |
| 2679 | * @brief Parse the action statement. |
| 2680 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2681 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2682 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 2683 | * @param[in,out] actions Actions to add to. |
| 2684 | * |
| 2685 | * @return LY_ERR values. |
| 2686 | */ |
Radek Krejci | 2d7a47b | 2019-05-16 13:34:10 +0200 | [diff] [blame] | 2687 | LY_ERR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 2688 | parse_action(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_action **actions) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2689 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2690 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2691 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 2692 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2693 | enum yang_keyword kw; |
| 2694 | struct lysp_action *act; |
| 2695 | |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 2696 | LY_ARRAY_NEW_RET(ctx->ctx, *actions, act, LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2697 | |
| 2698 | /* get value */ |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 2699 | LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len)); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2700 | INSERT_WORD(ctx, buf, act->name, word, word_len); |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2701 | act->nodetype = LYS_ACTION; |
| 2702 | act->parent = parent; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2703 | |
Radek Krejci | 7fc6829 | 2019-06-12 13:51:09 +0200 | [diff] [blame] | 2704 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2705 | switch (kw) { |
| 2706 | case YANG_DESCRIPTION: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2707 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &act->dsc, Y_STR_ARG, &act->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2708 | break; |
| 2709 | case YANG_IF_FEATURE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2710 | LY_CHECK_RET(parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &act->iffeatures, Y_STR_ARG, &act->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2711 | break; |
| 2712 | case YANG_REFERENCE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2713 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &act->ref, Y_STR_ARG, &act->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2714 | break; |
| 2715 | case YANG_STATUS: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2716 | LY_CHECK_RET(parse_status(ctx, data, &act->flags, &act->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2717 | break; |
| 2718 | |
| 2719 | case YANG_INPUT: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2720 | LY_CHECK_RET(parse_inout(ctx, data, kw, (struct lysp_node*)act, &act->input)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2721 | break; |
| 2722 | case YANG_OUTPUT: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2723 | LY_CHECK_RET(parse_inout(ctx, data, kw, (struct lysp_node*)act, &act->output)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2724 | break; |
| 2725 | |
| 2726 | case YANG_TYPEDEF: |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 2727 | LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node*)act, data, &act->typedefs)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2728 | break; |
| 2729 | case YANG_GROUPING: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2730 | LY_CHECK_RET(parse_grouping(ctx, data, (struct lysp_node*)act, &act->groupings)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2731 | break; |
| 2732 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2733 | LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &act->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2734 | break; |
| 2735 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 2736 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), parent ? "action" : "rpc"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2737 | return LY_EVALID; |
| 2738 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2739 | } |
Radek Krejci | 7fc6829 | 2019-06-12 13:51:09 +0200 | [diff] [blame] | 2740 | LY_CHECK_RET(ret); |
| 2741 | checks: |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 2742 | /* finalize parent pointers to the reallocated items */ |
David Sedlák | d2ebe57 | 2019-07-22 12:53:14 +0200 | [diff] [blame] | 2743 | LY_CHECK_RET(lysp_parse_finalize_reallocated(ctx, act->groupings, NULL, NULL, NULL)); |
Radek Krejci | 7fc6829 | 2019-06-12 13:51:09 +0200 | [diff] [blame] | 2744 | |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2745 | return ret; |
| 2746 | } |
| 2747 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2748 | /** |
| 2749 | * @brief Parse the notification statement. |
| 2750 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2751 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2752 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 2753 | * @param[in,out] notifs Notifications to add to. |
| 2754 | * |
| 2755 | * @return LY_ERR values. |
| 2756 | */ |
Radek Krejci | 2d7a47b | 2019-05-16 13:34:10 +0200 | [diff] [blame] | 2757 | LY_ERR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 2758 | parse_notif(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_notif **notifs) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2759 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2760 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2761 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 2762 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2763 | enum yang_keyword kw; |
| 2764 | struct lysp_notif *notif; |
| 2765 | |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 2766 | LY_ARRAY_NEW_RET(ctx->ctx, *notifs, notif, LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2767 | |
| 2768 | /* get value */ |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 2769 | LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len)); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2770 | INSERT_WORD(ctx, buf, notif->name, word, word_len); |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2771 | notif->nodetype = LYS_NOTIF; |
| 2772 | notif->parent = parent; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2773 | |
Radek Krejci | 7fc6829 | 2019-06-12 13:51:09 +0200 | [diff] [blame] | 2774 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2775 | switch (kw) { |
| 2776 | case YANG_DESCRIPTION: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2777 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, ¬if->dsc, Y_STR_ARG, ¬if->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2778 | break; |
| 2779 | case YANG_IF_FEATURE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2780 | LY_CHECK_RET(parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, ¬if->iffeatures, Y_STR_ARG, ¬if->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2781 | break; |
| 2782 | case YANG_REFERENCE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2783 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, ¬if->ref, Y_STR_ARG, ¬if->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2784 | break; |
| 2785 | case YANG_STATUS: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2786 | LY_CHECK_RET(parse_status(ctx, data, ¬if->flags, ¬if->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2787 | break; |
| 2788 | |
| 2789 | case YANG_ANYDATA: |
Radek Krejci | ceaf212 | 2019-01-02 15:03:26 +0100 | [diff] [blame] | 2790 | YANG_CHECK_STMTVER2_RET(ctx, "anydata", "notification"); |
Radek Krejci | 1011365 | 2018-11-14 16:56:50 +0100 | [diff] [blame] | 2791 | /* fall through */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2792 | case YANG_ANYXML: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2793 | LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)notif, ¬if->data)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2794 | break; |
| 2795 | case YANG_CHOICE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2796 | LY_CHECK_RET(parse_case(ctx, data, (struct lysp_node*)notif, ¬if->data)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2797 | break; |
| 2798 | case YANG_CONTAINER: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2799 | LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)notif, ¬if->data)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2800 | break; |
| 2801 | case YANG_LEAF: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2802 | LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)notif, ¬if->data)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2803 | break; |
| 2804 | case YANG_LEAF_LIST: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2805 | LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)notif, ¬if->data)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2806 | break; |
| 2807 | case YANG_LIST: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2808 | LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)notif, ¬if->data)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2809 | break; |
| 2810 | case YANG_USES: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2811 | LY_CHECK_RET(parse_uses(ctx, data, (struct lysp_node*)notif, ¬if->data)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2812 | break; |
| 2813 | |
| 2814 | case YANG_MUST: |
Radek Krejci | ceaf212 | 2019-01-02 15:03:26 +0100 | [diff] [blame] | 2815 | YANG_CHECK_STMTVER2_RET(ctx, "must", "notification"); |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2816 | LY_CHECK_RET(parse_restrs(ctx, data, kw, ¬if->musts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2817 | break; |
| 2818 | case YANG_TYPEDEF: |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 2819 | LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node*)notif, data, ¬if->typedefs)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2820 | break; |
| 2821 | case YANG_GROUPING: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2822 | LY_CHECK_RET(parse_grouping(ctx, data, (struct lysp_node*)notif, ¬if->groupings)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2823 | break; |
| 2824 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2825 | LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, ¬if->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2826 | break; |
| 2827 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 2828 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "notification"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2829 | return LY_EVALID; |
| 2830 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2831 | } |
Radek Krejci | 7fc6829 | 2019-06-12 13:51:09 +0200 | [diff] [blame] | 2832 | LY_CHECK_RET(ret); |
| 2833 | checks: |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 2834 | /* finalize parent pointers to the reallocated items */ |
David Sedlák | d2ebe57 | 2019-07-22 12:53:14 +0200 | [diff] [blame] | 2835 | LY_CHECK_RET(lysp_parse_finalize_reallocated(ctx, notif->groupings, NULL, NULL, NULL)); |
Radek Krejci | 7fc6829 | 2019-06-12 13:51:09 +0200 | [diff] [blame] | 2836 | |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2837 | return ret; |
| 2838 | } |
| 2839 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2840 | /** |
| 2841 | * @brief Parse the grouping statement. |
| 2842 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2843 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2844 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 2845 | * @param[in,out] groupings Groupings to add to. |
| 2846 | * |
| 2847 | * @return LY_ERR values. |
| 2848 | */ |
Radek Krejci | 2d7a47b | 2019-05-16 13:34:10 +0200 | [diff] [blame] | 2849 | LY_ERR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 2850 | parse_grouping(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_grp **groupings) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2851 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2852 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2853 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 2854 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2855 | enum yang_keyword kw; |
| 2856 | struct lysp_grp *grp; |
| 2857 | |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 2858 | LY_ARRAY_NEW_RET(ctx->ctx, *groupings, grp, LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2859 | |
| 2860 | /* get value */ |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 2861 | LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len)); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2862 | INSERT_WORD(ctx, buf, grp->name, word, word_len); |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2863 | grp->nodetype = LYS_GROUPING; |
| 2864 | grp->parent = parent; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2865 | |
Radek Krejci | 7fc6829 | 2019-06-12 13:51:09 +0200 | [diff] [blame] | 2866 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2867 | switch (kw) { |
| 2868 | case YANG_DESCRIPTION: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2869 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &grp->dsc, Y_STR_ARG, &grp->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2870 | break; |
| 2871 | case YANG_REFERENCE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2872 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &grp->ref, Y_STR_ARG, &grp->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2873 | break; |
| 2874 | case YANG_STATUS: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2875 | LY_CHECK_RET(parse_status(ctx, data, &grp->flags, &grp->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2876 | break; |
| 2877 | |
| 2878 | case YANG_ANYDATA: |
Radek Krejci | ceaf212 | 2019-01-02 15:03:26 +0100 | [diff] [blame] | 2879 | YANG_CHECK_STMTVER2_RET(ctx, "anydata", "grouping"); |
Radek Krejci | 1011365 | 2018-11-14 16:56:50 +0100 | [diff] [blame] | 2880 | /* fall through */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2881 | case YANG_ANYXML: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2882 | LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)grp, &grp->data)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2883 | break; |
| 2884 | case YANG_CHOICE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2885 | LY_CHECK_RET(parse_choice(ctx, data, (struct lysp_node*)grp, &grp->data)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2886 | break; |
| 2887 | case YANG_CONTAINER: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2888 | LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)grp, &grp->data)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2889 | break; |
| 2890 | case YANG_LEAF: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2891 | LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)grp, &grp->data)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2892 | break; |
| 2893 | case YANG_LEAF_LIST: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2894 | LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)grp, &grp->data)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2895 | break; |
| 2896 | case YANG_LIST: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2897 | LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)grp, &grp->data)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2898 | break; |
| 2899 | case YANG_USES: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2900 | LY_CHECK_RET(parse_uses(ctx, data, (struct lysp_node*)grp, &grp->data)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2901 | break; |
| 2902 | |
| 2903 | case YANG_TYPEDEF: |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 2904 | LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node*)grp, data, &grp->typedefs)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2905 | break; |
| 2906 | case YANG_ACTION: |
Radek Krejci | ceaf212 | 2019-01-02 15:03:26 +0100 | [diff] [blame] | 2907 | YANG_CHECK_STMTVER2_RET(ctx, "action", "grouping"); |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2908 | LY_CHECK_RET(parse_action(ctx, data, (struct lysp_node*)grp, &grp->actions)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2909 | break; |
| 2910 | case YANG_GROUPING: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2911 | LY_CHECK_RET(parse_grouping(ctx, data, (struct lysp_node*)grp, &grp->groupings)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2912 | break; |
| 2913 | case YANG_NOTIFICATION: |
Radek Krejci | ceaf212 | 2019-01-02 15:03:26 +0100 | [diff] [blame] | 2914 | YANG_CHECK_STMTVER2_RET(ctx, "notification", "grouping"); |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2915 | LY_CHECK_RET(parse_notif(ctx, data, (struct lysp_node*)grp, &grp->notifs)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2916 | break; |
| 2917 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2918 | LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &grp->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2919 | break; |
| 2920 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 2921 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "grouping"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2922 | return LY_EVALID; |
| 2923 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2924 | } |
Radek Krejci | 7fc6829 | 2019-06-12 13:51:09 +0200 | [diff] [blame] | 2925 | LY_CHECK_RET(ret); |
| 2926 | checks: |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 2927 | /* finalize parent pointers to the reallocated items */ |
David Sedlák | 0d6de5a | 2019-07-22 13:25:44 +0200 | [diff] [blame] | 2928 | LY_CHECK_RET(lysp_parse_finalize_reallocated(ctx, grp->groupings, NULL, grp->actions, grp->notifs)); |
Radek Krejci | 7fc6829 | 2019-06-12 13:51:09 +0200 | [diff] [blame] | 2929 | |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2930 | return ret; |
| 2931 | } |
| 2932 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2933 | /** |
David Sedlák | 0d6de5a | 2019-07-22 13:25:44 +0200 | [diff] [blame] | 2934 | * @brief Parse the augment statement. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2935 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2936 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2937 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 2938 | * @param[in,out] augments Augments to add to. |
| 2939 | * |
| 2940 | * @return LY_ERR values. |
| 2941 | */ |
Radek Krejci | 2d7a47b | 2019-05-16 13:34:10 +0200 | [diff] [blame] | 2942 | LY_ERR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 2943 | parse_augment(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_augment **augments) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2944 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2945 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2946 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 2947 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2948 | enum yang_keyword kw; |
| 2949 | struct lysp_augment *aug; |
| 2950 | |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 2951 | LY_ARRAY_NEW_RET(ctx->ctx, *augments, aug, LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2952 | |
| 2953 | /* get value */ |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 2954 | LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len)); |
David Sedlák | b9b892c | 2019-07-12 14:44:02 +0200 | [diff] [blame] | 2955 | YANG_CHECK_NONEMPTY(ctx, word_len, "augment"); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2956 | INSERT_WORD(ctx, buf, aug->nodeid, word, word_len); |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2957 | aug->nodetype = LYS_AUGMENT; |
| 2958 | aug->parent = parent; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2959 | |
Radek Krejci | 7fc6829 | 2019-06-12 13:51:09 +0200 | [diff] [blame] | 2960 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2961 | switch (kw) { |
| 2962 | case YANG_DESCRIPTION: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2963 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &aug->dsc, Y_STR_ARG, &aug->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2964 | break; |
| 2965 | case YANG_IF_FEATURE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2966 | LY_CHECK_RET(parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &aug->iffeatures, Y_STR_ARG, &aug->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2967 | break; |
| 2968 | case YANG_REFERENCE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2969 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &aug->ref, Y_STR_ARG, &aug->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2970 | break; |
| 2971 | case YANG_STATUS: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2972 | LY_CHECK_RET(parse_status(ctx, data, &aug->flags, &aug->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2973 | break; |
| 2974 | case YANG_WHEN: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2975 | LY_CHECK_RET(parse_when(ctx, data, &aug->when)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2976 | break; |
| 2977 | |
| 2978 | case YANG_ANYDATA: |
Radek Krejci | ceaf212 | 2019-01-02 15:03:26 +0100 | [diff] [blame] | 2979 | YANG_CHECK_STMTVER2_RET(ctx, "anydata", "augment"); |
Radek Krejci | 1011365 | 2018-11-14 16:56:50 +0100 | [diff] [blame] | 2980 | /* fall through */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2981 | case YANG_ANYXML: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2982 | LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)aug, &aug->child)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2983 | break; |
| 2984 | case YANG_CASE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2985 | LY_CHECK_RET(parse_case(ctx, data, (struct lysp_node*)aug, &aug->child)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2986 | break; |
| 2987 | case YANG_CHOICE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2988 | LY_CHECK_RET(parse_choice(ctx, data, (struct lysp_node*)aug, &aug->child)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2989 | break; |
| 2990 | case YANG_CONTAINER: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2991 | LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)aug, &aug->child)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2992 | break; |
| 2993 | case YANG_LEAF: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2994 | LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)aug, &aug->child)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2995 | break; |
| 2996 | case YANG_LEAF_LIST: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2997 | LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)aug, &aug->child)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2998 | break; |
| 2999 | case YANG_LIST: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3000 | LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)aug, &aug->child)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3001 | break; |
| 3002 | case YANG_USES: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3003 | LY_CHECK_RET(parse_uses(ctx, data, (struct lysp_node*)aug, &aug->child)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3004 | break; |
| 3005 | |
| 3006 | case YANG_ACTION: |
Radek Krejci | ceaf212 | 2019-01-02 15:03:26 +0100 | [diff] [blame] | 3007 | YANG_CHECK_STMTVER2_RET(ctx, "action", "augment"); |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3008 | LY_CHECK_RET(parse_action(ctx, data, (struct lysp_node*)aug, &aug->actions)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3009 | break; |
| 3010 | case YANG_NOTIFICATION: |
Radek Krejci | ceaf212 | 2019-01-02 15:03:26 +0100 | [diff] [blame] | 3011 | YANG_CHECK_STMTVER2_RET(ctx, "notification", "augment"); |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3012 | LY_CHECK_RET(parse_notif(ctx, data, (struct lysp_node*)aug, &aug->notifs)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3013 | break; |
| 3014 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3015 | LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &aug->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3016 | break; |
| 3017 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 3018 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "augment"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3019 | return LY_EVALID; |
| 3020 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3021 | } |
Radek Krejci | 7fc6829 | 2019-06-12 13:51:09 +0200 | [diff] [blame] | 3022 | LY_CHECK_RET(ret); |
| 3023 | checks: |
| 3024 | /* finalize parent pointers to the reallocated items */ |
David Sedlák | d2ebe57 | 2019-07-22 12:53:14 +0200 | [diff] [blame] | 3025 | LY_CHECK_RET(lysp_parse_finalize_reallocated(ctx, NULL, NULL, aug->actions, aug->notifs)); |
Radek Krejci | 7fc6829 | 2019-06-12 13:51:09 +0200 | [diff] [blame] | 3026 | |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3027 | return ret; |
| 3028 | } |
| 3029 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3030 | /** |
| 3031 | * @brief Parse the uses statement. |
| 3032 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3033 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3034 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 3035 | * @param[in,out] siblings Siblings to add to. |
| 3036 | * |
| 3037 | * @return LY_ERR values. |
| 3038 | */ |
Radek Krejci | 2d7a47b | 2019-05-16 13:34:10 +0200 | [diff] [blame] | 3039 | LY_ERR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 3040 | parse_uses(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3041 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3042 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3043 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 3044 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3045 | enum yang_keyword kw; |
| 3046 | struct lysp_node *iter; |
| 3047 | struct lysp_node_uses *uses; |
| 3048 | |
| 3049 | /* create structure */ |
| 3050 | uses = calloc(1, sizeof *uses); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3051 | LY_CHECK_ERR_RET(!uses, LOGMEM(ctx->ctx), LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3052 | uses->nodetype = LYS_USES; |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3053 | uses->parent = parent; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3054 | |
| 3055 | /* insert into siblings */ |
| 3056 | if (!*siblings) { |
| 3057 | *siblings = (struct lysp_node *)uses; |
| 3058 | } else { |
| 3059 | for (iter = *siblings; iter->next; iter = iter->next); |
| 3060 | iter->next = (struct lysp_node *)uses; |
| 3061 | } |
| 3062 | |
| 3063 | /* get name */ |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 3064 | LY_CHECK_RET(get_argument(ctx, data, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &word_len)); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3065 | INSERT_WORD(ctx, buf, uses->name, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3066 | |
| 3067 | /* parse substatements */ |
Radek Krejci | 7fc6829 | 2019-06-12 13:51:09 +0200 | [diff] [blame] | 3068 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3069 | switch (kw) { |
| 3070 | case YANG_DESCRIPTION: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3071 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &uses->dsc, Y_STR_ARG, &uses->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3072 | break; |
| 3073 | case YANG_IF_FEATURE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3074 | LY_CHECK_RET(parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &uses->iffeatures, Y_STR_ARG, &uses->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3075 | break; |
| 3076 | case YANG_REFERENCE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3077 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &uses->ref, Y_STR_ARG, &uses->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3078 | break; |
| 3079 | case YANG_STATUS: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3080 | LY_CHECK_RET(parse_status(ctx, data, &uses->flags, &uses->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3081 | break; |
| 3082 | case YANG_WHEN: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3083 | LY_CHECK_RET(parse_when(ctx, data, &uses->when)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3084 | break; |
| 3085 | |
| 3086 | case YANG_REFINE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3087 | LY_CHECK_RET(parse_refine(ctx, data, &uses->refines)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3088 | break; |
| 3089 | case YANG_AUGMENT: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3090 | LY_CHECK_RET(parse_augment(ctx, data, (struct lysp_node*)uses, &uses->augments)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3091 | break; |
| 3092 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3093 | LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &uses->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3094 | break; |
| 3095 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 3096 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "uses"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3097 | return LY_EVALID; |
| 3098 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3099 | } |
Radek Krejci | 7fc6829 | 2019-06-12 13:51:09 +0200 | [diff] [blame] | 3100 | checks: |
| 3101 | /* finalize parent pointers to the reallocated items */ |
David Sedlák | d2ebe57 | 2019-07-22 12:53:14 +0200 | [diff] [blame] | 3102 | LY_CHECK_RET(lysp_parse_finalize_reallocated(ctx, NULL, uses->augments, NULL, NULL)); |
Radek Krejci | 7fc6829 | 2019-06-12 13:51:09 +0200 | [diff] [blame] | 3103 | |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3104 | return ret; |
| 3105 | } |
| 3106 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3107 | /** |
| 3108 | * @brief Parse the case statement. |
| 3109 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3110 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3111 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 3112 | * @param[in,out] siblings Siblings to add to. |
| 3113 | * |
| 3114 | * @return LY_ERR values. |
| 3115 | */ |
Radek Krejci | 2d7a47b | 2019-05-16 13:34:10 +0200 | [diff] [blame] | 3116 | LY_ERR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 3117 | parse_case(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3118 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3119 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3120 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 3121 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3122 | enum yang_keyword kw; |
| 3123 | struct lysp_node *iter; |
| 3124 | struct lysp_node_case *cas; |
| 3125 | |
| 3126 | /* create structure */ |
| 3127 | cas = calloc(1, sizeof *cas); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3128 | LY_CHECK_ERR_RET(!cas, LOGMEM(ctx->ctx), LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3129 | cas->nodetype = LYS_CASE; |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3130 | cas->parent = parent; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3131 | |
| 3132 | /* insert into siblings */ |
| 3133 | if (!*siblings) { |
| 3134 | *siblings = (struct lysp_node *)cas; |
| 3135 | } else { |
| 3136 | for (iter = *siblings; iter->next; iter = iter->next); |
| 3137 | iter->next = (struct lysp_node *)cas; |
| 3138 | } |
| 3139 | |
| 3140 | /* get name */ |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 3141 | LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len)); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3142 | INSERT_WORD(ctx, buf, cas->name, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3143 | |
| 3144 | /* parse substatements */ |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 3145 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3146 | switch (kw) { |
| 3147 | case YANG_DESCRIPTION: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3148 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &cas->dsc, Y_STR_ARG, &cas->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3149 | break; |
| 3150 | case YANG_IF_FEATURE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3151 | LY_CHECK_RET(parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &cas->iffeatures, Y_STR_ARG, &cas->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3152 | break; |
| 3153 | case YANG_REFERENCE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3154 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &cas->ref, Y_STR_ARG, &cas->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3155 | break; |
| 3156 | case YANG_STATUS: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3157 | LY_CHECK_RET(parse_status(ctx, data, &cas->flags, &cas->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3158 | break; |
| 3159 | case YANG_WHEN: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3160 | LY_CHECK_RET(parse_when(ctx, data, &cas->when)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3161 | break; |
| 3162 | |
| 3163 | case YANG_ANYDATA: |
Radek Krejci | ceaf212 | 2019-01-02 15:03:26 +0100 | [diff] [blame] | 3164 | YANG_CHECK_STMTVER2_RET(ctx, "anydata", "case"); |
Radek Krejci | 1011365 | 2018-11-14 16:56:50 +0100 | [diff] [blame] | 3165 | /* fall through */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3166 | case YANG_ANYXML: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3167 | LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)cas, &cas->child)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3168 | break; |
| 3169 | case YANG_CHOICE: |
Radek Krejci | a9026eb | 2018-12-12 16:04:47 +0100 | [diff] [blame] | 3170 | LY_CHECK_RET(parse_choice(ctx, data, (struct lysp_node*)cas, &cas->child)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3171 | break; |
| 3172 | case YANG_CONTAINER: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3173 | LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)cas, &cas->child)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3174 | break; |
| 3175 | case YANG_LEAF: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3176 | LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)cas, &cas->child)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3177 | break; |
| 3178 | case YANG_LEAF_LIST: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3179 | LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)cas, &cas->child)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3180 | break; |
| 3181 | case YANG_LIST: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3182 | LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)cas, &cas->child)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3183 | break; |
| 3184 | case YANG_USES: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3185 | LY_CHECK_RET(parse_uses(ctx, data, (struct lysp_node*)cas, &cas->child)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3186 | break; |
| 3187 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3188 | LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &cas->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3189 | break; |
| 3190 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 3191 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "case"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3192 | return LY_EVALID; |
| 3193 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3194 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3195 | return ret; |
| 3196 | } |
| 3197 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3198 | /** |
| 3199 | * @brief Parse the choice statement. |
| 3200 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3201 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3202 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 3203 | * @param[in,out] siblings Siblings to add to. |
| 3204 | * |
| 3205 | * @return LY_ERR values. |
| 3206 | */ |
Radek Krejci | 2d7a47b | 2019-05-16 13:34:10 +0200 | [diff] [blame] | 3207 | LY_ERR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 3208 | parse_choice(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3209 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3210 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3211 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 3212 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3213 | enum yang_keyword kw; |
| 3214 | struct lysp_node *iter; |
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 | |
| 3217 | /* create structure */ |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3218 | choice = calloc(1, sizeof *choice); |
| 3219 | LY_CHECK_ERR_RET(!choice, LOGMEM(ctx->ctx), LY_EMEM); |
| 3220 | choice->nodetype = LYS_CHOICE; |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3221 | choice->parent = parent; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3222 | |
| 3223 | /* insert into siblings */ |
| 3224 | if (!*siblings) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3225 | *siblings = (struct lysp_node *)choice; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3226 | } else { |
| 3227 | for (iter = *siblings; iter->next; iter = iter->next); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3228 | iter->next = (struct lysp_node *)choice; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3229 | } |
| 3230 | |
| 3231 | /* get name */ |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 3232 | LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len)); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3233 | INSERT_WORD(ctx, buf, choice->name, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3234 | |
| 3235 | /* parse substatements */ |
Radek Krejci | a9026eb | 2018-12-12 16:04:47 +0100 | [diff] [blame] | 3236 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3237 | switch (kw) { |
| 3238 | case YANG_CONFIG: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3239 | LY_CHECK_RET(parse_config(ctx, data, &choice->flags, &choice->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3240 | break; |
| 3241 | case YANG_DESCRIPTION: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3242 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &choice->dsc, Y_STR_ARG, &choice->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3243 | break; |
| 3244 | case YANG_IF_FEATURE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3245 | LY_CHECK_RET(parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &choice->iffeatures, Y_STR_ARG, &choice->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3246 | break; |
| 3247 | case YANG_MANDATORY: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3248 | LY_CHECK_RET(parse_mandatory(ctx, data, &choice->flags, &choice->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3249 | break; |
| 3250 | case YANG_REFERENCE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3251 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &choice->ref, Y_STR_ARG, &choice->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3252 | break; |
| 3253 | case YANG_STATUS: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3254 | LY_CHECK_RET(parse_status(ctx, data, &choice->flags, &choice->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3255 | break; |
| 3256 | case YANG_WHEN: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3257 | LY_CHECK_RET(parse_when(ctx, data, &choice->when)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3258 | break; |
| 3259 | case YANG_DEFAULT: |
Radek Krejci | a9026eb | 2018-12-12 16:04:47 +0100 | [diff] [blame] | 3260 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DEFAULT, 0, &choice->dflt, Y_PREF_IDENTIF_ARG, &choice->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3261 | break; |
| 3262 | |
| 3263 | case YANG_ANYDATA: |
Radek Krejci | ceaf212 | 2019-01-02 15:03:26 +0100 | [diff] [blame] | 3264 | YANG_CHECK_STMTVER2_RET(ctx, "anydata", "choice"); |
Radek Krejci | 1011365 | 2018-11-14 16:56:50 +0100 | [diff] [blame] | 3265 | /* fall through */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3266 | case YANG_ANYXML: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3267 | LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)choice, &choice->child)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3268 | break; |
| 3269 | case YANG_CASE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3270 | LY_CHECK_RET(parse_case(ctx, data, (struct lysp_node*)choice, &choice->child)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3271 | break; |
| 3272 | case YANG_CHOICE: |
Radek Krejci | ceaf212 | 2019-01-02 15:03:26 +0100 | [diff] [blame] | 3273 | YANG_CHECK_STMTVER2_RET(ctx, "choice", "choice"); |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3274 | LY_CHECK_RET(parse_choice(ctx, data, (struct lysp_node*)choice, &choice->child)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3275 | break; |
| 3276 | case YANG_CONTAINER: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3277 | LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)choice, &choice->child)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3278 | break; |
| 3279 | case YANG_LEAF: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3280 | LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)choice, &choice->child)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3281 | break; |
| 3282 | case YANG_LEAF_LIST: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3283 | LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)choice, &choice->child)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3284 | break; |
| 3285 | case YANG_LIST: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3286 | LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)choice, &choice->child)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3287 | break; |
| 3288 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3289 | LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &choice->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3290 | break; |
| 3291 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 3292 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "choice"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3293 | return LY_EVALID; |
| 3294 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3295 | } |
Radek Krejci | a9026eb | 2018-12-12 16:04:47 +0100 | [diff] [blame] | 3296 | LY_CHECK_RET(ret); |
| 3297 | checks: |
| 3298 | if ((choice->flags & LYS_MAND_TRUE) && choice->dflt) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 3299 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMSCOMB, "mandatory", "default", "choice"); |
Radek Krejci | a9026eb | 2018-12-12 16:04:47 +0100 | [diff] [blame] | 3300 | return LY_EVALID; |
| 3301 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3302 | return ret; |
| 3303 | } |
| 3304 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3305 | /** |
| 3306 | * @brief Parse the container statement. |
| 3307 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3308 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3309 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 3310 | * @param[in,out] siblings Siblings to add to. |
| 3311 | * |
| 3312 | * @return LY_ERR values. |
| 3313 | */ |
Radek Krejci | 2d7a47b | 2019-05-16 13:34:10 +0200 | [diff] [blame] | 3314 | LY_ERR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 3315 | parse_container(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3316 | { |
| 3317 | LY_ERR ret = 0; |
| 3318 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 3319 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3320 | enum yang_keyword kw; |
| 3321 | struct lysp_node *iter; |
| 3322 | struct lysp_node_container *cont; |
| 3323 | |
| 3324 | /* create structure */ |
| 3325 | cont = calloc(1, sizeof *cont); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3326 | LY_CHECK_ERR_RET(!cont, LOGMEM(ctx->ctx), LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3327 | cont->nodetype = LYS_CONTAINER; |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3328 | cont->parent = parent; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3329 | |
| 3330 | /* insert into siblings */ |
| 3331 | if (!*siblings) { |
| 3332 | *siblings = (struct lysp_node *)cont; |
| 3333 | } else { |
| 3334 | for (iter = *siblings; iter->next; iter = iter->next); |
| 3335 | iter->next = (struct lysp_node *)cont; |
| 3336 | } |
| 3337 | |
| 3338 | /* get name */ |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 3339 | LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len)); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3340 | INSERT_WORD(ctx, buf, cont->name, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3341 | |
| 3342 | /* parse substatements */ |
Radek Krejci | 7fc6829 | 2019-06-12 13:51:09 +0200 | [diff] [blame] | 3343 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3344 | switch (kw) { |
| 3345 | case YANG_CONFIG: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3346 | LY_CHECK_RET(parse_config(ctx, data, &cont->flags, &cont->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3347 | break; |
| 3348 | case YANG_DESCRIPTION: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3349 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &cont->dsc, Y_STR_ARG, &cont->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3350 | break; |
| 3351 | case YANG_IF_FEATURE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3352 | LY_CHECK_RET(parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &cont->iffeatures, Y_STR_ARG, &cont->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3353 | break; |
| 3354 | case YANG_REFERENCE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3355 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &cont->ref, Y_STR_ARG, &cont->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3356 | break; |
| 3357 | case YANG_STATUS: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3358 | LY_CHECK_RET(parse_status(ctx, data, &cont->flags, &cont->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3359 | break; |
| 3360 | case YANG_WHEN: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3361 | LY_CHECK_RET(parse_when(ctx, data, &cont->when)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3362 | break; |
| 3363 | case YANG_PRESENCE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3364 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_PRESENCE, 0, &cont->presence, Y_STR_ARG, &cont->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3365 | break; |
| 3366 | |
| 3367 | case YANG_ANYDATA: |
Radek Krejci | ceaf212 | 2019-01-02 15:03:26 +0100 | [diff] [blame] | 3368 | YANG_CHECK_STMTVER2_RET(ctx, "anydata", "container"); |
Radek Krejci | 1011365 | 2018-11-14 16:56:50 +0100 | [diff] [blame] | 3369 | /* fall through */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3370 | case YANG_ANYXML: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3371 | LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)cont, &cont->child)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3372 | break; |
| 3373 | case YANG_CHOICE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3374 | LY_CHECK_RET(parse_choice(ctx, data, (struct lysp_node*)cont, &cont->child)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3375 | break; |
| 3376 | case YANG_CONTAINER: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3377 | LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)cont, &cont->child)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3378 | break; |
| 3379 | case YANG_LEAF: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3380 | LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)cont, &cont->child)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3381 | break; |
| 3382 | case YANG_LEAF_LIST: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3383 | LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)cont, &cont->child)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3384 | break; |
| 3385 | case YANG_LIST: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3386 | LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)cont, &cont->child)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3387 | break; |
| 3388 | case YANG_USES: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3389 | LY_CHECK_RET(parse_uses(ctx, data, (struct lysp_node*)cont, &cont->child)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3390 | break; |
| 3391 | |
| 3392 | case YANG_TYPEDEF: |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 3393 | LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node*)cont, data, &cont->typedefs)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3394 | break; |
| 3395 | case YANG_MUST: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3396 | LY_CHECK_RET(parse_restrs(ctx, data, kw, &cont->musts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3397 | break; |
| 3398 | case YANG_ACTION: |
Radek Krejci | ceaf212 | 2019-01-02 15:03:26 +0100 | [diff] [blame] | 3399 | YANG_CHECK_STMTVER2_RET(ctx, "action", "container"); |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3400 | LY_CHECK_RET(parse_action(ctx, data, (struct lysp_node*)cont, &cont->actions)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3401 | break; |
| 3402 | case YANG_GROUPING: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3403 | LY_CHECK_RET(parse_grouping(ctx, data, (struct lysp_node*)cont, &cont->groupings)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3404 | break; |
| 3405 | case YANG_NOTIFICATION: |
Radek Krejci | ceaf212 | 2019-01-02 15:03:26 +0100 | [diff] [blame] | 3406 | YANG_CHECK_STMTVER2_RET(ctx, "notification", "container"); |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3407 | LY_CHECK_RET(parse_notif(ctx, data, (struct lysp_node*)cont, &cont->notifs)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3408 | break; |
| 3409 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3410 | LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &cont->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3411 | break; |
| 3412 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 3413 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "container"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3414 | return LY_EVALID; |
| 3415 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3416 | } |
Radek Krejci | 7fc6829 | 2019-06-12 13:51:09 +0200 | [diff] [blame] | 3417 | checks: |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 3418 | /* finalize parent pointers to the reallocated items */ |
David Sedlák | d2ebe57 | 2019-07-22 12:53:14 +0200 | [diff] [blame] | 3419 | LY_CHECK_RET(lysp_parse_finalize_reallocated(ctx, cont->groupings, NULL, cont->actions, cont->notifs)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3420 | return ret; |
| 3421 | } |
| 3422 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3423 | /** |
| 3424 | * @brief Parse the list statement. |
| 3425 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3426 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3427 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 3428 | * @param[in,out] siblings Siblings to add to. |
| 3429 | * |
| 3430 | * @return LY_ERR values. |
| 3431 | */ |
Radek Krejci | 2d7a47b | 2019-05-16 13:34:10 +0200 | [diff] [blame] | 3432 | LY_ERR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 3433 | parse_list(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3434 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3435 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3436 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 3437 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3438 | enum yang_keyword kw; |
| 3439 | struct lysp_node *iter; |
| 3440 | struct lysp_node_list *list; |
| 3441 | |
| 3442 | /* create structure */ |
| 3443 | list = calloc(1, sizeof *list); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3444 | LY_CHECK_ERR_RET(!list, LOGMEM(ctx->ctx), LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3445 | list->nodetype = LYS_LIST; |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3446 | list->parent = parent; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3447 | |
| 3448 | /* insert into siblings */ |
| 3449 | if (!*siblings) { |
| 3450 | *siblings = (struct lysp_node *)list; |
| 3451 | } else { |
| 3452 | for (iter = *siblings; iter->next; iter = iter->next); |
| 3453 | iter->next = (struct lysp_node *)list; |
| 3454 | } |
| 3455 | |
| 3456 | /* get name */ |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 3457 | LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len)); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3458 | INSERT_WORD(ctx, buf, list->name, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3459 | |
| 3460 | /* parse substatements */ |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3461 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3462 | switch (kw) { |
| 3463 | case YANG_CONFIG: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3464 | LY_CHECK_RET(parse_config(ctx, data, &list->flags, &list->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3465 | break; |
| 3466 | case YANG_DESCRIPTION: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3467 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &list->dsc, Y_STR_ARG, &list->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3468 | break; |
| 3469 | case YANG_IF_FEATURE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3470 | LY_CHECK_RET(parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &list->iffeatures, Y_STR_ARG, &list->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3471 | break; |
| 3472 | case YANG_REFERENCE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3473 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &list->ref, Y_STR_ARG, &list->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3474 | break; |
| 3475 | case YANG_STATUS: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3476 | LY_CHECK_RET(parse_status(ctx, data, &list->flags, &list->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3477 | break; |
| 3478 | case YANG_WHEN: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3479 | LY_CHECK_RET(parse_when(ctx, data, &list->when)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3480 | break; |
| 3481 | case YANG_KEY: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3482 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_KEY, 0, &list->key, Y_STR_ARG, &list->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3483 | break; |
| 3484 | case YANG_MAX_ELEMENTS: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3485 | LY_CHECK_RET(parse_maxelements(ctx, data, &list->max, &list->flags, &list->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3486 | break; |
| 3487 | case YANG_MIN_ELEMENTS: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3488 | LY_CHECK_RET(parse_minelements(ctx, data, &list->min, &list->flags, &list->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3489 | break; |
| 3490 | case YANG_ORDERED_BY: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3491 | LY_CHECK_RET(parse_orderedby(ctx, data, &list->flags, &list->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3492 | break; |
| 3493 | case YANG_UNIQUE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3494 | LY_CHECK_RET(parse_text_fields(ctx, data, LYEXT_SUBSTMT_UNIQUE, &list->uniques, Y_STR_ARG, &list->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3495 | break; |
| 3496 | |
| 3497 | case YANG_ANYDATA: |
Radek Krejci | ceaf212 | 2019-01-02 15:03:26 +0100 | [diff] [blame] | 3498 | YANG_CHECK_STMTVER2_RET(ctx, "anydata", "list"); |
Radek Krejci | 1011365 | 2018-11-14 16:56:50 +0100 | [diff] [blame] | 3499 | /* fall through */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3500 | case YANG_ANYXML: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3501 | LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)list, &list->child)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3502 | break; |
| 3503 | case YANG_CHOICE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3504 | LY_CHECK_RET(parse_choice(ctx, data, (struct lysp_node*)list, &list->child)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3505 | break; |
| 3506 | case YANG_CONTAINER: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3507 | LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)list, &list->child)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3508 | break; |
| 3509 | case YANG_LEAF: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3510 | LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)list, &list->child)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3511 | break; |
| 3512 | case YANG_LEAF_LIST: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3513 | LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)list, &list->child)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3514 | break; |
| 3515 | case YANG_LIST: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3516 | LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)list, &list->child)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3517 | break; |
| 3518 | case YANG_USES: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3519 | LY_CHECK_RET(parse_uses(ctx, data, (struct lysp_node*)list, &list->child)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3520 | break; |
| 3521 | |
| 3522 | case YANG_TYPEDEF: |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 3523 | LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node*)list, data, &list->typedefs)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3524 | break; |
| 3525 | case YANG_MUST: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3526 | LY_CHECK_RET(parse_restrs(ctx, data, kw, &list->musts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3527 | break; |
| 3528 | case YANG_ACTION: |
Radek Krejci | ceaf212 | 2019-01-02 15:03:26 +0100 | [diff] [blame] | 3529 | YANG_CHECK_STMTVER2_RET(ctx, "action", "list"); |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3530 | LY_CHECK_RET(parse_action(ctx, data, (struct lysp_node*)list, &list->actions)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3531 | break; |
| 3532 | case YANG_GROUPING: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3533 | LY_CHECK_RET(parse_grouping(ctx, data, (struct lysp_node*)list, &list->groupings)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3534 | break; |
| 3535 | case YANG_NOTIFICATION: |
Radek Krejci | ceaf212 | 2019-01-02 15:03:26 +0100 | [diff] [blame] | 3536 | YANG_CHECK_STMTVER2_RET(ctx, "notification", "list"); |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3537 | LY_CHECK_RET(parse_notif(ctx, data, (struct lysp_node*)list, &list->notifs)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3538 | break; |
| 3539 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3540 | LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &list->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3541 | break; |
| 3542 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 3543 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "list"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3544 | return LY_EVALID; |
| 3545 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3546 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3547 | LY_CHECK_RET(ret); |
| 3548 | checks: |
Radek Krejci | 7fc6829 | 2019-06-12 13:51:09 +0200 | [diff] [blame] | 3549 | /* finalize parent pointers to the reallocated items */ |
David Sedlák | d2ebe57 | 2019-07-22 12:53:14 +0200 | [diff] [blame] | 3550 | LY_CHECK_RET(lysp_parse_finalize_reallocated(ctx, list->groupings, NULL, list->actions, list->notifs)); |
Radek Krejci | 7fc6829 | 2019-06-12 13:51:09 +0200 | [diff] [blame] | 3551 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3552 | if (list->max && list->min > list->max) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 3553 | LOGVAL_PARSER(ctx, LYVE_SEMANTICS, |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3554 | "Invalid combination of min-elements and max-elements: min value %u is bigger than the max value %u.", |
| 3555 | list->min, list->max); |
| 3556 | return LY_EVALID; |
| 3557 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3558 | |
| 3559 | return ret; |
| 3560 | } |
| 3561 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3562 | /** |
| 3563 | * @brief Parse the yin-element statement. |
| 3564 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3565 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3566 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 3567 | * @param[in,out] flags Flags to write to. |
| 3568 | * @param[in,out] exts Extension instances to add to. |
| 3569 | * |
| 3570 | * @return LY_ERR values. |
| 3571 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3572 | static LY_ERR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 3573 | parse_yinelement(struct lys_parser_ctx *ctx, const char **data, uint16_t *flags, struct lysp_ext_instance **exts) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3574 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3575 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3576 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 3577 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3578 | enum yang_keyword kw; |
| 3579 | |
| 3580 | if (*flags & LYS_YINELEM_MASK) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 3581 | LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "yin-element"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3582 | return LY_EVALID; |
| 3583 | } |
| 3584 | |
| 3585 | /* get value */ |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 3586 | LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3587 | |
| 3588 | if ((word_len == 4) && !strncmp(word, "true", word_len)) { |
| 3589 | *flags |= LYS_YINELEM_TRUE; |
| 3590 | } else if ((word_len == 5) && !strncmp(word, "false", word_len)) { |
| 3591 | *flags |= LYS_YINELEM_FALSE; |
| 3592 | } else { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 3593 | LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "yin-element"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3594 | free(buf); |
| 3595 | return LY_EVALID; |
| 3596 | } |
| 3597 | free(buf); |
| 3598 | |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 3599 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3600 | switch (kw) { |
| 3601 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3602 | LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_YINELEM, 0, exts)); |
| 3603 | LY_CHECK_RET(ret); break; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3604 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 3605 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "yin-element"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3606 | return LY_EVALID; |
| 3607 | } |
| 3608 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3609 | return ret; |
| 3610 | } |
| 3611 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3612 | /** |
David Sedlák | 2444f8f | 2019-07-09 11:02:47 +0200 | [diff] [blame] | 3613 | * @brief Parse the argument statement. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3614 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3615 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3616 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 3617 | * @param[in,out] argument Value to write to. |
| 3618 | * @param[in,out] flags Flags to write to. |
| 3619 | * @param[in,out] exts Extension instances to add to. |
| 3620 | * |
| 3621 | * @return LY_ERR values. |
| 3622 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3623 | static LY_ERR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 3624 | parse_argument(struct lys_parser_ctx *ctx, const char **data, const char **argument, uint16_t *flags, struct lysp_ext_instance **exts) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3625 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3626 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3627 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 3628 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3629 | enum yang_keyword kw; |
| 3630 | |
| 3631 | if (*argument) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 3632 | LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "argument"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3633 | return LY_EVALID; |
| 3634 | } |
| 3635 | |
| 3636 | /* get value */ |
David Sedlák | 2444f8f | 2019-07-09 11:02:47 +0200 | [diff] [blame] | 3637 | LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len)); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3638 | INSERT_WORD(ctx, buf, *argument, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3639 | |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 3640 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3641 | switch (kw) { |
| 3642 | case YANG_YIN_ELEMENT: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3643 | LY_CHECK_RET(parse_yinelement(ctx, data, flags, exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3644 | break; |
| 3645 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3646 | LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_ARGUMENT, 0, exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3647 | break; |
| 3648 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 3649 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "argument"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3650 | return LY_EVALID; |
| 3651 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3652 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3653 | return ret; |
| 3654 | } |
| 3655 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3656 | /** |
| 3657 | * @brief Parse the extension statement. |
| 3658 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3659 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3660 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 3661 | * @param[in,out] extensions Extensions to add to. |
| 3662 | * |
| 3663 | * @return LY_ERR values. |
| 3664 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3665 | static LY_ERR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 3666 | parse_extension(struct lys_parser_ctx *ctx, const char **data, struct lysp_ext **extensions) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3667 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3668 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3669 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 3670 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3671 | enum yang_keyword kw; |
| 3672 | struct lysp_ext *ex; |
| 3673 | |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 3674 | LY_ARRAY_NEW_RET(ctx->ctx, *extensions, ex, LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3675 | |
| 3676 | /* get value */ |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 3677 | LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len)); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3678 | INSERT_WORD(ctx, buf, ex->name, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3679 | |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 3680 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3681 | switch (kw) { |
| 3682 | case YANG_DESCRIPTION: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3683 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &ex->dsc, Y_STR_ARG, &ex->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3684 | break; |
| 3685 | case YANG_REFERENCE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3686 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &ex->ref, Y_STR_ARG, &ex->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3687 | break; |
| 3688 | case YANG_STATUS: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3689 | LY_CHECK_RET(parse_status(ctx, data, &ex->flags, &ex->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3690 | break; |
| 3691 | case YANG_ARGUMENT: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3692 | LY_CHECK_RET(parse_argument(ctx, data, &ex->argument, &ex->flags, &ex->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3693 | break; |
| 3694 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3695 | LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &ex->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3696 | break; |
| 3697 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 3698 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "extension"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3699 | return LY_EVALID; |
| 3700 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3701 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3702 | return ret; |
| 3703 | } |
| 3704 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3705 | /** |
| 3706 | * @brief Parse the deviate statement. |
| 3707 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3708 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3709 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 3710 | * @param[in,out] deviates Deviates to add to. |
| 3711 | * |
| 3712 | * @return LY_ERR values. |
| 3713 | */ |
Radek Krejci | 2d7a47b | 2019-05-16 13:34:10 +0200 | [diff] [blame] | 3714 | LY_ERR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 3715 | parse_deviate(struct lys_parser_ctx *ctx, const char **data, struct lysp_deviate **deviates) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3716 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3717 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3718 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 3719 | size_t word_len, dev_mod; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3720 | enum yang_keyword kw; |
| 3721 | struct lysp_deviate *iter, *d; |
| 3722 | struct lysp_deviate_add *d_add = NULL; |
| 3723 | struct lysp_deviate_rpl *d_rpl = NULL; |
| 3724 | struct lysp_deviate_del *d_del = NULL; |
| 3725 | const char **d_units, ***d_uniques, ***d_dflts; |
| 3726 | struct lysp_restr **d_musts; |
| 3727 | uint16_t *d_flags; |
| 3728 | uint32_t *d_min, *d_max; |
| 3729 | |
| 3730 | /* get value */ |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 3731 | LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3732 | |
| 3733 | if ((word_len == 13) && !strncmp(word, "not-supported", word_len)) { |
| 3734 | dev_mod = LYS_DEV_NOT_SUPPORTED; |
| 3735 | } else if ((word_len == 3) && !strncmp(word, "add", word_len)) { |
| 3736 | dev_mod = LYS_DEV_ADD; |
| 3737 | } else if ((word_len == 7) && !strncmp(word, "replace", word_len)) { |
| 3738 | dev_mod = LYS_DEV_REPLACE; |
| 3739 | } else if ((word_len == 6) && !strncmp(word, "delete", word_len)) { |
| 3740 | dev_mod = LYS_DEV_DELETE; |
| 3741 | } else { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 3742 | LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "deviate"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3743 | free(buf); |
| 3744 | return LY_EVALID; |
| 3745 | } |
| 3746 | free(buf); |
| 3747 | |
| 3748 | /* create structure */ |
| 3749 | switch (dev_mod) { |
| 3750 | case LYS_DEV_NOT_SUPPORTED: |
| 3751 | d = calloc(1, sizeof *d); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3752 | LY_CHECK_ERR_RET(!d, LOGMEM(ctx->ctx), LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3753 | break; |
| 3754 | case LYS_DEV_ADD: |
| 3755 | d_add = calloc(1, sizeof *d_add); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3756 | LY_CHECK_ERR_RET(!d_add, LOGMEM(ctx->ctx), LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3757 | d = (struct lysp_deviate *)d_add; |
| 3758 | d_units = &d_add->units; |
| 3759 | d_uniques = &d_add->uniques; |
| 3760 | d_dflts = &d_add->dflts; |
| 3761 | d_musts = &d_add->musts; |
| 3762 | d_flags = &d_add->flags; |
| 3763 | d_min = &d_add->min; |
| 3764 | d_max = &d_add->max; |
| 3765 | break; |
| 3766 | case LYS_DEV_REPLACE: |
| 3767 | d_rpl = calloc(1, sizeof *d_rpl); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3768 | LY_CHECK_ERR_RET(!d_rpl, LOGMEM(ctx->ctx), LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3769 | d = (struct lysp_deviate *)d_rpl; |
| 3770 | d_units = &d_rpl->units; |
| 3771 | d_flags = &d_rpl->flags; |
| 3772 | d_min = &d_rpl->min; |
| 3773 | d_max = &d_rpl->max; |
| 3774 | break; |
| 3775 | case LYS_DEV_DELETE: |
| 3776 | d_del = calloc(1, sizeof *d_del); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3777 | LY_CHECK_ERR_RET(!d_del, LOGMEM(ctx->ctx), LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3778 | d = (struct lysp_deviate *)d_del; |
| 3779 | d_units = &d_del->units; |
| 3780 | d_uniques = &d_del->uniques; |
| 3781 | d_dflts = &d_del->dflts; |
| 3782 | d_musts = &d_del->musts; |
| 3783 | d_flags = &d_del->flags; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3784 | break; |
| 3785 | default: |
| 3786 | assert(0); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3787 | LOGINT_RET(ctx->ctx); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3788 | } |
| 3789 | d->mod = dev_mod; |
| 3790 | |
| 3791 | /* insert into siblings */ |
| 3792 | if (!*deviates) { |
| 3793 | *deviates = d; |
| 3794 | } else { |
| 3795 | for (iter = *deviates; iter->next; iter = iter->next); |
| 3796 | iter->next = d; |
| 3797 | } |
| 3798 | |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 3799 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3800 | switch (kw) { |
| 3801 | case YANG_CONFIG: |
| 3802 | switch (dev_mod) { |
| 3803 | case LYS_DEV_NOT_SUPPORTED: |
Michal Vasko | 492bec7 | 2018-09-18 13:11:10 +0200 | [diff] [blame] | 3804 | case LYS_DEV_DELETE: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 3805 | 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] | 3806 | return LY_EVALID; |
| 3807 | default: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3808 | LY_CHECK_RET(parse_config(ctx, data, d_flags, &d->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3809 | break; |
| 3810 | } |
| 3811 | break; |
| 3812 | case YANG_DEFAULT: |
| 3813 | switch (dev_mod) { |
| 3814 | case LYS_DEV_NOT_SUPPORTED: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 3815 | 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] | 3816 | return LY_EVALID; |
| 3817 | case LYS_DEV_REPLACE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3818 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DEFAULT, 0, &d_rpl->dflt, Y_STR_ARG, &d->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3819 | break; |
| 3820 | default: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3821 | LY_CHECK_RET(parse_text_fields(ctx, data, LYEXT_SUBSTMT_DEFAULT, d_dflts, Y_STR_ARG, &d->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3822 | break; |
| 3823 | } |
| 3824 | break; |
| 3825 | case YANG_MANDATORY: |
| 3826 | switch (dev_mod) { |
| 3827 | case LYS_DEV_NOT_SUPPORTED: |
Michal Vasko | 492bec7 | 2018-09-18 13:11:10 +0200 | [diff] [blame] | 3828 | case LYS_DEV_DELETE: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 3829 | 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] | 3830 | return LY_EVALID; |
| 3831 | default: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3832 | LY_CHECK_RET(parse_mandatory(ctx, data, d_flags, &d->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3833 | break; |
| 3834 | } |
| 3835 | break; |
| 3836 | case YANG_MAX_ELEMENTS: |
| 3837 | switch (dev_mod) { |
| 3838 | case LYS_DEV_NOT_SUPPORTED: |
Michal Vasko | 492bec7 | 2018-09-18 13:11:10 +0200 | [diff] [blame] | 3839 | case LYS_DEV_DELETE: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 3840 | 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] | 3841 | return LY_EVALID; |
| 3842 | default: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3843 | LY_CHECK_RET(parse_maxelements(ctx, data, d_max, d_flags, &d->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3844 | break; |
| 3845 | } |
| 3846 | break; |
| 3847 | case YANG_MIN_ELEMENTS: |
| 3848 | switch (dev_mod) { |
| 3849 | case LYS_DEV_NOT_SUPPORTED: |
Michal Vasko | 492bec7 | 2018-09-18 13:11:10 +0200 | [diff] [blame] | 3850 | case LYS_DEV_DELETE: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 3851 | 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] | 3852 | return LY_EVALID; |
| 3853 | default: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3854 | LY_CHECK_RET(parse_minelements(ctx, data, d_min, d_flags, &d->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3855 | break; |
| 3856 | } |
| 3857 | break; |
| 3858 | case YANG_MUST: |
| 3859 | switch (dev_mod) { |
| 3860 | case LYS_DEV_NOT_SUPPORTED: |
Michal Vasko | 492bec7 | 2018-09-18 13:11:10 +0200 | [diff] [blame] | 3861 | case LYS_DEV_REPLACE: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 3862 | 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] | 3863 | return LY_EVALID; |
| 3864 | default: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3865 | LY_CHECK_RET(parse_restrs(ctx, data, kw, d_musts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3866 | break; |
| 3867 | } |
| 3868 | break; |
| 3869 | case YANG_TYPE: |
| 3870 | switch (dev_mod) { |
| 3871 | case LYS_DEV_NOT_SUPPORTED: |
| 3872 | case LYS_DEV_ADD: |
| 3873 | case LYS_DEV_DELETE: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 3874 | 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] | 3875 | return LY_EVALID; |
| 3876 | default: |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 3877 | if (d_rpl->type) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 3878 | LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw)); |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 3879 | return LY_EVALID; |
| 3880 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3881 | d_rpl->type = calloc(1, sizeof *d_rpl->type); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3882 | LY_CHECK_ERR_RET(!d_rpl->type, LOGMEM(ctx->ctx), LY_EMEM); |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3883 | LY_CHECK_RET(parse_type(ctx, data, d_rpl->type)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3884 | break; |
| 3885 | } |
| 3886 | break; |
| 3887 | case YANG_UNIQUE: |
| 3888 | switch (dev_mod) { |
| 3889 | case LYS_DEV_NOT_SUPPORTED: |
| 3890 | case LYS_DEV_REPLACE: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 3891 | 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] | 3892 | return LY_EVALID; |
| 3893 | default: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3894 | LY_CHECK_RET(parse_text_fields(ctx, data, LYEXT_SUBSTMT_UNIQUE, d_uniques, Y_STR_ARG, &d->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3895 | break; |
| 3896 | } |
| 3897 | break; |
| 3898 | case YANG_UNITS: |
| 3899 | switch (dev_mod) { |
| 3900 | case LYS_DEV_NOT_SUPPORTED: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 3901 | 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] | 3902 | return LY_EVALID; |
| 3903 | default: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3904 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_UNITS, 0, d_units, Y_STR_ARG, &d->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3905 | break; |
| 3906 | } |
| 3907 | break; |
| 3908 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3909 | LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &d->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3910 | break; |
| 3911 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 3912 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "deviate"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3913 | return LY_EVALID; |
| 3914 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3915 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3916 | return ret; |
| 3917 | } |
| 3918 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3919 | /** |
| 3920 | * @brief Parse the deviation statement. |
| 3921 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3922 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3923 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 3924 | * @param[in,out] deviations Deviations to add to. |
| 3925 | * |
| 3926 | * @return LY_ERR values. |
| 3927 | */ |
Radek Krejci | 2d7a47b | 2019-05-16 13:34:10 +0200 | [diff] [blame] | 3928 | LY_ERR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 3929 | parse_deviation(struct lys_parser_ctx *ctx, const char **data, struct lysp_deviation **deviations) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3930 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3931 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3932 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 3933 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3934 | enum yang_keyword kw; |
| 3935 | struct lysp_deviation *dev; |
| 3936 | |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 3937 | LY_ARRAY_NEW_RET(ctx->ctx, *deviations, dev, LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3938 | |
| 3939 | /* get value */ |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 3940 | LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len)); |
David Sedlák | b9b892c | 2019-07-12 14:44:02 +0200 | [diff] [blame] | 3941 | YANG_CHECK_NONEMPTY(ctx, word_len, "deviation"); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3942 | INSERT_WORD(ctx, buf, dev->nodeid, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3943 | |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 3944 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3945 | switch (kw) { |
| 3946 | case YANG_DESCRIPTION: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3947 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &dev->dsc, Y_STR_ARG, &dev->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3948 | break; |
| 3949 | case YANG_DEVIATE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3950 | LY_CHECK_RET(parse_deviate(ctx, data, &dev->deviates)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3951 | break; |
| 3952 | case YANG_REFERENCE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3953 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &dev->ref, Y_STR_ARG, &dev->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3954 | break; |
| 3955 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3956 | LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &dev->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3957 | break; |
| 3958 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 3959 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "deviation"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3960 | return LY_EVALID; |
| 3961 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3962 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 3963 | LY_CHECK_RET(ret); |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 3964 | checks: |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3965 | /* mandatory substatements */ |
| 3966 | if (!dev->deviates) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 3967 | LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "deviate", "deviation"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3968 | return LY_EVALID; |
| 3969 | } |
| 3970 | |
| 3971 | return ret; |
| 3972 | } |
| 3973 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3974 | /** |
| 3975 | * @brief Parse the feature statement. |
| 3976 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3977 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3978 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 3979 | * @param[in,out] features Features to add to. |
| 3980 | * |
| 3981 | * @return LY_ERR values. |
| 3982 | */ |
Radek Krejci | 2d7a47b | 2019-05-16 13:34:10 +0200 | [diff] [blame] | 3983 | LY_ERR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 3984 | parse_feature(struct lys_parser_ctx *ctx, const char **data, struct lysp_feature **features) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3985 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3986 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3987 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 3988 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3989 | enum yang_keyword kw; |
| 3990 | struct lysp_feature *feat; |
| 3991 | |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 3992 | LY_ARRAY_NEW_RET(ctx->ctx, *features, feat, LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3993 | |
| 3994 | /* get value */ |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 3995 | LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len)); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3996 | INSERT_WORD(ctx, buf, feat->name, word, word_len); |
Radek Krejci | faa1eac | 2018-10-30 14:34:55 +0100 | [diff] [blame] | 3997 | |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 3998 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3999 | switch (kw) { |
| 4000 | case YANG_DESCRIPTION: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 4001 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &feat->dsc, Y_STR_ARG, &feat->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4002 | break; |
| 4003 | case YANG_IF_FEATURE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 4004 | LY_CHECK_RET(parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &feat->iffeatures, Y_STR_ARG, &feat->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4005 | break; |
| 4006 | case YANG_REFERENCE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 4007 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &feat->ref, Y_STR_ARG, &feat->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4008 | break; |
| 4009 | case YANG_STATUS: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 4010 | LY_CHECK_RET(parse_status(ctx, data, &feat->flags, &feat->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4011 | break; |
| 4012 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 4013 | LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &feat->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4014 | break; |
| 4015 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 4016 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "feature"); |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 4017 | return LY_EVALID; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4018 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4019 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4020 | return ret; |
| 4021 | } |
| 4022 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 4023 | /** |
| 4024 | * @brief Parse the identity statement. |
| 4025 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 4026 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 4027 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 4028 | * @param[in,out] identities Identities to add to. |
| 4029 | * |
| 4030 | * @return LY_ERR values. |
| 4031 | */ |
Radek Krejci | 2d7a47b | 2019-05-16 13:34:10 +0200 | [diff] [blame] | 4032 | LY_ERR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 4033 | parse_identity(struct lys_parser_ctx *ctx, const char **data, struct lysp_ident **identities) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4034 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 4035 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4036 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 4037 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4038 | enum yang_keyword kw; |
| 4039 | struct lysp_ident *ident; |
| 4040 | |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 4041 | LY_ARRAY_NEW_RET(ctx->ctx, *identities, ident, LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4042 | |
| 4043 | /* get value */ |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 4044 | LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len)); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 4045 | INSERT_WORD(ctx, buf, ident->name, word, word_len); |
Radek Krejci | faa1eac | 2018-10-30 14:34:55 +0100 | [diff] [blame] | 4046 | |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 4047 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4048 | switch (kw) { |
| 4049 | case YANG_DESCRIPTION: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 4050 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &ident->dsc, Y_STR_ARG, &ident->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4051 | break; |
| 4052 | case YANG_IF_FEATURE: |
Radek Krejci | ceaf212 | 2019-01-02 15:03:26 +0100 | [diff] [blame] | 4053 | YANG_CHECK_STMTVER2_RET(ctx, "if-feature", "identity"); |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 4054 | LY_CHECK_RET(parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &ident->iffeatures, Y_STR_ARG, &ident->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4055 | break; |
| 4056 | case YANG_REFERENCE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 4057 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &ident->ref, Y_STR_ARG, &ident->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4058 | break; |
| 4059 | case YANG_STATUS: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 4060 | LY_CHECK_RET(parse_status(ctx, data, &ident->flags, &ident->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4061 | break; |
| 4062 | case YANG_BASE: |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4063 | if (ident->bases && ctx->mod_version < 2) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 4064 | 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] | 4065 | return LY_EVALID; |
| 4066 | } |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 4067 | LY_CHECK_RET(parse_text_fields(ctx, data, LYEXT_SUBSTMT_BASE, &ident->bases, Y_PREF_IDENTIF_ARG, &ident->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4068 | break; |
| 4069 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 4070 | LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &ident->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4071 | break; |
| 4072 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 4073 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "identity"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4074 | return LY_EVALID; |
| 4075 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4076 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4077 | return ret; |
| 4078 | } |
| 4079 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 4080 | /** |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4081 | * @brief Parse module substatements. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 4082 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 4083 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 4084 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 4085 | * @param[in,out] mod Module to write to. |
| 4086 | * |
| 4087 | * @return LY_ERR values. |
| 4088 | */ |
Radek Krejci | 2d7a47b | 2019-05-16 13:34:10 +0200 | [diff] [blame] | 4089 | LY_ERR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 4090 | parse_module(struct lys_parser_ctx *ctx, const char **data, struct lysp_module *mod) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4091 | { |
| 4092 | LY_ERR ret = 0; |
| 4093 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 4094 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4095 | enum yang_keyword kw, prev_kw = 0; |
| 4096 | enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4097 | struct lysp_submodule *dup; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4098 | |
| 4099 | /* (sub)module name */ |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 4100 | LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len)); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4101 | INSERT_WORD(ctx, buf, mod->mod->name, word, word_len); |
Radek Krejci | faa1eac | 2018-10-30 14:34:55 +0100 | [diff] [blame] | 4102 | |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 4103 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4104 | |
Radek Krejci | e384647 | 2018-10-15 15:24:51 +0200 | [diff] [blame] | 4105 | #define CHECK_ORDER(SECTION) \ |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 4106 | 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] | 4107 | |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4108 | switch (kw) { |
| 4109 | /* module header */ |
| 4110 | case YANG_NAMESPACE: |
| 4111 | case YANG_PREFIX: |
Radek Krejci | e384647 | 2018-10-15 15:24:51 +0200 | [diff] [blame] | 4112 | CHECK_ORDER(Y_MOD_MODULE_HEADER); |
| 4113 | break; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4114 | case YANG_YANG_VERSION: |
Radek Krejci | e384647 | 2018-10-15 15:24:51 +0200 | [diff] [blame] | 4115 | CHECK_ORDER(Y_MOD_MODULE_HEADER); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4116 | break; |
| 4117 | /* linkage */ |
| 4118 | case YANG_INCLUDE: |
| 4119 | case YANG_IMPORT: |
Radek Krejci | e384647 | 2018-10-15 15:24:51 +0200 | [diff] [blame] | 4120 | CHECK_ORDER(Y_MOD_LINKAGE); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4121 | break; |
| 4122 | /* meta */ |
| 4123 | case YANG_ORGANIZATION: |
| 4124 | case YANG_CONTACT: |
| 4125 | case YANG_DESCRIPTION: |
| 4126 | case YANG_REFERENCE: |
Radek Krejci | e384647 | 2018-10-15 15:24:51 +0200 | [diff] [blame] | 4127 | CHECK_ORDER(Y_MOD_META); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4128 | break; |
| 4129 | |
| 4130 | /* revision */ |
| 4131 | case YANG_REVISION: |
Radek Krejci | e384647 | 2018-10-15 15:24:51 +0200 | [diff] [blame] | 4132 | CHECK_ORDER(Y_MOD_REVISION); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4133 | break; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4134 | /* body */ |
| 4135 | case YANG_ANYDATA: |
| 4136 | case YANG_ANYXML: |
| 4137 | case YANG_AUGMENT: |
| 4138 | case YANG_CHOICE: |
| 4139 | case YANG_CONTAINER: |
| 4140 | case YANG_DEVIATION: |
| 4141 | case YANG_EXTENSION: |
| 4142 | case YANG_FEATURE: |
| 4143 | case YANG_GROUPING: |
| 4144 | case YANG_IDENTITY: |
| 4145 | case YANG_LEAF: |
| 4146 | case YANG_LEAF_LIST: |
| 4147 | case YANG_LIST: |
| 4148 | case YANG_NOTIFICATION: |
| 4149 | case YANG_RPC: |
| 4150 | case YANG_TYPEDEF: |
| 4151 | case YANG_USES: |
| 4152 | case YANG_CUSTOM: |
| 4153 | mod_stmt = Y_MOD_BODY; |
| 4154 | break; |
| 4155 | default: |
| 4156 | /* error handled in the next switch */ |
| 4157 | break; |
| 4158 | } |
Radek Krejci | e384647 | 2018-10-15 15:24:51 +0200 | [diff] [blame] | 4159 | #undef CHECK_ORDER |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4160 | |
Radek Krejci | e384647 | 2018-10-15 15:24:51 +0200 | [diff] [blame] | 4161 | prev_kw = kw; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4162 | switch (kw) { |
| 4163 | /* module header */ |
| 4164 | case YANG_YANG_VERSION: |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4165 | LY_CHECK_RET(parse_yangversion(ctx, data, &mod->mod->version, &mod->exts)); |
| 4166 | ctx->mod_version = mod->mod->version; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4167 | break; |
| 4168 | case YANG_NAMESPACE: |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4169 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_NAMESPACE, 0, &mod->mod->ns, Y_STR_ARG, &mod->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4170 | break; |
| 4171 | case YANG_PREFIX: |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4172 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_PREFIX, 0, &mod->mod->prefix, Y_IDENTIF_ARG, &mod->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4173 | break; |
| 4174 | |
| 4175 | /* linkage */ |
| 4176 | case YANG_INCLUDE: |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4177 | LY_CHECK_RET(parse_include(ctx, mod->mod->name, data, &mod->includes)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4178 | break; |
| 4179 | case YANG_IMPORT: |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4180 | LY_CHECK_RET(parse_import(ctx, mod->mod->prefix, data, &mod->imports)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4181 | break; |
| 4182 | |
| 4183 | /* meta */ |
| 4184 | case YANG_ORGANIZATION: |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4185 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_ORGANIZATION, 0, &mod->mod->org, Y_STR_ARG, &mod->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4186 | break; |
| 4187 | case YANG_CONTACT: |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4188 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_CONTACT, 0, &mod->mod->contact, Y_STR_ARG, &mod->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4189 | break; |
| 4190 | case YANG_DESCRIPTION: |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4191 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &mod->mod->dsc, Y_STR_ARG, &mod->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4192 | break; |
| 4193 | case YANG_REFERENCE: |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4194 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &mod->mod->ref, Y_STR_ARG, &mod->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4195 | break; |
| 4196 | |
| 4197 | /* revision */ |
| 4198 | case YANG_REVISION: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 4199 | LY_CHECK_RET(parse_revision(ctx, data, &mod->revs)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4200 | break; |
| 4201 | |
| 4202 | /* body */ |
| 4203 | case YANG_ANYDATA: |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4204 | YANG_CHECK_STMTVER2_RET(ctx, "anydata", "module"); |
Radek Krejci | 1011365 | 2018-11-14 16:56:50 +0100 | [diff] [blame] | 4205 | /* fall through */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4206 | case YANG_ANYXML: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 4207 | LY_CHECK_RET(parse_any(ctx, data, kw, NULL, &mod->data)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4208 | break; |
| 4209 | case YANG_CHOICE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 4210 | LY_CHECK_RET(parse_choice(ctx, data, NULL, &mod->data)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4211 | break; |
| 4212 | case YANG_CONTAINER: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 4213 | LY_CHECK_RET(parse_container(ctx, data, NULL, &mod->data)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4214 | break; |
| 4215 | case YANG_LEAF: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 4216 | LY_CHECK_RET(parse_leaf(ctx, data, NULL, &mod->data)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4217 | break; |
| 4218 | case YANG_LEAF_LIST: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 4219 | LY_CHECK_RET(parse_leaflist(ctx, data, NULL, &mod->data)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4220 | break; |
| 4221 | case YANG_LIST: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 4222 | LY_CHECK_RET(parse_list(ctx, data, NULL, &mod->data)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4223 | break; |
| 4224 | case YANG_USES: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 4225 | LY_CHECK_RET(parse_uses(ctx, data, NULL, &mod->data)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4226 | break; |
| 4227 | |
| 4228 | case YANG_AUGMENT: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 4229 | LY_CHECK_RET(parse_augment(ctx, data, NULL, &mod->augments)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4230 | break; |
| 4231 | case YANG_DEVIATION: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 4232 | LY_CHECK_RET(parse_deviation(ctx, data, &mod->deviations)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4233 | break; |
| 4234 | case YANG_EXTENSION: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 4235 | LY_CHECK_RET(parse_extension(ctx, data, &mod->extensions)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4236 | break; |
| 4237 | case YANG_FEATURE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 4238 | LY_CHECK_RET(parse_feature(ctx, data, &mod->features)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4239 | break; |
| 4240 | case YANG_GROUPING: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 4241 | LY_CHECK_RET(parse_grouping(ctx, data, NULL, &mod->groupings)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4242 | break; |
| 4243 | case YANG_IDENTITY: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 4244 | LY_CHECK_RET(parse_identity(ctx, data, &mod->identities)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4245 | break; |
| 4246 | case YANG_NOTIFICATION: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 4247 | LY_CHECK_RET(parse_notif(ctx, data, NULL, &mod->notifs)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4248 | break; |
| 4249 | case YANG_RPC: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 4250 | LY_CHECK_RET(parse_action(ctx, data, NULL, &mod->rpcs)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4251 | break; |
| 4252 | case YANG_TYPEDEF: |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 4253 | LY_CHECK_RET(parse_typedef(ctx, NULL, data, &mod->typedefs)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4254 | break; |
| 4255 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 4256 | LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &mod->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4257 | break; |
| 4258 | |
| 4259 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 4260 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "module"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4261 | return LY_EVALID; |
| 4262 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4263 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 4264 | LY_CHECK_RET(ret); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4265 | |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 4266 | checks: |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4267 | /* finalize parent pointers to the reallocated items */ |
David Sedlák | d2ebe57 | 2019-07-22 12:53:14 +0200 | [diff] [blame] | 4268 | LY_CHECK_RET(lysp_parse_finalize_reallocated(ctx, mod->groupings, mod->augments, mod->rpcs, mod->notifs)); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4269 | |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4270 | /* mandatory substatements */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4271 | if (!mod->mod->ns) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 4272 | LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "namespace", "module"); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4273 | return LY_EVALID; |
| 4274 | } else if (!mod->mod->prefix) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 4275 | LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "module"); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4276 | return LY_EVALID; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4277 | } |
| 4278 | |
Radek Krejci | e9e987e | 2018-10-31 12:50:27 +0100 | [diff] [blame] | 4279 | /* submodules share the namespace with the module names, so there must not be |
| 4280 | * a submodule of the same name in the context, no need for revision matching */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4281 | dup = ly_ctx_get_submodule(ctx->ctx, NULL, mod->mod->name, NULL); |
| 4282 | if (dup) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 4283 | LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Name collision between module and submodule of name \"%s\".", mod->mod->name); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4284 | return LY_EVALID; |
| 4285 | } |
| 4286 | |
| 4287 | return ret; |
| 4288 | } |
| 4289 | |
| 4290 | /** |
| 4291 | * @brief Parse submodule substatements. |
| 4292 | * |
| 4293 | * @param[in] ctx yang parser context for logging. |
| 4294 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 4295 | * @param[out] submod Parsed submodule structure. |
| 4296 | * |
| 4297 | * @return LY_ERR values. |
| 4298 | */ |
Radek Krejci | 2d7a47b | 2019-05-16 13:34:10 +0200 | [diff] [blame] | 4299 | LY_ERR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 4300 | parse_submodule(struct lys_parser_ctx *ctx, const char **data, struct lysp_submodule *submod) |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4301 | { |
| 4302 | LY_ERR ret = 0; |
| 4303 | char *buf, *word; |
| 4304 | size_t word_len; |
| 4305 | enum yang_keyword kw, prev_kw = 0; |
| 4306 | enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER; |
| 4307 | struct lysp_submodule *dup; |
| 4308 | |
| 4309 | /* submodule name */ |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 4310 | LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len)); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4311 | INSERT_WORD(ctx, buf, submod->name, word, word_len); |
| 4312 | |
| 4313 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) { |
| 4314 | |
| 4315 | #define CHECK_ORDER(SECTION) \ |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 4316 | 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] | 4317 | |
| 4318 | switch (kw) { |
| 4319 | /* module header */ |
| 4320 | case YANG_BELONGS_TO: |
| 4321 | CHECK_ORDER(Y_MOD_MODULE_HEADER); |
| 4322 | break; |
| 4323 | case YANG_YANG_VERSION: |
| 4324 | CHECK_ORDER(Y_MOD_MODULE_HEADER); |
| 4325 | break; |
| 4326 | /* linkage */ |
| 4327 | case YANG_INCLUDE: |
| 4328 | case YANG_IMPORT: |
| 4329 | CHECK_ORDER(Y_MOD_LINKAGE); |
| 4330 | break; |
| 4331 | /* meta */ |
| 4332 | case YANG_ORGANIZATION: |
| 4333 | case YANG_CONTACT: |
| 4334 | case YANG_DESCRIPTION: |
| 4335 | case YANG_REFERENCE: |
| 4336 | CHECK_ORDER(Y_MOD_META); |
| 4337 | break; |
| 4338 | |
| 4339 | /* revision */ |
| 4340 | case YANG_REVISION: |
| 4341 | CHECK_ORDER(Y_MOD_REVISION); |
| 4342 | break; |
| 4343 | /* body */ |
| 4344 | case YANG_ANYDATA: |
| 4345 | case YANG_ANYXML: |
| 4346 | case YANG_AUGMENT: |
| 4347 | case YANG_CHOICE: |
| 4348 | case YANG_CONTAINER: |
| 4349 | case YANG_DEVIATION: |
| 4350 | case YANG_EXTENSION: |
| 4351 | case YANG_FEATURE: |
| 4352 | case YANG_GROUPING: |
| 4353 | case YANG_IDENTITY: |
| 4354 | case YANG_LEAF: |
| 4355 | case YANG_LEAF_LIST: |
| 4356 | case YANG_LIST: |
| 4357 | case YANG_NOTIFICATION: |
| 4358 | case YANG_RPC: |
| 4359 | case YANG_TYPEDEF: |
| 4360 | case YANG_USES: |
| 4361 | case YANG_CUSTOM: |
| 4362 | mod_stmt = Y_MOD_BODY; |
| 4363 | break; |
| 4364 | default: |
| 4365 | /* error handled in the next switch */ |
| 4366 | break; |
| 4367 | } |
| 4368 | #undef CHECK_ORDER |
| 4369 | |
| 4370 | prev_kw = kw; |
| 4371 | switch (kw) { |
| 4372 | /* module header */ |
| 4373 | case YANG_YANG_VERSION: |
| 4374 | LY_CHECK_RET(parse_yangversion(ctx, data, &submod->version, &submod->exts)); |
| 4375 | ctx->mod_version = submod->version; |
| 4376 | break; |
| 4377 | case YANG_BELONGS_TO: |
| 4378 | LY_CHECK_RET(parse_belongsto(ctx, data, &submod->belongsto, &submod->prefix, &submod->exts)); |
| 4379 | break; |
| 4380 | |
| 4381 | /* linkage */ |
| 4382 | case YANG_INCLUDE: |
| 4383 | LY_CHECK_RET(parse_include(ctx, submod->name, data, &submod->includes)); |
| 4384 | break; |
| 4385 | case YANG_IMPORT: |
| 4386 | LY_CHECK_RET(parse_import(ctx, submod->prefix, data, &submod->imports)); |
| 4387 | break; |
| 4388 | |
| 4389 | /* meta */ |
| 4390 | case YANG_ORGANIZATION: |
| 4391 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_ORGANIZATION, 0, &submod->org, Y_STR_ARG, &submod->exts)); |
| 4392 | break; |
| 4393 | case YANG_CONTACT: |
| 4394 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_CONTACT, 0, &submod->contact, Y_STR_ARG, &submod->exts)); |
| 4395 | break; |
| 4396 | case YANG_DESCRIPTION: |
| 4397 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &submod->dsc, Y_STR_ARG, &submod->exts)); |
| 4398 | break; |
| 4399 | case YANG_REFERENCE: |
| 4400 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &submod->ref, Y_STR_ARG, &submod->exts)); |
| 4401 | break; |
| 4402 | |
| 4403 | /* revision */ |
| 4404 | case YANG_REVISION: |
| 4405 | LY_CHECK_RET(parse_revision(ctx, data, &submod->revs)); |
| 4406 | break; |
| 4407 | |
| 4408 | /* body */ |
| 4409 | case YANG_ANYDATA: |
| 4410 | YANG_CHECK_STMTVER2_RET(ctx, "anydata", "submodule"); |
| 4411 | /* fall through */ |
| 4412 | case YANG_ANYXML: |
| 4413 | LY_CHECK_RET(parse_any(ctx, data, kw, NULL, &submod->data)); |
| 4414 | break; |
| 4415 | case YANG_CHOICE: |
| 4416 | LY_CHECK_RET(parse_choice(ctx, data, NULL, &submod->data)); |
| 4417 | break; |
| 4418 | case YANG_CONTAINER: |
| 4419 | LY_CHECK_RET(parse_container(ctx, data, NULL, &submod->data)); |
| 4420 | break; |
| 4421 | case YANG_LEAF: |
| 4422 | LY_CHECK_RET(parse_leaf(ctx, data, NULL, &submod->data)); |
| 4423 | break; |
| 4424 | case YANG_LEAF_LIST: |
| 4425 | LY_CHECK_RET(parse_leaflist(ctx, data, NULL, &submod->data)); |
| 4426 | break; |
| 4427 | case YANG_LIST: |
| 4428 | LY_CHECK_RET(parse_list(ctx, data, NULL, &submod->data)); |
| 4429 | break; |
| 4430 | case YANG_USES: |
| 4431 | LY_CHECK_RET(parse_uses(ctx, data, NULL, &submod->data)); |
| 4432 | break; |
| 4433 | |
| 4434 | case YANG_AUGMENT: |
| 4435 | LY_CHECK_RET(parse_augment(ctx, data, NULL, &submod->augments)); |
| 4436 | break; |
| 4437 | case YANG_DEVIATION: |
| 4438 | LY_CHECK_RET(parse_deviation(ctx, data, &submod->deviations)); |
| 4439 | break; |
| 4440 | case YANG_EXTENSION: |
| 4441 | LY_CHECK_RET(parse_extension(ctx, data, &submod->extensions)); |
| 4442 | break; |
| 4443 | case YANG_FEATURE: |
| 4444 | LY_CHECK_RET(parse_feature(ctx, data, &submod->features)); |
| 4445 | break; |
| 4446 | case YANG_GROUPING: |
| 4447 | LY_CHECK_RET(parse_grouping(ctx, data, NULL, &submod->groupings)); |
| 4448 | break; |
| 4449 | case YANG_IDENTITY: |
| 4450 | LY_CHECK_RET(parse_identity(ctx, data, &submod->identities)); |
| 4451 | break; |
| 4452 | case YANG_NOTIFICATION: |
| 4453 | LY_CHECK_RET(parse_notif(ctx, data, NULL, &submod->notifs)); |
| 4454 | break; |
| 4455 | case YANG_RPC: |
| 4456 | LY_CHECK_RET(parse_action(ctx, data, NULL, &submod->rpcs)); |
| 4457 | break; |
| 4458 | case YANG_TYPEDEF: |
| 4459 | LY_CHECK_RET(parse_typedef(ctx, NULL, data, &submod->typedefs)); |
| 4460 | break; |
| 4461 | case YANG_CUSTOM: |
| 4462 | LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &submod->exts)); |
| 4463 | break; |
| 4464 | |
| 4465 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 4466 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "submodule"); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4467 | return LY_EVALID; |
| 4468 | } |
| 4469 | } |
| 4470 | LY_CHECK_RET(ret); |
| 4471 | |
| 4472 | checks: |
| 4473 | /* finalize parent pointers to the reallocated items */ |
David Sedlák | d2ebe57 | 2019-07-22 12:53:14 +0200 | [diff] [blame] | 4474 | LY_CHECK_RET(lysp_parse_finalize_reallocated(ctx, submod->groupings, submod->augments, submod->rpcs, submod->notifs)); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4475 | |
| 4476 | /* mandatory substatements */ |
| 4477 | if (!submod->belongsto) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 4478 | LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "belongs-to", "submodule"); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4479 | return LY_EVALID; |
| 4480 | } |
| 4481 | |
| 4482 | /* submodules share the namespace with the module names, so there must not be |
| 4483 | * a submodule of the same name in the context, no need for revision matching */ |
| 4484 | dup = ly_ctx_get_submodule(ctx->ctx, NULL, submod->name, NULL); |
| 4485 | if (dup && strcmp(dup->belongsto, submod->belongsto)) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 4486 | LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Name collision between submodules of name \"%s\".", dup->name); |
Radek Krejci | e9e987e | 2018-10-31 12:50:27 +0100 | [diff] [blame] | 4487 | return LY_EVALID; |
| 4488 | } |
| 4489 | |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4490 | return ret; |
| 4491 | } |
| 4492 | |
Radek Krejci | d4557c6 | 2018-09-17 11:42:09 +0200 | [diff] [blame] | 4493 | LY_ERR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 4494 | yang_parse_submodule(struct lys_parser_ctx *context, const char *data, struct lysp_submodule **submod) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4495 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 4496 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 0a1d0d4 | 2019-05-16 15:14:51 +0200 | [diff] [blame] | 4497 | char *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 4498 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4499 | enum yang_keyword kw; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4500 | struct lysp_submodule *mod_p = NULL; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4501 | |
| 4502 | /* "module"/"submodule" */ |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 4503 | ret = get_keyword(context, &data, &kw, &word, &word_len); |
| 4504 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4505 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4506 | if (kw == YANG_MODULE) { |
| 4507 | LOGERR(context->ctx, LY_EDENIED, "Input data contains module in situation when a submodule is expected."); |
| 4508 | ret = LY_EINVAL; |
| 4509 | goto cleanup; |
| 4510 | } else if (kw != YANG_SUBMODULE) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 4511 | LOGVAL_PARSER(context, LYVE_SYNTAX, "Invalid keyword \"%s\", expected \"module\" or \"submodule\".", |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 4512 | ly_stmt2str(kw)); |
Radek Krejci | 40544fa | 2019-01-11 09:38:37 +0100 | [diff] [blame] | 4513 | ret = LY_EVALID; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 4514 | goto cleanup; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4515 | } |
| 4516 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4517 | mod_p = calloc(1, sizeof *mod_p); |
| 4518 | LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(context->ctx), cleanup); |
| 4519 | mod_p->parsing = 1; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4520 | |
| 4521 | /* substatements */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4522 | ret = parse_submodule(context, &data, mod_p); |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 4523 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4524 | |
| 4525 | /* read some trailing spaces or new lines */ |
Radek Krejci | 0a1d0d4 | 2019-05-16 15:14:51 +0200 | [diff] [blame] | 4526 | while(*data && isspace(*data)) { |
| 4527 | data++; |
| 4528 | } |
| 4529 | if (*data) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 4530 | LOGVAL_PARSER(context, LYVE_SYNTAX, "Trailing garbage \"%.*s%s\" after submodule, expected end-of-input.", |
Radek Krejci | 0a1d0d4 | 2019-05-16 15:14:51 +0200 | [diff] [blame] | 4531 | 15, data, strlen(data) > 15 ? "..." : ""); |
Radek Krejci | 40544fa | 2019-01-11 09:38:37 +0100 | [diff] [blame] | 4532 | ret = LY_EVALID; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 4533 | goto cleanup; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4534 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4535 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4536 | mod_p->parsing = 0; |
| 4537 | *submod = mod_p; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4538 | |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 4539 | cleanup: |
| 4540 | if (ret) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4541 | lysp_submodule_free(context->ctx, mod_p); |
| 4542 | } |
| 4543 | |
| 4544 | return ret; |
| 4545 | } |
| 4546 | |
| 4547 | LY_ERR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 4548 | yang_parse_module(struct lys_parser_ctx *context, const char *data, struct lys_module *mod) |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4549 | { |
| 4550 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 0a1d0d4 | 2019-05-16 15:14:51 +0200 | [diff] [blame] | 4551 | char *word; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4552 | size_t word_len; |
| 4553 | enum yang_keyword kw; |
| 4554 | struct lysp_module *mod_p = NULL; |
| 4555 | |
| 4556 | /* "module"/"submodule" */ |
| 4557 | ret = get_keyword(context, &data, &kw, &word, &word_len); |
| 4558 | LY_CHECK_GOTO(ret, cleanup); |
| 4559 | |
| 4560 | if (kw == YANG_SUBMODULE) { |
| 4561 | LOGERR(context->ctx, LY_EDENIED, "Input data contains submodule which cannot be parsed directly without its main module."); |
| 4562 | ret = LY_EINVAL; |
| 4563 | goto cleanup; |
| 4564 | } else if (kw != YANG_MODULE) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 4565 | LOGVAL_PARSER(context, LYVE_SYNTAX, "Invalid keyword \"%s\", expected \"module\" or \"submodule\".", |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4566 | ly_stmt2str(kw)); |
Radek Krejci | 40544fa | 2019-01-11 09:38:37 +0100 | [diff] [blame] | 4567 | ret = LY_EVALID; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4568 | goto cleanup; |
| 4569 | } |
| 4570 | |
| 4571 | mod_p = calloc(1, sizeof *mod_p); |
| 4572 | LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(context->ctx), cleanup); |
| 4573 | mod_p->mod = mod; |
| 4574 | mod_p->parsing = 1; |
| 4575 | |
| 4576 | /* substatements */ |
| 4577 | ret = parse_module(context, &data, mod_p); |
| 4578 | LY_CHECK_GOTO(ret, cleanup); |
| 4579 | |
| 4580 | /* read some trailing spaces or new lines */ |
Radek Krejci | 0a1d0d4 | 2019-05-16 15:14:51 +0200 | [diff] [blame] | 4581 | while(*data && isspace(*data)) { |
| 4582 | data++; |
| 4583 | } |
| 4584 | if (*data) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 4585 | LOGVAL_PARSER(context, LYVE_SYNTAX, "Trailing garbage \"%.*s%s\" after module, expected end-of-input.", |
Radek Krejci | 0a1d0d4 | 2019-05-16 15:14:51 +0200 | [diff] [blame] | 4586 | 15, data, strlen(data) > 15 ? "..." : ""); |
Radek Krejci | 40544fa | 2019-01-11 09:38:37 +0100 | [diff] [blame] | 4587 | ret = LY_EVALID; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4588 | goto cleanup; |
| 4589 | } |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4590 | |
| 4591 | mod_p->parsing = 0; |
| 4592 | mod->parsed = mod_p; |
| 4593 | |
| 4594 | cleanup: |
| 4595 | if (ret) { |
| 4596 | lysp_module_free(mod_p); |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 4597 | } |
| 4598 | |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4599 | return ret; |
| 4600 | } |