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 | 156ccaf | 2018-10-15 15:49:17 +0200 | [diff] [blame] | 908 | case ':': |
| 909 | /* keyword is not actually a keyword, but prefix of an extension. |
| 910 | * To avoid repeated check of the prefix syntax, move to the point where the colon was read |
| 911 | * and we will be checking the keyword (extension instance) itself */ |
| 912 | prefix = 1; |
| 913 | MOVE_INPUT(ctx, data, 1); |
| 914 | goto extension; |
Radek Krejci | dcc7b32 | 2018-10-11 14:24:02 +0200 | [diff] [blame] | 915 | case '{': |
| 916 | /* allowed only for input and output statements which can be without arguments */ |
| 917 | if (*kw == YANG_INPUT || *kw == YANG_OUTPUT) { |
| 918 | break; |
| 919 | } |
| 920 | /* fallthrough */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 921 | default: |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 922 | MOVE_INPUT(ctx, data, 1); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 923 | LOGVAL_YANG(ctx, LY_VCODE_INSTREXP, (int)(*data - word_start), word_start, |
| 924 | "a keyword followed by a separator"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 925 | return LY_EVALID; |
| 926 | } |
| 927 | } else { |
| 928 | /* still can be an extension */ |
| 929 | prefix = 0; |
Radek Krejci | 156ccaf | 2018-10-15 15:49:17 +0200 | [diff] [blame] | 930 | extension: |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 931 | while (**data && (**data != ' ') && (**data != '\t') && (**data != '\n') && (**data != '{') && (**data != ';')) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 932 | LY_CHECK_ERR_RET(ly_getutf8(data, &c, &len), |
| 933 | LOGVAL_YANG(ctx, LY_VCODE_INCHAR, (*data)[-len]), LY_EVALID); |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 934 | ++ctx->indent; |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 935 | /* check character validity */ |
| 936 | 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] | 937 | } |
| 938 | if (!**data) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 939 | LOGVAL_YANG(ctx, LY_VCODE_EOF); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 940 | return LY_EVALID; |
| 941 | } |
| 942 | |
| 943 | /* prefix is mandatory for extension instances */ |
Radek Krejci | dcc7b32 | 2018-10-11 14:24:02 +0200 | [diff] [blame] | 944 | if (prefix != 2) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 945 | 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] | 946 | return LY_EVALID; |
| 947 | } |
| 948 | |
| 949 | *kw = YANG_CUSTOM; |
| 950 | } |
Radek Krejci | 626df48 | 2018-10-11 15:06:31 +0200 | [diff] [blame] | 951 | success: |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 952 | if (word_p) { |
| 953 | *word_p = (char *)word_start; |
| 954 | *word_len = *data - word_start; |
| 955 | } |
| 956 | |
| 957 | return LY_SUCCESS; |
| 958 | } |
| 959 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 960 | /** |
| 961 | * @brief Parse extension instance substatements. |
| 962 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 963 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 964 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 965 | * @param[in] word Extension instance substatement name (keyword). |
| 966 | * @param[in] word_len Extension instance substatement name length. |
| 967 | * @param[in,out] child Children of this extension instance to add to. |
| 968 | * |
| 969 | * @return LY_ERR values. |
| 970 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 971 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 972 | 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] | 973 | struct lysp_stmt **child) |
| 974 | { |
| 975 | char *buf; |
| 976 | LY_ERR ret = 0; |
| 977 | enum yang_keyword kw; |
| 978 | struct lysp_stmt *stmt, *par_child; |
| 979 | |
| 980 | stmt = calloc(1, sizeof *stmt); |
| 981 | LY_CHECK_ERR_RET(!stmt, LOGMEM(NULL), LY_EMEM); |
| 982 | |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 983 | stmt->stmt = lydict_insert(ctx->ctx, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 984 | |
| 985 | /* get optional argument */ |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 986 | 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] | 987 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 988 | |
Radek Krejci | 0ae092d | 2018-09-20 16:43:19 +0200 | [diff] [blame] | 989 | if (word) { |
| 990 | if (buf) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 991 | stmt->arg = lydict_insert_zc(ctx->ctx, word); |
Radek Krejci | 0ae092d | 2018-09-20 16:43:19 +0200 | [diff] [blame] | 992 | } else { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 993 | stmt->arg = lydict_insert(ctx->ctx, word, word_len); |
Radek Krejci | 0ae092d | 2018-09-20 16:43:19 +0200 | [diff] [blame] | 994 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 995 | } |
| 996 | |
| 997 | /* insert into parent statements */ |
| 998 | if (!*child) { |
| 999 | *child = stmt; |
| 1000 | } else { |
| 1001 | for (par_child = *child; par_child->next; par_child = par_child->next); |
| 1002 | par_child->next = stmt; |
| 1003 | } |
| 1004 | |
| 1005 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) { |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1006 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1007 | |
| 1008 | ret = parse_ext_substmt(ctx, data, word, word_len, &stmt->child); |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1009 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1010 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1011 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1012 | |
| 1013 | return ret; |
| 1014 | } |
| 1015 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1016 | /** |
| 1017 | * @brief Parse extension instance. |
| 1018 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1019 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1020 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 1021 | * @param[in] ext_name Extension instance substatement name (keyword). |
| 1022 | * @param[in] ext_name_len Extension instance substatement name length. |
| 1023 | * @param[in] insubstmt Type of the keyword this extension instance is a substatement of. |
| 1024 | * @param[in] insubstmt_index Index of the keyword instance this extension instance is a substatement of. |
| 1025 | * @param[in,out] exts Extension instances to add to. |
| 1026 | * |
| 1027 | * @return LY_ERR values. |
| 1028 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1029 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1030 | 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] | 1031 | uint32_t insubstmt_index, struct lysp_ext_instance **exts) |
| 1032 | { |
| 1033 | LY_ERR ret = 0; |
| 1034 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 1035 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1036 | struct lysp_ext_instance *e; |
| 1037 | enum yang_keyword kw; |
| 1038 | |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame] | 1039 | LYSP_ARRAY_NEW_RET(ctx->ctx, *exts, e, LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1040 | |
| 1041 | /* store name and insubstmt info */ |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1042 | e->name = lydict_insert(ctx->ctx, ext_name, ext_name_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1043 | e->insubstmt = insubstmt; |
| 1044 | e->insubstmt_index = insubstmt_index; |
| 1045 | |
| 1046 | /* get optional argument */ |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 1047 | 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] | 1048 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1049 | |
Radek Krejci | 0ae092d | 2018-09-20 16:43:19 +0200 | [diff] [blame] | 1050 | if (word) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1051 | INSERT_WORD(ctx, buf, e->argument, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1052 | } |
| 1053 | |
| 1054 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) { |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1055 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1056 | |
| 1057 | ret = parse_ext_substmt(ctx, data, word, word_len, &e->child); |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1058 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1059 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1060 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1061 | |
| 1062 | return ret; |
| 1063 | } |
| 1064 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1065 | /** |
| 1066 | * @brief Parse a generic text field without specific constraints. Those are contact, organization, |
| 1067 | * description, etc... |
| 1068 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1069 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1070 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 1071 | * @param[in] substmt Type of this substatement. |
| 1072 | * @param[in] substmt_index Index of this substatement. |
| 1073 | * @param[in,out] value Place to store the parsed value. |
| 1074 | * @param[in] arg Type of the YANG keyword argument (of the value). |
| 1075 | * @param[in,out] exts Extension instances to add to. |
| 1076 | * |
| 1077 | * @return LY_ERR values. |
| 1078 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1079 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1080 | 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] | 1081 | const char **value, enum yang_arg arg, struct lysp_ext_instance **exts) |
| 1082 | { |
| 1083 | LY_ERR ret = 0; |
| 1084 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 1085 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1086 | enum yang_keyword kw; |
| 1087 | |
| 1088 | if (*value) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1089 | LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, lyext_substmt2str(substmt)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1090 | return LY_EVALID; |
| 1091 | } |
| 1092 | |
| 1093 | /* get value */ |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 1094 | ret = get_argument(ctx, data, arg, &word, &buf, &word_len); |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1095 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1096 | |
| 1097 | /* store value and spend buf if allocated */ |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1098 | INSERT_WORD(ctx, buf, *value, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1099 | |
| 1100 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) { |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1101 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1102 | |
| 1103 | switch (kw) { |
| 1104 | case YANG_CUSTOM: |
| 1105 | ret = parse_ext(ctx, data, word, word_len, substmt, substmt_index, exts); |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1106 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1107 | break; |
| 1108 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1109 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), lyext_substmt2str(substmt)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1110 | return LY_EVALID; |
| 1111 | } |
| 1112 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1113 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1114 | |
| 1115 | return ret; |
| 1116 | } |
| 1117 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1118 | /** |
| 1119 | * @brief Parse the yang-version statement. |
| 1120 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1121 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1122 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 1123 | * @param[in] mod Module to store the parsed information in. |
| 1124 | * |
| 1125 | * @return LY_ERR values. |
| 1126 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1127 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1128 | 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] | 1129 | { |
| 1130 | LY_ERR ret = 0; |
| 1131 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 1132 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1133 | enum yang_keyword kw; |
| 1134 | |
| 1135 | if (mod->version) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1136 | LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "yang-version"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1137 | return LY_EVALID; |
| 1138 | } |
| 1139 | |
| 1140 | /* get value */ |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 1141 | ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len); |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1142 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1143 | |
| 1144 | if ((word_len == 3) && !strncmp(word, "1.0", word_len)) { |
| 1145 | mod->version = LYS_VERSION_1_0; |
| 1146 | } else if ((word_len == 3) && !strncmp(word, "1.1", word_len)) { |
| 1147 | mod->version = LYS_VERSION_1_1; |
| 1148 | } else { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1149 | LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "yang-version"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1150 | free(buf); |
| 1151 | return LY_EVALID; |
| 1152 | } |
| 1153 | free(buf); |
| 1154 | |
| 1155 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) { |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1156 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1157 | |
| 1158 | switch (kw) { |
| 1159 | case YANG_CUSTOM: |
| 1160 | 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] | 1161 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1162 | break; |
| 1163 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1164 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "yang-version"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1165 | return LY_EVALID; |
| 1166 | } |
| 1167 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1168 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1169 | |
| 1170 | return ret; |
| 1171 | } |
| 1172 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1173 | /** |
| 1174 | * @brief Parse the belongs-to statement. |
| 1175 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1176 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1177 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 1178 | * @param[in,out] belongsto Place to store the parsed value. |
| 1179 | * @param[in,out] prefix Place to store the parsed belongs-to prefix value. |
| 1180 | * @param[in,out] exts Extension instances to add to. |
| 1181 | * |
| 1182 | * @return LY_ERR values. |
| 1183 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1184 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1185 | 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] | 1186 | { |
| 1187 | LY_ERR ret = 0; |
| 1188 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 1189 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1190 | enum yang_keyword kw; |
| 1191 | |
| 1192 | if (*belongsto) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1193 | LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "belongs-to"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1194 | return LY_EVALID; |
| 1195 | } |
| 1196 | |
| 1197 | /* get value */ |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 1198 | ret = get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len); |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1199 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1200 | |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1201 | INSERT_WORD(ctx, buf, *belongsto, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1202 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) { |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1203 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1204 | |
| 1205 | switch (kw) { |
| 1206 | case YANG_PREFIX: |
| 1207 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_PREFIX, 0, prefix, Y_IDENTIF_ARG, exts); |
| 1208 | break; |
| 1209 | case YANG_CUSTOM: |
| 1210 | ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_BELONGSTO, 0, exts); |
| 1211 | break; |
| 1212 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1213 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "belongs-to"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1214 | return LY_EVALID; |
| 1215 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1216 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1217 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1218 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1219 | |
| 1220 | /* mandatory substatements */ |
| 1221 | if (!*prefix) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1222 | LOGVAL_YANG(ctx, LY_VCODE_MISSTMT, "prefix", "belongs-to"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1223 | return LY_EVALID; |
| 1224 | } |
| 1225 | |
| 1226 | return ret; |
| 1227 | } |
| 1228 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1229 | /** |
| 1230 | * @brief Parse the revision-date statement. |
| 1231 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1232 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1233 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 1234 | * @param[in,out] rev Array to store the parsed value in. |
| 1235 | * @param[in,out] exts Extension instances to add to. |
| 1236 | * |
| 1237 | * @return LY_ERR values. |
| 1238 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1239 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1240 | 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] | 1241 | { |
| 1242 | LY_ERR ret = 0; |
| 1243 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 1244 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1245 | enum yang_keyword kw; |
| 1246 | |
| 1247 | if (rev[0]) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1248 | LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "revision-date"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1249 | return LY_EVALID; |
| 1250 | } |
| 1251 | |
| 1252 | /* get value */ |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 1253 | ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len); |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1254 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1255 | |
| 1256 | /* check value */ |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1257 | if (lysp_check_date(ctx->ctx, word, word_len, "revision-date")) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1258 | free(buf); |
| 1259 | return LY_EVALID; |
| 1260 | } |
| 1261 | |
| 1262 | /* store value and spend buf if allocated */ |
| 1263 | strncpy(rev, word, word_len); |
| 1264 | free(buf); |
| 1265 | |
| 1266 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) { |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1267 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1268 | |
| 1269 | switch (kw) { |
| 1270 | case YANG_CUSTOM: |
| 1271 | 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] | 1272 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1273 | break; |
| 1274 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1275 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "revision-date"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1276 | return LY_EVALID; |
| 1277 | } |
| 1278 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1279 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1280 | |
| 1281 | return ret; |
| 1282 | } |
| 1283 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1284 | /** |
| 1285 | * @brief Parse the include statement. |
| 1286 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1287 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1288 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 1289 | * @param[in,out] includes Parsed includes to add to. |
| 1290 | * |
| 1291 | * @return LY_ERR values. |
| 1292 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1293 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1294 | 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] | 1295 | { |
| 1296 | LY_ERR ret = 0; |
| 1297 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 1298 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1299 | enum yang_keyword kw; |
| 1300 | struct lysp_include *inc; |
| 1301 | |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame] | 1302 | LYSP_ARRAY_NEW_RET(ctx->ctx, *includes, inc, LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1303 | |
| 1304 | /* get value */ |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 1305 | ret = get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len); |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1306 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1307 | |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1308 | INSERT_WORD(ctx, buf, inc->name, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1309 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) { |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1310 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1311 | |
| 1312 | switch (kw) { |
| 1313 | case YANG_DESCRIPTION: |
| 1314 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &inc->dsc, Y_STR_ARG, &inc->exts); |
| 1315 | break; |
| 1316 | case YANG_REFERENCE: |
| 1317 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &inc->ref, Y_STR_ARG, &inc->exts); |
| 1318 | break; |
| 1319 | case YANG_REVISION_DATE: |
| 1320 | ret = parse_revisiondate(ctx, data, inc->rev, &inc->exts); |
| 1321 | break; |
| 1322 | case YANG_CUSTOM: |
| 1323 | ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &inc->exts); |
| 1324 | break; |
| 1325 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1326 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "include"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1327 | return LY_EVALID; |
| 1328 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1329 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1330 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1331 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1332 | |
| 1333 | return ret; |
| 1334 | } |
| 1335 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1336 | /** |
| 1337 | * @brief Parse the import statement. |
| 1338 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1339 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1340 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 1341 | * @param[in,out] imports Parsed imports to add to. |
| 1342 | * |
| 1343 | * @return LY_ERR values. |
| 1344 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1345 | static LY_ERR |
Radek Krejci | 70853c5 | 2018-10-15 14:46:16 +0200 | [diff] [blame] | 1346 | 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] | 1347 | { |
| 1348 | LY_ERR ret = 0; |
| 1349 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 1350 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1351 | enum yang_keyword kw; |
| 1352 | struct lysp_import *imp; |
| 1353 | |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame] | 1354 | LYSP_ARRAY_NEW_RET(ctx->ctx, module->imports, imp, LY_EVALID); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1355 | |
| 1356 | /* get value */ |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 1357 | ret = get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len); |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1358 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1359 | |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1360 | INSERT_WORD(ctx, buf, imp->name, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1361 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) { |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1362 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1363 | |
| 1364 | switch (kw) { |
| 1365 | case YANG_PREFIX: |
| 1366 | 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] | 1367 | LY_CHECK_RET(lysp_check_prefix(ctx, module, &imp->prefix), LY_EVALID); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1368 | break; |
| 1369 | case YANG_DESCRIPTION: |
| 1370 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &imp->dsc, Y_STR_ARG, &imp->exts); |
| 1371 | break; |
| 1372 | case YANG_REFERENCE: |
| 1373 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &imp->ref, Y_STR_ARG, &imp->exts); |
| 1374 | break; |
| 1375 | case YANG_REVISION_DATE: |
| 1376 | ret = parse_revisiondate(ctx, data, imp->rev, &imp->exts); |
| 1377 | break; |
| 1378 | case YANG_CUSTOM: |
| 1379 | ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &imp->exts); |
| 1380 | break; |
| 1381 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1382 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "import"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1383 | return LY_EVALID; |
| 1384 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1385 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1386 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1387 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1388 | |
| 1389 | /* mandatory substatements */ |
| 1390 | if (!imp->prefix) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1391 | LOGVAL_YANG(ctx, LY_VCODE_MISSTMT, "prefix", "import"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1392 | return LY_EVALID; |
| 1393 | } |
| 1394 | |
| 1395 | return ret; |
| 1396 | } |
| 1397 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1398 | /** |
| 1399 | * @brief Parse the revision statement. |
| 1400 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1401 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1402 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 1403 | * @param[in,out] revs Parsed revisions to add to. |
| 1404 | * |
| 1405 | * @return LY_ERR values. |
| 1406 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1407 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1408 | 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] | 1409 | { |
| 1410 | LY_ERR ret = 0; |
| 1411 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 1412 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1413 | enum yang_keyword kw; |
| 1414 | struct lysp_revision *rev; |
| 1415 | |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame] | 1416 | LYSP_ARRAY_NEW_RET(ctx->ctx, *revs, rev, LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1417 | |
| 1418 | /* get value */ |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 1419 | ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len); |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1420 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1421 | |
| 1422 | /* check value */ |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1423 | if (lysp_check_date(ctx->ctx, word, word_len, "revision")) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1424 | return LY_EVALID; |
| 1425 | } |
| 1426 | |
| 1427 | strncpy(rev->rev, word, word_len); |
| 1428 | free(buf); |
| 1429 | |
| 1430 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) { |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1431 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1432 | |
| 1433 | switch (kw) { |
| 1434 | case YANG_DESCRIPTION: |
| 1435 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &rev->dsc, Y_STR_ARG, &rev->exts); |
| 1436 | break; |
| 1437 | case YANG_REFERENCE: |
| 1438 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &rev->ref, Y_STR_ARG, &rev->exts); |
| 1439 | break; |
| 1440 | case YANG_CUSTOM: |
| 1441 | ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &rev->exts); |
| 1442 | break; |
| 1443 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1444 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "revision"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1445 | return LY_EVALID; |
| 1446 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1447 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1448 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1449 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1450 | |
| 1451 | return ret; |
| 1452 | } |
| 1453 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1454 | /** |
| 1455 | * @brief Parse a generic text field that can have more instances such as base. |
| 1456 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1457 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1458 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 1459 | * @param[in] substmt Type of this substatement. |
| 1460 | * @param[in,out] texts Parsed values to add to. |
| 1461 | * @param[in] arg Type of the expected argument. |
| 1462 | * @param[in,out] exts Extension instances to add to. |
| 1463 | * |
| 1464 | * @return LY_ERR values. |
| 1465 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1466 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1467 | 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] | 1468 | struct lysp_ext_instance **exts) |
| 1469 | { |
| 1470 | LY_ERR ret = 0; |
| 1471 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 1472 | size_t count, word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1473 | enum yang_keyword kw; |
| 1474 | |
| 1475 | /* allocate new pointer */ |
| 1476 | for (count = 1; (*texts) && (*texts)[count - 1]; ++count); |
Radek Krejci | e7f84ae | 2018-10-15 16:42:29 +0200 | [diff] [blame] | 1477 | *texts = realloc(*texts, (1 + count) * sizeof **texts); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1478 | LY_CHECK_ERR_RET(!*texts, LOGMEM(ctx->ctx), LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1479 | |
| 1480 | /* get value */ |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 1481 | ret = get_argument(ctx, data, arg, &word, &buf, &word_len); |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1482 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1483 | |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1484 | INSERT_WORD(ctx, buf, (*texts)[count - 1], word, word_len); |
Radek Krejci | e7f84ae | 2018-10-15 16:42:29 +0200 | [diff] [blame] | 1485 | (*texts)[count] = NULL; /* NULL-termination of the array */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1486 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) { |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1487 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1488 | |
| 1489 | switch (kw) { |
| 1490 | case YANG_CUSTOM: |
| 1491 | ret = parse_ext(ctx, data, word, word_len, substmt, count - 1, exts); |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1492 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1493 | break; |
| 1494 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1495 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), lyext_substmt2str(substmt)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1496 | return LY_EVALID; |
| 1497 | } |
| 1498 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1499 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1500 | |
| 1501 | return ret; |
| 1502 | } |
| 1503 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1504 | /** |
| 1505 | * @brief Parse the config statement. |
| 1506 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1507 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1508 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 1509 | * @param[in,out] flags Flags to add to. |
| 1510 | * @param[in,out] exts Extension instances to add to. |
| 1511 | * |
| 1512 | * @return LY_ERR values. |
| 1513 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1514 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1515 | 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] | 1516 | { |
| 1517 | LY_ERR ret = 0; |
| 1518 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 1519 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1520 | enum yang_keyword kw; |
| 1521 | |
| 1522 | if (*flags & LYS_CONFIG_MASK) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1523 | LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "config"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1524 | return LY_EVALID; |
| 1525 | } |
| 1526 | |
| 1527 | /* get value */ |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 1528 | ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len); |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1529 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1530 | |
| 1531 | if ((word_len == 4) && !strncmp(word, "true", word_len)) { |
| 1532 | *flags |= LYS_CONFIG_W; |
| 1533 | } else if ((word_len == 5) && !strncmp(word, "false", word_len)) { |
| 1534 | *flags |= LYS_CONFIG_R; |
| 1535 | } else { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1536 | LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "config"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1537 | free(buf); |
| 1538 | return LY_EVALID; |
| 1539 | } |
| 1540 | free(buf); |
| 1541 | |
| 1542 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) { |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1543 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1544 | |
| 1545 | switch (kw) { |
| 1546 | case YANG_CUSTOM: |
| 1547 | 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] | 1548 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1549 | break; |
| 1550 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1551 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "config"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1552 | return LY_EVALID; |
| 1553 | } |
| 1554 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1555 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1556 | |
| 1557 | return ret; |
| 1558 | } |
| 1559 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1560 | /** |
| 1561 | * @brief Parse the mandatory statement. |
| 1562 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1563 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1564 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 1565 | * @param[in,out] flags Flags to add to. |
| 1566 | * @param[in,out] exts Extension instances to add to. |
| 1567 | * |
| 1568 | * @return LY_ERR values. |
| 1569 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1570 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1571 | 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] | 1572 | { |
| 1573 | LY_ERR ret = 0; |
| 1574 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 1575 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1576 | enum yang_keyword kw; |
| 1577 | |
| 1578 | if (*flags & LYS_MAND_MASK) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1579 | LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "mandatory"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1580 | return LY_EVALID; |
| 1581 | } |
| 1582 | |
| 1583 | /* get value */ |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 1584 | ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len); |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1585 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1586 | |
| 1587 | if ((word_len == 4) && !strncmp(word, "true", word_len)) { |
| 1588 | *flags |= LYS_MAND_TRUE; |
| 1589 | } else if ((word_len == 5) && !strncmp(word, "false", word_len)) { |
| 1590 | *flags |= LYS_MAND_FALSE; |
| 1591 | } else { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1592 | LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "mandatory"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1593 | free(buf); |
| 1594 | return LY_EVALID; |
| 1595 | } |
| 1596 | free(buf); |
| 1597 | |
| 1598 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) { |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1599 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1600 | |
| 1601 | switch (kw) { |
| 1602 | case YANG_CUSTOM: |
| 1603 | 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] | 1604 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1605 | break; |
| 1606 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1607 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "mandatory"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1608 | return LY_EVALID; |
| 1609 | } |
| 1610 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1611 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1612 | |
| 1613 | return ret; |
| 1614 | } |
| 1615 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1616 | /** |
| 1617 | * @brief Parse a restriction such as range or length. |
| 1618 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1619 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1620 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 1621 | * @param[in] restr_kw Type of this particular restriction. |
| 1622 | * @param[in,out] exts Extension instances to add to. |
| 1623 | * |
| 1624 | * @return LY_ERR values. |
| 1625 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1626 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1627 | 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] | 1628 | { |
| 1629 | LY_ERR ret = 0; |
| 1630 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 1631 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1632 | enum yang_keyword kw; |
| 1633 | |
| 1634 | /* get value */ |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 1635 | ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len); |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1636 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1637 | |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1638 | INSERT_WORD(ctx, buf, restr->arg, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1639 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) { |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1640 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1641 | |
| 1642 | switch (kw) { |
| 1643 | case YANG_DESCRIPTION: |
| 1644 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &restr->dsc, Y_STR_ARG, &restr->exts); |
| 1645 | break; |
| 1646 | case YANG_REFERENCE: |
| 1647 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &restr->ref, Y_STR_ARG, &restr->exts); |
| 1648 | break; |
| 1649 | case YANG_ERROR_APP_TAG: |
| 1650 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_ERRTAG, 0, &restr->eapptag, Y_STR_ARG, &restr->exts); |
| 1651 | break; |
| 1652 | case YANG_ERROR_MESSAGE: |
| 1653 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_ERRMSG, 0, &restr->emsg, Y_STR_ARG, &restr->exts); |
| 1654 | break; |
| 1655 | case YANG_CUSTOM: |
| 1656 | ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &restr->exts); |
| 1657 | break; |
| 1658 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1659 | 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] | 1660 | return LY_EVALID; |
| 1661 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1662 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1663 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1664 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1665 | |
| 1666 | return ret; |
| 1667 | } |
| 1668 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1669 | /** |
| 1670 | * @brief Parse a restriction that can have more instances such as must. |
| 1671 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1672 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1673 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 1674 | * @param[in] restr_kw Type of this particular restriction. |
| 1675 | * @param[in,out] restrs Restrictions to add to. |
| 1676 | * |
| 1677 | * @return LY_ERR values. |
| 1678 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1679 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1680 | 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] | 1681 | { |
| 1682 | struct lysp_restr *restr; |
| 1683 | |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame] | 1684 | LYSP_ARRAY_NEW_RET(ctx->ctx, *restrs, restr, LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1685 | |
| 1686 | return parse_restr(ctx, data, restr_kw, restr); |
| 1687 | } |
| 1688 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1689 | /** |
| 1690 | * @brief Parse the status statement. |
| 1691 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1692 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1693 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 1694 | * @param[in,out] flags Flags to add to. |
| 1695 | * @param[in,out] exts Extension instances to add to. |
| 1696 | * |
| 1697 | * @return LY_ERR values. |
| 1698 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1699 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1700 | 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] | 1701 | { |
| 1702 | LY_ERR ret = 0; |
| 1703 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 1704 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1705 | enum yang_keyword kw; |
| 1706 | |
| 1707 | if (*flags & LYS_STATUS_MASK) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1708 | LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "status"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1709 | return LY_EVALID; |
| 1710 | } |
| 1711 | |
| 1712 | /* get value */ |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 1713 | ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len); |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1714 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1715 | |
| 1716 | if ((word_len == 7) && !strncmp(word, "current", word_len)) { |
| 1717 | *flags |= LYS_STATUS_CURR; |
| 1718 | } else if ((word_len == 10) && !strncmp(word, "deprecated", word_len)) { |
| 1719 | *flags |= LYS_STATUS_DEPRC; |
| 1720 | } else if ((word_len == 8) && !strncmp(word, "obsolete", word_len)) { |
| 1721 | *flags |= LYS_STATUS_OBSLT; |
| 1722 | } else { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1723 | LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "status"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1724 | free(buf); |
| 1725 | return LY_EVALID; |
| 1726 | } |
| 1727 | free(buf); |
| 1728 | |
| 1729 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) { |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1730 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1731 | |
| 1732 | switch (kw) { |
| 1733 | case YANG_CUSTOM: |
| 1734 | 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] | 1735 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1736 | break; |
| 1737 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1738 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "status"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1739 | return LY_EVALID; |
| 1740 | } |
| 1741 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1742 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1743 | |
| 1744 | return ret; |
| 1745 | } |
| 1746 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1747 | /** |
| 1748 | * @brief Parse the when statement. |
| 1749 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1750 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1751 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 1752 | * @param[in,out] when_p When pointer to parse to. |
| 1753 | * |
| 1754 | * @return LY_ERR values. |
| 1755 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1756 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1757 | 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] | 1758 | { |
| 1759 | LY_ERR ret = 0; |
| 1760 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 1761 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1762 | enum yang_keyword kw; |
| 1763 | struct lysp_when *when; |
| 1764 | |
| 1765 | if (*when_p) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1766 | LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "when"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1767 | return LY_EVALID; |
| 1768 | } |
| 1769 | |
| 1770 | when = calloc(1, sizeof *when); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1771 | LY_CHECK_ERR_RET(!when, LOGMEM(ctx->ctx), LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1772 | *when_p = when; |
| 1773 | |
| 1774 | /* get value */ |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 1775 | ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len); |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1776 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1777 | |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1778 | INSERT_WORD(ctx, buf, when->cond, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1779 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) { |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1780 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1781 | |
| 1782 | switch (kw) { |
| 1783 | case YANG_DESCRIPTION: |
| 1784 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &when->dsc, Y_STR_ARG, &when->exts); |
| 1785 | break; |
| 1786 | case YANG_REFERENCE: |
| 1787 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &when->ref, Y_STR_ARG, &when->exts); |
| 1788 | break; |
| 1789 | case YANG_CUSTOM: |
| 1790 | ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &when->exts); |
| 1791 | break; |
| 1792 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1793 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "when"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1794 | return LY_EVALID; |
| 1795 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1796 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1797 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1798 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1799 | |
| 1800 | return ret; |
| 1801 | } |
| 1802 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1803 | /** |
| 1804 | * @brief Parse the anydata or anyxml statement. |
| 1805 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1806 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1807 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 1808 | * @param[in] kw Type of this particular keyword. |
| 1809 | * @param[in,out] siblings Siblings to add to. |
| 1810 | * |
| 1811 | * @return LY_ERR values. |
| 1812 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1813 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1814 | 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] | 1815 | { |
| 1816 | LY_ERR ret = 0; |
| 1817 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 1818 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1819 | struct lysp_node *iter; |
| 1820 | struct lysp_node_anydata *any; |
| 1821 | |
| 1822 | /* create structure */ |
| 1823 | any = calloc(1, sizeof *any); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1824 | LY_CHECK_ERR_RET(!any, LOGMEM(ctx->ctx), LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1825 | any->nodetype = kw == YANG_ANYDATA ? LYS_ANYDATA : LYS_ANYXML; |
| 1826 | |
| 1827 | /* insert into siblings */ |
| 1828 | if (!*siblings) { |
| 1829 | *siblings = (struct lysp_node *)any; |
| 1830 | } else { |
| 1831 | for (iter = *siblings; iter->next; iter = iter->next); |
| 1832 | iter->next = (struct lysp_node *)any; |
| 1833 | } |
| 1834 | |
| 1835 | /* get name */ |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 1836 | ret = get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len); |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1837 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1838 | |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1839 | INSERT_WORD(ctx, buf, any->name, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1840 | |
| 1841 | /* parse substatements */ |
| 1842 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) { |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1843 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1844 | |
| 1845 | switch (kw) { |
| 1846 | case YANG_CONFIG: |
| 1847 | ret = parse_config(ctx, data, &any->flags, &any->exts); |
| 1848 | break; |
| 1849 | case YANG_DESCRIPTION: |
| 1850 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &any->dsc, Y_STR_ARG, &any->exts); |
| 1851 | break; |
| 1852 | case YANG_IF_FEATURE: |
| 1853 | ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &any->iffeatures, Y_STR_ARG, &any->exts); |
| 1854 | break; |
| 1855 | case YANG_MANDATORY: |
| 1856 | ret = parse_mandatory(ctx, data, &any->flags, &any->exts); |
| 1857 | break; |
| 1858 | case YANG_MUST: |
| 1859 | ret = parse_restrs(ctx, data, kw, &any->musts); |
| 1860 | break; |
| 1861 | case YANG_REFERENCE: |
| 1862 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &any->ref, Y_STR_ARG, &any->exts); |
| 1863 | break; |
| 1864 | case YANG_STATUS: |
| 1865 | ret = parse_status(ctx, data, &any->flags, &any->exts); |
| 1866 | break; |
| 1867 | case YANG_WHEN: |
| 1868 | ret = parse_when(ctx, data, &any->when); |
| 1869 | break; |
| 1870 | case YANG_CUSTOM: |
| 1871 | ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &any->exts); |
| 1872 | break; |
| 1873 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1874 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1875 | (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] | 1876 | return LY_EVALID; |
| 1877 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1878 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1879 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1880 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1881 | |
| 1882 | return ret; |
| 1883 | } |
| 1884 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1885 | /** |
| 1886 | * @brief Parse the value or position statement. Substatement of type enum statement. |
| 1887 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1888 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1889 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 1890 | * @param[in] val_kw Type of this particular keyword. |
| 1891 | * @param[in,out] value Value to write to. |
| 1892 | * @param[in,out] flags Flags to write to. |
| 1893 | * @param[in,out] exts Extension instances to add to. |
| 1894 | * |
| 1895 | * @return LY_ERR values. |
| 1896 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1897 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1898 | 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] | 1899 | struct lysp_ext_instance **exts) |
| 1900 | { |
| 1901 | LY_ERR ret = 0; |
| 1902 | char *buf, *word, *ptr; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 1903 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1904 | long int num; |
| 1905 | unsigned long int unum; |
| 1906 | enum yang_keyword kw; |
| 1907 | |
| 1908 | if (*flags & LYS_SET_VALUE) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1909 | LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(val_kw)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1910 | return LY_EVALID; |
| 1911 | } |
| 1912 | *flags |= LYS_SET_VALUE; |
| 1913 | |
| 1914 | /* get value */ |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 1915 | ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len); |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1916 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1917 | |
| 1918 | 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] | 1919 | 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] | 1920 | free(buf); |
| 1921 | return LY_EVALID; |
| 1922 | } |
| 1923 | |
| 1924 | errno = 0; |
| 1925 | if (val_kw == YANG_VALUE) { |
| 1926 | num = strtol(word, &ptr, 10); |
| 1927 | } else { |
| 1928 | unum = strtoul(word, &ptr, 10); |
| 1929 | } |
| 1930 | /* we have not parsed the whole argument */ |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 1931 | if ((size_t)(ptr - word) != word_len) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1932 | 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] | 1933 | free(buf); |
| 1934 | return LY_EVALID; |
| 1935 | } |
| 1936 | if (errno == ERANGE) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1937 | 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] | 1938 | free(buf); |
| 1939 | return LY_EVALID; |
| 1940 | } |
| 1941 | if (val_kw == YANG_VALUE) { |
| 1942 | *value = num; |
| 1943 | } else { |
| 1944 | *value = unum; |
| 1945 | } |
| 1946 | free(buf); |
| 1947 | |
| 1948 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) { |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1949 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1950 | |
| 1951 | switch (kw) { |
| 1952 | case YANG_CUSTOM: |
| 1953 | 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] | 1954 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1955 | break; |
| 1956 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1957 | 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] | 1958 | return LY_EVALID; |
| 1959 | } |
| 1960 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1961 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1962 | |
| 1963 | return ret; |
| 1964 | } |
| 1965 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1966 | /** |
| 1967 | * @brief Parse the enum or bit statement. Substatement of type statement. |
| 1968 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1969 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1970 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 1971 | * @param[in] enum_kw Type of this particular keyword. |
| 1972 | * @param[in,out] enums Enums or bits to add to. |
| 1973 | * |
| 1974 | * @return LY_ERR values. |
| 1975 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1976 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1977 | 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] | 1978 | { |
| 1979 | LY_ERR ret = 0; |
| 1980 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 1981 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1982 | enum yang_keyword kw; |
| 1983 | struct lysp_type_enum *enm; |
| 1984 | |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame] | 1985 | LYSP_ARRAY_NEW_RET(ctx->ctx, *enums, enm, LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1986 | |
| 1987 | /* get value */ |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 1988 | ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len); |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1989 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1990 | |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1991 | INSERT_WORD(ctx, buf, enm->name, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1992 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) { |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1993 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1994 | |
| 1995 | switch (kw) { |
| 1996 | case YANG_DESCRIPTION: |
| 1997 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &enm->dsc, Y_STR_ARG, &enm->exts); |
| 1998 | break; |
| 1999 | case YANG_IF_FEATURE: |
| 2000 | ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &enm->iffeatures, Y_STR_ARG, &enm->exts); |
| 2001 | break; |
| 2002 | case YANG_REFERENCE: |
| 2003 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &enm->ref, Y_STR_ARG, &enm->exts); |
| 2004 | break; |
| 2005 | case YANG_STATUS: |
| 2006 | ret = parse_status(ctx, data, &enm->flags, &enm->exts); |
| 2007 | break; |
| 2008 | case YANG_VALUE: |
| 2009 | case YANG_POSITION: |
| 2010 | ret = parse_type_enum_value_pos(ctx, data, kw, &enm->value, &enm->flags, &enm->exts); |
| 2011 | break; |
| 2012 | case YANG_CUSTOM: |
| 2013 | ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &enm->exts); |
| 2014 | break; |
| 2015 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2016 | 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] | 2017 | return LY_EVALID; |
| 2018 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 2019 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2020 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 2021 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2022 | |
| 2023 | return ret; |
| 2024 | } |
| 2025 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2026 | /** |
| 2027 | * @brief Parse the fraction-digits statement. Substatement of type statement. |
| 2028 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2029 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2030 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 2031 | * @param[in,out] fracdig Value to write to. |
| 2032 | * @param[in,out] exts Extension instances to add to. |
| 2033 | * |
| 2034 | * @return LY_ERR values. |
| 2035 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2036 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2037 | 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] | 2038 | { |
| 2039 | LY_ERR ret = 0; |
| 2040 | char *buf, *word, *ptr; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 2041 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2042 | unsigned long int num; |
| 2043 | enum yang_keyword kw; |
| 2044 | |
| 2045 | if (*fracdig) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2046 | LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "fraction-digits"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2047 | return LY_EVALID; |
| 2048 | } |
| 2049 | |
| 2050 | /* get value */ |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 2051 | ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len); |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 2052 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2053 | |
| 2054 | if (!word_len || (word[0] == '0') || !isdigit(word[0])) { |
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 | |
| 2060 | errno = 0; |
| 2061 | num = strtoul(word, &ptr, 10); |
| 2062 | /* we have not parsed the whole argument */ |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 2063 | if ((size_t)(ptr - word) != word_len) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2064 | LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "fraction-digits"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2065 | free(buf); |
| 2066 | return LY_EVALID; |
| 2067 | } |
| 2068 | if ((errno == ERANGE) || (num > 18)) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2069 | LOGVAL_YANG(ctx, LY_VCODE_OOB, word_len, word, "fraction-digits"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2070 | free(buf); |
| 2071 | return LY_EVALID; |
| 2072 | } |
| 2073 | *fracdig = num; |
| 2074 | free(buf); |
| 2075 | |
| 2076 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) { |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 2077 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2078 | |
| 2079 | switch (kw) { |
| 2080 | case YANG_CUSTOM: |
| 2081 | 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] | 2082 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2083 | break; |
| 2084 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2085 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "fraction-digits"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2086 | return LY_EVALID; |
| 2087 | } |
| 2088 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 2089 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2090 | |
| 2091 | return ret; |
| 2092 | } |
| 2093 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2094 | /** |
| 2095 | * @brief Parse the require-instance statement. Substatement of type statement. |
| 2096 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2097 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2098 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 2099 | * @param[in,out] reqinst Value to write to. |
| 2100 | * @param[in,out] flags Flags to write to. |
| 2101 | * @param[in,out] exts Extension instances to add to. |
| 2102 | * |
| 2103 | * @return LY_ERR values. |
| 2104 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2105 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2106 | 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] | 2107 | struct lysp_ext_instance **exts) |
| 2108 | { |
| 2109 | LY_ERR ret = 0; |
| 2110 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 2111 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2112 | enum yang_keyword kw; |
| 2113 | |
| 2114 | if (*flags & LYS_SET_REQINST) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2115 | LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "require-instance"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2116 | return LY_EVALID; |
| 2117 | } |
| 2118 | *flags |= LYS_SET_REQINST; |
| 2119 | |
| 2120 | /* get value */ |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 2121 | ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len); |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 2122 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2123 | |
| 2124 | if ((word_len == 4) && !strncmp(word, "true", word_len)) { |
| 2125 | *reqinst = 1; |
| 2126 | } else if ((word_len != 5) || strncmp(word, "false", word_len)) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2127 | LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "require-instance"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2128 | free(buf); |
| 2129 | return LY_EVALID; |
| 2130 | } |
| 2131 | free(buf); |
| 2132 | |
| 2133 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) { |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 2134 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2135 | |
| 2136 | switch (kw) { |
| 2137 | case YANG_CUSTOM: |
| 2138 | 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] | 2139 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2140 | break; |
| 2141 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2142 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "require-instance"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2143 | return LY_EVALID; |
| 2144 | } |
| 2145 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 2146 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2147 | |
| 2148 | return ret; |
| 2149 | } |
| 2150 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2151 | /** |
| 2152 | * @brief Parse the modifier statement. Substatement of type pattern statement. |
| 2153 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2154 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2155 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 2156 | * @param[in,out] pat Value to write to. |
| 2157 | * @param[in,out] exts Extension instances to add to. |
| 2158 | * |
| 2159 | * @return LY_ERR values. |
| 2160 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2161 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2162 | 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] | 2163 | { |
| 2164 | LY_ERR ret = 0; |
| 2165 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 2166 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2167 | enum yang_keyword kw; |
| 2168 | |
| 2169 | if ((*pat)[0] == 0x15) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2170 | LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "modifier"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2171 | return LY_EVALID; |
| 2172 | } |
| 2173 | |
| 2174 | /* get value */ |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 2175 | ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len); |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 2176 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2177 | |
| 2178 | if ((word_len != 12) || strncmp(word, "invert-match", word_len)) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2179 | LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "modifier"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2180 | free(buf); |
| 2181 | return LY_EVALID; |
| 2182 | } |
| 2183 | free(buf); |
| 2184 | |
| 2185 | /* replace the value in the dictionary */ |
| 2186 | buf = malloc(strlen(*pat) + 1); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2187 | LY_CHECK_ERR_RET(!buf, LOGMEM(ctx->ctx), LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2188 | strcpy(buf, *pat); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2189 | lydict_remove(ctx->ctx, *pat); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2190 | |
| 2191 | assert(buf[0] == 0x06); |
| 2192 | buf[0] = 0x15; |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2193 | *pat = lydict_insert_zc(ctx->ctx, buf); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2194 | |
| 2195 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) { |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 2196 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2197 | |
| 2198 | switch (kw) { |
| 2199 | case YANG_CUSTOM: |
| 2200 | 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] | 2201 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2202 | break; |
| 2203 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2204 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "modifier"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2205 | return LY_EVALID; |
| 2206 | } |
| 2207 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 2208 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2209 | |
| 2210 | return ret; |
| 2211 | } |
| 2212 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2213 | /** |
| 2214 | * @brief Parse the pattern statement. Substatement of type statement. |
| 2215 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2216 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2217 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 2218 | * @param[in,out] patterns Restrictions to add to. |
| 2219 | * |
| 2220 | * @return LY_ERR values. |
| 2221 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2222 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2223 | 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] | 2224 | { |
| 2225 | LY_ERR ret = 0; |
| 2226 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 2227 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2228 | enum yang_keyword kw; |
| 2229 | struct lysp_restr *restr; |
| 2230 | |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame] | 2231 | LYSP_ARRAY_NEW_RET(ctx->ctx, *patterns, restr, LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2232 | |
| 2233 | /* get value */ |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 2234 | ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len); |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 2235 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2236 | |
| 2237 | /* add special meaning first byte */ |
| 2238 | if (buf) { |
| 2239 | buf = realloc(buf, word_len + 2); |
| 2240 | word = buf; |
| 2241 | } else { |
| 2242 | buf = malloc(word_len + 2); |
| 2243 | } |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2244 | LY_CHECK_ERR_RET(!buf, LOGMEM(ctx->ctx), LY_EMEM); |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame^] | 2245 | memmove(buf + 1, word, word_len); |
| 2246 | buf[0] = 0x06; /* pattern's default regular-match flag */ |
| 2247 | buf[word_len + 1] = '\0'; /* terminating NULL byte */ |
| 2248 | restr->arg = lydict_insert_zc(ctx->ctx, buf); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2249 | |
| 2250 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) { |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 2251 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2252 | |
| 2253 | switch (kw) { |
| 2254 | case YANG_DESCRIPTION: |
| 2255 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &restr->dsc, Y_STR_ARG, &restr->exts); |
| 2256 | break; |
| 2257 | case YANG_REFERENCE: |
| 2258 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &restr->ref, Y_STR_ARG, &restr->exts); |
| 2259 | break; |
| 2260 | case YANG_ERROR_APP_TAG: |
| 2261 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_ERRTAG, 0, &restr->eapptag, Y_STR_ARG, &restr->exts); |
| 2262 | break; |
| 2263 | case YANG_ERROR_MESSAGE: |
| 2264 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_ERRMSG, 0, &restr->emsg, Y_STR_ARG, &restr->exts); |
| 2265 | break; |
| 2266 | case YANG_MODIFIER: |
| 2267 | ret = parse_type_pattern_modifier(ctx, data, &restr->arg, &restr->exts); |
| 2268 | break; |
| 2269 | case YANG_CUSTOM: |
| 2270 | ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &restr->exts); |
| 2271 | break; |
| 2272 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2273 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "pattern"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2274 | return LY_EVALID; |
| 2275 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 2276 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2277 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 2278 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2279 | |
| 2280 | return ret; |
| 2281 | } |
| 2282 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2283 | /** |
| 2284 | * @brief Parse the type statement. |
| 2285 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2286 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2287 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 2288 | * @param[in,out] type Type to wrote to. |
| 2289 | * |
| 2290 | * @return LY_ERR values. |
| 2291 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2292 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2293 | 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] | 2294 | { |
| 2295 | LY_ERR ret = 0; |
| 2296 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 2297 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2298 | enum yang_keyword kw; |
| 2299 | struct lysp_type *nest_type; |
| 2300 | |
| 2301 | if (type->name) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2302 | LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "type"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2303 | return LY_EVALID; |
| 2304 | } |
| 2305 | |
| 2306 | /* get value */ |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 2307 | 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] | 2308 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2309 | |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2310 | INSERT_WORD(ctx, buf, type->name, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2311 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) { |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 2312 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2313 | |
| 2314 | switch (kw) { |
| 2315 | case YANG_BASE: |
| 2316 | ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_BASE, &type->bases, Y_PREF_IDENTIF_ARG, &type->exts); |
| 2317 | break; |
| 2318 | case YANG_BIT: |
| 2319 | ret = parse_type_enum(ctx, data, kw, &type->bits); |
| 2320 | break; |
| 2321 | case YANG_ENUM: |
| 2322 | ret = parse_type_enum(ctx, data, kw, &type->enums); |
| 2323 | break; |
| 2324 | case YANG_FRACTION_DIGITS: |
| 2325 | ret = parse_type_fracdigits(ctx, data, &type->fraction_digits, &type->exts); |
| 2326 | break; |
| 2327 | case YANG_LENGTH: |
| 2328 | if (type->length) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2329 | LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2330 | return LY_EVALID; |
| 2331 | } |
| 2332 | type->length = calloc(1, sizeof *type->length); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2333 | LY_CHECK_ERR_RET(!type->length, LOGMEM(ctx->ctx), LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2334 | |
| 2335 | ret = parse_restr(ctx, data, kw, type->length); |
| 2336 | break; |
| 2337 | case YANG_PATH: |
| 2338 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_PATH, 0, &type->path, Y_STR_ARG, &type->exts); |
| 2339 | break; |
| 2340 | case YANG_PATTERN: |
| 2341 | ret = parse_type_pattern(ctx, data, &type->patterns); |
| 2342 | break; |
| 2343 | case YANG_RANGE: |
| 2344 | if (type->range) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2345 | LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2346 | return LY_EVALID; |
| 2347 | } |
| 2348 | type->range = calloc(1, sizeof *type->range); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2349 | LY_CHECK_ERR_RET(!type->range, LOGMEM(ctx->ctx), LY_EVALID); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2350 | |
| 2351 | ret = parse_restr(ctx, data, kw, type->range); |
| 2352 | break; |
| 2353 | case YANG_REQUIRE_INSTANCE: |
| 2354 | ret = parse_type_reqinstance(ctx, data, &type->require_instance, &type->flags, &type->exts); |
| 2355 | break; |
| 2356 | case YANG_TYPE: |
| 2357 | { |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame] | 2358 | LYSP_ARRAY_NEW_RET(ctx->ctx, type->types, nest_type, LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2359 | } |
| 2360 | ret = parse_type(ctx, data, nest_type); |
| 2361 | break; |
| 2362 | case YANG_CUSTOM: |
| 2363 | ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &type->exts); |
| 2364 | break; |
| 2365 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2366 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "when"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2367 | return LY_EVALID; |
| 2368 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 2369 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2370 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 2371 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2372 | |
| 2373 | return ret; |
| 2374 | } |
| 2375 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2376 | /** |
| 2377 | * @brief Parse the leaf statement. |
| 2378 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2379 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2380 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 2381 | * @param[in,out] siblings Siblings to add to. |
| 2382 | * |
| 2383 | * @return LY_ERR values. |
| 2384 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2385 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2386 | 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] | 2387 | { |
| 2388 | LY_ERR ret = 0; |
| 2389 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 2390 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2391 | enum yang_keyword kw; |
| 2392 | struct lysp_node *iter; |
| 2393 | struct lysp_node_leaf *leaf; |
| 2394 | |
| 2395 | /* create structure */ |
| 2396 | leaf = calloc(1, sizeof *leaf); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2397 | LY_CHECK_ERR_RET(!leaf, LOGMEM(ctx->ctx), LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2398 | leaf->nodetype = LYS_LEAF; |
| 2399 | |
| 2400 | /* insert into siblings */ |
| 2401 | if (!*siblings) { |
| 2402 | *siblings = (struct lysp_node *)leaf; |
| 2403 | } else { |
| 2404 | for (iter = *siblings; iter->next; iter = iter->next); |
| 2405 | iter->next = (struct lysp_node *)leaf; |
| 2406 | } |
| 2407 | |
| 2408 | /* get name */ |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 2409 | ret = get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len); |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 2410 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2411 | |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2412 | INSERT_WORD(ctx, buf, leaf->name, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2413 | |
| 2414 | /* parse substatements */ |
| 2415 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) { |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 2416 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2417 | |
| 2418 | switch (kw) { |
| 2419 | case YANG_CONFIG: |
| 2420 | ret = parse_config(ctx, data, &leaf->flags, &leaf->exts); |
| 2421 | break; |
| 2422 | case YANG_DEFAULT: |
| 2423 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DEFAULT, 0, &leaf->dflt, Y_STR_ARG, &leaf->exts); |
| 2424 | break; |
| 2425 | case YANG_DESCRIPTION: |
| 2426 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &leaf->dsc, Y_STR_ARG, &leaf->exts); |
| 2427 | break; |
| 2428 | case YANG_IF_FEATURE: |
| 2429 | ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &leaf->iffeatures, Y_STR_ARG, &leaf->exts); |
| 2430 | break; |
| 2431 | case YANG_MANDATORY: |
| 2432 | ret = parse_mandatory(ctx, data, &leaf->flags, &leaf->exts); |
| 2433 | break; |
| 2434 | case YANG_MUST: |
| 2435 | ret = parse_restrs(ctx, data, kw, &leaf->musts); |
| 2436 | break; |
| 2437 | case YANG_REFERENCE: |
| 2438 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &leaf->ref, Y_STR_ARG, &leaf->exts); |
| 2439 | break; |
| 2440 | case YANG_STATUS: |
| 2441 | ret = parse_status(ctx, data, &leaf->flags, &leaf->exts); |
| 2442 | break; |
| 2443 | case YANG_TYPE: |
| 2444 | ret = parse_type(ctx, data, &leaf->type); |
| 2445 | break; |
| 2446 | case YANG_UNITS: |
| 2447 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_UNITS, 0, &leaf->units, Y_STR_ARG, &leaf->exts); |
| 2448 | break; |
| 2449 | case YANG_WHEN: |
| 2450 | ret = parse_when(ctx, data, &leaf->when); |
| 2451 | break; |
| 2452 | case YANG_CUSTOM: |
| 2453 | ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &leaf->exts); |
| 2454 | break; |
| 2455 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2456 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "leaf"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2457 | return LY_EVALID; |
| 2458 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 2459 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2460 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 2461 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2462 | |
| 2463 | /* mandatory substatements */ |
| 2464 | if (!leaf->type.name) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2465 | LOGVAL_YANG(ctx, LY_VCODE_MISSTMT, "type", "leaf"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2466 | return LY_EVALID; |
| 2467 | } |
| 2468 | |
| 2469 | return ret; |
| 2470 | } |
| 2471 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2472 | /** |
| 2473 | * @brief Parse the max-elements statement. |
| 2474 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2475 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2476 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 2477 | * @param[in,out] max Value to write to. |
| 2478 | * @param[in,out] flags Flags to write to. |
| 2479 | * @param[in,out] exts Extension instances to add to. |
| 2480 | * |
| 2481 | * @return LY_ERR values. |
| 2482 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2483 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2484 | 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] | 2485 | { |
| 2486 | LY_ERR ret = 0; |
| 2487 | char *buf, *word, *ptr; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 2488 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2489 | unsigned long int num; |
| 2490 | enum yang_keyword kw; |
| 2491 | |
| 2492 | if (*flags & LYS_SET_MAX) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2493 | LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "max-elements"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2494 | return LY_EVALID; |
| 2495 | } |
| 2496 | *flags |= LYS_SET_MAX; |
| 2497 | |
| 2498 | /* get value */ |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 2499 | ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len); |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 2500 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2501 | |
| 2502 | if (!word_len || (word[0] == '0') || ((word[0] != 'u') && !isdigit(word[0]))) { |
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 | |
| 2508 | if (strncmp(word, "unbounded", word_len)) { |
| 2509 | errno = 0; |
| 2510 | num = strtoul(word, &ptr, 10); |
| 2511 | /* we have not parsed the whole argument */ |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 2512 | if ((size_t)(ptr - word) != word_len) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2513 | LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "max-elements"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2514 | free(buf); |
| 2515 | return LY_EVALID; |
| 2516 | } |
| 2517 | if ((errno == ERANGE) || (num > UINT32_MAX)) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2518 | LOGVAL_YANG(ctx, LY_VCODE_OOB, word_len, word, "max-elements"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2519 | free(buf); |
| 2520 | return LY_EVALID; |
| 2521 | } |
| 2522 | |
| 2523 | *max = num; |
| 2524 | } |
| 2525 | free(buf); |
| 2526 | |
| 2527 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) { |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 2528 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2529 | |
| 2530 | switch (kw) { |
| 2531 | case YANG_CUSTOM: |
| 2532 | 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] | 2533 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2534 | break; |
| 2535 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2536 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "max-elements"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2537 | return LY_EVALID; |
| 2538 | } |
| 2539 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 2540 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2541 | |
| 2542 | return ret; |
| 2543 | } |
| 2544 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2545 | /** |
| 2546 | * @brief Parse the min-elements statement. |
| 2547 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2548 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2549 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 2550 | * @param[in,out] min Value to write to. |
| 2551 | * @param[in,out] flags Flags to write to. |
| 2552 | * @param[in,out] exts Extension instances to add to. |
| 2553 | * |
| 2554 | * @return LY_ERR values. |
| 2555 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2556 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2557 | 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] | 2558 | { |
| 2559 | LY_ERR ret = 0; |
| 2560 | char *buf, *word, *ptr; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 2561 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2562 | unsigned long int num; |
| 2563 | enum yang_keyword kw; |
| 2564 | |
| 2565 | if (*flags & LYS_SET_MIN) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2566 | LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "min-elements"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2567 | return LY_EVALID; |
| 2568 | } |
| 2569 | *flags |= LYS_SET_MIN; |
| 2570 | |
| 2571 | /* get value */ |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 2572 | ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len); |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 2573 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2574 | |
| 2575 | if (!word_len || !isdigit(word[0]) || ((word[0] == '0') && (word_len > 1))) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2576 | LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "min-elements"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2577 | free(buf); |
| 2578 | return LY_EVALID; |
| 2579 | } |
| 2580 | |
| 2581 | errno = 0; |
| 2582 | num = strtoul(word, &ptr, 10); |
| 2583 | /* we have not parsed the whole argument */ |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 2584 | if ((size_t)(ptr - word) != word_len) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2585 | LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "min-elements"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2586 | free(buf); |
| 2587 | return LY_EVALID; |
| 2588 | } |
| 2589 | if ((errno == ERANGE) || (num > UINT32_MAX)) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2590 | LOGVAL_YANG(ctx, LY_VCODE_OOB, word_len, word, "min-elements"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2591 | free(buf); |
| 2592 | return LY_EVALID; |
| 2593 | } |
| 2594 | *min = num; |
| 2595 | free(buf); |
| 2596 | |
| 2597 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) { |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 2598 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2599 | |
| 2600 | switch (kw) { |
| 2601 | case YANG_CUSTOM: |
| 2602 | 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] | 2603 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2604 | break; |
| 2605 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2606 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "min-elements"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2607 | return LY_EVALID; |
| 2608 | } |
| 2609 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 2610 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2611 | |
| 2612 | return ret; |
| 2613 | } |
| 2614 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2615 | /** |
| 2616 | * @brief Parse the ordered-by statement. |
| 2617 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2618 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2619 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 2620 | * @param[in,out] flags Flags to write to. |
| 2621 | * @param[in,out] exts Extension instances to add to. |
| 2622 | * |
| 2623 | * @return LY_ERR values. |
| 2624 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2625 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2626 | 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] | 2627 | { |
| 2628 | LY_ERR ret = 0; |
| 2629 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 2630 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2631 | enum yang_keyword kw; |
| 2632 | |
| 2633 | if (*flags & LYS_ORDBY_MASK) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2634 | LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "ordered-by"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2635 | return LY_EVALID; |
| 2636 | } |
| 2637 | |
| 2638 | /* get value */ |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 2639 | ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len); |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 2640 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2641 | |
| 2642 | if ((word_len == 6) && !strncmp(word, "system", word_len)) { |
| 2643 | *flags |= LYS_ORDBY_SYSTEM; |
| 2644 | } else if ((word_len == 4) && !strncmp(word, "user", word_len)) { |
| 2645 | *flags |= LYS_ORDBY_USER; |
| 2646 | } else { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2647 | LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "ordered-by"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2648 | free(buf); |
| 2649 | return LY_EVALID; |
| 2650 | } |
| 2651 | free(buf); |
| 2652 | |
| 2653 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) { |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 2654 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2655 | |
| 2656 | switch (kw) { |
| 2657 | case YANG_CUSTOM: |
| 2658 | 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] | 2659 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2660 | break; |
| 2661 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2662 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "ordered-by"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2663 | return LY_EVALID; |
| 2664 | } |
| 2665 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 2666 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2667 | |
| 2668 | return ret; |
| 2669 | } |
| 2670 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2671 | /** |
| 2672 | * @brief Parse the leaf-list statement. |
| 2673 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2674 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2675 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 2676 | * @param[in,out] siblings Siblings to add to. |
| 2677 | * |
| 2678 | * @return LY_ERR values. |
| 2679 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2680 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2681 | 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] | 2682 | { |
| 2683 | LY_ERR ret = 0; |
| 2684 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 2685 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2686 | enum yang_keyword kw; |
| 2687 | struct lysp_node *iter; |
| 2688 | struct lysp_node_leaflist *llist; |
| 2689 | |
| 2690 | /* create structure */ |
| 2691 | llist = calloc(1, sizeof *llist); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2692 | LY_CHECK_ERR_RET(!llist, LOGMEM(ctx->ctx), LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2693 | llist->nodetype = LYS_LEAFLIST; |
| 2694 | |
| 2695 | /* insert into siblings */ |
| 2696 | if (!*siblings) { |
| 2697 | *siblings = (struct lysp_node *)llist; |
| 2698 | } else { |
| 2699 | for (iter = *siblings; iter->next; iter = iter->next); |
| 2700 | iter->next = (struct lysp_node *)llist; |
| 2701 | } |
| 2702 | |
| 2703 | /* get name */ |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 2704 | ret = get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len); |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 2705 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2706 | |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2707 | INSERT_WORD(ctx, buf, llist->name, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2708 | |
| 2709 | /* parse substatements */ |
| 2710 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) { |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 2711 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2712 | |
| 2713 | switch (kw) { |
| 2714 | case YANG_CONFIG: |
| 2715 | ret = parse_config(ctx, data, &llist->flags, &llist->exts); |
| 2716 | break; |
| 2717 | case YANG_DEFAULT: |
| 2718 | ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_DEFAULT, &llist->dflts, Y_STR_ARG, &llist->exts); |
| 2719 | break; |
| 2720 | case YANG_DESCRIPTION: |
| 2721 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &llist->dsc, Y_STR_ARG, &llist->exts); |
| 2722 | break; |
| 2723 | case YANG_IF_FEATURE: |
| 2724 | ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &llist->iffeatures, Y_STR_ARG, &llist->exts); |
| 2725 | break; |
| 2726 | case YANG_MAX_ELEMENTS: |
| 2727 | ret = parse_maxelements(ctx, data, &llist->max, &llist->flags, &llist->exts); |
| 2728 | break; |
| 2729 | case YANG_MIN_ELEMENTS: |
| 2730 | ret = parse_minelements(ctx, data, &llist->min, &llist->flags, &llist->exts); |
| 2731 | break; |
| 2732 | case YANG_MUST: |
| 2733 | ret = parse_restrs(ctx, data, kw, &llist->musts); |
| 2734 | break; |
| 2735 | case YANG_ORDERED_BY: |
| 2736 | ret = parse_orderedby(ctx, data, &llist->flags, &llist->exts); |
| 2737 | break; |
| 2738 | case YANG_REFERENCE: |
| 2739 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &llist->ref, Y_STR_ARG, &llist->exts); |
| 2740 | break; |
| 2741 | case YANG_STATUS: |
| 2742 | ret = parse_status(ctx, data, &llist->flags, &llist->exts); |
| 2743 | break; |
| 2744 | case YANG_TYPE: |
| 2745 | ret = parse_type(ctx, data, &llist->type); |
| 2746 | break; |
| 2747 | case YANG_UNITS: |
| 2748 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_UNITS, 0, &llist->units, Y_STR_ARG, &llist->exts); |
| 2749 | break; |
| 2750 | case YANG_WHEN: |
| 2751 | ret = parse_when(ctx, data, &llist->when); |
| 2752 | break; |
| 2753 | case YANG_CUSTOM: |
| 2754 | ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &llist->exts); |
| 2755 | break; |
| 2756 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2757 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "llist"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2758 | return LY_EVALID; |
| 2759 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 2760 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2761 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 2762 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2763 | |
| 2764 | /* mandatory substatements */ |
| 2765 | if (!llist->type.name) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2766 | LOGVAL_YANG(ctx, LY_VCODE_MISSTMT, "type", "leaf-list"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2767 | return LY_EVALID; |
| 2768 | } |
| 2769 | |
| 2770 | return ret; |
| 2771 | } |
| 2772 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2773 | /** |
| 2774 | * @brief Parse the refine statement. |
| 2775 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2776 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2777 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 2778 | * @param[in,out] refines Refines to add to. |
| 2779 | * |
| 2780 | * @return LY_ERR values. |
| 2781 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2782 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2783 | 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] | 2784 | { |
| 2785 | LY_ERR ret = 0; |
| 2786 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 2787 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2788 | enum yang_keyword kw; |
| 2789 | struct lysp_refine *rf; |
| 2790 | |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame] | 2791 | LYSP_ARRAY_NEW_RET(ctx->ctx, *refines, rf, LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2792 | |
| 2793 | /* get value */ |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 2794 | ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len); |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 2795 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2796 | |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2797 | INSERT_WORD(ctx, buf, rf->nodeid, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2798 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) { |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 2799 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2800 | |
| 2801 | switch (kw) { |
| 2802 | case YANG_CONFIG: |
| 2803 | ret = parse_config(ctx, data, &rf->flags, &rf->exts); |
| 2804 | break; |
| 2805 | case YANG_DEFAULT: |
| 2806 | ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_DEFAULT, &rf->dflts, Y_STR_ARG, &rf->exts); |
| 2807 | break; |
| 2808 | case YANG_DESCRIPTION: |
| 2809 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &rf->dsc, Y_STR_ARG, &rf->exts); |
| 2810 | break; |
| 2811 | case YANG_IF_FEATURE: |
| 2812 | ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &rf->iffeatures, Y_STR_ARG, &rf->exts); |
| 2813 | break; |
| 2814 | case YANG_MAX_ELEMENTS: |
| 2815 | ret = parse_maxelements(ctx, data, &rf->max, &rf->flags, &rf->exts); |
| 2816 | break; |
| 2817 | case YANG_MIN_ELEMENTS: |
| 2818 | ret = parse_minelements(ctx, data, &rf->min, &rf->flags, &rf->exts); |
| 2819 | break; |
| 2820 | case YANG_MUST: |
| 2821 | ret = parse_restrs(ctx, data, kw, &rf->musts); |
| 2822 | break; |
| 2823 | case YANG_MANDATORY: |
| 2824 | ret = parse_mandatory(ctx, data, &rf->flags, &rf->exts); |
| 2825 | break; |
| 2826 | case YANG_REFERENCE: |
| 2827 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &rf->ref, Y_STR_ARG, &rf->exts); |
| 2828 | break; |
| 2829 | case YANG_PRESENCE: |
| 2830 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_PRESENCE, 0, &rf->presence, Y_STR_ARG, &rf->exts); |
| 2831 | break; |
| 2832 | case YANG_CUSTOM: |
| 2833 | ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &rf->exts); |
| 2834 | break; |
| 2835 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2836 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "refine"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2837 | return LY_EVALID; |
| 2838 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 2839 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2840 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 2841 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2842 | |
| 2843 | return ret; |
| 2844 | } |
| 2845 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2846 | /** |
| 2847 | * @brief Parse the typedef statement. |
| 2848 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2849 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2850 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 2851 | * @param[in,out] typedefs Typedefs to add to. |
| 2852 | * |
| 2853 | * @return LY_ERR values. |
| 2854 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2855 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2856 | 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] | 2857 | { |
| 2858 | LY_ERR ret = 0; |
| 2859 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 2860 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2861 | enum yang_keyword kw; |
| 2862 | struct lysp_tpdf *tpdf; |
| 2863 | |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame] | 2864 | LYSP_ARRAY_NEW_RET(ctx->ctx, *typedefs, tpdf, LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2865 | |
| 2866 | /* get value */ |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 2867 | ret = get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len); |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 2868 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2869 | |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2870 | INSERT_WORD(ctx, buf, tpdf->name, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2871 | |
| 2872 | /* parse substatements */ |
| 2873 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) { |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 2874 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2875 | |
| 2876 | switch (kw) { |
| 2877 | case YANG_DEFAULT: |
| 2878 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DEFAULT, 0, &tpdf->dflt, Y_STR_ARG, &tpdf->exts); |
| 2879 | break; |
| 2880 | case YANG_DESCRIPTION: |
| 2881 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &tpdf->dsc, Y_STR_ARG, &tpdf->exts); |
| 2882 | break; |
| 2883 | case YANG_REFERENCE: |
| 2884 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &tpdf->ref, Y_STR_ARG, &tpdf->exts); |
| 2885 | break; |
| 2886 | case YANG_STATUS: |
| 2887 | ret = parse_status(ctx, data, &tpdf->flags, &tpdf->exts); |
| 2888 | break; |
| 2889 | case YANG_TYPE: |
| 2890 | ret = parse_type(ctx, data, &tpdf->type); |
| 2891 | break; |
| 2892 | case YANG_UNITS: |
| 2893 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_UNITS, 0, &tpdf->units, Y_STR_ARG, &tpdf->exts); |
| 2894 | break; |
| 2895 | case YANG_CUSTOM: |
| 2896 | ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &tpdf->exts); |
| 2897 | break; |
| 2898 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2899 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "typedef"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2900 | return LY_EVALID; |
| 2901 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 2902 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2903 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 2904 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2905 | |
| 2906 | /* mandatory substatements */ |
| 2907 | if (!tpdf->type.name) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2908 | LOGVAL_YANG(ctx, LY_VCODE_MISSTMT, "type", "typedef"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2909 | return LY_EVALID; |
| 2910 | } |
| 2911 | |
| 2912 | return ret; |
| 2913 | } |
| 2914 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2915 | /** |
| 2916 | * @brief Parse the input or output statement. |
| 2917 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2918 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2919 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 2920 | * @param[in] kw Type of this particular keyword |
| 2921 | * @param[in,out] inout_p Input/output pointer to write to. |
| 2922 | * |
| 2923 | * @return LY_ERR values. |
| 2924 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2925 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2926 | 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] | 2927 | { |
| 2928 | LY_ERR ret = 0; |
| 2929 | char *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 2930 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2931 | struct lysp_action_inout *inout; |
| 2932 | |
| 2933 | if (*inout_p) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2934 | LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2935 | return LY_EVALID; |
| 2936 | } |
| 2937 | |
| 2938 | /* create structure */ |
| 2939 | inout = calloc(1, sizeof *inout); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2940 | LY_CHECK_ERR_RET(!inout, LOGMEM(ctx->ctx), LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2941 | *inout_p = inout; |
| 2942 | |
| 2943 | /* parse substatements */ |
| 2944 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) { |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 2945 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2946 | |
| 2947 | switch (kw) { |
| 2948 | case YANG_ANYDATA: |
| 2949 | case YANG_ANYXML: |
| 2950 | ret = parse_any(ctx, data, kw, &inout->data); |
| 2951 | break; |
| 2952 | case YANG_CHOICE: |
| 2953 | ret = parse_choice(ctx, data, &inout->data); |
| 2954 | break; |
| 2955 | case YANG_CONTAINER: |
| 2956 | ret = parse_container(ctx, data, &inout->data); |
| 2957 | break; |
| 2958 | case YANG_LEAF: |
| 2959 | ret = parse_leaf(ctx, data, &inout->data); |
| 2960 | break; |
| 2961 | case YANG_LEAF_LIST: |
| 2962 | ret = parse_leaflist(ctx, data, &inout->data); |
| 2963 | break; |
| 2964 | case YANG_LIST: |
| 2965 | ret = parse_list(ctx, data, &inout->data); |
| 2966 | break; |
| 2967 | case YANG_USES: |
| 2968 | ret = parse_uses(ctx, data, &inout->data); |
| 2969 | break; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2970 | case YANG_TYPEDEF: |
| 2971 | ret = parse_typedef(ctx, data, &inout->typedefs); |
| 2972 | break; |
| 2973 | case YANG_MUST: |
| 2974 | ret = parse_restrs(ctx, data, kw, &inout->musts); |
| 2975 | break; |
| 2976 | case YANG_GROUPING: |
| 2977 | ret = parse_grouping(ctx, data, &inout->groupings); |
| 2978 | break; |
| 2979 | case YANG_CUSTOM: |
| 2980 | ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &inout->exts); |
| 2981 | break; |
| 2982 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2983 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "input/output"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2984 | return LY_EVALID; |
| 2985 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 2986 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2987 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 2988 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2989 | |
| 2990 | return ret; |
| 2991 | } |
| 2992 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2993 | /** |
| 2994 | * @brief Parse the action statement. |
| 2995 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2996 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2997 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 2998 | * @param[in,out] actions Actions to add to. |
| 2999 | * |
| 3000 | * @return LY_ERR values. |
| 3001 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3002 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3003 | 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] | 3004 | { |
| 3005 | LY_ERR ret = 0; |
| 3006 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 3007 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3008 | enum yang_keyword kw; |
| 3009 | struct lysp_action *act; |
| 3010 | |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame] | 3011 | LYSP_ARRAY_NEW_RET(ctx->ctx, *actions, act, LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3012 | |
| 3013 | /* get value */ |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 3014 | ret = get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len); |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 3015 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3016 | |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3017 | INSERT_WORD(ctx, buf, act->name, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3018 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) { |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 3019 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3020 | |
| 3021 | switch (kw) { |
| 3022 | case YANG_DESCRIPTION: |
| 3023 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &act->dsc, Y_STR_ARG, &act->exts); |
| 3024 | break; |
| 3025 | case YANG_IF_FEATURE: |
| 3026 | ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &act->iffeatures, Y_STR_ARG, &act->exts); |
| 3027 | break; |
| 3028 | case YANG_REFERENCE: |
| 3029 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &act->ref, Y_STR_ARG, &act->exts); |
| 3030 | break; |
| 3031 | case YANG_STATUS: |
| 3032 | ret = parse_status(ctx, data, &act->flags, &act->exts); |
| 3033 | break; |
| 3034 | |
| 3035 | case YANG_INPUT: |
| 3036 | ret = parse_inout(ctx, data, kw, &act->input); |
| 3037 | break; |
| 3038 | case YANG_OUTPUT: |
| 3039 | ret = parse_inout(ctx, data, kw, &act->output); |
| 3040 | break; |
| 3041 | |
| 3042 | case YANG_TYPEDEF: |
| 3043 | ret = parse_typedef(ctx, data, &act->typedefs); |
| 3044 | break; |
| 3045 | case YANG_GROUPING: |
| 3046 | ret = parse_grouping(ctx, data, &act->groupings); |
| 3047 | break; |
| 3048 | case YANG_CUSTOM: |
| 3049 | ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &act->exts); |
| 3050 | break; |
| 3051 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3052 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "action"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3053 | return LY_EVALID; |
| 3054 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 3055 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3056 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 3057 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3058 | |
| 3059 | return ret; |
| 3060 | } |
| 3061 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3062 | /** |
| 3063 | * @brief Parse the notification statement. |
| 3064 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3065 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3066 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 3067 | * @param[in,out] notifs Notifications to add to. |
| 3068 | * |
| 3069 | * @return LY_ERR values. |
| 3070 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3071 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3072 | 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] | 3073 | { |
| 3074 | LY_ERR ret = 0; |
| 3075 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 3076 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3077 | enum yang_keyword kw; |
| 3078 | struct lysp_notif *notif; |
| 3079 | |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame] | 3080 | LYSP_ARRAY_NEW_RET(ctx->ctx, *notifs, notif, LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3081 | |
| 3082 | /* get value */ |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 3083 | ret = get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len); |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 3084 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3085 | |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3086 | INSERT_WORD(ctx, buf, notif->name, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3087 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) { |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 3088 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3089 | |
| 3090 | switch (kw) { |
| 3091 | case YANG_DESCRIPTION: |
| 3092 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, ¬if->dsc, Y_STR_ARG, ¬if->exts); |
| 3093 | break; |
| 3094 | case YANG_IF_FEATURE: |
| 3095 | ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, ¬if->iffeatures, Y_STR_ARG, ¬if->exts); |
| 3096 | break; |
| 3097 | case YANG_REFERENCE: |
| 3098 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, ¬if->ref, Y_STR_ARG, ¬if->exts); |
| 3099 | break; |
| 3100 | case YANG_STATUS: |
| 3101 | ret = parse_status(ctx, data, ¬if->flags, ¬if->exts); |
| 3102 | break; |
| 3103 | |
| 3104 | case YANG_ANYDATA: |
| 3105 | case YANG_ANYXML: |
| 3106 | ret = parse_any(ctx, data, kw, ¬if->data); |
| 3107 | break; |
| 3108 | case YANG_CHOICE: |
| 3109 | ret = parse_case(ctx, data, ¬if->data); |
| 3110 | break; |
| 3111 | case YANG_CONTAINER: |
| 3112 | ret = parse_container(ctx, data, ¬if->data); |
| 3113 | break; |
| 3114 | case YANG_LEAF: |
| 3115 | ret = parse_leaf(ctx, data, ¬if->data); |
| 3116 | break; |
| 3117 | case YANG_LEAF_LIST: |
| 3118 | ret = parse_leaflist(ctx, data, ¬if->data); |
| 3119 | break; |
| 3120 | case YANG_LIST: |
| 3121 | ret = parse_list(ctx, data, ¬if->data); |
| 3122 | break; |
| 3123 | case YANG_USES: |
| 3124 | ret = parse_uses(ctx, data, ¬if->data); |
| 3125 | break; |
| 3126 | |
| 3127 | case YANG_MUST: |
| 3128 | ret = parse_restrs(ctx, data, kw, ¬if->musts); |
| 3129 | break; |
| 3130 | case YANG_TYPEDEF: |
| 3131 | ret = parse_typedef(ctx, data, ¬if->typedefs); |
| 3132 | break; |
| 3133 | case YANG_GROUPING: |
| 3134 | ret = parse_grouping(ctx, data, ¬if->groupings); |
| 3135 | break; |
| 3136 | case YANG_CUSTOM: |
| 3137 | ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, ¬if->exts); |
| 3138 | break; |
| 3139 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3140 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "notification"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3141 | return LY_EVALID; |
| 3142 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 3143 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3144 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 3145 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3146 | |
| 3147 | return ret; |
| 3148 | } |
| 3149 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3150 | /** |
| 3151 | * @brief Parse the grouping statement. |
| 3152 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3153 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3154 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 3155 | * @param[in,out] groupings Groupings to add to. |
| 3156 | * |
| 3157 | * @return LY_ERR values. |
| 3158 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3159 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3160 | 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] | 3161 | { |
| 3162 | LY_ERR ret = 0; |
| 3163 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 3164 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3165 | enum yang_keyword kw; |
| 3166 | struct lysp_grp *grp; |
| 3167 | |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame] | 3168 | LYSP_ARRAY_NEW_RET(ctx->ctx, *groupings, grp, LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3169 | |
| 3170 | /* get value */ |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 3171 | ret = get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len); |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 3172 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3173 | |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3174 | INSERT_WORD(ctx, buf, grp->name, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3175 | |
| 3176 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) { |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 3177 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3178 | |
| 3179 | switch (kw) { |
| 3180 | case YANG_DESCRIPTION: |
| 3181 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &grp->dsc, Y_STR_ARG, &grp->exts); |
| 3182 | break; |
| 3183 | case YANG_REFERENCE: |
| 3184 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &grp->ref, Y_STR_ARG, &grp->exts); |
| 3185 | break; |
| 3186 | case YANG_STATUS: |
| 3187 | ret = parse_status(ctx, data, &grp->flags, &grp->exts); |
| 3188 | break; |
| 3189 | |
| 3190 | case YANG_ANYDATA: |
| 3191 | case YANG_ANYXML: |
| 3192 | ret = parse_any(ctx, data, kw, &grp->data); |
| 3193 | break; |
| 3194 | case YANG_CHOICE: |
| 3195 | ret = parse_choice(ctx, data, &grp->data); |
| 3196 | break; |
| 3197 | case YANG_CONTAINER: |
| 3198 | ret = parse_container(ctx, data, &grp->data); |
| 3199 | break; |
| 3200 | case YANG_LEAF: |
| 3201 | ret = parse_leaf(ctx, data, &grp->data); |
| 3202 | break; |
| 3203 | case YANG_LEAF_LIST: |
| 3204 | ret = parse_leaflist(ctx, data, &grp->data); |
| 3205 | break; |
| 3206 | case YANG_LIST: |
| 3207 | ret = parse_list(ctx, data, &grp->data); |
| 3208 | break; |
| 3209 | case YANG_USES: |
| 3210 | ret = parse_uses(ctx, data, &grp->data); |
| 3211 | break; |
| 3212 | |
| 3213 | case YANG_TYPEDEF: |
| 3214 | ret = parse_typedef(ctx, data, &grp->typedefs); |
| 3215 | break; |
| 3216 | case YANG_ACTION: |
| 3217 | ret = parse_action(ctx, data, &grp->actions); |
| 3218 | break; |
| 3219 | case YANG_GROUPING: |
| 3220 | ret = parse_grouping(ctx, data, &grp->groupings); |
| 3221 | break; |
| 3222 | case YANG_NOTIFICATION: |
| 3223 | ret = parse_notif(ctx, data, &grp->notifs); |
| 3224 | break; |
| 3225 | case YANG_CUSTOM: |
| 3226 | ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &grp->exts); |
| 3227 | break; |
| 3228 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3229 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "augment"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3230 | return LY_EVALID; |
| 3231 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 3232 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3233 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 3234 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3235 | |
| 3236 | return ret; |
| 3237 | } |
| 3238 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3239 | /** |
| 3240 | * @brief Parse the refine statement. |
| 3241 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3242 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3243 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 3244 | * @param[in,out] augments Augments to add to. |
| 3245 | * |
| 3246 | * @return LY_ERR values. |
| 3247 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3248 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3249 | 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] | 3250 | { |
| 3251 | LY_ERR ret = 0; |
| 3252 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 3253 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3254 | enum yang_keyword kw; |
| 3255 | struct lysp_augment *aug; |
| 3256 | |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame] | 3257 | LYSP_ARRAY_NEW_RET(ctx->ctx, *augments, aug, LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3258 | |
| 3259 | /* get value */ |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 3260 | ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len); |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 3261 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3262 | |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3263 | INSERT_WORD(ctx, buf, aug->nodeid, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3264 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) { |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 3265 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3266 | |
| 3267 | switch (kw) { |
| 3268 | case YANG_DESCRIPTION: |
| 3269 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &aug->dsc, Y_STR_ARG, &aug->exts); |
| 3270 | break; |
| 3271 | case YANG_IF_FEATURE: |
| 3272 | ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &aug->iffeatures, Y_STR_ARG, &aug->exts); |
| 3273 | break; |
| 3274 | case YANG_REFERENCE: |
| 3275 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &aug->ref, Y_STR_ARG, &aug->exts); |
| 3276 | break; |
| 3277 | case YANG_STATUS: |
| 3278 | ret = parse_status(ctx, data, &aug->flags, &aug->exts); |
| 3279 | break; |
| 3280 | case YANG_WHEN: |
| 3281 | ret = parse_when(ctx, data, &aug->when); |
| 3282 | break; |
| 3283 | |
| 3284 | case YANG_ANYDATA: |
| 3285 | case YANG_ANYXML: |
| 3286 | ret = parse_any(ctx, data, kw, &aug->child); |
| 3287 | break; |
| 3288 | case YANG_CASE: |
| 3289 | ret = parse_case(ctx, data, &aug->child); |
| 3290 | break; |
| 3291 | case YANG_CHOICE: |
| 3292 | ret = parse_choice(ctx, data, &aug->child); |
| 3293 | break; |
| 3294 | case YANG_CONTAINER: |
| 3295 | ret = parse_container(ctx, data, &aug->child); |
| 3296 | break; |
| 3297 | case YANG_LEAF: |
| 3298 | ret = parse_leaf(ctx, data, &aug->child); |
| 3299 | break; |
| 3300 | case YANG_LEAF_LIST: |
| 3301 | ret = parse_leaflist(ctx, data, &aug->child); |
| 3302 | break; |
| 3303 | case YANG_LIST: |
| 3304 | ret = parse_list(ctx, data, &aug->child); |
| 3305 | break; |
| 3306 | case YANG_USES: |
| 3307 | ret = parse_uses(ctx, data, &aug->child); |
| 3308 | break; |
| 3309 | |
| 3310 | case YANG_ACTION: |
| 3311 | ret = parse_action(ctx, data, &aug->actions); |
| 3312 | break; |
| 3313 | case YANG_NOTIFICATION: |
| 3314 | ret = parse_notif(ctx, data, &aug->notifs); |
| 3315 | break; |
| 3316 | case YANG_CUSTOM: |
| 3317 | ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &aug->exts); |
| 3318 | break; |
| 3319 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3320 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "augment"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3321 | return LY_EVALID; |
| 3322 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 3323 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3324 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 3325 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3326 | |
| 3327 | return ret; |
| 3328 | } |
| 3329 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3330 | /** |
| 3331 | * @brief Parse the uses statement. |
| 3332 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3333 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3334 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 3335 | * @param[in,out] siblings Siblings to add to. |
| 3336 | * |
| 3337 | * @return LY_ERR values. |
| 3338 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3339 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3340 | 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] | 3341 | { |
| 3342 | LY_ERR ret = 0; |
| 3343 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 3344 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3345 | enum yang_keyword kw; |
| 3346 | struct lysp_node *iter; |
| 3347 | struct lysp_node_uses *uses; |
| 3348 | |
| 3349 | /* create structure */ |
| 3350 | uses = calloc(1, sizeof *uses); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3351 | LY_CHECK_ERR_RET(!uses, LOGMEM(ctx->ctx), LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3352 | uses->nodetype = LYS_USES; |
| 3353 | |
| 3354 | /* insert into siblings */ |
| 3355 | if (!*siblings) { |
| 3356 | *siblings = (struct lysp_node *)uses; |
| 3357 | } else { |
| 3358 | for (iter = *siblings; iter->next; iter = iter->next); |
| 3359 | iter->next = (struct lysp_node *)uses; |
| 3360 | } |
| 3361 | |
| 3362 | /* get name */ |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 3363 | 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] | 3364 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3365 | |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3366 | INSERT_WORD(ctx, buf, uses->name, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3367 | |
| 3368 | /* parse substatements */ |
| 3369 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) { |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 3370 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3371 | |
| 3372 | switch (kw) { |
| 3373 | case YANG_DESCRIPTION: |
| 3374 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &uses->dsc, Y_STR_ARG, &uses->exts); |
| 3375 | break; |
| 3376 | case YANG_IF_FEATURE: |
| 3377 | ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &uses->iffeatures, Y_STR_ARG, &uses->exts); |
| 3378 | break; |
| 3379 | case YANG_REFERENCE: |
| 3380 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &uses->ref, Y_STR_ARG, &uses->exts); |
| 3381 | break; |
| 3382 | case YANG_STATUS: |
| 3383 | ret = parse_status(ctx, data, &uses->flags, &uses->exts); |
| 3384 | break; |
| 3385 | case YANG_WHEN: |
| 3386 | ret = parse_when(ctx, data, &uses->when); |
| 3387 | break; |
| 3388 | |
| 3389 | case YANG_REFINE: |
| 3390 | ret = parse_refine(ctx, data, &uses->refines); |
| 3391 | break; |
| 3392 | case YANG_AUGMENT: |
| 3393 | ret = parse_augment(ctx, data, &uses->augments); |
| 3394 | break; |
| 3395 | case YANG_CUSTOM: |
| 3396 | ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &uses->exts); |
| 3397 | break; |
| 3398 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3399 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "uses"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3400 | return LY_EVALID; |
| 3401 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 3402 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3403 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 3404 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3405 | |
| 3406 | return ret; |
| 3407 | } |
| 3408 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3409 | /** |
| 3410 | * @brief Parse the case statement. |
| 3411 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3412 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3413 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 3414 | * @param[in,out] siblings Siblings to add to. |
| 3415 | * |
| 3416 | * @return LY_ERR values. |
| 3417 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3418 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3419 | 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] | 3420 | { |
| 3421 | LY_ERR ret = 0; |
| 3422 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 3423 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3424 | enum yang_keyword kw; |
| 3425 | struct lysp_node *iter; |
| 3426 | struct lysp_node_case *cas; |
| 3427 | |
| 3428 | /* create structure */ |
| 3429 | cas = calloc(1, sizeof *cas); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3430 | LY_CHECK_ERR_RET(!cas, LOGMEM(ctx->ctx), LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3431 | cas->nodetype = LYS_CASE; |
| 3432 | |
| 3433 | /* insert into siblings */ |
| 3434 | if (!*siblings) { |
| 3435 | *siblings = (struct lysp_node *)cas; |
| 3436 | } else { |
| 3437 | for (iter = *siblings; iter->next; iter = iter->next); |
| 3438 | iter->next = (struct lysp_node *)cas; |
| 3439 | } |
| 3440 | |
| 3441 | /* get name */ |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 3442 | ret = get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len); |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 3443 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3444 | |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3445 | INSERT_WORD(ctx, buf, cas->name, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3446 | |
| 3447 | /* parse substatements */ |
| 3448 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) { |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 3449 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3450 | |
| 3451 | switch (kw) { |
| 3452 | case YANG_DESCRIPTION: |
| 3453 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &cas->dsc, Y_STR_ARG, &cas->exts); |
| 3454 | break; |
| 3455 | case YANG_IF_FEATURE: |
| 3456 | ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &cas->iffeatures, Y_STR_ARG, &cas->exts); |
| 3457 | break; |
| 3458 | case YANG_REFERENCE: |
| 3459 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &cas->ref, Y_STR_ARG, &cas->exts); |
| 3460 | break; |
| 3461 | case YANG_STATUS: |
| 3462 | ret = parse_status(ctx, data, &cas->flags, &cas->exts); |
| 3463 | break; |
| 3464 | case YANG_WHEN: |
| 3465 | ret = parse_when(ctx, data, &cas->when); |
| 3466 | break; |
| 3467 | |
| 3468 | case YANG_ANYDATA: |
| 3469 | case YANG_ANYXML: |
| 3470 | ret = parse_any(ctx, data, kw, &cas->child); |
| 3471 | break; |
| 3472 | case YANG_CHOICE: |
| 3473 | ret = parse_case(ctx, data, &cas->child); |
| 3474 | break; |
| 3475 | case YANG_CONTAINER: |
| 3476 | ret = parse_container(ctx, data, &cas->child); |
| 3477 | break; |
| 3478 | case YANG_LEAF: |
| 3479 | ret = parse_leaf(ctx, data, &cas->child); |
| 3480 | break; |
| 3481 | case YANG_LEAF_LIST: |
| 3482 | ret = parse_leaflist(ctx, data, &cas->child); |
| 3483 | break; |
| 3484 | case YANG_LIST: |
| 3485 | ret = parse_list(ctx, data, &cas->child); |
| 3486 | break; |
| 3487 | case YANG_USES: |
| 3488 | ret = parse_uses(ctx, data, &cas->child); |
| 3489 | break; |
| 3490 | case YANG_CUSTOM: |
| 3491 | ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &cas->exts); |
| 3492 | break; |
| 3493 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3494 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "case"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3495 | return LY_EVALID; |
| 3496 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 3497 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3498 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 3499 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3500 | |
| 3501 | return ret; |
| 3502 | } |
| 3503 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3504 | /** |
| 3505 | * @brief Parse the choice statement. |
| 3506 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3507 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3508 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 3509 | * @param[in,out] siblings Siblings to add to. |
| 3510 | * |
| 3511 | * @return LY_ERR values. |
| 3512 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3513 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3514 | 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] | 3515 | { |
| 3516 | LY_ERR ret = 0; |
| 3517 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 3518 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3519 | enum yang_keyword kw; |
| 3520 | struct lysp_node *iter; |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3521 | struct lysp_node_choice *choice; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3522 | |
| 3523 | /* create structure */ |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3524 | choice = calloc(1, sizeof *choice); |
| 3525 | LY_CHECK_ERR_RET(!choice, LOGMEM(ctx->ctx), LY_EMEM); |
| 3526 | choice->nodetype = LYS_CHOICE; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3527 | |
| 3528 | /* insert into siblings */ |
| 3529 | if (!*siblings) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3530 | *siblings = (struct lysp_node *)choice; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3531 | } else { |
| 3532 | for (iter = *siblings; iter->next; iter = iter->next); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3533 | iter->next = (struct lysp_node *)choice; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3534 | } |
| 3535 | |
| 3536 | /* get name */ |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 3537 | ret = get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len); |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 3538 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3539 | |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3540 | INSERT_WORD(ctx, buf, choice->name, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3541 | |
| 3542 | /* parse substatements */ |
| 3543 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) { |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 3544 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3545 | |
| 3546 | switch (kw) { |
| 3547 | case YANG_CONFIG: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3548 | ret = parse_config(ctx, data, &choice->flags, &choice->exts); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3549 | break; |
| 3550 | case YANG_DESCRIPTION: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3551 | 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] | 3552 | break; |
| 3553 | case YANG_IF_FEATURE: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3554 | 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] | 3555 | break; |
| 3556 | case YANG_MANDATORY: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3557 | ret = parse_mandatory(ctx, data, &choice->flags, &choice->exts); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3558 | break; |
| 3559 | case YANG_REFERENCE: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3560 | 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] | 3561 | break; |
| 3562 | case YANG_STATUS: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3563 | ret = parse_status(ctx, data, &choice->flags, &choice->exts); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3564 | break; |
| 3565 | case YANG_WHEN: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3566 | ret = parse_when(ctx, data, &choice->when); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3567 | break; |
| 3568 | case YANG_DEFAULT: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3569 | 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] | 3570 | break; |
| 3571 | |
| 3572 | case YANG_ANYDATA: |
| 3573 | case YANG_ANYXML: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3574 | ret = parse_any(ctx, data, kw, &choice->child); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3575 | break; |
| 3576 | case YANG_CASE: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3577 | ret = parse_case(ctx, data, &choice->child); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3578 | break; |
| 3579 | case YANG_CHOICE: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3580 | ret = parse_choice(ctx, data, &choice->child); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3581 | break; |
| 3582 | case YANG_CONTAINER: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3583 | ret = parse_container(ctx, data, &choice->child); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3584 | break; |
| 3585 | case YANG_LEAF: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3586 | ret = parse_leaf(ctx, data, &choice->child); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3587 | break; |
| 3588 | case YANG_LEAF_LIST: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3589 | ret = parse_leaflist(ctx, data, &choice->child); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3590 | break; |
| 3591 | case YANG_LIST: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3592 | ret = parse_list(ctx, data, &choice->child); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3593 | break; |
| 3594 | case YANG_CUSTOM: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3595 | 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] | 3596 | break; |
| 3597 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3598 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "choice"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3599 | return LY_EVALID; |
| 3600 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 3601 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3602 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 3603 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3604 | |
| 3605 | return ret; |
| 3606 | } |
| 3607 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3608 | /** |
| 3609 | * @brief Parse the container statement. |
| 3610 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3611 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3612 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 3613 | * @param[in,out] siblings Siblings to add to. |
| 3614 | * |
| 3615 | * @return LY_ERR values. |
| 3616 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3617 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3618 | 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] | 3619 | { |
| 3620 | LY_ERR ret = 0; |
| 3621 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 3622 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3623 | enum yang_keyword kw; |
| 3624 | struct lysp_node *iter; |
| 3625 | struct lysp_node_container *cont; |
| 3626 | |
| 3627 | /* create structure */ |
| 3628 | cont = calloc(1, sizeof *cont); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3629 | LY_CHECK_ERR_RET(!cont, LOGMEM(ctx->ctx), LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3630 | cont->nodetype = LYS_CONTAINER; |
| 3631 | |
| 3632 | /* insert into siblings */ |
| 3633 | if (!*siblings) { |
| 3634 | *siblings = (struct lysp_node *)cont; |
| 3635 | } else { |
| 3636 | for (iter = *siblings; iter->next; iter = iter->next); |
| 3637 | iter->next = (struct lysp_node *)cont; |
| 3638 | } |
| 3639 | |
| 3640 | /* get name */ |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 3641 | ret = get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len); |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 3642 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3643 | |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3644 | INSERT_WORD(ctx, buf, cont->name, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3645 | |
| 3646 | /* parse substatements */ |
| 3647 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) { |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 3648 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3649 | |
| 3650 | switch (kw) { |
| 3651 | case YANG_CONFIG: |
| 3652 | ret = parse_config(ctx, data, &cont->flags, &cont->exts); |
| 3653 | break; |
| 3654 | case YANG_DESCRIPTION: |
| 3655 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &cont->dsc, Y_STR_ARG, &cont->exts); |
| 3656 | break; |
| 3657 | case YANG_IF_FEATURE: |
| 3658 | ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &cont->iffeatures, Y_STR_ARG, &cont->exts); |
| 3659 | break; |
| 3660 | case YANG_REFERENCE: |
| 3661 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &cont->ref, Y_STR_ARG, &cont->exts); |
| 3662 | break; |
| 3663 | case YANG_STATUS: |
| 3664 | ret = parse_status(ctx, data, &cont->flags, &cont->exts); |
| 3665 | break; |
| 3666 | case YANG_WHEN: |
| 3667 | ret = parse_when(ctx, data, &cont->when); |
| 3668 | break; |
| 3669 | case YANG_PRESENCE: |
| 3670 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_PRESENCE, 0, &cont->presence, Y_STR_ARG, &cont->exts); |
| 3671 | break; |
| 3672 | |
| 3673 | case YANG_ANYDATA: |
| 3674 | case YANG_ANYXML: |
| 3675 | ret = parse_any(ctx, data, kw, &cont->child); |
| 3676 | break; |
| 3677 | case YANG_CHOICE: |
| 3678 | ret = parse_choice(ctx, data, &cont->child); |
| 3679 | break; |
| 3680 | case YANG_CONTAINER: |
| 3681 | ret = parse_container(ctx, data, &cont->child); |
| 3682 | break; |
| 3683 | case YANG_LEAF: |
| 3684 | ret = parse_leaf(ctx, data, &cont->child); |
| 3685 | break; |
| 3686 | case YANG_LEAF_LIST: |
| 3687 | ret = parse_leaflist(ctx, data, &cont->child); |
| 3688 | break; |
| 3689 | case YANG_LIST: |
| 3690 | ret = parse_list(ctx, data, &cont->child); |
| 3691 | break; |
| 3692 | case YANG_USES: |
| 3693 | ret = parse_uses(ctx, data, &cont->child); |
| 3694 | break; |
| 3695 | |
| 3696 | case YANG_TYPEDEF: |
| 3697 | ret = parse_typedef(ctx, data, &cont->typedefs); |
| 3698 | break; |
| 3699 | case YANG_MUST: |
| 3700 | ret = parse_restrs(ctx, data, kw, &cont->musts); |
| 3701 | break; |
| 3702 | case YANG_ACTION: |
| 3703 | ret = parse_action(ctx, data, &cont->actions); |
| 3704 | break; |
| 3705 | case YANG_GROUPING: |
| 3706 | ret = parse_grouping(ctx, data, &cont->groupings); |
| 3707 | break; |
| 3708 | case YANG_NOTIFICATION: |
| 3709 | ret = parse_notif(ctx, data, &cont->notifs); |
| 3710 | break; |
| 3711 | case YANG_CUSTOM: |
| 3712 | ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &cont->exts); |
| 3713 | break; |
| 3714 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3715 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "container"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3716 | return LY_EVALID; |
| 3717 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 3718 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3719 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 3720 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3721 | |
| 3722 | return ret; |
| 3723 | } |
| 3724 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3725 | /** |
| 3726 | * @brief Parse the list statement. |
| 3727 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3728 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3729 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 3730 | * @param[in,out] siblings Siblings to add to. |
| 3731 | * |
| 3732 | * @return LY_ERR values. |
| 3733 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3734 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3735 | 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] | 3736 | { |
| 3737 | LY_ERR ret = 0; |
| 3738 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 3739 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3740 | enum yang_keyword kw; |
| 3741 | struct lysp_node *iter; |
| 3742 | struct lysp_node_list *list; |
| 3743 | |
| 3744 | /* create structure */ |
| 3745 | list = calloc(1, sizeof *list); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3746 | LY_CHECK_ERR_RET(!list, LOGMEM(ctx->ctx), LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3747 | list->nodetype = LYS_LIST; |
| 3748 | |
| 3749 | /* insert into siblings */ |
| 3750 | if (!*siblings) { |
| 3751 | *siblings = (struct lysp_node *)list; |
| 3752 | } else { |
| 3753 | for (iter = *siblings; iter->next; iter = iter->next); |
| 3754 | iter->next = (struct lysp_node *)list; |
| 3755 | } |
| 3756 | |
| 3757 | /* get name */ |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 3758 | ret = get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len); |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 3759 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3760 | |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3761 | INSERT_WORD(ctx, buf, list->name, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3762 | |
| 3763 | /* parse substatements */ |
| 3764 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) { |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 3765 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3766 | |
| 3767 | switch (kw) { |
| 3768 | case YANG_CONFIG: |
| 3769 | ret = parse_config(ctx, data, &list->flags, &list->exts); |
| 3770 | break; |
| 3771 | case YANG_DESCRIPTION: |
| 3772 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &list->dsc, Y_STR_ARG, &list->exts); |
| 3773 | break; |
| 3774 | case YANG_IF_FEATURE: |
| 3775 | ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &list->iffeatures, Y_STR_ARG, &list->exts); |
| 3776 | break; |
| 3777 | case YANG_REFERENCE: |
| 3778 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &list->ref, Y_STR_ARG, &list->exts); |
| 3779 | break; |
| 3780 | case YANG_STATUS: |
| 3781 | ret = parse_status(ctx, data, &list->flags, &list->exts); |
| 3782 | break; |
| 3783 | case YANG_WHEN: |
| 3784 | ret = parse_when(ctx, data, &list->when); |
| 3785 | break; |
| 3786 | case YANG_KEY: |
| 3787 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_KEY, 0, &list->key, Y_STR_ARG, &list->exts); |
| 3788 | break; |
| 3789 | case YANG_MAX_ELEMENTS: |
| 3790 | ret = parse_maxelements(ctx, data, &list->max, &list->flags, &list->exts); |
| 3791 | break; |
| 3792 | case YANG_MIN_ELEMENTS: |
| 3793 | ret = parse_minelements(ctx, data, &list->min, &list->flags, &list->exts); |
| 3794 | break; |
| 3795 | case YANG_ORDERED_BY: |
| 3796 | ret = parse_orderedby(ctx, data, &list->flags, &list->exts); |
| 3797 | break; |
| 3798 | case YANG_UNIQUE: |
| 3799 | ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_UNIQUE, &list->uniques, Y_STR_ARG, &list->exts); |
| 3800 | break; |
| 3801 | |
| 3802 | case YANG_ANYDATA: |
| 3803 | case YANG_ANYXML: |
| 3804 | ret = parse_any(ctx, data, kw, &list->child); |
| 3805 | break; |
| 3806 | case YANG_CHOICE: |
| 3807 | ret = parse_choice(ctx, data, &list->child); |
| 3808 | break; |
| 3809 | case YANG_CONTAINER: |
| 3810 | ret = parse_container(ctx, data, &list->child); |
| 3811 | break; |
| 3812 | case YANG_LEAF: |
| 3813 | ret = parse_leaf(ctx, data, &list->child); |
| 3814 | break; |
| 3815 | case YANG_LEAF_LIST: |
| 3816 | ret = parse_leaflist(ctx, data, &list->child); |
| 3817 | break; |
| 3818 | case YANG_LIST: |
| 3819 | ret = parse_list(ctx, data, &list->child); |
| 3820 | break; |
| 3821 | case YANG_USES: |
| 3822 | ret = parse_uses(ctx, data, &list->child); |
| 3823 | break; |
| 3824 | |
| 3825 | case YANG_TYPEDEF: |
| 3826 | ret = parse_typedef(ctx, data, &list->typedefs); |
| 3827 | break; |
| 3828 | case YANG_MUST: |
| 3829 | ret = parse_restrs(ctx, data, kw, &list->musts); |
| 3830 | break; |
| 3831 | case YANG_ACTION: |
| 3832 | ret = parse_action(ctx, data, &list->actions); |
| 3833 | break; |
| 3834 | case YANG_GROUPING: |
| 3835 | ret = parse_grouping(ctx, data, &list->groupings); |
| 3836 | break; |
| 3837 | case YANG_NOTIFICATION: |
| 3838 | ret = parse_notif(ctx, data, &list->notifs); |
| 3839 | break; |
| 3840 | case YANG_CUSTOM: |
| 3841 | ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &list->exts); |
| 3842 | break; |
| 3843 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3844 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "container"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3845 | return LY_EVALID; |
| 3846 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 3847 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3848 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 3849 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3850 | |
| 3851 | return ret; |
| 3852 | } |
| 3853 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3854 | /** |
| 3855 | * @brief Parse the yin-element statement. |
| 3856 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3857 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3858 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 3859 | * @param[in,out] flags Flags to write to. |
| 3860 | * @param[in,out] exts Extension instances to add to. |
| 3861 | * |
| 3862 | * @return LY_ERR values. |
| 3863 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3864 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3865 | 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] | 3866 | { |
| 3867 | LY_ERR ret = 0; |
| 3868 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 3869 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3870 | enum yang_keyword kw; |
| 3871 | |
| 3872 | if (*flags & LYS_YINELEM_MASK) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3873 | LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "yin-element"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3874 | return LY_EVALID; |
| 3875 | } |
| 3876 | |
| 3877 | /* get value */ |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 3878 | ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len); |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 3879 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3880 | |
| 3881 | if ((word_len == 4) && !strncmp(word, "true", word_len)) { |
| 3882 | *flags |= LYS_YINELEM_TRUE; |
| 3883 | } else if ((word_len == 5) && !strncmp(word, "false", word_len)) { |
| 3884 | *flags |= LYS_YINELEM_FALSE; |
| 3885 | } else { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3886 | LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "yin-element"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3887 | free(buf); |
| 3888 | return LY_EVALID; |
| 3889 | } |
| 3890 | free(buf); |
| 3891 | |
| 3892 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) { |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 3893 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3894 | |
| 3895 | switch (kw) { |
| 3896 | case YANG_CUSTOM: |
| 3897 | 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] | 3898 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3899 | break; |
| 3900 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3901 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "yin-element"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3902 | return LY_EVALID; |
| 3903 | } |
| 3904 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 3905 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3906 | |
| 3907 | return ret; |
| 3908 | } |
| 3909 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3910 | /** |
| 3911 | * @brief Parse the yin-element statement. |
| 3912 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3913 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3914 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 3915 | * @param[in,out] argument Value to write to. |
| 3916 | * @param[in,out] flags Flags to write to. |
| 3917 | * @param[in,out] exts Extension instances to add to. |
| 3918 | * |
| 3919 | * @return LY_ERR values. |
| 3920 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3921 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3922 | 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] | 3923 | { |
| 3924 | LY_ERR ret = 0; |
| 3925 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 3926 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3927 | enum yang_keyword kw; |
| 3928 | |
| 3929 | if (*argument) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3930 | LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "argument"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3931 | return LY_EVALID; |
| 3932 | } |
| 3933 | |
| 3934 | /* get value */ |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 3935 | ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len); |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 3936 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3937 | |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3938 | INSERT_WORD(ctx, buf, *argument, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3939 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) { |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 3940 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3941 | |
| 3942 | switch (kw) { |
| 3943 | case YANG_YIN_ELEMENT: |
| 3944 | ret = parse_yinelement(ctx, data, flags, exts); |
| 3945 | break; |
| 3946 | case YANG_CUSTOM: |
| 3947 | ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_ARGUMENT, 0, exts); |
| 3948 | break; |
| 3949 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3950 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "argument"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3951 | return LY_EVALID; |
| 3952 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 3953 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3954 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 3955 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3956 | |
| 3957 | return ret; |
| 3958 | } |
| 3959 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3960 | /** |
| 3961 | * @brief Parse the extension statement. |
| 3962 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3963 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3964 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 3965 | * @param[in,out] extensions Extensions to add to. |
| 3966 | * |
| 3967 | * @return LY_ERR values. |
| 3968 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3969 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3970 | 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] | 3971 | { |
| 3972 | LY_ERR ret = 0; |
| 3973 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 3974 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3975 | enum yang_keyword kw; |
| 3976 | struct lysp_ext *ex; |
| 3977 | |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame] | 3978 | LYSP_ARRAY_NEW_RET(ctx->ctx, *extensions, ex, LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3979 | |
| 3980 | /* get value */ |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 3981 | ret = get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len); |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 3982 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3983 | |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3984 | INSERT_WORD(ctx, buf, ex->name, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3985 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) { |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 3986 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3987 | |
| 3988 | switch (kw) { |
| 3989 | case YANG_DESCRIPTION: |
| 3990 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &ex->dsc, Y_STR_ARG, &ex->exts); |
| 3991 | break; |
| 3992 | case YANG_REFERENCE: |
| 3993 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &ex->ref, Y_STR_ARG, &ex->exts); |
| 3994 | break; |
| 3995 | case YANG_STATUS: |
| 3996 | ret = parse_status(ctx, data, &ex->flags, &ex->exts); |
| 3997 | break; |
| 3998 | case YANG_ARGUMENT: |
| 3999 | ret = parse_argument(ctx, data, &ex->argument, &ex->flags, &ex->exts); |
| 4000 | break; |
| 4001 | case YANG_CUSTOM: |
| 4002 | ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &ex->exts); |
| 4003 | break; |
| 4004 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 4005 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "extension"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4006 | return LY_EVALID; |
| 4007 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 4008 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4009 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 4010 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4011 | |
| 4012 | return ret; |
| 4013 | } |
| 4014 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 4015 | /** |
| 4016 | * @brief Parse the deviate statement. |
| 4017 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 4018 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 4019 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 4020 | * @param[in,out] deviates Deviates to add to. |
| 4021 | * |
| 4022 | * @return LY_ERR values. |
| 4023 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4024 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 4025 | 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] | 4026 | { |
| 4027 | LY_ERR ret = 0; |
| 4028 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 4029 | size_t word_len, dev_mod; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4030 | enum yang_keyword kw; |
| 4031 | struct lysp_deviate *iter, *d; |
| 4032 | struct lysp_deviate_add *d_add = NULL; |
| 4033 | struct lysp_deviate_rpl *d_rpl = NULL; |
| 4034 | struct lysp_deviate_del *d_del = NULL; |
| 4035 | const char **d_units, ***d_uniques, ***d_dflts; |
| 4036 | struct lysp_restr **d_musts; |
| 4037 | uint16_t *d_flags; |
| 4038 | uint32_t *d_min, *d_max; |
| 4039 | |
| 4040 | /* get value */ |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 4041 | ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len); |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 4042 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4043 | |
| 4044 | if ((word_len == 13) && !strncmp(word, "not-supported", word_len)) { |
| 4045 | dev_mod = LYS_DEV_NOT_SUPPORTED; |
| 4046 | } else if ((word_len == 3) && !strncmp(word, "add", word_len)) { |
| 4047 | dev_mod = LYS_DEV_ADD; |
| 4048 | } else if ((word_len == 7) && !strncmp(word, "replace", word_len)) { |
| 4049 | dev_mod = LYS_DEV_REPLACE; |
| 4050 | } else if ((word_len == 6) && !strncmp(word, "delete", word_len)) { |
| 4051 | dev_mod = LYS_DEV_DELETE; |
| 4052 | } else { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 4053 | LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "deviate"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4054 | free(buf); |
| 4055 | return LY_EVALID; |
| 4056 | } |
| 4057 | free(buf); |
| 4058 | |
| 4059 | /* create structure */ |
| 4060 | switch (dev_mod) { |
| 4061 | case LYS_DEV_NOT_SUPPORTED: |
| 4062 | d = calloc(1, sizeof *d); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 4063 | LY_CHECK_ERR_RET(!d, LOGMEM(ctx->ctx), LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4064 | break; |
| 4065 | case LYS_DEV_ADD: |
| 4066 | d_add = calloc(1, sizeof *d_add); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 4067 | LY_CHECK_ERR_RET(!d_add, LOGMEM(ctx->ctx), LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4068 | d = (struct lysp_deviate *)d_add; |
| 4069 | d_units = &d_add->units; |
| 4070 | d_uniques = &d_add->uniques; |
| 4071 | d_dflts = &d_add->dflts; |
| 4072 | d_musts = &d_add->musts; |
| 4073 | d_flags = &d_add->flags; |
| 4074 | d_min = &d_add->min; |
| 4075 | d_max = &d_add->max; |
| 4076 | break; |
| 4077 | case LYS_DEV_REPLACE: |
| 4078 | d_rpl = calloc(1, sizeof *d_rpl); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 4079 | LY_CHECK_ERR_RET(!d_rpl, LOGMEM(ctx->ctx), LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4080 | d = (struct lysp_deviate *)d_rpl; |
| 4081 | d_units = &d_rpl->units; |
| 4082 | d_flags = &d_rpl->flags; |
| 4083 | d_min = &d_rpl->min; |
| 4084 | d_max = &d_rpl->max; |
| 4085 | break; |
| 4086 | case LYS_DEV_DELETE: |
| 4087 | d_del = calloc(1, sizeof *d_del); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 4088 | LY_CHECK_ERR_RET(!d_del, LOGMEM(ctx->ctx), LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4089 | d = (struct lysp_deviate *)d_del; |
| 4090 | d_units = &d_del->units; |
| 4091 | d_uniques = &d_del->uniques; |
| 4092 | d_dflts = &d_del->dflts; |
| 4093 | d_musts = &d_del->musts; |
| 4094 | d_flags = &d_del->flags; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4095 | break; |
| 4096 | default: |
| 4097 | assert(0); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 4098 | LOGINT_RET(ctx->ctx); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4099 | } |
| 4100 | d->mod = dev_mod; |
| 4101 | |
| 4102 | /* insert into siblings */ |
| 4103 | if (!*deviates) { |
| 4104 | *deviates = d; |
| 4105 | } else { |
| 4106 | for (iter = *deviates; iter->next; iter = iter->next); |
| 4107 | iter->next = d; |
| 4108 | } |
| 4109 | |
| 4110 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) { |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 4111 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4112 | |
| 4113 | switch (kw) { |
| 4114 | case YANG_CONFIG: |
| 4115 | switch (dev_mod) { |
| 4116 | case LYS_DEV_NOT_SUPPORTED: |
Michal Vasko | 492bec7 | 2018-09-18 13:11:10 +0200 | [diff] [blame] | 4117 | case LYS_DEV_DELETE: |
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 | default: |
| 4121 | ret = parse_config(ctx, data, d_flags, &d->exts); |
| 4122 | break; |
| 4123 | } |
| 4124 | break; |
| 4125 | case YANG_DEFAULT: |
| 4126 | switch (dev_mod) { |
| 4127 | case LYS_DEV_NOT_SUPPORTED: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 4128 | 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] | 4129 | return LY_EVALID; |
| 4130 | case LYS_DEV_REPLACE: |
| 4131 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DEFAULT, 0, &d_rpl->dflt, Y_STR_ARG, &d->exts); |
| 4132 | break; |
| 4133 | default: |
| 4134 | ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_DEFAULT, d_dflts, Y_STR_ARG, &d->exts); |
| 4135 | break; |
| 4136 | } |
| 4137 | break; |
| 4138 | case YANG_MANDATORY: |
| 4139 | switch (dev_mod) { |
| 4140 | case LYS_DEV_NOT_SUPPORTED: |
Michal Vasko | 492bec7 | 2018-09-18 13:11:10 +0200 | [diff] [blame] | 4141 | case LYS_DEV_DELETE: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 4142 | 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] | 4143 | return LY_EVALID; |
| 4144 | default: |
| 4145 | ret = parse_mandatory(ctx, data, d_flags, &d->exts); |
| 4146 | break; |
| 4147 | } |
| 4148 | break; |
| 4149 | case YANG_MAX_ELEMENTS: |
| 4150 | switch (dev_mod) { |
| 4151 | case LYS_DEV_NOT_SUPPORTED: |
Michal Vasko | 492bec7 | 2018-09-18 13:11:10 +0200 | [diff] [blame] | 4152 | case LYS_DEV_DELETE: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 4153 | 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] | 4154 | return LY_EVALID; |
| 4155 | default: |
| 4156 | ret = parse_maxelements(ctx, data, d_max, d_flags, &d->exts); |
| 4157 | break; |
| 4158 | } |
| 4159 | break; |
| 4160 | case YANG_MIN_ELEMENTS: |
| 4161 | switch (dev_mod) { |
| 4162 | case LYS_DEV_NOT_SUPPORTED: |
Michal Vasko | 492bec7 | 2018-09-18 13:11:10 +0200 | [diff] [blame] | 4163 | case LYS_DEV_DELETE: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 4164 | 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] | 4165 | return LY_EVALID; |
| 4166 | default: |
| 4167 | ret = parse_minelements(ctx, data, d_min, d_flags, &d->exts); |
| 4168 | break; |
| 4169 | } |
| 4170 | break; |
| 4171 | case YANG_MUST: |
| 4172 | switch (dev_mod) { |
| 4173 | case LYS_DEV_NOT_SUPPORTED: |
Michal Vasko | 492bec7 | 2018-09-18 13:11:10 +0200 | [diff] [blame] | 4174 | case LYS_DEV_REPLACE: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 4175 | 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] | 4176 | return LY_EVALID; |
| 4177 | default: |
| 4178 | ret = parse_restrs(ctx, data, kw, d_musts); |
| 4179 | break; |
| 4180 | } |
| 4181 | break; |
| 4182 | case YANG_TYPE: |
| 4183 | switch (dev_mod) { |
| 4184 | case LYS_DEV_NOT_SUPPORTED: |
| 4185 | case LYS_DEV_ADD: |
| 4186 | case LYS_DEV_DELETE: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 4187 | 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] | 4188 | return LY_EVALID; |
| 4189 | default: |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 4190 | if (d_rpl->type) { |
| 4191 | LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw)); |
| 4192 | return LY_EVALID; |
| 4193 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4194 | d_rpl->type = calloc(1, sizeof *d_rpl->type); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 4195 | LY_CHECK_ERR_RET(!d_rpl->type, LOGMEM(ctx->ctx), LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4196 | ret = parse_type(ctx, data, d_rpl->type); |
| 4197 | break; |
| 4198 | } |
| 4199 | break; |
| 4200 | case YANG_UNIQUE: |
| 4201 | switch (dev_mod) { |
| 4202 | case LYS_DEV_NOT_SUPPORTED: |
| 4203 | case LYS_DEV_REPLACE: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 4204 | 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] | 4205 | return LY_EVALID; |
| 4206 | default: |
| 4207 | ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_UNIQUE, d_uniques, Y_STR_ARG, &d->exts); |
| 4208 | break; |
| 4209 | } |
| 4210 | break; |
| 4211 | case YANG_UNITS: |
| 4212 | switch (dev_mod) { |
| 4213 | case LYS_DEV_NOT_SUPPORTED: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 4214 | 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] | 4215 | return LY_EVALID; |
| 4216 | default: |
| 4217 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_UNITS, 0, d_units, Y_STR_ARG, &d->exts); |
| 4218 | break; |
| 4219 | } |
| 4220 | break; |
| 4221 | case YANG_CUSTOM: |
| 4222 | ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &d->exts); |
| 4223 | break; |
| 4224 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 4225 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "deviate"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4226 | return LY_EVALID; |
| 4227 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 4228 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4229 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 4230 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4231 | |
| 4232 | return ret; |
| 4233 | } |
| 4234 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 4235 | /** |
| 4236 | * @brief Parse the deviation statement. |
| 4237 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 4238 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 4239 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 4240 | * @param[in,out] deviations Deviations to add to. |
| 4241 | * |
| 4242 | * @return LY_ERR values. |
| 4243 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4244 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 4245 | 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] | 4246 | { |
| 4247 | LY_ERR ret = 0; |
| 4248 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 4249 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4250 | enum yang_keyword kw; |
| 4251 | struct lysp_deviation *dev; |
| 4252 | |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame] | 4253 | LYSP_ARRAY_NEW_RET(ctx->ctx, *deviations, dev, LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4254 | |
| 4255 | /* get value */ |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 4256 | ret = get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len); |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 4257 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4258 | |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 4259 | INSERT_WORD(ctx, buf, dev->nodeid, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4260 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) { |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 4261 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4262 | |
| 4263 | switch (kw) { |
| 4264 | case YANG_DESCRIPTION: |
| 4265 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &dev->dsc, Y_STR_ARG, &dev->exts); |
| 4266 | break; |
| 4267 | case YANG_DEVIATE: |
| 4268 | ret = parse_deviate(ctx, data, &dev->deviates); |
| 4269 | break; |
| 4270 | case YANG_REFERENCE: |
| 4271 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &dev->ref, Y_STR_ARG, &dev->exts); |
| 4272 | break; |
| 4273 | case YANG_CUSTOM: |
| 4274 | ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &dev->exts); |
| 4275 | break; |
| 4276 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 4277 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "deviation"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4278 | return LY_EVALID; |
| 4279 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 4280 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4281 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 4282 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4283 | |
| 4284 | /* mandatory substatements */ |
| 4285 | if (!dev->deviates) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 4286 | LOGVAL_YANG(ctx, LY_VCODE_MISSTMT, "deviate", "deviation"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4287 | return LY_EVALID; |
| 4288 | } |
| 4289 | |
| 4290 | return ret; |
| 4291 | } |
| 4292 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 4293 | /** |
| 4294 | * @brief Parse the feature statement. |
| 4295 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 4296 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 4297 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 4298 | * @param[in,out] features Features to add to. |
| 4299 | * |
| 4300 | * @return LY_ERR values. |
| 4301 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4302 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 4303 | 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] | 4304 | { |
| 4305 | LY_ERR ret = 0; |
| 4306 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 4307 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4308 | enum yang_keyword kw; |
| 4309 | struct lysp_feature *feat; |
| 4310 | |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame] | 4311 | LYSP_ARRAY_NEW_RET(ctx->ctx, *features, feat, LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4312 | |
| 4313 | /* get value */ |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 4314 | ret = get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len); |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 4315 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4316 | |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 4317 | INSERT_WORD(ctx, buf, feat->name, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4318 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) { |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 4319 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4320 | |
| 4321 | switch (kw) { |
| 4322 | case YANG_DESCRIPTION: |
| 4323 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &feat->dsc, Y_STR_ARG, &feat->exts); |
| 4324 | break; |
| 4325 | case YANG_IF_FEATURE: |
| 4326 | ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &feat->iffeatures, Y_STR_ARG, &feat->exts); |
| 4327 | break; |
| 4328 | case YANG_REFERENCE: |
| 4329 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &feat->ref, Y_STR_ARG, &feat->exts); |
| 4330 | break; |
| 4331 | case YANG_STATUS: |
| 4332 | ret = parse_status(ctx, data, &feat->flags, &feat->exts); |
| 4333 | break; |
| 4334 | case YANG_CUSTOM: |
| 4335 | ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &feat->exts); |
| 4336 | break; |
| 4337 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 4338 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "feature"); |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 4339 | return LY_EVALID; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4340 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 4341 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4342 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 4343 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4344 | |
| 4345 | return ret; |
| 4346 | } |
| 4347 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 4348 | /** |
| 4349 | * @brief Parse the identity statement. |
| 4350 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 4351 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 4352 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 4353 | * @param[in,out] identities Identities to add to. |
| 4354 | * |
| 4355 | * @return LY_ERR values. |
| 4356 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4357 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 4358 | 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] | 4359 | { |
| 4360 | LY_ERR ret = 0; |
| 4361 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 4362 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4363 | enum yang_keyword kw; |
| 4364 | struct lysp_ident *ident; |
| 4365 | |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame] | 4366 | LYSP_ARRAY_NEW_RET(ctx->ctx, *identities, ident, LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4367 | |
| 4368 | /* get value */ |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 4369 | ret = get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len); |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 4370 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4371 | |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 4372 | INSERT_WORD(ctx, buf, ident->name, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4373 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) { |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 4374 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4375 | |
| 4376 | switch (kw) { |
| 4377 | case YANG_DESCRIPTION: |
| 4378 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &ident->dsc, Y_STR_ARG, &ident->exts); |
| 4379 | break; |
| 4380 | case YANG_IF_FEATURE: |
| 4381 | ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &ident->iffeatures, Y_STR_ARG, &ident->exts); |
| 4382 | break; |
| 4383 | case YANG_REFERENCE: |
| 4384 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &ident->ref, Y_STR_ARG, &ident->exts); |
| 4385 | break; |
| 4386 | case YANG_STATUS: |
| 4387 | ret = parse_status(ctx, data, &ident->flags, &ident->exts); |
| 4388 | break; |
| 4389 | case YANG_BASE: |
| 4390 | ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_BASE, &ident->bases, Y_PREF_IDENTIF_ARG, &ident->exts); |
| 4391 | break; |
| 4392 | case YANG_CUSTOM: |
| 4393 | ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &ident->exts); |
| 4394 | break; |
| 4395 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 4396 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "identity"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4397 | return LY_EVALID; |
| 4398 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 4399 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4400 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4401 | |
| 4402 | return ret; |
| 4403 | } |
| 4404 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 4405 | /** |
| 4406 | * @brief Parse the module or submodule statement. |
| 4407 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 4408 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 4409 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 4410 | * @param[in,out] mod Module to write to. |
| 4411 | * |
| 4412 | * @return LY_ERR values. |
| 4413 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4414 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 4415 | 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] | 4416 | { |
| 4417 | LY_ERR ret = 0; |
| 4418 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 4419 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4420 | enum yang_keyword kw, prev_kw = 0; |
| 4421 | enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER; |
| 4422 | |
| 4423 | /* (sub)module name */ |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 4424 | ret = get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len); |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 4425 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4426 | |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 4427 | INSERT_WORD(ctx, buf, mod->name, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4428 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) { |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 4429 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4430 | |
Radek Krejci | e384647 | 2018-10-15 15:24:51 +0200 | [diff] [blame] | 4431 | #define CHECK_ORDER(SECTION) \ |
| 4432 | if (mod_stmt > SECTION) {LOGVAL_YANG(ctx, LY_VCODE_INORD, ly_stmt2str(kw), ly_stmt2str(prev_kw)); return LY_EVALID;}mod_stmt = SECTION |
| 4433 | |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4434 | switch (kw) { |
| 4435 | /* module header */ |
| 4436 | case YANG_NAMESPACE: |
| 4437 | case YANG_PREFIX: |
| 4438 | if (mod->submodule) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 4439 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "submodule"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4440 | return LY_EVALID; |
| 4441 | } |
Radek Krejci | e384647 | 2018-10-15 15:24:51 +0200 | [diff] [blame] | 4442 | CHECK_ORDER(Y_MOD_MODULE_HEADER); |
| 4443 | break; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4444 | case YANG_BELONGS_TO: |
Radek Krejci | e384647 | 2018-10-15 15:24:51 +0200 | [diff] [blame] | 4445 | if (!mod->submodule) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 4446 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "module"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4447 | return LY_EVALID; |
| 4448 | } |
Radek Krejci | e384647 | 2018-10-15 15:24:51 +0200 | [diff] [blame] | 4449 | CHECK_ORDER(Y_MOD_MODULE_HEADER); |
| 4450 | break; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4451 | case YANG_YANG_VERSION: |
Radek Krejci | e384647 | 2018-10-15 15:24:51 +0200 | [diff] [blame] | 4452 | CHECK_ORDER(Y_MOD_MODULE_HEADER); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4453 | break; |
| 4454 | /* linkage */ |
| 4455 | case YANG_INCLUDE: |
| 4456 | case YANG_IMPORT: |
Radek Krejci | e384647 | 2018-10-15 15:24:51 +0200 | [diff] [blame] | 4457 | CHECK_ORDER(Y_MOD_LINKAGE); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4458 | break; |
| 4459 | /* meta */ |
| 4460 | case YANG_ORGANIZATION: |
| 4461 | case YANG_CONTACT: |
| 4462 | case YANG_DESCRIPTION: |
| 4463 | case YANG_REFERENCE: |
Radek Krejci | e384647 | 2018-10-15 15:24:51 +0200 | [diff] [blame] | 4464 | CHECK_ORDER(Y_MOD_META); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4465 | break; |
| 4466 | |
| 4467 | /* revision */ |
| 4468 | case YANG_REVISION: |
Radek Krejci | e384647 | 2018-10-15 15:24:51 +0200 | [diff] [blame] | 4469 | CHECK_ORDER(Y_MOD_REVISION); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4470 | break; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4471 | /* body */ |
| 4472 | case YANG_ANYDATA: |
| 4473 | case YANG_ANYXML: |
| 4474 | case YANG_AUGMENT: |
| 4475 | case YANG_CHOICE: |
| 4476 | case YANG_CONTAINER: |
| 4477 | case YANG_DEVIATION: |
| 4478 | case YANG_EXTENSION: |
| 4479 | case YANG_FEATURE: |
| 4480 | case YANG_GROUPING: |
| 4481 | case YANG_IDENTITY: |
| 4482 | case YANG_LEAF: |
| 4483 | case YANG_LEAF_LIST: |
| 4484 | case YANG_LIST: |
| 4485 | case YANG_NOTIFICATION: |
| 4486 | case YANG_RPC: |
| 4487 | case YANG_TYPEDEF: |
| 4488 | case YANG_USES: |
| 4489 | case YANG_CUSTOM: |
| 4490 | mod_stmt = Y_MOD_BODY; |
| 4491 | break; |
| 4492 | default: |
| 4493 | /* error handled in the next switch */ |
| 4494 | break; |
| 4495 | } |
Radek Krejci | e384647 | 2018-10-15 15:24:51 +0200 | [diff] [blame] | 4496 | #undef CHECK_ORDER |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4497 | |
Radek Krejci | e384647 | 2018-10-15 15:24:51 +0200 | [diff] [blame] | 4498 | prev_kw = kw; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4499 | switch (kw) { |
| 4500 | /* module header */ |
| 4501 | case YANG_YANG_VERSION: |
| 4502 | ret = parse_yangversion(ctx, data, mod); |
| 4503 | break; |
| 4504 | case YANG_NAMESPACE: |
| 4505 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_NAMESPACE, 0, &mod->ns, Y_STR_ARG, &mod->exts); |
| 4506 | break; |
| 4507 | case YANG_PREFIX: |
| 4508 | 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] | 4509 | LY_CHECK_RET(lysp_check_prefix(ctx, mod, &mod->prefix), LY_EVALID); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4510 | break; |
| 4511 | case YANG_BELONGS_TO: |
| 4512 | ret = parse_belongsto(ctx, data, &mod->belongsto, &mod->prefix, &mod->exts); |
| 4513 | break; |
| 4514 | |
| 4515 | /* linkage */ |
| 4516 | case YANG_INCLUDE: |
| 4517 | ret = parse_include(ctx, data, &mod->includes); |
| 4518 | break; |
| 4519 | case YANG_IMPORT: |
Radek Krejci | 70853c5 | 2018-10-15 14:46:16 +0200 | [diff] [blame] | 4520 | ret = parse_import(ctx, data, mod); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4521 | break; |
| 4522 | |
| 4523 | /* meta */ |
| 4524 | case YANG_ORGANIZATION: |
| 4525 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_ORGANIZATION, 0, &mod->org, Y_STR_ARG, &mod->exts); |
| 4526 | break; |
| 4527 | case YANG_CONTACT: |
| 4528 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_CONTACT, 0, &mod->contact, Y_STR_ARG, &mod->exts); |
| 4529 | break; |
| 4530 | case YANG_DESCRIPTION: |
| 4531 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &mod->dsc, Y_STR_ARG, &mod->exts); |
| 4532 | break; |
| 4533 | case YANG_REFERENCE: |
| 4534 | ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &mod->ref, Y_STR_ARG, &mod->exts); |
| 4535 | break; |
| 4536 | |
| 4537 | /* revision */ |
| 4538 | case YANG_REVISION: |
| 4539 | ret = parse_revision(ctx, data, &mod->revs); |
| 4540 | break; |
| 4541 | |
| 4542 | /* body */ |
| 4543 | case YANG_ANYDATA: |
| 4544 | case YANG_ANYXML: |
| 4545 | ret = parse_any(ctx, data, kw, &mod->data); |
| 4546 | break; |
| 4547 | case YANG_CHOICE: |
| 4548 | ret = parse_choice(ctx, data, &mod->data); |
| 4549 | break; |
| 4550 | case YANG_CONTAINER: |
| 4551 | ret = parse_container(ctx, data, &mod->data); |
| 4552 | break; |
| 4553 | case YANG_LEAF: |
| 4554 | ret = parse_leaf(ctx, data, &mod->data); |
| 4555 | break; |
| 4556 | case YANG_LEAF_LIST: |
| 4557 | ret = parse_leaflist(ctx, data, &mod->data); |
| 4558 | break; |
| 4559 | case YANG_LIST: |
| 4560 | ret = parse_list(ctx, data, &mod->data); |
| 4561 | break; |
| 4562 | case YANG_USES: |
| 4563 | ret = parse_uses(ctx, data, &mod->data); |
| 4564 | break; |
| 4565 | |
| 4566 | case YANG_AUGMENT: |
| 4567 | ret = parse_augment(ctx, data, &mod->augments); |
| 4568 | break; |
| 4569 | case YANG_DEVIATION: |
| 4570 | ret = parse_deviation(ctx, data, &mod->deviations); |
| 4571 | break; |
| 4572 | case YANG_EXTENSION: |
| 4573 | ret = parse_extension(ctx, data, &mod->extensions); |
| 4574 | break; |
| 4575 | case YANG_FEATURE: |
| 4576 | ret = parse_feature(ctx, data, &mod->features); |
| 4577 | break; |
| 4578 | case YANG_GROUPING: |
| 4579 | ret = parse_grouping(ctx, data, &mod->groupings); |
| 4580 | break; |
| 4581 | case YANG_IDENTITY: |
| 4582 | ret = parse_identity(ctx, data, &mod->identities); |
| 4583 | break; |
| 4584 | case YANG_NOTIFICATION: |
| 4585 | ret = parse_notif(ctx, data, &mod->notifs); |
| 4586 | break; |
| 4587 | case YANG_RPC: |
| 4588 | ret = parse_action(ctx, data, &mod->rpcs); |
| 4589 | break; |
| 4590 | case YANG_TYPEDEF: |
| 4591 | ret = parse_typedef(ctx, data, &mod->typedefs); |
| 4592 | break; |
| 4593 | case YANG_CUSTOM: |
| 4594 | ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &mod->exts); |
| 4595 | break; |
| 4596 | |
| 4597 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 4598 | 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] | 4599 | return LY_EVALID; |
| 4600 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 4601 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4602 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 4603 | LY_CHECK_RET(ret); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4604 | |
| 4605 | /* mandatory substatements */ |
| 4606 | if (mod->submodule) { |
| 4607 | if (!mod->belongsto) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 4608 | LOGVAL_YANG(ctx, LY_VCODE_MISSTMT, "belongs-to", "submodule"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4609 | return LY_EVALID; |
| 4610 | } |
| 4611 | } else { |
| 4612 | if (!mod->ns) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 4613 | LOGVAL_YANG(ctx, LY_VCODE_MISSTMT, "namespace", "module"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4614 | return LY_EVALID; |
| 4615 | } else if (!mod->prefix) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 4616 | LOGVAL_YANG(ctx, LY_VCODE_MISSTMT, "prefix", "module"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4617 | return LY_EVALID; |
| 4618 | } |
| 4619 | } |
| 4620 | |
| 4621 | return ret; |
| 4622 | } |
| 4623 | |
Radek Krejci | d4557c6 | 2018-09-17 11:42:09 +0200 | [diff] [blame] | 4624 | LY_ERR |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4625 | yang_parse(struct ly_ctx *ctx, const char *data, struct lysp_module **mod_p) |
| 4626 | { |
| 4627 | LY_ERR ret = 0; |
| 4628 | char *word, *buf; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 4629 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4630 | enum yang_keyword kw; |
Radek Krejci | 0c2cf32 | 2018-10-13 08:02:30 +0200 | [diff] [blame] | 4631 | struct lysp_module *mod = NULL; |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame^] | 4632 | struct ly_parser_ctx context = {0}; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4633 | |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 4634 | context.ctx = ctx; |
| 4635 | context.line = 1; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4636 | |
| 4637 | /* "module"/"submodule" */ |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 4638 | ret = get_keyword(&context, &data, &kw, &word, &word_len); |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 4639 | LY_CHECK_GOTO(ret, error); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4640 | |
| 4641 | if ((kw != YANG_MODULE) && (kw != YANG_SUBMODULE)) { |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 4642 | LOGVAL_YANG(&context, LYVE_SYNTAX, "Invalid keyword \"%s\", expected \"module\" or \"submodule\".", |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 4643 | ly_stmt2str(kw)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4644 | goto error; |
| 4645 | } |
| 4646 | |
| 4647 | mod = calloc(1, sizeof *mod); |
| 4648 | LY_CHECK_ERR_GOTO(!mod, LOGMEM(ctx), error); |
| 4649 | if (kw == YANG_SUBMODULE) { |
| 4650 | mod->submodule = 1; |
| 4651 | } |
Radek Krejci | 9fcacc1 | 2018-10-11 15:59:11 +0200 | [diff] [blame] | 4652 | mod->ctx = ctx; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4653 | |
| 4654 | /* substatements */ |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 4655 | ret = parse_sub_module(&context, &data, mod); |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 4656 | LY_CHECK_GOTO(ret, error); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4657 | |
| 4658 | /* read some trailing spaces or new lines */ |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 4659 | 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] | 4660 | LY_CHECK_GOTO(ret, error); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4661 | |
| 4662 | if (word) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 4663 | 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] | 4664 | word_len, word); |
| 4665 | free(buf); |
| 4666 | goto error; |
| 4667 | } |
| 4668 | assert(!buf); |
| 4669 | |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame^] | 4670 | /* make sure that the newest revision is at position 0 */ |
| 4671 | lysp_sort_revisions(mod->revs); |
| 4672 | |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4673 | *mod_p = mod; |
| 4674 | return ret; |
| 4675 | |
| 4676 | error: |
Radek Krejci | 9fcacc1 | 2018-10-11 15:59:11 +0200 | [diff] [blame] | 4677 | lysp_module_free(mod); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4678 | return ret; |
| 4679 | } |