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 | |
Radek Krejci | ceaf212 | 2019-01-02 15:03:26 +0100 | [diff] [blame] | 44 | /** |
| 45 | * @brief Try to find object with MEMBER string matching the IDENT in the given ARRAY. |
| 46 | * Macro logs an error message and returns LY_EVALID in case of existence of a matching object. |
| 47 | * |
| 48 | * @param[in] CTX yang parser context for logging. |
| 49 | * @param[in] ARRAY [sized array](@ref sizedarrays) of a generic objects with member named MEMBER to search. |
| 50 | * @param[in] MEMBER Name of the member of the objects in the ARRAY to compare. |
| 51 | * @param[in] STMT Name of the compared YANG statements for logging. |
| 52 | * @param[in] IDENT String trying to find in the ARRAY's objects inside the MEMBER member. |
| 53 | */ |
Radek Krejci | faa1eac | 2018-10-30 14:34:55 +0100 | [diff] [blame] | 54 | #define CHECK_UNIQUENESS(CTX, ARRAY, MEMBER, STMT, IDENT) \ |
| 55 | if (ARRAY) { \ |
| 56 | for (unsigned int u = 0; u < LY_ARRAY_SIZE(ARRAY) - 1; ++u) { \ |
| 57 | if (!strcmp((ARRAY)[u].MEMBER, IDENT)) { \ |
| 58 | LOGVAL_YANG(CTX, LY_VCODE_DUPIDENT, IDENT, STMT); \ |
| 59 | return LY_EVALID; \ |
| 60 | } \ |
| 61 | } \ |
| 62 | } |
| 63 | |
Radek Krejci | ceaf212 | 2019-01-02 15:03:26 +0100 | [diff] [blame] | 64 | /** |
| 65 | * @brief Insert WORD into the libyang context's dictionary and store as TARGET. |
| 66 | * @param[in] CTX yang parser context to access libyang context. |
| 67 | * @param[in] BUF buffer in case the word is not a constant and can be inserted directly (zero-copy) |
| 68 | * @param[out] TARGET variable where to store the pointer to the inserted value. |
| 69 | * @param[in] WORD string to store. |
| 70 | * @param[in] LEN length of the string in WORD to store. |
| 71 | */ |
Radek Krejci | 9fcacc1 | 2018-10-11 15:59:11 +0200 | [diff] [blame] | 72 | #define INSERT_WORD(CTX, BUF, TARGET, WORD, LEN) \ |
| 73 | if (BUF) {(TARGET) = lydict_insert_zc((CTX)->ctx, WORD);}\ |
| 74 | else {(TARGET) = lydict_insert((CTX)->ctx, WORD, LEN);} |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 75 | |
Radek Krejci | ceaf212 | 2019-01-02 15:03:26 +0100 | [diff] [blame] | 76 | /** |
| 77 | * @brief Move the DATA pointer by COUNT items. Also updates the indent value in yang parser context |
| 78 | * @param[in] CTX yang parser context to update its indent value. |
| 79 | * @param[in,out] DATA pointer to move |
| 80 | * @param[in] COUNT number of items for which the DATA pointer is supposed to move on. |
| 81 | */ |
Radek Krejci | 2b61048 | 2019-01-02 13:36:09 +0100 | [diff] [blame] | 82 | #define MOVE_INPUT(CTX, DATA, COUNT) (*(DATA))+=COUNT;(CTX)->indent+=COUNT |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 83 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 84 | /** |
| 85 | * @brief Loop through all substatements providing, return if there are none. |
| 86 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 87 | * @param[in] CTX yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 88 | * @param[in] DATA Raw data to read from. |
| 89 | * @param[out] KW YANG keyword read. |
| 90 | * @param[out] WORD Pointer to the keyword itself. |
| 91 | * @param[out] WORD_LEN Length of the keyword. |
| 92 | * @param[out] ERR Variable for error storing. |
| 93 | * |
| 94 | * @return In case there are no substatements or a fatal error encountered. |
| 95 | */ |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 96 | #define YANG_READ_SUBSTMT_FOR(CTX, DATA, KW, WORD, WORD_LEN, ERR, CHECKGOTO) \ |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 97 | LY_CHECK_RET(get_keyword(CTX, DATA, &KW, &WORD, &WORD_LEN)); \ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 98 | if (KW == YANG_SEMICOLON) { \ |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 99 | CHECKGOTO; \ |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 100 | return LY_SUCCESS; \ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 101 | } \ |
| 102 | if (KW != YANG_LEFT_BRACE) { \ |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 103 | 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] | 104 | return LY_EVALID; \ |
| 105 | } \ |
| 106 | for (ERR = get_keyword(CTX, DATA, &KW, &WORD, &WORD_LEN); \ |
| 107 | !ERR && (KW != YANG_RIGHT_BRACE); \ |
| 108 | ERR = get_keyword(CTX, DATA, &KW, &WORD, &WORD_LEN)) |
| 109 | |
Radek Krejci | ceaf212 | 2019-01-02 15:03:26 +0100 | [diff] [blame] | 110 | /** |
| 111 | * @brief Check module version is at least 2 (YANG 1.1) because of the keyword presence. |
| 112 | * Logs error message and returns LY_EVALID in case of module in YANG version 1.0. |
| 113 | * @param[in] CTX yang parser context to get current module and for logging. |
| 114 | * @param[in] KW keyword allowed only in YANG version 1.1 (or later) - for logging. |
| 115 | * @param[in] PARENT parent statement where the KW is present - for logging. |
| 116 | */ |
| 117 | #define YANG_CHECK_STMTVER2_RET(CTX, KW, PARENT) \ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 118 | if ((CTX)->mod_version < 2) {LOGVAL_YANG((CTX), LY_VCODE_INCHILDSTMT2, KW, PARENT); return LY_EVALID;} |
Radek Krejci | 1011365 | 2018-11-14 16:56:50 +0100 | [diff] [blame] | 119 | |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 120 | static LY_ERR parse_container(struct ly_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings); |
| 121 | static LY_ERR parse_uses(struct ly_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings); |
| 122 | static LY_ERR parse_choice(struct ly_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings); |
| 123 | static LY_ERR parse_case(struct ly_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings); |
| 124 | static LY_ERR parse_list(struct ly_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings); |
| 125 | static LY_ERR parse_grouping(struct ly_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_grp **groupings); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 126 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 127 | /** |
| 128 | * @brief Add another character to dynamic buffer, a low-level function. |
| 129 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 130 | * Enlarge if needed. Updates \p input as well as \p buf_used. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 131 | * |
Radek Krejci | 404251e | 2018-10-09 12:06:44 +0200 | [diff] [blame] | 132 | * @param[in] ctx libyang context for logging. |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 133 | * @param[in, out] input Input string to process. |
| 134 | * @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] | 135 | * @param[in,out] buf Buffer to use, can be moved by realloc(). |
| 136 | * @param[in,out] buf_len Current size of the buffer. |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 137 | * @param[in,out] buf_used Currently used characters of the buffer. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 138 | * |
| 139 | * @return LY_ERR values. |
| 140 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 141 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 142 | 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] | 143 | { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 144 | if (*buf_len <= (*buf_used) + len) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 145 | *buf_len += 16; |
| 146 | *buf = ly_realloc(*buf, *buf_len); |
| 147 | LY_CHECK_ERR_RET(!*buf, LOGMEM(ctx), LY_EMEM); |
| 148 | } |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 149 | memcpy(&(*buf)[*buf_used], *input, len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 150 | |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 151 | (*buf_used) += len; |
| 152 | (*input) += len; |
| 153 | |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 154 | return LY_SUCCESS; |
| 155 | } |
| 156 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 157 | /** |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 158 | * @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] | 159 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 160 | * @param[in] ctx yang parser context for logging. |
| 161 | * @param[in] c UTF8 code point of a character to check. |
| 162 | * @return LY_ERR values. |
| 163 | */ |
| 164 | static LY_ERR |
| 165 | check_stringchar(struct ly_parser_ctx *ctx, unsigned int c) |
| 166 | { |
| 167 | if (!is_yangutf8char(c)) { |
| 168 | LOGVAL_YANG(ctx, LY_VCODE_INCHAR, c); |
| 169 | return LY_EVALID; |
| 170 | } |
| 171 | return LY_SUCCESS; |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * @brief Check that \p c is valid UTF8 code point for YANG identifier. |
| 176 | * |
| 177 | * @param[in] ctx yang parser context for logging. |
| 178 | * @param[in] c UTF8 code point of a character to check. |
| 179 | * @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] | 180 | * @param[in,out] prefix Storage for internally used flag in case of possible prefixed identifiers: |
| 181 | * 0 - colon not yet found (no prefix) |
| 182 | * 1 - \p c is the colon character |
| 183 | * 2 - prefix already processed, now processing the identifier |
| 184 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 185 | * If the identifier cannot be prefixed, NULL is expected. |
| 186 | * @return LY_ERR values. |
| 187 | */ |
| 188 | static LY_ERR |
| 189 | check_identifierchar(struct ly_parser_ctx *ctx, unsigned int c, int first, int *prefix) |
| 190 | { |
| 191 | if (first || (prefix && (*prefix) == 1)) { |
| 192 | if (!is_yangidentstartchar(c)) { |
| 193 | LOGVAL_YANG(ctx, LYVE_SYNTAX_YANG, "Invalid identifier first character '%c'.", c); |
| 194 | return LY_EVALID; |
| 195 | } |
Radek Krejci | 9fcacc1 | 2018-10-11 15:59:11 +0200 | [diff] [blame] | 196 | if (prefix) { |
| 197 | if (first) { |
| 198 | (*prefix) = 0; |
| 199 | } else { |
| 200 | (*prefix) = 2; |
| 201 | } |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 202 | } |
| 203 | } else if (c == ':' && prefix && (*prefix) == 0) { |
| 204 | (*prefix) = 1; |
| 205 | } else if (!is_yangidentchar(c)) { |
| 206 | LOGVAL_YANG(ctx, LYVE_SYNTAX_YANG, "Invalid identifier character '%c'.", c); |
| 207 | return LY_EVALID; |
| 208 | } |
| 209 | |
| 210 | return LY_SUCCESS; |
| 211 | } |
| 212 | |
| 213 | /** |
| 214 | * @brief Store a single UTF8 character. It depends whether in a dynamically-allocated buffer or just as a pointer to the data. |
| 215 | * |
| 216 | * @param[in] ctx yang parser context for logging. |
| 217 | * @param[in,out] input Pointer to the string where to get the character to store. Automatically moved to the next character |
| 218 | * when function returns. |
| 219 | * @param[in] arg Type of the input string to select method of checking character validity. |
| 220 | * @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] | 221 | * 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] | 222 | * @param[in,out] word_len Current length of the word pointed to by \p word_p. |
| 223 | * @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] | 224 | * @param[in,out] buf_len Current length of \p word_b. |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 225 | * @param[in] need_buf Flag if the dynamically allocated buffer is required. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 226 | * |
| 227 | * @return LY_ERR values. |
| 228 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 229 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 230 | buf_store_char(struct ly_parser_ctx *ctx, const char **input, enum yang_arg arg, |
| 231 | 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] | 232 | { |
Radek Krejci | 404251e | 2018-10-09 12:06:44 +0200 | [diff] [blame] | 233 | int prefix = 0; |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 234 | unsigned int c; |
| 235 | size_t len; |
| 236 | |
| 237 | /* get UTF8 code point (and number of bytes coding the character) */ |
| 238 | LY_CHECK_ERR_RET(ly_getutf8(input, &c, &len), |
| 239 | LOGVAL_YANG(ctx, LY_VCODE_INCHAR, (*input)[-len]), LY_EVALID); |
| 240 | (*input) -= len; |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 241 | if (c == '\n') { |
| 242 | ctx->indent = 0; |
| 243 | } else { |
| 244 | /* note - even the multibyte character is count as 1 */ |
| 245 | ++ctx->indent; |
| 246 | } |
| 247 | |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 248 | /* check character validity */ |
| 249 | switch (arg) { |
| 250 | case Y_IDENTIF_ARG: |
| 251 | LY_CHECK_RET(check_identifierchar(ctx, c, !(*word_len), NULL)); |
| 252 | break; |
| 253 | case Y_PREF_IDENTIF_ARG: |
| 254 | LY_CHECK_RET(check_identifierchar(ctx, c, !(*word_len), &prefix)); |
| 255 | break; |
| 256 | case Y_STR_ARG: |
| 257 | case Y_MAYBE_STR_ARG: |
| 258 | LY_CHECK_RET(check_stringchar(ctx, c)); |
| 259 | break; |
| 260 | } |
| 261 | |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 262 | if (word_b && *word_b) { |
| 263 | /* add another character into buffer */ |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 264 | 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] | 265 | return LY_EMEM; |
| 266 | } |
| 267 | |
| 268 | /* in case of realloc */ |
| 269 | *word_p = *word_b; |
| 270 | } else if (need_buf) { |
| 271 | /* first time we need a buffer, copy everything read up to now */ |
| 272 | if (*word_len) { |
| 273 | *word_b = malloc(*word_len); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 274 | LY_CHECK_ERR_RET(!*word_b, LOGMEM(ctx->ctx), LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 275 | *buf_len = *word_len; |
| 276 | memcpy(*word_b, *word_p, *word_len); |
| 277 | } |
| 278 | |
| 279 | /* add this new character into buffer */ |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 280 | 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] | 281 | return LY_EMEM; |
| 282 | } |
| 283 | |
| 284 | /* in case of realloc */ |
| 285 | *word_p = *word_b; |
| 286 | } else { |
| 287 | /* just remember the first character pointer */ |
| 288 | if (!*word_p) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 289 | *word_p = (char *)(*input); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 290 | } |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 291 | /* ... and update the word's length */ |
| 292 | (*word_len) += len; |
| 293 | (*input) += len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | return LY_SUCCESS; |
| 297 | } |
| 298 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 299 | /** |
| 300 | * @brief Skip YANG comment in data. |
| 301 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 302 | * @param[in] ctx yang parser context for logging. |
| 303 | * @param[in,out] data Data to read from, automatically moved after the comment. |
| 304 | * @param[in] comment Type of the comment to process: |
| 305 | * 1 for a one-line comment, |
| 306 | * 2 for a block comment. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 307 | * |
| 308 | * @return LY_ERR values. |
| 309 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 310 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 311 | skip_comment(struct ly_parser_ctx *ctx, const char **data, int comment) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 312 | { |
Radek Krejci | 80dd33e | 2018-09-26 15:57:18 +0200 | [diff] [blame] | 313 | /* internal statuses: 0 - comment ended, |
| 314 | * 1 - in line comment, |
| 315 | * 2 - in block comment, |
| 316 | * 3 - in block comment with last read character '*' |
| 317 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 318 | while (**data && comment) { |
| 319 | switch (comment) { |
| 320 | case 1: |
| 321 | if (**data == '\n') { |
| 322 | comment = 0; |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 323 | ++ctx->line; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 324 | } |
| 325 | break; |
| 326 | case 2: |
| 327 | if (**data == '*') { |
Radek Krejci | 15c80ca | 2018-10-09 11:01:31 +0200 | [diff] [blame] | 328 | comment = 3; |
| 329 | } else if (**data == '\n') { |
| 330 | ++ctx->line; |
| 331 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 332 | break; |
| 333 | case 3: |
| 334 | if (**data == '/') { |
| 335 | comment = 0; |
| 336 | } else { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 337 | if (**data == '\n') { |
| 338 | ++ctx->line; |
| 339 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 340 | comment = 2; |
| 341 | } |
| 342 | break; |
| 343 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 344 | LOGINT_RET(ctx->ctx); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 345 | } |
| 346 | |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 347 | if (**data == '\n') { |
| 348 | ctx->indent = 0; |
| 349 | } else { |
| 350 | ++ctx->indent; |
| 351 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 352 | ++(*data); |
| 353 | } |
| 354 | |
| 355 | if (!**data && (comment > 1)) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 356 | LOGVAL_YANG(ctx, LYVE_SYNTAX, "Unexpected end-of-file, non-terminated comment."); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 357 | return LY_EVALID; |
| 358 | } |
| 359 | |
| 360 | return LY_SUCCESS; |
| 361 | } |
| 362 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 363 | /** |
| 364 | * @brief Read a quoted string from data. |
| 365 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 366 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 367 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 368 | * @param[in] arg Type of YANG keyword argument expected. |
| 369 | * @param[out] word_p Pointer to the read quoted string. |
| 370 | * @param[out] word_b Pointer to a dynamically-allocated buffer holding the read quoted string. If not needed, |
| 371 | * set to NULL. Otherwise equal to \p word_p. |
| 372 | * @param[out] word_len Length of the read quoted string. |
| 373 | * @param[out] buf_len Length of the dynamically-allocated buffer \p word_b. |
| 374 | * @param[in] indent Current indent (number of YANG spaces). Needed for correct multi-line string |
| 375 | * indenation in the final quoted string. |
| 376 | * |
| 377 | * @return LY_ERR values. |
| 378 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 379 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 380 | 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] | 381 | size_t *buf_len) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 382 | { |
| 383 | /* string: 0 - string ended, 1 - string with ', 2 - string with ", 3 - string with " with last character \, |
| 384 | * 4 - string finished, now skipping whitespaces looking for +, |
| 385 | * 5 - string continues after +, skipping whitespaces */ |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 386 | unsigned int string, block_indent = 0, current_indent = 0, need_buf = 0; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 387 | const char *c; |
| 388 | |
| 389 | if (**data == '\"') { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 390 | string = 2; |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 391 | current_indent = block_indent = ctx->indent + 1; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 392 | } else { |
| 393 | assert(**data == '\''); |
| 394 | string = 1; |
| 395 | } |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 396 | MOVE_INPUT(ctx, data, 1); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 397 | |
| 398 | while (**data && string) { |
| 399 | switch (string) { |
| 400 | case 1: |
| 401 | switch (**data) { |
| 402 | case '\'': |
| 403 | /* string may be finished, but check for + */ |
| 404 | string = 4; |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 405 | MOVE_INPUT(ctx, data, 1); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 406 | break; |
| 407 | default: |
| 408 | /* check and store character */ |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 409 | 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] | 410 | break; |
| 411 | } |
| 412 | break; |
| 413 | case 2: |
| 414 | switch (**data) { |
| 415 | case '\"': |
| 416 | /* string may be finished, but check for + */ |
| 417 | string = 4; |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 418 | MOVE_INPUT(ctx, data, 1); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 419 | break; |
| 420 | case '\\': |
| 421 | /* special character following */ |
| 422 | string = 3; |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 423 | ++(*data); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 424 | break; |
| 425 | case ' ': |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 426 | if (current_indent < block_indent) { |
| 427 | ++current_indent; |
| 428 | MOVE_INPUT(ctx, data, 1); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 429 | } else { |
| 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)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 432 | } |
| 433 | break; |
| 434 | case '\t': |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 435 | if (current_indent < block_indent) { |
| 436 | assert(need_buf); |
| 437 | current_indent += 8; |
| 438 | ctx->indent += 8; |
| 439 | for (; current_indent > block_indent; --current_indent, --ctx->indent) { |
| 440 | /* store leftover spaces from the tab */ |
| 441 | c = " "; |
| 442 | 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] | 443 | } |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 444 | ++(*data); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 445 | } else { |
| 446 | /* check and store character */ |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 447 | 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] | 448 | /* additional characters for indentation - only 1 was count in buf_store_char */ |
| 449 | ctx->indent += 7; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 450 | } |
| 451 | break; |
| 452 | case '\n': |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 453 | if (block_indent) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 454 | /* we will be removing the indents so we need our own buffer */ |
| 455 | need_buf = 1; |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 456 | |
| 457 | /* remove trailing tabs and spaces */ |
| 458 | while ((*word_len) && ((*word_p)[(*word_len) - 1] == '\t' || (*word_p)[(*word_len) - 1] == ' ')) { |
| 459 | --(*word_len); |
| 460 | } |
| 461 | |
| 462 | /* start indentation */ |
| 463 | current_indent = 0; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 464 | } |
| 465 | |
| 466 | /* check and store character */ |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 467 | LY_CHECK_RET(buf_store_char(ctx, data, arg, word_p, word_len, word_b, buf_len, need_buf)); |
| 468 | |
| 469 | /* maintain line number */ |
| 470 | ++ctx->line; |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 471 | |
| 472 | /* reset context indentation counter for possible string after this one */ |
| 473 | ctx->indent = 0; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 474 | break; |
| 475 | default: |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 476 | /* first non-whitespace character, stop eating indentation */ |
| 477 | current_indent = block_indent; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 478 | |
| 479 | /* check and store character */ |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 480 | 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] | 481 | break; |
| 482 | } |
| 483 | break; |
| 484 | case 3: |
| 485 | /* string encoded characters */ |
| 486 | switch (**data) { |
| 487 | case 'n': |
| 488 | c = "\n"; |
| 489 | break; |
| 490 | case 't': |
| 491 | c = "\t"; |
| 492 | break; |
| 493 | case '\"': |
| 494 | c = *data; |
| 495 | break; |
| 496 | case '\\': |
| 497 | c = *data; |
| 498 | break; |
| 499 | default: |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 500 | 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] | 501 | return LY_EVALID; |
| 502 | } |
| 503 | |
| 504 | /* check and store character */ |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 505 | 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] | 506 | |
| 507 | string = 2; |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 508 | ++(*data); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 509 | break; |
| 510 | case 4: |
| 511 | switch (**data) { |
| 512 | case '+': |
| 513 | /* string continues */ |
| 514 | string = 5; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 515 | need_buf = 1; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 516 | break; |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 517 | case '\n': |
| 518 | ++ctx->line; |
| 519 | /* fallthrough */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 520 | case ' ': |
| 521 | case '\t': |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 522 | /* just skip */ |
| 523 | break; |
| 524 | default: |
| 525 | /* string is finished */ |
| 526 | goto string_end; |
| 527 | } |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 528 | MOVE_INPUT(ctx, data, 1); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 529 | break; |
| 530 | case 5: |
| 531 | switch (**data) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 532 | case '\n': |
| 533 | ++ctx->line; |
| 534 | /* fallthrough */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 535 | case ' ': |
| 536 | case '\t': |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 537 | /* skip */ |
| 538 | break; |
| 539 | case '\'': |
| 540 | string = 1; |
| 541 | break; |
| 542 | case '\"': |
| 543 | string = 2; |
| 544 | break; |
| 545 | default: |
| 546 | /* it must be quoted again */ |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 547 | 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] | 548 | return LY_EVALID; |
| 549 | } |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 550 | MOVE_INPUT(ctx, data, 1); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 551 | break; |
| 552 | default: |
| 553 | return LY_EINT; |
| 554 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 555 | } |
| 556 | |
| 557 | string_end: |
| 558 | return LY_SUCCESS; |
| 559 | } |
| 560 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 561 | /** |
| 562 | * @brief Get another YANG string from the raw data. |
| 563 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 564 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 565 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 566 | * @param[in] arg Type of YANG keyword argument expected. |
Michal Vasko | 2ca70f5 | 2018-09-27 11:04:51 +0200 | [diff] [blame] | 567 | * @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] | 568 | * @param[out] word_b Pointer to a dynamically-allocated buffer holding the read string. If not needed, |
| 569 | * set to NULL. Otherwise equal to \p word_p. |
| 570 | * @param[out] word_len Length of the read string. |
| 571 | * |
| 572 | * @return LY_ERR values. |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 573 | */ |
| 574 | static LY_ERR |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 575 | 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] | 576 | { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 577 | size_t buf_len = 0; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 578 | |
| 579 | /* word buffer - dynamically allocated */ |
| 580 | *word_b = NULL; |
| 581 | |
| 582 | /* word pointer - just a pointer to data */ |
| 583 | *word_p = NULL; |
| 584 | |
| 585 | *word_len = 0; |
| 586 | while (**data) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 587 | switch (**data) { |
| 588 | case '\'': |
| 589 | case '\"': |
| 590 | if (*word_len) { |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 591 | /* invalid - quotes cannot be in unquoted string and only optsep, ; or { can follow it */ |
| 592 | LOGVAL_YANG(ctx, LY_VCODE_INSTREXP, 1, *data, |
| 593 | "unquoted string character, optsep, semicolon or opening brace"); |
| 594 | return LY_EVALID; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 595 | } |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 596 | LY_CHECK_RET(read_qstring(ctx, data, arg, word_p, word_b, word_len, &buf_len)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 597 | goto str_end; |
| 598 | case '/': |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 599 | if ((*data)[1] == '/') { |
| 600 | /* one-line comment */ |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 601 | MOVE_INPUT(ctx, data, 2); |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 602 | LY_CHECK_RET(skip_comment(ctx, data, 1)); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 603 | } else if ((*data)[1] == '*') { |
| 604 | /* block comment */ |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 605 | MOVE_INPUT(ctx, data, 2); |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 606 | LY_CHECK_RET(skip_comment(ctx, data, 2)); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 607 | } else { |
| 608 | /* not a comment after all */ |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 609 | LY_CHECK_RET(buf_store_char(ctx, data, arg, word_p, word_len, word_b, &buf_len, 0)); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 610 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 611 | break; |
| 612 | case ' ': |
| 613 | if (*word_len) { |
| 614 | /* word is finished */ |
| 615 | goto str_end; |
| 616 | } |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 617 | MOVE_INPUT(ctx, data, 1); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 618 | break; |
| 619 | case '\t': |
| 620 | if (*word_len) { |
| 621 | /* word is finished */ |
| 622 | goto str_end; |
| 623 | } |
| 624 | /* tabs count for 8 spaces */ |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 625 | ctx->indent += 8; |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 626 | |
| 627 | ++(*data); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 628 | break; |
| 629 | case '\n': |
| 630 | if (*word_len) { |
| 631 | /* word is finished */ |
| 632 | goto str_end; |
| 633 | } |
| 634 | /* reset indent */ |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 635 | ctx->indent = 0; |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 636 | |
| 637 | /* track line numbers */ |
| 638 | ++ctx->line; |
| 639 | |
| 640 | ++(*data); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 641 | break; |
| 642 | case ';': |
| 643 | case '{': |
| 644 | if (*word_len || (arg == Y_MAYBE_STR_ARG)) { |
| 645 | /* word is finished */ |
| 646 | goto str_end; |
| 647 | } |
| 648 | |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 649 | LOGVAL_YANG(ctx, LY_VCODE_INSTREXP, 1, *data, "an argument"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 650 | return LY_EVALID; |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 651 | case '}': |
| 652 | /* invalid - braces cannot be in unquoted string (opening braces terminates the string and can follow it) */ |
| 653 | LOGVAL_YANG(ctx, LY_VCODE_INSTREXP, 1, *data, |
| 654 | "unquoted string character, optsep, semicolon or opening brace"); |
| 655 | return LY_EVALID; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 656 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 657 | 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] | 658 | break; |
| 659 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 660 | } |
| 661 | |
| 662 | str_end: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 663 | /* terminating NULL byte for buf */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 664 | if (*word_b) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 665 | (*word_b) = ly_realloc(*word_b, (*word_len) + 1); |
| 666 | LY_CHECK_ERR_RET(!(*word_b), LOGMEM(ctx->ctx), LY_EMEM); |
| 667 | (*word_b)[*word_len] = '\0'; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 668 | *word_p = *word_b; |
| 669 | } |
| 670 | |
| 671 | return LY_SUCCESS; |
| 672 | } |
| 673 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 674 | /** |
| 675 | * @brief Get another YANG keyword from the raw data. |
| 676 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 677 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 678 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 679 | * @param[out] kw YANG keyword read. |
| 680 | * @param[out] word_p Pointer to the keyword in the data. Useful for extension instances. |
| 681 | * @param[out] word_len Length of the keyword in the data. Useful for extension instances. |
| 682 | * |
| 683 | * @return LY_ERR values. |
| 684 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 685 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 686 | 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] | 687 | { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 688 | int prefix; |
| 689 | const char *word_start; |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 690 | unsigned int c; |
| 691 | size_t len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 692 | |
| 693 | if (word_p) { |
| 694 | *word_p = NULL; |
| 695 | *word_len = 0; |
| 696 | } |
| 697 | |
| 698 | /* first skip "optsep", comments */ |
| 699 | while (**data) { |
Radek Krejci | dcc7b32 | 2018-10-11 14:24:02 +0200 | [diff] [blame] | 700 | switch (**data) { |
| 701 | case '/': |
| 702 | if ((*data)[1] == '/') { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 703 | /* one-line comment */ |
Radek Krejci | dcc7b32 | 2018-10-11 14:24:02 +0200 | [diff] [blame] | 704 | MOVE_INPUT(ctx, data, 2); |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 705 | LY_CHECK_RET(skip_comment(ctx, data, 1)); |
Radek Krejci | dcc7b32 | 2018-10-11 14:24:02 +0200 | [diff] [blame] | 706 | } else if ((*data)[1] == '*') { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 707 | /* block comment */ |
Radek Krejci | dcc7b32 | 2018-10-11 14:24:02 +0200 | [diff] [blame] | 708 | MOVE_INPUT(ctx, data, 2); |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 709 | LY_CHECK_RET(skip_comment(ctx, data, 2)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 710 | } else { |
Radek Krejci | dcc7b32 | 2018-10-11 14:24:02 +0200 | [diff] [blame] | 711 | /* error - not a comment after all, keyword cannot start with slash */ |
| 712 | LOGVAL_YANG(ctx, LYVE_SYNTAX_YANG, "Invalid identifier first character '/'."); |
| 713 | return LY_EVALID; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 714 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 715 | break; |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 716 | case '\n': |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 717 | /* skip whitespaces (optsep) */ |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 718 | ++ctx->line; |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 719 | ctx->indent = 0; |
| 720 | break; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 721 | case ' ': |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 722 | /* skip whitespaces (optsep) */ |
| 723 | ++ctx->indent; |
| 724 | break; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 725 | case '\t': |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 726 | /* skip whitespaces (optsep) */ |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 727 | ctx->indent += 8; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 728 | break; |
| 729 | default: |
| 730 | /* either a keyword start or an invalid character */ |
| 731 | goto keyword_start; |
| 732 | } |
| 733 | |
| 734 | ++(*data); |
| 735 | } |
| 736 | |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 737 | #define IF_KW(STR, LEN, STMT) if (!strncmp(*(data), STR, LEN)) {MOVE_INPUT(ctx, data, LEN);*kw=STMT;} |
| 738 | #define IF_KW_PREFIX(STR, LEN) if (!strncmp(*(data), STR, LEN)) {MOVE_INPUT(ctx, data, LEN); |
| 739 | #define IF_KW_PREFIX_END } |
| 740 | |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 741 | keyword_start: |
| 742 | word_start = *data; |
| 743 | *kw = YANG_NONE; |
| 744 | |
| 745 | /* read the keyword itself */ |
| 746 | switch (**data) { |
| 747 | case 'a': |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 748 | MOVE_INPUT(ctx, data, 1); |
| 749 | IF_KW("rgument", 7, YANG_ARGUMENT) |
| 750 | else IF_KW("ugment", 6, YANG_AUGMENT) |
| 751 | else IF_KW("ction", 5, YANG_ACTION) |
| 752 | else IF_KW_PREFIX("ny", 2) |
| 753 | IF_KW("data", 4, YANG_ANYDATA) |
| 754 | else IF_KW("xml", 3, YANG_ANYXML) |
| 755 | IF_KW_PREFIX_END |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 756 | break; |
| 757 | case 'b': |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 758 | MOVE_INPUT(ctx, data, 1); |
| 759 | IF_KW("ase", 3, YANG_BASE) |
| 760 | else IF_KW("elongs-to", 9, YANG_BELONGS_TO) |
| 761 | else IF_KW("it", 2, YANG_BIT) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 762 | break; |
| 763 | case 'c': |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 764 | MOVE_INPUT(ctx, data, 1); |
| 765 | IF_KW("ase", 3, YANG_CASE) |
| 766 | else IF_KW("hoice", 5, YANG_CHOICE) |
| 767 | else IF_KW_PREFIX("on", 2) |
| 768 | IF_KW("fig", 3, YANG_CONFIG) |
| 769 | else IF_KW_PREFIX("ta", 2) |
| 770 | IF_KW("ct", 2, YANG_CONTACT) |
| 771 | else IF_KW("iner", 4, YANG_CONTAINER) |
| 772 | IF_KW_PREFIX_END |
| 773 | IF_KW_PREFIX_END |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 774 | break; |
| 775 | case 'd': |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 776 | MOVE_INPUT(ctx, data, 1); |
| 777 | IF_KW_PREFIX("e", 1) |
| 778 | IF_KW("fault", 5, YANG_DEFAULT) |
| 779 | else IF_KW("scription", 9, YANG_DESCRIPTION) |
| 780 | else IF_KW_PREFIX("viat", 4) |
| 781 | IF_KW("e", 1, YANG_DEVIATE) |
| 782 | else IF_KW("ion", 3, YANG_DEVIATION) |
| 783 | IF_KW_PREFIX_END |
| 784 | IF_KW_PREFIX_END |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 785 | break; |
| 786 | case 'e': |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 787 | MOVE_INPUT(ctx, data, 1); |
| 788 | IF_KW("num", 3, YANG_ENUM) |
| 789 | else IF_KW_PREFIX("rror-", 5) |
| 790 | IF_KW("app-tag", 7, YANG_ERROR_APP_TAG) |
| 791 | else IF_KW("message", 7, YANG_ERROR_MESSAGE) |
| 792 | IF_KW_PREFIX_END |
| 793 | else IF_KW("xtension", 8, YANG_EXTENSION) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 794 | break; |
| 795 | case 'f': |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 796 | MOVE_INPUT(ctx, data, 1); |
| 797 | IF_KW("eature", 6, YANG_FEATURE) |
| 798 | else IF_KW("raction-digits", 14, YANG_FRACTION_DIGITS) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 799 | break; |
| 800 | case 'g': |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 801 | MOVE_INPUT(ctx, data, 1); |
| 802 | IF_KW("rouping", 7, YANG_GROUPING) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 803 | break; |
| 804 | case 'i': |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 805 | MOVE_INPUT(ctx, data, 1); |
| 806 | IF_KW("dentity", 7, YANG_IDENTITY) |
| 807 | else IF_KW("f-feature", 9, YANG_IF_FEATURE) |
| 808 | else IF_KW("mport", 5, YANG_IMPORT) |
| 809 | else IF_KW_PREFIX("n", 1) |
| 810 | IF_KW("clude", 5, YANG_INCLUDE) |
| 811 | else IF_KW("put", 3, YANG_INPUT) |
| 812 | IF_KW_PREFIX_END |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 813 | break; |
| 814 | case 'k': |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 815 | MOVE_INPUT(ctx, data, 1); |
| 816 | IF_KW("ey", 2, YANG_KEY) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 817 | break; |
| 818 | case 'l': |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 819 | MOVE_INPUT(ctx, data, 1); |
| 820 | IF_KW_PREFIX("e", 1) |
| 821 | IF_KW("af-list", 7, YANG_LEAF_LIST) |
| 822 | else IF_KW("af", 2, YANG_LEAF) |
| 823 | else IF_KW("ngth", 4, YANG_LENGTH) |
| 824 | IF_KW_PREFIX_END |
| 825 | else IF_KW("ist", 3, YANG_LIST) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 826 | break; |
| 827 | case 'm': |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 828 | MOVE_INPUT(ctx, data, 1); |
| 829 | IF_KW_PREFIX("a", 1) |
| 830 | IF_KW("ndatory", 7, YANG_MANDATORY) |
| 831 | else IF_KW("x-elements", 10, YANG_MAX_ELEMENTS) |
| 832 | IF_KW_PREFIX_END |
| 833 | else IF_KW("in-elements", 11, YANG_MIN_ELEMENTS) |
| 834 | else IF_KW("ust", 3, YANG_MUST) |
| 835 | else IF_KW_PREFIX("od", 2) |
| 836 | IF_KW("ule", 3, YANG_MODULE) |
| 837 | else IF_KW("ifier", 5, YANG_MODIFIER) |
| 838 | IF_KW_PREFIX_END |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 839 | break; |
| 840 | case 'n': |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 841 | MOVE_INPUT(ctx, data, 1); |
| 842 | IF_KW("amespace", 8, YANG_NAMESPACE) |
| 843 | else IF_KW("otification", 11, YANG_NOTIFICATION) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 844 | break; |
| 845 | case 'o': |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 846 | MOVE_INPUT(ctx, data, 1); |
| 847 | IF_KW_PREFIX("r", 1) |
| 848 | IF_KW("dered-by", 8, YANG_ORDERED_BY) |
| 849 | else IF_KW("ganization", 10, YANG_ORGANIZATION) |
| 850 | IF_KW_PREFIX_END |
| 851 | else IF_KW("utput", 5, YANG_OUTPUT) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 852 | break; |
| 853 | case 'p': |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 854 | MOVE_INPUT(ctx, data, 1); |
| 855 | IF_KW("ath", 3, YANG_PATH) |
| 856 | else IF_KW("attern", 6, YANG_PATTERN) |
| 857 | else IF_KW("osition", 7, YANG_POSITION) |
| 858 | else IF_KW_PREFIX("re", 2) |
| 859 | IF_KW("fix", 3, YANG_PREFIX) |
| 860 | else IF_KW("sence", 5, YANG_PRESENCE) |
| 861 | IF_KW_PREFIX_END |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 862 | break; |
| 863 | case 'r': |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 864 | MOVE_INPUT(ctx, data, 1); |
| 865 | IF_KW("ange", 4, YANG_RANGE) |
| 866 | else IF_KW_PREFIX("e", 1) |
| 867 | IF_KW_PREFIX("f", 1) |
| 868 | IF_KW("erence", 6, YANG_REFERENCE) |
| 869 | else IF_KW("ine", 3, YANG_REFINE) |
| 870 | IF_KW_PREFIX_END |
| 871 | else IF_KW("quire-instance", 14, YANG_REQUIRE_INSTANCE) |
| 872 | else IF_KW("vision-date", 11, YANG_REVISION_DATE) |
| 873 | else IF_KW("vision", 6, YANG_REVISION) |
| 874 | IF_KW_PREFIX_END |
| 875 | else IF_KW("pc", 2, YANG_RPC) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 876 | break; |
| 877 | case 's': |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 878 | MOVE_INPUT(ctx, data, 1); |
| 879 | IF_KW("tatus", 5, YANG_STATUS) |
| 880 | else IF_KW("ubmodule", 8, YANG_SUBMODULE) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 881 | break; |
| 882 | case 't': |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 883 | MOVE_INPUT(ctx, data, 1); |
| 884 | IF_KW("ypedef", 6, YANG_TYPEDEF) |
| 885 | else IF_KW("ype", 3, YANG_TYPE) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 886 | break; |
| 887 | case 'u': |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 888 | MOVE_INPUT(ctx, data, 1); |
| 889 | IF_KW_PREFIX("ni", 2) |
| 890 | IF_KW("que", 3, YANG_UNIQUE) |
| 891 | else IF_KW("ts", 2, YANG_UNITS) |
| 892 | IF_KW_PREFIX_END |
| 893 | else IF_KW("ses", 3, YANG_USES) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 894 | break; |
| 895 | case 'v': |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 896 | MOVE_INPUT(ctx, data, 1); |
| 897 | IF_KW("alue", 4, YANG_VALUE) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 898 | break; |
| 899 | case 'w': |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 900 | MOVE_INPUT(ctx, data, 1); |
| 901 | IF_KW("hen", 3, YANG_WHEN) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 902 | break; |
| 903 | case 'y': |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 904 | MOVE_INPUT(ctx, data, 1); |
| 905 | IF_KW("ang-version", 11, YANG_YANG_VERSION) |
| 906 | else IF_KW("in-element", 10, YANG_YIN_ELEMENT) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 907 | break; |
| 908 | case ';': |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 909 | MOVE_INPUT(ctx, data, 1); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 910 | *kw = YANG_SEMICOLON; |
Radek Krejci | 626df48 | 2018-10-11 15:06:31 +0200 | [diff] [blame] | 911 | goto success; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 912 | case '{': |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 913 | MOVE_INPUT(ctx, data, 1); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 914 | *kw = YANG_LEFT_BRACE; |
Radek Krejci | 626df48 | 2018-10-11 15:06:31 +0200 | [diff] [blame] | 915 | goto success; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 916 | case '}': |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 917 | MOVE_INPUT(ctx, data, 1); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 918 | *kw = YANG_RIGHT_BRACE; |
Radek Krejci | 626df48 | 2018-10-11 15:06:31 +0200 | [diff] [blame] | 919 | goto success; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 920 | default: |
| 921 | break; |
| 922 | } |
| 923 | |
Radek Krejci | 0904c16 | 2019-01-02 15:03:59 +0100 | [diff] [blame] | 924 | #undef IF_KW |
| 925 | #undef IF_KW_PREFIX |
| 926 | #undef IF_KW_PREFIX_END |
| 927 | |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 928 | if (*kw != YANG_NONE) { |
| 929 | /* make sure we have the whole keyword */ |
| 930 | switch (**data) { |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 931 | case '\n': |
| 932 | ++ctx->line; |
| 933 | /* fallthrough */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 934 | case ' ': |
| 935 | case '\t': |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 936 | /* mandatory "sep" */ |
| 937 | break; |
Radek Krejci | 156ccaf | 2018-10-15 15:49:17 +0200 | [diff] [blame] | 938 | case ':': |
| 939 | /* keyword is not actually a keyword, but prefix of an extension. |
| 940 | * To avoid repeated check of the prefix syntax, move to the point where the colon was read |
| 941 | * and we will be checking the keyword (extension instance) itself */ |
| 942 | prefix = 1; |
| 943 | MOVE_INPUT(ctx, data, 1); |
| 944 | goto extension; |
Radek Krejci | dcc7b32 | 2018-10-11 14:24:02 +0200 | [diff] [blame] | 945 | case '{': |
| 946 | /* allowed only for input and output statements which can be without arguments */ |
| 947 | if (*kw == YANG_INPUT || *kw == YANG_OUTPUT) { |
| 948 | break; |
| 949 | } |
| 950 | /* fallthrough */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 951 | default: |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 952 | MOVE_INPUT(ctx, data, 1); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 953 | LOGVAL_YANG(ctx, LY_VCODE_INSTREXP, (int)(*data - word_start), word_start, |
| 954 | "a keyword followed by a separator"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 955 | return LY_EVALID; |
| 956 | } |
| 957 | } else { |
| 958 | /* still can be an extension */ |
| 959 | prefix = 0; |
Radek Krejci | 156ccaf | 2018-10-15 15:49:17 +0200 | [diff] [blame] | 960 | extension: |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 961 | while (**data && (**data != ' ') && (**data != '\t') && (**data != '\n') && (**data != '{') && (**data != ';')) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 962 | LY_CHECK_ERR_RET(ly_getutf8(data, &c, &len), |
| 963 | LOGVAL_YANG(ctx, LY_VCODE_INCHAR, (*data)[-len]), LY_EVALID); |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 964 | ++ctx->indent; |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 965 | /* check character validity */ |
| 966 | 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] | 967 | } |
| 968 | if (!**data) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 969 | LOGVAL_YANG(ctx, LY_VCODE_EOF); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 970 | return LY_EVALID; |
| 971 | } |
| 972 | |
| 973 | /* prefix is mandatory for extension instances */ |
Radek Krejci | dcc7b32 | 2018-10-11 14:24:02 +0200 | [diff] [blame] | 974 | if (prefix != 2) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 975 | 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] | 976 | return LY_EVALID; |
| 977 | } |
| 978 | |
| 979 | *kw = YANG_CUSTOM; |
| 980 | } |
Radek Krejci | 626df48 | 2018-10-11 15:06:31 +0200 | [diff] [blame] | 981 | success: |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 982 | if (word_p) { |
| 983 | *word_p = (char *)word_start; |
| 984 | *word_len = *data - word_start; |
| 985 | } |
| 986 | |
| 987 | return LY_SUCCESS; |
| 988 | } |
| 989 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 990 | /** |
| 991 | * @brief Parse extension instance substatements. |
| 992 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 993 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 994 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 995 | * @param[in] word Extension instance substatement name (keyword). |
| 996 | * @param[in] word_len Extension instance substatement name length. |
| 997 | * @param[in,out] child Children of this extension instance to add to. |
| 998 | * |
| 999 | * @return LY_ERR values. |
| 1000 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1001 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1002 | 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] | 1003 | struct lysp_stmt **child) |
| 1004 | { |
| 1005 | char *buf; |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1006 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1007 | enum yang_keyword kw; |
| 1008 | struct lysp_stmt *stmt, *par_child; |
| 1009 | |
| 1010 | stmt = calloc(1, sizeof *stmt); |
| 1011 | LY_CHECK_ERR_RET(!stmt, LOGMEM(NULL), LY_EMEM); |
| 1012 | |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1013 | stmt->stmt = lydict_insert(ctx->ctx, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1014 | |
| 1015 | /* get optional argument */ |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1016 | LY_CHECK_RET(get_argument(ctx, data, Y_MAYBE_STR_ARG, &word, &buf, &word_len)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1017 | |
Radek Krejci | 0ae092d | 2018-09-20 16:43:19 +0200 | [diff] [blame] | 1018 | if (word) { |
| 1019 | if (buf) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1020 | stmt->arg = lydict_insert_zc(ctx->ctx, word); |
Radek Krejci | 0ae092d | 2018-09-20 16:43:19 +0200 | [diff] [blame] | 1021 | } else { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1022 | stmt->arg = lydict_insert(ctx->ctx, word, word_len); |
Radek Krejci | 0ae092d | 2018-09-20 16:43:19 +0200 | [diff] [blame] | 1023 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1024 | } |
| 1025 | |
| 1026 | /* insert into parent statements */ |
| 1027 | if (!*child) { |
| 1028 | *child = stmt; |
| 1029 | } else { |
| 1030 | for (par_child = *child; par_child->next; par_child = par_child->next); |
| 1031 | par_child->next = stmt; |
| 1032 | } |
| 1033 | |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 1034 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, ) { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1035 | LY_CHECK_RET(parse_ext_substmt(ctx, data, word, word_len, &stmt->child)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1036 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1037 | return ret; |
| 1038 | } |
| 1039 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1040 | /** |
| 1041 | * @brief Parse extension instance. |
| 1042 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1043 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1044 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 1045 | * @param[in] ext_name Extension instance substatement name (keyword). |
| 1046 | * @param[in] ext_name_len Extension instance substatement name length. |
| 1047 | * @param[in] insubstmt Type of the keyword this extension instance is a substatement of. |
| 1048 | * @param[in] insubstmt_index Index of the keyword instance this extension instance is a substatement of. |
| 1049 | * @param[in,out] exts Extension instances to add to. |
| 1050 | * |
| 1051 | * @return LY_ERR values. |
| 1052 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1053 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1054 | 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] | 1055 | uint32_t insubstmt_index, struct lysp_ext_instance **exts) |
| 1056 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1057 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1058 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 1059 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1060 | struct lysp_ext_instance *e; |
| 1061 | enum yang_keyword kw; |
| 1062 | |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 1063 | LY_ARRAY_NEW_RET(ctx->ctx, *exts, e, LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1064 | |
| 1065 | /* store name and insubstmt info */ |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1066 | e->name = lydict_insert(ctx->ctx, ext_name, ext_name_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1067 | e->insubstmt = insubstmt; |
| 1068 | e->insubstmt_index = insubstmt_index; |
| 1069 | |
| 1070 | /* get optional argument */ |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1071 | LY_CHECK_RET(get_argument(ctx, data, Y_MAYBE_STR_ARG, &word, &buf, &word_len)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1072 | |
Radek Krejci | 0ae092d | 2018-09-20 16:43:19 +0200 | [diff] [blame] | 1073 | if (word) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1074 | INSERT_WORD(ctx, buf, e->argument, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1075 | } |
| 1076 | |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 1077 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1078 | LY_CHECK_RET(parse_ext_substmt(ctx, data, word, word_len, &e->child)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1079 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1080 | return ret; |
| 1081 | } |
| 1082 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1083 | /** |
| 1084 | * @brief Parse a generic text field without specific constraints. Those are contact, organization, |
| 1085 | * description, etc... |
| 1086 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1087 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1088 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 1089 | * @param[in] substmt Type of this substatement. |
| 1090 | * @param[in] substmt_index Index of this substatement. |
| 1091 | * @param[in,out] value Place to store the parsed value. |
| 1092 | * @param[in] arg Type of the YANG keyword argument (of the value). |
| 1093 | * @param[in,out] exts Extension instances to add to. |
| 1094 | * |
| 1095 | * @return LY_ERR values. |
| 1096 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1097 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1098 | 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] | 1099 | const char **value, enum yang_arg arg, struct lysp_ext_instance **exts) |
| 1100 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1101 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1102 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 1103 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1104 | enum yang_keyword kw; |
| 1105 | |
| 1106 | if (*value) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1107 | LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, lyext_substmt2str(substmt)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1108 | return LY_EVALID; |
| 1109 | } |
| 1110 | |
| 1111 | /* get value */ |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1112 | LY_CHECK_RET(get_argument(ctx, data, arg, &word, &buf, &word_len)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1113 | |
| 1114 | /* store value and spend buf if allocated */ |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1115 | INSERT_WORD(ctx, buf, *value, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1116 | |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 1117 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1118 | switch (kw) { |
| 1119 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1120 | LY_CHECK_RET(parse_ext(ctx, data, word, word_len, substmt, substmt_index, exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1121 | break; |
| 1122 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1123 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), lyext_substmt2str(substmt)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1124 | return LY_EVALID; |
| 1125 | } |
| 1126 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1127 | return ret; |
| 1128 | } |
| 1129 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1130 | /** |
| 1131 | * @brief Parse the yang-version statement. |
| 1132 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1133 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1134 | * @param[in,out] data Data to read from, always moved to currently handled character. |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1135 | * @param[out] version Storage for the parsed information. |
| 1136 | * @param[in, out] exts Extension instances to add to. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1137 | * |
| 1138 | * @return LY_ERR values. |
| 1139 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1140 | static LY_ERR |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1141 | parse_yangversion(struct ly_parser_ctx *ctx, const char **data, uint8_t *version, struct lysp_ext_instance **exts) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1142 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1143 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1144 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 1145 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1146 | enum yang_keyword kw; |
| 1147 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1148 | if (*version) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1149 | LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "yang-version"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1150 | return LY_EVALID; |
| 1151 | } |
| 1152 | |
| 1153 | /* get value */ |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1154 | LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1155 | |
| 1156 | if ((word_len == 3) && !strncmp(word, "1.0", word_len)) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1157 | *version = LYS_VERSION_1_0; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1158 | } else if ((word_len == 3) && !strncmp(word, "1.1", word_len)) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1159 | *version = LYS_VERSION_1_1; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1160 | } else { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1161 | LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "yang-version"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1162 | free(buf); |
| 1163 | return LY_EVALID; |
| 1164 | } |
| 1165 | free(buf); |
| 1166 | |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 1167 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1168 | switch (kw) { |
| 1169 | case YANG_CUSTOM: |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1170 | LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_VERSION, 0, exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1171 | break; |
| 1172 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1173 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "yang-version"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1174 | return LY_EVALID; |
| 1175 | } |
| 1176 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1177 | return ret; |
| 1178 | } |
| 1179 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1180 | /** |
| 1181 | * @brief Parse the belongs-to statement. |
| 1182 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1183 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1184 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 1185 | * @param[in,out] belongsto Place to store the parsed value. |
| 1186 | * @param[in,out] prefix Place to store the parsed belongs-to prefix value. |
| 1187 | * @param[in,out] exts Extension instances to add to. |
| 1188 | * |
| 1189 | * @return LY_ERR values. |
| 1190 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1191 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1192 | 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] | 1193 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1194 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1195 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 1196 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1197 | enum yang_keyword kw; |
| 1198 | |
| 1199 | if (*belongsto) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1200 | LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "belongs-to"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1201 | return LY_EVALID; |
| 1202 | } |
| 1203 | |
| 1204 | /* get value */ |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1205 | LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1206 | |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1207 | INSERT_WORD(ctx, buf, *belongsto, word, word_len); |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 1208 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1209 | switch (kw) { |
| 1210 | case YANG_PREFIX: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1211 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_PREFIX, 0, prefix, Y_IDENTIF_ARG, exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1212 | break; |
| 1213 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1214 | LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_BELONGSTO, 0, exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1215 | break; |
| 1216 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1217 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "belongs-to"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1218 | return LY_EVALID; |
| 1219 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1220 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1221 | LY_CHECK_RET(ret); |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 1222 | checks: |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1223 | /* mandatory substatements */ |
| 1224 | if (!*prefix) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1225 | LOGVAL_YANG(ctx, LY_VCODE_MISSTMT, "prefix", "belongs-to"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1226 | return LY_EVALID; |
| 1227 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1228 | return ret; |
| 1229 | } |
| 1230 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1231 | /** |
| 1232 | * @brief Parse the revision-date statement. |
| 1233 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1234 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1235 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 1236 | * @param[in,out] rev Array to store the parsed value in. |
| 1237 | * @param[in,out] exts Extension instances to add to. |
| 1238 | * |
| 1239 | * @return LY_ERR values. |
| 1240 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1241 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1242 | 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] | 1243 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1244 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1245 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 1246 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1247 | enum yang_keyword kw; |
| 1248 | |
| 1249 | if (rev[0]) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1250 | LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "revision-date"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1251 | return LY_EVALID; |
| 1252 | } |
| 1253 | |
| 1254 | /* get value */ |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1255 | LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1256 | |
| 1257 | /* check value */ |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1258 | if (lysp_check_date(ctx, word, word_len, "revision-date")) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1259 | free(buf); |
| 1260 | return LY_EVALID; |
| 1261 | } |
| 1262 | |
| 1263 | /* store value and spend buf if allocated */ |
| 1264 | strncpy(rev, word, word_len); |
| 1265 | free(buf); |
| 1266 | |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 1267 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1268 | switch (kw) { |
| 1269 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1270 | LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_REVISIONDATE, 0, exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1271 | break; |
| 1272 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1273 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "revision-date"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1274 | return LY_EVALID; |
| 1275 | } |
| 1276 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1277 | return ret; |
| 1278 | } |
| 1279 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1280 | /** |
| 1281 | * @brief Parse the include statement. |
| 1282 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1283 | * @param[in] ctx yang parser context for logging. |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1284 | * @param[in] module_name Name of the module to check name collisions. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1285 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 1286 | * @param[in,out] includes Parsed includes to add to. |
| 1287 | * |
| 1288 | * @return LY_ERR values. |
| 1289 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1290 | static LY_ERR |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1291 | parse_include(struct ly_parser_ctx *ctx, const char *module_name, const char **data, struct lysp_include **includes) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1292 | { |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 1293 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1294 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 1295 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1296 | enum yang_keyword kw; |
| 1297 | struct lysp_include *inc; |
| 1298 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1299 | LY_ARRAY_NEW_RET(ctx->ctx, *includes, inc, LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1300 | |
| 1301 | /* get value */ |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1302 | LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1303 | |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 1304 | INSERT_WORD(ctx, buf, inc->name, word, word_len); |
| 1305 | |
Radek Krejci | faa1eac | 2018-10-30 14:34:55 +0100 | [diff] [blame] | 1306 | /* submodules share the namespace with the module names, so there must not be |
| 1307 | * a module of the same name in the context, no need for revision matching */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1308 | if (!strcmp(module_name, inc->name) || ly_ctx_get_module_latest(ctx->ctx, inc->name)) { |
Radek Krejci | faa1eac | 2018-10-30 14:34:55 +0100 | [diff] [blame] | 1309 | LOGVAL_YANG(ctx, LYVE_SYNTAX_YANG, "Name collision between module and submodule of name \"%s\".", inc->name); |
| 1310 | return LY_EVALID; |
| 1311 | } |
| 1312 | |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 1313 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1314 | switch (kw) { |
| 1315 | case YANG_DESCRIPTION: |
Radek Krejci | ceaf212 | 2019-01-02 15:03:26 +0100 | [diff] [blame] | 1316 | YANG_CHECK_STMTVER2_RET(ctx, "description", "include"); |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1317 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &inc->dsc, Y_STR_ARG, &inc->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1318 | break; |
| 1319 | case YANG_REFERENCE: |
Radek Krejci | ceaf212 | 2019-01-02 15:03:26 +0100 | [diff] [blame] | 1320 | YANG_CHECK_STMTVER2_RET(ctx, "reference", "include"); |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1321 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &inc->ref, Y_STR_ARG, &inc->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1322 | break; |
| 1323 | case YANG_REVISION_DATE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1324 | LY_CHECK_RET(parse_revisiondate(ctx, data, inc->rev, &inc->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1325 | break; |
| 1326 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1327 | LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &inc->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1328 | break; |
| 1329 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1330 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "include"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1331 | return LY_EVALID; |
| 1332 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1333 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1334 | return ret; |
| 1335 | } |
| 1336 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1337 | /** |
| 1338 | * @brief Parse the import statement. |
| 1339 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1340 | * @param[in] ctx yang parser context for logging. |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1341 | * @param[in] module_prefix Prefix of the module to check prefix collisions. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1342 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 1343 | * @param[in,out] imports Parsed imports to add to. |
| 1344 | * |
| 1345 | * @return LY_ERR values. |
| 1346 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1347 | static LY_ERR |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1348 | parse_import(struct ly_parser_ctx *ctx, const char *module_prefix, const char **data, struct lysp_import **imports) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1349 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1350 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1351 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 1352 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1353 | enum yang_keyword kw; |
| 1354 | struct lysp_import *imp; |
| 1355 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1356 | LY_ARRAY_NEW_RET(ctx->ctx, *imports, imp, LY_EVALID); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1357 | |
| 1358 | /* get value */ |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1359 | LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len)); |
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 | |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 1362 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1363 | switch (kw) { |
| 1364 | case YANG_PREFIX: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1365 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_PREFIX, 0, &imp->prefix, Y_IDENTIF_ARG, &imp->exts)); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1366 | LY_CHECK_RET(lysp_check_prefix(ctx, *imports, module_prefix, &imp->prefix), LY_EVALID); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1367 | break; |
| 1368 | case YANG_DESCRIPTION: |
Radek Krejci | ceaf212 | 2019-01-02 15:03:26 +0100 | [diff] [blame] | 1369 | YANG_CHECK_STMTVER2_RET(ctx, "description", "import"); |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1370 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &imp->dsc, Y_STR_ARG, &imp->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1371 | break; |
| 1372 | case YANG_REFERENCE: |
Radek Krejci | ceaf212 | 2019-01-02 15:03:26 +0100 | [diff] [blame] | 1373 | YANG_CHECK_STMTVER2_RET(ctx, "reference", "import"); |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1374 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &imp->ref, Y_STR_ARG, &imp->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1375 | break; |
| 1376 | case YANG_REVISION_DATE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1377 | LY_CHECK_RET(parse_revisiondate(ctx, data, imp->rev, &imp->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1378 | break; |
| 1379 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1380 | LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &imp->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1381 | break; |
| 1382 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1383 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "import"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1384 | return LY_EVALID; |
| 1385 | } |
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); |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 1388 | checks: |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1389 | /* mandatory substatements */ |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 1390 | LY_CHECK_ERR_RET(!imp->prefix, LOGVAL_YANG(ctx, LY_VCODE_MISSTMT, "prefix", "import"), LY_EVALID); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1391 | |
| 1392 | return ret; |
| 1393 | } |
| 1394 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1395 | /** |
| 1396 | * @brief Parse the revision statement. |
| 1397 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1398 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1399 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 1400 | * @param[in,out] revs Parsed revisions to add to. |
| 1401 | * |
| 1402 | * @return LY_ERR values. |
| 1403 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1404 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1405 | 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] | 1406 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1407 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1408 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 1409 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1410 | enum yang_keyword kw; |
| 1411 | struct lysp_revision *rev; |
| 1412 | |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 1413 | LY_ARRAY_NEW_RET(ctx->ctx, *revs, rev, LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1414 | |
| 1415 | /* get value */ |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1416 | LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1417 | |
| 1418 | /* check value */ |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1419 | if (lysp_check_date(ctx, word, word_len, "revision")) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1420 | return LY_EVALID; |
| 1421 | } |
| 1422 | |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 1423 | strncpy(rev->date, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1424 | free(buf); |
| 1425 | |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 1426 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1427 | switch (kw) { |
| 1428 | case YANG_DESCRIPTION: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1429 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &rev->dsc, Y_STR_ARG, &rev->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1430 | break; |
| 1431 | case YANG_REFERENCE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1432 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &rev->ref, Y_STR_ARG, &rev->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1433 | break; |
| 1434 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1435 | LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &rev->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1436 | break; |
| 1437 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1438 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "revision"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1439 | return LY_EVALID; |
| 1440 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1441 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1442 | return ret; |
| 1443 | } |
| 1444 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1445 | /** |
| 1446 | * @brief Parse a generic text field that can have more instances such as base. |
| 1447 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1448 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1449 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 1450 | * @param[in] substmt Type of this substatement. |
| 1451 | * @param[in,out] texts Parsed values to add to. |
| 1452 | * @param[in] arg Type of the expected argument. |
| 1453 | * @param[in,out] exts Extension instances to add to. |
| 1454 | * |
| 1455 | * @return LY_ERR values. |
| 1456 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1457 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1458 | 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] | 1459 | struct lysp_ext_instance **exts) |
| 1460 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1461 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1462 | char *buf, *word; |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 1463 | const char **item; |
| 1464 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1465 | enum yang_keyword kw; |
| 1466 | |
| 1467 | /* allocate new pointer */ |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 1468 | LY_ARRAY_NEW_RET(ctx->ctx, *texts, item, LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1469 | |
| 1470 | /* get value */ |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1471 | LY_CHECK_RET(get_argument(ctx, data, arg, &word, &buf, &word_len)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1472 | |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 1473 | INSERT_WORD(ctx, buf, *item, word, word_len); |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 1474 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1475 | switch (kw) { |
| 1476 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1477 | LY_CHECK_RET(parse_ext(ctx, data, word, word_len, substmt, LY_ARRAY_SIZE(*texts) - 1, exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1478 | break; |
| 1479 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1480 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), lyext_substmt2str(substmt)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1481 | return LY_EVALID; |
| 1482 | } |
| 1483 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1484 | return ret; |
| 1485 | } |
| 1486 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1487 | /** |
| 1488 | * @brief Parse the config statement. |
| 1489 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1490 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1491 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 1492 | * @param[in,out] flags Flags to add to. |
| 1493 | * @param[in,out] exts Extension instances to add to. |
| 1494 | * |
| 1495 | * @return LY_ERR values. |
| 1496 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1497 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1498 | 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] | 1499 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1500 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1501 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 1502 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1503 | enum yang_keyword kw; |
| 1504 | |
| 1505 | if (*flags & LYS_CONFIG_MASK) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1506 | LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "config"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1507 | return LY_EVALID; |
| 1508 | } |
| 1509 | |
| 1510 | /* get value */ |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1511 | LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1512 | |
| 1513 | if ((word_len == 4) && !strncmp(word, "true", word_len)) { |
| 1514 | *flags |= LYS_CONFIG_W; |
| 1515 | } else if ((word_len == 5) && !strncmp(word, "false", word_len)) { |
| 1516 | *flags |= LYS_CONFIG_R; |
| 1517 | } else { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1518 | LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "config"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1519 | free(buf); |
| 1520 | return LY_EVALID; |
| 1521 | } |
| 1522 | free(buf); |
| 1523 | |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 1524 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1525 | switch (kw) { |
| 1526 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1527 | LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_CONFIG, 0, exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1528 | break; |
| 1529 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1530 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "config"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1531 | return LY_EVALID; |
| 1532 | } |
| 1533 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1534 | return ret; |
| 1535 | } |
| 1536 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1537 | /** |
| 1538 | * @brief Parse the mandatory statement. |
| 1539 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1540 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1541 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 1542 | * @param[in,out] flags Flags to add to. |
| 1543 | * @param[in,out] exts Extension instances to add to. |
| 1544 | * |
| 1545 | * @return LY_ERR values. |
| 1546 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1547 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1548 | 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] | 1549 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1550 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1551 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 1552 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1553 | enum yang_keyword kw; |
| 1554 | |
| 1555 | if (*flags & LYS_MAND_MASK) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1556 | LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "mandatory"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1557 | return LY_EVALID; |
| 1558 | } |
| 1559 | |
| 1560 | /* get value */ |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1561 | LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1562 | |
| 1563 | if ((word_len == 4) && !strncmp(word, "true", word_len)) { |
| 1564 | *flags |= LYS_MAND_TRUE; |
| 1565 | } else if ((word_len == 5) && !strncmp(word, "false", word_len)) { |
| 1566 | *flags |= LYS_MAND_FALSE; |
| 1567 | } else { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1568 | LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "mandatory"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1569 | free(buf); |
| 1570 | return LY_EVALID; |
| 1571 | } |
| 1572 | free(buf); |
| 1573 | |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 1574 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1575 | switch (kw) { |
| 1576 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1577 | LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_MANDATORY, 0, exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1578 | break; |
| 1579 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1580 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "mandatory"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1581 | return LY_EVALID; |
| 1582 | } |
| 1583 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1584 | return ret; |
| 1585 | } |
| 1586 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1587 | /** |
| 1588 | * @brief Parse a restriction such as range or length. |
| 1589 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1590 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1591 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 1592 | * @param[in] restr_kw Type of this particular restriction. |
| 1593 | * @param[in,out] exts Extension instances to add to. |
| 1594 | * |
| 1595 | * @return LY_ERR values. |
| 1596 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1597 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1598 | 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] | 1599 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1600 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1601 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 1602 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1603 | enum yang_keyword kw; |
| 1604 | |
| 1605 | /* get value */ |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1606 | LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1607 | |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1608 | INSERT_WORD(ctx, buf, restr->arg, word, word_len); |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 1609 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1610 | switch (kw) { |
| 1611 | case YANG_DESCRIPTION: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1612 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &restr->dsc, Y_STR_ARG, &restr->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1613 | break; |
| 1614 | case YANG_REFERENCE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1615 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &restr->ref, Y_STR_ARG, &restr->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1616 | break; |
| 1617 | case YANG_ERROR_APP_TAG: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1618 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_ERRTAG, 0, &restr->eapptag, Y_STR_ARG, &restr->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1619 | break; |
| 1620 | case YANG_ERROR_MESSAGE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1621 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_ERRMSG, 0, &restr->emsg, Y_STR_ARG, &restr->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1622 | break; |
| 1623 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1624 | LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &restr->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1625 | break; |
| 1626 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1627 | 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] | 1628 | return LY_EVALID; |
| 1629 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1630 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1631 | return ret; |
| 1632 | } |
| 1633 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1634 | /** |
| 1635 | * @brief Parse a restriction that can have more instances such as must. |
| 1636 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1637 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1638 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 1639 | * @param[in] restr_kw Type of this particular restriction. |
| 1640 | * @param[in,out] restrs Restrictions to add to. |
| 1641 | * |
| 1642 | * @return LY_ERR values. |
| 1643 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1644 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1645 | 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] | 1646 | { |
| 1647 | struct lysp_restr *restr; |
| 1648 | |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 1649 | LY_ARRAY_NEW_RET(ctx->ctx, *restrs, restr, LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1650 | return parse_restr(ctx, data, restr_kw, restr); |
| 1651 | } |
| 1652 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1653 | /** |
| 1654 | * @brief Parse the status statement. |
| 1655 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1656 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1657 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 1658 | * @param[in,out] flags Flags to add to. |
| 1659 | * @param[in,out] exts Extension instances to add to. |
| 1660 | * |
| 1661 | * @return LY_ERR values. |
| 1662 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1663 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1664 | 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] | 1665 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1666 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1667 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 1668 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1669 | enum yang_keyword kw; |
| 1670 | |
| 1671 | if (*flags & LYS_STATUS_MASK) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1672 | LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "status"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1673 | return LY_EVALID; |
| 1674 | } |
| 1675 | |
| 1676 | /* get value */ |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1677 | LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1678 | |
| 1679 | if ((word_len == 7) && !strncmp(word, "current", word_len)) { |
| 1680 | *flags |= LYS_STATUS_CURR; |
| 1681 | } else if ((word_len == 10) && !strncmp(word, "deprecated", word_len)) { |
| 1682 | *flags |= LYS_STATUS_DEPRC; |
| 1683 | } else if ((word_len == 8) && !strncmp(word, "obsolete", word_len)) { |
| 1684 | *flags |= LYS_STATUS_OBSLT; |
| 1685 | } else { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1686 | LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "status"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1687 | free(buf); |
| 1688 | return LY_EVALID; |
| 1689 | } |
| 1690 | free(buf); |
| 1691 | |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 1692 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1693 | switch (kw) { |
| 1694 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1695 | LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_STATUS, 0, exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1696 | break; |
| 1697 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1698 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "status"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1699 | return LY_EVALID; |
| 1700 | } |
| 1701 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1702 | return ret; |
| 1703 | } |
| 1704 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1705 | /** |
| 1706 | * @brief Parse the when statement. |
| 1707 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1708 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1709 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 1710 | * @param[in,out] when_p When pointer to parse to. |
| 1711 | * |
| 1712 | * @return LY_ERR values. |
| 1713 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1714 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1715 | 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] | 1716 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1717 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1718 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 1719 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1720 | enum yang_keyword kw; |
| 1721 | struct lysp_when *when; |
| 1722 | |
| 1723 | if (*when_p) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1724 | LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "when"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1725 | return LY_EVALID; |
| 1726 | } |
| 1727 | |
| 1728 | when = calloc(1, sizeof *when); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1729 | LY_CHECK_ERR_RET(!when, LOGMEM(ctx->ctx), LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1730 | *when_p = when; |
| 1731 | |
| 1732 | /* get value */ |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1733 | LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len)); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1734 | INSERT_WORD(ctx, buf, when->cond, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1735 | |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 1736 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1737 | switch (kw) { |
| 1738 | case YANG_DESCRIPTION: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1739 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &when->dsc, Y_STR_ARG, &when->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1740 | break; |
| 1741 | case YANG_REFERENCE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1742 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &when->ref, Y_STR_ARG, &when->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1743 | break; |
| 1744 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1745 | LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &when->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1746 | break; |
| 1747 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1748 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "when"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1749 | return LY_EVALID; |
| 1750 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1751 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1752 | return ret; |
| 1753 | } |
| 1754 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1755 | /** |
| 1756 | * @brief Parse the anydata or anyxml statement. |
| 1757 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1758 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1759 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 1760 | * @param[in] kw Type of this particular keyword. |
| 1761 | * @param[in,out] siblings Siblings to add to. |
| 1762 | * |
| 1763 | * @return LY_ERR values. |
| 1764 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1765 | static LY_ERR |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1766 | parse_any(struct ly_parser_ctx *ctx, const char **data, enum yang_keyword kw, struct lysp_node *parent, struct lysp_node **siblings) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1767 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1768 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1769 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 1770 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1771 | struct lysp_node *iter; |
| 1772 | struct lysp_node_anydata *any; |
| 1773 | |
| 1774 | /* create structure */ |
| 1775 | any = calloc(1, sizeof *any); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1776 | LY_CHECK_ERR_RET(!any, LOGMEM(ctx->ctx), LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1777 | any->nodetype = kw == YANG_ANYDATA ? LYS_ANYDATA : LYS_ANYXML; |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1778 | any->parent = parent; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1779 | |
| 1780 | /* insert into siblings */ |
| 1781 | if (!*siblings) { |
| 1782 | *siblings = (struct lysp_node *)any; |
| 1783 | } else { |
| 1784 | for (iter = *siblings; iter->next; iter = iter->next); |
| 1785 | iter->next = (struct lysp_node *)any; |
| 1786 | } |
| 1787 | |
| 1788 | /* get name */ |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1789 | LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len)); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1790 | INSERT_WORD(ctx, buf, any->name, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1791 | |
| 1792 | /* parse substatements */ |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 1793 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1794 | switch (kw) { |
| 1795 | case YANG_CONFIG: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1796 | LY_CHECK_RET(parse_config(ctx, data, &any->flags, &any->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1797 | break; |
| 1798 | case YANG_DESCRIPTION: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1799 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &any->dsc, Y_STR_ARG, &any->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1800 | break; |
| 1801 | case YANG_IF_FEATURE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1802 | LY_CHECK_RET(parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &any->iffeatures, Y_STR_ARG, &any->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1803 | break; |
| 1804 | case YANG_MANDATORY: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1805 | LY_CHECK_RET(parse_mandatory(ctx, data, &any->flags, &any->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1806 | break; |
| 1807 | case YANG_MUST: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1808 | LY_CHECK_RET(parse_restrs(ctx, data, kw, &any->musts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1809 | break; |
| 1810 | case YANG_REFERENCE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1811 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &any->ref, Y_STR_ARG, &any->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1812 | break; |
| 1813 | case YANG_STATUS: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1814 | LY_CHECK_RET(parse_status(ctx, data, &any->flags, &any->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1815 | break; |
| 1816 | case YANG_WHEN: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1817 | LY_CHECK_RET(parse_when(ctx, data, &any->when)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1818 | break; |
| 1819 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1820 | LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &any->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1821 | break; |
| 1822 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1823 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1824 | (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] | 1825 | return LY_EVALID; |
| 1826 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1827 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1828 | return ret; |
| 1829 | } |
| 1830 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1831 | /** |
| 1832 | * @brief Parse the value or position statement. Substatement of type enum statement. |
| 1833 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1834 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1835 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 1836 | * @param[in] val_kw Type of this particular keyword. |
| 1837 | * @param[in,out] value Value to write to. |
| 1838 | * @param[in,out] flags Flags to write to. |
| 1839 | * @param[in,out] exts Extension instances to add to. |
| 1840 | * |
| 1841 | * @return LY_ERR values. |
| 1842 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1843 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1844 | 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] | 1845 | struct lysp_ext_instance **exts) |
| 1846 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1847 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1848 | char *buf, *word, *ptr; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 1849 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1850 | long int num; |
| 1851 | unsigned long int unum; |
| 1852 | enum yang_keyword kw; |
| 1853 | |
| 1854 | if (*flags & LYS_SET_VALUE) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1855 | LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(val_kw)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1856 | return LY_EVALID; |
| 1857 | } |
| 1858 | *flags |= LYS_SET_VALUE; |
| 1859 | |
| 1860 | /* get value */ |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1861 | LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1862 | |
| 1863 | 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] | 1864 | LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw)); |
Radek Krejci | 8b76466 | 2018-11-14 14:15:13 +0100 | [diff] [blame] | 1865 | goto error; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1866 | } |
| 1867 | |
| 1868 | errno = 0; |
| 1869 | if (val_kw == YANG_VALUE) { |
| 1870 | num = strtol(word, &ptr, 10); |
Radek Krejci | 8b76466 | 2018-11-14 14:15:13 +0100 | [diff] [blame] | 1871 | if (num < INT64_C(-2147483648) || num > INT64_C(2147483647)) { |
| 1872 | LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw)); |
| 1873 | goto error; |
| 1874 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1875 | } else { |
| 1876 | unum = strtoul(word, &ptr, 10); |
Radek Krejci | 8b76466 | 2018-11-14 14:15:13 +0100 | [diff] [blame] | 1877 | if (unum > UINT64_C(4294967295)) { |
| 1878 | LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw)); |
| 1879 | goto error; |
| 1880 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1881 | } |
| 1882 | /* we have not parsed the whole argument */ |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 1883 | if ((size_t)(ptr - word) != word_len) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1884 | LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw)); |
Radek Krejci | 8b76466 | 2018-11-14 14:15:13 +0100 | [diff] [blame] | 1885 | goto error; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1886 | } |
| 1887 | if (errno == ERANGE) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1888 | LOGVAL_YANG(ctx, LY_VCODE_OOB, word_len, word, ly_stmt2str(val_kw)); |
Radek Krejci | 8b76466 | 2018-11-14 14:15:13 +0100 | [diff] [blame] | 1889 | goto error; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1890 | } |
| 1891 | if (val_kw == YANG_VALUE) { |
| 1892 | *value = num; |
| 1893 | } else { |
| 1894 | *value = unum; |
| 1895 | } |
| 1896 | free(buf); |
| 1897 | |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 1898 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1899 | switch (kw) { |
| 1900 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1901 | LY_CHECK_RET(parse_ext(ctx, data, word, word_len, val_kw == YANG_VALUE ? LYEXT_SUBSTMT_VALUE : LYEXT_SUBSTMT_POSITION, 0, exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1902 | break; |
| 1903 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1904 | 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] | 1905 | return LY_EVALID; |
| 1906 | } |
| 1907 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1908 | return ret; |
Radek Krejci | 8b76466 | 2018-11-14 14:15:13 +0100 | [diff] [blame] | 1909 | |
| 1910 | error: |
| 1911 | free(buf); |
| 1912 | return LY_EVALID; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1913 | } |
| 1914 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1915 | /** |
| 1916 | * @brief Parse the enum or bit statement. Substatement of type statement. |
| 1917 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1918 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1919 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 1920 | * @param[in] enum_kw Type of this particular keyword. |
| 1921 | * @param[in,out] enums Enums or bits to add to. |
| 1922 | * |
| 1923 | * @return LY_ERR values. |
| 1924 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1925 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1926 | 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] | 1927 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1928 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1929 | char *buf, *word; |
Radek Krejci | 8b76466 | 2018-11-14 14:15:13 +0100 | [diff] [blame] | 1930 | size_t word_len, u; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1931 | enum yang_keyword kw; |
| 1932 | struct lysp_type_enum *enm; |
| 1933 | |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 1934 | LY_ARRAY_NEW_RET(ctx->ctx, *enums, enm, LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1935 | |
| 1936 | /* get value */ |
Radek Krejci | 8b76466 | 2018-11-14 14:15:13 +0100 | [diff] [blame] | 1937 | LY_CHECK_RET(get_argument(ctx, data, enum_kw == YANG_ENUM ? Y_STR_ARG : Y_IDENTIF_ARG, &word, &buf, &word_len)); |
| 1938 | if (enum_kw == YANG_ENUM) { |
| 1939 | if (!word_len) { |
| 1940 | LOGVAL_YANG(ctx, LYVE_SYNTAX_YANG, "Enum name must not be zero-length."); |
| 1941 | free(buf); |
| 1942 | return LY_EVALID; |
| 1943 | } else if (isspace(word[0]) || isspace(word[word_len - 1])) { |
| 1944 | LOGVAL_YANG(ctx, LYVE_SYNTAX_YANG, "Enum name must not have any leading or trailing whitespaces (\"%.*s\").", |
| 1945 | word_len, word); |
| 1946 | free(buf); |
| 1947 | return LY_EVALID; |
| 1948 | } else { |
| 1949 | for (u = 0; u < word_len; ++u) { |
| 1950 | if (iscntrl(word[u])) { |
| 1951 | LOGWRN(ctx->ctx, "Control characters in enum name should be avoided (\"%.*s\", character number %d).", |
| 1952 | word_len, word, u + 1); |
| 1953 | break; |
| 1954 | } |
| 1955 | } |
| 1956 | } |
| 1957 | } else { /* YANG_BIT */ |
| 1958 | |
| 1959 | } |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1960 | INSERT_WORD(ctx, buf, enm->name, word, word_len); |
Radek Krejci | 8b76466 | 2018-11-14 14:15:13 +0100 | [diff] [blame] | 1961 | CHECK_UNIQUENESS(ctx, *enums, name, ly_stmt2str(enum_kw), enm->name); |
| 1962 | |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 1963 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1964 | switch (kw) { |
| 1965 | case YANG_DESCRIPTION: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1966 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &enm->dsc, Y_STR_ARG, &enm->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1967 | break; |
| 1968 | case YANG_IF_FEATURE: |
Radek Krejci | ceaf212 | 2019-01-02 15:03:26 +0100 | [diff] [blame] | 1969 | YANG_CHECK_STMTVER2_RET(ctx, "if-feature", ly_stmt2str(enum_kw)); |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1970 | LY_CHECK_RET(parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &enm->iffeatures, Y_STR_ARG, &enm->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1971 | break; |
| 1972 | case YANG_REFERENCE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1973 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &enm->ref, Y_STR_ARG, &enm->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1974 | break; |
| 1975 | case YANG_STATUS: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1976 | LY_CHECK_RET(parse_status(ctx, data, &enm->flags, &enm->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1977 | break; |
| 1978 | case YANG_VALUE: |
| 1979 | case YANG_POSITION: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1980 | LY_CHECK_RET(parse_type_enum_value_pos(ctx, data, kw, &enm->value, &enm->flags, &enm->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1981 | break; |
| 1982 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1983 | LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &enm->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1984 | break; |
| 1985 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1986 | 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] | 1987 | return LY_EVALID; |
| 1988 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1989 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1990 | return ret; |
| 1991 | } |
| 1992 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1993 | /** |
| 1994 | * @brief Parse the fraction-digits statement. Substatement of type statement. |
| 1995 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1996 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1997 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 1998 | * @param[in,out] fracdig Value to write to. |
| 1999 | * @param[in,out] exts Extension instances to add to. |
| 2000 | * |
| 2001 | * @return LY_ERR values. |
| 2002 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2003 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2004 | 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] | 2005 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2006 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2007 | char *buf, *word, *ptr; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 2008 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2009 | unsigned long int num; |
| 2010 | enum yang_keyword kw; |
| 2011 | |
| 2012 | if (*fracdig) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2013 | LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "fraction-digits"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2014 | return LY_EVALID; |
| 2015 | } |
| 2016 | |
| 2017 | /* get value */ |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2018 | LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2019 | |
| 2020 | if (!word_len || (word[0] == '0') || !isdigit(word[0])) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2021 | LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "fraction-digits"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2022 | free(buf); |
| 2023 | return LY_EVALID; |
| 2024 | } |
| 2025 | |
| 2026 | errno = 0; |
| 2027 | num = strtoul(word, &ptr, 10); |
| 2028 | /* we have not parsed the whole argument */ |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 2029 | if ((size_t)(ptr - word) != word_len) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2030 | LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "fraction-digits"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2031 | free(buf); |
| 2032 | return LY_EVALID; |
| 2033 | } |
| 2034 | if ((errno == ERANGE) || (num > 18)) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2035 | LOGVAL_YANG(ctx, LY_VCODE_OOB, word_len, word, "fraction-digits"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2036 | free(buf); |
| 2037 | return LY_EVALID; |
| 2038 | } |
| 2039 | *fracdig = num; |
| 2040 | free(buf); |
| 2041 | |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 2042 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2043 | switch (kw) { |
| 2044 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2045 | LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_FRACDIGITS, 0, exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2046 | break; |
| 2047 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2048 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "fraction-digits"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2049 | return LY_EVALID; |
| 2050 | } |
| 2051 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2052 | return ret; |
| 2053 | } |
| 2054 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2055 | /** |
| 2056 | * @brief Parse the require-instance statement. Substatement of type statement. |
| 2057 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2058 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2059 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 2060 | * @param[in,out] reqinst Value to write to. |
| 2061 | * @param[in,out] flags Flags to write to. |
| 2062 | * @param[in,out] exts Extension instances to add to. |
| 2063 | * |
| 2064 | * @return LY_ERR values. |
| 2065 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2066 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2067 | 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] | 2068 | struct lysp_ext_instance **exts) |
| 2069 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2070 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2071 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 2072 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2073 | enum yang_keyword kw; |
| 2074 | |
| 2075 | if (*flags & LYS_SET_REQINST) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2076 | LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "require-instance"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2077 | return LY_EVALID; |
| 2078 | } |
| 2079 | *flags |= LYS_SET_REQINST; |
| 2080 | |
| 2081 | /* get value */ |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2082 | LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2083 | |
| 2084 | if ((word_len == 4) && !strncmp(word, "true", word_len)) { |
| 2085 | *reqinst = 1; |
| 2086 | } else if ((word_len != 5) || strncmp(word, "false", word_len)) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2087 | LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "require-instance"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2088 | free(buf); |
| 2089 | return LY_EVALID; |
| 2090 | } |
| 2091 | free(buf); |
| 2092 | |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 2093 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2094 | switch (kw) { |
| 2095 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2096 | LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_REQINSTANCE, 0, exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2097 | break; |
| 2098 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2099 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "require-instance"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2100 | return LY_EVALID; |
| 2101 | } |
| 2102 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2103 | return ret; |
| 2104 | } |
| 2105 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2106 | /** |
| 2107 | * @brief Parse the modifier statement. Substatement of type pattern statement. |
| 2108 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2109 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2110 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 2111 | * @param[in,out] pat Value to write to. |
| 2112 | * @param[in,out] exts Extension instances to add to. |
| 2113 | * |
| 2114 | * @return LY_ERR values. |
| 2115 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2116 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2117 | 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] | 2118 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2119 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2120 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 2121 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2122 | enum yang_keyword kw; |
| 2123 | |
| 2124 | if ((*pat)[0] == 0x15) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2125 | LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "modifier"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2126 | return LY_EVALID; |
| 2127 | } |
| 2128 | |
| 2129 | /* get value */ |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2130 | LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2131 | |
| 2132 | if ((word_len != 12) || strncmp(word, "invert-match", word_len)) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2133 | LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "modifier"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2134 | free(buf); |
| 2135 | return LY_EVALID; |
| 2136 | } |
| 2137 | free(buf); |
| 2138 | |
| 2139 | /* replace the value in the dictionary */ |
| 2140 | buf = malloc(strlen(*pat) + 1); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2141 | LY_CHECK_ERR_RET(!buf, LOGMEM(ctx->ctx), LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2142 | strcpy(buf, *pat); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2143 | lydict_remove(ctx->ctx, *pat); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2144 | |
| 2145 | assert(buf[0] == 0x06); |
| 2146 | buf[0] = 0x15; |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2147 | *pat = lydict_insert_zc(ctx->ctx, buf); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2148 | |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 2149 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2150 | switch (kw) { |
| 2151 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2152 | LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_MODIFIER, 0, exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2153 | break; |
| 2154 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2155 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "modifier"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2156 | return LY_EVALID; |
| 2157 | } |
| 2158 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2159 | return ret; |
| 2160 | } |
| 2161 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2162 | /** |
| 2163 | * @brief Parse the pattern statement. Substatement of type statement. |
| 2164 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2165 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2166 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 2167 | * @param[in,out] patterns Restrictions to add to. |
| 2168 | * |
| 2169 | * @return LY_ERR values. |
| 2170 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2171 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2172 | 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] | 2173 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2174 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2175 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 2176 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2177 | enum yang_keyword kw; |
| 2178 | struct lysp_restr *restr; |
| 2179 | |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 2180 | LY_ARRAY_NEW_RET(ctx->ctx, *patterns, restr, LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2181 | |
| 2182 | /* get value */ |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2183 | LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2184 | |
| 2185 | /* add special meaning first byte */ |
| 2186 | if (buf) { |
| 2187 | buf = realloc(buf, word_len + 2); |
| 2188 | word = buf; |
| 2189 | } else { |
| 2190 | buf = malloc(word_len + 2); |
| 2191 | } |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2192 | LY_CHECK_ERR_RET(!buf, LOGMEM(ctx->ctx), LY_EMEM); |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 2193 | memmove(buf + 1, word, word_len); |
| 2194 | buf[0] = 0x06; /* pattern's default regular-match flag */ |
| 2195 | buf[word_len + 1] = '\0'; /* terminating NULL byte */ |
| 2196 | restr->arg = lydict_insert_zc(ctx->ctx, buf); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2197 | |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 2198 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2199 | switch (kw) { |
| 2200 | case YANG_DESCRIPTION: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2201 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &restr->dsc, Y_STR_ARG, &restr->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2202 | break; |
| 2203 | case YANG_REFERENCE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2204 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &restr->ref, Y_STR_ARG, &restr->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2205 | break; |
| 2206 | case YANG_ERROR_APP_TAG: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2207 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_ERRTAG, 0, &restr->eapptag, Y_STR_ARG, &restr->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2208 | break; |
| 2209 | case YANG_ERROR_MESSAGE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2210 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_ERRMSG, 0, &restr->emsg, Y_STR_ARG, &restr->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2211 | break; |
| 2212 | case YANG_MODIFIER: |
Radek Krejci | ceaf212 | 2019-01-02 15:03:26 +0100 | [diff] [blame] | 2213 | YANG_CHECK_STMTVER2_RET(ctx, "modifier", "pattern"); |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2214 | LY_CHECK_RET(parse_type_pattern_modifier(ctx, data, &restr->arg, &restr->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2215 | break; |
| 2216 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2217 | LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &restr->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2218 | break; |
| 2219 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2220 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "pattern"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2221 | return LY_EVALID; |
| 2222 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2223 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2224 | return ret; |
| 2225 | } |
| 2226 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2227 | /** |
| 2228 | * @brief Parse the type statement. |
| 2229 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2230 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2231 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 2232 | * @param[in,out] type Type to wrote to. |
| 2233 | * |
| 2234 | * @return LY_ERR values. |
| 2235 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2236 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2237 | 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] | 2238 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2239 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2240 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 2241 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2242 | enum yang_keyword kw; |
| 2243 | struct lysp_type *nest_type; |
| 2244 | |
| 2245 | if (type->name) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2246 | LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "type"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2247 | return LY_EVALID; |
| 2248 | } |
| 2249 | |
| 2250 | /* get value */ |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2251 | LY_CHECK_RET(get_argument(ctx, data, Y_PREF_IDENTIF_ARG, &word, &buf, &word_len)); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2252 | INSERT_WORD(ctx, buf, type->name, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2253 | |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 2254 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2255 | switch (kw) { |
| 2256 | case YANG_BASE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2257 | LY_CHECK_RET(parse_text_fields(ctx, data, LYEXT_SUBSTMT_BASE, &type->bases, Y_PREF_IDENTIF_ARG, &type->exts)); |
Radek Krejci | d505e3d | 2018-11-13 09:04:17 +0100 | [diff] [blame] | 2258 | type->flags |= LYS_SET_BASE; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2259 | break; |
| 2260 | case YANG_BIT: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2261 | LY_CHECK_RET(parse_type_enum(ctx, data, kw, &type->bits)); |
Radek Krejci | d505e3d | 2018-11-13 09:04:17 +0100 | [diff] [blame] | 2262 | type->flags |= LYS_SET_BIT; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2263 | break; |
| 2264 | case YANG_ENUM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2265 | LY_CHECK_RET(parse_type_enum(ctx, data, kw, &type->enums)); |
Radek Krejci | d505e3d | 2018-11-13 09:04:17 +0100 | [diff] [blame] | 2266 | type->flags |= LYS_SET_ENUM; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2267 | break; |
| 2268 | case YANG_FRACTION_DIGITS: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2269 | LY_CHECK_RET(parse_type_fracdigits(ctx, data, &type->fraction_digits, &type->exts)); |
Radek Krejci | d505e3d | 2018-11-13 09:04:17 +0100 | [diff] [blame] | 2270 | type->flags |= LYS_SET_FRDIGITS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2271 | break; |
| 2272 | case YANG_LENGTH: |
| 2273 | if (type->length) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2274 | LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2275 | return LY_EVALID; |
| 2276 | } |
| 2277 | type->length = calloc(1, sizeof *type->length); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2278 | LY_CHECK_ERR_RET(!type->length, LOGMEM(ctx->ctx), LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2279 | |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2280 | LY_CHECK_RET(parse_restr(ctx, data, kw, type->length)); |
Radek Krejci | d505e3d | 2018-11-13 09:04:17 +0100 | [diff] [blame] | 2281 | type->flags |= LYS_SET_LENGTH; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2282 | break; |
| 2283 | case YANG_PATH: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2284 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_PATH, 0, &type->path, Y_STR_ARG, &type->exts)); |
Radek Krejci | d505e3d | 2018-11-13 09:04:17 +0100 | [diff] [blame] | 2285 | type->flags |= LYS_SET_PATH; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2286 | break; |
| 2287 | case YANG_PATTERN: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2288 | LY_CHECK_RET(parse_type_pattern(ctx, data, &type->patterns)); |
Radek Krejci | d505e3d | 2018-11-13 09:04:17 +0100 | [diff] [blame] | 2289 | type->flags |= LYS_SET_PATTERN; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2290 | break; |
| 2291 | case YANG_RANGE: |
| 2292 | if (type->range) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2293 | LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2294 | return LY_EVALID; |
| 2295 | } |
| 2296 | type->range = calloc(1, sizeof *type->range); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2297 | LY_CHECK_ERR_RET(!type->range, LOGMEM(ctx->ctx), LY_EVALID); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2298 | |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2299 | LY_CHECK_RET(parse_restr(ctx, data, kw, type->range)); |
Radek Krejci | d505e3d | 2018-11-13 09:04:17 +0100 | [diff] [blame] | 2300 | type->flags |= LYS_SET_RANGE; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2301 | break; |
| 2302 | case YANG_REQUIRE_INSTANCE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2303 | LY_CHECK_RET(parse_type_reqinstance(ctx, data, &type->require_instance, &type->flags, &type->exts)); |
Radek Krejci | d505e3d | 2018-11-13 09:04:17 +0100 | [diff] [blame] | 2304 | /* LYS_SET_REQINST checked and set inside parse_type_reqinstance() */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2305 | break; |
| 2306 | case YANG_TYPE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2307 | LY_ARRAY_NEW_RET(ctx->ctx, type->types, nest_type, LY_EMEM); |
| 2308 | LY_CHECK_RET(parse_type(ctx, data, nest_type)); |
Radek Krejci | d505e3d | 2018-11-13 09:04:17 +0100 | [diff] [blame] | 2309 | type->flags |= LYS_SET_TYPE; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2310 | break; |
| 2311 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2312 | LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &type->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2313 | break; |
| 2314 | default: |
Radek Krejci | 8b76466 | 2018-11-14 14:15:13 +0100 | [diff] [blame] | 2315 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "type"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2316 | return LY_EVALID; |
| 2317 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2318 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2319 | return ret; |
| 2320 | } |
| 2321 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2322 | /** |
| 2323 | * @brief Parse the leaf statement. |
| 2324 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2325 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2326 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 2327 | * @param[in,out] siblings Siblings to add to. |
| 2328 | * |
| 2329 | * @return LY_ERR values. |
| 2330 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2331 | static LY_ERR |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2332 | parse_leaf(struct ly_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2333 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2334 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2335 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 2336 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2337 | enum yang_keyword kw; |
| 2338 | struct lysp_node *iter; |
| 2339 | struct lysp_node_leaf *leaf; |
| 2340 | |
| 2341 | /* create structure */ |
| 2342 | leaf = calloc(1, sizeof *leaf); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2343 | LY_CHECK_ERR_RET(!leaf, LOGMEM(ctx->ctx), LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2344 | leaf->nodetype = LYS_LEAF; |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2345 | leaf->parent = parent; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2346 | |
| 2347 | /* insert into siblings */ |
| 2348 | if (!*siblings) { |
| 2349 | *siblings = (struct lysp_node *)leaf; |
| 2350 | } else { |
| 2351 | for (iter = *siblings; iter->next; iter = iter->next); |
| 2352 | iter->next = (struct lysp_node *)leaf; |
| 2353 | } |
| 2354 | |
| 2355 | /* get name */ |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2356 | LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len)); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2357 | INSERT_WORD(ctx, buf, leaf->name, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2358 | |
| 2359 | /* parse substatements */ |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 2360 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2361 | switch (kw) { |
| 2362 | case YANG_CONFIG: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2363 | LY_CHECK_RET(parse_config(ctx, data, &leaf->flags, &leaf->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2364 | break; |
| 2365 | case YANG_DEFAULT: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2366 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DEFAULT, 0, &leaf->dflt, Y_STR_ARG, &leaf->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2367 | break; |
| 2368 | case YANG_DESCRIPTION: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2369 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &leaf->dsc, Y_STR_ARG, &leaf->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2370 | break; |
| 2371 | case YANG_IF_FEATURE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2372 | LY_CHECK_RET(parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &leaf->iffeatures, Y_STR_ARG, &leaf->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2373 | break; |
| 2374 | case YANG_MANDATORY: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2375 | LY_CHECK_RET(parse_mandatory(ctx, data, &leaf->flags, &leaf->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2376 | break; |
| 2377 | case YANG_MUST: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2378 | LY_CHECK_RET(parse_restrs(ctx, data, kw, &leaf->musts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2379 | break; |
| 2380 | case YANG_REFERENCE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2381 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &leaf->ref, Y_STR_ARG, &leaf->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2382 | break; |
| 2383 | case YANG_STATUS: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2384 | LY_CHECK_RET(parse_status(ctx, data, &leaf->flags, &leaf->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2385 | break; |
| 2386 | case YANG_TYPE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2387 | LY_CHECK_RET(parse_type(ctx, data, &leaf->type)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2388 | break; |
| 2389 | case YANG_UNITS: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2390 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_UNITS, 0, &leaf->units, Y_STR_ARG, &leaf->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2391 | break; |
| 2392 | case YANG_WHEN: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2393 | LY_CHECK_RET(parse_when(ctx, data, &leaf->when)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2394 | break; |
| 2395 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2396 | LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &leaf->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2397 | break; |
| 2398 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2399 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "leaf"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2400 | return LY_EVALID; |
| 2401 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2402 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 2403 | LY_CHECK_RET(ret); |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 2404 | checks: |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2405 | /* mandatory substatements */ |
| 2406 | if (!leaf->type.name) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2407 | LOGVAL_YANG(ctx, LY_VCODE_MISSTMT, "type", "leaf"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2408 | return LY_EVALID; |
| 2409 | } |
Radek Krejci | b1a5dcc | 2018-11-26 14:50:05 +0100 | [diff] [blame] | 2410 | if ((leaf->flags & LYS_MAND_TRUE) && (leaf->dflt)) { |
| 2411 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMSCOMB, "mandatory", "default", "leaf"); |
| 2412 | return LY_EVALID; |
| 2413 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2414 | |
| 2415 | return ret; |
| 2416 | } |
| 2417 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2418 | /** |
| 2419 | * @brief Parse the max-elements statement. |
| 2420 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2421 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2422 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 2423 | * @param[in,out] max Value to write to. |
| 2424 | * @param[in,out] flags Flags to write to. |
| 2425 | * @param[in,out] exts Extension instances to add to. |
| 2426 | * |
| 2427 | * @return LY_ERR values. |
| 2428 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2429 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2430 | 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] | 2431 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2432 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2433 | char *buf, *word, *ptr; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 2434 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2435 | unsigned long int num; |
| 2436 | enum yang_keyword kw; |
| 2437 | |
| 2438 | if (*flags & LYS_SET_MAX) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2439 | LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "max-elements"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2440 | return LY_EVALID; |
| 2441 | } |
| 2442 | *flags |= LYS_SET_MAX; |
| 2443 | |
| 2444 | /* get value */ |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2445 | LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2446 | |
| 2447 | if (!word_len || (word[0] == '0') || ((word[0] != 'u') && !isdigit(word[0]))) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2448 | LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "max-elements"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2449 | free(buf); |
| 2450 | return LY_EVALID; |
| 2451 | } |
| 2452 | |
| 2453 | if (strncmp(word, "unbounded", word_len)) { |
| 2454 | errno = 0; |
| 2455 | num = strtoul(word, &ptr, 10); |
| 2456 | /* we have not parsed the whole argument */ |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 2457 | if ((size_t)(ptr - word) != word_len) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2458 | LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "max-elements"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2459 | free(buf); |
| 2460 | return LY_EVALID; |
| 2461 | } |
| 2462 | if ((errno == ERANGE) || (num > UINT32_MAX)) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2463 | LOGVAL_YANG(ctx, LY_VCODE_OOB, word_len, word, "max-elements"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2464 | free(buf); |
| 2465 | return LY_EVALID; |
| 2466 | } |
| 2467 | |
| 2468 | *max = num; |
| 2469 | } |
| 2470 | free(buf); |
| 2471 | |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 2472 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2473 | switch (kw) { |
| 2474 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2475 | LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_MAX, 0, exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2476 | break; |
| 2477 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2478 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "max-elements"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2479 | return LY_EVALID; |
| 2480 | } |
| 2481 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2482 | return ret; |
| 2483 | } |
| 2484 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2485 | /** |
| 2486 | * @brief Parse the min-elements statement. |
| 2487 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2488 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2489 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 2490 | * @param[in,out] min Value to write to. |
| 2491 | * @param[in,out] flags Flags to write to. |
| 2492 | * @param[in,out] exts Extension instances to add to. |
| 2493 | * |
| 2494 | * @return LY_ERR values. |
| 2495 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2496 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2497 | 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] | 2498 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2499 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2500 | char *buf, *word, *ptr; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 2501 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2502 | unsigned long int num; |
| 2503 | enum yang_keyword kw; |
| 2504 | |
| 2505 | if (*flags & LYS_SET_MIN) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2506 | LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "min-elements"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2507 | return LY_EVALID; |
| 2508 | } |
| 2509 | *flags |= LYS_SET_MIN; |
| 2510 | |
| 2511 | /* get value */ |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2512 | LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2513 | |
| 2514 | if (!word_len || !isdigit(word[0]) || ((word[0] == '0') && (word_len > 1))) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2515 | LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "min-elements"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2516 | free(buf); |
| 2517 | return LY_EVALID; |
| 2518 | } |
| 2519 | |
| 2520 | errno = 0; |
| 2521 | num = strtoul(word, &ptr, 10); |
| 2522 | /* we have not parsed the whole argument */ |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 2523 | if ((size_t)(ptr - word) != word_len) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2524 | LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "min-elements"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2525 | free(buf); |
| 2526 | return LY_EVALID; |
| 2527 | } |
| 2528 | if ((errno == ERANGE) || (num > UINT32_MAX)) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2529 | LOGVAL_YANG(ctx, LY_VCODE_OOB, word_len, word, "min-elements"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2530 | free(buf); |
| 2531 | return LY_EVALID; |
| 2532 | } |
| 2533 | *min = num; |
| 2534 | free(buf); |
| 2535 | |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 2536 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2537 | switch (kw) { |
| 2538 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2539 | LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_MIN, 0, exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2540 | break; |
| 2541 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2542 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "min-elements"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2543 | return LY_EVALID; |
| 2544 | } |
| 2545 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2546 | return ret; |
| 2547 | } |
| 2548 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2549 | /** |
| 2550 | * @brief Parse the ordered-by statement. |
| 2551 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2552 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2553 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 2554 | * @param[in,out] flags Flags to write to. |
| 2555 | * @param[in,out] exts Extension instances to add to. |
| 2556 | * |
| 2557 | * @return LY_ERR values. |
| 2558 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2559 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2560 | 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] | 2561 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2562 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2563 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 2564 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2565 | enum yang_keyword kw; |
| 2566 | |
| 2567 | if (*flags & LYS_ORDBY_MASK) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2568 | LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "ordered-by"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2569 | return LY_EVALID; |
| 2570 | } |
| 2571 | |
| 2572 | /* get value */ |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2573 | LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2574 | |
| 2575 | if ((word_len == 6) && !strncmp(word, "system", word_len)) { |
| 2576 | *flags |= LYS_ORDBY_SYSTEM; |
| 2577 | } else if ((word_len == 4) && !strncmp(word, "user", word_len)) { |
| 2578 | *flags |= LYS_ORDBY_USER; |
| 2579 | } else { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2580 | LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "ordered-by"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2581 | free(buf); |
| 2582 | return LY_EVALID; |
| 2583 | } |
| 2584 | free(buf); |
| 2585 | |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 2586 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2587 | switch (kw) { |
| 2588 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2589 | LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_ORDEREDBY, 0, exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2590 | break; |
| 2591 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2592 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "ordered-by"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2593 | return LY_EVALID; |
| 2594 | } |
| 2595 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2596 | return ret; |
| 2597 | } |
| 2598 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2599 | /** |
| 2600 | * @brief Parse the leaf-list statement. |
| 2601 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2602 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2603 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 2604 | * @param[in,out] siblings Siblings to add to. |
| 2605 | * |
| 2606 | * @return LY_ERR values. |
| 2607 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2608 | static LY_ERR |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2609 | parse_leaflist(struct ly_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2610 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2611 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2612 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 2613 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2614 | enum yang_keyword kw; |
| 2615 | struct lysp_node *iter; |
| 2616 | struct lysp_node_leaflist *llist; |
| 2617 | |
| 2618 | /* create structure */ |
| 2619 | llist = calloc(1, sizeof *llist); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2620 | LY_CHECK_ERR_RET(!llist, LOGMEM(ctx->ctx), LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2621 | llist->nodetype = LYS_LEAFLIST; |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2622 | llist->parent = parent; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2623 | |
| 2624 | /* insert into siblings */ |
| 2625 | if (!*siblings) { |
| 2626 | *siblings = (struct lysp_node *)llist; |
| 2627 | } else { |
| 2628 | for (iter = *siblings; iter->next; iter = iter->next); |
| 2629 | iter->next = (struct lysp_node *)llist; |
| 2630 | } |
| 2631 | |
| 2632 | /* get name */ |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2633 | LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len)); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2634 | INSERT_WORD(ctx, buf, llist->name, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2635 | |
| 2636 | /* parse substatements */ |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 2637 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2638 | switch (kw) { |
| 2639 | case YANG_CONFIG: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2640 | LY_CHECK_RET(parse_config(ctx, data, &llist->flags, &llist->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2641 | break; |
| 2642 | case YANG_DEFAULT: |
Radek Krejci | ceaf212 | 2019-01-02 15:03:26 +0100 | [diff] [blame] | 2643 | YANG_CHECK_STMTVER2_RET(ctx, "default", "leaf-list"); |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2644 | LY_CHECK_RET(parse_text_fields(ctx, data, LYEXT_SUBSTMT_DEFAULT, &llist->dflts, Y_STR_ARG, &llist->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2645 | break; |
| 2646 | case YANG_DESCRIPTION: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2647 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &llist->dsc, Y_STR_ARG, &llist->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2648 | break; |
| 2649 | case YANG_IF_FEATURE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2650 | LY_CHECK_RET(parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &llist->iffeatures, Y_STR_ARG, &llist->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2651 | break; |
| 2652 | case YANG_MAX_ELEMENTS: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2653 | LY_CHECK_RET(parse_maxelements(ctx, data, &llist->max, &llist->flags, &llist->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2654 | break; |
| 2655 | case YANG_MIN_ELEMENTS: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2656 | LY_CHECK_RET(parse_minelements(ctx, data, &llist->min, &llist->flags, &llist->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2657 | break; |
| 2658 | case YANG_MUST: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2659 | LY_CHECK_RET(parse_restrs(ctx, data, kw, &llist->musts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2660 | break; |
| 2661 | case YANG_ORDERED_BY: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2662 | LY_CHECK_RET(parse_orderedby(ctx, data, &llist->flags, &llist->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2663 | break; |
| 2664 | case YANG_REFERENCE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2665 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &llist->ref, Y_STR_ARG, &llist->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2666 | break; |
| 2667 | case YANG_STATUS: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2668 | LY_CHECK_RET(parse_status(ctx, data, &llist->flags, &llist->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2669 | break; |
| 2670 | case YANG_TYPE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2671 | LY_CHECK_RET(parse_type(ctx, data, &llist->type)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2672 | break; |
| 2673 | case YANG_UNITS: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2674 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_UNITS, 0, &llist->units, Y_STR_ARG, &llist->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2675 | break; |
| 2676 | case YANG_WHEN: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2677 | LY_CHECK_RET(parse_when(ctx, data, &llist->when)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2678 | break; |
| 2679 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2680 | LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &llist->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2681 | break; |
| 2682 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2683 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "llist"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2684 | return LY_EVALID; |
| 2685 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2686 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 2687 | LY_CHECK_RET(ret); |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 2688 | checks: |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2689 | /* mandatory substatements */ |
| 2690 | if (!llist->type.name) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2691 | LOGVAL_YANG(ctx, LY_VCODE_MISSTMT, "type", "leaf-list"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2692 | return LY_EVALID; |
| 2693 | } |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 2694 | if ((llist->min) && (llist->dflts)) { |
| 2695 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMSCOMB, "min-elements", "default", "leaf-list"); |
| 2696 | return LY_EVALID; |
| 2697 | } |
Radek Krejci | df6cad1 | 2018-11-28 17:10:55 +0100 | [diff] [blame] | 2698 | if (llist->max && llist->min > llist->max) { |
| 2699 | LOGVAL_YANG(ctx, LYVE_SEMANTICS, |
| 2700 | "Invalid combination of min-elements and max-elements: min value %u is bigger than the max value %u.", |
| 2701 | llist->min, llist->max); |
| 2702 | return LY_EVALID; |
| 2703 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2704 | |
| 2705 | return ret; |
| 2706 | } |
| 2707 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2708 | /** |
| 2709 | * @brief Parse the refine statement. |
| 2710 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2711 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2712 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 2713 | * @param[in,out] refines Refines to add to. |
| 2714 | * |
| 2715 | * @return LY_ERR values. |
| 2716 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2717 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2718 | 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] | 2719 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2720 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2721 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 2722 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2723 | enum yang_keyword kw; |
| 2724 | struct lysp_refine *rf; |
| 2725 | |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 2726 | LY_ARRAY_NEW_RET(ctx->ctx, *refines, rf, LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2727 | |
| 2728 | /* get value */ |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2729 | LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len)); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2730 | INSERT_WORD(ctx, buf, rf->nodeid, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2731 | |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 2732 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2733 | switch (kw) { |
| 2734 | case YANG_CONFIG: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2735 | LY_CHECK_RET(parse_config(ctx, data, &rf->flags, &rf->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2736 | break; |
| 2737 | case YANG_DEFAULT: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2738 | LY_CHECK_RET(parse_text_fields(ctx, data, LYEXT_SUBSTMT_DEFAULT, &rf->dflts, Y_STR_ARG, &rf->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2739 | break; |
| 2740 | case YANG_DESCRIPTION: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2741 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &rf->dsc, Y_STR_ARG, &rf->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2742 | break; |
| 2743 | case YANG_IF_FEATURE: |
Radek Krejci | ceaf212 | 2019-01-02 15:03:26 +0100 | [diff] [blame] | 2744 | YANG_CHECK_STMTVER2_RET(ctx, "if-feature", "refine"); |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2745 | LY_CHECK_RET(parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &rf->iffeatures, Y_STR_ARG, &rf->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2746 | break; |
| 2747 | case YANG_MAX_ELEMENTS: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2748 | LY_CHECK_RET(parse_maxelements(ctx, data, &rf->max, &rf->flags, &rf->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2749 | break; |
| 2750 | case YANG_MIN_ELEMENTS: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2751 | LY_CHECK_RET(parse_minelements(ctx, data, &rf->min, &rf->flags, &rf->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2752 | break; |
| 2753 | case YANG_MUST: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2754 | LY_CHECK_RET(parse_restrs(ctx, data, kw, &rf->musts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2755 | break; |
| 2756 | case YANG_MANDATORY: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2757 | LY_CHECK_RET(parse_mandatory(ctx, data, &rf->flags, &rf->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2758 | break; |
| 2759 | case YANG_REFERENCE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2760 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &rf->ref, Y_STR_ARG, &rf->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2761 | break; |
| 2762 | case YANG_PRESENCE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2763 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_PRESENCE, 0, &rf->presence, Y_STR_ARG, &rf->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2764 | break; |
| 2765 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2766 | LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &rf->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2767 | break; |
| 2768 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2769 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "refine"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2770 | return LY_EVALID; |
| 2771 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2772 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2773 | return ret; |
| 2774 | } |
| 2775 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2776 | /** |
| 2777 | * @brief Parse the typedef statement. |
| 2778 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2779 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2780 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 2781 | * @param[in,out] typedefs Typedefs to add to. |
| 2782 | * |
| 2783 | * @return LY_ERR values. |
| 2784 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2785 | static LY_ERR |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 2786 | parse_typedef(struct ly_parser_ctx *ctx, struct lysp_node *parent, const char **data, struct lysp_tpdf **typedefs) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2787 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2788 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2789 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 2790 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2791 | enum yang_keyword kw; |
| 2792 | struct lysp_tpdf *tpdf; |
| 2793 | |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 2794 | LY_ARRAY_NEW_RET(ctx->ctx, *typedefs, tpdf, LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2795 | |
| 2796 | /* get value */ |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2797 | LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len)); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2798 | INSERT_WORD(ctx, buf, tpdf->name, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2799 | |
| 2800 | /* parse substatements */ |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 2801 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2802 | switch (kw) { |
| 2803 | case YANG_DEFAULT: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2804 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DEFAULT, 0, &tpdf->dflt, Y_STR_ARG, &tpdf->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2805 | break; |
| 2806 | case YANG_DESCRIPTION: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2807 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &tpdf->dsc, Y_STR_ARG, &tpdf->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2808 | break; |
| 2809 | case YANG_REFERENCE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2810 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &tpdf->ref, Y_STR_ARG, &tpdf->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2811 | break; |
| 2812 | case YANG_STATUS: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2813 | LY_CHECK_RET(parse_status(ctx, data, &tpdf->flags, &tpdf->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2814 | break; |
| 2815 | case YANG_TYPE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2816 | LY_CHECK_RET(parse_type(ctx, data, &tpdf->type)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2817 | break; |
| 2818 | case YANG_UNITS: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2819 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_UNITS, 0, &tpdf->units, Y_STR_ARG, &tpdf->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2820 | break; |
| 2821 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2822 | LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &tpdf->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2823 | break; |
| 2824 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2825 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "typedef"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2826 | return LY_EVALID; |
| 2827 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2828 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 2829 | LY_CHECK_RET(ret); |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 2830 | checks: |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2831 | /* mandatory substatements */ |
| 2832 | if (!tpdf->type.name) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2833 | LOGVAL_YANG(ctx, LY_VCODE_MISSTMT, "type", "typedef"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2834 | return LY_EVALID; |
| 2835 | } |
| 2836 | |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 2837 | /* store data for collision check */ |
| 2838 | if (parent) { |
| 2839 | ly_set_add(&ctx->tpdfs_nodes, parent, 0); |
| 2840 | } |
| 2841 | |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2842 | return ret; |
| 2843 | } |
| 2844 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2845 | /** |
| 2846 | * @brief Parse the input or output statement. |
| 2847 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2848 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2849 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 2850 | * @param[in] kw Type of this particular keyword |
| 2851 | * @param[in,out] inout_p Input/output pointer to write to. |
| 2852 | * |
| 2853 | * @return LY_ERR values. |
| 2854 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2855 | static LY_ERR |
Radek Krejci | 1011365 | 2018-11-14 16:56:50 +0100 | [diff] [blame] | 2856 | parse_inout(struct ly_parser_ctx *ctx, const char **data, enum yang_keyword inout_kw, struct lysp_node *parent, struct lysp_action_inout **inout_p) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2857 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2858 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2859 | char *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 2860 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2861 | struct lysp_action_inout *inout; |
Radek Krejci | 1011365 | 2018-11-14 16:56:50 +0100 | [diff] [blame] | 2862 | enum yang_keyword kw; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 2863 | unsigned int u; |
| 2864 | struct lysp_node *child; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2865 | |
| 2866 | if (*inout_p) { |
Radek Krejci | 1011365 | 2018-11-14 16:56:50 +0100 | [diff] [blame] | 2867 | LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(inout_kw)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2868 | return LY_EVALID; |
| 2869 | } |
| 2870 | |
| 2871 | /* create structure */ |
| 2872 | inout = calloc(1, sizeof *inout); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2873 | LY_CHECK_ERR_RET(!inout, LOGMEM(ctx->ctx), LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2874 | *inout_p = inout; |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2875 | inout->nodetype = LYS_INOUT; |
| 2876 | inout->parent = parent; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2877 | |
| 2878 | /* parse substatements */ |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 2879 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2880 | switch (kw) { |
| 2881 | case YANG_ANYDATA: |
Radek Krejci | ceaf212 | 2019-01-02 15:03:26 +0100 | [diff] [blame] | 2882 | YANG_CHECK_STMTVER2_RET(ctx, "anydata", ly_stmt2str(inout_kw)); |
Radek Krejci | 1011365 | 2018-11-14 16:56:50 +0100 | [diff] [blame] | 2883 | /* fall through */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2884 | case YANG_ANYXML: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2885 | LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)inout, &inout->data)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2886 | break; |
| 2887 | case YANG_CHOICE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2888 | LY_CHECK_RET(parse_choice(ctx, data, (struct lysp_node*)inout, &inout->data)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2889 | break; |
| 2890 | case YANG_CONTAINER: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2891 | LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)inout, &inout->data)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2892 | break; |
| 2893 | case YANG_LEAF: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2894 | LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)inout, &inout->data)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2895 | break; |
| 2896 | case YANG_LEAF_LIST: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2897 | LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)inout, &inout->data)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2898 | break; |
| 2899 | case YANG_LIST: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2900 | LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)inout, &inout->data)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2901 | break; |
| 2902 | case YANG_USES: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2903 | LY_CHECK_RET(parse_uses(ctx, data, (struct lysp_node*)inout, &inout->data)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2904 | break; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2905 | case YANG_TYPEDEF: |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 2906 | LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node*)inout, data, &inout->typedefs)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2907 | break; |
| 2908 | case YANG_MUST: |
Radek Krejci | ceaf212 | 2019-01-02 15:03:26 +0100 | [diff] [blame] | 2909 | YANG_CHECK_STMTVER2_RET(ctx, "must", ly_stmt2str(inout_kw)); |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2910 | LY_CHECK_RET(parse_restrs(ctx, data, kw, &inout->musts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2911 | break; |
| 2912 | case YANG_GROUPING: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2913 | LY_CHECK_RET(parse_grouping(ctx, data, (struct lysp_node*)inout, &inout->groupings)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2914 | break; |
| 2915 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2916 | LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &inout->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2917 | break; |
| 2918 | default: |
Radek Krejci | 1011365 | 2018-11-14 16:56:50 +0100 | [diff] [blame] | 2919 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(inout_kw)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2920 | return LY_EVALID; |
| 2921 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2922 | } |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 2923 | /* finalize parent pointers to the reallocated items */ |
| 2924 | LY_ARRAY_FOR(inout->groupings, u) { |
| 2925 | LY_LIST_FOR(inout->groupings[u].data, child) { |
| 2926 | child->parent = (struct lysp_node*)&inout->groupings[u]; |
| 2927 | } |
| 2928 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2929 | return ret; |
| 2930 | } |
| 2931 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2932 | /** |
| 2933 | * @brief Parse the action statement. |
| 2934 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2935 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2936 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 2937 | * @param[in,out] actions Actions to add to. |
| 2938 | * |
| 2939 | * @return LY_ERR values. |
| 2940 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2941 | static LY_ERR |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2942 | parse_action(struct ly_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_action **actions) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2943 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2944 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2945 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 2946 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2947 | enum yang_keyword kw; |
| 2948 | struct lysp_action *act; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 2949 | struct lysp_node *child; |
| 2950 | unsigned int u; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2951 | |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 2952 | LY_ARRAY_NEW_RET(ctx->ctx, *actions, act, LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2953 | |
| 2954 | /* get value */ |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2955 | LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len)); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2956 | INSERT_WORD(ctx, buf, act->name, word, word_len); |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2957 | act->nodetype = LYS_ACTION; |
| 2958 | act->parent = parent; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2959 | |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 2960 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2961 | switch (kw) { |
| 2962 | case YANG_DESCRIPTION: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2963 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &act->dsc, Y_STR_ARG, &act->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2964 | break; |
| 2965 | case YANG_IF_FEATURE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2966 | LY_CHECK_RET(parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &act->iffeatures, Y_STR_ARG, &act->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2967 | break; |
| 2968 | case YANG_REFERENCE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2969 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &act->ref, Y_STR_ARG, &act->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2970 | break; |
| 2971 | case YANG_STATUS: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2972 | LY_CHECK_RET(parse_status(ctx, data, &act->flags, &act->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2973 | break; |
| 2974 | |
| 2975 | case YANG_INPUT: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2976 | LY_CHECK_RET(parse_inout(ctx, data, kw, (struct lysp_node*)act, &act->input)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2977 | break; |
| 2978 | case YANG_OUTPUT: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2979 | LY_CHECK_RET(parse_inout(ctx, data, kw, (struct lysp_node*)act, &act->output)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2980 | break; |
| 2981 | |
| 2982 | case YANG_TYPEDEF: |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 2983 | LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node*)act, data, &act->typedefs)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2984 | break; |
| 2985 | case YANG_GROUPING: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2986 | LY_CHECK_RET(parse_grouping(ctx, data, (struct lysp_node*)act, &act->groupings)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2987 | break; |
| 2988 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2989 | LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &act->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2990 | break; |
| 2991 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2992 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "action"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2993 | return LY_EVALID; |
| 2994 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2995 | } |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 2996 | /* finalize parent pointers to the reallocated items */ |
| 2997 | LY_ARRAY_FOR(act->groupings, u) { |
| 2998 | LY_LIST_FOR(act->groupings[u].data, child) { |
| 2999 | child->parent = (struct lysp_node*)&act->groupings[u]; |
| 3000 | } |
| 3001 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3002 | return ret; |
| 3003 | } |
| 3004 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3005 | /** |
| 3006 | * @brief Parse the notification statement. |
| 3007 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3008 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3009 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 3010 | * @param[in,out] notifs Notifications to add to. |
| 3011 | * |
| 3012 | * @return LY_ERR values. |
| 3013 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3014 | static LY_ERR |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3015 | parse_notif(struct ly_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_notif **notifs) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3016 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3017 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3018 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 3019 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3020 | enum yang_keyword kw; |
| 3021 | struct lysp_notif *notif; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 3022 | struct lysp_node *child; |
| 3023 | unsigned int u; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3024 | |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 3025 | LY_ARRAY_NEW_RET(ctx->ctx, *notifs, notif, LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3026 | |
| 3027 | /* get value */ |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3028 | LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len)); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3029 | INSERT_WORD(ctx, buf, notif->name, word, word_len); |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3030 | notif->nodetype = LYS_NOTIF; |
| 3031 | notif->parent = parent; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3032 | |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 3033 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3034 | switch (kw) { |
| 3035 | case YANG_DESCRIPTION: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3036 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, ¬if->dsc, Y_STR_ARG, ¬if->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3037 | break; |
| 3038 | case YANG_IF_FEATURE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3039 | LY_CHECK_RET(parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, ¬if->iffeatures, Y_STR_ARG, ¬if->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3040 | break; |
| 3041 | case YANG_REFERENCE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3042 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, ¬if->ref, Y_STR_ARG, ¬if->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3043 | break; |
| 3044 | case YANG_STATUS: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3045 | LY_CHECK_RET(parse_status(ctx, data, ¬if->flags, ¬if->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3046 | break; |
| 3047 | |
| 3048 | case YANG_ANYDATA: |
Radek Krejci | ceaf212 | 2019-01-02 15:03:26 +0100 | [diff] [blame] | 3049 | YANG_CHECK_STMTVER2_RET(ctx, "anydata", "notification"); |
Radek Krejci | 1011365 | 2018-11-14 16:56:50 +0100 | [diff] [blame] | 3050 | /* fall through */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3051 | case YANG_ANYXML: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3052 | LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)notif, ¬if->data)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3053 | break; |
| 3054 | case YANG_CHOICE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3055 | LY_CHECK_RET(parse_case(ctx, data, (struct lysp_node*)notif, ¬if->data)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3056 | break; |
| 3057 | case YANG_CONTAINER: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3058 | LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)notif, ¬if->data)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3059 | break; |
| 3060 | case YANG_LEAF: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3061 | LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)notif, ¬if->data)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3062 | break; |
| 3063 | case YANG_LEAF_LIST: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3064 | LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)notif, ¬if->data)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3065 | break; |
| 3066 | case YANG_LIST: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3067 | LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)notif, ¬if->data)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3068 | break; |
| 3069 | case YANG_USES: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3070 | LY_CHECK_RET(parse_uses(ctx, data, (struct lysp_node*)notif, ¬if->data)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3071 | break; |
| 3072 | |
| 3073 | case YANG_MUST: |
Radek Krejci | ceaf212 | 2019-01-02 15:03:26 +0100 | [diff] [blame] | 3074 | YANG_CHECK_STMTVER2_RET(ctx, "must", "notification"); |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3075 | LY_CHECK_RET(parse_restrs(ctx, data, kw, ¬if->musts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3076 | break; |
| 3077 | case YANG_TYPEDEF: |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 3078 | LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node*)notif, data, ¬if->typedefs)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3079 | break; |
| 3080 | case YANG_GROUPING: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3081 | LY_CHECK_RET(parse_grouping(ctx, data, (struct lysp_node*)notif, ¬if->groupings)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3082 | break; |
| 3083 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3084 | LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, ¬if->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3085 | break; |
| 3086 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3087 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "notification"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3088 | return LY_EVALID; |
| 3089 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3090 | } |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 3091 | /* finalize parent pointers to the reallocated items */ |
| 3092 | LY_ARRAY_FOR(notif->groupings, u) { |
| 3093 | LY_LIST_FOR(notif->groupings[u].data, child) { |
| 3094 | child->parent = (struct lysp_node*)¬if->groupings[u]; |
| 3095 | } |
| 3096 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3097 | return ret; |
| 3098 | } |
| 3099 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3100 | /** |
| 3101 | * @brief Parse the grouping statement. |
| 3102 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3103 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3104 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 3105 | * @param[in,out] groupings Groupings to add to. |
| 3106 | * |
| 3107 | * @return LY_ERR values. |
| 3108 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3109 | static LY_ERR |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3110 | parse_grouping(struct ly_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_grp **groupings) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3111 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3112 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3113 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 3114 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3115 | enum yang_keyword kw; |
| 3116 | struct lysp_grp *grp; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 3117 | struct lysp_node *child; |
| 3118 | unsigned int u; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3119 | |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 3120 | LY_ARRAY_NEW_RET(ctx->ctx, *groupings, grp, LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3121 | |
| 3122 | /* get value */ |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3123 | LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len)); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3124 | INSERT_WORD(ctx, buf, grp->name, word, word_len); |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3125 | grp->nodetype = LYS_GROUPING; |
| 3126 | grp->parent = parent; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3127 | |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 3128 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3129 | switch (kw) { |
| 3130 | case YANG_DESCRIPTION: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3131 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &grp->dsc, Y_STR_ARG, &grp->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3132 | break; |
| 3133 | case YANG_REFERENCE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3134 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &grp->ref, Y_STR_ARG, &grp->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3135 | break; |
| 3136 | case YANG_STATUS: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3137 | LY_CHECK_RET(parse_status(ctx, data, &grp->flags, &grp->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3138 | break; |
| 3139 | |
| 3140 | case YANG_ANYDATA: |
Radek Krejci | ceaf212 | 2019-01-02 15:03:26 +0100 | [diff] [blame] | 3141 | YANG_CHECK_STMTVER2_RET(ctx, "anydata", "grouping"); |
Radek Krejci | 1011365 | 2018-11-14 16:56:50 +0100 | [diff] [blame] | 3142 | /* fall through */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3143 | case YANG_ANYXML: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3144 | LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)grp, &grp->data)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3145 | break; |
| 3146 | case YANG_CHOICE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3147 | LY_CHECK_RET(parse_choice(ctx, data, (struct lysp_node*)grp, &grp->data)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3148 | break; |
| 3149 | case YANG_CONTAINER: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3150 | LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)grp, &grp->data)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3151 | break; |
| 3152 | case YANG_LEAF: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3153 | LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)grp, &grp->data)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3154 | break; |
| 3155 | case YANG_LEAF_LIST: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3156 | LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)grp, &grp->data)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3157 | break; |
| 3158 | case YANG_LIST: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3159 | LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)grp, &grp->data)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3160 | break; |
| 3161 | case YANG_USES: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3162 | LY_CHECK_RET(parse_uses(ctx, data, (struct lysp_node*)grp, &grp->data)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3163 | break; |
| 3164 | |
| 3165 | case YANG_TYPEDEF: |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 3166 | LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node*)grp, data, &grp->typedefs)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3167 | break; |
| 3168 | case YANG_ACTION: |
Radek Krejci | ceaf212 | 2019-01-02 15:03:26 +0100 | [diff] [blame] | 3169 | YANG_CHECK_STMTVER2_RET(ctx, "action", "grouping"); |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3170 | LY_CHECK_RET(parse_action(ctx, data, (struct lysp_node*)grp, &grp->actions)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3171 | break; |
| 3172 | case YANG_GROUPING: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3173 | LY_CHECK_RET(parse_grouping(ctx, data, (struct lysp_node*)grp, &grp->groupings)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3174 | break; |
| 3175 | case YANG_NOTIFICATION: |
Radek Krejci | ceaf212 | 2019-01-02 15:03:26 +0100 | [diff] [blame] | 3176 | YANG_CHECK_STMTVER2_RET(ctx, "notification", "grouping"); |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3177 | LY_CHECK_RET(parse_notif(ctx, data, (struct lysp_node*)grp, &grp->notifs)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3178 | break; |
| 3179 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3180 | LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &grp->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3181 | break; |
| 3182 | default: |
Radek Krejci | 1011365 | 2018-11-14 16:56:50 +0100 | [diff] [blame] | 3183 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "grouping"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3184 | return LY_EVALID; |
| 3185 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3186 | } |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 3187 | /* finalize parent pointers to the reallocated items */ |
| 3188 | LY_ARRAY_FOR(grp->groupings, u) { |
| 3189 | LY_LIST_FOR(grp->groupings[u].data, child) { |
| 3190 | child->parent = (struct lysp_node*)&grp->groupings[u]; |
| 3191 | } |
| 3192 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3193 | return ret; |
| 3194 | } |
| 3195 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3196 | /** |
| 3197 | * @brief Parse the refine statement. |
| 3198 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3199 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3200 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 3201 | * @param[in,out] augments Augments to add to. |
| 3202 | * |
| 3203 | * @return LY_ERR values. |
| 3204 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3205 | static LY_ERR |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3206 | parse_augment(struct ly_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_augment **augments) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3207 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3208 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3209 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 3210 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3211 | enum yang_keyword kw; |
| 3212 | struct lysp_augment *aug; |
| 3213 | |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 3214 | LY_ARRAY_NEW_RET(ctx->ctx, *augments, aug, LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3215 | |
| 3216 | /* get value */ |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3217 | LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len)); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3218 | INSERT_WORD(ctx, buf, aug->nodeid, word, word_len); |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3219 | aug->nodetype = LYS_AUGMENT; |
| 3220 | aug->parent = parent; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3221 | |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 3222 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3223 | switch (kw) { |
| 3224 | case YANG_DESCRIPTION: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3225 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &aug->dsc, Y_STR_ARG, &aug->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3226 | break; |
| 3227 | case YANG_IF_FEATURE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3228 | LY_CHECK_RET(parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &aug->iffeatures, Y_STR_ARG, &aug->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3229 | break; |
| 3230 | case YANG_REFERENCE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3231 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &aug->ref, Y_STR_ARG, &aug->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3232 | break; |
| 3233 | case YANG_STATUS: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3234 | LY_CHECK_RET(parse_status(ctx, data, &aug->flags, &aug->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3235 | break; |
| 3236 | case YANG_WHEN: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3237 | LY_CHECK_RET(parse_when(ctx, data, &aug->when)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3238 | break; |
| 3239 | |
| 3240 | case YANG_ANYDATA: |
Radek Krejci | ceaf212 | 2019-01-02 15:03:26 +0100 | [diff] [blame] | 3241 | YANG_CHECK_STMTVER2_RET(ctx, "anydata", "augment"); |
Radek Krejci | 1011365 | 2018-11-14 16:56:50 +0100 | [diff] [blame] | 3242 | /* fall through */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3243 | case YANG_ANYXML: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3244 | LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)aug, &aug->child)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3245 | break; |
| 3246 | case YANG_CASE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3247 | LY_CHECK_RET(parse_case(ctx, data, (struct lysp_node*)aug, &aug->child)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3248 | break; |
| 3249 | case YANG_CHOICE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3250 | LY_CHECK_RET(parse_choice(ctx, data, (struct lysp_node*)aug, &aug->child)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3251 | break; |
| 3252 | case YANG_CONTAINER: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3253 | LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)aug, &aug->child)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3254 | break; |
| 3255 | case YANG_LEAF: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3256 | LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)aug, &aug->child)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3257 | break; |
| 3258 | case YANG_LEAF_LIST: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3259 | LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)aug, &aug->child)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3260 | break; |
| 3261 | case YANG_LIST: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3262 | LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)aug, &aug->child)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3263 | break; |
| 3264 | case YANG_USES: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3265 | LY_CHECK_RET(parse_uses(ctx, data, (struct lysp_node*)aug, &aug->child)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3266 | break; |
| 3267 | |
| 3268 | case YANG_ACTION: |
Radek Krejci | ceaf212 | 2019-01-02 15:03:26 +0100 | [diff] [blame] | 3269 | YANG_CHECK_STMTVER2_RET(ctx, "action", "augment"); |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3270 | LY_CHECK_RET(parse_action(ctx, data, (struct lysp_node*)aug, &aug->actions)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3271 | break; |
| 3272 | case YANG_NOTIFICATION: |
Radek Krejci | ceaf212 | 2019-01-02 15:03:26 +0100 | [diff] [blame] | 3273 | YANG_CHECK_STMTVER2_RET(ctx, "notification", "augment"); |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3274 | LY_CHECK_RET(parse_notif(ctx, data, (struct lysp_node*)aug, &aug->notifs)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3275 | break; |
| 3276 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3277 | LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &aug->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3278 | break; |
| 3279 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3280 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "augment"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3281 | return LY_EVALID; |
| 3282 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3283 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3284 | return ret; |
| 3285 | } |
| 3286 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3287 | /** |
| 3288 | * @brief Parse the uses statement. |
| 3289 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3290 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3291 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 3292 | * @param[in,out] siblings Siblings to add to. |
| 3293 | * |
| 3294 | * @return LY_ERR values. |
| 3295 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3296 | static LY_ERR |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3297 | parse_uses(struct ly_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3298 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3299 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3300 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 3301 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3302 | enum yang_keyword kw; |
| 3303 | struct lysp_node *iter; |
| 3304 | struct lysp_node_uses *uses; |
| 3305 | |
| 3306 | /* create structure */ |
| 3307 | uses = calloc(1, sizeof *uses); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3308 | LY_CHECK_ERR_RET(!uses, LOGMEM(ctx->ctx), LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3309 | uses->nodetype = LYS_USES; |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3310 | uses->parent = parent; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3311 | |
| 3312 | /* insert into siblings */ |
| 3313 | if (!*siblings) { |
| 3314 | *siblings = (struct lysp_node *)uses; |
| 3315 | } else { |
| 3316 | for (iter = *siblings; iter->next; iter = iter->next); |
| 3317 | iter->next = (struct lysp_node *)uses; |
| 3318 | } |
| 3319 | |
| 3320 | /* get name */ |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3321 | LY_CHECK_RET(get_argument(ctx, data, Y_PREF_IDENTIF_ARG, &word, &buf, &word_len)); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3322 | INSERT_WORD(ctx, buf, uses->name, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3323 | |
| 3324 | /* parse substatements */ |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 3325 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3326 | switch (kw) { |
| 3327 | case YANG_DESCRIPTION: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3328 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &uses->dsc, Y_STR_ARG, &uses->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3329 | break; |
| 3330 | case YANG_IF_FEATURE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3331 | LY_CHECK_RET(parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &uses->iffeatures, Y_STR_ARG, &uses->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3332 | break; |
| 3333 | case YANG_REFERENCE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3334 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &uses->ref, Y_STR_ARG, &uses->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3335 | break; |
| 3336 | case YANG_STATUS: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3337 | LY_CHECK_RET(parse_status(ctx, data, &uses->flags, &uses->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3338 | break; |
| 3339 | case YANG_WHEN: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3340 | LY_CHECK_RET(parse_when(ctx, data, &uses->when)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3341 | break; |
| 3342 | |
| 3343 | case YANG_REFINE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3344 | LY_CHECK_RET(parse_refine(ctx, data, &uses->refines)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3345 | break; |
| 3346 | case YANG_AUGMENT: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3347 | LY_CHECK_RET(parse_augment(ctx, data, (struct lysp_node*)uses, &uses->augments)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3348 | break; |
| 3349 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3350 | LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &uses->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3351 | break; |
| 3352 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3353 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "uses"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3354 | return LY_EVALID; |
| 3355 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3356 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3357 | return ret; |
| 3358 | } |
| 3359 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3360 | /** |
| 3361 | * @brief Parse the case statement. |
| 3362 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3363 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3364 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 3365 | * @param[in,out] siblings Siblings to add to. |
| 3366 | * |
| 3367 | * @return LY_ERR values. |
| 3368 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3369 | static LY_ERR |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3370 | parse_case(struct ly_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3371 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3372 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3373 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 3374 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3375 | enum yang_keyword kw; |
| 3376 | struct lysp_node *iter; |
| 3377 | struct lysp_node_case *cas; |
| 3378 | |
| 3379 | /* create structure */ |
| 3380 | cas = calloc(1, sizeof *cas); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3381 | LY_CHECK_ERR_RET(!cas, LOGMEM(ctx->ctx), LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3382 | cas->nodetype = LYS_CASE; |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3383 | cas->parent = parent; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3384 | |
| 3385 | /* insert into siblings */ |
| 3386 | if (!*siblings) { |
| 3387 | *siblings = (struct lysp_node *)cas; |
| 3388 | } else { |
| 3389 | for (iter = *siblings; iter->next; iter = iter->next); |
| 3390 | iter->next = (struct lysp_node *)cas; |
| 3391 | } |
| 3392 | |
| 3393 | /* get name */ |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3394 | LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len)); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3395 | INSERT_WORD(ctx, buf, cas->name, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3396 | |
| 3397 | /* parse substatements */ |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 3398 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3399 | switch (kw) { |
| 3400 | case YANG_DESCRIPTION: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3401 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &cas->dsc, Y_STR_ARG, &cas->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3402 | break; |
| 3403 | case YANG_IF_FEATURE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3404 | LY_CHECK_RET(parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &cas->iffeatures, Y_STR_ARG, &cas->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3405 | break; |
| 3406 | case YANG_REFERENCE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3407 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &cas->ref, Y_STR_ARG, &cas->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3408 | break; |
| 3409 | case YANG_STATUS: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3410 | LY_CHECK_RET(parse_status(ctx, data, &cas->flags, &cas->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3411 | break; |
| 3412 | case YANG_WHEN: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3413 | LY_CHECK_RET(parse_when(ctx, data, &cas->when)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3414 | break; |
| 3415 | |
| 3416 | case YANG_ANYDATA: |
Radek Krejci | ceaf212 | 2019-01-02 15:03:26 +0100 | [diff] [blame] | 3417 | YANG_CHECK_STMTVER2_RET(ctx, "anydata", "case"); |
Radek Krejci | 1011365 | 2018-11-14 16:56:50 +0100 | [diff] [blame] | 3418 | /* fall through */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3419 | case YANG_ANYXML: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3420 | LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)cas, &cas->child)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3421 | break; |
| 3422 | case YANG_CHOICE: |
Radek Krejci | a9026eb | 2018-12-12 16:04:47 +0100 | [diff] [blame] | 3423 | LY_CHECK_RET(parse_choice(ctx, data, (struct lysp_node*)cas, &cas->child)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3424 | break; |
| 3425 | case YANG_CONTAINER: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3426 | LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)cas, &cas->child)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3427 | break; |
| 3428 | case YANG_LEAF: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3429 | LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)cas, &cas->child)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3430 | break; |
| 3431 | case YANG_LEAF_LIST: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3432 | LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)cas, &cas->child)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3433 | break; |
| 3434 | case YANG_LIST: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3435 | LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)cas, &cas->child)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3436 | break; |
| 3437 | case YANG_USES: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3438 | LY_CHECK_RET(parse_uses(ctx, data, (struct lysp_node*)cas, &cas->child)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3439 | break; |
| 3440 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3441 | LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &cas->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3442 | break; |
| 3443 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3444 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "case"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3445 | return LY_EVALID; |
| 3446 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3447 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3448 | return ret; |
| 3449 | } |
| 3450 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3451 | /** |
| 3452 | * @brief Parse the choice statement. |
| 3453 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3454 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3455 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 3456 | * @param[in,out] siblings Siblings to add to. |
| 3457 | * |
| 3458 | * @return LY_ERR values. |
| 3459 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3460 | static LY_ERR |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3461 | parse_choice(struct ly_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3462 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3463 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3464 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 3465 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3466 | enum yang_keyword kw; |
| 3467 | struct lysp_node *iter; |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3468 | struct lysp_node_choice *choice; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3469 | |
| 3470 | /* create structure */ |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3471 | choice = calloc(1, sizeof *choice); |
| 3472 | LY_CHECK_ERR_RET(!choice, LOGMEM(ctx->ctx), LY_EMEM); |
| 3473 | choice->nodetype = LYS_CHOICE; |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3474 | choice->parent = parent; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3475 | |
| 3476 | /* insert into siblings */ |
| 3477 | if (!*siblings) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3478 | *siblings = (struct lysp_node *)choice; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3479 | } else { |
| 3480 | for (iter = *siblings; iter->next; iter = iter->next); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3481 | iter->next = (struct lysp_node *)choice; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3482 | } |
| 3483 | |
| 3484 | /* get name */ |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3485 | LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len)); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3486 | INSERT_WORD(ctx, buf, choice->name, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3487 | |
| 3488 | /* parse substatements */ |
Radek Krejci | a9026eb | 2018-12-12 16:04:47 +0100 | [diff] [blame] | 3489 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3490 | switch (kw) { |
| 3491 | case YANG_CONFIG: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3492 | LY_CHECK_RET(parse_config(ctx, data, &choice->flags, &choice->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3493 | break; |
| 3494 | case YANG_DESCRIPTION: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3495 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &choice->dsc, Y_STR_ARG, &choice->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3496 | break; |
| 3497 | case YANG_IF_FEATURE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3498 | LY_CHECK_RET(parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &choice->iffeatures, Y_STR_ARG, &choice->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3499 | break; |
| 3500 | case YANG_MANDATORY: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3501 | LY_CHECK_RET(parse_mandatory(ctx, data, &choice->flags, &choice->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3502 | break; |
| 3503 | case YANG_REFERENCE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3504 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &choice->ref, Y_STR_ARG, &choice->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3505 | break; |
| 3506 | case YANG_STATUS: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3507 | LY_CHECK_RET(parse_status(ctx, data, &choice->flags, &choice->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3508 | break; |
| 3509 | case YANG_WHEN: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3510 | LY_CHECK_RET(parse_when(ctx, data, &choice->when)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3511 | break; |
| 3512 | case YANG_DEFAULT: |
Radek Krejci | a9026eb | 2018-12-12 16:04:47 +0100 | [diff] [blame] | 3513 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DEFAULT, 0, &choice->dflt, Y_PREF_IDENTIF_ARG, &choice->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3514 | break; |
| 3515 | |
| 3516 | case YANG_ANYDATA: |
Radek Krejci | ceaf212 | 2019-01-02 15:03:26 +0100 | [diff] [blame] | 3517 | YANG_CHECK_STMTVER2_RET(ctx, "anydata", "choice"); |
Radek Krejci | 1011365 | 2018-11-14 16:56:50 +0100 | [diff] [blame] | 3518 | /* fall through */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3519 | case YANG_ANYXML: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3520 | LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)choice, &choice->child)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3521 | break; |
| 3522 | case YANG_CASE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3523 | LY_CHECK_RET(parse_case(ctx, data, (struct lysp_node*)choice, &choice->child)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3524 | break; |
| 3525 | case YANG_CHOICE: |
Radek Krejci | ceaf212 | 2019-01-02 15:03:26 +0100 | [diff] [blame] | 3526 | YANG_CHECK_STMTVER2_RET(ctx, "choice", "choice"); |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3527 | LY_CHECK_RET(parse_choice(ctx, data, (struct lysp_node*)choice, &choice->child)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3528 | break; |
| 3529 | case YANG_CONTAINER: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3530 | LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)choice, &choice->child)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3531 | break; |
| 3532 | case YANG_LEAF: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3533 | LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)choice, &choice->child)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3534 | break; |
| 3535 | case YANG_LEAF_LIST: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3536 | LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)choice, &choice->child)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3537 | break; |
| 3538 | case YANG_LIST: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3539 | LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)choice, &choice->child)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3540 | break; |
| 3541 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3542 | LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &choice->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3543 | break; |
| 3544 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3545 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "choice"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3546 | return LY_EVALID; |
| 3547 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3548 | } |
Radek Krejci | a9026eb | 2018-12-12 16:04:47 +0100 | [diff] [blame] | 3549 | LY_CHECK_RET(ret); |
| 3550 | checks: |
| 3551 | if ((choice->flags & LYS_MAND_TRUE) && choice->dflt) { |
| 3552 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMSCOMB, "mandatory", "default", "choice"); |
| 3553 | return LY_EVALID; |
| 3554 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3555 | return ret; |
| 3556 | } |
| 3557 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3558 | /** |
| 3559 | * @brief Parse the container statement. |
| 3560 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3561 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3562 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 3563 | * @param[in,out] siblings Siblings to add to. |
| 3564 | * |
| 3565 | * @return LY_ERR values. |
| 3566 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3567 | static LY_ERR |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3568 | parse_container(struct ly_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3569 | { |
| 3570 | LY_ERR ret = 0; |
| 3571 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 3572 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3573 | enum yang_keyword kw; |
| 3574 | struct lysp_node *iter; |
| 3575 | struct lysp_node_container *cont; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 3576 | unsigned int u; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3577 | |
| 3578 | /* create structure */ |
| 3579 | cont = calloc(1, sizeof *cont); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3580 | LY_CHECK_ERR_RET(!cont, LOGMEM(ctx->ctx), LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3581 | cont->nodetype = LYS_CONTAINER; |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3582 | cont->parent = parent; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3583 | |
| 3584 | /* insert into siblings */ |
| 3585 | if (!*siblings) { |
| 3586 | *siblings = (struct lysp_node *)cont; |
| 3587 | } else { |
| 3588 | for (iter = *siblings; iter->next; iter = iter->next); |
| 3589 | iter->next = (struct lysp_node *)cont; |
| 3590 | } |
| 3591 | |
| 3592 | /* get name */ |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3593 | LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len)); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3594 | INSERT_WORD(ctx, buf, cont->name, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3595 | |
| 3596 | /* parse substatements */ |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 3597 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3598 | switch (kw) { |
| 3599 | case YANG_CONFIG: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3600 | LY_CHECK_RET(parse_config(ctx, data, &cont->flags, &cont->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3601 | break; |
| 3602 | case YANG_DESCRIPTION: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3603 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &cont->dsc, Y_STR_ARG, &cont->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3604 | break; |
| 3605 | case YANG_IF_FEATURE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3606 | LY_CHECK_RET(parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &cont->iffeatures, Y_STR_ARG, &cont->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3607 | break; |
| 3608 | case YANG_REFERENCE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3609 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &cont->ref, Y_STR_ARG, &cont->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3610 | break; |
| 3611 | case YANG_STATUS: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3612 | LY_CHECK_RET(parse_status(ctx, data, &cont->flags, &cont->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3613 | break; |
| 3614 | case YANG_WHEN: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3615 | LY_CHECK_RET(parse_when(ctx, data, &cont->when)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3616 | break; |
| 3617 | case YANG_PRESENCE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3618 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_PRESENCE, 0, &cont->presence, Y_STR_ARG, &cont->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3619 | break; |
| 3620 | |
| 3621 | case YANG_ANYDATA: |
Radek Krejci | ceaf212 | 2019-01-02 15:03:26 +0100 | [diff] [blame] | 3622 | YANG_CHECK_STMTVER2_RET(ctx, "anydata", "container"); |
Radek Krejci | 1011365 | 2018-11-14 16:56:50 +0100 | [diff] [blame] | 3623 | /* fall through */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3624 | case YANG_ANYXML: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3625 | LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)cont, &cont->child)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3626 | break; |
| 3627 | case YANG_CHOICE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3628 | LY_CHECK_RET(parse_choice(ctx, data, (struct lysp_node*)cont, &cont->child)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3629 | break; |
| 3630 | case YANG_CONTAINER: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3631 | LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)cont, &cont->child)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3632 | break; |
| 3633 | case YANG_LEAF: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3634 | LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)cont, &cont->child)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3635 | break; |
| 3636 | case YANG_LEAF_LIST: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3637 | LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)cont, &cont->child)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3638 | break; |
| 3639 | case YANG_LIST: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3640 | LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)cont, &cont->child)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3641 | break; |
| 3642 | case YANG_USES: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3643 | LY_CHECK_RET(parse_uses(ctx, data, (struct lysp_node*)cont, &cont->child)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3644 | break; |
| 3645 | |
| 3646 | case YANG_TYPEDEF: |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 3647 | LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node*)cont, data, &cont->typedefs)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3648 | break; |
| 3649 | case YANG_MUST: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3650 | LY_CHECK_RET(parse_restrs(ctx, data, kw, &cont->musts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3651 | break; |
| 3652 | case YANG_ACTION: |
Radek Krejci | ceaf212 | 2019-01-02 15:03:26 +0100 | [diff] [blame] | 3653 | YANG_CHECK_STMTVER2_RET(ctx, "action", "container"); |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3654 | LY_CHECK_RET(parse_action(ctx, data, (struct lysp_node*)cont, &cont->actions)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3655 | break; |
| 3656 | case YANG_GROUPING: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3657 | LY_CHECK_RET(parse_grouping(ctx, data, (struct lysp_node*)cont, &cont->groupings)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3658 | break; |
| 3659 | case YANG_NOTIFICATION: |
Radek Krejci | ceaf212 | 2019-01-02 15:03:26 +0100 | [diff] [blame] | 3660 | YANG_CHECK_STMTVER2_RET(ctx, "notification", "container"); |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3661 | LY_CHECK_RET(parse_notif(ctx, data, (struct lysp_node*)cont, &cont->notifs)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3662 | break; |
| 3663 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3664 | LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &cont->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3665 | break; |
| 3666 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3667 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "container"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3668 | return LY_EVALID; |
| 3669 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3670 | } |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 3671 | /* finalize parent pointers to the reallocated items */ |
| 3672 | LY_ARRAY_FOR(cont->groupings, u) { |
| 3673 | LY_LIST_FOR(cont->groupings[u].data, iter) { |
| 3674 | iter->parent = (struct lysp_node*)&cont->groupings[u]; |
| 3675 | } |
| 3676 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3677 | return ret; |
| 3678 | } |
| 3679 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3680 | /** |
| 3681 | * @brief Parse the list statement. |
| 3682 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3683 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3684 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 3685 | * @param[in,out] siblings Siblings to add to. |
| 3686 | * |
| 3687 | * @return LY_ERR values. |
| 3688 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3689 | static LY_ERR |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3690 | parse_list(struct ly_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3691 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3692 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3693 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 3694 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3695 | enum yang_keyword kw; |
| 3696 | struct lysp_node *iter; |
| 3697 | struct lysp_node_list *list; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 3698 | unsigned int u; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3699 | |
| 3700 | /* create structure */ |
| 3701 | list = calloc(1, sizeof *list); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3702 | LY_CHECK_ERR_RET(!list, LOGMEM(ctx->ctx), LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3703 | list->nodetype = LYS_LIST; |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3704 | list->parent = parent; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3705 | |
| 3706 | /* insert into siblings */ |
| 3707 | if (!*siblings) { |
| 3708 | *siblings = (struct lysp_node *)list; |
| 3709 | } else { |
| 3710 | for (iter = *siblings; iter->next; iter = iter->next); |
| 3711 | iter->next = (struct lysp_node *)list; |
| 3712 | } |
| 3713 | |
| 3714 | /* get name */ |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3715 | LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len)); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3716 | INSERT_WORD(ctx, buf, list->name, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3717 | |
| 3718 | /* parse substatements */ |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3719 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3720 | switch (kw) { |
| 3721 | case YANG_CONFIG: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3722 | LY_CHECK_RET(parse_config(ctx, data, &list->flags, &list->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3723 | break; |
| 3724 | case YANG_DESCRIPTION: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3725 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &list->dsc, Y_STR_ARG, &list->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3726 | break; |
| 3727 | case YANG_IF_FEATURE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3728 | LY_CHECK_RET(parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &list->iffeatures, Y_STR_ARG, &list->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3729 | break; |
| 3730 | case YANG_REFERENCE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3731 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &list->ref, Y_STR_ARG, &list->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3732 | break; |
| 3733 | case YANG_STATUS: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3734 | LY_CHECK_RET(parse_status(ctx, data, &list->flags, &list->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3735 | break; |
| 3736 | case YANG_WHEN: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3737 | LY_CHECK_RET(parse_when(ctx, data, &list->when)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3738 | break; |
| 3739 | case YANG_KEY: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3740 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_KEY, 0, &list->key, Y_STR_ARG, &list->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3741 | break; |
| 3742 | case YANG_MAX_ELEMENTS: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3743 | LY_CHECK_RET(parse_maxelements(ctx, data, &list->max, &list->flags, &list->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3744 | break; |
| 3745 | case YANG_MIN_ELEMENTS: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3746 | LY_CHECK_RET(parse_minelements(ctx, data, &list->min, &list->flags, &list->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3747 | break; |
| 3748 | case YANG_ORDERED_BY: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3749 | LY_CHECK_RET(parse_orderedby(ctx, data, &list->flags, &list->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3750 | break; |
| 3751 | case YANG_UNIQUE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3752 | LY_CHECK_RET(parse_text_fields(ctx, data, LYEXT_SUBSTMT_UNIQUE, &list->uniques, Y_STR_ARG, &list->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3753 | break; |
| 3754 | |
| 3755 | case YANG_ANYDATA: |
Radek Krejci | ceaf212 | 2019-01-02 15:03:26 +0100 | [diff] [blame] | 3756 | YANG_CHECK_STMTVER2_RET(ctx, "anydata", "list"); |
Radek Krejci | 1011365 | 2018-11-14 16:56:50 +0100 | [diff] [blame] | 3757 | /* fall through */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3758 | case YANG_ANYXML: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3759 | LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)list, &list->child)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3760 | break; |
| 3761 | case YANG_CHOICE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3762 | LY_CHECK_RET(parse_choice(ctx, data, (struct lysp_node*)list, &list->child)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3763 | break; |
| 3764 | case YANG_CONTAINER: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3765 | LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)list, &list->child)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3766 | break; |
| 3767 | case YANG_LEAF: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3768 | LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)list, &list->child)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3769 | break; |
| 3770 | case YANG_LEAF_LIST: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3771 | LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)list, &list->child)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3772 | break; |
| 3773 | case YANG_LIST: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3774 | LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)list, &list->child)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3775 | break; |
| 3776 | case YANG_USES: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3777 | LY_CHECK_RET(parse_uses(ctx, data, (struct lysp_node*)list, &list->child)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3778 | break; |
| 3779 | |
| 3780 | case YANG_TYPEDEF: |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 3781 | LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node*)list, data, &list->typedefs)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3782 | break; |
| 3783 | case YANG_MUST: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3784 | LY_CHECK_RET(parse_restrs(ctx, data, kw, &list->musts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3785 | break; |
| 3786 | case YANG_ACTION: |
Radek Krejci | ceaf212 | 2019-01-02 15:03:26 +0100 | [diff] [blame] | 3787 | YANG_CHECK_STMTVER2_RET(ctx, "action", "list"); |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3788 | LY_CHECK_RET(parse_action(ctx, data, (struct lysp_node*)list, &list->actions)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3789 | break; |
| 3790 | case YANG_GROUPING: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3791 | LY_CHECK_RET(parse_grouping(ctx, data, (struct lysp_node*)list, &list->groupings)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3792 | break; |
| 3793 | case YANG_NOTIFICATION: |
Radek Krejci | ceaf212 | 2019-01-02 15:03:26 +0100 | [diff] [blame] | 3794 | YANG_CHECK_STMTVER2_RET(ctx, "notification", "list"); |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3795 | LY_CHECK_RET(parse_notif(ctx, data, (struct lysp_node*)list, &list->notifs)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3796 | break; |
| 3797 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3798 | LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &list->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3799 | break; |
| 3800 | default: |
Radek Krejci | 1011365 | 2018-11-14 16:56:50 +0100 | [diff] [blame] | 3801 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "list"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3802 | return LY_EVALID; |
| 3803 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3804 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3805 | LY_CHECK_RET(ret); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 3806 | /* finalize parent pointers to the reallocated items */ |
| 3807 | LY_ARRAY_FOR(list->groupings, u) { |
| 3808 | LY_LIST_FOR(list->groupings[u].data, iter) { |
| 3809 | iter->parent = (struct lysp_node*)&list->groupings[u]; |
| 3810 | } |
| 3811 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3812 | checks: |
| 3813 | if (list->max && list->min > list->max) { |
| 3814 | LOGVAL_YANG(ctx, LYVE_SEMANTICS, |
| 3815 | "Invalid combination of min-elements and max-elements: min value %u is bigger than the max value %u.", |
| 3816 | list->min, list->max); |
| 3817 | return LY_EVALID; |
| 3818 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3819 | |
| 3820 | return ret; |
| 3821 | } |
| 3822 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3823 | /** |
| 3824 | * @brief Parse the yin-element statement. |
| 3825 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3826 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3827 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 3828 | * @param[in,out] flags Flags to write to. |
| 3829 | * @param[in,out] exts Extension instances to add to. |
| 3830 | * |
| 3831 | * @return LY_ERR values. |
| 3832 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3833 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3834 | 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] | 3835 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3836 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3837 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 3838 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3839 | enum yang_keyword kw; |
| 3840 | |
| 3841 | if (*flags & LYS_YINELEM_MASK) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3842 | LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "yin-element"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3843 | return LY_EVALID; |
| 3844 | } |
| 3845 | |
| 3846 | /* get value */ |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3847 | LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3848 | |
| 3849 | if ((word_len == 4) && !strncmp(word, "true", word_len)) { |
| 3850 | *flags |= LYS_YINELEM_TRUE; |
| 3851 | } else if ((word_len == 5) && !strncmp(word, "false", word_len)) { |
| 3852 | *flags |= LYS_YINELEM_FALSE; |
| 3853 | } else { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3854 | LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "yin-element"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3855 | free(buf); |
| 3856 | return LY_EVALID; |
| 3857 | } |
| 3858 | free(buf); |
| 3859 | |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 3860 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3861 | switch (kw) { |
| 3862 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3863 | LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_YINELEM, 0, exts)); |
| 3864 | LY_CHECK_RET(ret); break; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3865 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3866 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "yin-element"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3867 | return LY_EVALID; |
| 3868 | } |
| 3869 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3870 | return ret; |
| 3871 | } |
| 3872 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3873 | /** |
| 3874 | * @brief Parse the yin-element statement. |
| 3875 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3876 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3877 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 3878 | * @param[in,out] argument Value to write to. |
| 3879 | * @param[in,out] flags Flags to write to. |
| 3880 | * @param[in,out] exts Extension instances to add to. |
| 3881 | * |
| 3882 | * @return LY_ERR values. |
| 3883 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3884 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3885 | 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] | 3886 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3887 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3888 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 3889 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3890 | enum yang_keyword kw; |
| 3891 | |
| 3892 | if (*argument) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3893 | LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, "argument"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3894 | return LY_EVALID; |
| 3895 | } |
| 3896 | |
| 3897 | /* get value */ |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3898 | LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len)); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3899 | INSERT_WORD(ctx, buf, *argument, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3900 | |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 3901 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3902 | switch (kw) { |
| 3903 | case YANG_YIN_ELEMENT: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3904 | LY_CHECK_RET(parse_yinelement(ctx, data, flags, exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3905 | break; |
| 3906 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3907 | LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_ARGUMENT, 0, exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3908 | break; |
| 3909 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3910 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "argument"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3911 | return LY_EVALID; |
| 3912 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3913 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3914 | return ret; |
| 3915 | } |
| 3916 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3917 | /** |
| 3918 | * @brief Parse the extension statement. |
| 3919 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3920 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3921 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 3922 | * @param[in,out] extensions Extensions to add to. |
| 3923 | * |
| 3924 | * @return LY_ERR values. |
| 3925 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3926 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3927 | 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] | 3928 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3929 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3930 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 3931 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3932 | enum yang_keyword kw; |
| 3933 | struct lysp_ext *ex; |
| 3934 | |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 3935 | LY_ARRAY_NEW_RET(ctx->ctx, *extensions, ex, LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3936 | |
| 3937 | /* get value */ |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3938 | LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len)); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3939 | INSERT_WORD(ctx, buf, ex->name, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3940 | |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 3941 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3942 | switch (kw) { |
| 3943 | case YANG_DESCRIPTION: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3944 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &ex->dsc, Y_STR_ARG, &ex->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3945 | break; |
| 3946 | case YANG_REFERENCE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3947 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &ex->ref, Y_STR_ARG, &ex->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3948 | break; |
| 3949 | case YANG_STATUS: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3950 | LY_CHECK_RET(parse_status(ctx, data, &ex->flags, &ex->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3951 | break; |
| 3952 | case YANG_ARGUMENT: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3953 | LY_CHECK_RET(parse_argument(ctx, data, &ex->argument, &ex->flags, &ex->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3954 | break; |
| 3955 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3956 | LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &ex->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3957 | break; |
| 3958 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3959 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "extension"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3960 | return LY_EVALID; |
| 3961 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3962 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3963 | return ret; |
| 3964 | } |
| 3965 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3966 | /** |
| 3967 | * @brief Parse the deviate statement. |
| 3968 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3969 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3970 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 3971 | * @param[in,out] deviates Deviates to add to. |
| 3972 | * |
| 3973 | * @return LY_ERR values. |
| 3974 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3975 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3976 | 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] | 3977 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3978 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3979 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 3980 | size_t word_len, dev_mod; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3981 | enum yang_keyword kw; |
| 3982 | struct lysp_deviate *iter, *d; |
| 3983 | struct lysp_deviate_add *d_add = NULL; |
| 3984 | struct lysp_deviate_rpl *d_rpl = NULL; |
| 3985 | struct lysp_deviate_del *d_del = NULL; |
| 3986 | const char **d_units, ***d_uniques, ***d_dflts; |
| 3987 | struct lysp_restr **d_musts; |
| 3988 | uint16_t *d_flags; |
| 3989 | uint32_t *d_min, *d_max; |
| 3990 | |
| 3991 | /* get value */ |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3992 | LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3993 | |
| 3994 | if ((word_len == 13) && !strncmp(word, "not-supported", word_len)) { |
| 3995 | dev_mod = LYS_DEV_NOT_SUPPORTED; |
| 3996 | } else if ((word_len == 3) && !strncmp(word, "add", word_len)) { |
| 3997 | dev_mod = LYS_DEV_ADD; |
| 3998 | } else if ((word_len == 7) && !strncmp(word, "replace", word_len)) { |
| 3999 | dev_mod = LYS_DEV_REPLACE; |
| 4000 | } else if ((word_len == 6) && !strncmp(word, "delete", word_len)) { |
| 4001 | dev_mod = LYS_DEV_DELETE; |
| 4002 | } else { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 4003 | LOGVAL_YANG(ctx, LY_VCODE_INVAL, word_len, word, "deviate"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4004 | free(buf); |
| 4005 | return LY_EVALID; |
| 4006 | } |
| 4007 | free(buf); |
| 4008 | |
| 4009 | /* create structure */ |
| 4010 | switch (dev_mod) { |
| 4011 | case LYS_DEV_NOT_SUPPORTED: |
| 4012 | d = calloc(1, sizeof *d); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 4013 | LY_CHECK_ERR_RET(!d, LOGMEM(ctx->ctx), LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4014 | break; |
| 4015 | case LYS_DEV_ADD: |
| 4016 | d_add = calloc(1, sizeof *d_add); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 4017 | LY_CHECK_ERR_RET(!d_add, LOGMEM(ctx->ctx), LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4018 | d = (struct lysp_deviate *)d_add; |
| 4019 | d_units = &d_add->units; |
| 4020 | d_uniques = &d_add->uniques; |
| 4021 | d_dflts = &d_add->dflts; |
| 4022 | d_musts = &d_add->musts; |
| 4023 | d_flags = &d_add->flags; |
| 4024 | d_min = &d_add->min; |
| 4025 | d_max = &d_add->max; |
| 4026 | break; |
| 4027 | case LYS_DEV_REPLACE: |
| 4028 | d_rpl = calloc(1, sizeof *d_rpl); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 4029 | LY_CHECK_ERR_RET(!d_rpl, LOGMEM(ctx->ctx), LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4030 | d = (struct lysp_deviate *)d_rpl; |
| 4031 | d_units = &d_rpl->units; |
| 4032 | d_flags = &d_rpl->flags; |
| 4033 | d_min = &d_rpl->min; |
| 4034 | d_max = &d_rpl->max; |
| 4035 | break; |
| 4036 | case LYS_DEV_DELETE: |
| 4037 | d_del = calloc(1, sizeof *d_del); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 4038 | LY_CHECK_ERR_RET(!d_del, LOGMEM(ctx->ctx), LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4039 | d = (struct lysp_deviate *)d_del; |
| 4040 | d_units = &d_del->units; |
| 4041 | d_uniques = &d_del->uniques; |
| 4042 | d_dflts = &d_del->dflts; |
| 4043 | d_musts = &d_del->musts; |
| 4044 | d_flags = &d_del->flags; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4045 | break; |
| 4046 | default: |
| 4047 | assert(0); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 4048 | LOGINT_RET(ctx->ctx); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4049 | } |
| 4050 | d->mod = dev_mod; |
| 4051 | |
| 4052 | /* insert into siblings */ |
| 4053 | if (!*deviates) { |
| 4054 | *deviates = d; |
| 4055 | } else { |
| 4056 | for (iter = *deviates; iter->next; iter = iter->next); |
| 4057 | iter->next = d; |
| 4058 | } |
| 4059 | |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 4060 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4061 | switch (kw) { |
| 4062 | case YANG_CONFIG: |
| 4063 | switch (dev_mod) { |
| 4064 | case LYS_DEV_NOT_SUPPORTED: |
Michal Vasko | 492bec7 | 2018-09-18 13:11:10 +0200 | [diff] [blame] | 4065 | case LYS_DEV_DELETE: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 4066 | 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] | 4067 | return LY_EVALID; |
| 4068 | default: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 4069 | LY_CHECK_RET(parse_config(ctx, data, d_flags, &d->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4070 | break; |
| 4071 | } |
| 4072 | break; |
| 4073 | case YANG_DEFAULT: |
| 4074 | switch (dev_mod) { |
| 4075 | case LYS_DEV_NOT_SUPPORTED: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 4076 | 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] | 4077 | return LY_EVALID; |
| 4078 | case LYS_DEV_REPLACE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 4079 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DEFAULT, 0, &d_rpl->dflt, Y_STR_ARG, &d->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4080 | break; |
| 4081 | default: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 4082 | LY_CHECK_RET(parse_text_fields(ctx, data, LYEXT_SUBSTMT_DEFAULT, d_dflts, Y_STR_ARG, &d->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4083 | break; |
| 4084 | } |
| 4085 | break; |
| 4086 | case YANG_MANDATORY: |
| 4087 | switch (dev_mod) { |
| 4088 | case LYS_DEV_NOT_SUPPORTED: |
Michal Vasko | 492bec7 | 2018-09-18 13:11:10 +0200 | [diff] [blame] | 4089 | case LYS_DEV_DELETE: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 4090 | 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] | 4091 | return LY_EVALID; |
| 4092 | default: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 4093 | LY_CHECK_RET(parse_mandatory(ctx, data, d_flags, &d->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4094 | break; |
| 4095 | } |
| 4096 | break; |
| 4097 | case YANG_MAX_ELEMENTS: |
| 4098 | switch (dev_mod) { |
| 4099 | case LYS_DEV_NOT_SUPPORTED: |
Michal Vasko | 492bec7 | 2018-09-18 13:11:10 +0200 | [diff] [blame] | 4100 | case LYS_DEV_DELETE: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 4101 | 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] | 4102 | return LY_EVALID; |
| 4103 | default: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 4104 | LY_CHECK_RET(parse_maxelements(ctx, data, d_max, d_flags, &d->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4105 | break; |
| 4106 | } |
| 4107 | break; |
| 4108 | case YANG_MIN_ELEMENTS: |
| 4109 | switch (dev_mod) { |
| 4110 | case LYS_DEV_NOT_SUPPORTED: |
Michal Vasko | 492bec7 | 2018-09-18 13:11:10 +0200 | [diff] [blame] | 4111 | case LYS_DEV_DELETE: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 4112 | 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] | 4113 | return LY_EVALID; |
| 4114 | default: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 4115 | LY_CHECK_RET(parse_minelements(ctx, data, d_min, d_flags, &d->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4116 | break; |
| 4117 | } |
| 4118 | break; |
| 4119 | case YANG_MUST: |
| 4120 | switch (dev_mod) { |
| 4121 | case LYS_DEV_NOT_SUPPORTED: |
Michal Vasko | 492bec7 | 2018-09-18 13:11:10 +0200 | [diff] [blame] | 4122 | case LYS_DEV_REPLACE: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 4123 | 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] | 4124 | return LY_EVALID; |
| 4125 | default: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 4126 | LY_CHECK_RET(parse_restrs(ctx, data, kw, d_musts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4127 | break; |
| 4128 | } |
| 4129 | break; |
| 4130 | case YANG_TYPE: |
| 4131 | switch (dev_mod) { |
| 4132 | case LYS_DEV_NOT_SUPPORTED: |
| 4133 | case LYS_DEV_ADD: |
| 4134 | case LYS_DEV_DELETE: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 4135 | 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] | 4136 | return LY_EVALID; |
| 4137 | default: |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 4138 | if (d_rpl->type) { |
| 4139 | LOGVAL_YANG(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw)); |
| 4140 | return LY_EVALID; |
| 4141 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4142 | d_rpl->type = calloc(1, sizeof *d_rpl->type); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 4143 | LY_CHECK_ERR_RET(!d_rpl->type, LOGMEM(ctx->ctx), LY_EMEM); |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 4144 | LY_CHECK_RET(parse_type(ctx, data, d_rpl->type)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4145 | break; |
| 4146 | } |
| 4147 | break; |
| 4148 | case YANG_UNIQUE: |
| 4149 | switch (dev_mod) { |
| 4150 | case LYS_DEV_NOT_SUPPORTED: |
| 4151 | case LYS_DEV_REPLACE: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 4152 | 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] | 4153 | return LY_EVALID; |
| 4154 | default: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 4155 | LY_CHECK_RET(parse_text_fields(ctx, data, LYEXT_SUBSTMT_UNIQUE, d_uniques, Y_STR_ARG, &d->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4156 | break; |
| 4157 | } |
| 4158 | break; |
| 4159 | case YANG_UNITS: |
| 4160 | switch (dev_mod) { |
| 4161 | case LYS_DEV_NOT_SUPPORTED: |
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: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 4165 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_UNITS, 0, d_units, Y_STR_ARG, &d->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4166 | break; |
| 4167 | } |
| 4168 | break; |
| 4169 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 4170 | LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &d->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4171 | break; |
| 4172 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 4173 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "deviate"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4174 | return LY_EVALID; |
| 4175 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4176 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4177 | return ret; |
| 4178 | } |
| 4179 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 4180 | /** |
| 4181 | * @brief Parse the deviation statement. |
| 4182 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 4183 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 4184 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 4185 | * @param[in,out] deviations Deviations to add to. |
| 4186 | * |
| 4187 | * @return LY_ERR values. |
| 4188 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4189 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 4190 | 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] | 4191 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 4192 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4193 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 4194 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4195 | enum yang_keyword kw; |
| 4196 | struct lysp_deviation *dev; |
| 4197 | |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 4198 | LY_ARRAY_NEW_RET(ctx->ctx, *deviations, dev, LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4199 | |
| 4200 | /* get value */ |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 4201 | LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, &word, &buf, &word_len)); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 4202 | INSERT_WORD(ctx, buf, dev->nodeid, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4203 | |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 4204 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4205 | switch (kw) { |
| 4206 | case YANG_DESCRIPTION: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 4207 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &dev->dsc, Y_STR_ARG, &dev->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4208 | break; |
| 4209 | case YANG_DEVIATE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 4210 | LY_CHECK_RET(parse_deviate(ctx, data, &dev->deviates)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4211 | break; |
| 4212 | case YANG_REFERENCE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 4213 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &dev->ref, Y_STR_ARG, &dev->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4214 | break; |
| 4215 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 4216 | LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &dev->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 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), "deviation"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4220 | return LY_EVALID; |
| 4221 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4222 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 4223 | LY_CHECK_RET(ret); |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 4224 | checks: |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4225 | /* mandatory substatements */ |
| 4226 | if (!dev->deviates) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 4227 | LOGVAL_YANG(ctx, LY_VCODE_MISSTMT, "deviate", "deviation"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4228 | return LY_EVALID; |
| 4229 | } |
| 4230 | |
| 4231 | return ret; |
| 4232 | } |
| 4233 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 4234 | /** |
| 4235 | * @brief Parse the feature statement. |
| 4236 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 4237 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 4238 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 4239 | * @param[in,out] features Features to add to. |
| 4240 | * |
| 4241 | * @return LY_ERR values. |
| 4242 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4243 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 4244 | 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] | 4245 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 4246 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4247 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 4248 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4249 | enum yang_keyword kw; |
| 4250 | struct lysp_feature *feat; |
| 4251 | |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 4252 | LY_ARRAY_NEW_RET(ctx->ctx, *features, feat, LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4253 | |
| 4254 | /* get value */ |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 4255 | LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len)); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 4256 | INSERT_WORD(ctx, buf, feat->name, word, word_len); |
Radek Krejci | faa1eac | 2018-10-30 14:34:55 +0100 | [diff] [blame] | 4257 | |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 4258 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4259 | switch (kw) { |
| 4260 | case YANG_DESCRIPTION: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 4261 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &feat->dsc, Y_STR_ARG, &feat->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4262 | break; |
| 4263 | case YANG_IF_FEATURE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 4264 | LY_CHECK_RET(parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &feat->iffeatures, Y_STR_ARG, &feat->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4265 | break; |
| 4266 | case YANG_REFERENCE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 4267 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &feat->ref, Y_STR_ARG, &feat->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4268 | break; |
| 4269 | case YANG_STATUS: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 4270 | LY_CHECK_RET(parse_status(ctx, data, &feat->flags, &feat->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4271 | break; |
| 4272 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 4273 | LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &feat->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4274 | break; |
| 4275 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 4276 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "feature"); |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 4277 | return LY_EVALID; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4278 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4279 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4280 | return ret; |
| 4281 | } |
| 4282 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 4283 | /** |
| 4284 | * @brief Parse the identity statement. |
| 4285 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 4286 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 4287 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 4288 | * @param[in,out] identities Identities to add to. |
| 4289 | * |
| 4290 | * @return LY_ERR values. |
| 4291 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4292 | static LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 4293 | 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] | 4294 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 4295 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4296 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 4297 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4298 | enum yang_keyword kw; |
| 4299 | struct lysp_ident *ident; |
| 4300 | |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 4301 | LY_ARRAY_NEW_RET(ctx->ctx, *identities, ident, LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4302 | |
| 4303 | /* get value */ |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 4304 | LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len)); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 4305 | INSERT_WORD(ctx, buf, ident->name, word, word_len); |
Radek Krejci | faa1eac | 2018-10-30 14:34:55 +0100 | [diff] [blame] | 4306 | |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 4307 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4308 | switch (kw) { |
| 4309 | case YANG_DESCRIPTION: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 4310 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &ident->dsc, Y_STR_ARG, &ident->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4311 | break; |
| 4312 | case YANG_IF_FEATURE: |
Radek Krejci | ceaf212 | 2019-01-02 15:03:26 +0100 | [diff] [blame] | 4313 | YANG_CHECK_STMTVER2_RET(ctx, "if-feature", "identity"); |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 4314 | LY_CHECK_RET(parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &ident->iffeatures, Y_STR_ARG, &ident->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4315 | break; |
| 4316 | case YANG_REFERENCE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 4317 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &ident->ref, Y_STR_ARG, &ident->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4318 | break; |
| 4319 | case YANG_STATUS: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 4320 | LY_CHECK_RET(parse_status(ctx, data, &ident->flags, &ident->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4321 | break; |
| 4322 | case YANG_BASE: |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4323 | if (ident->bases && ctx->mod_version < 2) { |
Radek Krejci | 1011365 | 2018-11-14 16:56:50 +0100 | [diff] [blame] | 4324 | LOGVAL_YANG(ctx, LYVE_SYNTAX_YANG, "Identity can be derived from multiple base identities only in YANG 1.1 modules"); |
| 4325 | return LY_EVALID; |
| 4326 | } |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 4327 | LY_CHECK_RET(parse_text_fields(ctx, data, LYEXT_SUBSTMT_BASE, &ident->bases, Y_PREF_IDENTIF_ARG, &ident->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4328 | break; |
| 4329 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 4330 | LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &ident->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4331 | break; |
| 4332 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 4333 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "identity"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4334 | return LY_EVALID; |
| 4335 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4336 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4337 | return ret; |
| 4338 | } |
| 4339 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 4340 | /** |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4341 | * @brief Finalize some of the (sub)module structure after parsing. |
| 4342 | * |
| 4343 | * Update parent pointers in the nodes inside grouping/RPC/Notification, which could be reallocated. |
| 4344 | * |
| 4345 | * @param[in] mod Parsed module to be updated. |
| 4346 | * @return LY_ERR value (currently only LY_SUCCESS, but it can change in future). |
| 4347 | */ |
| 4348 | static LY_ERR |
| 4349 | parse_sub_module_finalize(struct lysp_module *mod) |
| 4350 | { |
| 4351 | unsigned int u; |
| 4352 | struct lysp_node *child; |
| 4353 | |
| 4354 | /* finalize parent pointers to the reallocated items */ |
| 4355 | LY_ARRAY_FOR(mod->groupings, u) { |
| 4356 | LY_LIST_FOR(mod->groupings[u].data, child) { |
| 4357 | child->parent = (struct lysp_node*)&mod->groupings[u]; |
| 4358 | } |
| 4359 | } |
| 4360 | |
| 4361 | /* TODO the same finalization for rpcs and notifications, do also in the relevant nodes */ |
| 4362 | |
| 4363 | return LY_SUCCESS; |
| 4364 | } |
| 4365 | |
| 4366 | /** |
| 4367 | * @brief Parse module substatements. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 4368 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 4369 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 4370 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 4371 | * @param[in,out] mod Module to write to. |
| 4372 | * |
| 4373 | * @return LY_ERR values. |
| 4374 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4375 | static LY_ERR |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4376 | parse_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] | 4377 | { |
| 4378 | LY_ERR ret = 0; |
| 4379 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 4380 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4381 | enum yang_keyword kw, prev_kw = 0; |
| 4382 | enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4383 | struct lysp_submodule *dup; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4384 | |
| 4385 | /* (sub)module name */ |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 4386 | LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len)); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4387 | INSERT_WORD(ctx, buf, mod->mod->name, word, word_len); |
Radek Krejci | faa1eac | 2018-10-30 14:34:55 +0100 | [diff] [blame] | 4388 | |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 4389 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4390 | |
Radek Krejci | e384647 | 2018-10-15 15:24:51 +0200 | [diff] [blame] | 4391 | #define CHECK_ORDER(SECTION) \ |
| 4392 | if (mod_stmt > SECTION) {LOGVAL_YANG(ctx, LY_VCODE_INORD, ly_stmt2str(kw), ly_stmt2str(prev_kw)); return LY_EVALID;}mod_stmt = SECTION |
| 4393 | |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4394 | switch (kw) { |
| 4395 | /* module header */ |
| 4396 | case YANG_NAMESPACE: |
| 4397 | case YANG_PREFIX: |
Radek Krejci | e384647 | 2018-10-15 15:24:51 +0200 | [diff] [blame] | 4398 | CHECK_ORDER(Y_MOD_MODULE_HEADER); |
| 4399 | break; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4400 | case YANG_YANG_VERSION: |
Radek Krejci | e384647 | 2018-10-15 15:24:51 +0200 | [diff] [blame] | 4401 | CHECK_ORDER(Y_MOD_MODULE_HEADER); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4402 | break; |
| 4403 | /* linkage */ |
| 4404 | case YANG_INCLUDE: |
| 4405 | case YANG_IMPORT: |
Radek Krejci | e384647 | 2018-10-15 15:24:51 +0200 | [diff] [blame] | 4406 | CHECK_ORDER(Y_MOD_LINKAGE); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4407 | break; |
| 4408 | /* meta */ |
| 4409 | case YANG_ORGANIZATION: |
| 4410 | case YANG_CONTACT: |
| 4411 | case YANG_DESCRIPTION: |
| 4412 | case YANG_REFERENCE: |
Radek Krejci | e384647 | 2018-10-15 15:24:51 +0200 | [diff] [blame] | 4413 | CHECK_ORDER(Y_MOD_META); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4414 | break; |
| 4415 | |
| 4416 | /* revision */ |
| 4417 | case YANG_REVISION: |
Radek Krejci | e384647 | 2018-10-15 15:24:51 +0200 | [diff] [blame] | 4418 | CHECK_ORDER(Y_MOD_REVISION); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4419 | break; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4420 | /* body */ |
| 4421 | case YANG_ANYDATA: |
| 4422 | case YANG_ANYXML: |
| 4423 | case YANG_AUGMENT: |
| 4424 | case YANG_CHOICE: |
| 4425 | case YANG_CONTAINER: |
| 4426 | case YANG_DEVIATION: |
| 4427 | case YANG_EXTENSION: |
| 4428 | case YANG_FEATURE: |
| 4429 | case YANG_GROUPING: |
| 4430 | case YANG_IDENTITY: |
| 4431 | case YANG_LEAF: |
| 4432 | case YANG_LEAF_LIST: |
| 4433 | case YANG_LIST: |
| 4434 | case YANG_NOTIFICATION: |
| 4435 | case YANG_RPC: |
| 4436 | case YANG_TYPEDEF: |
| 4437 | case YANG_USES: |
| 4438 | case YANG_CUSTOM: |
| 4439 | mod_stmt = Y_MOD_BODY; |
| 4440 | break; |
| 4441 | default: |
| 4442 | /* error handled in the next switch */ |
| 4443 | break; |
| 4444 | } |
Radek Krejci | e384647 | 2018-10-15 15:24:51 +0200 | [diff] [blame] | 4445 | #undef CHECK_ORDER |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4446 | |
Radek Krejci | e384647 | 2018-10-15 15:24:51 +0200 | [diff] [blame] | 4447 | prev_kw = kw; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4448 | switch (kw) { |
| 4449 | /* module header */ |
| 4450 | case YANG_YANG_VERSION: |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4451 | LY_CHECK_RET(parse_yangversion(ctx, data, &mod->mod->version, &mod->exts)); |
| 4452 | ctx->mod_version = mod->mod->version; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4453 | break; |
| 4454 | case YANG_NAMESPACE: |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4455 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_NAMESPACE, 0, &mod->mod->ns, Y_STR_ARG, &mod->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4456 | break; |
| 4457 | case YANG_PREFIX: |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4458 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_PREFIX, 0, &mod->mod->prefix, Y_IDENTIF_ARG, &mod->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4459 | break; |
| 4460 | |
| 4461 | /* linkage */ |
| 4462 | case YANG_INCLUDE: |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4463 | LY_CHECK_RET(parse_include(ctx, mod->mod->name, data, &mod->includes)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4464 | break; |
| 4465 | case YANG_IMPORT: |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4466 | LY_CHECK_RET(parse_import(ctx, mod->mod->prefix, data, &mod->imports)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4467 | break; |
| 4468 | |
| 4469 | /* meta */ |
| 4470 | case YANG_ORGANIZATION: |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4471 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_ORGANIZATION, 0, &mod->mod->org, Y_STR_ARG, &mod->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4472 | break; |
| 4473 | case YANG_CONTACT: |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4474 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_CONTACT, 0, &mod->mod->contact, Y_STR_ARG, &mod->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4475 | break; |
| 4476 | case YANG_DESCRIPTION: |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4477 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &mod->mod->dsc, Y_STR_ARG, &mod->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4478 | break; |
| 4479 | case YANG_REFERENCE: |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4480 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &mod->mod->ref, Y_STR_ARG, &mod->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4481 | break; |
| 4482 | |
| 4483 | /* revision */ |
| 4484 | case YANG_REVISION: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 4485 | LY_CHECK_RET(parse_revision(ctx, data, &mod->revs)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4486 | break; |
| 4487 | |
| 4488 | /* body */ |
| 4489 | case YANG_ANYDATA: |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4490 | YANG_CHECK_STMTVER2_RET(ctx, "anydata", "module"); |
Radek Krejci | 1011365 | 2018-11-14 16:56:50 +0100 | [diff] [blame] | 4491 | /* fall through */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4492 | case YANG_ANYXML: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 4493 | LY_CHECK_RET(parse_any(ctx, data, kw, NULL, &mod->data)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4494 | break; |
| 4495 | case YANG_CHOICE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 4496 | LY_CHECK_RET(parse_choice(ctx, data, NULL, &mod->data)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4497 | break; |
| 4498 | case YANG_CONTAINER: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 4499 | LY_CHECK_RET(parse_container(ctx, data, NULL, &mod->data)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4500 | break; |
| 4501 | case YANG_LEAF: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 4502 | LY_CHECK_RET(parse_leaf(ctx, data, NULL, &mod->data)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4503 | break; |
| 4504 | case YANG_LEAF_LIST: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 4505 | LY_CHECK_RET(parse_leaflist(ctx, data, NULL, &mod->data)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4506 | break; |
| 4507 | case YANG_LIST: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 4508 | LY_CHECK_RET(parse_list(ctx, data, NULL, &mod->data)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4509 | break; |
| 4510 | case YANG_USES: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 4511 | LY_CHECK_RET(parse_uses(ctx, data, NULL, &mod->data)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4512 | break; |
| 4513 | |
| 4514 | case YANG_AUGMENT: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 4515 | LY_CHECK_RET(parse_augment(ctx, data, NULL, &mod->augments)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4516 | break; |
| 4517 | case YANG_DEVIATION: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 4518 | LY_CHECK_RET(parse_deviation(ctx, data, &mod->deviations)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4519 | break; |
| 4520 | case YANG_EXTENSION: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 4521 | LY_CHECK_RET(parse_extension(ctx, data, &mod->extensions)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4522 | break; |
| 4523 | case YANG_FEATURE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 4524 | LY_CHECK_RET(parse_feature(ctx, data, &mod->features)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4525 | break; |
| 4526 | case YANG_GROUPING: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 4527 | LY_CHECK_RET(parse_grouping(ctx, data, NULL, &mod->groupings)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4528 | break; |
| 4529 | case YANG_IDENTITY: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 4530 | LY_CHECK_RET(parse_identity(ctx, data, &mod->identities)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4531 | break; |
| 4532 | case YANG_NOTIFICATION: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 4533 | LY_CHECK_RET(parse_notif(ctx, data, NULL, &mod->notifs)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4534 | break; |
| 4535 | case YANG_RPC: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 4536 | LY_CHECK_RET(parse_action(ctx, data, NULL, &mod->rpcs)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4537 | break; |
| 4538 | case YANG_TYPEDEF: |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 4539 | LY_CHECK_RET(parse_typedef(ctx, NULL, data, &mod->typedefs)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4540 | break; |
| 4541 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 4542 | LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &mod->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4543 | break; |
| 4544 | |
| 4545 | default: |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4546 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "module"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4547 | return LY_EVALID; |
| 4548 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4549 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 4550 | LY_CHECK_RET(ret); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4551 | |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 4552 | checks: |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4553 | /* finalize parent pointers to the reallocated items */ |
| 4554 | LY_CHECK_RET(parse_sub_module_finalize(mod)); |
| 4555 | |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4556 | /* mandatory substatements */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4557 | if (!mod->mod->ns) { |
| 4558 | LOGVAL_YANG(ctx, LY_VCODE_MISSTMT, "namespace", "module"); |
| 4559 | return LY_EVALID; |
| 4560 | } else if (!mod->mod->prefix) { |
| 4561 | LOGVAL_YANG(ctx, LY_VCODE_MISSTMT, "prefix", "module"); |
| 4562 | return LY_EVALID; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4563 | } |
| 4564 | |
Radek Krejci | e9e987e | 2018-10-31 12:50:27 +0100 | [diff] [blame] | 4565 | /* submodules share the namespace with the module names, so there must not be |
| 4566 | * a submodule of the same name in the context, no need for revision matching */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4567 | dup = ly_ctx_get_submodule(ctx->ctx, NULL, mod->mod->name, NULL); |
| 4568 | if (dup) { |
| 4569 | LOGVAL_YANG(ctx, LYVE_SYNTAX_YANG, "Name collision between module and submodule of name \"%s\".", mod->mod->name); |
| 4570 | return LY_EVALID; |
| 4571 | } |
| 4572 | |
| 4573 | return ret; |
| 4574 | } |
| 4575 | |
| 4576 | /** |
| 4577 | * @brief Parse submodule substatements. |
| 4578 | * |
| 4579 | * @param[in] ctx yang parser context for logging. |
| 4580 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 4581 | * @param[out] submod Parsed submodule structure. |
| 4582 | * |
| 4583 | * @return LY_ERR values. |
| 4584 | */ |
| 4585 | static LY_ERR |
| 4586 | parse_submodule(struct ly_parser_ctx *ctx, const char **data, struct lysp_submodule *submod) |
| 4587 | { |
| 4588 | LY_ERR ret = 0; |
| 4589 | char *buf, *word; |
| 4590 | size_t word_len; |
| 4591 | enum yang_keyword kw, prev_kw = 0; |
| 4592 | enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER; |
| 4593 | struct lysp_submodule *dup; |
| 4594 | |
| 4595 | /* submodule name */ |
| 4596 | LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len)); |
| 4597 | INSERT_WORD(ctx, buf, submod->name, word, word_len); |
| 4598 | |
| 4599 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) { |
| 4600 | |
| 4601 | #define CHECK_ORDER(SECTION) \ |
| 4602 | if (mod_stmt > SECTION) {LOGVAL_YANG(ctx, LY_VCODE_INORD, ly_stmt2str(kw), ly_stmt2str(prev_kw)); return LY_EVALID;}mod_stmt = SECTION |
| 4603 | |
| 4604 | switch (kw) { |
| 4605 | /* module header */ |
| 4606 | case YANG_BELONGS_TO: |
| 4607 | CHECK_ORDER(Y_MOD_MODULE_HEADER); |
| 4608 | break; |
| 4609 | case YANG_YANG_VERSION: |
| 4610 | CHECK_ORDER(Y_MOD_MODULE_HEADER); |
| 4611 | break; |
| 4612 | /* linkage */ |
| 4613 | case YANG_INCLUDE: |
| 4614 | case YANG_IMPORT: |
| 4615 | CHECK_ORDER(Y_MOD_LINKAGE); |
| 4616 | break; |
| 4617 | /* meta */ |
| 4618 | case YANG_ORGANIZATION: |
| 4619 | case YANG_CONTACT: |
| 4620 | case YANG_DESCRIPTION: |
| 4621 | case YANG_REFERENCE: |
| 4622 | CHECK_ORDER(Y_MOD_META); |
| 4623 | break; |
| 4624 | |
| 4625 | /* revision */ |
| 4626 | case YANG_REVISION: |
| 4627 | CHECK_ORDER(Y_MOD_REVISION); |
| 4628 | break; |
| 4629 | /* body */ |
| 4630 | case YANG_ANYDATA: |
| 4631 | case YANG_ANYXML: |
| 4632 | case YANG_AUGMENT: |
| 4633 | case YANG_CHOICE: |
| 4634 | case YANG_CONTAINER: |
| 4635 | case YANG_DEVIATION: |
| 4636 | case YANG_EXTENSION: |
| 4637 | case YANG_FEATURE: |
| 4638 | case YANG_GROUPING: |
| 4639 | case YANG_IDENTITY: |
| 4640 | case YANG_LEAF: |
| 4641 | case YANG_LEAF_LIST: |
| 4642 | case YANG_LIST: |
| 4643 | case YANG_NOTIFICATION: |
| 4644 | case YANG_RPC: |
| 4645 | case YANG_TYPEDEF: |
| 4646 | case YANG_USES: |
| 4647 | case YANG_CUSTOM: |
| 4648 | mod_stmt = Y_MOD_BODY; |
| 4649 | break; |
| 4650 | default: |
| 4651 | /* error handled in the next switch */ |
| 4652 | break; |
| 4653 | } |
| 4654 | #undef CHECK_ORDER |
| 4655 | |
| 4656 | prev_kw = kw; |
| 4657 | switch (kw) { |
| 4658 | /* module header */ |
| 4659 | case YANG_YANG_VERSION: |
| 4660 | LY_CHECK_RET(parse_yangversion(ctx, data, &submod->version, &submod->exts)); |
| 4661 | ctx->mod_version = submod->version; |
| 4662 | break; |
| 4663 | case YANG_BELONGS_TO: |
| 4664 | LY_CHECK_RET(parse_belongsto(ctx, data, &submod->belongsto, &submod->prefix, &submod->exts)); |
| 4665 | break; |
| 4666 | |
| 4667 | /* linkage */ |
| 4668 | case YANG_INCLUDE: |
| 4669 | LY_CHECK_RET(parse_include(ctx, submod->name, data, &submod->includes)); |
| 4670 | break; |
| 4671 | case YANG_IMPORT: |
| 4672 | LY_CHECK_RET(parse_import(ctx, submod->prefix, data, &submod->imports)); |
| 4673 | break; |
| 4674 | |
| 4675 | /* meta */ |
| 4676 | case YANG_ORGANIZATION: |
| 4677 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_ORGANIZATION, 0, &submod->org, Y_STR_ARG, &submod->exts)); |
| 4678 | break; |
| 4679 | case YANG_CONTACT: |
| 4680 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_CONTACT, 0, &submod->contact, Y_STR_ARG, &submod->exts)); |
| 4681 | break; |
| 4682 | case YANG_DESCRIPTION: |
| 4683 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &submod->dsc, Y_STR_ARG, &submod->exts)); |
| 4684 | break; |
| 4685 | case YANG_REFERENCE: |
| 4686 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &submod->ref, Y_STR_ARG, &submod->exts)); |
| 4687 | break; |
| 4688 | |
| 4689 | /* revision */ |
| 4690 | case YANG_REVISION: |
| 4691 | LY_CHECK_RET(parse_revision(ctx, data, &submod->revs)); |
| 4692 | break; |
| 4693 | |
| 4694 | /* body */ |
| 4695 | case YANG_ANYDATA: |
| 4696 | YANG_CHECK_STMTVER2_RET(ctx, "anydata", "submodule"); |
| 4697 | /* fall through */ |
| 4698 | case YANG_ANYXML: |
| 4699 | LY_CHECK_RET(parse_any(ctx, data, kw, NULL, &submod->data)); |
| 4700 | break; |
| 4701 | case YANG_CHOICE: |
| 4702 | LY_CHECK_RET(parse_choice(ctx, data, NULL, &submod->data)); |
| 4703 | break; |
| 4704 | case YANG_CONTAINER: |
| 4705 | LY_CHECK_RET(parse_container(ctx, data, NULL, &submod->data)); |
| 4706 | break; |
| 4707 | case YANG_LEAF: |
| 4708 | LY_CHECK_RET(parse_leaf(ctx, data, NULL, &submod->data)); |
| 4709 | break; |
| 4710 | case YANG_LEAF_LIST: |
| 4711 | LY_CHECK_RET(parse_leaflist(ctx, data, NULL, &submod->data)); |
| 4712 | break; |
| 4713 | case YANG_LIST: |
| 4714 | LY_CHECK_RET(parse_list(ctx, data, NULL, &submod->data)); |
| 4715 | break; |
| 4716 | case YANG_USES: |
| 4717 | LY_CHECK_RET(parse_uses(ctx, data, NULL, &submod->data)); |
| 4718 | break; |
| 4719 | |
| 4720 | case YANG_AUGMENT: |
| 4721 | LY_CHECK_RET(parse_augment(ctx, data, NULL, &submod->augments)); |
| 4722 | break; |
| 4723 | case YANG_DEVIATION: |
| 4724 | LY_CHECK_RET(parse_deviation(ctx, data, &submod->deviations)); |
| 4725 | break; |
| 4726 | case YANG_EXTENSION: |
| 4727 | LY_CHECK_RET(parse_extension(ctx, data, &submod->extensions)); |
| 4728 | break; |
| 4729 | case YANG_FEATURE: |
| 4730 | LY_CHECK_RET(parse_feature(ctx, data, &submod->features)); |
| 4731 | break; |
| 4732 | case YANG_GROUPING: |
| 4733 | LY_CHECK_RET(parse_grouping(ctx, data, NULL, &submod->groupings)); |
| 4734 | break; |
| 4735 | case YANG_IDENTITY: |
| 4736 | LY_CHECK_RET(parse_identity(ctx, data, &submod->identities)); |
| 4737 | break; |
| 4738 | case YANG_NOTIFICATION: |
| 4739 | LY_CHECK_RET(parse_notif(ctx, data, NULL, &submod->notifs)); |
| 4740 | break; |
| 4741 | case YANG_RPC: |
| 4742 | LY_CHECK_RET(parse_action(ctx, data, NULL, &submod->rpcs)); |
| 4743 | break; |
| 4744 | case YANG_TYPEDEF: |
| 4745 | LY_CHECK_RET(parse_typedef(ctx, NULL, data, &submod->typedefs)); |
| 4746 | break; |
| 4747 | case YANG_CUSTOM: |
| 4748 | LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &submod->exts)); |
| 4749 | break; |
| 4750 | |
| 4751 | default: |
| 4752 | LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "submodule"); |
| 4753 | return LY_EVALID; |
| 4754 | } |
| 4755 | } |
| 4756 | LY_CHECK_RET(ret); |
| 4757 | |
| 4758 | checks: |
| 4759 | /* finalize parent pointers to the reallocated items */ |
| 4760 | LY_CHECK_RET(parse_sub_module_finalize((struct lysp_module*)submod)); |
| 4761 | |
| 4762 | /* mandatory substatements */ |
| 4763 | if (!submod->belongsto) { |
| 4764 | LOGVAL_YANG(ctx, LY_VCODE_MISSTMT, "belongs-to", "submodule"); |
| 4765 | return LY_EVALID; |
| 4766 | } |
| 4767 | |
| 4768 | /* submodules share the namespace with the module names, so there must not be |
| 4769 | * a submodule of the same name in the context, no need for revision matching */ |
| 4770 | dup = ly_ctx_get_submodule(ctx->ctx, NULL, submod->name, NULL); |
| 4771 | if (dup && strcmp(dup->belongsto, submod->belongsto)) { |
| 4772 | LOGVAL_YANG(ctx, LYVE_SYNTAX_YANG, "Name collision between submodules of name \"%s\".", dup->name); |
Radek Krejci | e9e987e | 2018-10-31 12:50:27 +0100 | [diff] [blame] | 4773 | return LY_EVALID; |
| 4774 | } |
| 4775 | |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4776 | return ret; |
| 4777 | } |
| 4778 | |
Radek Krejci | d4557c6 | 2018-09-17 11:42:09 +0200 | [diff] [blame] | 4779 | LY_ERR |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4780 | yang_parse_submodule(struct ly_parser_ctx *context, const char *data, struct lysp_submodule **submod) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4781 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 4782 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4783 | char *word, *buf; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 4784 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4785 | enum yang_keyword kw; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4786 | struct lysp_submodule *mod_p = NULL; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4787 | |
| 4788 | /* "module"/"submodule" */ |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 4789 | ret = get_keyword(context, &data, &kw, &word, &word_len); |
| 4790 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4791 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4792 | if (kw == YANG_MODULE) { |
| 4793 | LOGERR(context->ctx, LY_EDENIED, "Input data contains module in situation when a submodule is expected."); |
| 4794 | ret = LY_EINVAL; |
| 4795 | goto cleanup; |
| 4796 | } else if (kw != YANG_SUBMODULE) { |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 4797 | LOGVAL_YANG(context, LYVE_SYNTAX, "Invalid keyword \"%s\", expected \"module\" or \"submodule\".", |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 4798 | ly_stmt2str(kw)); |
Radek Krejci | 40544fa | 2019-01-11 09:38:37 +0100 | [diff] [blame] | 4799 | ret = LY_EVALID; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 4800 | goto cleanup; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4801 | } |
| 4802 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4803 | mod_p = calloc(1, sizeof *mod_p); |
| 4804 | LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(context->ctx), cleanup); |
| 4805 | mod_p->parsing = 1; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4806 | |
| 4807 | /* substatements */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4808 | ret = parse_submodule(context, &data, mod_p); |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 4809 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4810 | |
| 4811 | /* read some trailing spaces or new lines */ |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 4812 | ret = get_argument(context, &data, Y_MAYBE_STR_ARG, &word, &buf, &word_len); |
| 4813 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4814 | |
| 4815 | if (word) { |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 4816 | 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] | 4817 | word_len, word); |
| 4818 | free(buf); |
Radek Krejci | 40544fa | 2019-01-11 09:38:37 +0100 | [diff] [blame] | 4819 | ret = LY_EVALID; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 4820 | goto cleanup; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4821 | } |
| 4822 | assert(!buf); |
| 4823 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4824 | mod_p->parsing = 0; |
| 4825 | *submod = mod_p; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4826 | |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 4827 | cleanup: |
| 4828 | if (ret) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4829 | lysp_submodule_free(context->ctx, mod_p); |
| 4830 | } |
| 4831 | |
| 4832 | return ret; |
| 4833 | } |
| 4834 | |
| 4835 | LY_ERR |
| 4836 | yang_parse_module(struct ly_parser_ctx *context, const char *data, struct lys_module *mod) |
| 4837 | { |
| 4838 | LY_ERR ret = LY_SUCCESS; |
| 4839 | char *word, *buf; |
| 4840 | size_t word_len; |
| 4841 | enum yang_keyword kw; |
| 4842 | struct lysp_module *mod_p = NULL; |
| 4843 | |
| 4844 | /* "module"/"submodule" */ |
| 4845 | ret = get_keyword(context, &data, &kw, &word, &word_len); |
| 4846 | LY_CHECK_GOTO(ret, cleanup); |
| 4847 | |
| 4848 | if (kw == YANG_SUBMODULE) { |
| 4849 | LOGERR(context->ctx, LY_EDENIED, "Input data contains submodule which cannot be parsed directly without its main module."); |
| 4850 | ret = LY_EINVAL; |
| 4851 | goto cleanup; |
| 4852 | } else if (kw != YANG_MODULE) { |
| 4853 | LOGVAL_YANG(context, LYVE_SYNTAX, "Invalid keyword \"%s\", expected \"module\" or \"submodule\".", |
| 4854 | ly_stmt2str(kw)); |
Radek Krejci | 40544fa | 2019-01-11 09:38:37 +0100 | [diff] [blame] | 4855 | ret = LY_EVALID; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4856 | goto cleanup; |
| 4857 | } |
| 4858 | |
| 4859 | mod_p = calloc(1, sizeof *mod_p); |
| 4860 | LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(context->ctx), cleanup); |
| 4861 | mod_p->mod = mod; |
| 4862 | mod_p->parsing = 1; |
| 4863 | |
| 4864 | /* substatements */ |
| 4865 | ret = parse_module(context, &data, mod_p); |
| 4866 | LY_CHECK_GOTO(ret, cleanup); |
| 4867 | |
| 4868 | /* read some trailing spaces or new lines */ |
| 4869 | ret = get_argument(context, &data, Y_MAYBE_STR_ARG, &word, &buf, &word_len); |
| 4870 | LY_CHECK_GOTO(ret, cleanup); |
| 4871 | |
| 4872 | if (word) { |
| 4873 | LOGVAL_YANG(context, LYVE_SYNTAX, "Invalid character sequence \"%.*s\", expected end-of-file.", |
| 4874 | word_len, word); |
| 4875 | free(buf); |
Radek Krejci | 40544fa | 2019-01-11 09:38:37 +0100 | [diff] [blame] | 4876 | ret = LY_EVALID; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4877 | goto cleanup; |
| 4878 | } |
| 4879 | assert(!buf); |
| 4880 | |
| 4881 | mod_p->parsing = 0; |
| 4882 | mod->parsed = mod_p; |
| 4883 | |
| 4884 | cleanup: |
| 4885 | if (ret) { |
| 4886 | lysp_module_free(mod_p); |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 4887 | } |
| 4888 | |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4889 | return ret; |
| 4890 | } |