Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @file parser_yang.c |
| 3 | * @author Michal Vasko <mvasko@cesnet.cz> |
| 4 | * @brief YANG parser |
| 5 | * |
| 6 | * Copyright (c) 2018 CESNET, z.s.p.o. |
| 7 | * |
| 8 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 9 | * You may not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
| 11 | * |
| 12 | * https://opensource.org/licenses/BSD-3-Clause |
| 13 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 14 | |
| 15 | #include "common.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 16 | |
| 17 | #include <assert.h> |
| 18 | #include <ctype.h> |
| 19 | #include <errno.h> |
| 20 | #include <stdint.h> |
| 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
| 24 | |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 25 | #include "context.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 26 | #include "dict.h" |
| 27 | #include "extensions.h" |
| 28 | #include "log.h" |
| 29 | #include "set.h" |
| 30 | #include "tree.h" |
| 31 | #include "tree_schema.h" |
Radek Krejci | 70853c5 | 2018-10-15 14:46:16 +0200 | [diff] [blame] | 32 | #include "tree_schema_internal.h" |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 33 | |
| 34 | /* Macro to check YANG's yang-char grammar rule */ |
| 35 | #define is_yangutf8char(c) ((c >= 0x20 && c <= 0xd77) || c == 0x09 || c == 0x0a || c == 0x0d || \ |
| 36 | (c >= 0xe000 && c <= 0xfdcf) || (c >= 0xfdf0 && c <= 0xfffd) || \ |
| 37 | (c >= 0x10000 && c <= 0x1fffd) || (c >= 0x20000 && c <= 0x2fffd) || \ |
| 38 | (c >= 0x30000 && c <= 0x3fffd) || (c >= 0x40000 && c <= 0x2fffd) || \ |
| 39 | (c >= 0x50000 && c <= 0x5fffd) || (c >= 0x60000 && c <= 0x6fffd) || \ |
| 40 | (c >= 0x70000 && c <= 0x7fffd) || (c >= 0x80000 && c <= 0x8fffd) || \ |
| 41 | (c >= 0x90000 && c <= 0x9fffd) || (c >= 0xa0000 && c <= 0xafffd) || \ |
| 42 | (c >= 0xb0000 && c <= 0xbfffd) || (c >= 0xc0000 && c <= 0xcfffd) || \ |
| 43 | (c >= 0xd0000 && c <= 0xdfffd) || (c >= 0xe0000 && c <= 0xefffd) || \ |
| 44 | (c >= 0xf0000 && c <= 0xffffd) || (c >= 0x100000 && c <= 0x10fffd)) |
| 45 | |
Radek Krejci | ceaf212 | 2019-01-02 15:03:26 +0100 | [diff] [blame] | 46 | /** |
| 47 | * @brief Try to find object with MEMBER string matching the IDENT in the given ARRAY. |
| 48 | * Macro logs an error message and returns LY_EVALID in case of existence of a matching object. |
| 49 | * |
| 50 | * @param[in] CTX yang parser context for logging. |
| 51 | * @param[in] ARRAY [sized array](@ref sizedarrays) of a generic objects with member named MEMBER to search. |
| 52 | * @param[in] MEMBER Name of the member of the objects in the ARRAY to compare. |
| 53 | * @param[in] STMT Name of the compared YANG statements for logging. |
| 54 | * @param[in] IDENT String trying to find in the ARRAY's objects inside the MEMBER member. |
| 55 | */ |
Radek Krejci | faa1eac | 2018-10-30 14:34:55 +0100 | [diff] [blame] | 56 | #define CHECK_UNIQUENESS(CTX, ARRAY, MEMBER, STMT, IDENT) \ |
| 57 | if (ARRAY) { \ |
| 58 | for (unsigned int u = 0; u < LY_ARRAY_SIZE(ARRAY) - 1; ++u) { \ |
| 59 | if (!strcmp((ARRAY)[u].MEMBER, IDENT)) { \ |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 60 | LOGVAL_PARSER(CTX, LY_VCODE_DUPIDENT, IDENT, STMT); \ |
Radek Krejci | faa1eac | 2018-10-30 14:34:55 +0100 | [diff] [blame] | 61 | return LY_EVALID; \ |
| 62 | } \ |
| 63 | } \ |
| 64 | } |
| 65 | |
Radek Krejci | ceaf212 | 2019-01-02 15:03:26 +0100 | [diff] [blame] | 66 | /** |
| 67 | * @brief Insert WORD into the libyang context's dictionary and store as TARGET. |
| 68 | * @param[in] CTX yang parser context to access libyang context. |
| 69 | * @param[in] BUF buffer in case the word is not a constant and can be inserted directly (zero-copy) |
| 70 | * @param[out] TARGET variable where to store the pointer to the inserted value. |
| 71 | * @param[in] WORD string to store. |
| 72 | * @param[in] LEN length of the string in WORD to store. |
| 73 | */ |
Radek Krejci | 9fcacc1 | 2018-10-11 15:59:11 +0200 | [diff] [blame] | 74 | #define INSERT_WORD(CTX, BUF, TARGET, WORD, LEN) \ |
| 75 | if (BUF) {(TARGET) = lydict_insert_zc((CTX)->ctx, WORD);}\ |
Radek Krejci | f09e4e8 | 2019-06-14 15:08:11 +0200 | [diff] [blame] | 76 | else {(TARGET) = lydict_insert((CTX)->ctx, LEN ? WORD : "", LEN);} |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 77 | |
Radek Krejci | ceaf212 | 2019-01-02 15:03:26 +0100 | [diff] [blame] | 78 | /** |
| 79 | * @brief Move the DATA pointer by COUNT items. Also updates the indent value in yang parser context |
| 80 | * @param[in] CTX yang parser context to update its indent value. |
| 81 | * @param[in,out] DATA pointer to move |
| 82 | * @param[in] COUNT number of items for which the DATA pointer is supposed to move on. |
| 83 | */ |
Radek Krejci | 2b61048 | 2019-01-02 13:36:09 +0100 | [diff] [blame] | 84 | #define MOVE_INPUT(CTX, DATA, COUNT) (*(DATA))+=COUNT;(CTX)->indent+=COUNT |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 85 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 86 | /** |
| 87 | * @brief Loop through all substatements providing, return if there are none. |
| 88 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 89 | * @param[in] CTX yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 90 | * @param[in] DATA Raw data to read from. |
| 91 | * @param[out] KW YANG keyword read. |
| 92 | * @param[out] WORD Pointer to the keyword itself. |
| 93 | * @param[out] WORD_LEN Length of the keyword. |
| 94 | * @param[out] ERR Variable for error storing. |
| 95 | * |
| 96 | * @return In case there are no substatements or a fatal error encountered. |
| 97 | */ |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 98 | #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] | 99 | LY_CHECK_RET(get_keyword(CTX, DATA, &KW, &WORD, &WORD_LEN)); \ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 100 | if (KW == YANG_SEMICOLON) { \ |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 101 | CHECKGOTO; \ |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 102 | return LY_SUCCESS; \ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 103 | } \ |
| 104 | if (KW != YANG_LEFT_BRACE) { \ |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 105 | LOGVAL_PARSER(CTX, LYVE_SYNTAX_YANG, "Invalid keyword \"%s\", expected \";\" or \"{\".", ly_stmt2str(KW)); \ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 106 | return LY_EVALID; \ |
| 107 | } \ |
| 108 | for (ERR = get_keyword(CTX, DATA, &KW, &WORD, &WORD_LEN); \ |
| 109 | !ERR && (KW != YANG_RIGHT_BRACE); \ |
| 110 | ERR = get_keyword(CTX, DATA, &KW, &WORD, &WORD_LEN)) |
| 111 | |
Radek Krejci | ceaf212 | 2019-01-02 15:03:26 +0100 | [diff] [blame] | 112 | /** |
| 113 | * @brief Check module version is at least 2 (YANG 1.1) because of the keyword presence. |
| 114 | * Logs error message and returns LY_EVALID in case of module in YANG version 1.0. |
| 115 | * @param[in] CTX yang parser context to get current module and for logging. |
| 116 | * @param[in] KW keyword allowed only in YANG version 1.1 (or later) - for logging. |
| 117 | * @param[in] PARENT parent statement where the KW is present - for logging. |
| 118 | */ |
| 119 | #define YANG_CHECK_STMTVER2_RET(CTX, KW, PARENT) \ |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 120 | if ((CTX)->mod_version < 2) {LOGVAL_PARSER((CTX), LY_VCODE_INCHILDSTMT2, KW, PARENT); return LY_EVALID;} |
Radek Krejci | 1011365 | 2018-11-14 16:56:50 +0100 | [diff] [blame] | 121 | |
Radek Krejci | f09e4e8 | 2019-06-14 15:08:11 +0200 | [diff] [blame] | 122 | #define YANG_CHECK_NONEMPTY(CTX, OBJECT, VALUE_LEN, STMT) \ |
| 123 | if (!VALUE_LEN) { \ |
| 124 | LOGWRN((CTX)->ctx, "Empty argument of %s statement does not make sense.", STMT); \ |
| 125 | } |
| 126 | |
Radek Krejci | 2d7a47b | 2019-05-16 13:34:10 +0200 | [diff] [blame] | 127 | LY_ERR parse_container(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings); |
| 128 | LY_ERR parse_uses(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings); |
| 129 | LY_ERR parse_choice(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings); |
| 130 | LY_ERR parse_case(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings); |
| 131 | LY_ERR parse_list(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings); |
| 132 | LY_ERR parse_grouping(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_grp **groupings); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 133 | |
Radek Krejci | 7fc6829 | 2019-06-12 13:51:09 +0200 | [diff] [blame] | 134 | static LY_ERR parse_finalize_reallocated(struct lys_parser_ctx *ctx, struct lysp_grp *groupings, struct lysp_augment *augments, |
| 135 | struct lysp_action *actions, struct lysp_notif *notifs); |
| 136 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 137 | /** |
| 138 | * @brief Add another character to dynamic buffer, a low-level function. |
| 139 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 140 | * Enlarge if needed. Updates \p input as well as \p buf_used. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 141 | * |
Radek Krejci | 404251e | 2018-10-09 12:06:44 +0200 | [diff] [blame] | 142 | * @param[in] ctx libyang context for logging. |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 143 | * @param[in, out] input Input string to process. |
| 144 | * @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] | 145 | * @param[in,out] buf Buffer to use, can be moved by realloc(). |
| 146 | * @param[in,out] buf_len Current size of the buffer. |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 147 | * @param[in,out] buf_used Currently used characters of the buffer. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 148 | * |
| 149 | * @return LY_ERR values. |
| 150 | */ |
Radek Krejci | 2d7a47b | 2019-05-16 13:34:10 +0200 | [diff] [blame] | 151 | LY_ERR |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 152 | 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] | 153 | { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 154 | if (*buf_len <= (*buf_used) + len) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 155 | *buf_len += 16; |
| 156 | *buf = ly_realloc(*buf, *buf_len); |
| 157 | LY_CHECK_ERR_RET(!*buf, LOGMEM(ctx), LY_EMEM); |
| 158 | } |
Radek Krejci | c091739 | 2019-04-10 13:04:04 +0200 | [diff] [blame] | 159 | if (*buf_used) { |
| 160 | memcpy(&(*buf)[*buf_used], *input, len); |
| 161 | } else { |
| 162 | memcpy(*buf, *input, len); |
| 163 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 164 | |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 165 | (*buf_used) += len; |
| 166 | (*input) += len; |
| 167 | |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 168 | return LY_SUCCESS; |
| 169 | } |
| 170 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 171 | /** |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 172 | * @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] | 173 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 174 | * @param[in] ctx yang parser context for logging. |
| 175 | * @param[in] c UTF8 code point of a character to check. |
| 176 | * @return LY_ERR values. |
| 177 | */ |
| 178 | static LY_ERR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 179 | check_stringchar(struct lys_parser_ctx *ctx, unsigned int c) |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 180 | { |
| 181 | if (!is_yangutf8char(c)) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 182 | LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, c); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 183 | return LY_EVALID; |
| 184 | } |
| 185 | return LY_SUCCESS; |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * @brief Check that \p c is valid UTF8 code point for YANG identifier. |
| 190 | * |
| 191 | * @param[in] ctx yang parser context for logging. |
| 192 | * @param[in] c UTF8 code point of a character to check. |
| 193 | * @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] | 194 | * @param[in,out] prefix Storage for internally used flag in case of possible prefixed identifiers: |
| 195 | * 0 - colon not yet found (no prefix) |
| 196 | * 1 - \p c is the colon character |
| 197 | * 2 - prefix already processed, now processing the identifier |
| 198 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 199 | * If the identifier cannot be prefixed, NULL is expected. |
| 200 | * @return LY_ERR values. |
| 201 | */ |
Radek Krejci | 2d7a47b | 2019-05-16 13:34:10 +0200 | [diff] [blame] | 202 | LY_ERR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 203 | check_identifierchar(struct lys_parser_ctx *ctx, unsigned int c, int first, int *prefix) |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 204 | { |
| 205 | if (first || (prefix && (*prefix) == 1)) { |
| 206 | if (!is_yangidentstartchar(c)) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 207 | LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid identifier first character '%c'.", c); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 208 | return LY_EVALID; |
| 209 | } |
Radek Krejci | 9fcacc1 | 2018-10-11 15:59:11 +0200 | [diff] [blame] | 210 | if (prefix) { |
| 211 | if (first) { |
| 212 | (*prefix) = 0; |
| 213 | } else { |
| 214 | (*prefix) = 2; |
| 215 | } |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 216 | } |
| 217 | } else if (c == ':' && prefix && (*prefix) == 0) { |
| 218 | (*prefix) = 1; |
| 219 | } else if (!is_yangidentchar(c)) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 220 | LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid identifier character '%c'.", c); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 221 | return LY_EVALID; |
| 222 | } |
| 223 | |
| 224 | return LY_SUCCESS; |
| 225 | } |
| 226 | |
| 227 | /** |
| 228 | * @brief Store a single UTF8 character. It depends whether in a dynamically-allocated buffer or just as a pointer to the data. |
| 229 | * |
| 230 | * @param[in] ctx yang parser context for logging. |
| 231 | * @param[in,out] input Pointer to the string where to get the character to store. Automatically moved to the next character |
| 232 | * when function returns. |
| 233 | * @param[in] arg Type of the input string to select method of checking character validity. |
| 234 | * @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] | 235 | * 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] | 236 | * @param[in,out] word_len Current length of the word pointed to by \p word_p. |
| 237 | * @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] | 238 | * @param[in,out] buf_len Current length of \p word_b. |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 239 | * @param[in] need_buf Flag if the dynamically allocated buffer is required. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 240 | * |
| 241 | * @return LY_ERR values. |
| 242 | */ |
Radek Krejci | 2d7a47b | 2019-05-16 13:34:10 +0200 | [diff] [blame] | 243 | LY_ERR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 244 | buf_store_char(struct lys_parser_ctx *ctx, const char **input, enum yang_arg arg, |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 245 | 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] | 246 | { |
Radek Krejci | 404251e | 2018-10-09 12:06:44 +0200 | [diff] [blame] | 247 | int prefix = 0; |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 248 | unsigned int c; |
| 249 | size_t len; |
| 250 | |
Radek Krejci | f29b7c3 | 2019-04-09 16:17:49 +0200 | [diff] [blame] | 251 | /* check valid combination of input paremeters - if need_buf specified, word_b must be provided */ |
| 252 | assert(!need_buf || (need_buf && word_b)); |
| 253 | |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 254 | /* get UTF8 code point (and number of bytes coding the character) */ |
| 255 | LY_CHECK_ERR_RET(ly_getutf8(input, &c, &len), |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 256 | LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, (*input)[-len]), LY_EVALID); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 257 | (*input) -= len; |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 258 | if (c == '\n') { |
| 259 | ctx->indent = 0; |
| 260 | } else { |
| 261 | /* note - even the multibyte character is count as 1 */ |
| 262 | ++ctx->indent; |
| 263 | } |
| 264 | |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 265 | /* check character validity */ |
| 266 | switch (arg) { |
| 267 | case Y_IDENTIF_ARG: |
| 268 | LY_CHECK_RET(check_identifierchar(ctx, c, !(*word_len), NULL)); |
| 269 | break; |
| 270 | case Y_PREF_IDENTIF_ARG: |
| 271 | LY_CHECK_RET(check_identifierchar(ctx, c, !(*word_len), &prefix)); |
| 272 | break; |
| 273 | case Y_STR_ARG: |
| 274 | case Y_MAYBE_STR_ARG: |
| 275 | LY_CHECK_RET(check_stringchar(ctx, c)); |
| 276 | break; |
| 277 | } |
| 278 | |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 279 | if (word_b && *word_b) { |
| 280 | /* add another character into buffer */ |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 281 | 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] | 282 | return LY_EMEM; |
| 283 | } |
| 284 | |
| 285 | /* in case of realloc */ |
| 286 | *word_p = *word_b; |
Radek Krejci | f29b7c3 | 2019-04-09 16:17:49 +0200 | [diff] [blame] | 287 | } else if (word_b && need_buf) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 288 | /* first time we need a buffer, copy everything read up to now */ |
| 289 | if (*word_len) { |
| 290 | *word_b = malloc(*word_len); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 291 | LY_CHECK_ERR_RET(!*word_b, LOGMEM(ctx->ctx), LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 292 | *buf_len = *word_len; |
| 293 | memcpy(*word_b, *word_p, *word_len); |
| 294 | } |
| 295 | |
| 296 | /* add this new character into buffer */ |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 297 | 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] | 298 | return LY_EMEM; |
| 299 | } |
| 300 | |
| 301 | /* in case of realloc */ |
| 302 | *word_p = *word_b; |
| 303 | } else { |
| 304 | /* just remember the first character pointer */ |
| 305 | if (!*word_p) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 306 | *word_p = (char *)(*input); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 307 | } |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 308 | /* ... and update the word's length */ |
| 309 | (*word_len) += len; |
| 310 | (*input) += len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | return LY_SUCCESS; |
| 314 | } |
| 315 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 316 | /** |
| 317 | * @brief Skip YANG comment in data. |
| 318 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 319 | * @param[in] ctx yang parser context for logging. |
| 320 | * @param[in,out] data Data to read from, automatically moved after the comment. |
| 321 | * @param[in] comment Type of the comment to process: |
| 322 | * 1 for a one-line comment, |
| 323 | * 2 for a block comment. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 324 | * |
| 325 | * @return LY_ERR values. |
| 326 | */ |
Radek Krejci | 2d7a47b | 2019-05-16 13:34:10 +0200 | [diff] [blame] | 327 | LY_ERR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 328 | skip_comment(struct lys_parser_ctx *ctx, const char **data, int comment) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 329 | { |
Radek Krejci | 80dd33e | 2018-09-26 15:57:18 +0200 | [diff] [blame] | 330 | /* internal statuses: 0 - comment ended, |
| 331 | * 1 - in line comment, |
| 332 | * 2 - in block comment, |
| 333 | * 3 - in block comment with last read character '*' |
| 334 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 335 | while (**data && comment) { |
| 336 | switch (comment) { |
| 337 | case 1: |
| 338 | if (**data == '\n') { |
| 339 | comment = 0; |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 340 | ++ctx->line; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 341 | } |
| 342 | break; |
| 343 | case 2: |
| 344 | if (**data == '*') { |
Radek Krejci | 15c80ca | 2018-10-09 11:01:31 +0200 | [diff] [blame] | 345 | comment = 3; |
| 346 | } else if (**data == '\n') { |
| 347 | ++ctx->line; |
| 348 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 349 | break; |
| 350 | case 3: |
| 351 | if (**data == '/') { |
| 352 | comment = 0; |
Radek Krejci | 5b93049 | 2019-06-11 14:54:08 +0200 | [diff] [blame] | 353 | } else if (**data != '*') { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 354 | if (**data == '\n') { |
| 355 | ++ctx->line; |
| 356 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 357 | comment = 2; |
| 358 | } |
| 359 | break; |
| 360 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 361 | LOGINT_RET(ctx->ctx); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 362 | } |
| 363 | |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 364 | if (**data == '\n') { |
| 365 | ctx->indent = 0; |
| 366 | } else { |
| 367 | ++ctx->indent; |
| 368 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 369 | ++(*data); |
| 370 | } |
| 371 | |
| 372 | if (!**data && (comment > 1)) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 373 | LOGVAL_PARSER(ctx, LYVE_SYNTAX, "Unexpected end-of-input, non-terminated comment."); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 374 | return LY_EVALID; |
| 375 | } |
| 376 | |
| 377 | return LY_SUCCESS; |
| 378 | } |
| 379 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 380 | /** |
| 381 | * @brief Read a quoted string from data. |
| 382 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 383 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 384 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 385 | * @param[in] arg Type of YANG keyword argument expected. |
| 386 | * @param[out] word_p Pointer to the read quoted string. |
| 387 | * @param[out] word_b Pointer to a dynamically-allocated buffer holding the read quoted string. If not needed, |
| 388 | * set to NULL. Otherwise equal to \p word_p. |
| 389 | * @param[out] word_len Length of the read quoted string. |
| 390 | * @param[out] buf_len Length of the dynamically-allocated buffer \p word_b. |
| 391 | * @param[in] indent Current indent (number of YANG spaces). Needed for correct multi-line string |
| 392 | * indenation in the final quoted string. |
| 393 | * |
| 394 | * @return LY_ERR values. |
| 395 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 396 | static LY_ERR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 397 | read_qstring(struct lys_parser_ctx *ctx, const char **data, enum yang_arg arg, char **word_p, char **word_b, size_t *word_len, |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 398 | size_t *buf_len) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 399 | { |
| 400 | /* string: 0 - string ended, 1 - string with ', 2 - string with ", 3 - string with " with last character \, |
| 401 | * 4 - string finished, now skipping whitespaces looking for +, |
| 402 | * 5 - string continues after +, skipping whitespaces */ |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 403 | unsigned int string, block_indent = 0, current_indent = 0, need_buf = 0; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 404 | const char *c; |
| 405 | |
| 406 | if (**data == '\"') { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 407 | string = 2; |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 408 | current_indent = block_indent = ctx->indent + 1; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 409 | } else { |
| 410 | assert(**data == '\''); |
| 411 | string = 1; |
| 412 | } |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 413 | MOVE_INPUT(ctx, data, 1); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 414 | |
| 415 | while (**data && string) { |
| 416 | switch (string) { |
| 417 | case 1: |
| 418 | switch (**data) { |
| 419 | case '\'': |
| 420 | /* string may be finished, but check for + */ |
| 421 | string = 4; |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 422 | MOVE_INPUT(ctx, data, 1); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 423 | break; |
| 424 | default: |
| 425 | /* check and store character */ |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 426 | 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] | 427 | break; |
| 428 | } |
| 429 | break; |
| 430 | case 2: |
| 431 | switch (**data) { |
| 432 | case '\"': |
| 433 | /* string may be finished, but check for + */ |
| 434 | string = 4; |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 435 | MOVE_INPUT(ctx, data, 1); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 436 | break; |
| 437 | case '\\': |
| 438 | /* special character following */ |
| 439 | string = 3; |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 440 | ++(*data); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 441 | break; |
| 442 | case ' ': |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 443 | if (current_indent < block_indent) { |
| 444 | ++current_indent; |
| 445 | MOVE_INPUT(ctx, data, 1); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 446 | } else { |
| 447 | /* check and store character */ |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 448 | 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] | 449 | } |
| 450 | break; |
| 451 | case '\t': |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 452 | if (current_indent < block_indent) { |
| 453 | assert(need_buf); |
| 454 | current_indent += 8; |
| 455 | ctx->indent += 8; |
| 456 | for (; current_indent > block_indent; --current_indent, --ctx->indent) { |
| 457 | /* store leftover spaces from the tab */ |
| 458 | c = " "; |
| 459 | 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] | 460 | } |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 461 | ++(*data); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 462 | } else { |
| 463 | /* check and store character */ |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 464 | 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] | 465 | /* additional characters for indentation - only 1 was count in buf_store_char */ |
| 466 | ctx->indent += 7; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 467 | } |
| 468 | break; |
| 469 | case '\n': |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 470 | if (block_indent) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 471 | /* we will be removing the indents so we need our own buffer */ |
| 472 | need_buf = 1; |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 473 | |
| 474 | /* remove trailing tabs and spaces */ |
| 475 | while ((*word_len) && ((*word_p)[(*word_len) - 1] == '\t' || (*word_p)[(*word_len) - 1] == ' ')) { |
| 476 | --(*word_len); |
| 477 | } |
| 478 | |
| 479 | /* start indentation */ |
| 480 | current_indent = 0; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | /* check and store character */ |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 484 | LY_CHECK_RET(buf_store_char(ctx, data, arg, word_p, word_len, word_b, buf_len, need_buf)); |
| 485 | |
| 486 | /* maintain line number */ |
| 487 | ++ctx->line; |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 488 | |
| 489 | /* reset context indentation counter for possible string after this one */ |
| 490 | ctx->indent = 0; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 491 | break; |
| 492 | default: |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 493 | /* first non-whitespace character, stop eating indentation */ |
| 494 | current_indent = block_indent; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 495 | |
| 496 | /* check and store character */ |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 497 | 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] | 498 | break; |
| 499 | } |
| 500 | break; |
| 501 | case 3: |
| 502 | /* string encoded characters */ |
| 503 | switch (**data) { |
| 504 | case 'n': |
| 505 | c = "\n"; |
| 506 | break; |
| 507 | case 't': |
| 508 | c = "\t"; |
| 509 | break; |
| 510 | case '\"': |
| 511 | c = *data; |
| 512 | break; |
| 513 | case '\\': |
| 514 | c = *data; |
| 515 | break; |
| 516 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 517 | LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Double-quoted string unknown special character '\\%c'.", **data); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 518 | return LY_EVALID; |
| 519 | } |
| 520 | |
| 521 | /* check and store character */ |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 522 | 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] | 523 | |
| 524 | string = 2; |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 525 | ++(*data); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 526 | break; |
| 527 | case 4: |
| 528 | switch (**data) { |
| 529 | case '+': |
| 530 | /* string continues */ |
| 531 | string = 5; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 532 | need_buf = 1; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 533 | break; |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 534 | case '\n': |
| 535 | ++ctx->line; |
| 536 | /* fallthrough */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 537 | case ' ': |
| 538 | case '\t': |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 539 | /* just skip */ |
| 540 | break; |
| 541 | default: |
| 542 | /* string is finished */ |
| 543 | goto string_end; |
| 544 | } |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 545 | MOVE_INPUT(ctx, data, 1); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 546 | break; |
| 547 | case 5: |
| 548 | switch (**data) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 549 | case '\n': |
| 550 | ++ctx->line; |
| 551 | /* fallthrough */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 552 | case ' ': |
| 553 | case '\t': |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 554 | /* skip */ |
| 555 | break; |
| 556 | case '\'': |
| 557 | string = 1; |
| 558 | break; |
| 559 | case '\"': |
| 560 | string = 2; |
| 561 | break; |
| 562 | default: |
| 563 | /* it must be quoted again */ |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 564 | LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Both string parts divided by '+' must be quoted."); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 565 | return LY_EVALID; |
| 566 | } |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 567 | MOVE_INPUT(ctx, data, 1); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 568 | break; |
| 569 | default: |
| 570 | return LY_EINT; |
| 571 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 572 | } |
| 573 | |
| 574 | string_end: |
Radek Krejci | 4e199f5 | 2019-05-28 09:09:28 +0200 | [diff] [blame] | 575 | if (arg <= Y_PREF_IDENTIF_ARG && !(*word_len)) { |
| 576 | /* empty identifier */ |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 577 | LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Statement argument is required."); |
Radek Krejci | 4e199f5 | 2019-05-28 09:09:28 +0200 | [diff] [blame] | 578 | return LY_EVALID; |
| 579 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 580 | return LY_SUCCESS; |
| 581 | } |
| 582 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 583 | /** |
| 584 | * @brief Get another YANG string from the raw data. |
| 585 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 586 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 587 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 588 | * @param[in] arg Type of YANG keyword argument expected. |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 589 | * @param[out] flags optional output argument to get flag of the argument's quoting (LYS_*QOUTED - see [schema node flags](@ref snodeflags)) |
Michal Vasko | 2ca70f5 | 2018-09-27 11:04:51 +0200 | [diff] [blame] | 590 | * @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] | 591 | * @param[out] word_b Pointer to a dynamically-allocated buffer holding the read string. If not needed, |
| 592 | * set to NULL. Otherwise equal to \p word_p. |
| 593 | * @param[out] word_len Length of the read string. |
| 594 | * |
| 595 | * @return LY_ERR values. |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 596 | */ |
Radek Krejci | 2d7a47b | 2019-05-16 13:34:10 +0200 | [diff] [blame] | 597 | LY_ERR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 598 | get_argument(struct lys_parser_ctx *ctx, const char **data, enum yang_arg arg, |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 599 | uint16_t *flags, char **word_p, char **word_b, size_t *word_len) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 600 | { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 601 | size_t buf_len = 0; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 602 | |
| 603 | /* word buffer - dynamically allocated */ |
| 604 | *word_b = NULL; |
| 605 | |
| 606 | /* word pointer - just a pointer to data */ |
| 607 | *word_p = NULL; |
| 608 | |
| 609 | *word_len = 0; |
| 610 | while (**data) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 611 | switch (**data) { |
| 612 | case '\'': |
| 613 | case '\"': |
| 614 | if (*word_len) { |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 615 | /* invalid - quotes cannot be in unquoted string and only optsep, ; or { can follow it */ |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 616 | LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, 1, *data, |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 617 | "unquoted string character, optsep, semicolon or opening brace"); |
| 618 | return LY_EVALID; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 619 | } |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 620 | if (flags) { |
| 621 | (*flags) |= (**data) == '\'' ? LYS_SINGLEQUOTED : LYS_DOUBLEQUOTED; |
| 622 | } |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 623 | 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] | 624 | goto str_end; |
| 625 | case '/': |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 626 | if ((*data)[1] == '/') { |
| 627 | /* one-line comment */ |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 628 | MOVE_INPUT(ctx, data, 2); |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 629 | LY_CHECK_RET(skip_comment(ctx, data, 1)); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 630 | } else if ((*data)[1] == '*') { |
| 631 | /* block comment */ |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 632 | MOVE_INPUT(ctx, data, 2); |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 633 | LY_CHECK_RET(skip_comment(ctx, data, 2)); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 634 | } else { |
| 635 | /* not a comment after all */ |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 636 | 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] | 637 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 638 | break; |
| 639 | case ' ': |
| 640 | if (*word_len) { |
| 641 | /* word is finished */ |
| 642 | goto str_end; |
| 643 | } |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 644 | MOVE_INPUT(ctx, data, 1); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 645 | break; |
| 646 | case '\t': |
| 647 | if (*word_len) { |
| 648 | /* word is finished */ |
| 649 | goto str_end; |
| 650 | } |
| 651 | /* tabs count for 8 spaces */ |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 652 | ctx->indent += 8; |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 653 | |
| 654 | ++(*data); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 655 | break; |
| 656 | case '\n': |
| 657 | if (*word_len) { |
| 658 | /* word is finished */ |
| 659 | goto str_end; |
| 660 | } |
| 661 | /* reset indent */ |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 662 | ctx->indent = 0; |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 663 | |
| 664 | /* track line numbers */ |
| 665 | ++ctx->line; |
| 666 | |
| 667 | ++(*data); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 668 | break; |
| 669 | case ';': |
| 670 | case '{': |
| 671 | if (*word_len || (arg == Y_MAYBE_STR_ARG)) { |
| 672 | /* word is finished */ |
| 673 | goto str_end; |
| 674 | } |
| 675 | |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 676 | LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, 1, *data, "an argument"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 677 | return LY_EVALID; |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 678 | case '}': |
| 679 | /* invalid - braces cannot be in unquoted string (opening braces terminates the string and can follow it) */ |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 680 | LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, 1, *data, |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 681 | "unquoted string character, optsep, semicolon or opening brace"); |
| 682 | return LY_EVALID; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 683 | default: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 684 | 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] | 685 | break; |
| 686 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 687 | } |
| 688 | |
Radek Krejci | 0a1d0d4 | 2019-05-16 15:14:51 +0200 | [diff] [blame] | 689 | /* unexpected end of loop */ |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 690 | LOGVAL_PARSER(ctx, LY_VCODE_EOF); |
Radek Krejci | 0a1d0d4 | 2019-05-16 15:14:51 +0200 | [diff] [blame] | 691 | return LY_EVALID; |
| 692 | |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 693 | str_end: |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 694 | /* terminating NULL byte for buf */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 695 | if (*word_b) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 696 | (*word_b) = ly_realloc(*word_b, (*word_len) + 1); |
| 697 | LY_CHECK_ERR_RET(!(*word_b), LOGMEM(ctx->ctx), LY_EMEM); |
| 698 | (*word_b)[*word_len] = '\0'; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 699 | *word_p = *word_b; |
| 700 | } |
| 701 | |
| 702 | return LY_SUCCESS; |
| 703 | } |
| 704 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 705 | /** |
| 706 | * @brief Get another YANG keyword from the raw data. |
| 707 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 708 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 709 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 710 | * @param[out] kw YANG keyword read. |
| 711 | * @param[out] word_p Pointer to the keyword in the data. Useful for extension instances. |
| 712 | * @param[out] word_len Length of the keyword in the data. Useful for extension instances. |
| 713 | * |
| 714 | * @return LY_ERR values. |
| 715 | */ |
Radek Krejci | 2d7a47b | 2019-05-16 13:34:10 +0200 | [diff] [blame] | 716 | LY_ERR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 717 | get_keyword(struct lys_parser_ctx *ctx, const char **data, enum yang_keyword *kw, char **word_p, size_t *word_len) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 718 | { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 719 | int prefix; |
| 720 | const char *word_start; |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 721 | unsigned int c; |
| 722 | size_t len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 723 | |
| 724 | if (word_p) { |
| 725 | *word_p = NULL; |
| 726 | *word_len = 0; |
| 727 | } |
| 728 | |
| 729 | /* first skip "optsep", comments */ |
| 730 | while (**data) { |
Radek Krejci | dcc7b32 | 2018-10-11 14:24:02 +0200 | [diff] [blame] | 731 | switch (**data) { |
| 732 | case '/': |
| 733 | if ((*data)[1] == '/') { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 734 | /* one-line comment */ |
Radek Krejci | dcc7b32 | 2018-10-11 14:24:02 +0200 | [diff] [blame] | 735 | MOVE_INPUT(ctx, data, 2); |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 736 | LY_CHECK_RET(skip_comment(ctx, data, 1)); |
Radek Krejci | dcc7b32 | 2018-10-11 14:24:02 +0200 | [diff] [blame] | 737 | } else if ((*data)[1] == '*') { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 738 | /* block comment */ |
Radek Krejci | dcc7b32 | 2018-10-11 14:24:02 +0200 | [diff] [blame] | 739 | MOVE_INPUT(ctx, data, 2); |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 740 | LY_CHECK_RET(skip_comment(ctx, data, 2)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 741 | } else { |
Radek Krejci | dcc7b32 | 2018-10-11 14:24:02 +0200 | [diff] [blame] | 742 | /* error - not a comment after all, keyword cannot start with slash */ |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 743 | LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid identifier first character '/'."); |
Radek Krejci | dcc7b32 | 2018-10-11 14:24:02 +0200 | [diff] [blame] | 744 | return LY_EVALID; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 745 | } |
Radek Krejci | 1302828 | 2019-06-11 14:56:48 +0200 | [diff] [blame] | 746 | continue; |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 747 | case '\n': |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 748 | /* skip whitespaces (optsep) */ |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 749 | ++ctx->line; |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 750 | ctx->indent = 0; |
| 751 | break; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 752 | case ' ': |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 753 | /* skip whitespaces (optsep) */ |
| 754 | ++ctx->indent; |
| 755 | break; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 756 | case '\t': |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 757 | /* skip whitespaces (optsep) */ |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 758 | ctx->indent += 8; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 759 | break; |
| 760 | default: |
| 761 | /* either a keyword start or an invalid character */ |
| 762 | goto keyword_start; |
| 763 | } |
| 764 | |
| 765 | ++(*data); |
| 766 | } |
| 767 | |
| 768 | keyword_start: |
| 769 | word_start = *data; |
David Sedlák | 5f8f033 | 2019-06-18 16:34:30 +0200 | [diff] [blame] | 770 | *kw = lysp_match_kw(ctx, data); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 771 | |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 772 | if (*kw == YANG_SEMICOLON || *kw == YANG_LEFT_BRACE || *kw == YANG_RIGHT_BRACE) { |
Radek Krejci | 626df48 | 2018-10-11 15:06:31 +0200 | [diff] [blame] | 773 | goto success; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 774 | } |
| 775 | |
| 776 | if (*kw != YANG_NONE) { |
| 777 | /* make sure we have the whole keyword */ |
| 778 | switch (**data) { |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 779 | case '\n': |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 780 | case '\t': |
Radek Krejci | abdd806 | 2019-06-11 16:44:19 +0200 | [diff] [blame] | 781 | case ' ': |
| 782 | /* mandatory "sep" is just checked, not eaten so nothing in the context is updated */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 783 | break; |
Radek Krejci | 156ccaf | 2018-10-15 15:49:17 +0200 | [diff] [blame] | 784 | case ':': |
| 785 | /* keyword is not actually a keyword, but prefix of an extension. |
| 786 | * To avoid repeated check of the prefix syntax, move to the point where the colon was read |
| 787 | * and we will be checking the keyword (extension instance) itself */ |
| 788 | prefix = 1; |
| 789 | MOVE_INPUT(ctx, data, 1); |
| 790 | goto extension; |
Radek Krejci | dcc7b32 | 2018-10-11 14:24:02 +0200 | [diff] [blame] | 791 | case '{': |
| 792 | /* allowed only for input and output statements which can be without arguments */ |
| 793 | if (*kw == YANG_INPUT || *kw == YANG_OUTPUT) { |
| 794 | break; |
| 795 | } |
| 796 | /* fallthrough */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 797 | default: |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 798 | MOVE_INPUT(ctx, data, 1); |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 799 | LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, (int)(*data - word_start), word_start, |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 800 | "a keyword followed by a separator"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 801 | return LY_EVALID; |
| 802 | } |
| 803 | } else { |
| 804 | /* still can be an extension */ |
| 805 | prefix = 0; |
Radek Krejci | 156ccaf | 2018-10-15 15:49:17 +0200 | [diff] [blame] | 806 | extension: |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 807 | while (**data && (**data != ' ') && (**data != '\t') && (**data != '\n') && (**data != '{') && (**data != ';')) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 808 | LY_CHECK_ERR_RET(ly_getutf8(data, &c, &len), |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 809 | LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, (*data)[-len]), LY_EVALID); |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 810 | ++ctx->indent; |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 811 | /* check character validity */ |
| 812 | 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] | 813 | } |
| 814 | if (!**data) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 815 | LOGVAL_PARSER(ctx, LY_VCODE_EOF); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 816 | return LY_EVALID; |
| 817 | } |
| 818 | |
| 819 | /* prefix is mandatory for extension instances */ |
Radek Krejci | dcc7b32 | 2018-10-11 14:24:02 +0200 | [diff] [blame] | 820 | if (prefix != 2) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 821 | LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, (int)(*data - word_start), word_start, "a keyword"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 822 | return LY_EVALID; |
| 823 | } |
| 824 | |
| 825 | *kw = YANG_CUSTOM; |
| 826 | } |
Radek Krejci | 626df48 | 2018-10-11 15:06:31 +0200 | [diff] [blame] | 827 | success: |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 828 | if (word_p) { |
| 829 | *word_p = (char *)word_start; |
| 830 | *word_len = *data - word_start; |
| 831 | } |
| 832 | |
| 833 | return LY_SUCCESS; |
| 834 | } |
| 835 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 836 | /** |
| 837 | * @brief Parse extension instance substatements. |
| 838 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 839 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 840 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 841 | * @param[in] word Extension instance substatement name (keyword). |
| 842 | * @param[in] word_len Extension instance substatement name length. |
| 843 | * @param[in,out] child Children of this extension instance to add to. |
| 844 | * |
| 845 | * @return LY_ERR values. |
| 846 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 847 | static LY_ERR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 848 | parse_ext_substmt(struct lys_parser_ctx *ctx, const char **data, char *word, size_t word_len, |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 849 | struct lysp_stmt **child) |
| 850 | { |
| 851 | char *buf; |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 852 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 853 | enum yang_keyword kw; |
| 854 | struct lysp_stmt *stmt, *par_child; |
| 855 | |
| 856 | stmt = calloc(1, sizeof *stmt); |
| 857 | LY_CHECK_ERR_RET(!stmt, LOGMEM(NULL), LY_EMEM); |
| 858 | |
Radek Krejci | bb9b198 | 2019-04-08 14:24:59 +0200 | [diff] [blame] | 859 | /* insert into parent statements */ |
| 860 | if (!*child) { |
| 861 | *child = stmt; |
| 862 | } else { |
| 863 | for (par_child = *child; par_child->next; par_child = par_child->next); |
| 864 | par_child->next = stmt; |
| 865 | } |
| 866 | |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 867 | stmt->stmt = lydict_insert(ctx->ctx, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 868 | |
| 869 | /* get optional argument */ |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 870 | LY_CHECK_RET(get_argument(ctx, data, Y_MAYBE_STR_ARG, &stmt->flags, &word, &buf, &word_len)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 871 | |
Radek Krejci | 0ae092d | 2018-09-20 16:43:19 +0200 | [diff] [blame] | 872 | if (word) { |
| 873 | if (buf) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 874 | stmt->arg = lydict_insert_zc(ctx->ctx, word); |
Radek Krejci | 0ae092d | 2018-09-20 16:43:19 +0200 | [diff] [blame] | 875 | } else { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 876 | stmt->arg = lydict_insert(ctx->ctx, word, word_len); |
Radek Krejci | 0ae092d | 2018-09-20 16:43:19 +0200 | [diff] [blame] | 877 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 878 | } |
| 879 | |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 880 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, ) { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 881 | 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] | 882 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 883 | return ret; |
| 884 | } |
| 885 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 886 | /** |
| 887 | * @brief Parse extension instance. |
| 888 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 889 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 890 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 891 | * @param[in] ext_name Extension instance substatement name (keyword). |
| 892 | * @param[in] ext_name_len Extension instance substatement name length. |
| 893 | * @param[in] insubstmt Type of the keyword this extension instance is a substatement of. |
| 894 | * @param[in] insubstmt_index Index of the keyword instance this extension instance is a substatement of. |
| 895 | * @param[in,out] exts Extension instances to add to. |
| 896 | * |
| 897 | * @return LY_ERR values. |
| 898 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 899 | static LY_ERR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 900 | parse_ext(struct lys_parser_ctx *ctx, const char **data, const char *ext_name, int ext_name_len, LYEXT_SUBSTMT insubstmt, |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 901 | uint32_t insubstmt_index, struct lysp_ext_instance **exts) |
| 902 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 903 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 904 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 905 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 906 | struct lysp_ext_instance *e; |
| 907 | enum yang_keyword kw; |
| 908 | |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 909 | LY_ARRAY_NEW_RET(ctx->ctx, *exts, e, LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 910 | |
| 911 | /* store name and insubstmt info */ |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 912 | e->name = lydict_insert(ctx->ctx, ext_name, ext_name_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 913 | e->insubstmt = insubstmt; |
| 914 | e->insubstmt_index = insubstmt_index; |
| 915 | |
| 916 | /* get optional argument */ |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 917 | LY_CHECK_RET(get_argument(ctx, data, Y_MAYBE_STR_ARG, NULL, &word, &buf, &word_len)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 918 | |
Radek Krejci | 0ae092d | 2018-09-20 16:43:19 +0200 | [diff] [blame] | 919 | if (word) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 920 | INSERT_WORD(ctx, buf, e->argument, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 921 | } |
| 922 | |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 923 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 924 | 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] | 925 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 926 | return ret; |
| 927 | } |
| 928 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 929 | /** |
| 930 | * @brief Parse a generic text field without specific constraints. Those are contact, organization, |
| 931 | * description, etc... |
| 932 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 933 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 934 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 935 | * @param[in] substmt Type of this substatement. |
| 936 | * @param[in] substmt_index Index of this substatement. |
| 937 | * @param[in,out] value Place to store the parsed value. |
| 938 | * @param[in] arg Type of the YANG keyword argument (of the value). |
| 939 | * @param[in,out] exts Extension instances to add to. |
| 940 | * |
| 941 | * @return LY_ERR values. |
| 942 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 943 | static LY_ERR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 944 | parse_text_field(struct lys_parser_ctx *ctx, const char **data, LYEXT_SUBSTMT substmt, uint32_t substmt_index, |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 945 | const char **value, enum yang_arg arg, struct lysp_ext_instance **exts) |
| 946 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 947 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 948 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 949 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 950 | enum yang_keyword kw; |
| 951 | |
| 952 | if (*value) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 953 | LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, lyext_substmt2str(substmt)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 954 | return LY_EVALID; |
| 955 | } |
| 956 | |
| 957 | /* get value */ |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 958 | LY_CHECK_RET(get_argument(ctx, data, arg, NULL, &word, &buf, &word_len)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 959 | |
| 960 | /* store value and spend buf if allocated */ |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 961 | INSERT_WORD(ctx, buf, *value, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 962 | |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 963 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 964 | switch (kw) { |
| 965 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 966 | 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] | 967 | break; |
| 968 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 969 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), lyext_substmt2str(substmt)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 970 | return LY_EVALID; |
| 971 | } |
| 972 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 973 | return ret; |
| 974 | } |
| 975 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 976 | /** |
| 977 | * @brief Parse the yang-version statement. |
| 978 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 979 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 980 | * @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] | 981 | * @param[out] version Storage for the parsed information. |
| 982 | * @param[in, out] exts Extension instances to add to. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 983 | * |
| 984 | * @return LY_ERR values. |
| 985 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 986 | static LY_ERR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 987 | parse_yangversion(struct lys_parser_ctx *ctx, const char **data, uint8_t *version, struct lysp_ext_instance **exts) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 988 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 989 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 990 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 991 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 992 | enum yang_keyword kw; |
| 993 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 994 | if (*version) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 995 | LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "yang-version"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 996 | return LY_EVALID; |
| 997 | } |
| 998 | |
| 999 | /* get value */ |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 1000 | LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1001 | |
| 1002 | if ((word_len == 3) && !strncmp(word, "1.0", word_len)) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1003 | *version = LYS_VERSION_1_0; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1004 | } else if ((word_len == 3) && !strncmp(word, "1.1", word_len)) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1005 | *version = LYS_VERSION_1_1; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1006 | } else { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 1007 | LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "yang-version"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1008 | free(buf); |
| 1009 | return LY_EVALID; |
| 1010 | } |
| 1011 | free(buf); |
| 1012 | |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 1013 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1014 | switch (kw) { |
| 1015 | case YANG_CUSTOM: |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1016 | 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] | 1017 | break; |
| 1018 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 1019 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "yang-version"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1020 | return LY_EVALID; |
| 1021 | } |
| 1022 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1023 | return ret; |
| 1024 | } |
| 1025 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1026 | /** |
| 1027 | * @brief Parse the belongs-to statement. |
| 1028 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1029 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1030 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 1031 | * @param[in,out] belongsto Place to store the parsed value. |
| 1032 | * @param[in,out] prefix Place to store the parsed belongs-to prefix value. |
| 1033 | * @param[in,out] exts Extension instances to add to. |
| 1034 | * |
| 1035 | * @return LY_ERR values. |
| 1036 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1037 | static LY_ERR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1038 | parse_belongsto(struct lys_parser_ctx *ctx, const char **data, const char **belongsto, const char **prefix, struct lysp_ext_instance **exts) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1039 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1040 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1041 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 1042 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1043 | enum yang_keyword kw; |
| 1044 | |
| 1045 | if (*belongsto) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 1046 | LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "belongs-to"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1047 | return LY_EVALID; |
| 1048 | } |
| 1049 | |
| 1050 | /* get value */ |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 1051 | LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1052 | |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1053 | INSERT_WORD(ctx, buf, *belongsto, word, word_len); |
Radek Krejci | f09e4e8 | 2019-06-14 15:08:11 +0200 | [diff] [blame] | 1054 | |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 1055 | 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] | 1056 | switch (kw) { |
| 1057 | case YANG_PREFIX: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1058 | 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] | 1059 | break; |
| 1060 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1061 | 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] | 1062 | break; |
| 1063 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 1064 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "belongs-to"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1065 | return LY_EVALID; |
| 1066 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1067 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1068 | LY_CHECK_RET(ret); |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 1069 | checks: |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1070 | /* mandatory substatements */ |
| 1071 | if (!*prefix) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 1072 | LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "belongs-to"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1073 | return LY_EVALID; |
| 1074 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1075 | return ret; |
| 1076 | } |
| 1077 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1078 | /** |
| 1079 | * @brief Parse the revision-date statement. |
| 1080 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1081 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1082 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 1083 | * @param[in,out] rev Array to store the parsed value in. |
| 1084 | * @param[in,out] exts Extension instances to add to. |
| 1085 | * |
| 1086 | * @return LY_ERR values. |
| 1087 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1088 | static LY_ERR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1089 | parse_revisiondate(struct lys_parser_ctx *ctx, const char **data, char *rev, struct lysp_ext_instance **exts) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1090 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1091 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1092 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 1093 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1094 | enum yang_keyword kw; |
| 1095 | |
| 1096 | if (rev[0]) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 1097 | LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "revision-date"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1098 | return LY_EVALID; |
| 1099 | } |
| 1100 | |
| 1101 | /* get value */ |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 1102 | LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1103 | |
| 1104 | /* check value */ |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1105 | if (lysp_check_date(ctx, word, word_len, "revision-date")) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1106 | free(buf); |
| 1107 | return LY_EVALID; |
| 1108 | } |
| 1109 | |
| 1110 | /* store value and spend buf if allocated */ |
| 1111 | strncpy(rev, word, word_len); |
| 1112 | free(buf); |
| 1113 | |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 1114 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1115 | switch (kw) { |
| 1116 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1117 | 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] | 1118 | break; |
| 1119 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 1120 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "revision-date"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1121 | return LY_EVALID; |
| 1122 | } |
| 1123 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1124 | return ret; |
| 1125 | } |
| 1126 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1127 | /** |
| 1128 | * @brief Parse the include statement. |
| 1129 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1130 | * @param[in] ctx yang parser context for logging. |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1131 | * @param[in] module_name Name of the module to check name collisions. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1132 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 1133 | * @param[in,out] includes Parsed includes to add to. |
| 1134 | * |
| 1135 | * @return LY_ERR values. |
| 1136 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1137 | static LY_ERR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1138 | parse_include(struct lys_parser_ctx *ctx, const char *module_name, const char **data, struct lysp_include **includes) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1139 | { |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 1140 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1141 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 1142 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1143 | enum yang_keyword kw; |
| 1144 | struct lysp_include *inc; |
| 1145 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1146 | LY_ARRAY_NEW_RET(ctx->ctx, *includes, inc, LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1147 | |
| 1148 | /* get value */ |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 1149 | LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1150 | |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 1151 | INSERT_WORD(ctx, buf, inc->name, word, word_len); |
| 1152 | |
Radek Krejci | faa1eac | 2018-10-30 14:34:55 +0100 | [diff] [blame] | 1153 | /* submodules share the namespace with the module names, so there must not be |
| 1154 | * 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] | 1155 | if (!strcmp(module_name, inc->name) || ly_ctx_get_module_latest(ctx->ctx, inc->name)) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 1156 | LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Name collision between module and submodule of name \"%s\".", inc->name); |
Radek Krejci | faa1eac | 2018-10-30 14:34:55 +0100 | [diff] [blame] | 1157 | return LY_EVALID; |
| 1158 | } |
| 1159 | |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 1160 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1161 | switch (kw) { |
| 1162 | case YANG_DESCRIPTION: |
Radek Krejci | ceaf212 | 2019-01-02 15:03:26 +0100 | [diff] [blame] | 1163 | YANG_CHECK_STMTVER2_RET(ctx, "description", "include"); |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1164 | 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] | 1165 | break; |
| 1166 | case YANG_REFERENCE: |
Radek Krejci | ceaf212 | 2019-01-02 15:03:26 +0100 | [diff] [blame] | 1167 | YANG_CHECK_STMTVER2_RET(ctx, "reference", "include"); |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1168 | 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] | 1169 | break; |
| 1170 | case YANG_REVISION_DATE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1171 | LY_CHECK_RET(parse_revisiondate(ctx, data, inc->rev, &inc->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1172 | break; |
| 1173 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1174 | 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] | 1175 | break; |
| 1176 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 1177 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "include"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1178 | return LY_EVALID; |
| 1179 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1180 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1181 | return ret; |
| 1182 | } |
| 1183 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1184 | /** |
| 1185 | * @brief Parse the import statement. |
| 1186 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1187 | * @param[in] ctx yang parser context for logging. |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1188 | * @param[in] module_prefix Prefix of the module to check prefix collisions. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1189 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 1190 | * @param[in,out] imports Parsed imports to add to. |
| 1191 | * |
| 1192 | * @return LY_ERR values. |
| 1193 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1194 | static LY_ERR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1195 | parse_import(struct lys_parser_ctx *ctx, const char *module_prefix, const char **data, struct lysp_import **imports) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1196 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1197 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1198 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 1199 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1200 | enum yang_keyword kw; |
| 1201 | struct lysp_import *imp; |
| 1202 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1203 | LY_ARRAY_NEW_RET(ctx->ctx, *imports, imp, LY_EVALID); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1204 | |
| 1205 | /* get value */ |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 1206 | LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len)); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1207 | INSERT_WORD(ctx, buf, imp->name, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1208 | |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 1209 | 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] | 1210 | switch (kw) { |
| 1211 | case YANG_PREFIX: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1212 | 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] | 1213 | 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] | 1214 | break; |
| 1215 | case YANG_DESCRIPTION: |
Radek Krejci | ceaf212 | 2019-01-02 15:03:26 +0100 | [diff] [blame] | 1216 | YANG_CHECK_STMTVER2_RET(ctx, "description", "import"); |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1217 | 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] | 1218 | break; |
| 1219 | case YANG_REFERENCE: |
Radek Krejci | ceaf212 | 2019-01-02 15:03:26 +0100 | [diff] [blame] | 1220 | YANG_CHECK_STMTVER2_RET(ctx, "reference", "import"); |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1221 | 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] | 1222 | break; |
| 1223 | case YANG_REVISION_DATE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1224 | LY_CHECK_RET(parse_revisiondate(ctx, data, imp->rev, &imp->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1225 | break; |
| 1226 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1227 | 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] | 1228 | break; |
| 1229 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 1230 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "import"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1231 | return LY_EVALID; |
| 1232 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1233 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1234 | LY_CHECK_RET(ret); |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 1235 | checks: |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1236 | /* mandatory substatements */ |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 1237 | LY_CHECK_ERR_RET(!imp->prefix, LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "import"), LY_EVALID); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1238 | |
| 1239 | return ret; |
| 1240 | } |
| 1241 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1242 | /** |
| 1243 | * @brief Parse the revision statement. |
| 1244 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1245 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1246 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 1247 | * @param[in,out] revs Parsed revisions to add to. |
| 1248 | * |
| 1249 | * @return LY_ERR values. |
| 1250 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1251 | static LY_ERR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1252 | parse_revision(struct lys_parser_ctx *ctx, const char **data, struct lysp_revision **revs) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1253 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1254 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1255 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 1256 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1257 | enum yang_keyword kw; |
| 1258 | struct lysp_revision *rev; |
| 1259 | |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 1260 | LY_ARRAY_NEW_RET(ctx->ctx, *revs, rev, LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1261 | |
| 1262 | /* get value */ |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 1263 | LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1264 | |
| 1265 | /* check value */ |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 1266 | if (lysp_check_date(ctx, word, word_len, "revision")) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1267 | return LY_EVALID; |
| 1268 | } |
| 1269 | |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 1270 | strncpy(rev->date, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1271 | free(buf); |
| 1272 | |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 1273 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1274 | switch (kw) { |
| 1275 | case YANG_DESCRIPTION: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1276 | 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] | 1277 | break; |
| 1278 | case YANG_REFERENCE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1279 | 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] | 1280 | break; |
| 1281 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1282 | 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] | 1283 | break; |
| 1284 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 1285 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "revision"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1286 | return LY_EVALID; |
| 1287 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1288 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1289 | return ret; |
| 1290 | } |
| 1291 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1292 | /** |
| 1293 | * @brief Parse a generic text field that can have more instances such as base. |
| 1294 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1295 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1296 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 1297 | * @param[in] substmt Type of this substatement. |
| 1298 | * @param[in,out] texts Parsed values to add to. |
| 1299 | * @param[in] arg Type of the expected argument. |
| 1300 | * @param[in,out] exts Extension instances to add to. |
| 1301 | * |
| 1302 | * @return LY_ERR values. |
| 1303 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1304 | static LY_ERR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1305 | parse_text_fields(struct lys_parser_ctx *ctx, const char **data, LYEXT_SUBSTMT substmt, const char ***texts, enum yang_arg arg, |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1306 | struct lysp_ext_instance **exts) |
| 1307 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1308 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1309 | char *buf, *word; |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 1310 | const char **item; |
| 1311 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1312 | enum yang_keyword kw; |
| 1313 | |
| 1314 | /* allocate new pointer */ |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 1315 | LY_ARRAY_NEW_RET(ctx->ctx, *texts, item, LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1316 | |
| 1317 | /* get value */ |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 1318 | LY_CHECK_RET(get_argument(ctx, data, arg, NULL, &word, &buf, &word_len)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1319 | |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 1320 | INSERT_WORD(ctx, buf, *item, word, word_len); |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 1321 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1322 | switch (kw) { |
| 1323 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1324 | 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] | 1325 | break; |
| 1326 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 1327 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), lyext_substmt2str(substmt)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1328 | return LY_EVALID; |
| 1329 | } |
| 1330 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1331 | return ret; |
| 1332 | } |
| 1333 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1334 | /** |
| 1335 | * @brief Parse the config statement. |
| 1336 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1337 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1338 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 1339 | * @param[in,out] flags Flags to add to. |
| 1340 | * @param[in,out] exts Extension instances to add to. |
| 1341 | * |
| 1342 | * @return LY_ERR values. |
| 1343 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1344 | static LY_ERR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1345 | parse_config(struct lys_parser_ctx *ctx, const char **data, uint16_t *flags, struct lysp_ext_instance **exts) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1346 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1347 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1348 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 1349 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1350 | enum yang_keyword kw; |
| 1351 | |
| 1352 | if (*flags & LYS_CONFIG_MASK) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 1353 | LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "config"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1354 | return LY_EVALID; |
| 1355 | } |
| 1356 | |
| 1357 | /* get value */ |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 1358 | LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1359 | |
| 1360 | if ((word_len == 4) && !strncmp(word, "true", word_len)) { |
| 1361 | *flags |= LYS_CONFIG_W; |
| 1362 | } else if ((word_len == 5) && !strncmp(word, "false", word_len)) { |
| 1363 | *flags |= LYS_CONFIG_R; |
| 1364 | } else { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 1365 | LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "config"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1366 | free(buf); |
| 1367 | return LY_EVALID; |
| 1368 | } |
| 1369 | free(buf); |
| 1370 | |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 1371 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1372 | switch (kw) { |
| 1373 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1374 | 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] | 1375 | break; |
| 1376 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 1377 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "config"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1378 | return LY_EVALID; |
| 1379 | } |
| 1380 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1381 | return ret; |
| 1382 | } |
| 1383 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1384 | /** |
| 1385 | * @brief Parse the mandatory statement. |
| 1386 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1387 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1388 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 1389 | * @param[in,out] flags Flags to add to. |
| 1390 | * @param[in,out] exts Extension instances to add to. |
| 1391 | * |
| 1392 | * @return LY_ERR values. |
| 1393 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1394 | static LY_ERR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1395 | parse_mandatory(struct lys_parser_ctx *ctx, const char **data, uint16_t *flags, struct lysp_ext_instance **exts) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1396 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1397 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1398 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 1399 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1400 | enum yang_keyword kw; |
| 1401 | |
| 1402 | if (*flags & LYS_MAND_MASK) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 1403 | LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "mandatory"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1404 | return LY_EVALID; |
| 1405 | } |
| 1406 | |
| 1407 | /* get value */ |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 1408 | LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1409 | |
| 1410 | if ((word_len == 4) && !strncmp(word, "true", word_len)) { |
| 1411 | *flags |= LYS_MAND_TRUE; |
| 1412 | } else if ((word_len == 5) && !strncmp(word, "false", word_len)) { |
| 1413 | *flags |= LYS_MAND_FALSE; |
| 1414 | } else { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 1415 | LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "mandatory"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1416 | free(buf); |
| 1417 | return LY_EVALID; |
| 1418 | } |
| 1419 | free(buf); |
| 1420 | |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 1421 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1422 | switch (kw) { |
| 1423 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1424 | 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] | 1425 | break; |
| 1426 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 1427 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "mandatory"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1428 | return LY_EVALID; |
| 1429 | } |
| 1430 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1431 | return ret; |
| 1432 | } |
| 1433 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1434 | /** |
| 1435 | * @brief Parse a restriction such as range or length. |
| 1436 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1437 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1438 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 1439 | * @param[in] restr_kw Type of this particular restriction. |
| 1440 | * @param[in,out] exts Extension instances to add to. |
| 1441 | * |
| 1442 | * @return LY_ERR values. |
| 1443 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1444 | static LY_ERR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1445 | parse_restr(struct lys_parser_ctx *ctx, const char **data, enum yang_keyword restr_kw, struct lysp_restr *restr) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1446 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1447 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1448 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 1449 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1450 | enum yang_keyword kw; |
| 1451 | |
| 1452 | /* get value */ |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 1453 | LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1454 | |
Radek Krejci | f09e4e8 | 2019-06-14 15:08:11 +0200 | [diff] [blame] | 1455 | YANG_CHECK_NONEMPTY(ctx, NULL, word_len, ly_stmt2str(restr_kw)); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1456 | INSERT_WORD(ctx, buf, restr->arg, word, word_len); |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 1457 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1458 | switch (kw) { |
| 1459 | case YANG_DESCRIPTION: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1460 | 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] | 1461 | break; |
| 1462 | case YANG_REFERENCE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1463 | 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] | 1464 | break; |
| 1465 | case YANG_ERROR_APP_TAG: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1466 | 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] | 1467 | break; |
| 1468 | case YANG_ERROR_MESSAGE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1469 | 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] | 1470 | break; |
| 1471 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1472 | 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] | 1473 | break; |
| 1474 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 1475 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(restr_kw)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1476 | return LY_EVALID; |
| 1477 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1478 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1479 | return ret; |
| 1480 | } |
| 1481 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1482 | /** |
| 1483 | * @brief Parse a restriction that can have more instances such as must. |
| 1484 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1485 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1486 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 1487 | * @param[in] restr_kw Type of this particular restriction. |
| 1488 | * @param[in,out] restrs Restrictions to add to. |
| 1489 | * |
| 1490 | * @return LY_ERR values. |
| 1491 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1492 | static LY_ERR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1493 | parse_restrs(struct lys_parser_ctx *ctx, const char **data, enum yang_keyword restr_kw, struct lysp_restr **restrs) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1494 | { |
| 1495 | struct lysp_restr *restr; |
| 1496 | |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 1497 | LY_ARRAY_NEW_RET(ctx->ctx, *restrs, restr, LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1498 | return parse_restr(ctx, data, restr_kw, restr); |
| 1499 | } |
| 1500 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1501 | /** |
| 1502 | * @brief Parse the status statement. |
| 1503 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1504 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1505 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 1506 | * @param[in,out] flags Flags to add to. |
| 1507 | * @param[in,out] exts Extension instances to add to. |
| 1508 | * |
| 1509 | * @return LY_ERR values. |
| 1510 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1511 | static LY_ERR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1512 | parse_status(struct lys_parser_ctx *ctx, const char **data, uint16_t *flags, struct lysp_ext_instance **exts) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1513 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1514 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1515 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 1516 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1517 | enum yang_keyword kw; |
| 1518 | |
| 1519 | if (*flags & LYS_STATUS_MASK) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 1520 | LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "status"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1521 | return LY_EVALID; |
| 1522 | } |
| 1523 | |
| 1524 | /* get value */ |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 1525 | LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1526 | |
| 1527 | if ((word_len == 7) && !strncmp(word, "current", word_len)) { |
| 1528 | *flags |= LYS_STATUS_CURR; |
| 1529 | } else if ((word_len == 10) && !strncmp(word, "deprecated", word_len)) { |
| 1530 | *flags |= LYS_STATUS_DEPRC; |
| 1531 | } else if ((word_len == 8) && !strncmp(word, "obsolete", word_len)) { |
| 1532 | *flags |= LYS_STATUS_OBSLT; |
| 1533 | } else { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 1534 | LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "status"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1535 | free(buf); |
| 1536 | return LY_EVALID; |
| 1537 | } |
| 1538 | free(buf); |
| 1539 | |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 1540 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1541 | switch (kw) { |
| 1542 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1543 | 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] | 1544 | break; |
| 1545 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 1546 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "status"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1547 | return LY_EVALID; |
| 1548 | } |
| 1549 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1550 | return ret; |
| 1551 | } |
| 1552 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1553 | /** |
| 1554 | * @brief Parse the when statement. |
| 1555 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1556 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1557 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 1558 | * @param[in,out] when_p When pointer to parse to. |
| 1559 | * |
| 1560 | * @return LY_ERR values. |
| 1561 | */ |
Radek Krejci | f09e4e8 | 2019-06-14 15:08:11 +0200 | [diff] [blame] | 1562 | LY_ERR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1563 | parse_when(struct lys_parser_ctx *ctx, const char **data, struct lysp_when **when_p) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1564 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1565 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1566 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 1567 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1568 | enum yang_keyword kw; |
| 1569 | struct lysp_when *when; |
| 1570 | |
| 1571 | if (*when_p) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 1572 | LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "when"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1573 | return LY_EVALID; |
| 1574 | } |
| 1575 | |
| 1576 | when = calloc(1, sizeof *when); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1577 | LY_CHECK_ERR_RET(!when, LOGMEM(ctx->ctx), LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1578 | |
| 1579 | /* get value */ |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 1580 | LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len)); |
Radek Krejci | f09e4e8 | 2019-06-14 15:08:11 +0200 | [diff] [blame] | 1581 | YANG_CHECK_NONEMPTY(ctx, when, word_len, "when"); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1582 | INSERT_WORD(ctx, buf, when->cond, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1583 | |
Radek Krejci | f09e4e8 | 2019-06-14 15:08:11 +0200 | [diff] [blame] | 1584 | *when_p = when; |
| 1585 | |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 1586 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1587 | switch (kw) { |
| 1588 | case YANG_DESCRIPTION: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1589 | 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] | 1590 | break; |
| 1591 | case YANG_REFERENCE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1592 | 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] | 1593 | break; |
| 1594 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1595 | 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] | 1596 | break; |
| 1597 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 1598 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "when"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1599 | return LY_EVALID; |
| 1600 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1601 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1602 | return ret; |
| 1603 | } |
| 1604 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1605 | /** |
| 1606 | * @brief Parse the anydata or anyxml statement. |
| 1607 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1608 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1609 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 1610 | * @param[in] kw Type of this particular keyword. |
| 1611 | * @param[in,out] siblings Siblings to add to. |
| 1612 | * |
| 1613 | * @return LY_ERR values. |
| 1614 | */ |
Radek Krejci | 2d7a47b | 2019-05-16 13:34:10 +0200 | [diff] [blame] | 1615 | LY_ERR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1616 | parse_any(struct lys_parser_ctx *ctx, const char **data, enum yang_keyword kw, struct lysp_node *parent, struct lysp_node **siblings) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1617 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1618 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1619 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 1620 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1621 | struct lysp_node *iter; |
| 1622 | struct lysp_node_anydata *any; |
| 1623 | |
| 1624 | /* create structure */ |
| 1625 | any = calloc(1, sizeof *any); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1626 | LY_CHECK_ERR_RET(!any, LOGMEM(ctx->ctx), LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1627 | any->nodetype = kw == YANG_ANYDATA ? LYS_ANYDATA : LYS_ANYXML; |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1628 | any->parent = parent; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1629 | |
| 1630 | /* insert into siblings */ |
| 1631 | if (!*siblings) { |
| 1632 | *siblings = (struct lysp_node *)any; |
| 1633 | } else { |
| 1634 | for (iter = *siblings; iter->next; iter = iter->next); |
| 1635 | iter->next = (struct lysp_node *)any; |
| 1636 | } |
| 1637 | |
| 1638 | /* get name */ |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 1639 | LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len)); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1640 | INSERT_WORD(ctx, buf, any->name, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1641 | |
| 1642 | /* parse substatements */ |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 1643 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1644 | switch (kw) { |
| 1645 | case YANG_CONFIG: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1646 | LY_CHECK_RET(parse_config(ctx, data, &any->flags, &any->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1647 | break; |
| 1648 | case YANG_DESCRIPTION: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1649 | 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] | 1650 | break; |
| 1651 | case YANG_IF_FEATURE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1652 | 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] | 1653 | break; |
| 1654 | case YANG_MANDATORY: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1655 | LY_CHECK_RET(parse_mandatory(ctx, data, &any->flags, &any->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1656 | break; |
| 1657 | case YANG_MUST: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1658 | LY_CHECK_RET(parse_restrs(ctx, data, kw, &any->musts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1659 | break; |
| 1660 | case YANG_REFERENCE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1661 | 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] | 1662 | break; |
| 1663 | case YANG_STATUS: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1664 | LY_CHECK_RET(parse_status(ctx, data, &any->flags, &any->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1665 | break; |
| 1666 | case YANG_WHEN: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1667 | LY_CHECK_RET(parse_when(ctx, data, &any->when)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1668 | break; |
| 1669 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1670 | 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] | 1671 | break; |
| 1672 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 1673 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 1674 | (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] | 1675 | return LY_EVALID; |
| 1676 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1677 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1678 | return ret; |
| 1679 | } |
| 1680 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1681 | /** |
| 1682 | * @brief Parse the value or position statement. Substatement of type enum statement. |
| 1683 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1684 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1685 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 1686 | * @param[in] val_kw Type of this particular keyword. |
| 1687 | * @param[in,out] value Value to write to. |
| 1688 | * @param[in,out] flags Flags to write to. |
| 1689 | * @param[in,out] exts Extension instances to add to. |
| 1690 | * |
| 1691 | * @return LY_ERR values. |
| 1692 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1693 | static LY_ERR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1694 | parse_type_enum_value_pos(struct lys_parser_ctx *ctx, const char **data, enum yang_keyword val_kw, int64_t *value, uint16_t *flags, |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1695 | struct lysp_ext_instance **exts) |
| 1696 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1697 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1698 | char *buf, *word, *ptr; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 1699 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1700 | long int num; |
| 1701 | unsigned long int unum; |
| 1702 | enum yang_keyword kw; |
| 1703 | |
| 1704 | if (*flags & LYS_SET_VALUE) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 1705 | LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(val_kw)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1706 | return LY_EVALID; |
| 1707 | } |
| 1708 | *flags |= LYS_SET_VALUE; |
| 1709 | |
| 1710 | /* get value */ |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 1711 | LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1712 | |
| 1713 | if (!word_len || (word[0] == '+') || ((word[0] == '0') && (word_len > 1)) || ((val_kw == YANG_VALUE) && !strncmp(word, "-0", 2))) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 1714 | LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw)); |
Radek Krejci | 8b76466 | 2018-11-14 14:15:13 +0100 | [diff] [blame] | 1715 | goto error; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1716 | } |
| 1717 | |
| 1718 | errno = 0; |
| 1719 | if (val_kw == YANG_VALUE) { |
| 1720 | num = strtol(word, &ptr, 10); |
Radek Krejci | 8b76466 | 2018-11-14 14:15:13 +0100 | [diff] [blame] | 1721 | if (num < INT64_C(-2147483648) || num > INT64_C(2147483647)) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 1722 | LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw)); |
Radek Krejci | 8b76466 | 2018-11-14 14:15:13 +0100 | [diff] [blame] | 1723 | goto error; |
| 1724 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1725 | } else { |
| 1726 | unum = strtoul(word, &ptr, 10); |
Radek Krejci | 8b76466 | 2018-11-14 14:15:13 +0100 | [diff] [blame] | 1727 | if (unum > UINT64_C(4294967295)) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 1728 | LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw)); |
Radek Krejci | 8b76466 | 2018-11-14 14:15:13 +0100 | [diff] [blame] | 1729 | goto error; |
| 1730 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1731 | } |
| 1732 | /* we have not parsed the whole argument */ |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 1733 | if ((size_t)(ptr - word) != word_len) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 1734 | LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw)); |
Radek Krejci | 8b76466 | 2018-11-14 14:15:13 +0100 | [diff] [blame] | 1735 | goto error; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1736 | } |
| 1737 | if (errno == ERANGE) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 1738 | LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, ly_stmt2str(val_kw)); |
Radek Krejci | 8b76466 | 2018-11-14 14:15:13 +0100 | [diff] [blame] | 1739 | goto error; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1740 | } |
| 1741 | if (val_kw == YANG_VALUE) { |
| 1742 | *value = num; |
| 1743 | } else { |
| 1744 | *value = unum; |
| 1745 | } |
| 1746 | free(buf); |
| 1747 | |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 1748 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1749 | switch (kw) { |
| 1750 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1751 | 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] | 1752 | break; |
| 1753 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 1754 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(val_kw)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1755 | return LY_EVALID; |
| 1756 | } |
| 1757 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1758 | return ret; |
Radek Krejci | 8b76466 | 2018-11-14 14:15:13 +0100 | [diff] [blame] | 1759 | |
| 1760 | error: |
| 1761 | free(buf); |
| 1762 | return LY_EVALID; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1763 | } |
| 1764 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1765 | /** |
| 1766 | * @brief Parse the enum or bit statement. Substatement of type statement. |
| 1767 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1768 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1769 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 1770 | * @param[in] enum_kw Type of this particular keyword. |
| 1771 | * @param[in,out] enums Enums or bits to add to. |
| 1772 | * |
| 1773 | * @return LY_ERR values. |
| 1774 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1775 | static LY_ERR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1776 | parse_type_enum(struct lys_parser_ctx *ctx, const char **data, enum yang_keyword enum_kw, struct lysp_type_enum **enums) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1777 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1778 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1779 | char *buf, *word; |
Radek Krejci | 8b76466 | 2018-11-14 14:15:13 +0100 | [diff] [blame] | 1780 | size_t word_len, u; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1781 | enum yang_keyword kw; |
| 1782 | struct lysp_type_enum *enm; |
| 1783 | |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 1784 | LY_ARRAY_NEW_RET(ctx->ctx, *enums, enm, LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1785 | |
| 1786 | /* get value */ |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 1787 | LY_CHECK_RET(get_argument(ctx, data, enum_kw == YANG_ENUM ? Y_STR_ARG : Y_IDENTIF_ARG, NULL, &word, &buf, &word_len)); |
Radek Krejci | 8b76466 | 2018-11-14 14:15:13 +0100 | [diff] [blame] | 1788 | if (enum_kw == YANG_ENUM) { |
| 1789 | if (!word_len) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 1790 | LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Enum name must not be zero-length."); |
Radek Krejci | 8b76466 | 2018-11-14 14:15:13 +0100 | [diff] [blame] | 1791 | free(buf); |
| 1792 | return LY_EVALID; |
| 1793 | } else if (isspace(word[0]) || isspace(word[word_len - 1])) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 1794 | LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Enum name must not have any leading or trailing whitespaces (\"%.*s\").", |
Radek Krejci | 8b76466 | 2018-11-14 14:15:13 +0100 | [diff] [blame] | 1795 | word_len, word); |
| 1796 | free(buf); |
| 1797 | return LY_EVALID; |
| 1798 | } else { |
| 1799 | for (u = 0; u < word_len; ++u) { |
| 1800 | if (iscntrl(word[u])) { |
| 1801 | LOGWRN(ctx->ctx, "Control characters in enum name should be avoided (\"%.*s\", character number %d).", |
| 1802 | word_len, word, u + 1); |
| 1803 | break; |
| 1804 | } |
| 1805 | } |
| 1806 | } |
| 1807 | } else { /* YANG_BIT */ |
| 1808 | |
| 1809 | } |
Radek Krejci | f09e4e8 | 2019-06-14 15:08:11 +0200 | [diff] [blame] | 1810 | if (enum_kw == YANG_ENUM) { |
| 1811 | YANG_CHECK_NONEMPTY(ctx, NULL, word_len, "enum"); |
| 1812 | } |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1813 | INSERT_WORD(ctx, buf, enm->name, word, word_len); |
Radek Krejci | 8b76466 | 2018-11-14 14:15:13 +0100 | [diff] [blame] | 1814 | CHECK_UNIQUENESS(ctx, *enums, name, ly_stmt2str(enum_kw), enm->name); |
| 1815 | |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 1816 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1817 | switch (kw) { |
| 1818 | case YANG_DESCRIPTION: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1819 | 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] | 1820 | break; |
| 1821 | case YANG_IF_FEATURE: |
Radek Krejci | ceaf212 | 2019-01-02 15:03:26 +0100 | [diff] [blame] | 1822 | YANG_CHECK_STMTVER2_RET(ctx, "if-feature", ly_stmt2str(enum_kw)); |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1823 | 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] | 1824 | break; |
| 1825 | case YANG_REFERENCE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1826 | 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] | 1827 | break; |
| 1828 | case YANG_STATUS: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1829 | LY_CHECK_RET(parse_status(ctx, data, &enm->flags, &enm->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1830 | break; |
| 1831 | case YANG_VALUE: |
| 1832 | case YANG_POSITION: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1833 | 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] | 1834 | break; |
| 1835 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1836 | 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] | 1837 | break; |
| 1838 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 1839 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(enum_kw)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1840 | return LY_EVALID; |
| 1841 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1842 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1843 | return ret; |
| 1844 | } |
| 1845 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1846 | /** |
| 1847 | * @brief Parse the fraction-digits statement. Substatement of type statement. |
| 1848 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1849 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1850 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 1851 | * @param[in,out] fracdig Value to write to. |
| 1852 | * @param[in,out] exts Extension instances to add to. |
| 1853 | * |
| 1854 | * @return LY_ERR values. |
| 1855 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1856 | static LY_ERR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1857 | parse_type_fracdigits(struct lys_parser_ctx *ctx, const char **data, uint8_t *fracdig, struct lysp_ext_instance **exts) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1858 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1859 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1860 | char *buf, *word, *ptr; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 1861 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1862 | unsigned long int num; |
| 1863 | enum yang_keyword kw; |
| 1864 | |
| 1865 | if (*fracdig) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 1866 | LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "fraction-digits"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1867 | return LY_EVALID; |
| 1868 | } |
| 1869 | |
| 1870 | /* get value */ |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 1871 | LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1872 | |
| 1873 | if (!word_len || (word[0] == '0') || !isdigit(word[0])) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 1874 | LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "fraction-digits"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1875 | free(buf); |
| 1876 | return LY_EVALID; |
| 1877 | } |
| 1878 | |
| 1879 | errno = 0; |
| 1880 | num = strtoul(word, &ptr, 10); |
| 1881 | /* we have not parsed the whole argument */ |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 1882 | if ((size_t)(ptr - word) != word_len) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 1883 | LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "fraction-digits"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1884 | free(buf); |
| 1885 | return LY_EVALID; |
| 1886 | } |
| 1887 | if ((errno == ERANGE) || (num > 18)) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 1888 | LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, "fraction-digits"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1889 | free(buf); |
| 1890 | return LY_EVALID; |
| 1891 | } |
| 1892 | *fracdig = num; |
| 1893 | free(buf); |
| 1894 | |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 1895 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1896 | switch (kw) { |
| 1897 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1898 | 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] | 1899 | break; |
| 1900 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 1901 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "fraction-digits"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1902 | return LY_EVALID; |
| 1903 | } |
| 1904 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1905 | return ret; |
| 1906 | } |
| 1907 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1908 | /** |
| 1909 | * @brief Parse the require-instance statement. Substatement of type statement. |
| 1910 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1911 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1912 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 1913 | * @param[in,out] reqinst Value to write to. |
| 1914 | * @param[in,out] flags Flags to write to. |
| 1915 | * @param[in,out] exts Extension instances to add to. |
| 1916 | * |
| 1917 | * @return LY_ERR values. |
| 1918 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1919 | static LY_ERR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1920 | parse_type_reqinstance(struct lys_parser_ctx *ctx, const char **data, uint8_t *reqinst, uint16_t *flags, |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1921 | struct lysp_ext_instance **exts) |
| 1922 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1923 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1924 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 1925 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1926 | enum yang_keyword kw; |
| 1927 | |
| 1928 | if (*flags & LYS_SET_REQINST) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 1929 | LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "require-instance"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1930 | return LY_EVALID; |
| 1931 | } |
| 1932 | *flags |= LYS_SET_REQINST; |
| 1933 | |
| 1934 | /* get value */ |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 1935 | LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1936 | |
| 1937 | if ((word_len == 4) && !strncmp(word, "true", word_len)) { |
| 1938 | *reqinst = 1; |
| 1939 | } else if ((word_len != 5) || strncmp(word, "false", word_len)) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 1940 | LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "require-instance"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1941 | free(buf); |
| 1942 | return LY_EVALID; |
| 1943 | } |
| 1944 | free(buf); |
| 1945 | |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 1946 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1947 | switch (kw) { |
| 1948 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1949 | 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] | 1950 | break; |
| 1951 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 1952 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "require-instance"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1953 | return LY_EVALID; |
| 1954 | } |
| 1955 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1956 | return ret; |
| 1957 | } |
| 1958 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1959 | /** |
| 1960 | * @brief Parse the modifier statement. Substatement of type pattern statement. |
| 1961 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1962 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 1963 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 1964 | * @param[in,out] pat Value to write to. |
| 1965 | * @param[in,out] exts Extension instances to add to. |
| 1966 | * |
| 1967 | * @return LY_ERR values. |
| 1968 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1969 | static LY_ERR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1970 | parse_type_pattern_modifier(struct lys_parser_ctx *ctx, const char **data, const char **pat, struct lysp_ext_instance **exts) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1971 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 1972 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1973 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 1974 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1975 | enum yang_keyword kw; |
| 1976 | |
| 1977 | if ((*pat)[0] == 0x15) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 1978 | LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "modifier"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1979 | return LY_EVALID; |
| 1980 | } |
| 1981 | |
| 1982 | /* get value */ |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 1983 | LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1984 | |
| 1985 | if ((word_len != 12) || strncmp(word, "invert-match", word_len)) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 1986 | LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "modifier"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1987 | free(buf); |
| 1988 | return LY_EVALID; |
| 1989 | } |
| 1990 | free(buf); |
| 1991 | |
| 1992 | /* replace the value in the dictionary */ |
| 1993 | buf = malloc(strlen(*pat) + 1); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1994 | LY_CHECK_ERR_RET(!buf, LOGMEM(ctx->ctx), LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1995 | strcpy(buf, *pat); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1996 | lydict_remove(ctx->ctx, *pat); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 1997 | |
| 1998 | assert(buf[0] == 0x06); |
| 1999 | buf[0] = 0x15; |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2000 | *pat = lydict_insert_zc(ctx->ctx, buf); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2001 | |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 2002 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2003 | switch (kw) { |
| 2004 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2005 | 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] | 2006 | break; |
| 2007 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 2008 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "modifier"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2009 | return LY_EVALID; |
| 2010 | } |
| 2011 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2012 | return ret; |
| 2013 | } |
| 2014 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2015 | /** |
| 2016 | * @brief Parse the pattern statement. Substatement of type statement. |
| 2017 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2018 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2019 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 2020 | * @param[in,out] patterns Restrictions to add to. |
| 2021 | * |
| 2022 | * @return LY_ERR values. |
| 2023 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2024 | static LY_ERR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 2025 | parse_type_pattern(struct lys_parser_ctx *ctx, const char **data, struct lysp_restr **patterns) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2026 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2027 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2028 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 2029 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2030 | enum yang_keyword kw; |
| 2031 | struct lysp_restr *restr; |
| 2032 | |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 2033 | LY_ARRAY_NEW_RET(ctx->ctx, *patterns, restr, LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2034 | |
| 2035 | /* get value */ |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 2036 | LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2037 | |
| 2038 | /* add special meaning first byte */ |
| 2039 | if (buf) { |
| 2040 | buf = realloc(buf, word_len + 2); |
| 2041 | word = buf; |
| 2042 | } else { |
| 2043 | buf = malloc(word_len + 2); |
| 2044 | } |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2045 | LY_CHECK_ERR_RET(!buf, LOGMEM(ctx->ctx), LY_EMEM); |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 2046 | memmove(buf + 1, word, word_len); |
| 2047 | buf[0] = 0x06; /* pattern's default regular-match flag */ |
| 2048 | buf[word_len + 1] = '\0'; /* terminating NULL byte */ |
| 2049 | restr->arg = lydict_insert_zc(ctx->ctx, buf); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2050 | |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 2051 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2052 | switch (kw) { |
| 2053 | case YANG_DESCRIPTION: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2054 | 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] | 2055 | break; |
| 2056 | case YANG_REFERENCE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2057 | 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] | 2058 | break; |
| 2059 | case YANG_ERROR_APP_TAG: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2060 | 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] | 2061 | break; |
| 2062 | case YANG_ERROR_MESSAGE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2063 | 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] | 2064 | break; |
| 2065 | case YANG_MODIFIER: |
Radek Krejci | ceaf212 | 2019-01-02 15:03:26 +0100 | [diff] [blame] | 2066 | YANG_CHECK_STMTVER2_RET(ctx, "modifier", "pattern"); |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2067 | 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] | 2068 | break; |
| 2069 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2070 | 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] | 2071 | break; |
| 2072 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 2073 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "pattern"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2074 | return LY_EVALID; |
| 2075 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2076 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2077 | return ret; |
| 2078 | } |
| 2079 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2080 | /** |
| 2081 | * @brief Parse the type statement. |
| 2082 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2083 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2084 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 2085 | * @param[in,out] type Type to wrote to. |
| 2086 | * |
| 2087 | * @return LY_ERR values. |
| 2088 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2089 | static LY_ERR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 2090 | parse_type(struct lys_parser_ctx *ctx, const char **data, struct lysp_type *type) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2091 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2092 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2093 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 2094 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2095 | enum yang_keyword kw; |
| 2096 | struct lysp_type *nest_type; |
| 2097 | |
| 2098 | if (type->name) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 2099 | LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "type"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2100 | return LY_EVALID; |
| 2101 | } |
| 2102 | |
| 2103 | /* get value */ |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 2104 | LY_CHECK_RET(get_argument(ctx, data, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &word_len)); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2105 | INSERT_WORD(ctx, buf, type->name, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2106 | |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 2107 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2108 | switch (kw) { |
| 2109 | case YANG_BASE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2110 | 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] | 2111 | type->flags |= LYS_SET_BASE; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2112 | break; |
| 2113 | case YANG_BIT: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2114 | LY_CHECK_RET(parse_type_enum(ctx, data, kw, &type->bits)); |
Radek Krejci | d505e3d | 2018-11-13 09:04:17 +0100 | [diff] [blame] | 2115 | type->flags |= LYS_SET_BIT; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2116 | break; |
| 2117 | case YANG_ENUM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2118 | LY_CHECK_RET(parse_type_enum(ctx, data, kw, &type->enums)); |
Radek Krejci | d505e3d | 2018-11-13 09:04:17 +0100 | [diff] [blame] | 2119 | type->flags |= LYS_SET_ENUM; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2120 | break; |
| 2121 | case YANG_FRACTION_DIGITS: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2122 | 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] | 2123 | type->flags |= LYS_SET_FRDIGITS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2124 | break; |
| 2125 | case YANG_LENGTH: |
| 2126 | if (type->length) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 2127 | LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2128 | return LY_EVALID; |
| 2129 | } |
| 2130 | type->length = calloc(1, sizeof *type->length); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2131 | LY_CHECK_ERR_RET(!type->length, LOGMEM(ctx->ctx), LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2132 | |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2133 | LY_CHECK_RET(parse_restr(ctx, data, kw, type->length)); |
Radek Krejci | d505e3d | 2018-11-13 09:04:17 +0100 | [diff] [blame] | 2134 | type->flags |= LYS_SET_LENGTH; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2135 | break; |
| 2136 | case YANG_PATH: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2137 | 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] | 2138 | type->flags |= LYS_SET_PATH; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2139 | break; |
| 2140 | case YANG_PATTERN: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2141 | LY_CHECK_RET(parse_type_pattern(ctx, data, &type->patterns)); |
Radek Krejci | d505e3d | 2018-11-13 09:04:17 +0100 | [diff] [blame] | 2142 | type->flags |= LYS_SET_PATTERN; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2143 | break; |
| 2144 | case YANG_RANGE: |
| 2145 | if (type->range) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 2146 | LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2147 | return LY_EVALID; |
| 2148 | } |
| 2149 | type->range = calloc(1, sizeof *type->range); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2150 | LY_CHECK_ERR_RET(!type->range, LOGMEM(ctx->ctx), LY_EVALID); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2151 | |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2152 | LY_CHECK_RET(parse_restr(ctx, data, kw, type->range)); |
Radek Krejci | d505e3d | 2018-11-13 09:04:17 +0100 | [diff] [blame] | 2153 | type->flags |= LYS_SET_RANGE; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2154 | break; |
| 2155 | case YANG_REQUIRE_INSTANCE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2156 | 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] | 2157 | /* LYS_SET_REQINST checked and set inside parse_type_reqinstance() */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2158 | break; |
| 2159 | case YANG_TYPE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2160 | LY_ARRAY_NEW_RET(ctx->ctx, type->types, nest_type, LY_EMEM); |
| 2161 | LY_CHECK_RET(parse_type(ctx, data, nest_type)); |
Radek Krejci | d505e3d | 2018-11-13 09:04:17 +0100 | [diff] [blame] | 2162 | type->flags |= LYS_SET_TYPE; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2163 | break; |
| 2164 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2165 | 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] | 2166 | break; |
| 2167 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 2168 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "type"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2169 | return LY_EVALID; |
| 2170 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2171 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2172 | return ret; |
| 2173 | } |
| 2174 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2175 | /** |
| 2176 | * @brief Parse the leaf statement. |
| 2177 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2178 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2179 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 2180 | * @param[in,out] siblings Siblings to add to. |
| 2181 | * |
| 2182 | * @return LY_ERR values. |
| 2183 | */ |
Radek Krejci | 2d7a47b | 2019-05-16 13:34:10 +0200 | [diff] [blame] | 2184 | LY_ERR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 2185 | parse_leaf(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2186 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2187 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2188 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 2189 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2190 | enum yang_keyword kw; |
| 2191 | struct lysp_node *iter; |
| 2192 | struct lysp_node_leaf *leaf; |
| 2193 | |
| 2194 | /* create structure */ |
| 2195 | leaf = calloc(1, sizeof *leaf); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2196 | LY_CHECK_ERR_RET(!leaf, LOGMEM(ctx->ctx), LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2197 | leaf->nodetype = LYS_LEAF; |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2198 | leaf->parent = parent; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2199 | |
| 2200 | /* insert into siblings */ |
| 2201 | if (!*siblings) { |
| 2202 | *siblings = (struct lysp_node *)leaf; |
| 2203 | } else { |
| 2204 | for (iter = *siblings; iter->next; iter = iter->next); |
| 2205 | iter->next = (struct lysp_node *)leaf; |
| 2206 | } |
| 2207 | |
| 2208 | /* get name */ |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 2209 | LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len)); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2210 | INSERT_WORD(ctx, buf, leaf->name, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2211 | |
| 2212 | /* parse substatements */ |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 2213 | 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] | 2214 | switch (kw) { |
| 2215 | case YANG_CONFIG: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2216 | LY_CHECK_RET(parse_config(ctx, data, &leaf->flags, &leaf->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2217 | break; |
| 2218 | case YANG_DEFAULT: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2219 | 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] | 2220 | break; |
| 2221 | case YANG_DESCRIPTION: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2222 | 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] | 2223 | break; |
| 2224 | case YANG_IF_FEATURE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2225 | 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] | 2226 | break; |
| 2227 | case YANG_MANDATORY: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2228 | LY_CHECK_RET(parse_mandatory(ctx, data, &leaf->flags, &leaf->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2229 | break; |
| 2230 | case YANG_MUST: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2231 | LY_CHECK_RET(parse_restrs(ctx, data, kw, &leaf->musts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2232 | break; |
| 2233 | case YANG_REFERENCE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2234 | 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] | 2235 | break; |
| 2236 | case YANG_STATUS: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2237 | LY_CHECK_RET(parse_status(ctx, data, &leaf->flags, &leaf->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2238 | break; |
| 2239 | case YANG_TYPE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2240 | LY_CHECK_RET(parse_type(ctx, data, &leaf->type)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2241 | break; |
| 2242 | case YANG_UNITS: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2243 | 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] | 2244 | break; |
| 2245 | case YANG_WHEN: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2246 | LY_CHECK_RET(parse_when(ctx, data, &leaf->when)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2247 | break; |
| 2248 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2249 | 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] | 2250 | break; |
| 2251 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 2252 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "leaf"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2253 | return LY_EVALID; |
| 2254 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2255 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 2256 | LY_CHECK_RET(ret); |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 2257 | checks: |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2258 | /* mandatory substatements */ |
| 2259 | if (!leaf->type.name) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 2260 | LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "leaf"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2261 | return LY_EVALID; |
| 2262 | } |
Radek Krejci | b1a5dcc | 2018-11-26 14:50:05 +0100 | [diff] [blame] | 2263 | if ((leaf->flags & LYS_MAND_TRUE) && (leaf->dflt)) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 2264 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMSCOMB, "mandatory", "default", "leaf"); |
Radek Krejci | b1a5dcc | 2018-11-26 14:50:05 +0100 | [diff] [blame] | 2265 | return LY_EVALID; |
| 2266 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2267 | |
| 2268 | return ret; |
| 2269 | } |
| 2270 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2271 | /** |
| 2272 | * @brief Parse the max-elements statement. |
| 2273 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2274 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2275 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 2276 | * @param[in,out] max Value to write to. |
| 2277 | * @param[in,out] flags Flags to write to. |
| 2278 | * @param[in,out] exts Extension instances to add to. |
| 2279 | * |
| 2280 | * @return LY_ERR values. |
| 2281 | */ |
Radek Krejci | 2d7a47b | 2019-05-16 13:34:10 +0200 | [diff] [blame] | 2282 | LY_ERR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 2283 | parse_maxelements(struct lys_parser_ctx *ctx, const char **data, uint32_t *max, uint16_t *flags, struct lysp_ext_instance **exts) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2284 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2285 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2286 | char *buf, *word, *ptr; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 2287 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2288 | unsigned long int num; |
| 2289 | enum yang_keyword kw; |
| 2290 | |
| 2291 | if (*flags & LYS_SET_MAX) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 2292 | LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "max-elements"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2293 | return LY_EVALID; |
| 2294 | } |
| 2295 | *flags |= LYS_SET_MAX; |
| 2296 | |
| 2297 | /* get value */ |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 2298 | LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2299 | |
| 2300 | if (!word_len || (word[0] == '0') || ((word[0] != 'u') && !isdigit(word[0]))) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 2301 | LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "max-elements"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2302 | free(buf); |
| 2303 | return LY_EVALID; |
| 2304 | } |
| 2305 | |
| 2306 | if (strncmp(word, "unbounded", word_len)) { |
| 2307 | errno = 0; |
| 2308 | num = strtoul(word, &ptr, 10); |
| 2309 | /* we have not parsed the whole argument */ |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 2310 | if ((size_t)(ptr - word) != word_len) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 2311 | LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "max-elements"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2312 | free(buf); |
| 2313 | return LY_EVALID; |
| 2314 | } |
| 2315 | if ((errno == ERANGE) || (num > UINT32_MAX)) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 2316 | LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, "max-elements"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2317 | free(buf); |
| 2318 | return LY_EVALID; |
| 2319 | } |
| 2320 | |
| 2321 | *max = num; |
| 2322 | } |
| 2323 | free(buf); |
| 2324 | |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 2325 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2326 | switch (kw) { |
| 2327 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2328 | 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] | 2329 | break; |
| 2330 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 2331 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "max-elements"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2332 | return LY_EVALID; |
| 2333 | } |
| 2334 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2335 | return ret; |
| 2336 | } |
| 2337 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2338 | /** |
| 2339 | * @brief Parse the min-elements statement. |
| 2340 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2341 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2342 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 2343 | * @param[in,out] min Value to write to. |
| 2344 | * @param[in,out] flags Flags to write to. |
| 2345 | * @param[in,out] exts Extension instances to add to. |
| 2346 | * |
| 2347 | * @return LY_ERR values. |
| 2348 | */ |
Radek Krejci | 2d7a47b | 2019-05-16 13:34:10 +0200 | [diff] [blame] | 2349 | LY_ERR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 2350 | parse_minelements(struct lys_parser_ctx *ctx, const char **data, uint32_t *min, uint16_t *flags, struct lysp_ext_instance **exts) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2351 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2352 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2353 | char *buf, *word, *ptr; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 2354 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2355 | unsigned long int num; |
| 2356 | enum yang_keyword kw; |
| 2357 | |
| 2358 | if (*flags & LYS_SET_MIN) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 2359 | LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "min-elements"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2360 | return LY_EVALID; |
| 2361 | } |
| 2362 | *flags |= LYS_SET_MIN; |
| 2363 | |
| 2364 | /* get value */ |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 2365 | LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2366 | |
| 2367 | if (!word_len || !isdigit(word[0]) || ((word[0] == '0') && (word_len > 1))) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 2368 | LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "min-elements"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2369 | free(buf); |
| 2370 | return LY_EVALID; |
| 2371 | } |
| 2372 | |
| 2373 | errno = 0; |
| 2374 | num = strtoul(word, &ptr, 10); |
| 2375 | /* we have not parsed the whole argument */ |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 2376 | if ((size_t)(ptr - word) != word_len) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 2377 | LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "min-elements"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2378 | free(buf); |
| 2379 | return LY_EVALID; |
| 2380 | } |
| 2381 | if ((errno == ERANGE) || (num > UINT32_MAX)) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 2382 | LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, "min-elements"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2383 | free(buf); |
| 2384 | return LY_EVALID; |
| 2385 | } |
| 2386 | *min = num; |
| 2387 | free(buf); |
| 2388 | |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 2389 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2390 | switch (kw) { |
| 2391 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2392 | 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] | 2393 | break; |
| 2394 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 2395 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "min-elements"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2396 | return LY_EVALID; |
| 2397 | } |
| 2398 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2399 | return ret; |
| 2400 | } |
| 2401 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2402 | /** |
| 2403 | * @brief Parse the ordered-by statement. |
| 2404 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2405 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2406 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 2407 | * @param[in,out] flags Flags to write to. |
| 2408 | * @param[in,out] exts Extension instances to add to. |
| 2409 | * |
| 2410 | * @return LY_ERR values. |
| 2411 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2412 | static LY_ERR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 2413 | parse_orderedby(struct lys_parser_ctx *ctx, const char **data, uint16_t *flags, struct lysp_ext_instance **exts) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2414 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2415 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2416 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 2417 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2418 | enum yang_keyword kw; |
| 2419 | |
| 2420 | if (*flags & LYS_ORDBY_MASK) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 2421 | LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "ordered-by"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2422 | return LY_EVALID; |
| 2423 | } |
| 2424 | |
| 2425 | /* get value */ |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 2426 | LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2427 | |
| 2428 | if ((word_len == 6) && !strncmp(word, "system", word_len)) { |
| 2429 | *flags |= LYS_ORDBY_SYSTEM; |
| 2430 | } else if ((word_len == 4) && !strncmp(word, "user", word_len)) { |
| 2431 | *flags |= LYS_ORDBY_USER; |
| 2432 | } else { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 2433 | LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "ordered-by"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2434 | free(buf); |
| 2435 | return LY_EVALID; |
| 2436 | } |
| 2437 | free(buf); |
| 2438 | |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 2439 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2440 | switch (kw) { |
| 2441 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2442 | 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] | 2443 | break; |
| 2444 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 2445 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "ordered-by"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2446 | return LY_EVALID; |
| 2447 | } |
| 2448 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2449 | return ret; |
| 2450 | } |
| 2451 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2452 | /** |
| 2453 | * @brief Parse the leaf-list statement. |
| 2454 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2455 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2456 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 2457 | * @param[in,out] siblings Siblings to add to. |
| 2458 | * |
| 2459 | * @return LY_ERR values. |
| 2460 | */ |
Radek Krejci | 2d7a47b | 2019-05-16 13:34:10 +0200 | [diff] [blame] | 2461 | LY_ERR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 2462 | parse_leaflist(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2463 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2464 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2465 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 2466 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2467 | enum yang_keyword kw; |
| 2468 | struct lysp_node *iter; |
| 2469 | struct lysp_node_leaflist *llist; |
| 2470 | |
| 2471 | /* create structure */ |
| 2472 | llist = calloc(1, sizeof *llist); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2473 | LY_CHECK_ERR_RET(!llist, LOGMEM(ctx->ctx), LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2474 | llist->nodetype = LYS_LEAFLIST; |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2475 | llist->parent = parent; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2476 | |
| 2477 | /* insert into siblings */ |
| 2478 | if (!*siblings) { |
| 2479 | *siblings = (struct lysp_node *)llist; |
| 2480 | } else { |
| 2481 | for (iter = *siblings; iter->next; iter = iter->next); |
| 2482 | iter->next = (struct lysp_node *)llist; |
| 2483 | } |
| 2484 | |
| 2485 | /* get name */ |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 2486 | LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len)); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2487 | INSERT_WORD(ctx, buf, llist->name, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2488 | |
| 2489 | /* parse substatements */ |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 2490 | 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] | 2491 | switch (kw) { |
| 2492 | case YANG_CONFIG: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2493 | LY_CHECK_RET(parse_config(ctx, data, &llist->flags, &llist->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2494 | break; |
| 2495 | case YANG_DEFAULT: |
Radek Krejci | ceaf212 | 2019-01-02 15:03:26 +0100 | [diff] [blame] | 2496 | YANG_CHECK_STMTVER2_RET(ctx, "default", "leaf-list"); |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2497 | 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] | 2498 | break; |
| 2499 | case YANG_DESCRIPTION: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2500 | 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] | 2501 | break; |
| 2502 | case YANG_IF_FEATURE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2503 | 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] | 2504 | break; |
| 2505 | case YANG_MAX_ELEMENTS: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2506 | 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] | 2507 | break; |
| 2508 | case YANG_MIN_ELEMENTS: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2509 | 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] | 2510 | break; |
| 2511 | case YANG_MUST: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2512 | LY_CHECK_RET(parse_restrs(ctx, data, kw, &llist->musts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2513 | break; |
| 2514 | case YANG_ORDERED_BY: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2515 | LY_CHECK_RET(parse_orderedby(ctx, data, &llist->flags, &llist->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2516 | break; |
| 2517 | case YANG_REFERENCE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2518 | 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] | 2519 | break; |
| 2520 | case YANG_STATUS: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2521 | LY_CHECK_RET(parse_status(ctx, data, &llist->flags, &llist->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2522 | break; |
| 2523 | case YANG_TYPE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2524 | LY_CHECK_RET(parse_type(ctx, data, &llist->type)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2525 | break; |
| 2526 | case YANG_UNITS: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2527 | 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] | 2528 | break; |
| 2529 | case YANG_WHEN: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2530 | LY_CHECK_RET(parse_when(ctx, data, &llist->when)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2531 | break; |
| 2532 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2533 | 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] | 2534 | break; |
| 2535 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 2536 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "llist"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2537 | return LY_EVALID; |
| 2538 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2539 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 2540 | LY_CHECK_RET(ret); |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 2541 | checks: |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2542 | /* mandatory substatements */ |
| 2543 | if (!llist->type.name) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 2544 | LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "leaf-list"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2545 | return LY_EVALID; |
| 2546 | } |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 2547 | if ((llist->min) && (llist->dflts)) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 2548 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMSCOMB, "min-elements", "default", "leaf-list"); |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 2549 | return LY_EVALID; |
| 2550 | } |
Radek Krejci | df6cad1 | 2018-11-28 17:10:55 +0100 | [diff] [blame] | 2551 | if (llist->max && llist->min > llist->max) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 2552 | LOGVAL_PARSER(ctx, LYVE_SEMANTICS, |
Radek Krejci | df6cad1 | 2018-11-28 17:10:55 +0100 | [diff] [blame] | 2553 | "Invalid combination of min-elements and max-elements: min value %u is bigger than the max value %u.", |
| 2554 | llist->min, llist->max); |
| 2555 | return LY_EVALID; |
| 2556 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2557 | |
| 2558 | return ret; |
| 2559 | } |
| 2560 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2561 | /** |
| 2562 | * @brief Parse the refine statement. |
| 2563 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2564 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2565 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 2566 | * @param[in,out] refines Refines to add to. |
| 2567 | * |
| 2568 | * @return LY_ERR values. |
| 2569 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2570 | static LY_ERR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 2571 | parse_refine(struct lys_parser_ctx *ctx, const char **data, struct lysp_refine **refines) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2572 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2573 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2574 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 2575 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2576 | enum yang_keyword kw; |
| 2577 | struct lysp_refine *rf; |
| 2578 | |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 2579 | LY_ARRAY_NEW_RET(ctx->ctx, *refines, rf, LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2580 | |
| 2581 | /* get value */ |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 2582 | LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len)); |
Radek Krejci | f09e4e8 | 2019-06-14 15:08:11 +0200 | [diff] [blame] | 2583 | YANG_CHECK_NONEMPTY(ctx, NULL, word_len, "refine"); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2584 | INSERT_WORD(ctx, buf, rf->nodeid, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 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_CONFIG: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2589 | LY_CHECK_RET(parse_config(ctx, data, &rf->flags, &rf->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2590 | break; |
| 2591 | case YANG_DEFAULT: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2592 | 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] | 2593 | break; |
| 2594 | case YANG_DESCRIPTION: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2595 | 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] | 2596 | break; |
| 2597 | case YANG_IF_FEATURE: |
Radek Krejci | ceaf212 | 2019-01-02 15:03:26 +0100 | [diff] [blame] | 2598 | YANG_CHECK_STMTVER2_RET(ctx, "if-feature", "refine"); |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2599 | 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] | 2600 | break; |
| 2601 | case YANG_MAX_ELEMENTS: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2602 | 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] | 2603 | break; |
| 2604 | case YANG_MIN_ELEMENTS: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2605 | 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] | 2606 | break; |
| 2607 | case YANG_MUST: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2608 | LY_CHECK_RET(parse_restrs(ctx, data, kw, &rf->musts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2609 | break; |
| 2610 | case YANG_MANDATORY: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2611 | LY_CHECK_RET(parse_mandatory(ctx, data, &rf->flags, &rf->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2612 | break; |
| 2613 | case YANG_REFERENCE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2614 | 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] | 2615 | break; |
| 2616 | case YANG_PRESENCE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2617 | 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] | 2618 | break; |
| 2619 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2620 | 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] | 2621 | break; |
| 2622 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 2623 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "refine"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2624 | return LY_EVALID; |
| 2625 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2626 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2627 | return ret; |
| 2628 | } |
| 2629 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2630 | /** |
| 2631 | * @brief Parse the typedef statement. |
| 2632 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2633 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2634 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 2635 | * @param[in,out] typedefs Typedefs to add to. |
| 2636 | * |
| 2637 | * @return LY_ERR values. |
| 2638 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2639 | static LY_ERR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 2640 | parse_typedef(struct lys_parser_ctx *ctx, struct lysp_node *parent, const char **data, struct lysp_tpdf **typedefs) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2641 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2642 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2643 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 2644 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2645 | enum yang_keyword kw; |
| 2646 | struct lysp_tpdf *tpdf; |
| 2647 | |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 2648 | LY_ARRAY_NEW_RET(ctx->ctx, *typedefs, tpdf, LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2649 | |
| 2650 | /* get value */ |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 2651 | LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len)); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2652 | INSERT_WORD(ctx, buf, tpdf->name, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2653 | |
| 2654 | /* parse substatements */ |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 2655 | 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] | 2656 | switch (kw) { |
| 2657 | case YANG_DEFAULT: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2658 | 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] | 2659 | break; |
| 2660 | case YANG_DESCRIPTION: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2661 | 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] | 2662 | break; |
| 2663 | case YANG_REFERENCE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2664 | 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] | 2665 | break; |
| 2666 | case YANG_STATUS: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2667 | LY_CHECK_RET(parse_status(ctx, data, &tpdf->flags, &tpdf->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2668 | break; |
| 2669 | case YANG_TYPE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2670 | LY_CHECK_RET(parse_type(ctx, data, &tpdf->type)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2671 | break; |
| 2672 | case YANG_UNITS: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2673 | 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] | 2674 | break; |
| 2675 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2676 | 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] | 2677 | break; |
| 2678 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 2679 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "typedef"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2680 | return LY_EVALID; |
| 2681 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2682 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 2683 | LY_CHECK_RET(ret); |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 2684 | checks: |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2685 | /* mandatory substatements */ |
| 2686 | if (!tpdf->type.name) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 2687 | LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "typedef"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2688 | return LY_EVALID; |
| 2689 | } |
| 2690 | |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 2691 | /* store data for collision check */ |
Radek Krejci | 7fc6829 | 2019-06-12 13:51:09 +0200 | [diff] [blame] | 2692 | if (parent && !(parent->nodetype & (LYS_GROUPING | LYS_ACTION | LYS_INOUT | LYS_NOTIF))) { |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 2693 | ly_set_add(&ctx->tpdfs_nodes, parent, 0); |
| 2694 | } |
| 2695 | |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2696 | return ret; |
| 2697 | } |
| 2698 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2699 | /** |
| 2700 | * @brief Parse the input or output statement. |
| 2701 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2702 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2703 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 2704 | * @param[in] kw Type of this particular keyword |
| 2705 | * @param[in,out] inout_p Input/output pointer to write to. |
| 2706 | * |
| 2707 | * @return LY_ERR values. |
| 2708 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2709 | static LY_ERR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 2710 | parse_inout(struct lys_parser_ctx *ctx, const char **data, enum yang_keyword inout_kw, struct lysp_node *parent, struct lysp_action_inout *inout_p) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2711 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2712 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2713 | char *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 2714 | size_t word_len; |
Radek Krejci | 1011365 | 2018-11-14 16:56:50 +0100 | [diff] [blame] | 2715 | enum yang_keyword kw; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2716 | |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 2717 | if (inout_p->nodetype) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 2718 | LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(inout_kw)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2719 | return LY_EVALID; |
| 2720 | } |
| 2721 | |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 2722 | /* initiate structure */ |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 2723 | inout_p->nodetype = &((struct lysp_action*)parent)->input == inout_p ? LYS_INPUT : LYS_OUTPUT; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 2724 | inout_p->parent = parent; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2725 | |
| 2726 | /* parse substatements */ |
Radek Krejci | 7fc6829 | 2019-06-12 13:51:09 +0200 | [diff] [blame] | 2727 | 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] | 2728 | switch (kw) { |
| 2729 | case YANG_ANYDATA: |
Radek Krejci | ceaf212 | 2019-01-02 15:03:26 +0100 | [diff] [blame] | 2730 | YANG_CHECK_STMTVER2_RET(ctx, "anydata", ly_stmt2str(inout_kw)); |
Radek Krejci | 1011365 | 2018-11-14 16:56:50 +0100 | [diff] [blame] | 2731 | /* fall through */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2732 | case YANG_ANYXML: |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 2733 | LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)inout_p, &inout_p->data)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2734 | break; |
| 2735 | case YANG_CHOICE: |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 2736 | LY_CHECK_RET(parse_choice(ctx, data, (struct lysp_node*)inout_p, &inout_p->data)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2737 | break; |
| 2738 | case YANG_CONTAINER: |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 2739 | LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)inout_p, &inout_p->data)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2740 | break; |
| 2741 | case YANG_LEAF: |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 2742 | LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)inout_p, &inout_p->data)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2743 | break; |
| 2744 | case YANG_LEAF_LIST: |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 2745 | LY_CHECK_RET(parse_leaflist(ctx, data, (struct lysp_node*)inout_p, &inout_p->data)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2746 | break; |
| 2747 | case YANG_LIST: |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 2748 | LY_CHECK_RET(parse_list(ctx, data, (struct lysp_node*)inout_p, &inout_p->data)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2749 | break; |
| 2750 | case YANG_USES: |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 2751 | LY_CHECK_RET(parse_uses(ctx, data, (struct lysp_node*)inout_p, &inout_p->data)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2752 | break; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2753 | case YANG_TYPEDEF: |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 2754 | LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node*)inout_p, data, &inout_p->typedefs)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2755 | break; |
| 2756 | case YANG_MUST: |
Radek Krejci | ceaf212 | 2019-01-02 15:03:26 +0100 | [diff] [blame] | 2757 | YANG_CHECK_STMTVER2_RET(ctx, "must", ly_stmt2str(inout_kw)); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 2758 | LY_CHECK_RET(parse_restrs(ctx, data, kw, &inout_p->musts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2759 | break; |
| 2760 | case YANG_GROUPING: |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 2761 | LY_CHECK_RET(parse_grouping(ctx, data, (struct lysp_node*)inout_p, &inout_p->groupings)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2762 | break; |
| 2763 | case YANG_CUSTOM: |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 2764 | LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &inout_p->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2765 | break; |
| 2766 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 2767 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(inout_kw)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2768 | return LY_EVALID; |
| 2769 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2770 | } |
Radek Krejci | 7fc6829 | 2019-06-12 13:51:09 +0200 | [diff] [blame] | 2771 | LY_CHECK_RET(ret); |
| 2772 | checks: |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 2773 | /* finalize parent pointers to the reallocated items */ |
Radek Krejci | 7fc6829 | 2019-06-12 13:51:09 +0200 | [diff] [blame] | 2774 | LY_CHECK_RET(parse_finalize_reallocated(ctx, inout_p->groupings, NULL, NULL, NULL)); |
| 2775 | |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2776 | return ret; |
| 2777 | } |
| 2778 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2779 | /** |
| 2780 | * @brief Parse the action statement. |
| 2781 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2782 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2783 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 2784 | * @param[in,out] actions Actions to add to. |
| 2785 | * |
| 2786 | * @return LY_ERR values. |
| 2787 | */ |
Radek Krejci | 2d7a47b | 2019-05-16 13:34:10 +0200 | [diff] [blame] | 2788 | LY_ERR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 2789 | parse_action(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_action **actions) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2790 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2791 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2792 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 2793 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2794 | enum yang_keyword kw; |
| 2795 | struct lysp_action *act; |
| 2796 | |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 2797 | LY_ARRAY_NEW_RET(ctx->ctx, *actions, act, LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2798 | |
| 2799 | /* get value */ |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 2800 | LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len)); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2801 | INSERT_WORD(ctx, buf, act->name, word, word_len); |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2802 | act->nodetype = LYS_ACTION; |
| 2803 | act->parent = parent; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2804 | |
Radek Krejci | 7fc6829 | 2019-06-12 13:51:09 +0200 | [diff] [blame] | 2805 | 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] | 2806 | switch (kw) { |
| 2807 | case YANG_DESCRIPTION: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2808 | 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] | 2809 | break; |
| 2810 | case YANG_IF_FEATURE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2811 | 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] | 2812 | break; |
| 2813 | case YANG_REFERENCE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2814 | 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] | 2815 | break; |
| 2816 | case YANG_STATUS: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2817 | LY_CHECK_RET(parse_status(ctx, data, &act->flags, &act->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2818 | break; |
| 2819 | |
| 2820 | case YANG_INPUT: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2821 | 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] | 2822 | break; |
| 2823 | case YANG_OUTPUT: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2824 | 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] | 2825 | break; |
| 2826 | |
| 2827 | case YANG_TYPEDEF: |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 2828 | 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] | 2829 | break; |
| 2830 | case YANG_GROUPING: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2831 | 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] | 2832 | break; |
| 2833 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2834 | 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] | 2835 | break; |
| 2836 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 2837 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), parent ? "action" : "rpc"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2838 | return LY_EVALID; |
| 2839 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2840 | } |
Radek Krejci | 7fc6829 | 2019-06-12 13:51:09 +0200 | [diff] [blame] | 2841 | LY_CHECK_RET(ret); |
| 2842 | checks: |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 2843 | /* finalize parent pointers to the reallocated items */ |
Radek Krejci | 7fc6829 | 2019-06-12 13:51:09 +0200 | [diff] [blame] | 2844 | LY_CHECK_RET(parse_finalize_reallocated(ctx, act->groupings, NULL, NULL, NULL)); |
| 2845 | |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2846 | return ret; |
| 2847 | } |
| 2848 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2849 | /** |
| 2850 | * @brief Parse the notification statement. |
| 2851 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2852 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2853 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 2854 | * @param[in,out] notifs Notifications to add to. |
| 2855 | * |
| 2856 | * @return LY_ERR values. |
| 2857 | */ |
Radek Krejci | 2d7a47b | 2019-05-16 13:34:10 +0200 | [diff] [blame] | 2858 | LY_ERR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 2859 | parse_notif(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_notif **notifs) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2860 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2861 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2862 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 2863 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2864 | enum yang_keyword kw; |
| 2865 | struct lysp_notif *notif; |
| 2866 | |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 2867 | LY_ARRAY_NEW_RET(ctx->ctx, *notifs, notif, LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2868 | |
| 2869 | /* get value */ |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 2870 | LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len)); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2871 | INSERT_WORD(ctx, buf, notif->name, word, word_len); |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2872 | notif->nodetype = LYS_NOTIF; |
| 2873 | notif->parent = parent; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2874 | |
Radek Krejci | 7fc6829 | 2019-06-12 13:51:09 +0200 | [diff] [blame] | 2875 | 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] | 2876 | switch (kw) { |
| 2877 | case YANG_DESCRIPTION: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2878 | 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] | 2879 | break; |
| 2880 | case YANG_IF_FEATURE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2881 | 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] | 2882 | break; |
| 2883 | case YANG_REFERENCE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2884 | 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] | 2885 | break; |
| 2886 | case YANG_STATUS: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2887 | LY_CHECK_RET(parse_status(ctx, data, ¬if->flags, ¬if->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2888 | break; |
| 2889 | |
| 2890 | case YANG_ANYDATA: |
Radek Krejci | ceaf212 | 2019-01-02 15:03:26 +0100 | [diff] [blame] | 2891 | YANG_CHECK_STMTVER2_RET(ctx, "anydata", "notification"); |
Radek Krejci | 1011365 | 2018-11-14 16:56:50 +0100 | [diff] [blame] | 2892 | /* fall through */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2893 | case YANG_ANYXML: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2894 | 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] | 2895 | break; |
| 2896 | case YANG_CHOICE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2897 | 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] | 2898 | break; |
| 2899 | case YANG_CONTAINER: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2900 | 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] | 2901 | break; |
| 2902 | case YANG_LEAF: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2903 | 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] | 2904 | break; |
| 2905 | case YANG_LEAF_LIST: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2906 | 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] | 2907 | break; |
| 2908 | case YANG_LIST: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2909 | 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] | 2910 | break; |
| 2911 | case YANG_USES: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2912 | 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] | 2913 | break; |
| 2914 | |
| 2915 | case YANG_MUST: |
Radek Krejci | ceaf212 | 2019-01-02 15:03:26 +0100 | [diff] [blame] | 2916 | YANG_CHECK_STMTVER2_RET(ctx, "must", "notification"); |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2917 | LY_CHECK_RET(parse_restrs(ctx, data, kw, ¬if->musts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2918 | break; |
| 2919 | case YANG_TYPEDEF: |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 2920 | 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] | 2921 | break; |
| 2922 | case YANG_GROUPING: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2923 | 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] | 2924 | break; |
| 2925 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2926 | 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] | 2927 | break; |
| 2928 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 2929 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "notification"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2930 | return LY_EVALID; |
| 2931 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2932 | } |
Radek Krejci | 7fc6829 | 2019-06-12 13:51:09 +0200 | [diff] [blame] | 2933 | LY_CHECK_RET(ret); |
| 2934 | checks: |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 2935 | /* finalize parent pointers to the reallocated items */ |
Radek Krejci | 7fc6829 | 2019-06-12 13:51:09 +0200 | [diff] [blame] | 2936 | LY_CHECK_RET(parse_finalize_reallocated(ctx, notif->groupings, NULL, NULL, NULL)); |
| 2937 | |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2938 | return ret; |
| 2939 | } |
| 2940 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2941 | /** |
| 2942 | * @brief Parse the grouping statement. |
| 2943 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2944 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 2945 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 2946 | * @param[in,out] groupings Groupings to add to. |
| 2947 | * |
| 2948 | * @return LY_ERR values. |
| 2949 | */ |
Radek Krejci | 2d7a47b | 2019-05-16 13:34:10 +0200 | [diff] [blame] | 2950 | LY_ERR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 2951 | parse_grouping(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_grp **groupings) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2952 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2953 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2954 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 2955 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2956 | enum yang_keyword kw; |
| 2957 | struct lysp_grp *grp; |
| 2958 | |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 2959 | LY_ARRAY_NEW_RET(ctx->ctx, *groupings, grp, LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2960 | |
| 2961 | /* get value */ |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 2962 | LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len)); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2963 | INSERT_WORD(ctx, buf, grp->name, word, word_len); |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2964 | grp->nodetype = LYS_GROUPING; |
| 2965 | grp->parent = parent; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2966 | |
Radek Krejci | 7fc6829 | 2019-06-12 13:51:09 +0200 | [diff] [blame] | 2967 | 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] | 2968 | switch (kw) { |
| 2969 | case YANG_DESCRIPTION: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2970 | 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] | 2971 | break; |
| 2972 | case YANG_REFERENCE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2973 | 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] | 2974 | break; |
| 2975 | case YANG_STATUS: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2976 | LY_CHECK_RET(parse_status(ctx, data, &grp->flags, &grp->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2977 | break; |
| 2978 | |
| 2979 | case YANG_ANYDATA: |
Radek Krejci | ceaf212 | 2019-01-02 15:03:26 +0100 | [diff] [blame] | 2980 | YANG_CHECK_STMTVER2_RET(ctx, "anydata", "grouping"); |
Radek Krejci | 1011365 | 2018-11-14 16:56:50 +0100 | [diff] [blame] | 2981 | /* fall through */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 2982 | case YANG_ANYXML: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2983 | 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] | 2984 | break; |
| 2985 | case YANG_CHOICE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2986 | 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] | 2987 | break; |
| 2988 | case YANG_CONTAINER: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2989 | 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] | 2990 | break; |
| 2991 | case YANG_LEAF: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2992 | 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] | 2993 | break; |
| 2994 | case YANG_LEAF_LIST: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2995 | 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] | 2996 | break; |
| 2997 | case YANG_LIST: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 2998 | 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] | 2999 | break; |
| 3000 | case YANG_USES: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3001 | 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] | 3002 | break; |
| 3003 | |
| 3004 | case YANG_TYPEDEF: |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 3005 | 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] | 3006 | break; |
| 3007 | case YANG_ACTION: |
Radek Krejci | ceaf212 | 2019-01-02 15:03:26 +0100 | [diff] [blame] | 3008 | YANG_CHECK_STMTVER2_RET(ctx, "action", "grouping"); |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3009 | 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] | 3010 | break; |
| 3011 | case YANG_GROUPING: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3012 | 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] | 3013 | break; |
| 3014 | case YANG_NOTIFICATION: |
Radek Krejci | ceaf212 | 2019-01-02 15:03:26 +0100 | [diff] [blame] | 3015 | YANG_CHECK_STMTVER2_RET(ctx, "notification", "grouping"); |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3016 | 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] | 3017 | break; |
| 3018 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3019 | 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] | 3020 | break; |
| 3021 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 3022 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "grouping"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3023 | return LY_EVALID; |
| 3024 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3025 | } |
Radek Krejci | 7fc6829 | 2019-06-12 13:51:09 +0200 | [diff] [blame] | 3026 | LY_CHECK_RET(ret); |
| 3027 | checks: |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 3028 | /* finalize parent pointers to the reallocated items */ |
Radek Krejci | 7fc6829 | 2019-06-12 13:51:09 +0200 | [diff] [blame] | 3029 | LY_CHECK_RET(parse_finalize_reallocated(ctx, grp->groupings, NULL, grp->actions, grp->notifs)); |
| 3030 | |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3031 | return ret; |
| 3032 | } |
| 3033 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3034 | /** |
| 3035 | * @brief Parse the refine statement. |
| 3036 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3037 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3038 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 3039 | * @param[in,out] augments Augments to add to. |
| 3040 | * |
| 3041 | * @return LY_ERR values. |
| 3042 | */ |
Radek Krejci | 2d7a47b | 2019-05-16 13:34:10 +0200 | [diff] [blame] | 3043 | LY_ERR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 3044 | parse_augment(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_augment **augments) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3045 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3046 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3047 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 3048 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3049 | enum yang_keyword kw; |
| 3050 | struct lysp_augment *aug; |
| 3051 | |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 3052 | LY_ARRAY_NEW_RET(ctx->ctx, *augments, aug, LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3053 | |
| 3054 | /* get value */ |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 3055 | LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len)); |
Radek Krejci | f09e4e8 | 2019-06-14 15:08:11 +0200 | [diff] [blame] | 3056 | YANG_CHECK_NONEMPTY(ctx, NULL, word_len, "augment"); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3057 | INSERT_WORD(ctx, buf, aug->nodeid, word, word_len); |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3058 | aug->nodetype = LYS_AUGMENT; |
| 3059 | aug->parent = parent; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3060 | |
Radek Krejci | 7fc6829 | 2019-06-12 13:51:09 +0200 | [diff] [blame] | 3061 | 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] | 3062 | switch (kw) { |
| 3063 | case YANG_DESCRIPTION: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3064 | 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] | 3065 | break; |
| 3066 | case YANG_IF_FEATURE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3067 | 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] | 3068 | break; |
| 3069 | case YANG_REFERENCE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3070 | 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] | 3071 | break; |
| 3072 | case YANG_STATUS: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3073 | LY_CHECK_RET(parse_status(ctx, data, &aug->flags, &aug->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3074 | break; |
| 3075 | case YANG_WHEN: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3076 | LY_CHECK_RET(parse_when(ctx, data, &aug->when)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3077 | break; |
| 3078 | |
| 3079 | case YANG_ANYDATA: |
Radek Krejci | ceaf212 | 2019-01-02 15:03:26 +0100 | [diff] [blame] | 3080 | YANG_CHECK_STMTVER2_RET(ctx, "anydata", "augment"); |
Radek Krejci | 1011365 | 2018-11-14 16:56:50 +0100 | [diff] [blame] | 3081 | /* fall through */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3082 | case YANG_ANYXML: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3083 | 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] | 3084 | break; |
| 3085 | case YANG_CASE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3086 | 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] | 3087 | break; |
| 3088 | case YANG_CHOICE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3089 | 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] | 3090 | break; |
| 3091 | case YANG_CONTAINER: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3092 | 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] | 3093 | break; |
| 3094 | case YANG_LEAF: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3095 | 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] | 3096 | break; |
| 3097 | case YANG_LEAF_LIST: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3098 | 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] | 3099 | break; |
| 3100 | case YANG_LIST: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3101 | 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] | 3102 | break; |
| 3103 | case YANG_USES: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3104 | 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] | 3105 | break; |
| 3106 | |
| 3107 | case YANG_ACTION: |
Radek Krejci | ceaf212 | 2019-01-02 15:03:26 +0100 | [diff] [blame] | 3108 | YANG_CHECK_STMTVER2_RET(ctx, "action", "augment"); |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3109 | 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] | 3110 | break; |
| 3111 | case YANG_NOTIFICATION: |
Radek Krejci | ceaf212 | 2019-01-02 15:03:26 +0100 | [diff] [blame] | 3112 | YANG_CHECK_STMTVER2_RET(ctx, "notification", "augment"); |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3113 | 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] | 3114 | break; |
| 3115 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3116 | 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] | 3117 | break; |
| 3118 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 3119 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "augment"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3120 | return LY_EVALID; |
| 3121 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3122 | } |
Radek Krejci | 7fc6829 | 2019-06-12 13:51:09 +0200 | [diff] [blame] | 3123 | LY_CHECK_RET(ret); |
| 3124 | checks: |
| 3125 | /* finalize parent pointers to the reallocated items */ |
| 3126 | LY_CHECK_RET(parse_finalize_reallocated(ctx, NULL, NULL, aug->actions, aug->notifs)); |
| 3127 | |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3128 | return ret; |
| 3129 | } |
| 3130 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3131 | /** |
Radek Krejci | 7fc6829 | 2019-06-12 13:51:09 +0200 | [diff] [blame] | 3132 | * @brief Finalize some of the structures in case they are stored in sized array, |
| 3133 | * which can be possibly reallocated and some other data may point to them. |
| 3134 | * |
| 3135 | * Update parent pointers in the nodes inside grouping/augment/RPC/Notification, which could be reallocated. |
| 3136 | * |
| 3137 | * @param[in] mod Parsed module to be updated. |
| 3138 | * @return LY_ERR value (currently only LY_SUCCESS, but it can change in future). |
| 3139 | */ |
| 3140 | static LY_ERR |
| 3141 | parse_finalize_reallocated(struct lys_parser_ctx *ctx, struct lysp_grp *groupings, struct lysp_augment *augments, |
| 3142 | struct lysp_action *actions, struct lysp_notif *notifs) |
| 3143 | { |
| 3144 | unsigned int u, v; |
| 3145 | struct lysp_node *child; |
| 3146 | |
| 3147 | /* finalize parent pointers to the reallocated items */ |
| 3148 | |
| 3149 | /* gropings */ |
| 3150 | LY_ARRAY_FOR(groupings, u) { |
| 3151 | LY_LIST_FOR(groupings[u].data, child) { |
| 3152 | child->parent = (struct lysp_node*)&groupings[u]; |
| 3153 | } |
| 3154 | LY_ARRAY_FOR(groupings[u].actions, v) { |
| 3155 | groupings[u].actions[v].parent = (struct lysp_node*)&groupings[u]; |
| 3156 | } |
| 3157 | LY_ARRAY_FOR(groupings[u].notifs, v) { |
| 3158 | groupings[u].notifs[v].parent = (struct lysp_node*)&groupings[u]; |
| 3159 | } |
| 3160 | LY_ARRAY_FOR(groupings[u].groupings, v) { |
| 3161 | groupings[u].groupings[v].parent = (struct lysp_node*)&groupings[u]; |
| 3162 | } |
| 3163 | if (groupings[u].typedefs) { |
| 3164 | ly_set_add(&ctx->tpdfs_nodes, &groupings[u], 0); |
| 3165 | } |
| 3166 | } |
| 3167 | |
| 3168 | /* augments */ |
| 3169 | LY_ARRAY_FOR(augments, u) { |
| 3170 | LY_LIST_FOR(augments[u].child, child) { |
| 3171 | child->parent = (struct lysp_node*)&augments[u]; |
| 3172 | } |
| 3173 | LY_ARRAY_FOR(augments[u].actions, v) { |
| 3174 | augments[u].actions[v].parent = (struct lysp_node*)&augments[u]; |
| 3175 | } |
| 3176 | LY_ARRAY_FOR(augments[u].notifs, v) { |
| 3177 | augments[u].notifs[v].parent = (struct lysp_node*)&augments[u]; |
| 3178 | } |
| 3179 | } |
| 3180 | |
| 3181 | /* actions */ |
| 3182 | LY_ARRAY_FOR(actions, u) { |
| 3183 | if (actions[u].input.parent) { |
| 3184 | actions[u].input.parent = (struct lysp_node*)&actions[u]; |
| 3185 | LY_LIST_FOR(actions[u].input.data, child) { |
| 3186 | child->parent = (struct lysp_node*)&actions[u].input; |
| 3187 | } |
| 3188 | LY_ARRAY_FOR(actions[u].input.groupings, v) { |
| 3189 | actions[u].input.groupings[v].parent = (struct lysp_node*)&actions[u].input; |
| 3190 | } |
| 3191 | if (actions[u].input.typedefs) { |
| 3192 | ly_set_add(&ctx->tpdfs_nodes, &actions[u].input, 0); |
| 3193 | } |
| 3194 | } |
| 3195 | if (actions[u].output.parent) { |
| 3196 | actions[u].output.parent = (struct lysp_node*)&actions[u]; |
| 3197 | LY_LIST_FOR(actions[u].output.data, child) { |
| 3198 | child->parent = (struct lysp_node*)&actions[u].output; |
| 3199 | } |
| 3200 | LY_ARRAY_FOR(actions[u].output.groupings, v) { |
| 3201 | actions[u].output.groupings[v].parent = (struct lysp_node*)&actions[u].output; |
| 3202 | } |
| 3203 | if (actions[u].output.typedefs) { |
| 3204 | ly_set_add(&ctx->tpdfs_nodes, &actions[u].output, 0); |
| 3205 | } |
| 3206 | } |
| 3207 | LY_ARRAY_FOR(actions[u].groupings, v) { |
| 3208 | actions[u].groupings[v].parent = (struct lysp_node*)&actions[u]; |
| 3209 | } |
| 3210 | if (actions[u].typedefs) { |
| 3211 | ly_set_add(&ctx->tpdfs_nodes, &actions[u], 0); |
| 3212 | } |
| 3213 | } |
| 3214 | |
| 3215 | /* notifications */ |
| 3216 | LY_ARRAY_FOR(notifs, u) { |
| 3217 | LY_LIST_FOR(notifs[u].data, child) { |
| 3218 | child->parent = (struct lysp_node*)¬ifs[u]; |
| 3219 | } |
| 3220 | LY_ARRAY_FOR(notifs[u].groupings, v) { |
| 3221 | notifs[u].groupings[v].parent = (struct lysp_node*)¬ifs[u]; |
| 3222 | } |
| 3223 | if (notifs[u].typedefs) { |
| 3224 | ly_set_add(&ctx->tpdfs_nodes, ¬ifs[u], 0); |
| 3225 | } |
| 3226 | } |
| 3227 | |
| 3228 | return LY_SUCCESS; |
| 3229 | } |
| 3230 | |
| 3231 | /** |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3232 | * @brief Parse the uses statement. |
| 3233 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3234 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3235 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 3236 | * @param[in,out] siblings Siblings to add to. |
| 3237 | * |
| 3238 | * @return LY_ERR values. |
| 3239 | */ |
Radek Krejci | 2d7a47b | 2019-05-16 13:34:10 +0200 | [diff] [blame] | 3240 | LY_ERR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 3241 | parse_uses(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3242 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3243 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3244 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 3245 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3246 | enum yang_keyword kw; |
| 3247 | struct lysp_node *iter; |
| 3248 | struct lysp_node_uses *uses; |
| 3249 | |
| 3250 | /* create structure */ |
| 3251 | uses = calloc(1, sizeof *uses); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3252 | LY_CHECK_ERR_RET(!uses, LOGMEM(ctx->ctx), LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3253 | uses->nodetype = LYS_USES; |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3254 | uses->parent = parent; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3255 | |
| 3256 | /* insert into siblings */ |
| 3257 | if (!*siblings) { |
| 3258 | *siblings = (struct lysp_node *)uses; |
| 3259 | } else { |
| 3260 | for (iter = *siblings; iter->next; iter = iter->next); |
| 3261 | iter->next = (struct lysp_node *)uses; |
| 3262 | } |
| 3263 | |
| 3264 | /* get name */ |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 3265 | LY_CHECK_RET(get_argument(ctx, data, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &word_len)); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3266 | INSERT_WORD(ctx, buf, uses->name, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3267 | |
| 3268 | /* parse substatements */ |
Radek Krejci | 7fc6829 | 2019-06-12 13:51:09 +0200 | [diff] [blame] | 3269 | 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] | 3270 | switch (kw) { |
| 3271 | case YANG_DESCRIPTION: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3272 | 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] | 3273 | break; |
| 3274 | case YANG_IF_FEATURE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3275 | 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] | 3276 | break; |
| 3277 | case YANG_REFERENCE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3278 | 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] | 3279 | break; |
| 3280 | case YANG_STATUS: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3281 | LY_CHECK_RET(parse_status(ctx, data, &uses->flags, &uses->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3282 | break; |
| 3283 | case YANG_WHEN: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3284 | LY_CHECK_RET(parse_when(ctx, data, &uses->when)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3285 | break; |
| 3286 | |
| 3287 | case YANG_REFINE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3288 | LY_CHECK_RET(parse_refine(ctx, data, &uses->refines)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3289 | break; |
| 3290 | case YANG_AUGMENT: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3291 | 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] | 3292 | break; |
| 3293 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3294 | 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] | 3295 | break; |
| 3296 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 3297 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "uses"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3298 | return LY_EVALID; |
| 3299 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3300 | } |
Radek Krejci | 7fc6829 | 2019-06-12 13:51:09 +0200 | [diff] [blame] | 3301 | checks: |
| 3302 | /* finalize parent pointers to the reallocated items */ |
| 3303 | LY_CHECK_RET(parse_finalize_reallocated(ctx, NULL, uses->augments, NULL, NULL)); |
| 3304 | |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3305 | return ret; |
| 3306 | } |
| 3307 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3308 | /** |
| 3309 | * @brief Parse the case statement. |
| 3310 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3311 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3312 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 3313 | * @param[in,out] siblings Siblings to add to. |
| 3314 | * |
| 3315 | * @return LY_ERR values. |
| 3316 | */ |
Radek Krejci | 2d7a47b | 2019-05-16 13:34:10 +0200 | [diff] [blame] | 3317 | LY_ERR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 3318 | parse_case(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3319 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3320 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3321 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 3322 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3323 | enum yang_keyword kw; |
| 3324 | struct lysp_node *iter; |
| 3325 | struct lysp_node_case *cas; |
| 3326 | |
| 3327 | /* create structure */ |
| 3328 | cas = calloc(1, sizeof *cas); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3329 | LY_CHECK_ERR_RET(!cas, LOGMEM(ctx->ctx), LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3330 | cas->nodetype = LYS_CASE; |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3331 | cas->parent = parent; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3332 | |
| 3333 | /* insert into siblings */ |
| 3334 | if (!*siblings) { |
| 3335 | *siblings = (struct lysp_node *)cas; |
| 3336 | } else { |
| 3337 | for (iter = *siblings; iter->next; iter = iter->next); |
| 3338 | iter->next = (struct lysp_node *)cas; |
| 3339 | } |
| 3340 | |
| 3341 | /* get name */ |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 3342 | LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len)); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3343 | INSERT_WORD(ctx, buf, cas->name, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3344 | |
| 3345 | /* parse substatements */ |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 3346 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3347 | switch (kw) { |
| 3348 | case YANG_DESCRIPTION: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3349 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &cas->dsc, Y_STR_ARG, &cas->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3350 | break; |
| 3351 | case YANG_IF_FEATURE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3352 | LY_CHECK_RET(parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &cas->iffeatures, Y_STR_ARG, &cas->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3353 | break; |
| 3354 | case YANG_REFERENCE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3355 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &cas->ref, Y_STR_ARG, &cas->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3356 | break; |
| 3357 | case YANG_STATUS: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3358 | LY_CHECK_RET(parse_status(ctx, data, &cas->flags, &cas->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3359 | break; |
| 3360 | case YANG_WHEN: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3361 | LY_CHECK_RET(parse_when(ctx, data, &cas->when)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3362 | break; |
| 3363 | |
| 3364 | case YANG_ANYDATA: |
Radek Krejci | ceaf212 | 2019-01-02 15:03:26 +0100 | [diff] [blame] | 3365 | YANG_CHECK_STMTVER2_RET(ctx, "anydata", "case"); |
Radek Krejci | 1011365 | 2018-11-14 16:56:50 +0100 | [diff] [blame] | 3366 | /* fall through */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3367 | case YANG_ANYXML: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3368 | 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] | 3369 | break; |
| 3370 | case YANG_CHOICE: |
Radek Krejci | a9026eb | 2018-12-12 16:04:47 +0100 | [diff] [blame] | 3371 | 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] | 3372 | break; |
| 3373 | case YANG_CONTAINER: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3374 | 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] | 3375 | break; |
| 3376 | case YANG_LEAF: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3377 | 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] | 3378 | break; |
| 3379 | case YANG_LEAF_LIST: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3380 | 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] | 3381 | break; |
| 3382 | case YANG_LIST: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3383 | 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] | 3384 | break; |
| 3385 | case YANG_USES: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3386 | 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] | 3387 | break; |
| 3388 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3389 | 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] | 3390 | break; |
| 3391 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 3392 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "case"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3393 | return LY_EVALID; |
| 3394 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3395 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3396 | return ret; |
| 3397 | } |
| 3398 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3399 | /** |
| 3400 | * @brief Parse the choice statement. |
| 3401 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3402 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3403 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 3404 | * @param[in,out] siblings Siblings to add to. |
| 3405 | * |
| 3406 | * @return LY_ERR values. |
| 3407 | */ |
Radek Krejci | 2d7a47b | 2019-05-16 13:34:10 +0200 | [diff] [blame] | 3408 | LY_ERR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 3409 | parse_choice(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3410 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3411 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3412 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 3413 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3414 | enum yang_keyword kw; |
| 3415 | struct lysp_node *iter; |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3416 | struct lysp_node_choice *choice; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3417 | |
| 3418 | /* create structure */ |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3419 | choice = calloc(1, sizeof *choice); |
| 3420 | LY_CHECK_ERR_RET(!choice, LOGMEM(ctx->ctx), LY_EMEM); |
| 3421 | choice->nodetype = LYS_CHOICE; |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3422 | choice->parent = parent; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3423 | |
| 3424 | /* insert into siblings */ |
| 3425 | if (!*siblings) { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3426 | *siblings = (struct lysp_node *)choice; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3427 | } else { |
| 3428 | for (iter = *siblings; iter->next; iter = iter->next); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3429 | iter->next = (struct lysp_node *)choice; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3430 | } |
| 3431 | |
| 3432 | /* get name */ |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 3433 | LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len)); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3434 | INSERT_WORD(ctx, buf, choice->name, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3435 | |
| 3436 | /* parse substatements */ |
Radek Krejci | a9026eb | 2018-12-12 16:04:47 +0100 | [diff] [blame] | 3437 | 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] | 3438 | switch (kw) { |
| 3439 | case YANG_CONFIG: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3440 | LY_CHECK_RET(parse_config(ctx, data, &choice->flags, &choice->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3441 | break; |
| 3442 | case YANG_DESCRIPTION: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3443 | 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] | 3444 | break; |
| 3445 | case YANG_IF_FEATURE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3446 | 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] | 3447 | break; |
| 3448 | case YANG_MANDATORY: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3449 | LY_CHECK_RET(parse_mandatory(ctx, data, &choice->flags, &choice->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3450 | break; |
| 3451 | case YANG_REFERENCE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3452 | 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] | 3453 | break; |
| 3454 | case YANG_STATUS: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3455 | LY_CHECK_RET(parse_status(ctx, data, &choice->flags, &choice->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3456 | break; |
| 3457 | case YANG_WHEN: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3458 | LY_CHECK_RET(parse_when(ctx, data, &choice->when)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3459 | break; |
| 3460 | case YANG_DEFAULT: |
Radek Krejci | a9026eb | 2018-12-12 16:04:47 +0100 | [diff] [blame] | 3461 | 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] | 3462 | break; |
| 3463 | |
| 3464 | case YANG_ANYDATA: |
Radek Krejci | ceaf212 | 2019-01-02 15:03:26 +0100 | [diff] [blame] | 3465 | YANG_CHECK_STMTVER2_RET(ctx, "anydata", "choice"); |
Radek Krejci | 1011365 | 2018-11-14 16:56:50 +0100 | [diff] [blame] | 3466 | /* fall through */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3467 | case YANG_ANYXML: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3468 | 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] | 3469 | break; |
| 3470 | case YANG_CASE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3471 | 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] | 3472 | break; |
| 3473 | case YANG_CHOICE: |
Radek Krejci | ceaf212 | 2019-01-02 15:03:26 +0100 | [diff] [blame] | 3474 | YANG_CHECK_STMTVER2_RET(ctx, "choice", "choice"); |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3475 | 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] | 3476 | break; |
| 3477 | case YANG_CONTAINER: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3478 | 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] | 3479 | break; |
| 3480 | case YANG_LEAF: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3481 | 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] | 3482 | break; |
| 3483 | case YANG_LEAF_LIST: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3484 | 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] | 3485 | break; |
| 3486 | case YANG_LIST: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3487 | 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] | 3488 | break; |
| 3489 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3490 | 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] | 3491 | break; |
| 3492 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 3493 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "choice"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3494 | return LY_EVALID; |
| 3495 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3496 | } |
Radek Krejci | a9026eb | 2018-12-12 16:04:47 +0100 | [diff] [blame] | 3497 | LY_CHECK_RET(ret); |
| 3498 | checks: |
| 3499 | if ((choice->flags & LYS_MAND_TRUE) && choice->dflt) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 3500 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMSCOMB, "mandatory", "default", "choice"); |
Radek Krejci | a9026eb | 2018-12-12 16:04:47 +0100 | [diff] [blame] | 3501 | return LY_EVALID; |
| 3502 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3503 | return ret; |
| 3504 | } |
| 3505 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3506 | /** |
| 3507 | * @brief Parse the container statement. |
| 3508 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3509 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3510 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 3511 | * @param[in,out] siblings Siblings to add to. |
| 3512 | * |
| 3513 | * @return LY_ERR values. |
| 3514 | */ |
Radek Krejci | 2d7a47b | 2019-05-16 13:34:10 +0200 | [diff] [blame] | 3515 | LY_ERR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 3516 | parse_container(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3517 | { |
| 3518 | LY_ERR ret = 0; |
| 3519 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 3520 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3521 | enum yang_keyword kw; |
| 3522 | struct lysp_node *iter; |
| 3523 | struct lysp_node_container *cont; |
| 3524 | |
| 3525 | /* create structure */ |
| 3526 | cont = calloc(1, sizeof *cont); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3527 | LY_CHECK_ERR_RET(!cont, LOGMEM(ctx->ctx), LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3528 | cont->nodetype = LYS_CONTAINER; |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3529 | cont->parent = parent; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3530 | |
| 3531 | /* insert into siblings */ |
| 3532 | if (!*siblings) { |
| 3533 | *siblings = (struct lysp_node *)cont; |
| 3534 | } else { |
| 3535 | for (iter = *siblings; iter->next; iter = iter->next); |
| 3536 | iter->next = (struct lysp_node *)cont; |
| 3537 | } |
| 3538 | |
| 3539 | /* get name */ |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 3540 | LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len)); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3541 | INSERT_WORD(ctx, buf, cont->name, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3542 | |
| 3543 | /* parse substatements */ |
Radek Krejci | 7fc6829 | 2019-06-12 13:51:09 +0200 | [diff] [blame] | 3544 | 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] | 3545 | switch (kw) { |
| 3546 | case YANG_CONFIG: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3547 | LY_CHECK_RET(parse_config(ctx, data, &cont->flags, &cont->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3548 | break; |
| 3549 | case YANG_DESCRIPTION: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3550 | 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] | 3551 | break; |
| 3552 | case YANG_IF_FEATURE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3553 | 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] | 3554 | break; |
| 3555 | case YANG_REFERENCE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3556 | 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] | 3557 | break; |
| 3558 | case YANG_STATUS: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3559 | LY_CHECK_RET(parse_status(ctx, data, &cont->flags, &cont->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3560 | break; |
| 3561 | case YANG_WHEN: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3562 | LY_CHECK_RET(parse_when(ctx, data, &cont->when)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3563 | break; |
| 3564 | case YANG_PRESENCE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3565 | 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] | 3566 | break; |
| 3567 | |
| 3568 | case YANG_ANYDATA: |
Radek Krejci | ceaf212 | 2019-01-02 15:03:26 +0100 | [diff] [blame] | 3569 | YANG_CHECK_STMTVER2_RET(ctx, "anydata", "container"); |
Radek Krejci | 1011365 | 2018-11-14 16:56:50 +0100 | [diff] [blame] | 3570 | /* fall through */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3571 | case YANG_ANYXML: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3572 | 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] | 3573 | break; |
| 3574 | case YANG_CHOICE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3575 | 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] | 3576 | break; |
| 3577 | case YANG_CONTAINER: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3578 | 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] | 3579 | break; |
| 3580 | case YANG_LEAF: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3581 | 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] | 3582 | break; |
| 3583 | case YANG_LEAF_LIST: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3584 | 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] | 3585 | break; |
| 3586 | case YANG_LIST: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3587 | 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] | 3588 | break; |
| 3589 | case YANG_USES: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3590 | 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] | 3591 | break; |
| 3592 | |
| 3593 | case YANG_TYPEDEF: |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 3594 | 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] | 3595 | break; |
| 3596 | case YANG_MUST: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3597 | LY_CHECK_RET(parse_restrs(ctx, data, kw, &cont->musts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3598 | break; |
| 3599 | case YANG_ACTION: |
Radek Krejci | ceaf212 | 2019-01-02 15:03:26 +0100 | [diff] [blame] | 3600 | YANG_CHECK_STMTVER2_RET(ctx, "action", "container"); |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3601 | 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] | 3602 | break; |
| 3603 | case YANG_GROUPING: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3604 | 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] | 3605 | break; |
| 3606 | case YANG_NOTIFICATION: |
Radek Krejci | ceaf212 | 2019-01-02 15:03:26 +0100 | [diff] [blame] | 3607 | YANG_CHECK_STMTVER2_RET(ctx, "notification", "container"); |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3608 | 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] | 3609 | break; |
| 3610 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3611 | 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] | 3612 | break; |
| 3613 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 3614 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "container"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3615 | return LY_EVALID; |
| 3616 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3617 | } |
Radek Krejci | 7fc6829 | 2019-06-12 13:51:09 +0200 | [diff] [blame] | 3618 | checks: |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 3619 | /* finalize parent pointers to the reallocated items */ |
Radek Krejci | 7fc6829 | 2019-06-12 13:51:09 +0200 | [diff] [blame] | 3620 | LY_CHECK_RET(parse_finalize_reallocated(ctx, cont->groupings, NULL, cont->actions, cont->notifs)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3621 | return ret; |
| 3622 | } |
| 3623 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3624 | /** |
| 3625 | * @brief Parse the list statement. |
| 3626 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3627 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3628 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 3629 | * @param[in,out] siblings Siblings to add to. |
| 3630 | * |
| 3631 | * @return LY_ERR values. |
| 3632 | */ |
Radek Krejci | 2d7a47b | 2019-05-16 13:34:10 +0200 | [diff] [blame] | 3633 | LY_ERR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 3634 | parse_list(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3635 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3636 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3637 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 3638 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3639 | enum yang_keyword kw; |
| 3640 | struct lysp_node *iter; |
| 3641 | struct lysp_node_list *list; |
| 3642 | |
| 3643 | /* create structure */ |
| 3644 | list = calloc(1, sizeof *list); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3645 | LY_CHECK_ERR_RET(!list, LOGMEM(ctx->ctx), LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3646 | list->nodetype = LYS_LIST; |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3647 | list->parent = parent; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3648 | |
| 3649 | /* insert into siblings */ |
| 3650 | if (!*siblings) { |
| 3651 | *siblings = (struct lysp_node *)list; |
| 3652 | } else { |
| 3653 | for (iter = *siblings; iter->next; iter = iter->next); |
| 3654 | iter->next = (struct lysp_node *)list; |
| 3655 | } |
| 3656 | |
| 3657 | /* get name */ |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 3658 | LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len)); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3659 | INSERT_WORD(ctx, buf, list->name, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3660 | |
| 3661 | /* parse substatements */ |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3662 | 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] | 3663 | switch (kw) { |
| 3664 | case YANG_CONFIG: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3665 | LY_CHECK_RET(parse_config(ctx, data, &list->flags, &list->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3666 | break; |
| 3667 | case YANG_DESCRIPTION: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3668 | 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] | 3669 | break; |
| 3670 | case YANG_IF_FEATURE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3671 | 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] | 3672 | break; |
| 3673 | case YANG_REFERENCE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3674 | 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] | 3675 | break; |
| 3676 | case YANG_STATUS: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3677 | LY_CHECK_RET(parse_status(ctx, data, &list->flags, &list->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3678 | break; |
| 3679 | case YANG_WHEN: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3680 | LY_CHECK_RET(parse_when(ctx, data, &list->when)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3681 | break; |
| 3682 | case YANG_KEY: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3683 | 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] | 3684 | break; |
| 3685 | case YANG_MAX_ELEMENTS: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3686 | 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] | 3687 | break; |
| 3688 | case YANG_MIN_ELEMENTS: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3689 | 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] | 3690 | break; |
| 3691 | case YANG_ORDERED_BY: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3692 | LY_CHECK_RET(parse_orderedby(ctx, data, &list->flags, &list->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3693 | break; |
| 3694 | case YANG_UNIQUE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3695 | 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] | 3696 | break; |
| 3697 | |
| 3698 | case YANG_ANYDATA: |
Radek Krejci | ceaf212 | 2019-01-02 15:03:26 +0100 | [diff] [blame] | 3699 | YANG_CHECK_STMTVER2_RET(ctx, "anydata", "list"); |
Radek Krejci | 1011365 | 2018-11-14 16:56:50 +0100 | [diff] [blame] | 3700 | /* fall through */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3701 | case YANG_ANYXML: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3702 | 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] | 3703 | break; |
| 3704 | case YANG_CHOICE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3705 | 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] | 3706 | break; |
| 3707 | case YANG_CONTAINER: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3708 | 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] | 3709 | break; |
| 3710 | case YANG_LEAF: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3711 | 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] | 3712 | break; |
| 3713 | case YANG_LEAF_LIST: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3714 | 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] | 3715 | break; |
| 3716 | case YANG_LIST: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3717 | 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] | 3718 | break; |
| 3719 | case YANG_USES: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3720 | 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] | 3721 | break; |
| 3722 | |
| 3723 | case YANG_TYPEDEF: |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 3724 | 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] | 3725 | break; |
| 3726 | case YANG_MUST: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3727 | LY_CHECK_RET(parse_restrs(ctx, data, kw, &list->musts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3728 | break; |
| 3729 | case YANG_ACTION: |
Radek Krejci | ceaf212 | 2019-01-02 15:03:26 +0100 | [diff] [blame] | 3730 | YANG_CHECK_STMTVER2_RET(ctx, "action", "list"); |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3731 | 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] | 3732 | break; |
| 3733 | case YANG_GROUPING: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3734 | 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] | 3735 | break; |
| 3736 | case YANG_NOTIFICATION: |
Radek Krejci | ceaf212 | 2019-01-02 15:03:26 +0100 | [diff] [blame] | 3737 | YANG_CHECK_STMTVER2_RET(ctx, "notification", "list"); |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3738 | 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] | 3739 | break; |
| 3740 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3741 | 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] | 3742 | break; |
| 3743 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 3744 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "list"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3745 | return LY_EVALID; |
| 3746 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3747 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3748 | LY_CHECK_RET(ret); |
| 3749 | checks: |
Radek Krejci | 7fc6829 | 2019-06-12 13:51:09 +0200 | [diff] [blame] | 3750 | /* finalize parent pointers to the reallocated items */ |
| 3751 | LY_CHECK_RET(parse_finalize_reallocated(ctx, list->groupings, NULL, list->actions, list->notifs)); |
| 3752 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3753 | if (list->max && list->min > list->max) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 3754 | LOGVAL_PARSER(ctx, LYVE_SEMANTICS, |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3755 | "Invalid combination of min-elements and max-elements: min value %u is bigger than the max value %u.", |
| 3756 | list->min, list->max); |
| 3757 | return LY_EVALID; |
| 3758 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3759 | |
| 3760 | return ret; |
| 3761 | } |
| 3762 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3763 | /** |
| 3764 | * @brief Parse the yin-element statement. |
| 3765 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3766 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3767 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 3768 | * @param[in,out] flags Flags to write to. |
| 3769 | * @param[in,out] exts Extension instances to add to. |
| 3770 | * |
| 3771 | * @return LY_ERR values. |
| 3772 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3773 | static LY_ERR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 3774 | parse_yinelement(struct lys_parser_ctx *ctx, const char **data, uint16_t *flags, struct lysp_ext_instance **exts) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3775 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3776 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3777 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 3778 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3779 | enum yang_keyword kw; |
| 3780 | |
| 3781 | if (*flags & LYS_YINELEM_MASK) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 3782 | LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "yin-element"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3783 | return LY_EVALID; |
| 3784 | } |
| 3785 | |
| 3786 | /* get value */ |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 3787 | LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3788 | |
| 3789 | if ((word_len == 4) && !strncmp(word, "true", word_len)) { |
| 3790 | *flags |= LYS_YINELEM_TRUE; |
| 3791 | } else if ((word_len == 5) && !strncmp(word, "false", word_len)) { |
| 3792 | *flags |= LYS_YINELEM_FALSE; |
| 3793 | } else { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 3794 | LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "yin-element"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3795 | free(buf); |
| 3796 | return LY_EVALID; |
| 3797 | } |
| 3798 | free(buf); |
| 3799 | |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 3800 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3801 | switch (kw) { |
| 3802 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3803 | LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_YINELEM, 0, exts)); |
| 3804 | LY_CHECK_RET(ret); break; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3805 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 3806 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "yin-element"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3807 | return LY_EVALID; |
| 3808 | } |
| 3809 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3810 | return ret; |
| 3811 | } |
| 3812 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3813 | /** |
David Sedlák | 2444f8f | 2019-07-09 11:02:47 +0200 | [diff] [blame^] | 3814 | * @brief Parse the argument statement. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3815 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3816 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3817 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 3818 | * @param[in,out] argument Value to write to. |
| 3819 | * @param[in,out] flags Flags to write to. |
| 3820 | * @param[in,out] exts Extension instances to add to. |
| 3821 | * |
| 3822 | * @return LY_ERR values. |
| 3823 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3824 | static LY_ERR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 3825 | parse_argument(struct lys_parser_ctx *ctx, const char **data, const char **argument, uint16_t *flags, struct lysp_ext_instance **exts) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3826 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3827 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3828 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 3829 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3830 | enum yang_keyword kw; |
| 3831 | |
| 3832 | if (*argument) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 3833 | LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "argument"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3834 | return LY_EVALID; |
| 3835 | } |
| 3836 | |
| 3837 | /* get value */ |
David Sedlák | 2444f8f | 2019-07-09 11:02:47 +0200 | [diff] [blame^] | 3838 | LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len)); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3839 | INSERT_WORD(ctx, buf, *argument, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3840 | |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 3841 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3842 | switch (kw) { |
| 3843 | case YANG_YIN_ELEMENT: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3844 | LY_CHECK_RET(parse_yinelement(ctx, data, flags, exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3845 | break; |
| 3846 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3847 | 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] | 3848 | break; |
| 3849 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 3850 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "argument"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3851 | return LY_EVALID; |
| 3852 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3853 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3854 | return ret; |
| 3855 | } |
| 3856 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3857 | /** |
| 3858 | * @brief Parse the extension statement. |
| 3859 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3860 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3861 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 3862 | * @param[in,out] extensions Extensions to add to. |
| 3863 | * |
| 3864 | * @return LY_ERR values. |
| 3865 | */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3866 | static LY_ERR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 3867 | parse_extension(struct lys_parser_ctx *ctx, const char **data, struct lysp_ext **extensions) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3868 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3869 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3870 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 3871 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3872 | enum yang_keyword kw; |
| 3873 | struct lysp_ext *ex; |
| 3874 | |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 3875 | LY_ARRAY_NEW_RET(ctx->ctx, *extensions, ex, LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3876 | |
| 3877 | /* get value */ |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 3878 | LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len)); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3879 | INSERT_WORD(ctx, buf, ex->name, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3880 | |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 3881 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3882 | switch (kw) { |
| 3883 | case YANG_DESCRIPTION: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3884 | 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] | 3885 | break; |
| 3886 | case YANG_REFERENCE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3887 | 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] | 3888 | break; |
| 3889 | case YANG_STATUS: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3890 | LY_CHECK_RET(parse_status(ctx, data, &ex->flags, &ex->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3891 | break; |
| 3892 | case YANG_ARGUMENT: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3893 | 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] | 3894 | break; |
| 3895 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3896 | 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] | 3897 | break; |
| 3898 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 3899 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "extension"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3900 | return LY_EVALID; |
| 3901 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3902 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3903 | return ret; |
| 3904 | } |
| 3905 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3906 | /** |
| 3907 | * @brief Parse the deviate statement. |
| 3908 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3909 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 3910 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 3911 | * @param[in,out] deviates Deviates to add to. |
| 3912 | * |
| 3913 | * @return LY_ERR values. |
| 3914 | */ |
Radek Krejci | 2d7a47b | 2019-05-16 13:34:10 +0200 | [diff] [blame] | 3915 | LY_ERR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 3916 | parse_deviate(struct lys_parser_ctx *ctx, const char **data, struct lysp_deviate **deviates) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3917 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 3918 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3919 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 3920 | size_t word_len, dev_mod; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3921 | enum yang_keyword kw; |
| 3922 | struct lysp_deviate *iter, *d; |
| 3923 | struct lysp_deviate_add *d_add = NULL; |
| 3924 | struct lysp_deviate_rpl *d_rpl = NULL; |
| 3925 | struct lysp_deviate_del *d_del = NULL; |
| 3926 | const char **d_units, ***d_uniques, ***d_dflts; |
| 3927 | struct lysp_restr **d_musts; |
| 3928 | uint16_t *d_flags; |
| 3929 | uint32_t *d_min, *d_max; |
| 3930 | |
| 3931 | /* get value */ |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 3932 | LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3933 | |
| 3934 | if ((word_len == 13) && !strncmp(word, "not-supported", word_len)) { |
| 3935 | dev_mod = LYS_DEV_NOT_SUPPORTED; |
| 3936 | } else if ((word_len == 3) && !strncmp(word, "add", word_len)) { |
| 3937 | dev_mod = LYS_DEV_ADD; |
| 3938 | } else if ((word_len == 7) && !strncmp(word, "replace", word_len)) { |
| 3939 | dev_mod = LYS_DEV_REPLACE; |
| 3940 | } else if ((word_len == 6) && !strncmp(word, "delete", word_len)) { |
| 3941 | dev_mod = LYS_DEV_DELETE; |
| 3942 | } else { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 3943 | LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "deviate"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3944 | free(buf); |
| 3945 | return LY_EVALID; |
| 3946 | } |
| 3947 | free(buf); |
| 3948 | |
| 3949 | /* create structure */ |
| 3950 | switch (dev_mod) { |
| 3951 | case LYS_DEV_NOT_SUPPORTED: |
| 3952 | d = calloc(1, sizeof *d); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3953 | LY_CHECK_ERR_RET(!d, LOGMEM(ctx->ctx), LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3954 | break; |
| 3955 | case LYS_DEV_ADD: |
| 3956 | d_add = calloc(1, sizeof *d_add); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3957 | LY_CHECK_ERR_RET(!d_add, LOGMEM(ctx->ctx), LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3958 | d = (struct lysp_deviate *)d_add; |
| 3959 | d_units = &d_add->units; |
| 3960 | d_uniques = &d_add->uniques; |
| 3961 | d_dflts = &d_add->dflts; |
| 3962 | d_musts = &d_add->musts; |
| 3963 | d_flags = &d_add->flags; |
| 3964 | d_min = &d_add->min; |
| 3965 | d_max = &d_add->max; |
| 3966 | break; |
| 3967 | case LYS_DEV_REPLACE: |
| 3968 | d_rpl = calloc(1, sizeof *d_rpl); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3969 | LY_CHECK_ERR_RET(!d_rpl, LOGMEM(ctx->ctx), LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3970 | d = (struct lysp_deviate *)d_rpl; |
| 3971 | d_units = &d_rpl->units; |
| 3972 | d_flags = &d_rpl->flags; |
| 3973 | d_min = &d_rpl->min; |
| 3974 | d_max = &d_rpl->max; |
| 3975 | break; |
| 3976 | case LYS_DEV_DELETE: |
| 3977 | d_del = calloc(1, sizeof *d_del); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3978 | LY_CHECK_ERR_RET(!d_del, LOGMEM(ctx->ctx), LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3979 | d = (struct lysp_deviate *)d_del; |
| 3980 | d_units = &d_del->units; |
| 3981 | d_uniques = &d_del->uniques; |
| 3982 | d_dflts = &d_del->dflts; |
| 3983 | d_musts = &d_del->musts; |
| 3984 | d_flags = &d_del->flags; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3985 | break; |
| 3986 | default: |
| 3987 | assert(0); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 3988 | LOGINT_RET(ctx->ctx); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 3989 | } |
| 3990 | d->mod = dev_mod; |
| 3991 | |
| 3992 | /* insert into siblings */ |
| 3993 | if (!*deviates) { |
| 3994 | *deviates = d; |
| 3995 | } else { |
| 3996 | for (iter = *deviates; iter->next; iter = iter->next); |
| 3997 | iter->next = d; |
| 3998 | } |
| 3999 | |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 4000 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4001 | switch (kw) { |
| 4002 | case YANG_CONFIG: |
| 4003 | switch (dev_mod) { |
| 4004 | case LYS_DEV_NOT_SUPPORTED: |
Michal Vasko | 492bec7 | 2018-09-18 13:11:10 +0200 | [diff] [blame] | 4005 | case LYS_DEV_DELETE: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 4006 | LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4007 | return LY_EVALID; |
| 4008 | default: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 4009 | LY_CHECK_RET(parse_config(ctx, data, d_flags, &d->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4010 | break; |
| 4011 | } |
| 4012 | break; |
| 4013 | case YANG_DEFAULT: |
| 4014 | switch (dev_mod) { |
| 4015 | case LYS_DEV_NOT_SUPPORTED: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 4016 | LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4017 | return LY_EVALID; |
| 4018 | case LYS_DEV_REPLACE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 4019 | 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] | 4020 | break; |
| 4021 | default: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 4022 | 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] | 4023 | break; |
| 4024 | } |
| 4025 | break; |
| 4026 | case YANG_MANDATORY: |
| 4027 | switch (dev_mod) { |
| 4028 | case LYS_DEV_NOT_SUPPORTED: |
Michal Vasko | 492bec7 | 2018-09-18 13:11:10 +0200 | [diff] [blame] | 4029 | case LYS_DEV_DELETE: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 4030 | LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4031 | return LY_EVALID; |
| 4032 | default: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 4033 | LY_CHECK_RET(parse_mandatory(ctx, data, d_flags, &d->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4034 | break; |
| 4035 | } |
| 4036 | break; |
| 4037 | case YANG_MAX_ELEMENTS: |
| 4038 | switch (dev_mod) { |
| 4039 | case LYS_DEV_NOT_SUPPORTED: |
Michal Vasko | 492bec7 | 2018-09-18 13:11:10 +0200 | [diff] [blame] | 4040 | case LYS_DEV_DELETE: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 4041 | LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4042 | return LY_EVALID; |
| 4043 | default: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 4044 | 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] | 4045 | break; |
| 4046 | } |
| 4047 | break; |
| 4048 | case YANG_MIN_ELEMENTS: |
| 4049 | switch (dev_mod) { |
| 4050 | case LYS_DEV_NOT_SUPPORTED: |
Michal Vasko | 492bec7 | 2018-09-18 13:11:10 +0200 | [diff] [blame] | 4051 | case LYS_DEV_DELETE: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 4052 | LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4053 | return LY_EVALID; |
| 4054 | default: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 4055 | 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] | 4056 | break; |
| 4057 | } |
| 4058 | break; |
| 4059 | case YANG_MUST: |
| 4060 | switch (dev_mod) { |
| 4061 | case LYS_DEV_NOT_SUPPORTED: |
Michal Vasko | 492bec7 | 2018-09-18 13:11:10 +0200 | [diff] [blame] | 4062 | case LYS_DEV_REPLACE: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 4063 | LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4064 | return LY_EVALID; |
| 4065 | default: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 4066 | LY_CHECK_RET(parse_restrs(ctx, data, kw, d_musts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4067 | break; |
| 4068 | } |
| 4069 | break; |
| 4070 | case YANG_TYPE: |
| 4071 | switch (dev_mod) { |
| 4072 | case LYS_DEV_NOT_SUPPORTED: |
| 4073 | case LYS_DEV_ADD: |
| 4074 | case LYS_DEV_DELETE: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 4075 | LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4076 | return LY_EVALID; |
| 4077 | default: |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 4078 | if (d_rpl->type) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 4079 | LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw)); |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 4080 | return LY_EVALID; |
| 4081 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4082 | d_rpl->type = calloc(1, sizeof *d_rpl->type); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 4083 | LY_CHECK_ERR_RET(!d_rpl->type, LOGMEM(ctx->ctx), LY_EMEM); |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 4084 | LY_CHECK_RET(parse_type(ctx, data, d_rpl->type)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4085 | break; |
| 4086 | } |
| 4087 | break; |
| 4088 | case YANG_UNIQUE: |
| 4089 | switch (dev_mod) { |
| 4090 | case LYS_DEV_NOT_SUPPORTED: |
| 4091 | case LYS_DEV_REPLACE: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 4092 | LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4093 | return LY_EVALID; |
| 4094 | default: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 4095 | 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] | 4096 | break; |
| 4097 | } |
| 4098 | break; |
| 4099 | case YANG_UNITS: |
| 4100 | switch (dev_mod) { |
| 4101 | case LYS_DEV_NOT_SUPPORTED: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 4102 | LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4103 | return LY_EVALID; |
| 4104 | default: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 4105 | 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] | 4106 | break; |
| 4107 | } |
| 4108 | break; |
| 4109 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 4110 | 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] | 4111 | break; |
| 4112 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 4113 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "deviate"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4114 | return LY_EVALID; |
| 4115 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4116 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4117 | return ret; |
| 4118 | } |
| 4119 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 4120 | /** |
| 4121 | * @brief Parse the deviation statement. |
| 4122 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 4123 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 4124 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 4125 | * @param[in,out] deviations Deviations to add to. |
| 4126 | * |
| 4127 | * @return LY_ERR values. |
| 4128 | */ |
Radek Krejci | 2d7a47b | 2019-05-16 13:34:10 +0200 | [diff] [blame] | 4129 | LY_ERR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 4130 | parse_deviation(struct lys_parser_ctx *ctx, const char **data, struct lysp_deviation **deviations) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4131 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 4132 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4133 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 4134 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4135 | enum yang_keyword kw; |
| 4136 | struct lysp_deviation *dev; |
| 4137 | |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 4138 | LY_ARRAY_NEW_RET(ctx->ctx, *deviations, dev, LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4139 | |
| 4140 | /* get value */ |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 4141 | LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len)); |
Radek Krejci | f09e4e8 | 2019-06-14 15:08:11 +0200 | [diff] [blame] | 4142 | YANG_CHECK_NONEMPTY(ctx, NULL, word_len, "deviation"); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 4143 | INSERT_WORD(ctx, buf, dev->nodeid, word, word_len); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4144 | |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 4145 | 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] | 4146 | switch (kw) { |
| 4147 | case YANG_DESCRIPTION: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 4148 | 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] | 4149 | break; |
| 4150 | case YANG_DEVIATE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 4151 | LY_CHECK_RET(parse_deviate(ctx, data, &dev->deviates)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4152 | break; |
| 4153 | case YANG_REFERENCE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 4154 | 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] | 4155 | break; |
| 4156 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 4157 | 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] | 4158 | break; |
| 4159 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 4160 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "deviation"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4161 | return LY_EVALID; |
| 4162 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4163 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 4164 | LY_CHECK_RET(ret); |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 4165 | checks: |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4166 | /* mandatory substatements */ |
| 4167 | if (!dev->deviates) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 4168 | LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "deviate", "deviation"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4169 | return LY_EVALID; |
| 4170 | } |
| 4171 | |
| 4172 | return ret; |
| 4173 | } |
| 4174 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 4175 | /** |
| 4176 | * @brief Parse the feature statement. |
| 4177 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 4178 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 4179 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 4180 | * @param[in,out] features Features to add to. |
| 4181 | * |
| 4182 | * @return LY_ERR values. |
| 4183 | */ |
Radek Krejci | 2d7a47b | 2019-05-16 13:34:10 +0200 | [diff] [blame] | 4184 | LY_ERR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 4185 | parse_feature(struct lys_parser_ctx *ctx, const char **data, struct lysp_feature **features) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4186 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 4187 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4188 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 4189 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4190 | enum yang_keyword kw; |
| 4191 | struct lysp_feature *feat; |
| 4192 | |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 4193 | LY_ARRAY_NEW_RET(ctx->ctx, *features, feat, LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4194 | |
| 4195 | /* get value */ |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 4196 | LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len)); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 4197 | INSERT_WORD(ctx, buf, feat->name, word, word_len); |
Radek Krejci | faa1eac | 2018-10-30 14:34:55 +0100 | [diff] [blame] | 4198 | |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 4199 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4200 | switch (kw) { |
| 4201 | case YANG_DESCRIPTION: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 4202 | 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] | 4203 | break; |
| 4204 | case YANG_IF_FEATURE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 4205 | 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] | 4206 | break; |
| 4207 | case YANG_REFERENCE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 4208 | 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] | 4209 | break; |
| 4210 | case YANG_STATUS: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 4211 | LY_CHECK_RET(parse_status(ctx, data, &feat->flags, &feat->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4212 | break; |
| 4213 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 4214 | 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] | 4215 | break; |
| 4216 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 4217 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "feature"); |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 4218 | return LY_EVALID; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4219 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4220 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4221 | return ret; |
| 4222 | } |
| 4223 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 4224 | /** |
| 4225 | * @brief Parse the identity statement. |
| 4226 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 4227 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 4228 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 4229 | * @param[in,out] identities Identities to add to. |
| 4230 | * |
| 4231 | * @return LY_ERR values. |
| 4232 | */ |
Radek Krejci | 2d7a47b | 2019-05-16 13:34:10 +0200 | [diff] [blame] | 4233 | LY_ERR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 4234 | parse_identity(struct lys_parser_ctx *ctx, const char **data, struct lysp_ident **identities) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4235 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 4236 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4237 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 4238 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4239 | enum yang_keyword kw; |
| 4240 | struct lysp_ident *ident; |
| 4241 | |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 4242 | LY_ARRAY_NEW_RET(ctx->ctx, *identities, ident, LY_EMEM); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4243 | |
| 4244 | /* get value */ |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 4245 | LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len)); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 4246 | INSERT_WORD(ctx, buf, ident->name, word, word_len); |
Radek Krejci | faa1eac | 2018-10-30 14:34:55 +0100 | [diff] [blame] | 4247 | |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 4248 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) { |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4249 | switch (kw) { |
| 4250 | case YANG_DESCRIPTION: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 4251 | 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] | 4252 | break; |
| 4253 | case YANG_IF_FEATURE: |
Radek Krejci | ceaf212 | 2019-01-02 15:03:26 +0100 | [diff] [blame] | 4254 | YANG_CHECK_STMTVER2_RET(ctx, "if-feature", "identity"); |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 4255 | 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] | 4256 | break; |
| 4257 | case YANG_REFERENCE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 4258 | 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] | 4259 | break; |
| 4260 | case YANG_STATUS: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 4261 | LY_CHECK_RET(parse_status(ctx, data, &ident->flags, &ident->exts)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4262 | break; |
| 4263 | case YANG_BASE: |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4264 | if (ident->bases && ctx->mod_version < 2) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 4265 | LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Identity can be derived from multiple base identities only in YANG 1.1 modules"); |
Radek Krejci | 1011365 | 2018-11-14 16:56:50 +0100 | [diff] [blame] | 4266 | return LY_EVALID; |
| 4267 | } |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 4268 | 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] | 4269 | break; |
| 4270 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 4271 | 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] | 4272 | break; |
| 4273 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 4274 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "identity"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4275 | return LY_EVALID; |
| 4276 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4277 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4278 | return ret; |
| 4279 | } |
| 4280 | |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 4281 | /** |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4282 | * @brief Parse module substatements. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 4283 | * |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 4284 | * @param[in] ctx yang parser context for logging. |
Michal Vasko | ea5abea | 2018-09-18 13:10:54 +0200 | [diff] [blame] | 4285 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 4286 | * @param[in,out] mod Module to write to. |
| 4287 | * |
| 4288 | * @return LY_ERR values. |
| 4289 | */ |
Radek Krejci | 2d7a47b | 2019-05-16 13:34:10 +0200 | [diff] [blame] | 4290 | LY_ERR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 4291 | parse_module(struct lys_parser_ctx *ctx, const char **data, struct lysp_module *mod) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4292 | { |
| 4293 | LY_ERR ret = 0; |
| 4294 | char *buf, *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 4295 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4296 | enum yang_keyword kw, prev_kw = 0; |
| 4297 | enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4298 | struct lysp_submodule *dup; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4299 | |
| 4300 | /* (sub)module name */ |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 4301 | LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len)); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4302 | INSERT_WORD(ctx, buf, mod->mod->name, word, word_len); |
Radek Krejci | faa1eac | 2018-10-30 14:34:55 +0100 | [diff] [blame] | 4303 | |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 4304 | 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] | 4305 | |
Radek Krejci | e384647 | 2018-10-15 15:24:51 +0200 | [diff] [blame] | 4306 | #define CHECK_ORDER(SECTION) \ |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 4307 | if (mod_stmt > SECTION) {LOGVAL_PARSER(ctx, LY_VCODE_INORD, ly_stmt2str(kw), ly_stmt2str(prev_kw)); return LY_EVALID;}mod_stmt = SECTION |
Radek Krejci | e384647 | 2018-10-15 15:24:51 +0200 | [diff] [blame] | 4308 | |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4309 | switch (kw) { |
| 4310 | /* module header */ |
| 4311 | case YANG_NAMESPACE: |
| 4312 | case YANG_PREFIX: |
Radek Krejci | e384647 | 2018-10-15 15:24:51 +0200 | [diff] [blame] | 4313 | CHECK_ORDER(Y_MOD_MODULE_HEADER); |
| 4314 | break; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4315 | case YANG_YANG_VERSION: |
Radek Krejci | e384647 | 2018-10-15 15:24:51 +0200 | [diff] [blame] | 4316 | CHECK_ORDER(Y_MOD_MODULE_HEADER); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4317 | break; |
| 4318 | /* linkage */ |
| 4319 | case YANG_INCLUDE: |
| 4320 | case YANG_IMPORT: |
Radek Krejci | e384647 | 2018-10-15 15:24:51 +0200 | [diff] [blame] | 4321 | CHECK_ORDER(Y_MOD_LINKAGE); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4322 | break; |
| 4323 | /* meta */ |
| 4324 | case YANG_ORGANIZATION: |
| 4325 | case YANG_CONTACT: |
| 4326 | case YANG_DESCRIPTION: |
| 4327 | case YANG_REFERENCE: |
Radek Krejci | e384647 | 2018-10-15 15:24:51 +0200 | [diff] [blame] | 4328 | CHECK_ORDER(Y_MOD_META); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4329 | break; |
| 4330 | |
| 4331 | /* revision */ |
| 4332 | case YANG_REVISION: |
Radek Krejci | e384647 | 2018-10-15 15:24:51 +0200 | [diff] [blame] | 4333 | CHECK_ORDER(Y_MOD_REVISION); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4334 | break; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4335 | /* body */ |
| 4336 | case YANG_ANYDATA: |
| 4337 | case YANG_ANYXML: |
| 4338 | case YANG_AUGMENT: |
| 4339 | case YANG_CHOICE: |
| 4340 | case YANG_CONTAINER: |
| 4341 | case YANG_DEVIATION: |
| 4342 | case YANG_EXTENSION: |
| 4343 | case YANG_FEATURE: |
| 4344 | case YANG_GROUPING: |
| 4345 | case YANG_IDENTITY: |
| 4346 | case YANG_LEAF: |
| 4347 | case YANG_LEAF_LIST: |
| 4348 | case YANG_LIST: |
| 4349 | case YANG_NOTIFICATION: |
| 4350 | case YANG_RPC: |
| 4351 | case YANG_TYPEDEF: |
| 4352 | case YANG_USES: |
| 4353 | case YANG_CUSTOM: |
| 4354 | mod_stmt = Y_MOD_BODY; |
| 4355 | break; |
| 4356 | default: |
| 4357 | /* error handled in the next switch */ |
| 4358 | break; |
| 4359 | } |
Radek Krejci | e384647 | 2018-10-15 15:24:51 +0200 | [diff] [blame] | 4360 | #undef CHECK_ORDER |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4361 | |
Radek Krejci | e384647 | 2018-10-15 15:24:51 +0200 | [diff] [blame] | 4362 | prev_kw = kw; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4363 | switch (kw) { |
| 4364 | /* module header */ |
| 4365 | case YANG_YANG_VERSION: |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4366 | LY_CHECK_RET(parse_yangversion(ctx, data, &mod->mod->version, &mod->exts)); |
| 4367 | ctx->mod_version = mod->mod->version; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4368 | break; |
| 4369 | case YANG_NAMESPACE: |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4370 | 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] | 4371 | break; |
| 4372 | case YANG_PREFIX: |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4373 | 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] | 4374 | break; |
| 4375 | |
| 4376 | /* linkage */ |
| 4377 | case YANG_INCLUDE: |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4378 | LY_CHECK_RET(parse_include(ctx, mod->mod->name, data, &mod->includes)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4379 | break; |
| 4380 | case YANG_IMPORT: |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4381 | LY_CHECK_RET(parse_import(ctx, mod->mod->prefix, data, &mod->imports)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4382 | break; |
| 4383 | |
| 4384 | /* meta */ |
| 4385 | case YANG_ORGANIZATION: |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4386 | 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] | 4387 | break; |
| 4388 | case YANG_CONTACT: |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4389 | 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] | 4390 | break; |
| 4391 | case YANG_DESCRIPTION: |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4392 | 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] | 4393 | break; |
| 4394 | case YANG_REFERENCE: |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4395 | 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] | 4396 | break; |
| 4397 | |
| 4398 | /* revision */ |
| 4399 | case YANG_REVISION: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 4400 | LY_CHECK_RET(parse_revision(ctx, data, &mod->revs)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4401 | break; |
| 4402 | |
| 4403 | /* body */ |
| 4404 | case YANG_ANYDATA: |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4405 | YANG_CHECK_STMTVER2_RET(ctx, "anydata", "module"); |
Radek Krejci | 1011365 | 2018-11-14 16:56:50 +0100 | [diff] [blame] | 4406 | /* fall through */ |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4407 | case YANG_ANYXML: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 4408 | LY_CHECK_RET(parse_any(ctx, data, kw, NULL, &mod->data)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4409 | break; |
| 4410 | case YANG_CHOICE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 4411 | LY_CHECK_RET(parse_choice(ctx, data, NULL, &mod->data)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4412 | break; |
| 4413 | case YANG_CONTAINER: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 4414 | LY_CHECK_RET(parse_container(ctx, data, NULL, &mod->data)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4415 | break; |
| 4416 | case YANG_LEAF: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 4417 | LY_CHECK_RET(parse_leaf(ctx, data, NULL, &mod->data)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4418 | break; |
| 4419 | case YANG_LEAF_LIST: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 4420 | LY_CHECK_RET(parse_leaflist(ctx, data, NULL, &mod->data)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4421 | break; |
| 4422 | case YANG_LIST: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 4423 | LY_CHECK_RET(parse_list(ctx, data, NULL, &mod->data)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4424 | break; |
| 4425 | case YANG_USES: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 4426 | LY_CHECK_RET(parse_uses(ctx, data, NULL, &mod->data)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4427 | break; |
| 4428 | |
| 4429 | case YANG_AUGMENT: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 4430 | LY_CHECK_RET(parse_augment(ctx, data, NULL, &mod->augments)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4431 | break; |
| 4432 | case YANG_DEVIATION: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 4433 | LY_CHECK_RET(parse_deviation(ctx, data, &mod->deviations)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4434 | break; |
| 4435 | case YANG_EXTENSION: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 4436 | LY_CHECK_RET(parse_extension(ctx, data, &mod->extensions)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4437 | break; |
| 4438 | case YANG_FEATURE: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 4439 | LY_CHECK_RET(parse_feature(ctx, data, &mod->features)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4440 | break; |
| 4441 | case YANG_GROUPING: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 4442 | LY_CHECK_RET(parse_grouping(ctx, data, NULL, &mod->groupings)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4443 | break; |
| 4444 | case YANG_IDENTITY: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 4445 | LY_CHECK_RET(parse_identity(ctx, data, &mod->identities)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4446 | break; |
| 4447 | case YANG_NOTIFICATION: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 4448 | LY_CHECK_RET(parse_notif(ctx, data, NULL, &mod->notifs)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4449 | break; |
| 4450 | case YANG_RPC: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 4451 | LY_CHECK_RET(parse_action(ctx, data, NULL, &mod->rpcs)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4452 | break; |
| 4453 | case YANG_TYPEDEF: |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 4454 | LY_CHECK_RET(parse_typedef(ctx, NULL, data, &mod->typedefs)); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4455 | break; |
| 4456 | case YANG_CUSTOM: |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 4457 | 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] | 4458 | break; |
| 4459 | |
| 4460 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 4461 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "module"); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4462 | return LY_EVALID; |
| 4463 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4464 | } |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 4465 | LY_CHECK_RET(ret); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4466 | |
Radek Krejci | 6d6556c | 2018-11-08 09:37:45 +0100 | [diff] [blame] | 4467 | checks: |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4468 | /* finalize parent pointers to the reallocated items */ |
Radek Krejci | 7fc6829 | 2019-06-12 13:51:09 +0200 | [diff] [blame] | 4469 | LY_CHECK_RET(parse_finalize_reallocated(ctx, mod->groupings, mod->augments, mod->rpcs, mod->notifs)); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4470 | |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4471 | /* mandatory substatements */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4472 | if (!mod->mod->ns) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 4473 | LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "namespace", "module"); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4474 | return LY_EVALID; |
| 4475 | } else if (!mod->mod->prefix) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 4476 | LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "module"); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4477 | return LY_EVALID; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4478 | } |
| 4479 | |
Radek Krejci | e9e987e | 2018-10-31 12:50:27 +0100 | [diff] [blame] | 4480 | /* submodules share the namespace with the module names, so there must not be |
| 4481 | * 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] | 4482 | dup = ly_ctx_get_submodule(ctx->ctx, NULL, mod->mod->name, NULL); |
| 4483 | if (dup) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 4484 | LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Name collision between module and submodule of name \"%s\".", mod->mod->name); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4485 | return LY_EVALID; |
| 4486 | } |
| 4487 | |
| 4488 | return ret; |
| 4489 | } |
| 4490 | |
| 4491 | /** |
| 4492 | * @brief Parse submodule substatements. |
| 4493 | * |
| 4494 | * @param[in] ctx yang parser context for logging. |
| 4495 | * @param[in,out] data Data to read from, always moved to currently handled character. |
| 4496 | * @param[out] submod Parsed submodule structure. |
| 4497 | * |
| 4498 | * @return LY_ERR values. |
| 4499 | */ |
Radek Krejci | 2d7a47b | 2019-05-16 13:34:10 +0200 | [diff] [blame] | 4500 | LY_ERR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 4501 | parse_submodule(struct lys_parser_ctx *ctx, const char **data, struct lysp_submodule *submod) |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4502 | { |
| 4503 | LY_ERR ret = 0; |
| 4504 | char *buf, *word; |
| 4505 | size_t word_len; |
| 4506 | enum yang_keyword kw, prev_kw = 0; |
| 4507 | enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER; |
| 4508 | struct lysp_submodule *dup; |
| 4509 | |
| 4510 | /* submodule name */ |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 4511 | LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len)); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4512 | INSERT_WORD(ctx, buf, submod->name, word, word_len); |
| 4513 | |
| 4514 | YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) { |
| 4515 | |
| 4516 | #define CHECK_ORDER(SECTION) \ |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 4517 | if (mod_stmt > SECTION) {LOGVAL_PARSER(ctx, LY_VCODE_INORD, ly_stmt2str(kw), ly_stmt2str(prev_kw)); return LY_EVALID;}mod_stmt = SECTION |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4518 | |
| 4519 | switch (kw) { |
| 4520 | /* module header */ |
| 4521 | case YANG_BELONGS_TO: |
| 4522 | CHECK_ORDER(Y_MOD_MODULE_HEADER); |
| 4523 | break; |
| 4524 | case YANG_YANG_VERSION: |
| 4525 | CHECK_ORDER(Y_MOD_MODULE_HEADER); |
| 4526 | break; |
| 4527 | /* linkage */ |
| 4528 | case YANG_INCLUDE: |
| 4529 | case YANG_IMPORT: |
| 4530 | CHECK_ORDER(Y_MOD_LINKAGE); |
| 4531 | break; |
| 4532 | /* meta */ |
| 4533 | case YANG_ORGANIZATION: |
| 4534 | case YANG_CONTACT: |
| 4535 | case YANG_DESCRIPTION: |
| 4536 | case YANG_REFERENCE: |
| 4537 | CHECK_ORDER(Y_MOD_META); |
| 4538 | break; |
| 4539 | |
| 4540 | /* revision */ |
| 4541 | case YANG_REVISION: |
| 4542 | CHECK_ORDER(Y_MOD_REVISION); |
| 4543 | break; |
| 4544 | /* body */ |
| 4545 | case YANG_ANYDATA: |
| 4546 | case YANG_ANYXML: |
| 4547 | case YANG_AUGMENT: |
| 4548 | case YANG_CHOICE: |
| 4549 | case YANG_CONTAINER: |
| 4550 | case YANG_DEVIATION: |
| 4551 | case YANG_EXTENSION: |
| 4552 | case YANG_FEATURE: |
| 4553 | case YANG_GROUPING: |
| 4554 | case YANG_IDENTITY: |
| 4555 | case YANG_LEAF: |
| 4556 | case YANG_LEAF_LIST: |
| 4557 | case YANG_LIST: |
| 4558 | case YANG_NOTIFICATION: |
| 4559 | case YANG_RPC: |
| 4560 | case YANG_TYPEDEF: |
| 4561 | case YANG_USES: |
| 4562 | case YANG_CUSTOM: |
| 4563 | mod_stmt = Y_MOD_BODY; |
| 4564 | break; |
| 4565 | default: |
| 4566 | /* error handled in the next switch */ |
| 4567 | break; |
| 4568 | } |
| 4569 | #undef CHECK_ORDER |
| 4570 | |
| 4571 | prev_kw = kw; |
| 4572 | switch (kw) { |
| 4573 | /* module header */ |
| 4574 | case YANG_YANG_VERSION: |
| 4575 | LY_CHECK_RET(parse_yangversion(ctx, data, &submod->version, &submod->exts)); |
| 4576 | ctx->mod_version = submod->version; |
| 4577 | break; |
| 4578 | case YANG_BELONGS_TO: |
| 4579 | LY_CHECK_RET(parse_belongsto(ctx, data, &submod->belongsto, &submod->prefix, &submod->exts)); |
| 4580 | break; |
| 4581 | |
| 4582 | /* linkage */ |
| 4583 | case YANG_INCLUDE: |
| 4584 | LY_CHECK_RET(parse_include(ctx, submod->name, data, &submod->includes)); |
| 4585 | break; |
| 4586 | case YANG_IMPORT: |
| 4587 | LY_CHECK_RET(parse_import(ctx, submod->prefix, data, &submod->imports)); |
| 4588 | break; |
| 4589 | |
| 4590 | /* meta */ |
| 4591 | case YANG_ORGANIZATION: |
| 4592 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_ORGANIZATION, 0, &submod->org, Y_STR_ARG, &submod->exts)); |
| 4593 | break; |
| 4594 | case YANG_CONTACT: |
| 4595 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_CONTACT, 0, &submod->contact, Y_STR_ARG, &submod->exts)); |
| 4596 | break; |
| 4597 | case YANG_DESCRIPTION: |
| 4598 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &submod->dsc, Y_STR_ARG, &submod->exts)); |
| 4599 | break; |
| 4600 | case YANG_REFERENCE: |
| 4601 | LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &submod->ref, Y_STR_ARG, &submod->exts)); |
| 4602 | break; |
| 4603 | |
| 4604 | /* revision */ |
| 4605 | case YANG_REVISION: |
| 4606 | LY_CHECK_RET(parse_revision(ctx, data, &submod->revs)); |
| 4607 | break; |
| 4608 | |
| 4609 | /* body */ |
| 4610 | case YANG_ANYDATA: |
| 4611 | YANG_CHECK_STMTVER2_RET(ctx, "anydata", "submodule"); |
| 4612 | /* fall through */ |
| 4613 | case YANG_ANYXML: |
| 4614 | LY_CHECK_RET(parse_any(ctx, data, kw, NULL, &submod->data)); |
| 4615 | break; |
| 4616 | case YANG_CHOICE: |
| 4617 | LY_CHECK_RET(parse_choice(ctx, data, NULL, &submod->data)); |
| 4618 | break; |
| 4619 | case YANG_CONTAINER: |
| 4620 | LY_CHECK_RET(parse_container(ctx, data, NULL, &submod->data)); |
| 4621 | break; |
| 4622 | case YANG_LEAF: |
| 4623 | LY_CHECK_RET(parse_leaf(ctx, data, NULL, &submod->data)); |
| 4624 | break; |
| 4625 | case YANG_LEAF_LIST: |
| 4626 | LY_CHECK_RET(parse_leaflist(ctx, data, NULL, &submod->data)); |
| 4627 | break; |
| 4628 | case YANG_LIST: |
| 4629 | LY_CHECK_RET(parse_list(ctx, data, NULL, &submod->data)); |
| 4630 | break; |
| 4631 | case YANG_USES: |
| 4632 | LY_CHECK_RET(parse_uses(ctx, data, NULL, &submod->data)); |
| 4633 | break; |
| 4634 | |
| 4635 | case YANG_AUGMENT: |
| 4636 | LY_CHECK_RET(parse_augment(ctx, data, NULL, &submod->augments)); |
| 4637 | break; |
| 4638 | case YANG_DEVIATION: |
| 4639 | LY_CHECK_RET(parse_deviation(ctx, data, &submod->deviations)); |
| 4640 | break; |
| 4641 | case YANG_EXTENSION: |
| 4642 | LY_CHECK_RET(parse_extension(ctx, data, &submod->extensions)); |
| 4643 | break; |
| 4644 | case YANG_FEATURE: |
| 4645 | LY_CHECK_RET(parse_feature(ctx, data, &submod->features)); |
| 4646 | break; |
| 4647 | case YANG_GROUPING: |
| 4648 | LY_CHECK_RET(parse_grouping(ctx, data, NULL, &submod->groupings)); |
| 4649 | break; |
| 4650 | case YANG_IDENTITY: |
| 4651 | LY_CHECK_RET(parse_identity(ctx, data, &submod->identities)); |
| 4652 | break; |
| 4653 | case YANG_NOTIFICATION: |
| 4654 | LY_CHECK_RET(parse_notif(ctx, data, NULL, &submod->notifs)); |
| 4655 | break; |
| 4656 | case YANG_RPC: |
| 4657 | LY_CHECK_RET(parse_action(ctx, data, NULL, &submod->rpcs)); |
| 4658 | break; |
| 4659 | case YANG_TYPEDEF: |
| 4660 | LY_CHECK_RET(parse_typedef(ctx, NULL, data, &submod->typedefs)); |
| 4661 | break; |
| 4662 | case YANG_CUSTOM: |
| 4663 | LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &submod->exts)); |
| 4664 | break; |
| 4665 | |
| 4666 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 4667 | LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "submodule"); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4668 | return LY_EVALID; |
| 4669 | } |
| 4670 | } |
| 4671 | LY_CHECK_RET(ret); |
| 4672 | |
| 4673 | checks: |
| 4674 | /* finalize parent pointers to the reallocated items */ |
Radek Krejci | 7fc6829 | 2019-06-12 13:51:09 +0200 | [diff] [blame] | 4675 | LY_CHECK_RET(parse_finalize_reallocated(ctx, submod->groupings, submod->augments, submod->rpcs, submod->notifs)); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4676 | |
| 4677 | /* mandatory substatements */ |
| 4678 | if (!submod->belongsto) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 4679 | LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "belongs-to", "submodule"); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4680 | return LY_EVALID; |
| 4681 | } |
| 4682 | |
| 4683 | /* submodules share the namespace with the module names, so there must not be |
| 4684 | * a submodule of the same name in the context, no need for revision matching */ |
| 4685 | dup = ly_ctx_get_submodule(ctx->ctx, NULL, submod->name, NULL); |
| 4686 | if (dup && strcmp(dup->belongsto, submod->belongsto)) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 4687 | LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Name collision between submodules of name \"%s\".", dup->name); |
Radek Krejci | e9e987e | 2018-10-31 12:50:27 +0100 | [diff] [blame] | 4688 | return LY_EVALID; |
| 4689 | } |
| 4690 | |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4691 | return ret; |
| 4692 | } |
| 4693 | |
Radek Krejci | d4557c6 | 2018-09-17 11:42:09 +0200 | [diff] [blame] | 4694 | LY_ERR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 4695 | yang_parse_submodule(struct lys_parser_ctx *context, const char *data, struct lysp_submodule **submod) |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4696 | { |
Radek Krejci | 6d9b9b5 | 2018-11-02 12:43:39 +0100 | [diff] [blame] | 4697 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 0a1d0d4 | 2019-05-16 15:14:51 +0200 | [diff] [blame] | 4698 | char *word; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 4699 | size_t word_len; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4700 | enum yang_keyword kw; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4701 | struct lysp_submodule *mod_p = NULL; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4702 | |
| 4703 | /* "module"/"submodule" */ |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 4704 | ret = get_keyword(context, &data, &kw, &word, &word_len); |
| 4705 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4706 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4707 | if (kw == YANG_MODULE) { |
| 4708 | LOGERR(context->ctx, LY_EDENIED, "Input data contains module in situation when a submodule is expected."); |
| 4709 | ret = LY_EINVAL; |
| 4710 | goto cleanup; |
| 4711 | } else if (kw != YANG_SUBMODULE) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 4712 | LOGVAL_PARSER(context, LYVE_SYNTAX, "Invalid keyword \"%s\", expected \"module\" or \"submodule\".", |
Radek Krejci | c59bc97 | 2018-09-17 16:13:06 +0200 | [diff] [blame] | 4713 | ly_stmt2str(kw)); |
Radek Krejci | 40544fa | 2019-01-11 09:38:37 +0100 | [diff] [blame] | 4714 | ret = LY_EVALID; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 4715 | goto cleanup; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4716 | } |
| 4717 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4718 | mod_p = calloc(1, sizeof *mod_p); |
| 4719 | LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(context->ctx), cleanup); |
| 4720 | mod_p->parsing = 1; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4721 | |
| 4722 | /* substatements */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4723 | ret = parse_submodule(context, &data, mod_p); |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 4724 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4725 | |
| 4726 | /* read some trailing spaces or new lines */ |
Radek Krejci | 0a1d0d4 | 2019-05-16 15:14:51 +0200 | [diff] [blame] | 4727 | while(*data && isspace(*data)) { |
| 4728 | data++; |
| 4729 | } |
| 4730 | if (*data) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 4731 | LOGVAL_PARSER(context, LYVE_SYNTAX, "Trailing garbage \"%.*s%s\" after submodule, expected end-of-input.", |
Radek Krejci | 0a1d0d4 | 2019-05-16 15:14:51 +0200 | [diff] [blame] | 4732 | 15, data, strlen(data) > 15 ? "..." : ""); |
Radek Krejci | 40544fa | 2019-01-11 09:38:37 +0100 | [diff] [blame] | 4733 | ret = LY_EVALID; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 4734 | goto cleanup; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4735 | } |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4736 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4737 | mod_p->parsing = 0; |
| 4738 | *submod = mod_p; |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4739 | |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 4740 | cleanup: |
| 4741 | if (ret) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4742 | lysp_submodule_free(context->ctx, mod_p); |
| 4743 | } |
| 4744 | |
| 4745 | return ret; |
| 4746 | } |
| 4747 | |
| 4748 | LY_ERR |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 4749 | yang_parse_module(struct lys_parser_ctx *context, const char *data, struct lys_module *mod) |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4750 | { |
| 4751 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 0a1d0d4 | 2019-05-16 15:14:51 +0200 | [diff] [blame] | 4752 | char *word; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4753 | size_t word_len; |
| 4754 | enum yang_keyword kw; |
| 4755 | struct lysp_module *mod_p = NULL; |
| 4756 | |
| 4757 | /* "module"/"submodule" */ |
| 4758 | ret = get_keyword(context, &data, &kw, &word, &word_len); |
| 4759 | LY_CHECK_GOTO(ret, cleanup); |
| 4760 | |
| 4761 | if (kw == YANG_SUBMODULE) { |
| 4762 | LOGERR(context->ctx, LY_EDENIED, "Input data contains submodule which cannot be parsed directly without its main module."); |
| 4763 | ret = LY_EINVAL; |
| 4764 | goto cleanup; |
| 4765 | } else if (kw != YANG_MODULE) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 4766 | LOGVAL_PARSER(context, LYVE_SYNTAX, "Invalid keyword \"%s\", expected \"module\" or \"submodule\".", |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4767 | ly_stmt2str(kw)); |
Radek Krejci | 40544fa | 2019-01-11 09:38:37 +0100 | [diff] [blame] | 4768 | ret = LY_EVALID; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4769 | goto cleanup; |
| 4770 | } |
| 4771 | |
| 4772 | mod_p = calloc(1, sizeof *mod_p); |
| 4773 | LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(context->ctx), cleanup); |
| 4774 | mod_p->mod = mod; |
| 4775 | mod_p->parsing = 1; |
| 4776 | |
| 4777 | /* substatements */ |
| 4778 | ret = parse_module(context, &data, mod_p); |
| 4779 | LY_CHECK_GOTO(ret, cleanup); |
| 4780 | |
| 4781 | /* read some trailing spaces or new lines */ |
Radek Krejci | 0a1d0d4 | 2019-05-16 15:14:51 +0200 | [diff] [blame] | 4782 | while(*data && isspace(*data)) { |
| 4783 | data++; |
| 4784 | } |
| 4785 | if (*data) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 4786 | LOGVAL_PARSER(context, LYVE_SYNTAX, "Trailing garbage \"%.*s%s\" after module, expected end-of-input.", |
Radek Krejci | 0a1d0d4 | 2019-05-16 15:14:51 +0200 | [diff] [blame] | 4787 | 15, data, strlen(data) > 15 ? "..." : ""); |
Radek Krejci | 40544fa | 2019-01-11 09:38:37 +0100 | [diff] [blame] | 4788 | ret = LY_EVALID; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4789 | goto cleanup; |
| 4790 | } |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4791 | |
| 4792 | mod_p->parsing = 0; |
| 4793 | mod->parsed = mod_p; |
| 4794 | |
| 4795 | cleanup: |
| 4796 | if (ret) { |
| 4797 | lysp_module_free(mod_p); |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 4798 | } |
| 4799 | |
Michal Vasko | 7fbc816 | 2018-09-17 10:35:16 +0200 | [diff] [blame] | 4800 | return ret; |
| 4801 | } |