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