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 | */ |
| 14 | #include <sys/types.h> |
| 15 | #include <sys/stat.h> |
| 16 | #include <unistd.h> |
| 17 | #include <fcntl.h> |
| 18 | #include <stdio.h> |
| 19 | #include <stdlib.h> |
| 20 | #include <stdint.h> |
| 21 | #include <errno.h> |
| 22 | #include <ctype.h> |
| 23 | #include <string.h> |
| 24 | #include <dirent.h> |
| 25 | #include <assert.h> |
| 26 | |
| 27 | #include "common.h" |
| 28 | #include "context.h" |
| 29 | #include "libyang.h" |
Radek Krejci | 70853c5 | 2018-10-15 14:46:16 +0200 | [diff] [blame] | 30 | #include "tree_schema_internal.h" |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 31 | |
| 32 | /* Macro to check YANG's yang-char grammar rule */ |
| 33 | #define is_yangutf8char(c) ((c >= 0x20 && c <= 0xd77) || c == 0x09 || c == 0x0a || c == 0x0d || \ |
| 34 | (c >= 0xe000 && c <= 0xfdcf) || (c >= 0xfdf0 && c <= 0xfffd) || \ |
| 35 | (c >= 0x10000 && c <= 0x1fffd) || (c >= 0x20000 && c <= 0x2fffd) || \ |
| 36 | (c >= 0x30000 && c <= 0x3fffd) || (c >= 0x40000 && c <= 0x2fffd) || \ |
| 37 | (c >= 0x50000 && c <= 0x5fffd) || (c >= 0x60000 && c <= 0x6fffd) || \ |
| 38 | (c >= 0x70000 && c <= 0x7fffd) || (c >= 0x80000 && c <= 0x8fffd) || \ |
| 39 | (c >= 0x90000 && c <= 0x9fffd) || (c >= 0xa0000 && c <= 0xafffd) || \ |
| 40 | (c >= 0xb0000 && c <= 0xbfffd) || (c >= 0xc0000 && c <= 0xcfffd) || \ |
| 41 | (c >= 0xd0000 && c <= 0xdfffd) || (c >= 0xe0000 && c <= 0xefffd) || \ |
| 42 | (c >= 0xf0000 && c <= 0xffffd) || (c >= 0x100000 && c <= 0x10fffd)) |
| 43 | |
| 44 | /* These 2 macros checks YANG's identifier grammar rule */ |
| 45 | #define is_yangidentstartchar(c) ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_') |
| 46 | #define is_yangidentchar(c) ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || \ |
| 47 | c == '_' || c == '-' || c == '.') |
| 48 | |
Radek Krejci | 9fcacc1 | 2018-10-11 15:59:11 +0200 | [diff] [blame] | 49 | #define INSERT_WORD(CTX, BUF, TARGET, WORD, LEN) \ |
| 50 | if (BUF) {(TARGET) = lydict_insert_zc((CTX)->ctx, WORD);}\ |
| 51 | else {(TARGET) = lydict_insert((CTX)->ctx, WORD, LEN);} |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 52 | |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 53 | #define MOVE_INPUT(CTX, DATA, COUNT) (*(data))+=COUNT;(CTX)->indent+=COUNT |
| 54 | |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 55 | #define LOGVAL_YANG(CTX, ...) LOGVAL((CTX)->ctx, LY_VLOG_LINE, &(CTX)->line, __VA_ARGS__) |
| 56 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 57 | /** |
| 58 | * @brief Loop through all substatements providing, return if there are none. |
| 59 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 60 | * @param[in] CTX yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 61 | * @param[in] DATA Raw data to read from. |
| 62 | * @param[out] KW YANG keyword read. |
| 63 | * @param[out] WORD Pointer to the keyword itself. |
| 64 | * @param[out] WORD_LEN Length of the keyword. |
| 65 | * @param[out] ERR Variable for error storing. |
| 66 | * |
| 67 | * @return In case there are no substatements or a fatal error encountered. |
| 68 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 69 | #define YANG_READ_SUBSTMT_FOR(CTX, DATA, KW, WORD, WORD_LEN, ERR) \ |
| 70 | ERR = get_keyword(CTX, DATA, &KW, &WORD, &WORD_LEN); \ |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 71 | LY_CHECK_RET(ERR); \ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 72 | \ |
| 73 | if (KW == YANG_SEMICOLON) { \ |
| 74 | return ERR; \ |
| 75 | } \ |
| 76 | if (KW != YANG_LEFT_BRACE) { \ |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 77 | LOGVAL_YANG(CTX, LYVE_SYNTAX_YANG, "Invalid keyword \"%s\", expected \";\" or \"{\".", ly_stmt2str(KW)); \ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 78 | return LY_EVALID; \ |
| 79 | } \ |
| 80 | for (ERR = get_keyword(CTX, DATA, &KW, &WORD, &WORD_LEN); \ |
| 81 | !ERR && (KW != YANG_RIGHT_BRACE); \ |
| 82 | ERR = get_keyword(CTX, DATA, &KW, &WORD, &WORD_LEN)) |
| 83 | |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 84 | static LY_ERR parse_container(struct ly_parser_ctx *ctx, const char **data, struct lysp_node **siblings); |
| 85 | static LY_ERR parse_uses(struct ly_parser_ctx *ctx, const char **data, struct lysp_node **siblings); |
| 86 | static LY_ERR parse_choice(struct ly_parser_ctx *ctx, const char **data, struct lysp_node **siblings); |
| 87 | static LY_ERR parse_case(struct ly_parser_ctx *ctx, const char **data, struct lysp_node **siblings); |
| 88 | static LY_ERR parse_list(struct ly_parser_ctx *ctx, const char **data, struct lysp_node **siblings); |
| 89 | static LY_ERR parse_grouping(struct ly_parser_ctx *ctx, const char **data, struct lysp_grp **groupings); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 90 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 91 | /** |
| 92 | * @brief Add another character to dynamic buffer, a low-level function. |
| 93 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 94 | * Enlarge if needed. Updates \p input as well as \p buf_used. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 95 | * |
Radek Krejci | 404251e | 2018-10-09 12:06:44 +0200 | [diff] [blame] | 96 | * @param[in] ctx libyang context for logging. |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 97 | * @param[in, out] input Input string to process. |
| 98 | * @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] | 99 | * @param[in,out] buf Buffer to use, can be moved by realloc(). |
| 100 | * @param[in,out] buf_len Current size of the buffer. |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 101 | * @param[in,out] buf_used Currently used characters of the buffer. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 102 | * |
| 103 | * @return LY_ERR values. |
| 104 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 105 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 106 | 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] | 107 | { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 108 | if (*buf_len <= (*buf_used) + len) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 109 | *buf_len += 16; |
| 110 | *buf = ly_realloc(*buf, *buf_len); |
| 111 | LY_CHECK_ERR_RET(!*buf, LOGMEM(ctx), LY_EMEM); |
| 112 | } |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 113 | memcpy(&(*buf)[*buf_used], *input, len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 114 | |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 115 | (*buf_used) += len; |
| 116 | (*input) += len; |
| 117 | |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 118 | return LY_SUCCESS; |
| 119 | } |
| 120 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 121 | /** |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 122 | * @brief Check that \p c is valid UTF8 code point for YANG string. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 123 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 124 | * @param[in] ctx yang parser context for logging. |
| 125 | * @param[in] c UTF8 code point of a character to check. |
| 126 | * @return LY_ERR values. |
| 127 | */ |
| 128 | static LY_ERR |
| 129 | check_stringchar(struct ly_parser_ctx *ctx, unsigned int c) |
| 130 | { |
| 131 | if (!is_yangutf8char(c)) { |
| 132 | LOGVAL_YANG(ctx, LY_VCODE_INCHAR, c); |
| 133 | return LY_EVALID; |
| 134 | } |
| 135 | return LY_SUCCESS; |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * @brief Check that \p c is valid UTF8 code point for YANG identifier. |
| 140 | * |
| 141 | * @param[in] ctx yang parser context for logging. |
| 142 | * @param[in] c UTF8 code point of a character to check. |
| 143 | * @param[in] first Flag to check the first character of an identifier, which is more restricted. |
Radek Krejci | dcc7b32 | 2018-10-11 14:24:02 +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 |
| 148 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 149 | * If the identifier cannot be prefixed, NULL is expected. |
| 150 | * @return LY_ERR values. |
| 151 | */ |
| 152 | static LY_ERR |
| 153 | check_identifierchar(struct ly_parser_ctx *ctx, unsigned int c, int first, int *prefix) |
| 154 | { |
| 155 | if (first || (prefix && (*prefix) == 1)) { |
| 156 | if (!is_yangidentstartchar(c)) { |
| 157 | LOGVAL_YANG(ctx, LYVE_SYNTAX_YANG, "Invalid identifier first character '%c'.", c); |
| 158 | return LY_EVALID; |
| 159 | } |
Radek Krejci | 9fcacc1 | 2018-10-11 15:59:11 +0200 | [diff] [blame] | 160 | if (prefix) { |
| 161 | if (first) { |
| 162 | (*prefix) = 0; |
| 163 | } else { |
| 164 | (*prefix) = 2; |
| 165 | } |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 166 | } |
| 167 | } else if (c == ':' && prefix && (*prefix) == 0) { |
| 168 | (*prefix) = 1; |
| 169 | } else if (!is_yangidentchar(c)) { |
| 170 | LOGVAL_YANG(ctx, LYVE_SYNTAX_YANG, "Invalid identifier character '%c'.", c); |
| 171 | return LY_EVALID; |
| 172 | } |
| 173 | |
| 174 | return LY_SUCCESS; |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * @brief Store a single UTF8 character. It depends whether in a dynamically-allocated buffer or just as a pointer to the data. |
| 179 | * |
| 180 | * @param[in] ctx yang parser context for logging. |
| 181 | * @param[in,out] input Pointer to the string where to get the character to store. Automatically moved to the next character |
| 182 | * when function returns. |
| 183 | * @param[in] arg Type of the input string to select method of checking character validity. |
| 184 | * @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] | 185 | * 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] | 186 | * @param[in,out] word_len Current length of the word pointed to by \p word_p. |
| 187 | * @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] | 188 | * @param[in,out] buf_len Current length of \p word_b. |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 189 | * @param[in] need_buf Flag if the dynamically allocated buffer is required. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 190 | * |
| 191 | * @return LY_ERR values. |
| 192 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 193 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 194 | buf_store_char(struct ly_parser_ctx *ctx, const char **input, enum yang_arg arg, |
| 195 | char **word_p, size_t *word_len, char **word_b, size_t *buf_len, int need_buf) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 196 | { |
Radek Krejci | 404251e | 2018-10-09 12:06:44 +0200 | [diff] [blame] | 197 | int prefix = 0; |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 198 | unsigned int c; |
| 199 | size_t len; |
| 200 | |
| 201 | /* get UTF8 code point (and number of bytes coding the character) */ |
| 202 | LY_CHECK_ERR_RET(ly_getutf8(input, &c, &len), |
| 203 | LOGVAL_YANG(ctx, LY_VCODE_INCHAR, (*input)[-len]), LY_EVALID); |
| 204 | (*input) -= len; |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 205 | if (c == '\n') { |
| 206 | ctx->indent = 0; |
| 207 | } else { |
| 208 | /* note - even the multibyte character is count as 1 */ |
| 209 | ++ctx->indent; |
| 210 | } |
| 211 | |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 212 | /* check character validity */ |
| 213 | switch (arg) { |
| 214 | case Y_IDENTIF_ARG: |
| 215 | LY_CHECK_RET(check_identifierchar(ctx, c, !(*word_len), NULL)); |
| 216 | break; |
| 217 | case Y_PREF_IDENTIF_ARG: |
| 218 | LY_CHECK_RET(check_identifierchar(ctx, c, !(*word_len), &prefix)); |
| 219 | break; |
| 220 | case Y_STR_ARG: |
| 221 | case Y_MAYBE_STR_ARG: |
| 222 | LY_CHECK_RET(check_stringchar(ctx, c)); |
| 223 | break; |
| 224 | } |
| 225 | |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 226 | if (word_b && *word_b) { |
| 227 | /* add another character into buffer */ |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 228 | 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] | 229 | return LY_EMEM; |
| 230 | } |
| 231 | |
| 232 | /* in case of realloc */ |
| 233 | *word_p = *word_b; |
| 234 | } else if (need_buf) { |
| 235 | /* first time we need a buffer, copy everything read up to now */ |
| 236 | if (*word_len) { |
| 237 | *word_b = malloc(*word_len); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 238 | LY_CHECK_ERR_RET(!*word_b, LOGMEM(ctx->ctx), LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 239 | *buf_len = *word_len; |
| 240 | memcpy(*word_b, *word_p, *word_len); |
| 241 | } |
| 242 | |
| 243 | /* add this new character into buffer */ |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 244 | 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] | 245 | return LY_EMEM; |
| 246 | } |
| 247 | |
| 248 | /* in case of realloc */ |
| 249 | *word_p = *word_b; |
| 250 | } else { |
| 251 | /* just remember the first character pointer */ |
| 252 | if (!*word_p) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 253 | *word_p = (char *)(*input); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 254 | } |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 255 | /* ... and update the word's length */ |
| 256 | (*word_len) += len; |
| 257 | (*input) += len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | return LY_SUCCESS; |
| 261 | } |
| 262 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 263 | /** |
| 264 | * @brief Skip YANG comment in data. |
| 265 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 266 | * @param[in] ctx yang parser context for logging. |
| 267 | * @param[in,out] data Data to read from, automatically moved after the comment. |
| 268 | * @param[in] comment Type of the comment to process: |
| 269 | * 1 for a one-line comment, |
| 270 | * 2 for a block comment. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 271 | * |
| 272 | * @return LY_ERR values. |
| 273 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 274 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 275 | skip_comment(struct ly_parser_ctx *ctx, const char **data, int comment) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 276 | { |
Radek Krejci | 80dd33e | 2018-09-26 15:57:18 +0200 | [diff] [blame] | 277 | /* internal statuses: 0 - comment ended, |
| 278 | * 1 - in line comment, |
| 279 | * 2 - in block comment, |
| 280 | * 3 - in block comment with last read character '*' |
| 281 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 282 | while (**data && comment) { |
| 283 | switch (comment) { |
| 284 | case 1: |
| 285 | if (**data == '\n') { |
| 286 | comment = 0; |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 287 | ++ctx->line; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 288 | } |
| 289 | break; |
| 290 | case 2: |
| 291 | if (**data == '*') { |
Radek Krejci | 15c80ca | 2018-10-09 11:01:31 +0200 | [diff] [blame] | 292 | comment = 3; |
| 293 | } else if (**data == '\n') { |
| 294 | ++ctx->line; |
| 295 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 296 | break; |
| 297 | case 3: |
| 298 | if (**data == '/') { |
| 299 | comment = 0; |
| 300 | } else { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 301 | if (**data == '\n') { |
| 302 | ++ctx->line; |
| 303 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 304 | comment = 2; |
| 305 | } |
| 306 | break; |
| 307 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 308 | LOGINT_RET(ctx->ctx); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 309 | } |
| 310 | |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 311 | if (**data == '\n') { |
| 312 | ctx->indent = 0; |
| 313 | } else { |
| 314 | ++ctx->indent; |
| 315 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 316 | ++(*data); |
| 317 | } |
| 318 | |
| 319 | if (!**data && (comment > 1)) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 320 | LOGVAL_YANG(ctx, LYVE_SYNTAX, "Unexpected end-of-file, non-terminated comment."); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 321 | return LY_EVALID; |
| 322 | } |
| 323 | |
| 324 | return LY_SUCCESS; |
| 325 | } |
| 326 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 327 | /** |
| 328 | * @brief Read a quoted string from data. |
| 329 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 330 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 331 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 332 | * @param[in] arg Type of YANG keyword argument expected. |
| 333 | * @param[out] word_p Pointer to the read quoted string. |
| 334 | * @param[out] word_b Pointer to a dynamically-allocated buffer holding the read quoted string. If not needed, |
| 335 | * set to NULL. Otherwise equal to \p word_p. |
| 336 | * @param[out] word_len Length of the read quoted string. |
| 337 | * @param[out] buf_len Length of the dynamically-allocated buffer \p word_b. |
| 338 | * @param[in] indent Current indent (number of YANG spaces). Needed for correct multi-line string |
| 339 | * indenation in the final quoted string. |
| 340 | * |
| 341 | * @return LY_ERR values. |
| 342 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 343 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 344 | read_qstring(struct ly_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] | 345 | size_t *buf_len) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 346 | { |
| 347 | /* string: 0 - string ended, 1 - string with ', 2 - string with ", 3 - string with " with last character \, |
| 348 | * 4 - string finished, now skipping whitespaces looking for +, |
| 349 | * 5 - string continues after +, skipping whitespaces */ |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 350 | unsigned int string, block_indent = 0, current_indent = 0, need_buf = 0; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 351 | const char *c; |
| 352 | |
| 353 | if (**data == '\"') { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 354 | string = 2; |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 355 | current_indent = block_indent = ctx->indent + 1; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 356 | } else { |
| 357 | assert(**data == '\''); |
| 358 | string = 1; |
| 359 | } |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 360 | MOVE_INPUT(ctx, data, 1); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 361 | |
| 362 | while (**data && string) { |
| 363 | switch (string) { |
| 364 | case 1: |
| 365 | switch (**data) { |
| 366 | case '\'': |
| 367 | /* string may be finished, but check for + */ |
| 368 | string = 4; |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 369 | MOVE_INPUT(ctx, data, 1); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 370 | break; |
| 371 | default: |
| 372 | /* check and store character */ |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 373 | LY_CHECK_RET(buf_store_char(ctx, data, arg, word_p, word_len, word_b, buf_len, need_buf)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 374 | break; |
| 375 | } |
| 376 | break; |
| 377 | case 2: |
| 378 | switch (**data) { |
| 379 | case '\"': |
| 380 | /* string may be finished, but check for + */ |
| 381 | string = 4; |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 382 | MOVE_INPUT(ctx, data, 1); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 383 | break; |
| 384 | case '\\': |
| 385 | /* special character following */ |
| 386 | string = 3; |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 387 | ++(*data); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 388 | break; |
| 389 | case ' ': |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 390 | if (current_indent < block_indent) { |
| 391 | ++current_indent; |
| 392 | MOVE_INPUT(ctx, data, 1); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 393 | } else { |
| 394 | /* check and store character */ |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 395 | LY_CHECK_RET(buf_store_char(ctx, data, arg, word_p, word_len, word_b, buf_len, need_buf)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 396 | } |
| 397 | break; |
| 398 | case '\t': |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 399 | if (current_indent < block_indent) { |
| 400 | assert(need_buf); |
| 401 | current_indent += 8; |
| 402 | ctx->indent += 8; |
| 403 | for (; current_indent > block_indent; --current_indent, --ctx->indent) { |
| 404 | /* store leftover spaces from the tab */ |
| 405 | c = " "; |
| 406 | LY_CHECK_RET(buf_store_char(ctx, &c, arg, word_p, word_len, word_b, buf_len, need_buf)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 407 | } |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 408 | ++(*data); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 409 | } else { |
| 410 | /* check and store character */ |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 411 | LY_CHECK_RET(buf_store_char(ctx, data, arg, word_p, word_len, word_b, buf_len, need_buf)); |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 412 | /* additional characters for indentation - only 1 was count in buf_store_char */ |
| 413 | ctx->indent += 7; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 414 | } |
| 415 | break; |
| 416 | case '\n': |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 417 | if (block_indent) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 418 | /* we will be removing the indents so we need our own buffer */ |
| 419 | need_buf = 1; |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 420 | |
| 421 | /* remove trailing tabs and spaces */ |
| 422 | while ((*word_len) && ((*word_p)[(*word_len) - 1] == '\t' || (*word_p)[(*word_len) - 1] == ' ')) { |
| 423 | --(*word_len); |
| 424 | } |
| 425 | |
| 426 | /* start indentation */ |
| 427 | current_indent = 0; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | /* check and store character */ |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 431 | LY_CHECK_RET(buf_store_char(ctx, data, arg, word_p, word_len, word_b, buf_len, need_buf)); |
| 432 | |
| 433 | /* maintain line number */ |
| 434 | ++ctx->line; |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 435 | |
| 436 | /* reset context indentation counter for possible string after this one */ |
| 437 | ctx->indent = 0; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 438 | break; |
| 439 | default: |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 440 | /* first non-whitespace character, stop eating indentation */ |
| 441 | current_indent = block_indent; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 442 | |
| 443 | /* check and store character */ |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 444 | LY_CHECK_RET(buf_store_char(ctx, data, arg, word_p, word_len, word_b, buf_len, need_buf)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 445 | break; |
| 446 | } |
| 447 | break; |
| 448 | case 3: |
| 449 | /* string encoded characters */ |
| 450 | switch (**data) { |
| 451 | case 'n': |
| 452 | c = "\n"; |
| 453 | break; |
| 454 | case 't': |
| 455 | c = "\t"; |
| 456 | break; |
| 457 | case '\"': |
| 458 | c = *data; |
| 459 | break; |
| 460 | case '\\': |
| 461 | c = *data; |
| 462 | break; |
| 463 | default: |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 464 | LOGVAL_YANG(ctx, LYVE_SYNTAX_YANG, "Double-quoted string unknown special character '\\%c'.", **data); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 465 | return LY_EVALID; |
| 466 | } |
| 467 | |
| 468 | /* check and store character */ |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 469 | LY_CHECK_RET(buf_store_char(ctx, &c, arg, word_p, word_len, word_b, buf_len, need_buf)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 470 | |
| 471 | string = 2; |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 472 | ++(*data); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 473 | break; |
| 474 | case 4: |
| 475 | switch (**data) { |
| 476 | case '+': |
| 477 | /* string continues */ |
| 478 | string = 5; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 479 | need_buf = 1; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 480 | break; |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 481 | case '\n': |
| 482 | ++ctx->line; |
| 483 | /* fallthrough */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 484 | case ' ': |
| 485 | case '\t': |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 486 | /* just skip */ |
| 487 | break; |
| 488 | default: |
| 489 | /* string is finished */ |
| 490 | goto string_end; |
| 491 | } |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 492 | MOVE_INPUT(ctx, data, 1); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 493 | break; |
| 494 | case 5: |
| 495 | switch (**data) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 496 | case '\n': |
| 497 | ++ctx->line; |
| 498 | /* fallthrough */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 499 | case ' ': |
| 500 | case '\t': |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 501 | /* skip */ |
| 502 | break; |
| 503 | case '\'': |
| 504 | string = 1; |
| 505 | break; |
| 506 | case '\"': |
| 507 | string = 2; |
| 508 | break; |
| 509 | default: |
| 510 | /* it must be quoted again */ |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 511 | LOGVAL_YANG(ctx, LYVE_SYNTAX_YANG, "Both string parts divided by '+' must be quoted."); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 512 | return LY_EVALID; |
| 513 | } |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 514 | MOVE_INPUT(ctx, data, 1); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 515 | break; |
| 516 | default: |
| 517 | return LY_EINT; |
| 518 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | string_end: |
| 522 | return LY_SUCCESS; |
| 523 | } |
| 524 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 525 | /** |
| 526 | * @brief Get another YANG string from the raw data. |
| 527 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 528 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 529 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 530 | * @param[in] arg Type of YANG keyword argument expected. |
Michal Vasko | 2ca70f5 | 2018-09-27 11:04:51 +0200 | [diff] [blame] | 531 | * @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] | 532 | * @param[out] word_b Pointer to a dynamically-allocated buffer holding the read string. If not needed, |
| 533 | * set to NULL. Otherwise equal to \p word_p. |
| 534 | * @param[out] word_len Length of the read string. |
| 535 | * |
| 536 | * @return LY_ERR values. |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 537 | */ |
| 538 | static LY_ERR |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 539 | get_argument(struct ly_parser_ctx *ctx, const char **data, enum yang_arg arg, char **word_p, char **word_b, size_t *word_len) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 540 | { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 541 | size_t buf_len = 0; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 542 | LY_ERR ret; |
| 543 | |
| 544 | /* word buffer - dynamically allocated */ |
| 545 | *word_b = NULL; |
| 546 | |
| 547 | /* word pointer - just a pointer to data */ |
| 548 | *word_p = NULL; |
| 549 | |
| 550 | *word_len = 0; |
| 551 | while (**data) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 552 | switch (**data) { |
| 553 | case '\'': |
| 554 | case '\"': |
| 555 | if (*word_len) { |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 556 | /* invalid - quotes cannot be in unquoted string and only optsep, ; or { can follow it */ |
| 557 | LOGVAL_YANG(ctx, LY_VCODE_INSTREXP, 1, *data, |
| 558 | "unquoted string character, optsep, semicolon or opening brace"); |
| 559 | return LY_EVALID; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 560 | } |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 561 | ret = read_qstring(ctx, data, arg, word_p, word_b, word_len, &buf_len); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 562 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 563 | goto str_end; |
| 564 | case '/': |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 565 | if ((*data)[1] == '/') { |
| 566 | /* one-line comment */ |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 567 | MOVE_INPUT(ctx, data, 2); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 568 | ret = skip_comment(ctx, data, 1); |
| 569 | } else if ((*data)[1] == '*') { |
| 570 | /* block comment */ |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 571 | MOVE_INPUT(ctx, data, 2); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 572 | ret = skip_comment(ctx, data, 2); |
| 573 | } else { |
| 574 | /* not a comment after all */ |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 575 | ret = buf_store_char(ctx, data, arg, word_p, word_len, word_b, &buf_len, 0); |
| 576 | } |
| 577 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 578 | break; |
| 579 | case ' ': |
| 580 | if (*word_len) { |
| 581 | /* word is finished */ |
| 582 | goto str_end; |
| 583 | } |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 584 | MOVE_INPUT(ctx, data, 1); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 585 | break; |
| 586 | case '\t': |
| 587 | if (*word_len) { |
| 588 | /* word is finished */ |
| 589 | goto str_end; |
| 590 | } |
| 591 | /* tabs count for 8 spaces */ |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 592 | ctx->indent += 8; |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 593 | |
| 594 | ++(*data); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 595 | break; |
| 596 | case '\n': |
| 597 | if (*word_len) { |
| 598 | /* word is finished */ |
| 599 | goto str_end; |
| 600 | } |
| 601 | /* reset indent */ |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 602 | ctx->indent = 0; |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 603 | |
| 604 | /* track line numbers */ |
| 605 | ++ctx->line; |
| 606 | |
| 607 | ++(*data); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 608 | break; |
| 609 | case ';': |
| 610 | case '{': |
| 611 | if (*word_len || (arg == Y_MAYBE_STR_ARG)) { |
| 612 | /* word is finished */ |
| 613 | goto str_end; |
| 614 | } |
| 615 | |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 616 | LOGVAL_YANG(ctx, LY_VCODE_INSTREXP, 1, *data, "an argument"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 617 | return LY_EVALID; |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 618 | case '}': |
| 619 | /* invalid - braces cannot be in unquoted string (opening braces terminates the string and can follow it) */ |
| 620 | LOGVAL_YANG(ctx, LY_VCODE_INSTREXP, 1, *data, |
| 621 | "unquoted string character, optsep, semicolon or opening brace"); |
| 622 | return LY_EVALID; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 623 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 624 | LY_CHECK_RET(buf_store_char(ctx, data, arg, word_p, word_len, word_b, &buf_len, 0)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 625 | break; |
| 626 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 627 | } |
| 628 | |
| 629 | str_end: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 630 | /* terminating NULL byte for buf */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 631 | if (*word_b) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 632 | (*word_b) = ly_realloc(*word_b, (*word_len) + 1); |
| 633 | LY_CHECK_ERR_RET(!(*word_b), LOGMEM(ctx->ctx), LY_EMEM); |
| 634 | (*word_b)[*word_len] = '\0'; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 635 | *word_p = *word_b; |
| 636 | } |
| 637 | |
| 638 | return LY_SUCCESS; |
| 639 | } |
| 640 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 641 | /** |
| 642 | * @brief Get another YANG keyword from the raw data. |
| 643 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 644 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 645 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 646 | * @param[out] kw YANG keyword read. |
| 647 | * @param[out] word_p Pointer to the keyword in the data. Useful for extension instances. |
| 648 | * @param[out] word_len Length of the keyword in the data. Useful for extension instances. |
| 649 | * |
| 650 | * @return LY_ERR values. |
| 651 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 652 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 653 | get_keyword(struct ly_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] | 654 | { |
| 655 | LY_ERR ret; |
| 656 | int prefix; |
| 657 | const char *word_start; |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 658 | unsigned int c; |
| 659 | size_t len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 660 | |
| 661 | if (word_p) { |
| 662 | *word_p = NULL; |
| 663 | *word_len = 0; |
| 664 | } |
| 665 | |
| 666 | /* first skip "optsep", comments */ |
| 667 | while (**data) { |
Radek Krejci | dcc7b32 | 2018-10-11 14:24:02 +0200 | [diff] [blame] | 668 | switch (**data) { |
| 669 | case '/': |
| 670 | if ((*data)[1] == '/') { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 671 | /* one-line comment */ |
Radek Krejci | dcc7b32 | 2018-10-11 14:24:02 +0200 | [diff] [blame] | 672 | MOVE_INPUT(ctx, data, 2); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 673 | ret = skip_comment(ctx, data, 1); |
| 674 | if (ret) { |
| 675 | return ret; |
| 676 | } |
Radek Krejci | dcc7b32 | 2018-10-11 14:24:02 +0200 | [diff] [blame] | 677 | } else if ((*data)[1] == '*') { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 678 | /* block comment */ |
Radek Krejci | dcc7b32 | 2018-10-11 14:24:02 +0200 | [diff] [blame] | 679 | MOVE_INPUT(ctx, data, 2); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 680 | ret = skip_comment(ctx, data, 2); |
| 681 | if (ret) { |
| 682 | return ret; |
| 683 | } |
| 684 | } else { |
Radek Krejci | dcc7b32 | 2018-10-11 14:24:02 +0200 | [diff] [blame] | 685 | /* error - not a comment after all, keyword cannot start with slash */ |
| 686 | LOGVAL_YANG(ctx, LYVE_SYNTAX_YANG, "Invalid identifier first character '/'."); |
| 687 | return LY_EVALID; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 688 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 689 | break; |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 690 | case '\n': |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 691 | /* skip whitespaces (optsep) */ |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 692 | ++ctx->line; |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 693 | ctx->indent = 0; |
| 694 | break; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 695 | case ' ': |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 696 | /* skip whitespaces (optsep) */ |
| 697 | ++ctx->indent; |
| 698 | break; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 699 | case '\t': |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 700 | /* skip whitespaces (optsep) */ |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 701 | ctx->indent += 8; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 702 | break; |
| 703 | default: |
| 704 | /* either a keyword start or an invalid character */ |
| 705 | goto keyword_start; |
| 706 | } |
| 707 | |
| 708 | ++(*data); |
| 709 | } |
| 710 | |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 711 | #define IF_KW(STR, LEN, STMT) if (!strncmp(*(data), STR, LEN)) {MOVE_INPUT(ctx, data, LEN);*kw=STMT;} |
| 712 | #define IF_KW_PREFIX(STR, LEN) if (!strncmp(*(data), STR, LEN)) {MOVE_INPUT(ctx, data, LEN); |
| 713 | #define IF_KW_PREFIX_END } |
| 714 | |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 715 | keyword_start: |
| 716 | word_start = *data; |
| 717 | *kw = YANG_NONE; |
| 718 | |
| 719 | /* read the keyword itself */ |
| 720 | switch (**data) { |
| 721 | case 'a': |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 722 | MOVE_INPUT(ctx, data, 1); |
| 723 | IF_KW("rgument", 7, YANG_ARGUMENT) |
| 724 | else IF_KW("ugment", 6, YANG_AUGMENT) |
| 725 | else IF_KW("ction", 5, YANG_ACTION) |
| 726 | else IF_KW_PREFIX("ny", 2) |
| 727 | IF_KW("data", 4, YANG_ANYDATA) |
| 728 | else IF_KW("xml", 3, YANG_ANYXML) |
| 729 | IF_KW_PREFIX_END |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 730 | break; |
| 731 | case 'b': |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 732 | MOVE_INPUT(ctx, data, 1); |
| 733 | IF_KW("ase", 3, YANG_BASE) |
| 734 | else IF_KW("elongs-to", 9, YANG_BELONGS_TO) |
| 735 | else IF_KW("it", 2, YANG_BIT) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 736 | break; |
| 737 | case 'c': |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 738 | MOVE_INPUT(ctx, data, 1); |
| 739 | IF_KW("ase", 3, YANG_CASE) |
| 740 | else IF_KW("hoice", 5, YANG_CHOICE) |
| 741 | else IF_KW_PREFIX("on", 2) |
| 742 | IF_KW("fig", 3, YANG_CONFIG) |
| 743 | else IF_KW_PREFIX("ta", 2) |
| 744 | IF_KW("ct", 2, YANG_CONTACT) |
| 745 | else IF_KW("iner", 4, YANG_CONTAINER) |
| 746 | IF_KW_PREFIX_END |
| 747 | IF_KW_PREFIX_END |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 748 | break; |
| 749 | case 'd': |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 750 | MOVE_INPUT(ctx, data, 1); |
| 751 | IF_KW_PREFIX("e", 1) |
| 752 | IF_KW("fault", 5, YANG_DEFAULT) |
| 753 | else IF_KW("scription", 9, YANG_DESCRIPTION) |
| 754 | else IF_KW_PREFIX("viat", 4) |
| 755 | IF_KW("e", 1, YANG_DEVIATE) |
| 756 | else IF_KW("ion", 3, YANG_DEVIATION) |
| 757 | IF_KW_PREFIX_END |
| 758 | IF_KW_PREFIX_END |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 759 | break; |
| 760 | case 'e': |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 761 | MOVE_INPUT(ctx, data, 1); |
| 762 | IF_KW("num", 3, YANG_ENUM) |
| 763 | else IF_KW_PREFIX("rror-", 5) |
| 764 | IF_KW("app-tag", 7, YANG_ERROR_APP_TAG) |
| 765 | else IF_KW("message", 7, YANG_ERROR_MESSAGE) |
| 766 | IF_KW_PREFIX_END |
| 767 | else IF_KW("xtension", 8, YANG_EXTENSION) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 768 | break; |
| 769 | case 'f': |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 770 | MOVE_INPUT(ctx, data, 1); |
| 771 | IF_KW("eature", 6, YANG_FEATURE) |
| 772 | else IF_KW("raction-digits", 14, YANG_FRACTION_DIGITS) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 773 | break; |
| 774 | case 'g': |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 775 | MOVE_INPUT(ctx, data, 1); |
| 776 | IF_KW("rouping", 7, YANG_GROUPING) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 777 | break; |
| 778 | case 'i': |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 779 | MOVE_INPUT(ctx, data, 1); |
| 780 | IF_KW("dentity", 7, YANG_IDENTITY) |
| 781 | else IF_KW("f-feature", 9, YANG_IF_FEATURE) |
| 782 | else IF_KW("mport", 5, YANG_IMPORT) |
| 783 | else IF_KW_PREFIX("n", 1) |
| 784 | IF_KW("clude", 5, YANG_INCLUDE) |
| 785 | else IF_KW("put", 3, YANG_INPUT) |
| 786 | IF_KW_PREFIX_END |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 787 | break; |
| 788 | case 'k': |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 789 | MOVE_INPUT(ctx, data, 1); |
| 790 | IF_KW("ey", 2, YANG_KEY) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 791 | break; |
| 792 | case 'l': |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 793 | MOVE_INPUT(ctx, data, 1); |
| 794 | IF_KW_PREFIX("e", 1) |
| 795 | IF_KW("af-list", 7, YANG_LEAF_LIST) |
| 796 | else IF_KW("af", 2, YANG_LEAF) |
| 797 | else IF_KW("ngth", 4, YANG_LENGTH) |
| 798 | IF_KW_PREFIX_END |
| 799 | else IF_KW("ist", 3, YANG_LIST) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 800 | break; |
| 801 | case 'm': |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 802 | MOVE_INPUT(ctx, data, 1); |
| 803 | IF_KW_PREFIX("a", 1) |
| 804 | IF_KW("ndatory", 7, YANG_MANDATORY) |
| 805 | else IF_KW("x-elements", 10, YANG_MAX_ELEMENTS) |
| 806 | IF_KW_PREFIX_END |
| 807 | else IF_KW("in-elements", 11, YANG_MIN_ELEMENTS) |
| 808 | else IF_KW("ust", 3, YANG_MUST) |
| 809 | else IF_KW_PREFIX("od", 2) |
| 810 | IF_KW("ule", 3, YANG_MODULE) |
| 811 | else IF_KW("ifier", 5, YANG_MODIFIER) |
| 812 | IF_KW_PREFIX_END |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 813 | break; |
| 814 | case 'n': |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 815 | MOVE_INPUT(ctx, data, 1); |
| 816 | IF_KW("amespace", 8, YANG_NAMESPACE) |
| 817 | else IF_KW("otification", 11, YANG_NOTIFICATION) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 818 | break; |
| 819 | case 'o': |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 820 | MOVE_INPUT(ctx, data, 1); |
| 821 | IF_KW_PREFIX("r", 1) |
| 822 | IF_KW("dered-by", 8, YANG_ORDERED_BY) |
| 823 | else IF_KW("ganization", 10, YANG_ORGANIZATION) |
| 824 | IF_KW_PREFIX_END |
| 825 | else IF_KW("utput", 5, YANG_OUTPUT) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 826 | break; |
| 827 | case 'p': |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 828 | MOVE_INPUT(ctx, data, 1); |
| 829 | IF_KW("ath", 3, YANG_PATH) |
| 830 | else IF_KW("attern", 6, YANG_PATTERN) |
| 831 | else IF_KW("osition", 7, YANG_POSITION) |
| 832 | else IF_KW_PREFIX("re", 2) |
| 833 | IF_KW("fix", 3, YANG_PREFIX) |
| 834 | else IF_KW("sence", 5, YANG_PRESENCE) |
| 835 | IF_KW_PREFIX_END |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 836 | break; |
| 837 | case 'r': |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 838 | MOVE_INPUT(ctx, data, 1); |
| 839 | IF_KW("ange", 4, YANG_RANGE) |
| 840 | else IF_KW_PREFIX("e", 1) |
| 841 | IF_KW_PREFIX("f", 1) |
| 842 | IF_KW("erence", 6, YANG_REFERENCE) |
| 843 | else IF_KW("ine", 3, YANG_REFINE) |
| 844 | IF_KW_PREFIX_END |
| 845 | else IF_KW("quire-instance", 14, YANG_REQUIRE_INSTANCE) |
| 846 | else IF_KW("vision-date", 11, YANG_REVISION_DATE) |
| 847 | else IF_KW("vision", 6, YANG_REVISION) |
| 848 | IF_KW_PREFIX_END |
| 849 | else IF_KW("pc", 2, YANG_RPC) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 850 | break; |
| 851 | case 's': |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 852 | MOVE_INPUT(ctx, data, 1); |
| 853 | IF_KW("tatus", 5, YANG_STATUS) |
| 854 | else IF_KW("ubmodule", 8, YANG_SUBMODULE) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 855 | break; |
| 856 | case 't': |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 857 | MOVE_INPUT(ctx, data, 1); |
| 858 | IF_KW("ypedef", 6, YANG_TYPEDEF) |
| 859 | else IF_KW("ype", 3, YANG_TYPE) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 860 | break; |
| 861 | case 'u': |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 862 | MOVE_INPUT(ctx, data, 1); |
| 863 | IF_KW_PREFIX("ni", 2) |
| 864 | IF_KW("que", 3, YANG_UNIQUE) |
| 865 | else IF_KW("ts", 2, YANG_UNITS) |
| 866 | IF_KW_PREFIX_END |
| 867 | else IF_KW("ses", 3, YANG_USES) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 868 | break; |
| 869 | case 'v': |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 870 | MOVE_INPUT(ctx, data, 1); |
| 871 | IF_KW("alue", 4, YANG_VALUE) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 872 | break; |
| 873 | case 'w': |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 874 | MOVE_INPUT(ctx, data, 1); |
| 875 | IF_KW("hen", 3, YANG_WHEN) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 876 | break; |
| 877 | case 'y': |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 878 | MOVE_INPUT(ctx, data, 1); |
| 879 | IF_KW("ang-version", 11, YANG_YANG_VERSION) |
| 880 | else IF_KW("in-element", 10, YANG_YIN_ELEMENT) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 881 | break; |
| 882 | case ';': |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 883 | MOVE_INPUT(ctx, data, 1); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 884 | *kw = YANG_SEMICOLON; |
Radek Krejci | 626df48 | 2018-10-11 15:06:31 +0200 | [diff] [blame] | 885 | goto success; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 886 | case '{': |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 887 | MOVE_INPUT(ctx, data, 1); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 888 | *kw = YANG_LEFT_BRACE; |
Radek Krejci | 626df48 | 2018-10-11 15:06:31 +0200 | [diff] [blame] | 889 | goto success; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 890 | case '}': |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 891 | MOVE_INPUT(ctx, data, 1); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 892 | *kw = YANG_RIGHT_BRACE; |
Radek Krejci | 626df48 | 2018-10-11 15:06:31 +0200 | [diff] [blame] | 893 | goto success; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 894 | default: |
| 895 | break; |
| 896 | } |
| 897 | |
| 898 | if (*kw != YANG_NONE) { |
| 899 | /* make sure we have the whole keyword */ |
| 900 | switch (**data) { |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 901 | case '\n': |
| 902 | ++ctx->line; |
| 903 | /* fallthrough */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 904 | case ' ': |
| 905 | case '\t': |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 906 | /* mandatory "sep" */ |
| 907 | break; |
Radek Krejci | dcc7b32 | 2018-10-11 14:24:02 +0200 | [diff] [blame] | 908 | case '{': |
| 909 | /* allowed only for input and output statements which can be without arguments */ |
| 910 | if (*kw == YANG_INPUT || *kw == YANG_OUTPUT) { |
| 911 | break; |
| 912 | } |
| 913 | /* fallthrough */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 914 | default: |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 915 | MOVE_INPUT(ctx, data, 1); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 916 | LOGVAL_YANG(ctx, LY_VCODE_INSTREXP, (int)(*data - word_start), word_start, |
| 917 | "a keyword followed by a separator"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 918 | return LY_EVALID; |
| 919 | } |
| 920 | } else { |
| 921 | /* still can be an extension */ |
| 922 | prefix = 0; |
| 923 | while (**data && (**data != ' ') && (**data != '\t') && (**data != '\n') && (**data != '{') && (**data != ';')) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 924 | LY_CHECK_ERR_RET(ly_getutf8(data, &c, &len), |
| 925 | LOGVAL_YANG(ctx, LY_VCODE_INCHAR, (*data)[-len]), LY_EVALID); |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 926 | ++ctx->indent; |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 927 | /* check character validity */ |
| 928 | LY_CHECK_RET(check_identifierchar(ctx, c, *data - len == word_start ? 1 : 0, &prefix)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 929 | } |
| 930 | if (!**data) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 931 | LOGVAL_YANG(ctx, LY_VCODE_EOF); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 932 | return LY_EVALID; |
| 933 | } |
| 934 | |
| 935 | /* prefix is mandatory for extension instances */ |
Radek Krejci | dcc7b32 | 2018-10-11 14:24:02 +0200 | [diff] [blame] | 936 | if (prefix != 2) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 937 | LOGVAL_YANG(ctx, LY_VCODE_INSTREXP, (int)(*data - word_start), word_start, "a keyword"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 938 | return LY_EVALID; |
| 939 | } |
| 940 | |
| 941 | *kw = YANG_CUSTOM; |
| 942 | } |
Radek Krejci | 626df48 | 2018-10-11 15:06:31 +0200 | [diff] [blame] | 943 | success: |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 944 | if (word_p) { |
| 945 | *word_p = (char *)word_start; |
| 946 | *word_len = *data - word_start; |
| 947 | } |
| 948 | |
| 949 | return LY_SUCCESS; |
| 950 | } |
| 951 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 952 | /** |
| 953 | * @brief Parse extension instance substatements. |
| 954 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 955 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 956 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 957 | * @param[in] word Extension instance substatement name (keyword). |
| 958 | * @param[in] word_len Extension instance substatement name length. |
| 959 | * @param[in,out] child Children of this extension instance to add to. |
| 960 | * |
| 961 | * @return LY_ERR values. |
| 962 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 963 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 964 | parse_ext_substmt(struct ly_parser_ctx *ctx, const char **data, char *word, size_t word_len, |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 965 | struct lysp_stmt **child) |
| 966 | { |
| 967 | char *buf; |
| 968 | LY_ERR ret = 0; |
| 969 | enum yang_keyword kw; |
| 970 | struct lysp_stmt *stmt, *par_child; |
| 971 | |
| 972 | stmt = calloc(1, sizeof *stmt); |
| 973 | LY_CHECK_ERR_RET(!stmt, LOGMEM(NULL), LY_EMEM); |
| 974 | |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 975 | stmt->stmt = lydict_insert(ctx->ctx, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 976 | |
| 977 | /* get optional argument */ |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 978 | ret = get_argument(ctx, data, Y_MAYBE_STR_ARG, &word, &buf, &word_len); |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 979 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 980 | |
Radek Krejci | 0ae092d | 2018-09-20 16:43:19 +0200 | [diff] [blame] | 981 | if (word) { |
| 982 | if (buf) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 983 | stmt->arg = lydict_insert_zc(ctx->ctx, word); |
Radek Krejci | 0ae092d | 2018-09-20 16:43:19 +0200 | [diff] [blame] | 984 | } else { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 985 | stmt->arg = lydict_insert(ctx->ctx, word, word_len); |
Radek Krejci | 0ae092d | 2018-09-20 16:43:19 +0200 | [diff] [blame] | 986 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 987 | } |
| 988 | |
| 989 | /* insert into parent statements */ |
| 990 | if (!*child) { |
| 991 | *child = stmt; |
| 992 | } else { |
| 993 | for (par_child = *child; par_child->next; par_child = par_child->next); |
| 994 | par_child->next = stmt; |
| 995 | } |
| 996 | |
| 997 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) { |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 998 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 999 | |
| 1000 | ret = parse_ext_substmt(ctx, data, word, word_len, &stmt->child); |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1001 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1002 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1003 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1004 | |
| 1005 | return ret; |
| 1006 | } |
| 1007 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1008 | /** |
| 1009 | * @brief Parse extension instance. |
| 1010 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1011 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1012 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 1013 | * @param[in] ext_name Extension instance substatement name (keyword). |
| 1014 | * @param[in] ext_name_len Extension instance substatement name length. |
| 1015 | * @param[in] insubstmt Type of the keyword this extension instance is a substatement of. |
| 1016 | * @param[in] insubstmt_index Index of the keyword instance this extension instance is a substatement of. |
| 1017 | * @param[in,out] exts Extension instances to add to. |
| 1018 | * |
| 1019 | * @return LY_ERR values. |
| 1020 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1021 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1022 | parse_ext(struct ly_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] | 1023 | uint32_t insubstmt_index, struct lysp_ext_instance **exts) |
| 1024 | { |
| 1025 | LY_ERR ret = 0; |
| 1026 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 1027 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1028 | struct lysp_ext_instance *e; |
| 1029 | enum yang_keyword kw; |
| 1030 | |
| 1031 | LYSP_ARRAY_NEW_RET(ctx, exts, e, LY_EMEM); |
| 1032 | |
| 1033 | /* store name and insubstmt info */ |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1034 | e->name = lydict_insert(ctx->ctx, ext_name, ext_name_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1035 | e->insubstmt = insubstmt; |
| 1036 | e->insubstmt_index = insubstmt_index; |
| 1037 | |
| 1038 | /* get optional argument */ |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 1039 | ret = get_argument(ctx, data, Y_MAYBE_STR_ARG, &word, &buf, &word_len); |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1040 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1041 | |
Radek Krejci | 0ae092d | 2018-09-20 16:43:19 +0200 | [diff] [blame] | 1042 | if (word) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1043 | INSERT_WORD(ctx, buf, e->argument, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1044 | } |
| 1045 | |
| 1046 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) { |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1047 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1048 | |
| 1049 | ret = parse_ext_substmt(ctx, data, word, word_len, &e->child); |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1050 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1051 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1052 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1053 | |
| 1054 | return ret; |
| 1055 | } |
| 1056 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1057 | /** |
| 1058 | * @brief Parse a generic text field without specific constraints. Those are contact, organization, |
| 1059 | * description, etc... |
| 1060 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1061 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1062 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 1063 | * @param[in] substmt Type of this substatement. |
| 1064 | * @param[in] substmt_index Index of this substatement. |
| 1065 | * @param[in,out] value Place to store the parsed value. |
| 1066 | * @param[in] arg Type of the YANG keyword argument (of the value). |
| 1067 | * @param[in,out] exts Extension instances to add to. |
| 1068 | * |
| 1069 | * @return LY_ERR values. |
| 1070 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1071 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1072 | parse_text_field(struct ly_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] | 1073 | const char **value, enum yang_arg arg, struct lysp_ext_instance **exts) |
| 1074 | { |
| 1075 | LY_ERR ret = 0; |
| 1076 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 1077 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1078 | enum yang_keyword kw; |
| 1079 | |
| 1080 | if (*value) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1081 | LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, lyext_substmt2str(substmt)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1082 | return LY_EVALID; |
| 1083 | } |
| 1084 | |
| 1085 | /* get value */ |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 1086 | ret = get_argument(ctx, data, arg, &word, &buf, &word_len); |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1087 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1088 | |
| 1089 | /* store value and spend buf if allocated */ |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1090 | INSERT_WORD(ctx, buf, *value, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1091 | |
| 1092 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) { |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1093 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1094 | |
| 1095 | switch (kw) { |
| 1096 | case YANG_CUSTOM: |
| 1097 | ret = parse_ext(ctx, data, word, word_len, substmt, substmt_index, exts); |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1098 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1099 | break; |
| 1100 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1101 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), lyext_substmt2str(substmt)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1102 | return LY_EVALID; |
| 1103 | } |
| 1104 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1105 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1106 | |
| 1107 | return ret; |
| 1108 | } |
| 1109 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1110 | /** |
| 1111 | * @brief Parse the yang-version statement. |
| 1112 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1113 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1114 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 1115 | * @param[in] mod Module to store the parsed information in. |
| 1116 | * |
| 1117 | * @return LY_ERR values. |
| 1118 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1119 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1120 | parse_yangversion(struct ly_parser_ctx *ctx, const char **data, struct lysp_module *mod) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1121 | { |
| 1122 | LY_ERR ret = 0; |
| 1123 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 1124 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1125 | enum yang_keyword kw; |
| 1126 | |
| 1127 | if (mod->version) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1128 | LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "yang-version"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1129 | return LY_EVALID; |
| 1130 | } |
| 1131 | |
| 1132 | /* get value */ |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 1133 | ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len); |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1134 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1135 | |
| 1136 | if ((word_len == 3) && !strncmp(word, "1.0", word_len)) { |
| 1137 | mod->version = LYS_VERSION_1_0; |
| 1138 | } else if ((word_len == 3) && !strncmp(word, "1.1", word_len)) { |
| 1139 | mod->version = LYS_VERSION_1_1; |
| 1140 | } else { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1141 | LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "yang-version"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1142 | free(buf); |
| 1143 | return LY_EVALID; |
| 1144 | } |
| 1145 | free(buf); |
| 1146 | |
| 1147 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) { |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1148 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1149 | |
| 1150 | switch (kw) { |
| 1151 | case YANG_CUSTOM: |
| 1152 | ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_VERSION, 0, &mod->exts); |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1153 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1154 | break; |
| 1155 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1156 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "yang-version"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1157 | return LY_EVALID; |
| 1158 | } |
| 1159 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1160 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1161 | |
| 1162 | return ret; |
| 1163 | } |
| 1164 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1165 | /** |
| 1166 | * @brief Parse the belongs-to statement. |
| 1167 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1168 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1169 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 1170 | * @param[in,out] belongsto Place to store the parsed value. |
| 1171 | * @param[in,out] prefix Place to store the parsed belongs-to prefix value. |
| 1172 | * @param[in,out] exts Extension instances to add to. |
| 1173 | * |
| 1174 | * @return LY_ERR values. |
| 1175 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1176 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1177 | parse_belongsto(struct ly_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] | 1178 | { |
| 1179 | LY_ERR ret = 0; |
| 1180 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 1181 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1182 | enum yang_keyword kw; |
| 1183 | |
| 1184 | if (*belongsto) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1185 | LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "belongs-to"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1186 | return LY_EVALID; |
| 1187 | } |
| 1188 | |
| 1189 | /* get value */ |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 1190 | ret = get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len); |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1191 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1192 | |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1193 | INSERT_WORD(ctx, buf, *belongsto, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1194 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) { |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1195 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1196 | |
| 1197 | switch (kw) { |
| 1198 | case YANG_PREFIX: |
| 1199 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_PREFIX, 0, prefix, Y_IDENTIF_ARG, exts); |
| 1200 | break; |
| 1201 | case YANG_CUSTOM: |
| 1202 | ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_BELONGSTO, 0, exts); |
| 1203 | break; |
| 1204 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1205 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "belongs-to"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1206 | return LY_EVALID; |
| 1207 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1208 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1209 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1210 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1211 | |
| 1212 | /* mandatory substatements */ |
| 1213 | if (!*prefix) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1214 | LOGVAL_YANG(ctx, LY_VCODE_MISSTMT, "prefix", "belongs-to"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1215 | return LY_EVALID; |
| 1216 | } |
| 1217 | |
| 1218 | return ret; |
| 1219 | } |
| 1220 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1221 | /** |
| 1222 | * @brief Parse the revision-date statement. |
| 1223 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1224 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1225 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 1226 | * @param[in,out] rev Array to store the parsed value in. |
| 1227 | * @param[in,out] exts Extension instances to add to. |
| 1228 | * |
| 1229 | * @return LY_ERR values. |
| 1230 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1231 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1232 | parse_revisiondate(struct ly_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] | 1233 | { |
| 1234 | LY_ERR ret = 0; |
| 1235 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 1236 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1237 | enum yang_keyword kw; |
| 1238 | |
| 1239 | if (rev[0]) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1240 | LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "revision-date"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1241 | return LY_EVALID; |
| 1242 | } |
| 1243 | |
| 1244 | /* get value */ |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 1245 | ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len); |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1246 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1247 | |
| 1248 | /* check value */ |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1249 | if (lysp_check_date(ctx->ctx, word, word_len, "revision-date")) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1250 | free(buf); |
| 1251 | return LY_EVALID; |
| 1252 | } |
| 1253 | |
| 1254 | /* store value and spend buf if allocated */ |
| 1255 | strncpy(rev, word, word_len); |
| 1256 | free(buf); |
| 1257 | |
| 1258 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) { |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1259 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1260 | |
| 1261 | switch (kw) { |
| 1262 | case YANG_CUSTOM: |
| 1263 | ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_REVISIONDATE, 0, exts); |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1264 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1265 | break; |
| 1266 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1267 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "revision-date"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1268 | return LY_EVALID; |
| 1269 | } |
| 1270 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1271 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1272 | |
| 1273 | return ret; |
| 1274 | } |
| 1275 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1276 | /** |
| 1277 | * @brief Parse the include statement. |
| 1278 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1279 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1280 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 1281 | * @param[in,out] includes Parsed includes to add to. |
| 1282 | * |
| 1283 | * @return LY_ERR values. |
| 1284 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1285 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1286 | parse_include(struct ly_parser_ctx *ctx, const char **data, struct lysp_include **includes) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1287 | { |
| 1288 | LY_ERR ret = 0; |
| 1289 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 1290 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1291 | enum yang_keyword kw; |
| 1292 | struct lysp_include *inc; |
| 1293 | |
| 1294 | LYSP_ARRAY_NEW_RET(ctx, includes, inc, LY_EMEM); |
| 1295 | |
| 1296 | /* get value */ |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 1297 | ret = get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len); |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1298 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1299 | |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1300 | INSERT_WORD(ctx, buf, inc->name, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1301 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) { |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1302 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1303 | |
| 1304 | switch (kw) { |
| 1305 | case YANG_DESCRIPTION: |
| 1306 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &inc->dsc, Y_STR_ARG, &inc->exts); |
| 1307 | break; |
| 1308 | case YANG_REFERENCE: |
| 1309 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &inc->ref, Y_STR_ARG, &inc->exts); |
| 1310 | break; |
| 1311 | case YANG_REVISION_DATE: |
| 1312 | ret = parse_revisiondate(ctx, data, inc->rev, &inc->exts); |
| 1313 | break; |
| 1314 | case YANG_CUSTOM: |
| 1315 | ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &inc->exts); |
| 1316 | break; |
| 1317 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1318 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "include"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1319 | return LY_EVALID; |
| 1320 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1321 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1322 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1323 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1324 | |
| 1325 | return ret; |
| 1326 | } |
| 1327 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1328 | /** |
| 1329 | * @brief Parse the import statement. |
| 1330 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1331 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1332 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 1333 | * @param[in,out] imports Parsed imports to add to. |
| 1334 | * |
| 1335 | * @return LY_ERR values. |
| 1336 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1337 | static LY_ERR |
Radek Krejci | 70853c5 | 2018-10-15 14:46:16 +0200 | [diff] [blame] | 1338 | parse_import(struct ly_parser_ctx *ctx, const char **data, struct lysp_module *module) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1339 | { |
| 1340 | LY_ERR ret = 0; |
| 1341 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 1342 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1343 | enum yang_keyword kw; |
| 1344 | struct lysp_import *imp; |
| 1345 | |
Radek Krejci | 70853c5 | 2018-10-15 14:46:16 +0200 | [diff] [blame] | 1346 | LYSP_ARRAY_NEW_RET(ctx, &module->imports, imp, LY_EVALID); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1347 | |
| 1348 | /* get value */ |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 1349 | ret = get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len); |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1350 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1351 | |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1352 | INSERT_WORD(ctx, buf, imp->name, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1353 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) { |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1354 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1355 | |
| 1356 | switch (kw) { |
| 1357 | case YANG_PREFIX: |
| 1358 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_PREFIX, 0, &imp->prefix, Y_IDENTIF_ARG, &imp->exts); |
Radek Krejci | 70853c5 | 2018-10-15 14:46:16 +0200 | [diff] [blame] | 1359 | LY_CHECK_RET(lysp_check_prefix(ctx, module, &imp->prefix), LY_EVALID); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1360 | break; |
| 1361 | case YANG_DESCRIPTION: |
| 1362 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &imp->dsc, Y_STR_ARG, &imp->exts); |
| 1363 | break; |
| 1364 | case YANG_REFERENCE: |
| 1365 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &imp->ref, Y_STR_ARG, &imp->exts); |
| 1366 | break; |
| 1367 | case YANG_REVISION_DATE: |
| 1368 | ret = parse_revisiondate(ctx, data, imp->rev, &imp->exts); |
| 1369 | break; |
| 1370 | case YANG_CUSTOM: |
| 1371 | ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &imp->exts); |
| 1372 | break; |
| 1373 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1374 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "import"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1375 | return LY_EVALID; |
| 1376 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1377 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1378 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1379 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1380 | |
| 1381 | /* mandatory substatements */ |
| 1382 | if (!imp->prefix) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1383 | LOGVAL_YANG(ctx, LY_VCODE_MISSTMT, "prefix", "import"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1384 | return LY_EVALID; |
| 1385 | } |
| 1386 | |
| 1387 | return ret; |
| 1388 | } |
| 1389 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1390 | /** |
| 1391 | * @brief Parse the revision statement. |
| 1392 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1393 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1394 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 1395 | * @param[in,out] revs Parsed revisions to add to. |
| 1396 | * |
| 1397 | * @return LY_ERR values. |
| 1398 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1399 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1400 | parse_revision(struct ly_parser_ctx *ctx, const char **data, struct lysp_revision **revs) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1401 | { |
| 1402 | LY_ERR ret = 0; |
| 1403 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 1404 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1405 | enum yang_keyword kw; |
| 1406 | struct lysp_revision *rev; |
| 1407 | |
| 1408 | LYSP_ARRAY_NEW_RET(ctx, revs, rev, LY_EMEM); |
| 1409 | |
| 1410 | /* get value */ |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 1411 | ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len); |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1412 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1413 | |
| 1414 | /* check value */ |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1415 | if (lysp_check_date(ctx->ctx, word, word_len, "revision")) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1416 | return LY_EVALID; |
| 1417 | } |
| 1418 | |
| 1419 | strncpy(rev->rev, word, word_len); |
| 1420 | free(buf); |
| 1421 | |
| 1422 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) { |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1423 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1424 | |
| 1425 | switch (kw) { |
| 1426 | case YANG_DESCRIPTION: |
| 1427 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &rev->dsc, Y_STR_ARG, &rev->exts); |
| 1428 | break; |
| 1429 | case YANG_REFERENCE: |
| 1430 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &rev->ref, Y_STR_ARG, &rev->exts); |
| 1431 | break; |
| 1432 | case YANG_CUSTOM: |
| 1433 | ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &rev->exts); |
| 1434 | break; |
| 1435 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1436 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "revision"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1437 | return LY_EVALID; |
| 1438 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1439 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1440 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1441 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1442 | |
| 1443 | return ret; |
| 1444 | } |
| 1445 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1446 | /** |
| 1447 | * @brief Parse a generic text field that can have more instances such as base. |
| 1448 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1449 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1450 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 1451 | * @param[in] substmt Type of this substatement. |
| 1452 | * @param[in,out] texts Parsed values to add to. |
| 1453 | * @param[in] arg Type of the expected argument. |
| 1454 | * @param[in,out] exts Extension instances to add to. |
| 1455 | * |
| 1456 | * @return LY_ERR values. |
| 1457 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1458 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1459 | parse_text_fields(struct ly_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] | 1460 | struct lysp_ext_instance **exts) |
| 1461 | { |
| 1462 | LY_ERR ret = 0; |
| 1463 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 1464 | size_t count, word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1465 | enum yang_keyword kw; |
| 1466 | |
| 1467 | /* allocate new pointer */ |
| 1468 | for (count = 1; (*texts) && (*texts)[count - 1]; ++count); |
| 1469 | *texts = realloc(*texts, count * sizeof **texts); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1470 | LY_CHECK_ERR_RET(!*texts, LOGMEM(ctx->ctx), LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1471 | |
| 1472 | /* get value */ |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 1473 | ret = get_argument(ctx, data, arg, &word, &buf, &word_len); |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1474 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1475 | |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1476 | INSERT_WORD(ctx, buf, (*texts)[count - 1], word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1477 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) { |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1478 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1479 | |
| 1480 | switch (kw) { |
| 1481 | case YANG_CUSTOM: |
| 1482 | ret = parse_ext(ctx, data, word, word_len, substmt, count - 1, exts); |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1483 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1484 | break; |
| 1485 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1486 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), lyext_substmt2str(substmt)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1487 | return LY_EVALID; |
| 1488 | } |
| 1489 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1490 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1491 | |
| 1492 | return ret; |
| 1493 | } |
| 1494 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1495 | /** |
| 1496 | * @brief Parse the config statement. |
| 1497 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1498 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1499 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 1500 | * @param[in,out] flags Flags to add to. |
| 1501 | * @param[in,out] exts Extension instances to add to. |
| 1502 | * |
| 1503 | * @return LY_ERR values. |
| 1504 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1505 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1506 | parse_config(struct ly_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] | 1507 | { |
| 1508 | LY_ERR ret = 0; |
| 1509 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 1510 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1511 | enum yang_keyword kw; |
| 1512 | |
| 1513 | if (*flags & LYS_CONFIG_MASK) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1514 | LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "config"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1515 | return LY_EVALID; |
| 1516 | } |
| 1517 | |
| 1518 | /* get value */ |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 1519 | ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len); |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1520 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1521 | |
| 1522 | if ((word_len == 4) && !strncmp(word, "true", word_len)) { |
| 1523 | *flags |= LYS_CONFIG_W; |
| 1524 | } else if ((word_len == 5) && !strncmp(word, "false", word_len)) { |
| 1525 | *flags |= LYS_CONFIG_R; |
| 1526 | } else { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1527 | LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "config"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1528 | free(buf); |
| 1529 | return LY_EVALID; |
| 1530 | } |
| 1531 | free(buf); |
| 1532 | |
| 1533 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) { |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1534 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1535 | |
| 1536 | switch (kw) { |
| 1537 | case YANG_CUSTOM: |
| 1538 | ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_CONFIG, 0, exts); |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1539 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1540 | break; |
| 1541 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1542 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "config"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1543 | return LY_EVALID; |
| 1544 | } |
| 1545 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1546 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1547 | |
| 1548 | return ret; |
| 1549 | } |
| 1550 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1551 | /** |
| 1552 | * @brief Parse the mandatory statement. |
| 1553 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1554 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1555 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 1556 | * @param[in,out] flags Flags to add to. |
| 1557 | * @param[in,out] exts Extension instances to add to. |
| 1558 | * |
| 1559 | * @return LY_ERR values. |
| 1560 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1561 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1562 | parse_mandatory(struct ly_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] | 1563 | { |
| 1564 | LY_ERR ret = 0; |
| 1565 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 1566 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1567 | enum yang_keyword kw; |
| 1568 | |
| 1569 | if (*flags & LYS_MAND_MASK) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1570 | LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "mandatory"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1571 | return LY_EVALID; |
| 1572 | } |
| 1573 | |
| 1574 | /* get value */ |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 1575 | ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len); |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1576 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1577 | |
| 1578 | if ((word_len == 4) && !strncmp(word, "true", word_len)) { |
| 1579 | *flags |= LYS_MAND_TRUE; |
| 1580 | } else if ((word_len == 5) && !strncmp(word, "false", word_len)) { |
| 1581 | *flags |= LYS_MAND_FALSE; |
| 1582 | } else { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1583 | LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "mandatory"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1584 | free(buf); |
| 1585 | return LY_EVALID; |
| 1586 | } |
| 1587 | free(buf); |
| 1588 | |
| 1589 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) { |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1590 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1591 | |
| 1592 | switch (kw) { |
| 1593 | case YANG_CUSTOM: |
| 1594 | ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_MANDATORY, 0, exts); |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1595 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1596 | break; |
| 1597 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1598 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "mandatory"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1599 | return LY_EVALID; |
| 1600 | } |
| 1601 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1602 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1603 | |
| 1604 | return ret; |
| 1605 | } |
| 1606 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1607 | /** |
| 1608 | * @brief Parse a restriction such as range or length. |
| 1609 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1610 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1611 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 1612 | * @param[in] restr_kw Type of this particular restriction. |
| 1613 | * @param[in,out] exts Extension instances to add to. |
| 1614 | * |
| 1615 | * @return LY_ERR values. |
| 1616 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1617 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1618 | parse_restr(struct ly_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] | 1619 | { |
| 1620 | LY_ERR ret = 0; |
| 1621 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 1622 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1623 | enum yang_keyword kw; |
| 1624 | |
| 1625 | /* get value */ |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 1626 | ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len); |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1627 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1628 | |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1629 | INSERT_WORD(ctx, buf, restr->arg, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1630 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) { |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1631 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1632 | |
| 1633 | switch (kw) { |
| 1634 | case YANG_DESCRIPTION: |
| 1635 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &restr->dsc, Y_STR_ARG, &restr->exts); |
| 1636 | break; |
| 1637 | case YANG_REFERENCE: |
| 1638 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &restr->ref, Y_STR_ARG, &restr->exts); |
| 1639 | break; |
| 1640 | case YANG_ERROR_APP_TAG: |
| 1641 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_ERRTAG, 0, &restr->eapptag, Y_STR_ARG, &restr->exts); |
| 1642 | break; |
| 1643 | case YANG_ERROR_MESSAGE: |
| 1644 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_ERRMSG, 0, &restr->emsg, Y_STR_ARG, &restr->exts); |
| 1645 | break; |
| 1646 | case YANG_CUSTOM: |
| 1647 | ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &restr->exts); |
| 1648 | break; |
| 1649 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1650 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(restr_kw)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1651 | return LY_EVALID; |
| 1652 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1653 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1654 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1655 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1656 | |
| 1657 | return ret; |
| 1658 | } |
| 1659 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1660 | /** |
| 1661 | * @brief Parse a restriction that can have more instances such as must. |
| 1662 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1663 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1664 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 1665 | * @param[in] restr_kw Type of this particular restriction. |
| 1666 | * @param[in,out] restrs Restrictions to add to. |
| 1667 | * |
| 1668 | * @return LY_ERR values. |
| 1669 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1670 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1671 | parse_restrs(struct ly_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] | 1672 | { |
| 1673 | struct lysp_restr *restr; |
| 1674 | |
| 1675 | LYSP_ARRAY_NEW_RET(ctx, restrs, restr, LY_EMEM); |
| 1676 | |
| 1677 | return parse_restr(ctx, data, restr_kw, restr); |
| 1678 | } |
| 1679 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1680 | /** |
| 1681 | * @brief Parse the status statement. |
| 1682 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1683 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1684 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 1685 | * @param[in,out] flags Flags to add to. |
| 1686 | * @param[in,out] exts Extension instances to add to. |
| 1687 | * |
| 1688 | * @return LY_ERR values. |
| 1689 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1690 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1691 | parse_status(struct ly_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] | 1692 | { |
| 1693 | LY_ERR ret = 0; |
| 1694 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 1695 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1696 | enum yang_keyword kw; |
| 1697 | |
| 1698 | if (*flags & LYS_STATUS_MASK) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1699 | LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "status"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1700 | return LY_EVALID; |
| 1701 | } |
| 1702 | |
| 1703 | /* get value */ |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 1704 | ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len); |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1705 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1706 | |
| 1707 | if ((word_len == 7) && !strncmp(word, "current", word_len)) { |
| 1708 | *flags |= LYS_STATUS_CURR; |
| 1709 | } else if ((word_len == 10) && !strncmp(word, "deprecated", word_len)) { |
| 1710 | *flags |= LYS_STATUS_DEPRC; |
| 1711 | } else if ((word_len == 8) && !strncmp(word, "obsolete", word_len)) { |
| 1712 | *flags |= LYS_STATUS_OBSLT; |
| 1713 | } else { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1714 | LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "status"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1715 | free(buf); |
| 1716 | return LY_EVALID; |
| 1717 | } |
| 1718 | free(buf); |
| 1719 | |
| 1720 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) { |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1721 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1722 | |
| 1723 | switch (kw) { |
| 1724 | case YANG_CUSTOM: |
| 1725 | ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_STATUS, 0, exts); |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1726 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1727 | break; |
| 1728 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1729 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "status"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1730 | return LY_EVALID; |
| 1731 | } |
| 1732 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1733 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1734 | |
| 1735 | return ret; |
| 1736 | } |
| 1737 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1738 | /** |
| 1739 | * @brief Parse the when statement. |
| 1740 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1741 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1742 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 1743 | * @param[in,out] when_p When pointer to parse to. |
| 1744 | * |
| 1745 | * @return LY_ERR values. |
| 1746 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1747 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1748 | parse_when(struct ly_parser_ctx *ctx, const char **data, struct lysp_when **when_p) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1749 | { |
| 1750 | LY_ERR ret = 0; |
| 1751 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 1752 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1753 | enum yang_keyword kw; |
| 1754 | struct lysp_when *when; |
| 1755 | |
| 1756 | if (*when_p) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1757 | LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "when"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1758 | return LY_EVALID; |
| 1759 | } |
| 1760 | |
| 1761 | when = calloc(1, sizeof *when); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1762 | LY_CHECK_ERR_RET(!when, LOGMEM(ctx->ctx), LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1763 | *when_p = when; |
| 1764 | |
| 1765 | /* get value */ |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 1766 | ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len); |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1767 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1768 | |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1769 | INSERT_WORD(ctx, buf, when->cond, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1770 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) { |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1771 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1772 | |
| 1773 | switch (kw) { |
| 1774 | case YANG_DESCRIPTION: |
| 1775 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &when->dsc, Y_STR_ARG, &when->exts); |
| 1776 | break; |
| 1777 | case YANG_REFERENCE: |
| 1778 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &when->ref, Y_STR_ARG, &when->exts); |
| 1779 | break; |
| 1780 | case YANG_CUSTOM: |
| 1781 | ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &when->exts); |
| 1782 | break; |
| 1783 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1784 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "when"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1785 | return LY_EVALID; |
| 1786 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1787 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1788 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1789 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1790 | |
| 1791 | return ret; |
| 1792 | } |
| 1793 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1794 | /** |
| 1795 | * @brief Parse the anydata or anyxml statement. |
| 1796 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1797 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1798 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 1799 | * @param[in] kw Type of this particular keyword. |
| 1800 | * @param[in,out] siblings Siblings to add to. |
| 1801 | * |
| 1802 | * @return LY_ERR values. |
| 1803 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1804 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1805 | parse_any(struct ly_parser_ctx *ctx, const char **data, enum yang_keyword kw, struct lysp_node **siblings) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1806 | { |
| 1807 | LY_ERR ret = 0; |
| 1808 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 1809 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1810 | struct lysp_node *iter; |
| 1811 | struct lysp_node_anydata *any; |
| 1812 | |
| 1813 | /* create structure */ |
| 1814 | any = calloc(1, sizeof *any); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1815 | LY_CHECK_ERR_RET(!any, LOGMEM(ctx->ctx), LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1816 | any->nodetype = kw == YANG_ANYDATA ? LYS_ANYDATA : LYS_ANYXML; |
| 1817 | |
| 1818 | /* insert into siblings */ |
| 1819 | if (!*siblings) { |
| 1820 | *siblings = (struct lysp_node *)any; |
| 1821 | } else { |
| 1822 | for (iter = *siblings; iter->next; iter = iter->next); |
| 1823 | iter->next = (struct lysp_node *)any; |
| 1824 | } |
| 1825 | |
| 1826 | /* get name */ |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 1827 | ret = get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len); |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1828 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1829 | |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1830 | INSERT_WORD(ctx, buf, any->name, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1831 | |
| 1832 | /* parse substatements */ |
| 1833 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) { |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1834 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1835 | |
| 1836 | switch (kw) { |
| 1837 | case YANG_CONFIG: |
| 1838 | ret = parse_config(ctx, data, &any->flags, &any->exts); |
| 1839 | break; |
| 1840 | case YANG_DESCRIPTION: |
| 1841 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &any->dsc, Y_STR_ARG, &any->exts); |
| 1842 | break; |
| 1843 | case YANG_IF_FEATURE: |
| 1844 | ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &any->iffeatures, Y_STR_ARG, &any->exts); |
| 1845 | break; |
| 1846 | case YANG_MANDATORY: |
| 1847 | ret = parse_mandatory(ctx, data, &any->flags, &any->exts); |
| 1848 | break; |
| 1849 | case YANG_MUST: |
| 1850 | ret = parse_restrs(ctx, data, kw, &any->musts); |
| 1851 | break; |
| 1852 | case YANG_REFERENCE: |
| 1853 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &any->ref, Y_STR_ARG, &any->exts); |
| 1854 | break; |
| 1855 | case YANG_STATUS: |
| 1856 | ret = parse_status(ctx, data, &any->flags, &any->exts); |
| 1857 | break; |
| 1858 | case YANG_WHEN: |
| 1859 | ret = parse_when(ctx, data, &any->when); |
| 1860 | break; |
| 1861 | case YANG_CUSTOM: |
| 1862 | ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &any->exts); |
| 1863 | break; |
| 1864 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1865 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1866 | (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] | 1867 | return LY_EVALID; |
| 1868 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1869 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1870 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1871 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1872 | |
| 1873 | return ret; |
| 1874 | } |
| 1875 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1876 | /** |
| 1877 | * @brief Parse the value or position statement. Substatement of type enum statement. |
| 1878 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1879 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1880 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 1881 | * @param[in] val_kw Type of this particular keyword. |
| 1882 | * @param[in,out] value Value to write to. |
| 1883 | * @param[in,out] flags Flags to write to. |
| 1884 | * @param[in,out] exts Extension instances to add to. |
| 1885 | * |
| 1886 | * @return LY_ERR values. |
| 1887 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1888 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1889 | parse_type_enum_value_pos(struct ly_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] | 1890 | struct lysp_ext_instance **exts) |
| 1891 | { |
| 1892 | LY_ERR ret = 0; |
| 1893 | char *buf, *word, *ptr; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 1894 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1895 | long int num; |
| 1896 | unsigned long int unum; |
| 1897 | enum yang_keyword kw; |
| 1898 | |
| 1899 | if (*flags & LYS_SET_VALUE) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1900 | LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(val_kw)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1901 | return LY_EVALID; |
| 1902 | } |
| 1903 | *flags |= LYS_SET_VALUE; |
| 1904 | |
| 1905 | /* get value */ |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 1906 | ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len); |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1907 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1908 | |
| 1909 | if (!word_len || (word[0] == '+') || ((word[0] == '0') && (word_len > 1)) || ((val_kw == YANG_VALUE) && !strncmp(word, "-0", 2))) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1910 | LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1911 | free(buf); |
| 1912 | return LY_EVALID; |
| 1913 | } |
| 1914 | |
| 1915 | errno = 0; |
| 1916 | if (val_kw == YANG_VALUE) { |
| 1917 | num = strtol(word, &ptr, 10); |
| 1918 | } else { |
| 1919 | unum = strtoul(word, &ptr, 10); |
| 1920 | } |
| 1921 | /* we have not parsed the whole argument */ |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 1922 | if ((size_t)(ptr - word) != word_len) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1923 | LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1924 | free(buf); |
| 1925 | return LY_EVALID; |
| 1926 | } |
| 1927 | if (errno == ERANGE) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1928 | LOGVAL_YANG(ctx, LY_VCODE_OOB, word_len, word, ly_stmt2str(val_kw)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1929 | free(buf); |
| 1930 | return LY_EVALID; |
| 1931 | } |
| 1932 | if (val_kw == YANG_VALUE) { |
| 1933 | *value = num; |
| 1934 | } else { |
| 1935 | *value = unum; |
| 1936 | } |
| 1937 | free(buf); |
| 1938 | |
| 1939 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) { |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1940 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1941 | |
| 1942 | switch (kw) { |
| 1943 | case YANG_CUSTOM: |
| 1944 | ret = parse_ext(ctx, data, word, word_len, val_kw == YANG_VALUE ? LYEXT_SUBSTMT_VALUE : LYEXT_SUBSTMT_POSITION, 0, exts); |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1945 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1946 | break; |
| 1947 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1948 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(val_kw)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1949 | return LY_EVALID; |
| 1950 | } |
| 1951 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1952 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1953 | |
| 1954 | return ret; |
| 1955 | } |
| 1956 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1957 | /** |
| 1958 | * @brief Parse the enum or bit statement. Substatement of type statement. |
| 1959 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1960 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1961 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 1962 | * @param[in] enum_kw Type of this particular keyword. |
| 1963 | * @param[in,out] enums Enums or bits to add to. |
| 1964 | * |
| 1965 | * @return LY_ERR values. |
| 1966 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1967 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1968 | parse_type_enum(struct ly_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] | 1969 | { |
| 1970 | LY_ERR ret = 0; |
| 1971 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 1972 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1973 | enum yang_keyword kw; |
| 1974 | struct lysp_type_enum *enm; |
| 1975 | |
| 1976 | LYSP_ARRAY_NEW_RET(ctx, enums, enm, LY_EMEM); |
| 1977 | |
| 1978 | /* get value */ |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 1979 | ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len); |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1980 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1981 | |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1982 | INSERT_WORD(ctx, buf, enm->name, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1983 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) { |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1984 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1985 | |
| 1986 | switch (kw) { |
| 1987 | case YANG_DESCRIPTION: |
| 1988 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &enm->dsc, Y_STR_ARG, &enm->exts); |
| 1989 | break; |
| 1990 | case YANG_IF_FEATURE: |
| 1991 | ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &enm->iffeatures, Y_STR_ARG, &enm->exts); |
| 1992 | break; |
| 1993 | case YANG_REFERENCE: |
| 1994 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &enm->ref, Y_STR_ARG, &enm->exts); |
| 1995 | break; |
| 1996 | case YANG_STATUS: |
| 1997 | ret = parse_status(ctx, data, &enm->flags, &enm->exts); |
| 1998 | break; |
| 1999 | case YANG_VALUE: |
| 2000 | case YANG_POSITION: |
| 2001 | ret = parse_type_enum_value_pos(ctx, data, kw, &enm->value, &enm->flags, &enm->exts); |
| 2002 | break; |
| 2003 | case YANG_CUSTOM: |
| 2004 | ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &enm->exts); |
| 2005 | break; |
| 2006 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2007 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(enum_kw)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2008 | return LY_EVALID; |
| 2009 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 2010 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2011 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 2012 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2013 | |
| 2014 | return ret; |
| 2015 | } |
| 2016 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2017 | /** |
| 2018 | * @brief Parse the fraction-digits statement. Substatement of type statement. |
| 2019 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2020 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2021 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 2022 | * @param[in,out] fracdig Value to write to. |
| 2023 | * @param[in,out] exts Extension instances to add to. |
| 2024 | * |
| 2025 | * @return LY_ERR values. |
| 2026 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2027 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2028 | parse_type_fracdigits(struct ly_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] | 2029 | { |
| 2030 | LY_ERR ret = 0; |
| 2031 | char *buf, *word, *ptr; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 2032 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2033 | unsigned long int num; |
| 2034 | enum yang_keyword kw; |
| 2035 | |
| 2036 | if (*fracdig) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2037 | LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "fraction-digits"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2038 | return LY_EVALID; |
| 2039 | } |
| 2040 | |
| 2041 | /* get value */ |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 2042 | ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len); |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 2043 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2044 | |
| 2045 | if (!word_len || (word[0] == '0') || !isdigit(word[0])) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2046 | LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "fraction-digits"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2047 | free(buf); |
| 2048 | return LY_EVALID; |
| 2049 | } |
| 2050 | |
| 2051 | errno = 0; |
| 2052 | num = strtoul(word, &ptr, 10); |
| 2053 | /* we have not parsed the whole argument */ |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 2054 | if ((size_t)(ptr - word) != word_len) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2055 | LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "fraction-digits"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2056 | free(buf); |
| 2057 | return LY_EVALID; |
| 2058 | } |
| 2059 | if ((errno == ERANGE) || (num > 18)) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2060 | LOGVAL_YANG(ctx, LY_VCODE_OOB, word_len, word, "fraction-digits"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2061 | free(buf); |
| 2062 | return LY_EVALID; |
| 2063 | } |
| 2064 | *fracdig = num; |
| 2065 | free(buf); |
| 2066 | |
| 2067 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) { |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 2068 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2069 | |
| 2070 | switch (kw) { |
| 2071 | case YANG_CUSTOM: |
| 2072 | ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_FRACDIGITS, 0, exts); |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 2073 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2074 | break; |
| 2075 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2076 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "fraction-digits"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2077 | return LY_EVALID; |
| 2078 | } |
| 2079 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 2080 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2081 | |
| 2082 | return ret; |
| 2083 | } |
| 2084 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2085 | /** |
| 2086 | * @brief Parse the require-instance statement. Substatement of type statement. |
| 2087 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2088 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2089 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 2090 | * @param[in,out] reqinst Value to write to. |
| 2091 | * @param[in,out] flags Flags to write to. |
| 2092 | * @param[in,out] exts Extension instances to add to. |
| 2093 | * |
| 2094 | * @return LY_ERR values. |
| 2095 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2096 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2097 | parse_type_reqinstance(struct ly_parser_ctx *ctx, const char **data, uint8_t *reqinst, uint16_t *flags, |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2098 | struct lysp_ext_instance **exts) |
| 2099 | { |
| 2100 | LY_ERR ret = 0; |
| 2101 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 2102 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2103 | enum yang_keyword kw; |
| 2104 | |
| 2105 | if (*flags & LYS_SET_REQINST) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2106 | LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "require-instance"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2107 | return LY_EVALID; |
| 2108 | } |
| 2109 | *flags |= LYS_SET_REQINST; |
| 2110 | |
| 2111 | /* get value */ |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 2112 | ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len); |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 2113 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2114 | |
| 2115 | if ((word_len == 4) && !strncmp(word, "true", word_len)) { |
| 2116 | *reqinst = 1; |
| 2117 | } else if ((word_len != 5) || strncmp(word, "false", word_len)) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2118 | LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "require-instance"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2119 | free(buf); |
| 2120 | return LY_EVALID; |
| 2121 | } |
| 2122 | free(buf); |
| 2123 | |
| 2124 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) { |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 2125 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2126 | |
| 2127 | switch (kw) { |
| 2128 | case YANG_CUSTOM: |
| 2129 | ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_REQINSTANCE, 0, exts); |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 2130 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2131 | break; |
| 2132 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2133 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "require-instance"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2134 | return LY_EVALID; |
| 2135 | } |
| 2136 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 2137 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2138 | |
| 2139 | return ret; |
| 2140 | } |
| 2141 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2142 | /** |
| 2143 | * @brief Parse the modifier statement. Substatement of type pattern statement. |
| 2144 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2145 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2146 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 2147 | * @param[in,out] pat Value to write to. |
| 2148 | * @param[in,out] exts Extension instances to add to. |
| 2149 | * |
| 2150 | * @return LY_ERR values. |
| 2151 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2152 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2153 | parse_type_pattern_modifier(struct ly_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] | 2154 | { |
| 2155 | LY_ERR ret = 0; |
| 2156 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 2157 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2158 | enum yang_keyword kw; |
| 2159 | |
| 2160 | if ((*pat)[0] == 0x15) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2161 | LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "modifier"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2162 | return LY_EVALID; |
| 2163 | } |
| 2164 | |
| 2165 | /* get value */ |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 2166 | ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len); |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 2167 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2168 | |
| 2169 | if ((word_len != 12) || strncmp(word, "invert-match", word_len)) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2170 | LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "modifier"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2171 | free(buf); |
| 2172 | return LY_EVALID; |
| 2173 | } |
| 2174 | free(buf); |
| 2175 | |
| 2176 | /* replace the value in the dictionary */ |
| 2177 | buf = malloc(strlen(*pat) + 1); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2178 | LY_CHECK_ERR_RET(!buf, LOGMEM(ctx->ctx), LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2179 | strcpy(buf, *pat); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2180 | lydict_remove(ctx->ctx, *pat); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2181 | |
| 2182 | assert(buf[0] == 0x06); |
| 2183 | buf[0] = 0x15; |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2184 | *pat = lydict_insert_zc(ctx->ctx, buf); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2185 | |
| 2186 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) { |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 2187 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2188 | |
| 2189 | switch (kw) { |
| 2190 | case YANG_CUSTOM: |
| 2191 | ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_MODIFIER, 0, exts); |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 2192 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2193 | break; |
| 2194 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2195 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "modifier"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2196 | return LY_EVALID; |
| 2197 | } |
| 2198 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 2199 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2200 | |
| 2201 | return ret; |
| 2202 | } |
| 2203 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2204 | /** |
| 2205 | * @brief Parse the pattern statement. Substatement of type statement. |
| 2206 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2207 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2208 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 2209 | * @param[in,out] patterns Restrictions to add to. |
| 2210 | * |
| 2211 | * @return LY_ERR values. |
| 2212 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2213 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2214 | parse_type_pattern(struct ly_parser_ctx *ctx, const char **data, struct lysp_restr **patterns) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2215 | { |
| 2216 | LY_ERR ret = 0; |
| 2217 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 2218 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2219 | enum yang_keyword kw; |
| 2220 | struct lysp_restr *restr; |
| 2221 | |
| 2222 | LYSP_ARRAY_NEW_RET(ctx, patterns, restr, LY_EMEM); |
| 2223 | |
| 2224 | /* get value */ |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 2225 | ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len); |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 2226 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2227 | |
| 2228 | /* add special meaning first byte */ |
| 2229 | if (buf) { |
| 2230 | buf = realloc(buf, word_len + 2); |
| 2231 | word = buf; |
| 2232 | } else { |
| 2233 | buf = malloc(word_len + 2); |
| 2234 | } |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2235 | LY_CHECK_ERR_RET(!buf, LOGMEM(ctx->ctx), LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2236 | memmove(buf + 1, word, word_len + 1); |
| 2237 | word[0] = 0x06; |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2238 | restr->arg = lydict_insert_zc(ctx->ctx, word); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2239 | |
| 2240 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) { |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 2241 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2242 | |
| 2243 | switch (kw) { |
| 2244 | case YANG_DESCRIPTION: |
| 2245 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &restr->dsc, Y_STR_ARG, &restr->exts); |
| 2246 | break; |
| 2247 | case YANG_REFERENCE: |
| 2248 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &restr->ref, Y_STR_ARG, &restr->exts); |
| 2249 | break; |
| 2250 | case YANG_ERROR_APP_TAG: |
| 2251 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_ERRTAG, 0, &restr->eapptag, Y_STR_ARG, &restr->exts); |
| 2252 | break; |
| 2253 | case YANG_ERROR_MESSAGE: |
| 2254 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_ERRMSG, 0, &restr->emsg, Y_STR_ARG, &restr->exts); |
| 2255 | break; |
| 2256 | case YANG_MODIFIER: |
| 2257 | ret = parse_type_pattern_modifier(ctx, data, &restr->arg, &restr->exts); |
| 2258 | break; |
| 2259 | case YANG_CUSTOM: |
| 2260 | ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &restr->exts); |
| 2261 | break; |
| 2262 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2263 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "pattern"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2264 | return LY_EVALID; |
| 2265 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 2266 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2267 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 2268 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2269 | |
| 2270 | return ret; |
| 2271 | } |
| 2272 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2273 | /** |
| 2274 | * @brief Parse the type statement. |
| 2275 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2276 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2277 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 2278 | * @param[in,out] type Type to wrote to. |
| 2279 | * |
| 2280 | * @return LY_ERR values. |
| 2281 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2282 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2283 | parse_type(struct ly_parser_ctx *ctx, const char **data, struct lysp_type *type) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2284 | { |
| 2285 | LY_ERR ret = 0; |
| 2286 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 2287 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2288 | enum yang_keyword kw; |
| 2289 | struct lysp_type *nest_type; |
| 2290 | |
| 2291 | if (type->name) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2292 | LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "type"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2293 | return LY_EVALID; |
| 2294 | } |
| 2295 | |
| 2296 | /* get value */ |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 2297 | ret = get_argument(ctx, data, Y_PREF_IDENTIF_ARG, &word, &buf, &word_len); |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 2298 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2299 | |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2300 | INSERT_WORD(ctx, buf, type->name, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2301 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) { |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 2302 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2303 | |
| 2304 | switch (kw) { |
| 2305 | case YANG_BASE: |
| 2306 | ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_BASE, &type->bases, Y_PREF_IDENTIF_ARG, &type->exts); |
| 2307 | break; |
| 2308 | case YANG_BIT: |
| 2309 | ret = parse_type_enum(ctx, data, kw, &type->bits); |
| 2310 | break; |
| 2311 | case YANG_ENUM: |
| 2312 | ret = parse_type_enum(ctx, data, kw, &type->enums); |
| 2313 | break; |
| 2314 | case YANG_FRACTION_DIGITS: |
| 2315 | ret = parse_type_fracdigits(ctx, data, &type->fraction_digits, &type->exts); |
| 2316 | break; |
| 2317 | case YANG_LENGTH: |
| 2318 | if (type->length) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2319 | LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2320 | return LY_EVALID; |
| 2321 | } |
| 2322 | type->length = calloc(1, sizeof *type->length); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2323 | LY_CHECK_ERR_RET(!type->length, LOGMEM(ctx->ctx), LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2324 | |
| 2325 | ret = parse_restr(ctx, data, kw, type->length); |
| 2326 | break; |
| 2327 | case YANG_PATH: |
| 2328 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_PATH, 0, &type->path, Y_STR_ARG, &type->exts); |
| 2329 | break; |
| 2330 | case YANG_PATTERN: |
| 2331 | ret = parse_type_pattern(ctx, data, &type->patterns); |
| 2332 | break; |
| 2333 | case YANG_RANGE: |
| 2334 | if (type->range) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2335 | LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2336 | return LY_EVALID; |
| 2337 | } |
| 2338 | type->range = calloc(1, sizeof *type->range); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2339 | LY_CHECK_ERR_RET(!type->range, LOGMEM(ctx->ctx), LY_EVALID); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2340 | |
| 2341 | ret = parse_restr(ctx, data, kw, type->range); |
| 2342 | break; |
| 2343 | case YANG_REQUIRE_INSTANCE: |
| 2344 | ret = parse_type_reqinstance(ctx, data, &type->require_instance, &type->flags, &type->exts); |
| 2345 | break; |
| 2346 | case YANG_TYPE: |
| 2347 | { |
| 2348 | LYSP_ARRAY_NEW_RET(ctx, &type->types, nest_type, LY_EMEM); |
| 2349 | } |
| 2350 | ret = parse_type(ctx, data, nest_type); |
| 2351 | break; |
| 2352 | case YANG_CUSTOM: |
| 2353 | ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &type->exts); |
| 2354 | break; |
| 2355 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2356 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "when"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2357 | return LY_EVALID; |
| 2358 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 2359 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2360 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 2361 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2362 | |
| 2363 | return ret; |
| 2364 | } |
| 2365 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2366 | /** |
| 2367 | * @brief Parse the leaf statement. |
| 2368 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2369 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2370 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 2371 | * @param[in,out] siblings Siblings to add to. |
| 2372 | * |
| 2373 | * @return LY_ERR values. |
| 2374 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2375 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2376 | parse_leaf(struct ly_parser_ctx *ctx, const char **data, struct lysp_node **siblings) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2377 | { |
| 2378 | LY_ERR ret = 0; |
| 2379 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 2380 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2381 | enum yang_keyword kw; |
| 2382 | struct lysp_node *iter; |
| 2383 | struct lysp_node_leaf *leaf; |
| 2384 | |
| 2385 | /* create structure */ |
| 2386 | leaf = calloc(1, sizeof *leaf); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2387 | LY_CHECK_ERR_RET(!leaf, LOGMEM(ctx->ctx), LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2388 | leaf->nodetype = LYS_LEAF; |
| 2389 | |
| 2390 | /* insert into siblings */ |
| 2391 | if (!*siblings) { |
| 2392 | *siblings = (struct lysp_node *)leaf; |
| 2393 | } else { |
| 2394 | for (iter = *siblings; iter->next; iter = iter->next); |
| 2395 | iter->next = (struct lysp_node *)leaf; |
| 2396 | } |
| 2397 | |
| 2398 | /* get name */ |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 2399 | ret = get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len); |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 2400 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2401 | |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2402 | INSERT_WORD(ctx, buf, leaf->name, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2403 | |
| 2404 | /* parse substatements */ |
| 2405 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) { |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 2406 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2407 | |
| 2408 | switch (kw) { |
| 2409 | case YANG_CONFIG: |
| 2410 | ret = parse_config(ctx, data, &leaf->flags, &leaf->exts); |
| 2411 | break; |
| 2412 | case YANG_DEFAULT: |
| 2413 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DEFAULT, 0, &leaf->dflt, Y_STR_ARG, &leaf->exts); |
| 2414 | break; |
| 2415 | case YANG_DESCRIPTION: |
| 2416 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &leaf->dsc, Y_STR_ARG, &leaf->exts); |
| 2417 | break; |
| 2418 | case YANG_IF_FEATURE: |
| 2419 | ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &leaf->iffeatures, Y_STR_ARG, &leaf->exts); |
| 2420 | break; |
| 2421 | case YANG_MANDATORY: |
| 2422 | ret = parse_mandatory(ctx, data, &leaf->flags, &leaf->exts); |
| 2423 | break; |
| 2424 | case YANG_MUST: |
| 2425 | ret = parse_restrs(ctx, data, kw, &leaf->musts); |
| 2426 | break; |
| 2427 | case YANG_REFERENCE: |
| 2428 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &leaf->ref, Y_STR_ARG, &leaf->exts); |
| 2429 | break; |
| 2430 | case YANG_STATUS: |
| 2431 | ret = parse_status(ctx, data, &leaf->flags, &leaf->exts); |
| 2432 | break; |
| 2433 | case YANG_TYPE: |
| 2434 | ret = parse_type(ctx, data, &leaf->type); |
| 2435 | break; |
| 2436 | case YANG_UNITS: |
| 2437 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_UNITS, 0, &leaf->units, Y_STR_ARG, &leaf->exts); |
| 2438 | break; |
| 2439 | case YANG_WHEN: |
| 2440 | ret = parse_when(ctx, data, &leaf->when); |
| 2441 | break; |
| 2442 | case YANG_CUSTOM: |
| 2443 | ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &leaf->exts); |
| 2444 | break; |
| 2445 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2446 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "leaf"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2447 | return LY_EVALID; |
| 2448 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 2449 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2450 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 2451 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2452 | |
| 2453 | /* mandatory substatements */ |
| 2454 | if (!leaf->type.name) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2455 | LOGVAL_YANG(ctx, LY_VCODE_MISSTMT, "type", "leaf"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2456 | return LY_EVALID; |
| 2457 | } |
| 2458 | |
| 2459 | return ret; |
| 2460 | } |
| 2461 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2462 | /** |
| 2463 | * @brief Parse the max-elements statement. |
| 2464 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2465 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2466 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 2467 | * @param[in,out] max Value to write to. |
| 2468 | * @param[in,out] flags Flags to write to. |
| 2469 | * @param[in,out] exts Extension instances to add to. |
| 2470 | * |
| 2471 | * @return LY_ERR values. |
| 2472 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2473 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2474 | parse_maxelements(struct ly_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] | 2475 | { |
| 2476 | LY_ERR ret = 0; |
| 2477 | char *buf, *word, *ptr; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 2478 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2479 | unsigned long int num; |
| 2480 | enum yang_keyword kw; |
| 2481 | |
| 2482 | if (*flags & LYS_SET_MAX) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2483 | LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "max-elements"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2484 | return LY_EVALID; |
| 2485 | } |
| 2486 | *flags |= LYS_SET_MAX; |
| 2487 | |
| 2488 | /* get value */ |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 2489 | ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len); |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 2490 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2491 | |
| 2492 | if (!word_len || (word[0] == '0') || ((word[0] != 'u') && !isdigit(word[0]))) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2493 | LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "max-elements"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2494 | free(buf); |
| 2495 | return LY_EVALID; |
| 2496 | } |
| 2497 | |
| 2498 | if (strncmp(word, "unbounded", word_len)) { |
| 2499 | errno = 0; |
| 2500 | num = strtoul(word, &ptr, 10); |
| 2501 | /* we have not parsed the whole argument */ |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 2502 | if ((size_t)(ptr - word) != word_len) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2503 | LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "max-elements"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2504 | free(buf); |
| 2505 | return LY_EVALID; |
| 2506 | } |
| 2507 | if ((errno == ERANGE) || (num > UINT32_MAX)) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2508 | LOGVAL_YANG(ctx, LY_VCODE_OOB, word_len, word, "max-elements"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2509 | free(buf); |
| 2510 | return LY_EVALID; |
| 2511 | } |
| 2512 | |
| 2513 | *max = num; |
| 2514 | } |
| 2515 | free(buf); |
| 2516 | |
| 2517 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) { |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 2518 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2519 | |
| 2520 | switch (kw) { |
| 2521 | case YANG_CUSTOM: |
| 2522 | ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_MAX, 0, exts); |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 2523 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2524 | break; |
| 2525 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2526 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "max-elements"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2527 | return LY_EVALID; |
| 2528 | } |
| 2529 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 2530 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2531 | |
| 2532 | return ret; |
| 2533 | } |
| 2534 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2535 | /** |
| 2536 | * @brief Parse the min-elements statement. |
| 2537 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2538 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2539 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 2540 | * @param[in,out] min Value to write to. |
| 2541 | * @param[in,out] flags Flags to write to. |
| 2542 | * @param[in,out] exts Extension instances to add to. |
| 2543 | * |
| 2544 | * @return LY_ERR values. |
| 2545 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2546 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2547 | parse_minelements(struct ly_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] | 2548 | { |
| 2549 | LY_ERR ret = 0; |
| 2550 | char *buf, *word, *ptr; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 2551 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2552 | unsigned long int num; |
| 2553 | enum yang_keyword kw; |
| 2554 | |
| 2555 | if (*flags & LYS_SET_MIN) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2556 | LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "min-elements"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2557 | return LY_EVALID; |
| 2558 | } |
| 2559 | *flags |= LYS_SET_MIN; |
| 2560 | |
| 2561 | /* get value */ |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 2562 | ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len); |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 2563 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2564 | |
| 2565 | if (!word_len || !isdigit(word[0]) || ((word[0] == '0') && (word_len > 1))) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2566 | LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "min-elements"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2567 | free(buf); |
| 2568 | return LY_EVALID; |
| 2569 | } |
| 2570 | |
| 2571 | errno = 0; |
| 2572 | num = strtoul(word, &ptr, 10); |
| 2573 | /* we have not parsed the whole argument */ |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 2574 | if ((size_t)(ptr - word) != word_len) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2575 | LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "min-elements"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2576 | free(buf); |
| 2577 | return LY_EVALID; |
| 2578 | } |
| 2579 | if ((errno == ERANGE) || (num > UINT32_MAX)) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2580 | LOGVAL_YANG(ctx, LY_VCODE_OOB, word_len, word, "min-elements"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2581 | free(buf); |
| 2582 | return LY_EVALID; |
| 2583 | } |
| 2584 | *min = num; |
| 2585 | free(buf); |
| 2586 | |
| 2587 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) { |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 2588 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2589 | |
| 2590 | switch (kw) { |
| 2591 | case YANG_CUSTOM: |
| 2592 | ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_MIN, 0, exts); |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 2593 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2594 | break; |
| 2595 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2596 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "min-elements"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2597 | return LY_EVALID; |
| 2598 | } |
| 2599 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 2600 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2601 | |
| 2602 | return ret; |
| 2603 | } |
| 2604 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2605 | /** |
| 2606 | * @brief Parse the ordered-by statement. |
| 2607 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2608 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2609 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 2610 | * @param[in,out] flags Flags to write to. |
| 2611 | * @param[in,out] exts Extension instances to add to. |
| 2612 | * |
| 2613 | * @return LY_ERR values. |
| 2614 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2615 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2616 | parse_orderedby(struct ly_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] | 2617 | { |
| 2618 | LY_ERR ret = 0; |
| 2619 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 2620 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2621 | enum yang_keyword kw; |
| 2622 | |
| 2623 | if (*flags & LYS_ORDBY_MASK) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2624 | LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "ordered-by"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2625 | return LY_EVALID; |
| 2626 | } |
| 2627 | |
| 2628 | /* get value */ |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 2629 | ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len); |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 2630 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2631 | |
| 2632 | if ((word_len == 6) && !strncmp(word, "system", word_len)) { |
| 2633 | *flags |= LYS_ORDBY_SYSTEM; |
| 2634 | } else if ((word_len == 4) && !strncmp(word, "user", word_len)) { |
| 2635 | *flags |= LYS_ORDBY_USER; |
| 2636 | } else { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2637 | LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "ordered-by"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2638 | free(buf); |
| 2639 | return LY_EVALID; |
| 2640 | } |
| 2641 | free(buf); |
| 2642 | |
| 2643 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) { |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 2644 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2645 | |
| 2646 | switch (kw) { |
| 2647 | case YANG_CUSTOM: |
| 2648 | ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_ORDEREDBY, 0, exts); |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 2649 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2650 | break; |
| 2651 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2652 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "ordered-by"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2653 | return LY_EVALID; |
| 2654 | } |
| 2655 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 2656 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2657 | |
| 2658 | return ret; |
| 2659 | } |
| 2660 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2661 | /** |
| 2662 | * @brief Parse the leaf-list statement. |
| 2663 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2664 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2665 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 2666 | * @param[in,out] siblings Siblings to add to. |
| 2667 | * |
| 2668 | * @return LY_ERR values. |
| 2669 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2670 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2671 | parse_leaflist(struct ly_parser_ctx *ctx, const char **data, struct lysp_node **siblings) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2672 | { |
| 2673 | LY_ERR ret = 0; |
| 2674 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 2675 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2676 | enum yang_keyword kw; |
| 2677 | struct lysp_node *iter; |
| 2678 | struct lysp_node_leaflist *llist; |
| 2679 | |
| 2680 | /* create structure */ |
| 2681 | llist = calloc(1, sizeof *llist); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2682 | LY_CHECK_ERR_RET(!llist, LOGMEM(ctx->ctx), LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2683 | llist->nodetype = LYS_LEAFLIST; |
| 2684 | |
| 2685 | /* insert into siblings */ |
| 2686 | if (!*siblings) { |
| 2687 | *siblings = (struct lysp_node *)llist; |
| 2688 | } else { |
| 2689 | for (iter = *siblings; iter->next; iter = iter->next); |
| 2690 | iter->next = (struct lysp_node *)llist; |
| 2691 | } |
| 2692 | |
| 2693 | /* get name */ |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 2694 | ret = get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len); |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 2695 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2696 | |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2697 | INSERT_WORD(ctx, buf, llist->name, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2698 | |
| 2699 | /* parse substatements */ |
| 2700 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) { |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 2701 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2702 | |
| 2703 | switch (kw) { |
| 2704 | case YANG_CONFIG: |
| 2705 | ret = parse_config(ctx, data, &llist->flags, &llist->exts); |
| 2706 | break; |
| 2707 | case YANG_DEFAULT: |
| 2708 | ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_DEFAULT, &llist->dflts, Y_STR_ARG, &llist->exts); |
| 2709 | break; |
| 2710 | case YANG_DESCRIPTION: |
| 2711 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &llist->dsc, Y_STR_ARG, &llist->exts); |
| 2712 | break; |
| 2713 | case YANG_IF_FEATURE: |
| 2714 | ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &llist->iffeatures, Y_STR_ARG, &llist->exts); |
| 2715 | break; |
| 2716 | case YANG_MAX_ELEMENTS: |
| 2717 | ret = parse_maxelements(ctx, data, &llist->max, &llist->flags, &llist->exts); |
| 2718 | break; |
| 2719 | case YANG_MIN_ELEMENTS: |
| 2720 | ret = parse_minelements(ctx, data, &llist->min, &llist->flags, &llist->exts); |
| 2721 | break; |
| 2722 | case YANG_MUST: |
| 2723 | ret = parse_restrs(ctx, data, kw, &llist->musts); |
| 2724 | break; |
| 2725 | case YANG_ORDERED_BY: |
| 2726 | ret = parse_orderedby(ctx, data, &llist->flags, &llist->exts); |
| 2727 | break; |
| 2728 | case YANG_REFERENCE: |
| 2729 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &llist->ref, Y_STR_ARG, &llist->exts); |
| 2730 | break; |
| 2731 | case YANG_STATUS: |
| 2732 | ret = parse_status(ctx, data, &llist->flags, &llist->exts); |
| 2733 | break; |
| 2734 | case YANG_TYPE: |
| 2735 | ret = parse_type(ctx, data, &llist->type); |
| 2736 | break; |
| 2737 | case YANG_UNITS: |
| 2738 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_UNITS, 0, &llist->units, Y_STR_ARG, &llist->exts); |
| 2739 | break; |
| 2740 | case YANG_WHEN: |
| 2741 | ret = parse_when(ctx, data, &llist->when); |
| 2742 | break; |
| 2743 | case YANG_CUSTOM: |
| 2744 | ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &llist->exts); |
| 2745 | break; |
| 2746 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2747 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "llist"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2748 | return LY_EVALID; |
| 2749 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 2750 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2751 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 2752 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2753 | |
| 2754 | /* mandatory substatements */ |
| 2755 | if (!llist->type.name) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2756 | LOGVAL_YANG(ctx, LY_VCODE_MISSTMT, "type", "leaf-list"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2757 | return LY_EVALID; |
| 2758 | } |
| 2759 | |
| 2760 | return ret; |
| 2761 | } |
| 2762 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2763 | /** |
| 2764 | * @brief Parse the refine statement. |
| 2765 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2766 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2767 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 2768 | * @param[in,out] refines Refines to add to. |
| 2769 | * |
| 2770 | * @return LY_ERR values. |
| 2771 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2772 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2773 | parse_refine(struct ly_parser_ctx *ctx, const char **data, struct lysp_refine **refines) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2774 | { |
| 2775 | LY_ERR ret = 0; |
| 2776 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 2777 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2778 | enum yang_keyword kw; |
| 2779 | struct lysp_refine *rf; |
| 2780 | |
| 2781 | LYSP_ARRAY_NEW_RET(ctx, refines, rf, LY_EMEM); |
| 2782 | |
| 2783 | /* get value */ |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 2784 | ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len); |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 2785 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2786 | |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2787 | INSERT_WORD(ctx, buf, rf->nodeid, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2788 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) { |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 2789 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2790 | |
| 2791 | switch (kw) { |
| 2792 | case YANG_CONFIG: |
| 2793 | ret = parse_config(ctx, data, &rf->flags, &rf->exts); |
| 2794 | break; |
| 2795 | case YANG_DEFAULT: |
| 2796 | ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_DEFAULT, &rf->dflts, Y_STR_ARG, &rf->exts); |
| 2797 | break; |
| 2798 | case YANG_DESCRIPTION: |
| 2799 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &rf->dsc, Y_STR_ARG, &rf->exts); |
| 2800 | break; |
| 2801 | case YANG_IF_FEATURE: |
| 2802 | ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &rf->iffeatures, Y_STR_ARG, &rf->exts); |
| 2803 | break; |
| 2804 | case YANG_MAX_ELEMENTS: |
| 2805 | ret = parse_maxelements(ctx, data, &rf->max, &rf->flags, &rf->exts); |
| 2806 | break; |
| 2807 | case YANG_MIN_ELEMENTS: |
| 2808 | ret = parse_minelements(ctx, data, &rf->min, &rf->flags, &rf->exts); |
| 2809 | break; |
| 2810 | case YANG_MUST: |
| 2811 | ret = parse_restrs(ctx, data, kw, &rf->musts); |
| 2812 | break; |
| 2813 | case YANG_MANDATORY: |
| 2814 | ret = parse_mandatory(ctx, data, &rf->flags, &rf->exts); |
| 2815 | break; |
| 2816 | case YANG_REFERENCE: |
| 2817 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &rf->ref, Y_STR_ARG, &rf->exts); |
| 2818 | break; |
| 2819 | case YANG_PRESENCE: |
| 2820 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_PRESENCE, 0, &rf->presence, Y_STR_ARG, &rf->exts); |
| 2821 | break; |
| 2822 | case YANG_CUSTOM: |
| 2823 | ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &rf->exts); |
| 2824 | break; |
| 2825 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2826 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "refine"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2827 | return LY_EVALID; |
| 2828 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 2829 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2830 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 2831 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2832 | |
| 2833 | return ret; |
| 2834 | } |
| 2835 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2836 | /** |
| 2837 | * @brief Parse the typedef statement. |
| 2838 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2839 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2840 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 2841 | * @param[in,out] typedefs Typedefs to add to. |
| 2842 | * |
| 2843 | * @return LY_ERR values. |
| 2844 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2845 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2846 | parse_typedef(struct ly_parser_ctx *ctx, const char **data, struct lysp_tpdf **typedefs) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2847 | { |
| 2848 | LY_ERR ret = 0; |
| 2849 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 2850 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2851 | enum yang_keyword kw; |
| 2852 | struct lysp_tpdf *tpdf; |
| 2853 | |
| 2854 | LYSP_ARRAY_NEW_RET(ctx, typedefs, tpdf, LY_EMEM); |
| 2855 | |
| 2856 | /* get value */ |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 2857 | ret = get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len); |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 2858 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2859 | |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2860 | INSERT_WORD(ctx, buf, tpdf->name, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2861 | |
| 2862 | /* parse substatements */ |
| 2863 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) { |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 2864 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2865 | |
| 2866 | switch (kw) { |
| 2867 | case YANG_DEFAULT: |
| 2868 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DEFAULT, 0, &tpdf->dflt, Y_STR_ARG, &tpdf->exts); |
| 2869 | break; |
| 2870 | case YANG_DESCRIPTION: |
| 2871 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &tpdf->dsc, Y_STR_ARG, &tpdf->exts); |
| 2872 | break; |
| 2873 | case YANG_REFERENCE: |
| 2874 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &tpdf->ref, Y_STR_ARG, &tpdf->exts); |
| 2875 | break; |
| 2876 | case YANG_STATUS: |
| 2877 | ret = parse_status(ctx, data, &tpdf->flags, &tpdf->exts); |
| 2878 | break; |
| 2879 | case YANG_TYPE: |
| 2880 | ret = parse_type(ctx, data, &tpdf->type); |
| 2881 | break; |
| 2882 | case YANG_UNITS: |
| 2883 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_UNITS, 0, &tpdf->units, Y_STR_ARG, &tpdf->exts); |
| 2884 | break; |
| 2885 | case YANG_CUSTOM: |
| 2886 | ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &tpdf->exts); |
| 2887 | break; |
| 2888 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2889 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "typedef"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2890 | return LY_EVALID; |
| 2891 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 2892 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2893 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 2894 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2895 | |
| 2896 | /* mandatory substatements */ |
| 2897 | if (!tpdf->type.name) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2898 | LOGVAL_YANG(ctx, LY_VCODE_MISSTMT, "type", "typedef"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2899 | return LY_EVALID; |
| 2900 | } |
| 2901 | |
| 2902 | return ret; |
| 2903 | } |
| 2904 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2905 | /** |
| 2906 | * @brief Parse the input or output statement. |
| 2907 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2908 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2909 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 2910 | * @param[in] kw Type of this particular keyword |
| 2911 | * @param[in,out] inout_p Input/output pointer to write to. |
| 2912 | * |
| 2913 | * @return LY_ERR values. |
| 2914 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2915 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2916 | parse_inout(struct ly_parser_ctx *ctx, const char **data, enum yang_keyword kw, struct lysp_action_inout **inout_p) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2917 | { |
| 2918 | LY_ERR ret = 0; |
| 2919 | char *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 2920 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2921 | struct lysp_action_inout *inout; |
| 2922 | |
| 2923 | if (*inout_p) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2924 | LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2925 | return LY_EVALID; |
| 2926 | } |
| 2927 | |
| 2928 | /* create structure */ |
| 2929 | inout = calloc(1, sizeof *inout); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2930 | LY_CHECK_ERR_RET(!inout, LOGMEM(ctx->ctx), LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2931 | *inout_p = inout; |
| 2932 | |
| 2933 | /* parse substatements */ |
| 2934 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) { |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 2935 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2936 | |
| 2937 | switch (kw) { |
| 2938 | case YANG_ANYDATA: |
| 2939 | case YANG_ANYXML: |
| 2940 | ret = parse_any(ctx, data, kw, &inout->data); |
| 2941 | break; |
| 2942 | case YANG_CHOICE: |
| 2943 | ret = parse_choice(ctx, data, &inout->data); |
| 2944 | break; |
| 2945 | case YANG_CONTAINER: |
| 2946 | ret = parse_container(ctx, data, &inout->data); |
| 2947 | break; |
| 2948 | case YANG_LEAF: |
| 2949 | ret = parse_leaf(ctx, data, &inout->data); |
| 2950 | break; |
| 2951 | case YANG_LEAF_LIST: |
| 2952 | ret = parse_leaflist(ctx, data, &inout->data); |
| 2953 | break; |
| 2954 | case YANG_LIST: |
| 2955 | ret = parse_list(ctx, data, &inout->data); |
| 2956 | break; |
| 2957 | case YANG_USES: |
| 2958 | ret = parse_uses(ctx, data, &inout->data); |
| 2959 | break; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2960 | case YANG_TYPEDEF: |
| 2961 | ret = parse_typedef(ctx, data, &inout->typedefs); |
| 2962 | break; |
| 2963 | case YANG_MUST: |
| 2964 | ret = parse_restrs(ctx, data, kw, &inout->musts); |
| 2965 | break; |
| 2966 | case YANG_GROUPING: |
| 2967 | ret = parse_grouping(ctx, data, &inout->groupings); |
| 2968 | break; |
| 2969 | case YANG_CUSTOM: |
| 2970 | ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &inout->exts); |
| 2971 | break; |
| 2972 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2973 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "input/output"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2974 | return LY_EVALID; |
| 2975 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 2976 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2977 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 2978 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2979 | |
| 2980 | return ret; |
| 2981 | } |
| 2982 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2983 | /** |
| 2984 | * @brief Parse the action statement. |
| 2985 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2986 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2987 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 2988 | * @param[in,out] actions Actions to add to. |
| 2989 | * |
| 2990 | * @return LY_ERR values. |
| 2991 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2992 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2993 | parse_action(struct ly_parser_ctx *ctx, const char **data, struct lysp_action **actions) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2994 | { |
| 2995 | LY_ERR ret = 0; |
| 2996 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 2997 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2998 | enum yang_keyword kw; |
| 2999 | struct lysp_action *act; |
| 3000 | |
| 3001 | LYSP_ARRAY_NEW_RET(ctx, actions, act, LY_EMEM); |
| 3002 | |
| 3003 | /* get value */ |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 3004 | ret = get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len); |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 3005 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3006 | |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3007 | INSERT_WORD(ctx, buf, act->name, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3008 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) { |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 3009 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3010 | |
| 3011 | switch (kw) { |
| 3012 | case YANG_DESCRIPTION: |
| 3013 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &act->dsc, Y_STR_ARG, &act->exts); |
| 3014 | break; |
| 3015 | case YANG_IF_FEATURE: |
| 3016 | ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &act->iffeatures, Y_STR_ARG, &act->exts); |
| 3017 | break; |
| 3018 | case YANG_REFERENCE: |
| 3019 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &act->ref, Y_STR_ARG, &act->exts); |
| 3020 | break; |
| 3021 | case YANG_STATUS: |
| 3022 | ret = parse_status(ctx, data, &act->flags, &act->exts); |
| 3023 | break; |
| 3024 | |
| 3025 | case YANG_INPUT: |
| 3026 | ret = parse_inout(ctx, data, kw, &act->input); |
| 3027 | break; |
| 3028 | case YANG_OUTPUT: |
| 3029 | ret = parse_inout(ctx, data, kw, &act->output); |
| 3030 | break; |
| 3031 | |
| 3032 | case YANG_TYPEDEF: |
| 3033 | ret = parse_typedef(ctx, data, &act->typedefs); |
| 3034 | break; |
| 3035 | case YANG_GROUPING: |
| 3036 | ret = parse_grouping(ctx, data, &act->groupings); |
| 3037 | break; |
| 3038 | case YANG_CUSTOM: |
| 3039 | ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &act->exts); |
| 3040 | break; |
| 3041 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3042 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "action"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3043 | return LY_EVALID; |
| 3044 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 3045 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3046 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 3047 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3048 | |
| 3049 | return ret; |
| 3050 | } |
| 3051 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3052 | /** |
| 3053 | * @brief Parse the notification statement. |
| 3054 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3055 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3056 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 3057 | * @param[in,out] notifs Notifications to add to. |
| 3058 | * |
| 3059 | * @return LY_ERR values. |
| 3060 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3061 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3062 | parse_notif(struct ly_parser_ctx *ctx, const char **data, struct lysp_notif **notifs) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3063 | { |
| 3064 | LY_ERR ret = 0; |
| 3065 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 3066 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3067 | enum yang_keyword kw; |
| 3068 | struct lysp_notif *notif; |
| 3069 | |
| 3070 | LYSP_ARRAY_NEW_RET(ctx, notifs, notif, LY_EMEM); |
| 3071 | |
| 3072 | /* get value */ |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 3073 | ret = get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len); |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 3074 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3075 | |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3076 | INSERT_WORD(ctx, buf, notif->name, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3077 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) { |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 3078 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3079 | |
| 3080 | switch (kw) { |
| 3081 | case YANG_DESCRIPTION: |
| 3082 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, ¬if->dsc, Y_STR_ARG, ¬if->exts); |
| 3083 | break; |
| 3084 | case YANG_IF_FEATURE: |
| 3085 | ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, ¬if->iffeatures, Y_STR_ARG, ¬if->exts); |
| 3086 | break; |
| 3087 | case YANG_REFERENCE: |
| 3088 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, ¬if->ref, Y_STR_ARG, ¬if->exts); |
| 3089 | break; |
| 3090 | case YANG_STATUS: |
| 3091 | ret = parse_status(ctx, data, ¬if->flags, ¬if->exts); |
| 3092 | break; |
| 3093 | |
| 3094 | case YANG_ANYDATA: |
| 3095 | case YANG_ANYXML: |
| 3096 | ret = parse_any(ctx, data, kw, ¬if->data); |
| 3097 | break; |
| 3098 | case YANG_CHOICE: |
| 3099 | ret = parse_case(ctx, data, ¬if->data); |
| 3100 | break; |
| 3101 | case YANG_CONTAINER: |
| 3102 | ret = parse_container(ctx, data, ¬if->data); |
| 3103 | break; |
| 3104 | case YANG_LEAF: |
| 3105 | ret = parse_leaf(ctx, data, ¬if->data); |
| 3106 | break; |
| 3107 | case YANG_LEAF_LIST: |
| 3108 | ret = parse_leaflist(ctx, data, ¬if->data); |
| 3109 | break; |
| 3110 | case YANG_LIST: |
| 3111 | ret = parse_list(ctx, data, ¬if->data); |
| 3112 | break; |
| 3113 | case YANG_USES: |
| 3114 | ret = parse_uses(ctx, data, ¬if->data); |
| 3115 | break; |
| 3116 | |
| 3117 | case YANG_MUST: |
| 3118 | ret = parse_restrs(ctx, data, kw, ¬if->musts); |
| 3119 | break; |
| 3120 | case YANG_TYPEDEF: |
| 3121 | ret = parse_typedef(ctx, data, ¬if->typedefs); |
| 3122 | break; |
| 3123 | case YANG_GROUPING: |
| 3124 | ret = parse_grouping(ctx, data, ¬if->groupings); |
| 3125 | break; |
| 3126 | case YANG_CUSTOM: |
| 3127 | ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, ¬if->exts); |
| 3128 | break; |
| 3129 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3130 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "notification"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3131 | return LY_EVALID; |
| 3132 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 3133 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3134 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 3135 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3136 | |
| 3137 | return ret; |
| 3138 | } |
| 3139 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3140 | /** |
| 3141 | * @brief Parse the grouping statement. |
| 3142 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3143 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3144 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 3145 | * @param[in,out] groupings Groupings to add to. |
| 3146 | * |
| 3147 | * @return LY_ERR values. |
| 3148 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3149 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3150 | parse_grouping(struct ly_parser_ctx *ctx, const char **data, struct lysp_grp **groupings) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3151 | { |
| 3152 | LY_ERR ret = 0; |
| 3153 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 3154 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3155 | enum yang_keyword kw; |
| 3156 | struct lysp_grp *grp; |
| 3157 | |
| 3158 | LYSP_ARRAY_NEW_RET(ctx, groupings, grp, LY_EMEM); |
| 3159 | |
| 3160 | /* get value */ |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 3161 | ret = get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len); |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 3162 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3163 | |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3164 | INSERT_WORD(ctx, buf, grp->name, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3165 | |
| 3166 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) { |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 3167 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3168 | |
| 3169 | switch (kw) { |
| 3170 | case YANG_DESCRIPTION: |
| 3171 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &grp->dsc, Y_STR_ARG, &grp->exts); |
| 3172 | break; |
| 3173 | case YANG_REFERENCE: |
| 3174 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &grp->ref, Y_STR_ARG, &grp->exts); |
| 3175 | break; |
| 3176 | case YANG_STATUS: |
| 3177 | ret = parse_status(ctx, data, &grp->flags, &grp->exts); |
| 3178 | break; |
| 3179 | |
| 3180 | case YANG_ANYDATA: |
| 3181 | case YANG_ANYXML: |
| 3182 | ret = parse_any(ctx, data, kw, &grp->data); |
| 3183 | break; |
| 3184 | case YANG_CHOICE: |
| 3185 | ret = parse_choice(ctx, data, &grp->data); |
| 3186 | break; |
| 3187 | case YANG_CONTAINER: |
| 3188 | ret = parse_container(ctx, data, &grp->data); |
| 3189 | break; |
| 3190 | case YANG_LEAF: |
| 3191 | ret = parse_leaf(ctx, data, &grp->data); |
| 3192 | break; |
| 3193 | case YANG_LEAF_LIST: |
| 3194 | ret = parse_leaflist(ctx, data, &grp->data); |
| 3195 | break; |
| 3196 | case YANG_LIST: |
| 3197 | ret = parse_list(ctx, data, &grp->data); |
| 3198 | break; |
| 3199 | case YANG_USES: |
| 3200 | ret = parse_uses(ctx, data, &grp->data); |
| 3201 | break; |
| 3202 | |
| 3203 | case YANG_TYPEDEF: |
| 3204 | ret = parse_typedef(ctx, data, &grp->typedefs); |
| 3205 | break; |
| 3206 | case YANG_ACTION: |
| 3207 | ret = parse_action(ctx, data, &grp->actions); |
| 3208 | break; |
| 3209 | case YANG_GROUPING: |
| 3210 | ret = parse_grouping(ctx, data, &grp->groupings); |
| 3211 | break; |
| 3212 | case YANG_NOTIFICATION: |
| 3213 | ret = parse_notif(ctx, data, &grp->notifs); |
| 3214 | break; |
| 3215 | case YANG_CUSTOM: |
| 3216 | ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &grp->exts); |
| 3217 | break; |
| 3218 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3219 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "augment"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3220 | return LY_EVALID; |
| 3221 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 3222 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3223 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 3224 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3225 | |
| 3226 | return ret; |
| 3227 | } |
| 3228 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3229 | /** |
| 3230 | * @brief Parse the refine statement. |
| 3231 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3232 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3233 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 3234 | * @param[in,out] augments Augments to add to. |
| 3235 | * |
| 3236 | * @return LY_ERR values. |
| 3237 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3238 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3239 | parse_augment(struct ly_parser_ctx *ctx, const char **data, struct lysp_augment **augments) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3240 | { |
| 3241 | LY_ERR ret = 0; |
| 3242 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 3243 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3244 | enum yang_keyword kw; |
| 3245 | struct lysp_augment *aug; |
| 3246 | |
| 3247 | LYSP_ARRAY_NEW_RET(ctx, augments, aug, LY_EMEM); |
| 3248 | |
| 3249 | /* get value */ |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 3250 | ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len); |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 3251 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3252 | |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3253 | INSERT_WORD(ctx, buf, aug->nodeid, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3254 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) { |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 3255 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3256 | |
| 3257 | switch (kw) { |
| 3258 | case YANG_DESCRIPTION: |
| 3259 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &aug->dsc, Y_STR_ARG, &aug->exts); |
| 3260 | break; |
| 3261 | case YANG_IF_FEATURE: |
| 3262 | ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &aug->iffeatures, Y_STR_ARG, &aug->exts); |
| 3263 | break; |
| 3264 | case YANG_REFERENCE: |
| 3265 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &aug->ref, Y_STR_ARG, &aug->exts); |
| 3266 | break; |
| 3267 | case YANG_STATUS: |
| 3268 | ret = parse_status(ctx, data, &aug->flags, &aug->exts); |
| 3269 | break; |
| 3270 | case YANG_WHEN: |
| 3271 | ret = parse_when(ctx, data, &aug->when); |
| 3272 | break; |
| 3273 | |
| 3274 | case YANG_ANYDATA: |
| 3275 | case YANG_ANYXML: |
| 3276 | ret = parse_any(ctx, data, kw, &aug->child); |
| 3277 | break; |
| 3278 | case YANG_CASE: |
| 3279 | ret = parse_case(ctx, data, &aug->child); |
| 3280 | break; |
| 3281 | case YANG_CHOICE: |
| 3282 | ret = parse_choice(ctx, data, &aug->child); |
| 3283 | break; |
| 3284 | case YANG_CONTAINER: |
| 3285 | ret = parse_container(ctx, data, &aug->child); |
| 3286 | break; |
| 3287 | case YANG_LEAF: |
| 3288 | ret = parse_leaf(ctx, data, &aug->child); |
| 3289 | break; |
| 3290 | case YANG_LEAF_LIST: |
| 3291 | ret = parse_leaflist(ctx, data, &aug->child); |
| 3292 | break; |
| 3293 | case YANG_LIST: |
| 3294 | ret = parse_list(ctx, data, &aug->child); |
| 3295 | break; |
| 3296 | case YANG_USES: |
| 3297 | ret = parse_uses(ctx, data, &aug->child); |
| 3298 | break; |
| 3299 | |
| 3300 | case YANG_ACTION: |
| 3301 | ret = parse_action(ctx, data, &aug->actions); |
| 3302 | break; |
| 3303 | case YANG_NOTIFICATION: |
| 3304 | ret = parse_notif(ctx, data, &aug->notifs); |
| 3305 | break; |
| 3306 | case YANG_CUSTOM: |
| 3307 | ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &aug->exts); |
| 3308 | break; |
| 3309 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3310 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "augment"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3311 | return LY_EVALID; |
| 3312 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 3313 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3314 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 3315 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3316 | |
| 3317 | return ret; |
| 3318 | } |
| 3319 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3320 | /** |
| 3321 | * @brief Parse the uses statement. |
| 3322 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3323 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3324 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 3325 | * @param[in,out] siblings Siblings to add to. |
| 3326 | * |
| 3327 | * @return LY_ERR values. |
| 3328 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3329 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3330 | parse_uses(struct ly_parser_ctx *ctx, const char **data, struct lysp_node **siblings) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3331 | { |
| 3332 | LY_ERR ret = 0; |
| 3333 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 3334 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3335 | enum yang_keyword kw; |
| 3336 | struct lysp_node *iter; |
| 3337 | struct lysp_node_uses *uses; |
| 3338 | |
| 3339 | /* create structure */ |
| 3340 | uses = calloc(1, sizeof *uses); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3341 | LY_CHECK_ERR_RET(!uses, LOGMEM(ctx->ctx), LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3342 | uses->nodetype = LYS_USES; |
| 3343 | |
| 3344 | /* insert into siblings */ |
| 3345 | if (!*siblings) { |
| 3346 | *siblings = (struct lysp_node *)uses; |
| 3347 | } else { |
| 3348 | for (iter = *siblings; iter->next; iter = iter->next); |
| 3349 | iter->next = (struct lysp_node *)uses; |
| 3350 | } |
| 3351 | |
| 3352 | /* get name */ |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 3353 | ret = get_argument(ctx, data, Y_PREF_IDENTIF_ARG, &word, &buf, &word_len); |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 3354 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3355 | |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3356 | INSERT_WORD(ctx, buf, uses->name, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3357 | |
| 3358 | /* parse substatements */ |
| 3359 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) { |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 3360 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3361 | |
| 3362 | switch (kw) { |
| 3363 | case YANG_DESCRIPTION: |
| 3364 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &uses->dsc, Y_STR_ARG, &uses->exts); |
| 3365 | break; |
| 3366 | case YANG_IF_FEATURE: |
| 3367 | ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &uses->iffeatures, Y_STR_ARG, &uses->exts); |
| 3368 | break; |
| 3369 | case YANG_REFERENCE: |
| 3370 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &uses->ref, Y_STR_ARG, &uses->exts); |
| 3371 | break; |
| 3372 | case YANG_STATUS: |
| 3373 | ret = parse_status(ctx, data, &uses->flags, &uses->exts); |
| 3374 | break; |
| 3375 | case YANG_WHEN: |
| 3376 | ret = parse_when(ctx, data, &uses->when); |
| 3377 | break; |
| 3378 | |
| 3379 | case YANG_REFINE: |
| 3380 | ret = parse_refine(ctx, data, &uses->refines); |
| 3381 | break; |
| 3382 | case YANG_AUGMENT: |
| 3383 | ret = parse_augment(ctx, data, &uses->augments); |
| 3384 | break; |
| 3385 | case YANG_CUSTOM: |
| 3386 | ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &uses->exts); |
| 3387 | break; |
| 3388 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3389 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "uses"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3390 | return LY_EVALID; |
| 3391 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 3392 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3393 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 3394 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3395 | |
| 3396 | return ret; |
| 3397 | } |
| 3398 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3399 | /** |
| 3400 | * @brief Parse the case statement. |
| 3401 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3402 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3403 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 3404 | * @param[in,out] siblings Siblings to add to. |
| 3405 | * |
| 3406 | * @return LY_ERR values. |
| 3407 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3408 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3409 | parse_case(struct ly_parser_ctx *ctx, const char **data, struct lysp_node **siblings) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3410 | { |
| 3411 | LY_ERR ret = 0; |
| 3412 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 3413 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3414 | enum yang_keyword kw; |
| 3415 | struct lysp_node *iter; |
| 3416 | struct lysp_node_case *cas; |
| 3417 | |
| 3418 | /* create structure */ |
| 3419 | cas = calloc(1, sizeof *cas); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3420 | LY_CHECK_ERR_RET(!cas, LOGMEM(ctx->ctx), LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3421 | cas->nodetype = LYS_CASE; |
| 3422 | |
| 3423 | /* insert into siblings */ |
| 3424 | if (!*siblings) { |
| 3425 | *siblings = (struct lysp_node *)cas; |
| 3426 | } else { |
| 3427 | for (iter = *siblings; iter->next; iter = iter->next); |
| 3428 | iter->next = (struct lysp_node *)cas; |
| 3429 | } |
| 3430 | |
| 3431 | /* get name */ |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 3432 | ret = get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len); |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 3433 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3434 | |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3435 | INSERT_WORD(ctx, buf, cas->name, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3436 | |
| 3437 | /* parse substatements */ |
| 3438 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) { |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 3439 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3440 | |
| 3441 | switch (kw) { |
| 3442 | case YANG_DESCRIPTION: |
| 3443 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &cas->dsc, Y_STR_ARG, &cas->exts); |
| 3444 | break; |
| 3445 | case YANG_IF_FEATURE: |
| 3446 | ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &cas->iffeatures, Y_STR_ARG, &cas->exts); |
| 3447 | break; |
| 3448 | case YANG_REFERENCE: |
| 3449 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &cas->ref, Y_STR_ARG, &cas->exts); |
| 3450 | break; |
| 3451 | case YANG_STATUS: |
| 3452 | ret = parse_status(ctx, data, &cas->flags, &cas->exts); |
| 3453 | break; |
| 3454 | case YANG_WHEN: |
| 3455 | ret = parse_when(ctx, data, &cas->when); |
| 3456 | break; |
| 3457 | |
| 3458 | case YANG_ANYDATA: |
| 3459 | case YANG_ANYXML: |
| 3460 | ret = parse_any(ctx, data, kw, &cas->child); |
| 3461 | break; |
| 3462 | case YANG_CHOICE: |
| 3463 | ret = parse_case(ctx, data, &cas->child); |
| 3464 | break; |
| 3465 | case YANG_CONTAINER: |
| 3466 | ret = parse_container(ctx, data, &cas->child); |
| 3467 | break; |
| 3468 | case YANG_LEAF: |
| 3469 | ret = parse_leaf(ctx, data, &cas->child); |
| 3470 | break; |
| 3471 | case YANG_LEAF_LIST: |
| 3472 | ret = parse_leaflist(ctx, data, &cas->child); |
| 3473 | break; |
| 3474 | case YANG_LIST: |
| 3475 | ret = parse_list(ctx, data, &cas->child); |
| 3476 | break; |
| 3477 | case YANG_USES: |
| 3478 | ret = parse_uses(ctx, data, &cas->child); |
| 3479 | break; |
| 3480 | case YANG_CUSTOM: |
| 3481 | ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &cas->exts); |
| 3482 | break; |
| 3483 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3484 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "case"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3485 | return LY_EVALID; |
| 3486 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 3487 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3488 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 3489 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3490 | |
| 3491 | return ret; |
| 3492 | } |
| 3493 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3494 | /** |
| 3495 | * @brief Parse the choice statement. |
| 3496 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3497 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3498 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 3499 | * @param[in,out] siblings Siblings to add to. |
| 3500 | * |
| 3501 | * @return LY_ERR values. |
| 3502 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3503 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3504 | parse_choice(struct ly_parser_ctx *ctx, const char **data, struct lysp_node **siblings) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3505 | { |
| 3506 | LY_ERR ret = 0; |
| 3507 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 3508 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3509 | enum yang_keyword kw; |
| 3510 | struct lysp_node *iter; |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3511 | struct lysp_node_choice *choice; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3512 | |
| 3513 | /* create structure */ |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3514 | choice = calloc(1, sizeof *choice); |
| 3515 | LY_CHECK_ERR_RET(!choice, LOGMEM(ctx->ctx), LY_EMEM); |
| 3516 | choice->nodetype = LYS_CHOICE; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3517 | |
| 3518 | /* insert into siblings */ |
| 3519 | if (!*siblings) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3520 | *siblings = (struct lysp_node *)choice; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3521 | } else { |
| 3522 | for (iter = *siblings; iter->next; iter = iter->next); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3523 | iter->next = (struct lysp_node *)choice; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3524 | } |
| 3525 | |
| 3526 | /* get name */ |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 3527 | ret = get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len); |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 3528 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3529 | |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3530 | INSERT_WORD(ctx, buf, choice->name, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3531 | |
| 3532 | /* parse substatements */ |
| 3533 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) { |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 3534 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3535 | |
| 3536 | switch (kw) { |
| 3537 | case YANG_CONFIG: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3538 | ret = parse_config(ctx, data, &choice->flags, &choice->exts); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3539 | break; |
| 3540 | case YANG_DESCRIPTION: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3541 | 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] | 3542 | break; |
| 3543 | case YANG_IF_FEATURE: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3544 | 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] | 3545 | break; |
| 3546 | case YANG_MANDATORY: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3547 | ret = parse_mandatory(ctx, data, &choice->flags, &choice->exts); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3548 | break; |
| 3549 | case YANG_REFERENCE: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3550 | 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] | 3551 | break; |
| 3552 | case YANG_STATUS: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3553 | ret = parse_status(ctx, data, &choice->flags, &choice->exts); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3554 | break; |
| 3555 | case YANG_WHEN: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3556 | ret = parse_when(ctx, data, &choice->when); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3557 | break; |
| 3558 | case YANG_DEFAULT: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3559 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DEFAULT, 0, &choice->dflt, Y_IDENTIF_ARG, &choice->exts); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3560 | break; |
| 3561 | |
| 3562 | case YANG_ANYDATA: |
| 3563 | case YANG_ANYXML: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3564 | ret = parse_any(ctx, data, kw, &choice->child); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3565 | break; |
| 3566 | case YANG_CASE: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3567 | ret = parse_case(ctx, data, &choice->child); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3568 | break; |
| 3569 | case YANG_CHOICE: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3570 | ret = parse_choice(ctx, data, &choice->child); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3571 | break; |
| 3572 | case YANG_CONTAINER: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3573 | ret = parse_container(ctx, data, &choice->child); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3574 | break; |
| 3575 | case YANG_LEAF: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3576 | ret = parse_leaf(ctx, data, &choice->child); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3577 | break; |
| 3578 | case YANG_LEAF_LIST: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3579 | ret = parse_leaflist(ctx, data, &choice->child); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3580 | break; |
| 3581 | case YANG_LIST: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3582 | ret = parse_list(ctx, data, &choice->child); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3583 | break; |
| 3584 | case YANG_CUSTOM: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3585 | 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] | 3586 | break; |
| 3587 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3588 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "choice"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3589 | return LY_EVALID; |
| 3590 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 3591 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3592 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 3593 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3594 | |
| 3595 | return ret; |
| 3596 | } |
| 3597 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3598 | /** |
| 3599 | * @brief Parse the container statement. |
| 3600 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3601 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3602 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 3603 | * @param[in,out] siblings Siblings to add to. |
| 3604 | * |
| 3605 | * @return LY_ERR values. |
| 3606 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3607 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3608 | parse_container(struct ly_parser_ctx *ctx, const char **data, struct lysp_node **siblings) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3609 | { |
| 3610 | LY_ERR ret = 0; |
| 3611 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 3612 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3613 | enum yang_keyword kw; |
| 3614 | struct lysp_node *iter; |
| 3615 | struct lysp_node_container *cont; |
| 3616 | |
| 3617 | /* create structure */ |
| 3618 | cont = calloc(1, sizeof *cont); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3619 | LY_CHECK_ERR_RET(!cont, LOGMEM(ctx->ctx), LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3620 | cont->nodetype = LYS_CONTAINER; |
| 3621 | |
| 3622 | /* insert into siblings */ |
| 3623 | if (!*siblings) { |
| 3624 | *siblings = (struct lysp_node *)cont; |
| 3625 | } else { |
| 3626 | for (iter = *siblings; iter->next; iter = iter->next); |
| 3627 | iter->next = (struct lysp_node *)cont; |
| 3628 | } |
| 3629 | |
| 3630 | /* get name */ |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 3631 | ret = get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len); |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 3632 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3633 | |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3634 | INSERT_WORD(ctx, buf, cont->name, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3635 | |
| 3636 | /* parse substatements */ |
| 3637 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) { |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 3638 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3639 | |
| 3640 | switch (kw) { |
| 3641 | case YANG_CONFIG: |
| 3642 | ret = parse_config(ctx, data, &cont->flags, &cont->exts); |
| 3643 | break; |
| 3644 | case YANG_DESCRIPTION: |
| 3645 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &cont->dsc, Y_STR_ARG, &cont->exts); |
| 3646 | break; |
| 3647 | case YANG_IF_FEATURE: |
| 3648 | ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &cont->iffeatures, Y_STR_ARG, &cont->exts); |
| 3649 | break; |
| 3650 | case YANG_REFERENCE: |
| 3651 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &cont->ref, Y_STR_ARG, &cont->exts); |
| 3652 | break; |
| 3653 | case YANG_STATUS: |
| 3654 | ret = parse_status(ctx, data, &cont->flags, &cont->exts); |
| 3655 | break; |
| 3656 | case YANG_WHEN: |
| 3657 | ret = parse_when(ctx, data, &cont->when); |
| 3658 | break; |
| 3659 | case YANG_PRESENCE: |
| 3660 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_PRESENCE, 0, &cont->presence, Y_STR_ARG, &cont->exts); |
| 3661 | break; |
| 3662 | |
| 3663 | case YANG_ANYDATA: |
| 3664 | case YANG_ANYXML: |
| 3665 | ret = parse_any(ctx, data, kw, &cont->child); |
| 3666 | break; |
| 3667 | case YANG_CHOICE: |
| 3668 | ret = parse_choice(ctx, data, &cont->child); |
| 3669 | break; |
| 3670 | case YANG_CONTAINER: |
| 3671 | ret = parse_container(ctx, data, &cont->child); |
| 3672 | break; |
| 3673 | case YANG_LEAF: |
| 3674 | ret = parse_leaf(ctx, data, &cont->child); |
| 3675 | break; |
| 3676 | case YANG_LEAF_LIST: |
| 3677 | ret = parse_leaflist(ctx, data, &cont->child); |
| 3678 | break; |
| 3679 | case YANG_LIST: |
| 3680 | ret = parse_list(ctx, data, &cont->child); |
| 3681 | break; |
| 3682 | case YANG_USES: |
| 3683 | ret = parse_uses(ctx, data, &cont->child); |
| 3684 | break; |
| 3685 | |
| 3686 | case YANG_TYPEDEF: |
| 3687 | ret = parse_typedef(ctx, data, &cont->typedefs); |
| 3688 | break; |
| 3689 | case YANG_MUST: |
| 3690 | ret = parse_restrs(ctx, data, kw, &cont->musts); |
| 3691 | break; |
| 3692 | case YANG_ACTION: |
| 3693 | ret = parse_action(ctx, data, &cont->actions); |
| 3694 | break; |
| 3695 | case YANG_GROUPING: |
| 3696 | ret = parse_grouping(ctx, data, &cont->groupings); |
| 3697 | break; |
| 3698 | case YANG_NOTIFICATION: |
| 3699 | ret = parse_notif(ctx, data, &cont->notifs); |
| 3700 | break; |
| 3701 | case YANG_CUSTOM: |
| 3702 | ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &cont->exts); |
| 3703 | break; |
| 3704 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3705 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "container"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3706 | return LY_EVALID; |
| 3707 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 3708 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3709 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 3710 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3711 | |
| 3712 | return ret; |
| 3713 | } |
| 3714 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3715 | /** |
| 3716 | * @brief Parse the list statement. |
| 3717 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3718 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3719 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 3720 | * @param[in,out] siblings Siblings to add to. |
| 3721 | * |
| 3722 | * @return LY_ERR values. |
| 3723 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3724 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3725 | parse_list(struct ly_parser_ctx *ctx, const char **data, struct lysp_node **siblings) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3726 | { |
| 3727 | LY_ERR ret = 0; |
| 3728 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 3729 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3730 | enum yang_keyword kw; |
| 3731 | struct lysp_node *iter; |
| 3732 | struct lysp_node_list *list; |
| 3733 | |
| 3734 | /* create structure */ |
| 3735 | list = calloc(1, sizeof *list); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3736 | LY_CHECK_ERR_RET(!list, LOGMEM(ctx->ctx), LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3737 | list->nodetype = LYS_LIST; |
| 3738 | |
| 3739 | /* insert into siblings */ |
| 3740 | if (!*siblings) { |
| 3741 | *siblings = (struct lysp_node *)list; |
| 3742 | } else { |
| 3743 | for (iter = *siblings; iter->next; iter = iter->next); |
| 3744 | iter->next = (struct lysp_node *)list; |
| 3745 | } |
| 3746 | |
| 3747 | /* get name */ |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 3748 | ret = get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len); |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 3749 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3750 | |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3751 | INSERT_WORD(ctx, buf, list->name, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3752 | |
| 3753 | /* parse substatements */ |
| 3754 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) { |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 3755 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3756 | |
| 3757 | switch (kw) { |
| 3758 | case YANG_CONFIG: |
| 3759 | ret = parse_config(ctx, data, &list->flags, &list->exts); |
| 3760 | break; |
| 3761 | case YANG_DESCRIPTION: |
| 3762 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &list->dsc, Y_STR_ARG, &list->exts); |
| 3763 | break; |
| 3764 | case YANG_IF_FEATURE: |
| 3765 | ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &list->iffeatures, Y_STR_ARG, &list->exts); |
| 3766 | break; |
| 3767 | case YANG_REFERENCE: |
| 3768 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &list->ref, Y_STR_ARG, &list->exts); |
| 3769 | break; |
| 3770 | case YANG_STATUS: |
| 3771 | ret = parse_status(ctx, data, &list->flags, &list->exts); |
| 3772 | break; |
| 3773 | case YANG_WHEN: |
| 3774 | ret = parse_when(ctx, data, &list->when); |
| 3775 | break; |
| 3776 | case YANG_KEY: |
| 3777 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_KEY, 0, &list->key, Y_STR_ARG, &list->exts); |
| 3778 | break; |
| 3779 | case YANG_MAX_ELEMENTS: |
| 3780 | ret = parse_maxelements(ctx, data, &list->max, &list->flags, &list->exts); |
| 3781 | break; |
| 3782 | case YANG_MIN_ELEMENTS: |
| 3783 | ret = parse_minelements(ctx, data, &list->min, &list->flags, &list->exts); |
| 3784 | break; |
| 3785 | case YANG_ORDERED_BY: |
| 3786 | ret = parse_orderedby(ctx, data, &list->flags, &list->exts); |
| 3787 | break; |
| 3788 | case YANG_UNIQUE: |
| 3789 | ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_UNIQUE, &list->uniques, Y_STR_ARG, &list->exts); |
| 3790 | break; |
| 3791 | |
| 3792 | case YANG_ANYDATA: |
| 3793 | case YANG_ANYXML: |
| 3794 | ret = parse_any(ctx, data, kw, &list->child); |
| 3795 | break; |
| 3796 | case YANG_CHOICE: |
| 3797 | ret = parse_choice(ctx, data, &list->child); |
| 3798 | break; |
| 3799 | case YANG_CONTAINER: |
| 3800 | ret = parse_container(ctx, data, &list->child); |
| 3801 | break; |
| 3802 | case YANG_LEAF: |
| 3803 | ret = parse_leaf(ctx, data, &list->child); |
| 3804 | break; |
| 3805 | case YANG_LEAF_LIST: |
| 3806 | ret = parse_leaflist(ctx, data, &list->child); |
| 3807 | break; |
| 3808 | case YANG_LIST: |
| 3809 | ret = parse_list(ctx, data, &list->child); |
| 3810 | break; |
| 3811 | case YANG_USES: |
| 3812 | ret = parse_uses(ctx, data, &list->child); |
| 3813 | break; |
| 3814 | |
| 3815 | case YANG_TYPEDEF: |
| 3816 | ret = parse_typedef(ctx, data, &list->typedefs); |
| 3817 | break; |
| 3818 | case YANG_MUST: |
| 3819 | ret = parse_restrs(ctx, data, kw, &list->musts); |
| 3820 | break; |
| 3821 | case YANG_ACTION: |
| 3822 | ret = parse_action(ctx, data, &list->actions); |
| 3823 | break; |
| 3824 | case YANG_GROUPING: |
| 3825 | ret = parse_grouping(ctx, data, &list->groupings); |
| 3826 | break; |
| 3827 | case YANG_NOTIFICATION: |
| 3828 | ret = parse_notif(ctx, data, &list->notifs); |
| 3829 | break; |
| 3830 | case YANG_CUSTOM: |
| 3831 | ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &list->exts); |
| 3832 | break; |
| 3833 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3834 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "container"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3835 | return LY_EVALID; |
| 3836 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 3837 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3838 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 3839 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3840 | |
| 3841 | return ret; |
| 3842 | } |
| 3843 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3844 | /** |
| 3845 | * @brief Parse the yin-element statement. |
| 3846 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3847 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3848 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 3849 | * @param[in,out] flags Flags to write to. |
| 3850 | * @param[in,out] exts Extension instances to add to. |
| 3851 | * |
| 3852 | * @return LY_ERR values. |
| 3853 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3854 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3855 | parse_yinelement(struct ly_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] | 3856 | { |
| 3857 | LY_ERR ret = 0; |
| 3858 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 3859 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3860 | enum yang_keyword kw; |
| 3861 | |
| 3862 | if (*flags & LYS_YINELEM_MASK) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3863 | LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "yin-element"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3864 | return LY_EVALID; |
| 3865 | } |
| 3866 | |
| 3867 | /* get value */ |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 3868 | ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len); |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 3869 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3870 | |
| 3871 | if ((word_len == 4) && !strncmp(word, "true", word_len)) { |
| 3872 | *flags |= LYS_YINELEM_TRUE; |
| 3873 | } else if ((word_len == 5) && !strncmp(word, "false", word_len)) { |
| 3874 | *flags |= LYS_YINELEM_FALSE; |
| 3875 | } else { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3876 | LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "yin-element"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3877 | free(buf); |
| 3878 | return LY_EVALID; |
| 3879 | } |
| 3880 | free(buf); |
| 3881 | |
| 3882 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) { |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 3883 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3884 | |
| 3885 | switch (kw) { |
| 3886 | case YANG_CUSTOM: |
| 3887 | ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_YINELEM, 0, exts); |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 3888 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3889 | break; |
| 3890 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3891 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "yin-element"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3892 | return LY_EVALID; |
| 3893 | } |
| 3894 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 3895 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3896 | |
| 3897 | return ret; |
| 3898 | } |
| 3899 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3900 | /** |
| 3901 | * @brief Parse the yin-element statement. |
| 3902 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3903 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3904 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 3905 | * @param[in,out] argument Value to write to. |
| 3906 | * @param[in,out] flags Flags to write to. |
| 3907 | * @param[in,out] exts Extension instances to add to. |
| 3908 | * |
| 3909 | * @return LY_ERR values. |
| 3910 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3911 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3912 | parse_argument(struct ly_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] | 3913 | { |
| 3914 | LY_ERR ret = 0; |
| 3915 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 3916 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3917 | enum yang_keyword kw; |
| 3918 | |
| 3919 | if (*argument) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3920 | LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "argument"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3921 | return LY_EVALID; |
| 3922 | } |
| 3923 | |
| 3924 | /* get value */ |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 3925 | ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len); |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 3926 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3927 | |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3928 | INSERT_WORD(ctx, buf, *argument, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3929 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) { |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 3930 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3931 | |
| 3932 | switch (kw) { |
| 3933 | case YANG_YIN_ELEMENT: |
| 3934 | ret = parse_yinelement(ctx, data, flags, exts); |
| 3935 | break; |
| 3936 | case YANG_CUSTOM: |
| 3937 | ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_ARGUMENT, 0, exts); |
| 3938 | break; |
| 3939 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3940 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "argument"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3941 | return LY_EVALID; |
| 3942 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 3943 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3944 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 3945 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3946 | |
| 3947 | return ret; |
| 3948 | } |
| 3949 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3950 | /** |
| 3951 | * @brief Parse the extension statement. |
| 3952 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3953 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3954 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 3955 | * @param[in,out] extensions Extensions to add to. |
| 3956 | * |
| 3957 | * @return LY_ERR values. |
| 3958 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3959 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3960 | parse_extension(struct ly_parser_ctx *ctx, const char **data, struct lysp_ext **extensions) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3961 | { |
| 3962 | LY_ERR ret = 0; |
| 3963 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 3964 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3965 | enum yang_keyword kw; |
| 3966 | struct lysp_ext *ex; |
| 3967 | |
| 3968 | LYSP_ARRAY_NEW_RET(ctx, extensions, ex, LY_EMEM); |
| 3969 | |
| 3970 | /* get value */ |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 3971 | ret = get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len); |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 3972 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3973 | |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3974 | INSERT_WORD(ctx, buf, ex->name, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3975 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) { |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 3976 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3977 | |
| 3978 | switch (kw) { |
| 3979 | case YANG_DESCRIPTION: |
| 3980 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &ex->dsc, Y_STR_ARG, &ex->exts); |
| 3981 | break; |
| 3982 | case YANG_REFERENCE: |
| 3983 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &ex->ref, Y_STR_ARG, &ex->exts); |
| 3984 | break; |
| 3985 | case YANG_STATUS: |
| 3986 | ret = parse_status(ctx, data, &ex->flags, &ex->exts); |
| 3987 | break; |
| 3988 | case YANG_ARGUMENT: |
| 3989 | ret = parse_argument(ctx, data, &ex->argument, &ex->flags, &ex->exts); |
| 3990 | break; |
| 3991 | case YANG_CUSTOM: |
| 3992 | ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &ex->exts); |
| 3993 | break; |
| 3994 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3995 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "extension"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3996 | return LY_EVALID; |
| 3997 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 3998 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3999 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 4000 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4001 | |
| 4002 | return ret; |
| 4003 | } |
| 4004 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 4005 | /** |
| 4006 | * @brief Parse the deviate statement. |
| 4007 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 4008 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 4009 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 4010 | * @param[in,out] deviates Deviates to add to. |
| 4011 | * |
| 4012 | * @return LY_ERR values. |
| 4013 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4014 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 4015 | parse_deviate(struct ly_parser_ctx *ctx, const char **data, struct lysp_deviate **deviates) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4016 | { |
| 4017 | LY_ERR ret = 0; |
| 4018 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 4019 | size_t word_len, dev_mod; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4020 | enum yang_keyword kw; |
| 4021 | struct lysp_deviate *iter, *d; |
| 4022 | struct lysp_deviate_add *d_add = NULL; |
| 4023 | struct lysp_deviate_rpl *d_rpl = NULL; |
| 4024 | struct lysp_deviate_del *d_del = NULL; |
| 4025 | const char **d_units, ***d_uniques, ***d_dflts; |
| 4026 | struct lysp_restr **d_musts; |
| 4027 | uint16_t *d_flags; |
| 4028 | uint32_t *d_min, *d_max; |
| 4029 | |
| 4030 | /* get value */ |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 4031 | ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len); |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 4032 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4033 | |
| 4034 | if ((word_len == 13) && !strncmp(word, "not-supported", word_len)) { |
| 4035 | dev_mod = LYS_DEV_NOT_SUPPORTED; |
| 4036 | } else if ((word_len == 3) && !strncmp(word, "add", word_len)) { |
| 4037 | dev_mod = LYS_DEV_ADD; |
| 4038 | } else if ((word_len == 7) && !strncmp(word, "replace", word_len)) { |
| 4039 | dev_mod = LYS_DEV_REPLACE; |
| 4040 | } else if ((word_len == 6) && !strncmp(word, "delete", word_len)) { |
| 4041 | dev_mod = LYS_DEV_DELETE; |
| 4042 | } else { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 4043 | LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "deviate"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4044 | free(buf); |
| 4045 | return LY_EVALID; |
| 4046 | } |
| 4047 | free(buf); |
| 4048 | |
| 4049 | /* create structure */ |
| 4050 | switch (dev_mod) { |
| 4051 | case LYS_DEV_NOT_SUPPORTED: |
| 4052 | d = calloc(1, sizeof *d); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 4053 | LY_CHECK_ERR_RET(!d, LOGMEM(ctx->ctx), LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4054 | break; |
| 4055 | case LYS_DEV_ADD: |
| 4056 | d_add = calloc(1, sizeof *d_add); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 4057 | LY_CHECK_ERR_RET(!d_add, LOGMEM(ctx->ctx), LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4058 | d = (struct lysp_deviate *)d_add; |
| 4059 | d_units = &d_add->units; |
| 4060 | d_uniques = &d_add->uniques; |
| 4061 | d_dflts = &d_add->dflts; |
| 4062 | d_musts = &d_add->musts; |
| 4063 | d_flags = &d_add->flags; |
| 4064 | d_min = &d_add->min; |
| 4065 | d_max = &d_add->max; |
| 4066 | break; |
| 4067 | case LYS_DEV_REPLACE: |
| 4068 | d_rpl = calloc(1, sizeof *d_rpl); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 4069 | LY_CHECK_ERR_RET(!d_rpl, LOGMEM(ctx->ctx), LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4070 | d = (struct lysp_deviate *)d_rpl; |
| 4071 | d_units = &d_rpl->units; |
| 4072 | d_flags = &d_rpl->flags; |
| 4073 | d_min = &d_rpl->min; |
| 4074 | d_max = &d_rpl->max; |
| 4075 | break; |
| 4076 | case LYS_DEV_DELETE: |
| 4077 | d_del = calloc(1, sizeof *d_del); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 4078 | LY_CHECK_ERR_RET(!d_del, LOGMEM(ctx->ctx), LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4079 | d = (struct lysp_deviate *)d_del; |
| 4080 | d_units = &d_del->units; |
| 4081 | d_uniques = &d_del->uniques; |
| 4082 | d_dflts = &d_del->dflts; |
| 4083 | d_musts = &d_del->musts; |
| 4084 | d_flags = &d_del->flags; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4085 | break; |
| 4086 | default: |
| 4087 | assert(0); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 4088 | LOGINT_RET(ctx->ctx); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4089 | } |
| 4090 | d->mod = dev_mod; |
| 4091 | |
| 4092 | /* insert into siblings */ |
| 4093 | if (!*deviates) { |
| 4094 | *deviates = d; |
| 4095 | } else { |
| 4096 | for (iter = *deviates; iter->next; iter = iter->next); |
| 4097 | iter->next = d; |
| 4098 | } |
| 4099 | |
| 4100 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) { |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 4101 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4102 | |
| 4103 | switch (kw) { |
| 4104 | case YANG_CONFIG: |
| 4105 | switch (dev_mod) { |
| 4106 | case LYS_DEV_NOT_SUPPORTED: |
Michal Vasko | 492bec7 | 2018-09-18 13:11:10 +0200 | [diff] [blame] | 4107 | case LYS_DEV_DELETE: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 4108 | LOGVAL_YANG(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4109 | return LY_EVALID; |
| 4110 | default: |
| 4111 | ret = parse_config(ctx, data, d_flags, &d->exts); |
| 4112 | break; |
| 4113 | } |
| 4114 | break; |
| 4115 | case YANG_DEFAULT: |
| 4116 | switch (dev_mod) { |
| 4117 | case LYS_DEV_NOT_SUPPORTED: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 4118 | LOGVAL_YANG(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4119 | return LY_EVALID; |
| 4120 | case LYS_DEV_REPLACE: |
| 4121 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DEFAULT, 0, &d_rpl->dflt, Y_STR_ARG, &d->exts); |
| 4122 | break; |
| 4123 | default: |
| 4124 | ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_DEFAULT, d_dflts, Y_STR_ARG, &d->exts); |
| 4125 | break; |
| 4126 | } |
| 4127 | break; |
| 4128 | case YANG_MANDATORY: |
| 4129 | switch (dev_mod) { |
| 4130 | case LYS_DEV_NOT_SUPPORTED: |
Michal Vasko | 492bec7 | 2018-09-18 13:11:10 +0200 | [diff] [blame] | 4131 | case LYS_DEV_DELETE: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 4132 | LOGVAL_YANG(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4133 | return LY_EVALID; |
| 4134 | default: |
| 4135 | ret = parse_mandatory(ctx, data, d_flags, &d->exts); |
| 4136 | break; |
| 4137 | } |
| 4138 | break; |
| 4139 | case YANG_MAX_ELEMENTS: |
| 4140 | switch (dev_mod) { |
| 4141 | case LYS_DEV_NOT_SUPPORTED: |
Michal Vasko | 492bec7 | 2018-09-18 13:11:10 +0200 | [diff] [blame] | 4142 | case LYS_DEV_DELETE: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 4143 | LOGVAL_YANG(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4144 | return LY_EVALID; |
| 4145 | default: |
| 4146 | ret = parse_maxelements(ctx, data, d_max, d_flags, &d->exts); |
| 4147 | break; |
| 4148 | } |
| 4149 | break; |
| 4150 | case YANG_MIN_ELEMENTS: |
| 4151 | switch (dev_mod) { |
| 4152 | case LYS_DEV_NOT_SUPPORTED: |
Michal Vasko | 492bec7 | 2018-09-18 13:11:10 +0200 | [diff] [blame] | 4153 | case LYS_DEV_DELETE: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 4154 | LOGVAL_YANG(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4155 | return LY_EVALID; |
| 4156 | default: |
| 4157 | ret = parse_minelements(ctx, data, d_min, d_flags, &d->exts); |
| 4158 | break; |
| 4159 | } |
| 4160 | break; |
| 4161 | case YANG_MUST: |
| 4162 | switch (dev_mod) { |
| 4163 | case LYS_DEV_NOT_SUPPORTED: |
Michal Vasko | 492bec7 | 2018-09-18 13:11:10 +0200 | [diff] [blame] | 4164 | case LYS_DEV_REPLACE: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 4165 | LOGVAL_YANG(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4166 | return LY_EVALID; |
| 4167 | default: |
| 4168 | ret = parse_restrs(ctx, data, kw, d_musts); |
| 4169 | break; |
| 4170 | } |
| 4171 | break; |
| 4172 | case YANG_TYPE: |
| 4173 | switch (dev_mod) { |
| 4174 | case LYS_DEV_NOT_SUPPORTED: |
| 4175 | case LYS_DEV_ADD: |
| 4176 | case LYS_DEV_DELETE: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 4177 | LOGVAL_YANG(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4178 | return LY_EVALID; |
| 4179 | default: |
| 4180 | d_rpl->type = calloc(1, sizeof *d_rpl->type); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 4181 | LY_CHECK_ERR_RET(!d_rpl->type, LOGMEM(ctx->ctx), LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4182 | ret = parse_type(ctx, data, d_rpl->type); |
| 4183 | break; |
| 4184 | } |
| 4185 | break; |
| 4186 | case YANG_UNIQUE: |
| 4187 | switch (dev_mod) { |
| 4188 | case LYS_DEV_NOT_SUPPORTED: |
| 4189 | case LYS_DEV_REPLACE: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 4190 | LOGVAL_YANG(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4191 | return LY_EVALID; |
| 4192 | default: |
| 4193 | ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_UNIQUE, d_uniques, Y_STR_ARG, &d->exts); |
| 4194 | break; |
| 4195 | } |
| 4196 | break; |
| 4197 | case YANG_UNITS: |
| 4198 | switch (dev_mod) { |
| 4199 | case LYS_DEV_NOT_SUPPORTED: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 4200 | LOGVAL_YANG(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4201 | return LY_EVALID; |
| 4202 | default: |
| 4203 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_UNITS, 0, d_units, Y_STR_ARG, &d->exts); |
| 4204 | break; |
| 4205 | } |
| 4206 | break; |
| 4207 | case YANG_CUSTOM: |
| 4208 | ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &d->exts); |
| 4209 | break; |
| 4210 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 4211 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "deviate"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4212 | return LY_EVALID; |
| 4213 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 4214 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4215 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 4216 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4217 | |
| 4218 | return ret; |
| 4219 | } |
| 4220 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 4221 | /** |
| 4222 | * @brief Parse the deviation statement. |
| 4223 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 4224 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 4225 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 4226 | * @param[in,out] deviations Deviations to add to. |
| 4227 | * |
| 4228 | * @return LY_ERR values. |
| 4229 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4230 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 4231 | parse_deviation(struct ly_parser_ctx *ctx, const char **data, struct lysp_deviation **deviations) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4232 | { |
| 4233 | LY_ERR ret = 0; |
| 4234 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 4235 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4236 | enum yang_keyword kw; |
| 4237 | struct lysp_deviation *dev; |
| 4238 | |
| 4239 | LYSP_ARRAY_NEW_RET(ctx, deviations, dev, LY_EMEM); |
| 4240 | |
| 4241 | /* get value */ |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 4242 | ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len); |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 4243 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4244 | |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 4245 | INSERT_WORD(ctx, buf, dev->nodeid, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4246 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) { |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 4247 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4248 | |
| 4249 | switch (kw) { |
| 4250 | case YANG_DESCRIPTION: |
| 4251 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &dev->dsc, Y_STR_ARG, &dev->exts); |
| 4252 | break; |
| 4253 | case YANG_DEVIATE: |
| 4254 | ret = parse_deviate(ctx, data, &dev->deviates); |
| 4255 | break; |
| 4256 | case YANG_REFERENCE: |
| 4257 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &dev->ref, Y_STR_ARG, &dev->exts); |
| 4258 | break; |
| 4259 | case YANG_CUSTOM: |
| 4260 | ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &dev->exts); |
| 4261 | break; |
| 4262 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 4263 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "deviation"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4264 | return LY_EVALID; |
| 4265 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 4266 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4267 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 4268 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4269 | |
| 4270 | /* mandatory substatements */ |
| 4271 | if (!dev->deviates) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 4272 | LOGVAL_YANG(ctx, LY_VCODE_MISSTMT, "deviate", "deviation"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4273 | return LY_EVALID; |
| 4274 | } |
| 4275 | |
| 4276 | return ret; |
| 4277 | } |
| 4278 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 4279 | /** |
| 4280 | * @brief Parse the feature statement. |
| 4281 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 4282 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 4283 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 4284 | * @param[in,out] features Features to add to. |
| 4285 | * |
| 4286 | * @return LY_ERR values. |
| 4287 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4288 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 4289 | parse_feature(struct ly_parser_ctx *ctx, const char **data, struct lysp_feature **features) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4290 | { |
| 4291 | LY_ERR ret = 0; |
| 4292 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 4293 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4294 | enum yang_keyword kw; |
| 4295 | struct lysp_feature *feat; |
| 4296 | |
| 4297 | LYSP_ARRAY_NEW_RET(ctx, features, feat, LY_EMEM); |
| 4298 | |
| 4299 | /* get value */ |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 4300 | ret = get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len); |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 4301 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4302 | |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 4303 | INSERT_WORD(ctx, buf, feat->name, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4304 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) { |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 4305 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4306 | |
| 4307 | switch (kw) { |
| 4308 | case YANG_DESCRIPTION: |
| 4309 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &feat->dsc, Y_STR_ARG, &feat->exts); |
| 4310 | break; |
| 4311 | case YANG_IF_FEATURE: |
| 4312 | ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &feat->iffeatures, Y_STR_ARG, &feat->exts); |
| 4313 | break; |
| 4314 | case YANG_REFERENCE: |
| 4315 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &feat->ref, Y_STR_ARG, &feat->exts); |
| 4316 | break; |
| 4317 | case YANG_STATUS: |
| 4318 | ret = parse_status(ctx, data, &feat->flags, &feat->exts); |
| 4319 | break; |
| 4320 | case YANG_CUSTOM: |
| 4321 | ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &feat->exts); |
| 4322 | break; |
| 4323 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 4324 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "feature"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4325 | return LY_EMEM; |
| 4326 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 4327 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4328 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 4329 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4330 | |
| 4331 | return ret; |
| 4332 | } |
| 4333 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 4334 | /** |
| 4335 | * @brief Parse the identity statement. |
| 4336 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 4337 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 4338 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 4339 | * @param[in,out] identities Identities to add to. |
| 4340 | * |
| 4341 | * @return LY_ERR values. |
| 4342 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4343 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 4344 | parse_identity(struct ly_parser_ctx *ctx, const char **data, struct lysp_ident **identities) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4345 | { |
| 4346 | LY_ERR ret = 0; |
| 4347 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 4348 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4349 | enum yang_keyword kw; |
| 4350 | struct lysp_ident *ident; |
| 4351 | |
| 4352 | LYSP_ARRAY_NEW_RET(ctx, identities, ident, LY_EMEM); |
| 4353 | |
| 4354 | /* get value */ |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 4355 | ret = get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len); |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 4356 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4357 | |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 4358 | INSERT_WORD(ctx, buf, ident->name, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4359 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) { |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 4360 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4361 | |
| 4362 | switch (kw) { |
| 4363 | case YANG_DESCRIPTION: |
| 4364 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &ident->dsc, Y_STR_ARG, &ident->exts); |
| 4365 | break; |
| 4366 | case YANG_IF_FEATURE: |
| 4367 | ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &ident->iffeatures, Y_STR_ARG, &ident->exts); |
| 4368 | break; |
| 4369 | case YANG_REFERENCE: |
| 4370 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &ident->ref, Y_STR_ARG, &ident->exts); |
| 4371 | break; |
| 4372 | case YANG_STATUS: |
| 4373 | ret = parse_status(ctx, data, &ident->flags, &ident->exts); |
| 4374 | break; |
| 4375 | case YANG_BASE: |
| 4376 | ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_BASE, &ident->bases, Y_PREF_IDENTIF_ARG, &ident->exts); |
| 4377 | break; |
| 4378 | case YANG_CUSTOM: |
| 4379 | ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &ident->exts); |
| 4380 | break; |
| 4381 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 4382 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "identity"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4383 | return LY_EVALID; |
| 4384 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 4385 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4386 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 4387 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4388 | |
| 4389 | return ret; |
| 4390 | } |
| 4391 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 4392 | /** |
| 4393 | * @brief Parse the module or submodule statement. |
| 4394 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 4395 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 4396 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 4397 | * @param[in,out] mod Module to write to. |
| 4398 | * |
| 4399 | * @return LY_ERR values. |
| 4400 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4401 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 4402 | parse_sub_module(struct ly_parser_ctx *ctx, const char **data, struct lysp_module *mod) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4403 | { |
| 4404 | LY_ERR ret = 0; |
| 4405 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 4406 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4407 | enum yang_keyword kw, prev_kw = 0; |
| 4408 | enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER; |
| 4409 | |
| 4410 | /* (sub)module name */ |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 4411 | ret = get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len); |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 4412 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4413 | |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 4414 | INSERT_WORD(ctx, buf, mod->name, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4415 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) { |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 4416 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4417 | |
Radek Krejci | e384647 | 2018-10-15 15:24:51 +0200 | [diff] [blame^] | 4418 | #define CHECK_ORDER(SECTION) \ |
| 4419 | if (mod_stmt > SECTION) {LOGVAL_YANG(ctx, LY_VCODE_INORD, ly_stmt2str(kw), ly_stmt2str(prev_kw)); return LY_EVALID;}mod_stmt = SECTION |
| 4420 | |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4421 | switch (kw) { |
| 4422 | /* module header */ |
| 4423 | case YANG_NAMESPACE: |
| 4424 | case YANG_PREFIX: |
| 4425 | if (mod->submodule) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 4426 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "submodule"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4427 | return LY_EVALID; |
| 4428 | } |
Radek Krejci | e384647 | 2018-10-15 15:24:51 +0200 | [diff] [blame^] | 4429 | CHECK_ORDER(Y_MOD_MODULE_HEADER); |
| 4430 | break; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4431 | case YANG_BELONGS_TO: |
Radek Krejci | e384647 | 2018-10-15 15:24:51 +0200 | [diff] [blame^] | 4432 | if (!mod->submodule) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 4433 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "module"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4434 | return LY_EVALID; |
| 4435 | } |
Radek Krejci | e384647 | 2018-10-15 15:24:51 +0200 | [diff] [blame^] | 4436 | CHECK_ORDER(Y_MOD_MODULE_HEADER); |
| 4437 | break; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4438 | case YANG_YANG_VERSION: |
Radek Krejci | e384647 | 2018-10-15 15:24:51 +0200 | [diff] [blame^] | 4439 | CHECK_ORDER(Y_MOD_MODULE_HEADER); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4440 | break; |
| 4441 | /* linkage */ |
| 4442 | case YANG_INCLUDE: |
| 4443 | case YANG_IMPORT: |
Radek Krejci | e384647 | 2018-10-15 15:24:51 +0200 | [diff] [blame^] | 4444 | CHECK_ORDER(Y_MOD_LINKAGE); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4445 | break; |
| 4446 | /* meta */ |
| 4447 | case YANG_ORGANIZATION: |
| 4448 | case YANG_CONTACT: |
| 4449 | case YANG_DESCRIPTION: |
| 4450 | case YANG_REFERENCE: |
Radek Krejci | e384647 | 2018-10-15 15:24:51 +0200 | [diff] [blame^] | 4451 | CHECK_ORDER(Y_MOD_META); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4452 | break; |
| 4453 | |
| 4454 | /* revision */ |
| 4455 | case YANG_REVISION: |
Radek Krejci | e384647 | 2018-10-15 15:24:51 +0200 | [diff] [blame^] | 4456 | CHECK_ORDER(Y_MOD_REVISION); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4457 | break; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4458 | /* body */ |
| 4459 | case YANG_ANYDATA: |
| 4460 | case YANG_ANYXML: |
| 4461 | case YANG_AUGMENT: |
| 4462 | case YANG_CHOICE: |
| 4463 | case YANG_CONTAINER: |
| 4464 | case YANG_DEVIATION: |
| 4465 | case YANG_EXTENSION: |
| 4466 | case YANG_FEATURE: |
| 4467 | case YANG_GROUPING: |
| 4468 | case YANG_IDENTITY: |
| 4469 | case YANG_LEAF: |
| 4470 | case YANG_LEAF_LIST: |
| 4471 | case YANG_LIST: |
| 4472 | case YANG_NOTIFICATION: |
| 4473 | case YANG_RPC: |
| 4474 | case YANG_TYPEDEF: |
| 4475 | case YANG_USES: |
| 4476 | case YANG_CUSTOM: |
| 4477 | mod_stmt = Y_MOD_BODY; |
| 4478 | break; |
| 4479 | default: |
| 4480 | /* error handled in the next switch */ |
| 4481 | break; |
| 4482 | } |
Radek Krejci | e384647 | 2018-10-15 15:24:51 +0200 | [diff] [blame^] | 4483 | #undef CHECK_ORDER |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4484 | |
Radek Krejci | e384647 | 2018-10-15 15:24:51 +0200 | [diff] [blame^] | 4485 | prev_kw = kw; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4486 | switch (kw) { |
| 4487 | /* module header */ |
| 4488 | case YANG_YANG_VERSION: |
| 4489 | ret = parse_yangversion(ctx, data, mod); |
| 4490 | break; |
| 4491 | case YANG_NAMESPACE: |
| 4492 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_NAMESPACE, 0, &mod->ns, Y_STR_ARG, &mod->exts); |
| 4493 | break; |
| 4494 | case YANG_PREFIX: |
| 4495 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_PREFIX, 0, &mod->prefix, Y_IDENTIF_ARG, &mod->exts); |
Radek Krejci | 70853c5 | 2018-10-15 14:46:16 +0200 | [diff] [blame] | 4496 | LY_CHECK_RET(lysp_check_prefix(ctx, mod, &mod->prefix), LY_EVALID); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4497 | break; |
| 4498 | case YANG_BELONGS_TO: |
| 4499 | ret = parse_belongsto(ctx, data, &mod->belongsto, &mod->prefix, &mod->exts); |
| 4500 | break; |
| 4501 | |
| 4502 | /* linkage */ |
| 4503 | case YANG_INCLUDE: |
| 4504 | ret = parse_include(ctx, data, &mod->includes); |
| 4505 | break; |
| 4506 | case YANG_IMPORT: |
Radek Krejci | 70853c5 | 2018-10-15 14:46:16 +0200 | [diff] [blame] | 4507 | ret = parse_import(ctx, data, mod); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4508 | break; |
| 4509 | |
| 4510 | /* meta */ |
| 4511 | case YANG_ORGANIZATION: |
| 4512 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_ORGANIZATION, 0, &mod->org, Y_STR_ARG, &mod->exts); |
| 4513 | break; |
| 4514 | case YANG_CONTACT: |
| 4515 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_CONTACT, 0, &mod->contact, Y_STR_ARG, &mod->exts); |
| 4516 | break; |
| 4517 | case YANG_DESCRIPTION: |
| 4518 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &mod->dsc, Y_STR_ARG, &mod->exts); |
| 4519 | break; |
| 4520 | case YANG_REFERENCE: |
| 4521 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &mod->ref, Y_STR_ARG, &mod->exts); |
| 4522 | break; |
| 4523 | |
| 4524 | /* revision */ |
| 4525 | case YANG_REVISION: |
| 4526 | ret = parse_revision(ctx, data, &mod->revs); |
| 4527 | break; |
| 4528 | |
| 4529 | /* body */ |
| 4530 | case YANG_ANYDATA: |
| 4531 | case YANG_ANYXML: |
| 4532 | ret = parse_any(ctx, data, kw, &mod->data); |
| 4533 | break; |
| 4534 | case YANG_CHOICE: |
| 4535 | ret = parse_choice(ctx, data, &mod->data); |
| 4536 | break; |
| 4537 | case YANG_CONTAINER: |
| 4538 | ret = parse_container(ctx, data, &mod->data); |
| 4539 | break; |
| 4540 | case YANG_LEAF: |
| 4541 | ret = parse_leaf(ctx, data, &mod->data); |
| 4542 | break; |
| 4543 | case YANG_LEAF_LIST: |
| 4544 | ret = parse_leaflist(ctx, data, &mod->data); |
| 4545 | break; |
| 4546 | case YANG_LIST: |
| 4547 | ret = parse_list(ctx, data, &mod->data); |
| 4548 | break; |
| 4549 | case YANG_USES: |
| 4550 | ret = parse_uses(ctx, data, &mod->data); |
| 4551 | break; |
| 4552 | |
| 4553 | case YANG_AUGMENT: |
| 4554 | ret = parse_augment(ctx, data, &mod->augments); |
| 4555 | break; |
| 4556 | case YANG_DEVIATION: |
| 4557 | ret = parse_deviation(ctx, data, &mod->deviations); |
| 4558 | break; |
| 4559 | case YANG_EXTENSION: |
| 4560 | ret = parse_extension(ctx, data, &mod->extensions); |
| 4561 | break; |
| 4562 | case YANG_FEATURE: |
| 4563 | ret = parse_feature(ctx, data, &mod->features); |
| 4564 | break; |
| 4565 | case YANG_GROUPING: |
| 4566 | ret = parse_grouping(ctx, data, &mod->groupings); |
| 4567 | break; |
| 4568 | case YANG_IDENTITY: |
| 4569 | ret = parse_identity(ctx, data, &mod->identities); |
| 4570 | break; |
| 4571 | case YANG_NOTIFICATION: |
| 4572 | ret = parse_notif(ctx, data, &mod->notifs); |
| 4573 | break; |
| 4574 | case YANG_RPC: |
| 4575 | ret = parse_action(ctx, data, &mod->rpcs); |
| 4576 | break; |
| 4577 | case YANG_TYPEDEF: |
| 4578 | ret = parse_typedef(ctx, data, &mod->typedefs); |
| 4579 | break; |
| 4580 | case YANG_CUSTOM: |
| 4581 | ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &mod->exts); |
| 4582 | break; |
| 4583 | |
| 4584 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 4585 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), mod->submodule ? "submodule" : "module"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4586 | return LY_EVALID; |
| 4587 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 4588 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4589 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 4590 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4591 | |
| 4592 | /* mandatory substatements */ |
| 4593 | if (mod->submodule) { |
| 4594 | if (!mod->belongsto) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 4595 | LOGVAL_YANG(ctx, LY_VCODE_MISSTMT, "belongs-to", "submodule"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4596 | return LY_EVALID; |
| 4597 | } |
| 4598 | } else { |
| 4599 | if (!mod->ns) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 4600 | LOGVAL_YANG(ctx, LY_VCODE_MISSTMT, "namespace", "module"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4601 | return LY_EVALID; |
| 4602 | } else if (!mod->prefix) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 4603 | LOGVAL_YANG(ctx, LY_VCODE_MISSTMT, "prefix", "module"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4604 | return LY_EVALID; |
| 4605 | } |
| 4606 | } |
| 4607 | |
| 4608 | return ret; |
| 4609 | } |
| 4610 | |
Radek Krejci | d4557c6 | 2018-09-17 11:42:09 +0200 | [diff] [blame] | 4611 | LY_ERR |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4612 | yang_parse(struct ly_ctx *ctx, const char *data, struct lysp_module **mod_p) |
| 4613 | { |
| 4614 | LY_ERR ret = 0; |
| 4615 | char *word, *buf; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 4616 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4617 | enum yang_keyword kw; |
Radek Krejci | 0c2cf32 | 2018-10-13 08:02:30 +0200 | [diff] [blame] | 4618 | struct lysp_module *mod = NULL; |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 4619 | struct ly_parser_ctx context; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4620 | |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 4621 | context.ctx = ctx; |
| 4622 | context.line = 1; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4623 | |
| 4624 | /* "module"/"submodule" */ |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 4625 | ret = get_keyword(&context, &data, &kw, &word, &word_len); |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 4626 | LY_CHECK_GOTO(ret, error); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4627 | |
| 4628 | if ((kw != YANG_MODULE) && (kw != YANG_SUBMODULE)) { |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 4629 | LOGVAL_YANG(&context, LYVE_SYNTAX, "Invalid keyword \"%s\", expected \"module\" or \"submodule\".", |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 4630 | ly_stmt2str(kw)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4631 | goto error; |
| 4632 | } |
| 4633 | |
| 4634 | mod = calloc(1, sizeof *mod); |
| 4635 | LY_CHECK_ERR_GOTO(!mod, LOGMEM(ctx), error); |
| 4636 | if (kw == YANG_SUBMODULE) { |
| 4637 | mod->submodule = 1; |
| 4638 | } |
Radek Krejci | 9fcacc1 | 2018-10-11 15:59:11 +0200 | [diff] [blame] | 4639 | mod->ctx = ctx; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4640 | |
| 4641 | /* substatements */ |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 4642 | ret = parse_sub_module(&context, &data, mod); |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 4643 | LY_CHECK_GOTO(ret, error); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4644 | |
| 4645 | /* read some trailing spaces or new lines */ |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 4646 | ret = get_argument(&context, &data, Y_MAYBE_STR_ARG, &word, &buf, &word_len); |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 4647 | LY_CHECK_GOTO(ret, error); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4648 | |
| 4649 | if (word) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 4650 | LOGVAL_YANG(&context, LYVE_SYNTAX, "Invalid character sequence \"%.*s\", expected end-of-file.", |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4651 | word_len, word); |
| 4652 | free(buf); |
| 4653 | goto error; |
| 4654 | } |
| 4655 | assert(!buf); |
| 4656 | |
| 4657 | *mod_p = mod; |
| 4658 | return ret; |
| 4659 | |
| 4660 | error: |
Radek Krejci | 9fcacc1 | 2018-10-11 15:59:11 +0200 | [diff] [blame] | 4661 | lysp_module_free(mod); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4662 | return ret; |
| 4663 | } |