blob: 529db42dbc729591674a552f39ce05efa01e0d67 [file] [log] [blame]
Michal Vasko7fbc8162018-09-17 10:35:16 +02001/**
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 Vasko63f3d842020-07-08 10:10:14 +020014#include "parser_internal.h"
Michal Vasko7fbc8162018-09-17 10:35:16 +020015
Radek Krejcie7b95092019-05-15 11:03:07 +020016#include <assert.h>
17#include <ctype.h>
18#include <errno.h>
19#include <stdint.h>
20#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
23
Radek Krejci535ea9f2020-05-29 16:01:05 +020024#include "common.h"
Michal Vasko7fbc8162018-09-17 10:35:16 +020025#include "context.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020026#include "dict.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020027#include "log.h"
Michal Vasko004d3152020-06-11 19:59:22 +020028#include "path.h"
Radek Krejcica376bd2020-06-11 16:04:06 +020029#include "parser_schema.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020030#include "set.h"
31#include "tree.h"
32#include "tree_schema.h"
Radek Krejci70853c52018-10-15 14:46:16 +020033#include "tree_schema_internal.h"
Radek Krejci44ceedc2018-10-02 15:54:31 +020034
Radek Krejciceaf2122019-01-02 15:03:26 +010035/**
36 * @brief Insert WORD into the libyang context's dictionary and store as TARGET.
37 * @param[in] CTX yang parser context to access libyang context.
38 * @param[in] BUF buffer in case the word is not a constant and can be inserted directly (zero-copy)
39 * @param[out] TARGET variable where to store the pointer to the inserted value.
40 * @param[in] WORD string to store.
41 * @param[in] LEN length of the string in WORD to store.
42 */
Radek Krejci9fcacc12018-10-11 15:59:11 +020043#define INSERT_WORD(CTX, BUF, TARGET, WORD, LEN) \
44 if (BUF) {(TARGET) = lydict_insert_zc((CTX)->ctx, WORD);}\
Radek Krejcif09e4e82019-06-14 15:08:11 +020045 else {(TARGET) = lydict_insert((CTX)->ctx, LEN ? WORD : "", LEN);}
Radek Krejci44ceedc2018-10-02 15:54:31 +020046
Radek Krejciceaf2122019-01-02 15:03:26 +010047/**
Michal Vasko63f3d842020-07-08 10:10:14 +020048 * @brief Read from the IN structure COUNT items. Also updates the indent value in yang parser context
Radek Krejciceaf2122019-01-02 15:03:26 +010049 * @param[in] CTX yang parser context to update its indent value.
Michal Vasko63f3d842020-07-08 10:10:14 +020050 * @param[in,out] IN input structure
Radek Krejciceaf2122019-01-02 15:03:26 +010051 * @param[in] COUNT number of items for which the DATA pointer is supposed to move on.
52 */
Michal Vasko63f3d842020-07-08 10:10:14 +020053#define MOVE_INPUT(CTX, IN, COUNT) ly_in_skip(IN, COUNT);(CTX)->indent+=COUNT
Radek Krejcid5f2b5f2018-10-11 10:54:36 +020054
Michal Vaskoea5abea2018-09-18 13:10:54 +020055/**
56 * @brief Loop through all substatements providing, return if there are none.
57 *
Radek Krejci44ceedc2018-10-02 15:54:31 +020058 * @param[in] CTX yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +020059 * @param[in] IN Input structure to read from.
Michal Vaskoea5abea2018-09-18 13:10:54 +020060 * @param[out] KW YANG keyword read.
61 * @param[out] WORD Pointer to the keyword itself.
62 * @param[out] WORD_LEN Length of the keyword.
63 * @param[out] ERR Variable for error storing.
64 *
65 * @return In case there are no substatements or a fatal error encountered.
66 */
Michal Vasko63f3d842020-07-08 10:10:14 +020067#define YANG_READ_SUBSTMT_FOR(CTX, IN, KW, WORD, WORD_LEN, ERR, CHECKGOTO) \
68 LY_CHECK_RET(get_keyword(CTX, IN, &KW, &WORD, &WORD_LEN)); \
Radek Krejcid6b76452019-09-03 17:03:03 +020069 if (KW == LY_STMT_SYNTAX_SEMICOLON) { \
Radek Krejci6d6556c2018-11-08 09:37:45 +010070 CHECKGOTO; \
Radek Krejci6d9b9b52018-11-02 12:43:39 +010071 return LY_SUCCESS; \
Michal Vasko7fbc8162018-09-17 10:35:16 +020072 } \
Radek Krejcid6b76452019-09-03 17:03:03 +020073 if (KW != LY_STMT_SYNTAX_LEFT_BRACE) { \
David Sedlákb3077192019-06-19 10:55:37 +020074 LOGVAL_PARSER(CTX, LYVE_SYNTAX_YANG, "Invalid keyword \"%s\", expected \";\" or \"{\".", ly_stmt2str(KW)); \
Michal Vasko7fbc8162018-09-17 10:35:16 +020075 return LY_EVALID; \
76 } \
Michal Vasko63f3d842020-07-08 10:10:14 +020077 for (ERR = get_keyword(CTX, IN, &KW, &WORD, &WORD_LEN); \
Radek Krejcid6b76452019-09-03 17:03:03 +020078 !ERR && (KW != LY_STMT_SYNTAX_RIGHT_BRACE); \
Michal Vasko63f3d842020-07-08 10:10:14 +020079 ERR = get_keyword(CTX, IN, &KW, &WORD, &WORD_LEN))
Michal Vasko7fbc8162018-09-17 10:35:16 +020080
Michal Vasko63f3d842020-07-08 10:10:14 +020081LY_ERR parse_container(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_node *parent,
Radek Krejci0f969882020-08-21 16:56:47 +020082 struct lysp_node **siblings);
Michal Vasko63f3d842020-07-08 10:10:14 +020083LY_ERR parse_uses(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_node *parent, struct lysp_node **siblings);
84LY_ERR parse_choice(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_node *parent, struct lysp_node **siblings);
85LY_ERR parse_case(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_node *parent, struct lysp_node **siblings);
86LY_ERR parse_list(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_node *parent, struct lysp_node **siblings);
87LY_ERR parse_grouping(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_node *parent, struct lysp_grp **groupings);
Michal Vasko7fbc8162018-09-17 10:35:16 +020088
Michal Vaskoea5abea2018-09-18 13:10:54 +020089/**
90 * @brief Add another character to dynamic buffer, a low-level function.
91 *
Radek Krejci44ceedc2018-10-02 15:54:31 +020092 * Enlarge if needed. Updates \p input as well as \p buf_used.
Michal Vaskoea5abea2018-09-18 13:10:54 +020093 *
Radek Krejci404251e2018-10-09 12:06:44 +020094 * @param[in] ctx libyang context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +020095 * @param[in,out] in Input structure.
Radek Krejci44ceedc2018-10-02 15:54:31 +020096 * @param[in] len Number of bytes to get from the input string and copy into the buffer.
Michal Vaskoea5abea2018-09-18 13:10:54 +020097 * @param[in,out] buf Buffer to use, can be moved by realloc().
98 * @param[in,out] buf_len Current size of the buffer.
Radek Krejci44ceedc2018-10-02 15:54:31 +020099 * @param[in,out] buf_used Currently used characters of the buffer.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200100 *
101 * @return LY_ERR values.
102 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200103LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +0200104buf_add_char(struct ly_ctx *ctx, struct ly_in *in, size_t len, char **buf, size_t *buf_len, size_t *buf_used)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200105{
Radek Krejci44ceedc2018-10-02 15:54:31 +0200106 if (*buf_len <= (*buf_used) + len) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200107 *buf_len += 16;
108 *buf = ly_realloc(*buf, *buf_len);
109 LY_CHECK_ERR_RET(!*buf, LOGMEM(ctx), LY_EMEM);
110 }
Radek Krejcic0917392019-04-10 13:04:04 +0200111 if (*buf_used) {
Michal Vasko63f3d842020-07-08 10:10:14 +0200112 ly_in_read(in, &(*buf)[*buf_used], len);
Radek Krejcic0917392019-04-10 13:04:04 +0200113 } else {
Michal Vasko63f3d842020-07-08 10:10:14 +0200114 ly_in_read(in, *buf, len);
Radek Krejcic0917392019-04-10 13:04:04 +0200115 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200116
Radek Krejci44ceedc2018-10-02 15:54:31 +0200117 (*buf_used) += len;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200118 return LY_SUCCESS;
119}
120
Michal Vaskoea5abea2018-09-18 13:10:54 +0200121/**
Radek Krejci44ceedc2018-10-02 15:54:31 +0200122 * @brief Store a single UTF8 character. It depends whether in a dynamically-allocated buffer or just as a pointer to the data.
123 *
124 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +0200125 * @param[in,out] in Input structure.
Radek Krejci44ceedc2018-10-02 15:54:31 +0200126 * @param[in] arg Type of the input string to select method of checking character validity.
127 * @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 Vaskoea5abea2018-09-18 13:10:54 +0200128 * 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 Krejci44ceedc2018-10-02 15:54:31 +0200129 * @param[in,out] word_len Current length of the word pointed to by \p word_p.
130 * @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 Vaskoea5abea2018-09-18 13:10:54 +0200131 * @param[in,out] buf_len Current length of \p word_b.
Radek Krejci44ceedc2018-10-02 15:54:31 +0200132 * @param[in] need_buf Flag if the dynamically allocated buffer is required.
David Sedlák40bb13b2019-07-10 14:34:18 +0200133 * @param[in,out] prefix Storage for internally used flag in case of possible prefixed identifiers:
134 * 0 - colon not yet found (no prefix)
135 * 1 - \p c is the colon character
136 * 2 - prefix already processed, now processing the identifier
Michal Vaskoea5abea2018-09-18 13:10:54 +0200137 *
138 * @return LY_ERR values.
139 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200140LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +0200141buf_store_char(struct lys_yang_parser_ctx *ctx, struct ly_in *in, enum yang_arg arg, char **word_p, size_t *word_len,
Radek Krejci857189e2020-09-01 13:26:36 +0200142 char **word_b, size_t *buf_len, ly_bool need_buf, uint8_t *prefix)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200143{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200144 uint32_t c;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200145 size_t len;
146
Radek Krejcif29b7c32019-04-09 16:17:49 +0200147 /* check valid combination of input paremeters - if need_buf specified, word_b must be provided */
148 assert(!need_buf || (need_buf && word_b));
149
Radek Krejci44ceedc2018-10-02 15:54:31 +0200150 /* get UTF8 code point (and number of bytes coding the character) */
Michal Vasko63f3d842020-07-08 10:10:14 +0200151 LY_CHECK_ERR_RET(ly_getutf8(&in->current, &c, &len),
152 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, in->current[-len]), LY_EVALID);
153 in->current -= len;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200154 if (c == '\n') {
155 ctx->indent = 0;
156 } else {
157 /* note - even the multibyte character is count as 1 */
158 ++ctx->indent;
159 }
160
Radek Krejci44ceedc2018-10-02 15:54:31 +0200161 /* check character validity */
162 switch (arg) {
163 case Y_IDENTIF_ARG:
Michal Vaskob36053d2020-03-26 15:49:30 +0100164 LY_CHECK_RET(lysp_check_identifierchar((struct lys_parser_ctx *)ctx, c, !(*word_len), NULL));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200165 break;
166 case Y_PREF_IDENTIF_ARG:
Michal Vaskob36053d2020-03-26 15:49:30 +0100167 LY_CHECK_RET(lysp_check_identifierchar((struct lys_parser_ctx *)ctx, c, !(*word_len), prefix));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200168 break;
169 case Y_STR_ARG:
170 case Y_MAYBE_STR_ARG:
Michal Vaskob36053d2020-03-26 15:49:30 +0100171 LY_CHECK_RET(lysp_check_stringchar((struct lys_parser_ctx *)ctx, c));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200172 break;
173 }
174
Michal Vasko7fbc8162018-09-17 10:35:16 +0200175 if (word_b && *word_b) {
176 /* add another character into buffer */
Michal Vasko63f3d842020-07-08 10:10:14 +0200177 if (buf_add_char(ctx->ctx, in, len, word_b, buf_len, word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200178 return LY_EMEM;
179 }
180
181 /* in case of realloc */
182 *word_p = *word_b;
Radek Krejcif29b7c32019-04-09 16:17:49 +0200183 } else if (word_b && need_buf) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200184 /* first time we need a buffer, copy everything read up to now */
185 if (*word_len) {
186 *word_b = malloc(*word_len);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200187 LY_CHECK_ERR_RET(!*word_b, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200188 *buf_len = *word_len;
189 memcpy(*word_b, *word_p, *word_len);
190 }
191
192 /* add this new character into buffer */
Michal Vasko63f3d842020-07-08 10:10:14 +0200193 if (buf_add_char(ctx->ctx, in, len, word_b, buf_len, word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200194 return LY_EMEM;
195 }
196
197 /* in case of realloc */
198 *word_p = *word_b;
199 } else {
200 /* just remember the first character pointer */
201 if (!*word_p) {
Michal Vasko63f3d842020-07-08 10:10:14 +0200202 *word_p = (char *)in->current;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200203 }
Radek Krejci44ceedc2018-10-02 15:54:31 +0200204 /* ... and update the word's length */
205 (*word_len) += len;
Michal Vasko63f3d842020-07-08 10:10:14 +0200206 ly_in_skip(in, len);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200207 }
208
209 return LY_SUCCESS;
210}
211
Michal Vaskoea5abea2018-09-18 13:10:54 +0200212/**
213 * @brief Skip YANG comment in data.
214 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200215 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +0200216 * @param[in,out] in Input structure.
Radek Krejci44ceedc2018-10-02 15:54:31 +0200217 * @param[in] comment Type of the comment to process:
218 * 1 for a one-line comment,
219 * 2 for a block comment.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200220 * @return LY_ERR values.
221 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200222LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200223skip_comment(struct lys_yang_parser_ctx *ctx, struct ly_in *in, uint8_t comment)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200224{
Radek Krejci80dd33e2018-09-26 15:57:18 +0200225 /* internal statuses: 0 - comment ended,
226 * 1 - in line comment,
227 * 2 - in block comment,
228 * 3 - in block comment with last read character '*'
229 */
Michal Vasko63f3d842020-07-08 10:10:14 +0200230 while (in->current[0] && comment) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200231 switch (comment) {
232 case 1:
Michal Vasko63f3d842020-07-08 10:10:14 +0200233 if (in->current[0] == '\n') {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200234 comment = 0;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200235 ++ctx->line;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200236 }
237 break;
238 case 2:
Michal Vasko63f3d842020-07-08 10:10:14 +0200239 if (in->current[0] == '*') {
Radek Krejci15c80ca2018-10-09 11:01:31 +0200240 comment = 3;
Michal Vasko63f3d842020-07-08 10:10:14 +0200241 } else if (in->current[0] == '\n') {
Radek Krejci15c80ca2018-10-09 11:01:31 +0200242 ++ctx->line;
243 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200244 break;
245 case 3:
Michal Vasko63f3d842020-07-08 10:10:14 +0200246 if (in->current[0] == '/') {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200247 comment = 0;
Michal Vasko63f3d842020-07-08 10:10:14 +0200248 } else if (in->current[0] != '*') {
249 if (in->current[0] == '\n') {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200250 ++ctx->line;
251 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200252 comment = 2;
253 }
254 break;
255 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +0200256 LOGINT_RET(ctx->ctx);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200257 }
258
Michal Vasko63f3d842020-07-08 10:10:14 +0200259 if (in->current[0] == '\n') {
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200260 ctx->indent = 0;
261 } else {
262 ++ctx->indent;
263 }
Michal Vasko63f3d842020-07-08 10:10:14 +0200264 ++in->current;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200265 }
266
Michal Vasko63f3d842020-07-08 10:10:14 +0200267 if (!in->current[0] && (comment > 1)) {
David Sedlákb3077192019-06-19 10:55:37 +0200268 LOGVAL_PARSER(ctx, LYVE_SYNTAX, "Unexpected end-of-input, non-terminated comment.");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200269 return LY_EVALID;
270 }
271
272 return LY_SUCCESS;
273}
274
Michal Vaskoea5abea2018-09-18 13:10:54 +0200275/**
276 * @brief Read a quoted string from data.
277 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200278 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +0200279 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200280 * @param[in] arg Type of YANG keyword argument expected.
281 * @param[out] word_p Pointer to the read quoted string.
282 * @param[out] word_b Pointer to a dynamically-allocated buffer holding the read quoted string. If not needed,
283 * set to NULL. Otherwise equal to \p word_p.
284 * @param[out] word_len Length of the read quoted string.
285 * @param[out] buf_len Length of the dynamically-allocated buffer \p word_b.
286 * @param[in] indent Current indent (number of YANG spaces). Needed for correct multi-line string
287 * indenation in the final quoted string.
288 *
289 * @return LY_ERR values.
290 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200291static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +0200292read_qstring(struct lys_yang_parser_ctx *ctx, struct ly_in *in, enum yang_arg arg, char **word_p, char **word_b,
Radek Krejci0f969882020-08-21 16:56:47 +0200293 size_t *word_len, size_t *buf_len)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200294{
295 /* string: 0 - string ended, 1 - string with ', 2 - string with ", 3 - string with " with last character \,
296 * 4 - string finished, now skipping whitespaces looking for +,
297 * 5 - string continues after +, skipping whitespaces */
Radek Krejci1deb5be2020-08-26 16:43:36 +0200298 uint8_t string;
299 uint64_t block_indent = 0, current_indent = 0;
Radek Krejci857189e2020-09-01 13:26:36 +0200300 ly_bool need_buf = 0;
301 uint8_t prefix = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200302 const char *c;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200303 uint64_t trailing_ws = 0; /* current number of stored trailing whitespace characters */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200304
Michal Vasko63f3d842020-07-08 10:10:14 +0200305 if (in->current[0] == '\"') {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200306 string = 2;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200307 current_indent = block_indent = ctx->indent + 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200308 } else {
Michal Vasko63f3d842020-07-08 10:10:14 +0200309 assert(in->current[0] == '\'');
Michal Vasko7fbc8162018-09-17 10:35:16 +0200310 string = 1;
311 }
Michal Vasko63f3d842020-07-08 10:10:14 +0200312 MOVE_INPUT(ctx, in, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200313
Michal Vasko63f3d842020-07-08 10:10:14 +0200314 while (in->current[0] && string) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200315 switch (string) {
316 case 1:
Michal Vasko63f3d842020-07-08 10:10:14 +0200317 switch (in->current[0]) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200318 case '\'':
319 /* string may be finished, but check for + */
320 string = 4;
Michal Vasko63f3d842020-07-08 10:10:14 +0200321 MOVE_INPUT(ctx, in, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200322 break;
323 default:
324 /* check and store character */
Michal Vasko63f3d842020-07-08 10:10:14 +0200325 LY_CHECK_RET(buf_store_char(ctx, in, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200326 break;
327 }
328 break;
329 case 2:
Michal Vasko63f3d842020-07-08 10:10:14 +0200330 switch (in->current[0]) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200331 case '\"':
332 /* string may be finished, but check for + */
333 string = 4;
Michal Vasko63f3d842020-07-08 10:10:14 +0200334 MOVE_INPUT(ctx, in, 1);
Radek Krejciff13cd12019-10-25 15:34:24 +0200335 trailing_ws = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200336 break;
337 case '\\':
338 /* special character following */
339 string = 3;
Radek Krejciff13cd12019-10-25 15:34:24 +0200340
341 /* the backslash sequence is substituted, so we will need a buffer to store the result */
342 need_buf = 1;
343
344 /* move forward to the escaped character */
Michal Vasko63f3d842020-07-08 10:10:14 +0200345 ++in->current;
Radek Krejciff13cd12019-10-25 15:34:24 +0200346
347 /* note that the trailing whitespaces are supposed to be trimmed before substitution of
348 * backslash-escaped characters (RFC 7950, 6.1.3), so we have to zero the trailing whitespaces counter */
349 trailing_ws = 0;
350
351 /* since the backslash-escaped character is handled as first non-whitespace character, stop eating indentation */
352 current_indent = block_indent;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200353 break;
354 case ' ':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200355 if (current_indent < block_indent) {
356 ++current_indent;
Michal Vasko63f3d842020-07-08 10:10:14 +0200357 MOVE_INPUT(ctx, in, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200358 } else {
Michal Vasko90edde42019-11-25 15:25:07 +0100359 /* check and store whitespace character */
Michal Vasko63f3d842020-07-08 10:10:14 +0200360 LY_CHECK_RET(buf_store_char(ctx, in, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
Michal Vasko90edde42019-11-25 15:25:07 +0100361 trailing_ws++;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200362 }
363 break;
364 case '\t':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200365 if (current_indent < block_indent) {
366 assert(need_buf);
367 current_indent += 8;
368 ctx->indent += 8;
Michal Vaskod989ba02020-08-24 10:59:24 +0200369 for ( ; current_indent > block_indent; --current_indent, --ctx->indent) {
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200370 /* store leftover spaces from the tab */
Michal Vasko63f3d842020-07-08 10:10:14 +0200371 c = in->current;
372 in->current = " ";
373 LY_CHECK_RET(buf_store_char(ctx, in, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
374 in->current = c;
Michal Vasko90edde42019-11-25 15:25:07 +0100375 trailing_ws++;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200376 }
Michal Vasko63f3d842020-07-08 10:10:14 +0200377 ++in->current;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200378 } else {
Michal Vasko90edde42019-11-25 15:25:07 +0100379 /* check and store whitespace character */
Michal Vasko63f3d842020-07-08 10:10:14 +0200380 LY_CHECK_RET(buf_store_char(ctx, in, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
Michal Vasko90edde42019-11-25 15:25:07 +0100381 trailing_ws++;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200382 /* additional characters for indentation - only 1 was count in buf_store_char */
383 ctx->indent += 7;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200384 }
385 break;
386 case '\n':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200387 if (block_indent) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200388 /* we will be removing the indents so we need our own buffer */
389 need_buf = 1;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200390
391 /* remove trailing tabs and spaces */
Radek Krejciff13cd12019-10-25 15:34:24 +0200392 (*word_len) = *word_len - trailing_ws;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200393
Radek Krejciff13cd12019-10-25 15:34:24 +0200394 /* restart indentation */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200395 current_indent = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200396 }
397
398 /* check and store character */
Michal Vasko63f3d842020-07-08 10:10:14 +0200399 LY_CHECK_RET(buf_store_char(ctx, in, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200400
401 /* maintain line number */
402 ++ctx->line;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200403
404 /* reset context indentation counter for possible string after this one */
405 ctx->indent = 0;
Radek Krejciff13cd12019-10-25 15:34:24 +0200406 trailing_ws = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200407 break;
408 default:
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200409 /* first non-whitespace character, stop eating indentation */
410 current_indent = block_indent;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200411
412 /* check and store character */
Michal Vasko63f3d842020-07-08 10:10:14 +0200413 LY_CHECK_RET(buf_store_char(ctx, in, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
Radek Krejciff13cd12019-10-25 15:34:24 +0200414 trailing_ws = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200415 break;
416 }
417 break;
418 case 3:
419 /* string encoded characters */
Michal Vasko63f3d842020-07-08 10:10:14 +0200420 c = in->current;
421 switch (in->current[0]) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200422 case 'n':
Michal Vasko63f3d842020-07-08 10:10:14 +0200423 in->current = "\n";
Michal Vasko7fbc8162018-09-17 10:35:16 +0200424 break;
425 case 't':
Michal Vasko63f3d842020-07-08 10:10:14 +0200426 in->current = "\t";
Michal Vasko7fbc8162018-09-17 10:35:16 +0200427 break;
428 case '\"':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200429 case '\\':
Michal Vasko63f3d842020-07-08 10:10:14 +0200430 /* ok as is */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200431 break;
432 default:
Michal Vasko63f3d842020-07-08 10:10:14 +0200433 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Double-quoted string unknown special character '\\%c'.",
434 in->current[0]);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200435 return LY_EVALID;
436 }
437
438 /* check and store character */
Michal Vasko63f3d842020-07-08 10:10:14 +0200439 LY_CHECK_RET(buf_store_char(ctx, in, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200440
441 string = 2;
Michal Vasko63f3d842020-07-08 10:10:14 +0200442 in->current = c + 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200443 break;
444 case 4:
Michal Vasko63f3d842020-07-08 10:10:14 +0200445 switch (in->current[0]) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200446 case '+':
447 /* string continues */
448 string = 5;
Radek Krejciefd22f62018-09-27 11:47:58 +0200449 need_buf = 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200450 break;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200451 case '\n':
452 ++ctx->line;
Radek Krejci0f969882020-08-21 16:56:47 +0200453 /* fallthrough */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200454 case ' ':
455 case '\t':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200456 /* just skip */
457 break;
458 default:
459 /* string is finished */
460 goto string_end;
461 }
Michal Vasko63f3d842020-07-08 10:10:14 +0200462 MOVE_INPUT(ctx, in, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200463 break;
464 case 5:
Michal Vasko63f3d842020-07-08 10:10:14 +0200465 switch (in->current[0]) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200466 case '\n':
467 ++ctx->line;
Radek Krejci0f969882020-08-21 16:56:47 +0200468 /* fallthrough */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200469 case ' ':
470 case '\t':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200471 /* skip */
472 break;
473 case '\'':
474 string = 1;
475 break;
476 case '\"':
477 string = 2;
478 break;
479 default:
480 /* it must be quoted again */
David Sedlákb3077192019-06-19 10:55:37 +0200481 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Both string parts divided by '+' must be quoted.");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200482 return LY_EVALID;
483 }
Michal Vasko63f3d842020-07-08 10:10:14 +0200484 MOVE_INPUT(ctx, in, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200485 break;
486 default:
487 return LY_EINT;
488 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200489 }
490
491string_end:
Radek Krejci4e199f52019-05-28 09:09:28 +0200492 if (arg <= Y_PREF_IDENTIF_ARG && !(*word_len)) {
493 /* empty identifier */
David Sedlákb3077192019-06-19 10:55:37 +0200494 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Statement argument is required.");
Radek Krejci4e199f52019-05-28 09:09:28 +0200495 return LY_EVALID;
496 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200497 return LY_SUCCESS;
498}
499
Michal Vaskoea5abea2018-09-18 13:10:54 +0200500/**
501 * @brief Get another YANG string from the raw data.
502 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200503 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +0200504 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200505 * @param[in] arg Type of YANG keyword argument expected.
Radek Krejcid3ca0632019-04-16 16:54:54 +0200506 * @param[out] flags optional output argument to get flag of the argument's quoting (LYS_*QOUTED - see [schema node flags](@ref snodeflags))
Michal Vasko2ca70f52018-09-27 11:04:51 +0200507 * @param[out] word_p Pointer to the read string. Can return NULL if \p arg is #Y_MAYBE_STR_ARG.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200508 * @param[out] word_b Pointer to a dynamically-allocated buffer holding the read string. If not needed,
509 * set to NULL. Otherwise equal to \p word_p.
510 * @param[out] word_len Length of the read string.
511 *
512 * @return LY_ERR values.
Michal Vasko7fbc8162018-09-17 10:35:16 +0200513 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200514LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +0200515get_argument(struct lys_yang_parser_ctx *ctx, struct ly_in *in, enum yang_arg arg, uint16_t *flags, char **word_p,
Radek Krejci0f969882020-08-21 16:56:47 +0200516 char **word_b, size_t *word_len)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200517{
Radek Krejci44ceedc2018-10-02 15:54:31 +0200518 size_t buf_len = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200519 uint8_t prefix = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200520 /* word buffer - dynamically allocated */
521 *word_b = NULL;
522
523 /* word pointer - just a pointer to data */
524 *word_p = NULL;
525
526 *word_len = 0;
Michal Vasko63f3d842020-07-08 10:10:14 +0200527 while (in->current[0]) {
528 switch (in->current[0]) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200529 case '\'':
530 case '\"':
531 if (*word_len) {
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200532 /* invalid - quotes cannot be in unquoted string and only optsep, ; or { can follow it */
Michal Vasko63f3d842020-07-08 10:10:14 +0200533 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, 1, in->current,
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200534 "unquoted string character, optsep, semicolon or opening brace");
535 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200536 }
Radek Krejcid3ca0632019-04-16 16:54:54 +0200537 if (flags) {
Michal Vasko63f3d842020-07-08 10:10:14 +0200538 (*flags) |= in->current[0] == '\'' ? LYS_SINGLEQUOTED : LYS_DOUBLEQUOTED;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200539 }
Michal Vasko63f3d842020-07-08 10:10:14 +0200540 LY_CHECK_RET(read_qstring(ctx, in, arg, word_p, word_b, word_len, &buf_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200541 goto str_end;
542 case '/':
Michal Vasko63f3d842020-07-08 10:10:14 +0200543 if (in->current[1] == '/') {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200544 /* one-line comment */
Michal Vasko63f3d842020-07-08 10:10:14 +0200545 MOVE_INPUT(ctx, in, 2);
546 LY_CHECK_RET(skip_comment(ctx, in, 1));
547 } else if (in->current[1] == '*') {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200548 /* block comment */
Michal Vasko63f3d842020-07-08 10:10:14 +0200549 MOVE_INPUT(ctx, in, 2);
550 LY_CHECK_RET(skip_comment(ctx, in, 2));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200551 } else {
552 /* not a comment after all */
Michal Vasko63f3d842020-07-08 10:10:14 +0200553 LY_CHECK_RET(buf_store_char(ctx, in, arg, word_p, word_len, word_b, &buf_len, 0, &prefix));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200554 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200555 break;
556 case ' ':
557 if (*word_len) {
558 /* word is finished */
559 goto str_end;
560 }
Michal Vasko63f3d842020-07-08 10:10:14 +0200561 MOVE_INPUT(ctx, in, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200562 break;
563 case '\t':
564 if (*word_len) {
565 /* word is finished */
566 goto str_end;
567 }
568 /* tabs count for 8 spaces */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200569 ctx->indent += 8;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200570
Michal Vasko63f3d842020-07-08 10:10:14 +0200571 ++in->current;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200572 break;
573 case '\n':
574 if (*word_len) {
575 /* word is finished */
576 goto str_end;
577 }
578 /* reset indent */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200579 ctx->indent = 0;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200580
581 /* track line numbers */
582 ++ctx->line;
583
Michal Vasko63f3d842020-07-08 10:10:14 +0200584 ++in->current;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200585 break;
586 case ';':
587 case '{':
588 if (*word_len || (arg == Y_MAYBE_STR_ARG)) {
589 /* word is finished */
590 goto str_end;
591 }
592
Michal Vasko63f3d842020-07-08 10:10:14 +0200593 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, 1, in->current, "an argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200594 return LY_EVALID;
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200595 case '}':
596 /* invalid - braces cannot be in unquoted string (opening braces terminates the string and can follow it) */
Michal Vasko63f3d842020-07-08 10:10:14 +0200597 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, 1, in->current,
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200598 "unquoted string character, optsep, semicolon or opening brace");
599 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200600 default:
Michal Vasko63f3d842020-07-08 10:10:14 +0200601 LY_CHECK_RET(buf_store_char(ctx, in, arg, word_p, word_len, word_b, &buf_len, 0, &prefix));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200602 break;
603 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200604 }
605
Radek Krejci0a1d0d42019-05-16 15:14:51 +0200606 /* unexpected end of loop */
David Sedlákb3077192019-06-19 10:55:37 +0200607 LOGVAL_PARSER(ctx, LY_VCODE_EOF);
Radek Krejci0a1d0d42019-05-16 15:14:51 +0200608 return LY_EVALID;
609
Michal Vasko7fbc8162018-09-17 10:35:16 +0200610str_end:
Radek Krejci44ceedc2018-10-02 15:54:31 +0200611 /* terminating NULL byte for buf */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200612 if (*word_b) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200613 (*word_b) = ly_realloc(*word_b, (*word_len) + 1);
614 LY_CHECK_ERR_RET(!(*word_b), LOGMEM(ctx->ctx), LY_EMEM);
615 (*word_b)[*word_len] = '\0';
Michal Vasko7fbc8162018-09-17 10:35:16 +0200616 *word_p = *word_b;
617 }
618
619 return LY_SUCCESS;
620}
621
Michal Vaskoea5abea2018-09-18 13:10:54 +0200622/**
623 * @brief Get another YANG keyword from the raw data.
624 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200625 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +0200626 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200627 * @param[out] kw YANG keyword read.
628 * @param[out] word_p Pointer to the keyword in the data. Useful for extension instances.
629 * @param[out] word_len Length of the keyword in the data. Useful for extension instances.
630 *
631 * @return LY_ERR values.
632 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200633LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +0200634get_keyword(struct lys_yang_parser_ctx *ctx, struct ly_in *in, enum ly_stmt *kw, char **word_p, size_t *word_len)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200635{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200636 uint8_t prefix;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200637 const char *word_start;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200638 size_t len;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200639
640 if (word_p) {
641 *word_p = NULL;
642 *word_len = 0;
643 }
644
645 /* first skip "optsep", comments */
Michal Vasko63f3d842020-07-08 10:10:14 +0200646 while (in->current[0]) {
647 switch (in->current[0]) {
Radek Krejcidcc7b322018-10-11 14:24:02 +0200648 case '/':
Michal Vasko63f3d842020-07-08 10:10:14 +0200649 if (in->current[1] == '/') {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200650 /* one-line comment */
Michal Vasko63f3d842020-07-08 10:10:14 +0200651 MOVE_INPUT(ctx, in, 2);
652 LY_CHECK_RET(skip_comment(ctx, in, 1));
653 } else if (in->current[1] == '*') {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200654 /* block comment */
Michal Vasko63f3d842020-07-08 10:10:14 +0200655 MOVE_INPUT(ctx, in, 2);
656 LY_CHECK_RET(skip_comment(ctx, in, 2));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200657 } else {
Radek Krejcidcc7b322018-10-11 14:24:02 +0200658 /* error - not a comment after all, keyword cannot start with slash */
David Sedlákb3077192019-06-19 10:55:37 +0200659 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid identifier first character '/'.");
Radek Krejcidcc7b322018-10-11 14:24:02 +0200660 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200661 }
Radek Krejci13028282019-06-11 14:56:48 +0200662 continue;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200663 case '\n':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200664 /* skip whitespaces (optsep) */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200665 ++ctx->line;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200666 ctx->indent = 0;
667 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200668 case ' ':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200669 /* skip whitespaces (optsep) */
670 ++ctx->indent;
671 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200672 case '\t':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200673 /* skip whitespaces (optsep) */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200674 ctx->indent += 8;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200675 break;
676 default:
677 /* either a keyword start or an invalid character */
678 goto keyword_start;
679 }
680
Michal Vasko63f3d842020-07-08 10:10:14 +0200681 ly_in_skip(in, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200682 }
683
684keyword_start:
Michal Vasko63f3d842020-07-08 10:10:14 +0200685 word_start = in->current;
686 *kw = lysp_match_kw(ctx, in);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200687
Radek Krejcid6b76452019-09-03 17:03:03 +0200688 if (*kw == LY_STMT_SYNTAX_SEMICOLON || *kw == LY_STMT_SYNTAX_LEFT_BRACE || *kw == LY_STMT_SYNTAX_RIGHT_BRACE) {
Radek Krejci626df482018-10-11 15:06:31 +0200689 goto success;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200690 }
691
Radek Krejcid6b76452019-09-03 17:03:03 +0200692 if (*kw != LY_STMT_NONE) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200693 /* make sure we have the whole keyword */
Michal Vasko63f3d842020-07-08 10:10:14 +0200694 switch (in->current[0]) {
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200695 case '\n':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200696 case '\t':
Radek Krejciabdd8062019-06-11 16:44:19 +0200697 case ' ':
698 /* mandatory "sep" is just checked, not eaten so nothing in the context is updated */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200699 break;
Radek Krejci156ccaf2018-10-15 15:49:17 +0200700 case ':':
701 /* keyword is not actually a keyword, but prefix of an extension.
702 * To avoid repeated check of the prefix syntax, move to the point where the colon was read
703 * and we will be checking the keyword (extension instance) itself */
704 prefix = 1;
Michal Vasko63f3d842020-07-08 10:10:14 +0200705 MOVE_INPUT(ctx, in, 1);
Radek Krejci156ccaf2018-10-15 15:49:17 +0200706 goto extension;
Radek Krejcidcc7b322018-10-11 14:24:02 +0200707 case '{':
708 /* allowed only for input and output statements which can be without arguments */
Radek Krejcid6b76452019-09-03 17:03:03 +0200709 if (*kw == LY_STMT_INPUT || *kw == LY_STMT_OUTPUT) {
Radek Krejcidcc7b322018-10-11 14:24:02 +0200710 break;
711 }
Radek Krejci0f969882020-08-21 16:56:47 +0200712 /* fallthrough */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200713 default:
Michal Vasko63f3d842020-07-08 10:10:14 +0200714 MOVE_INPUT(ctx, in, 1);
715 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, (int)(in->current - word_start), word_start,
Radek Krejci44ceedc2018-10-02 15:54:31 +0200716 "a keyword followed by a separator");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200717 return LY_EVALID;
718 }
719 } else {
720 /* still can be an extension */
721 prefix = 0;
Radek Krejci156ccaf2018-10-15 15:49:17 +0200722extension:
Michal Vasko63f3d842020-07-08 10:10:14 +0200723 while (in->current[0] && (in->current[0] != ' ') && (in->current[0] != '\t') && (in->current[0] != '\n')
724 && (in->current[0] != '{') && (in->current[0] != ';')) {
Radek Krejci1deb5be2020-08-26 16:43:36 +0200725 uint32_t c = 0;
726
Michal Vasko63f3d842020-07-08 10:10:14 +0200727 LY_CHECK_ERR_RET(ly_getutf8(&in->current, &c, &len),
728 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, in->current[-len]), LY_EVALID);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200729 ++ctx->indent;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200730 /* check character validity */
Michal Vasko63f3d842020-07-08 10:10:14 +0200731 LY_CHECK_RET(lysp_check_identifierchar((struct lys_parser_ctx *)ctx, c,
732 in->current - len == word_start ? 1 : 0, &prefix));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200733 }
Michal Vasko63f3d842020-07-08 10:10:14 +0200734 if (!in->current[0]) {
David Sedlákb3077192019-06-19 10:55:37 +0200735 LOGVAL_PARSER(ctx, LY_VCODE_EOF);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200736 return LY_EVALID;
737 }
738
739 /* prefix is mandatory for extension instances */
Radek Krejcidcc7b322018-10-11 14:24:02 +0200740 if (prefix != 2) {
Michal Vasko63f3d842020-07-08 10:10:14 +0200741 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, (int)(in->current - word_start), word_start, "a keyword");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200742 return LY_EVALID;
743 }
744
Radek Krejcid6b76452019-09-03 17:03:03 +0200745 *kw = LY_STMT_EXTENSION_INSTANCE;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200746 }
Radek Krejci626df482018-10-11 15:06:31 +0200747success:
Michal Vasko7fbc8162018-09-17 10:35:16 +0200748 if (word_p) {
749 *word_p = (char *)word_start;
Michal Vasko63f3d842020-07-08 10:10:14 +0200750 *word_len = in->current - word_start;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200751 }
752
753 return LY_SUCCESS;
754}
755
Michal Vaskoea5abea2018-09-18 13:10:54 +0200756/**
757 * @brief Parse extension instance substatements.
758 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200759 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +0200760 * @param[in,out] in Input structure.
Radek Krejci335332a2019-09-05 13:03:35 +0200761 * @param[in] kw Statement keyword value matching @p word value.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200762 * @param[in] word Extension instance substatement name (keyword).
763 * @param[in] word_len Extension instance substatement name length.
764 * @param[in,out] child Children of this extension instance to add to.
765 *
766 * @return LY_ERR values.
767 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200768static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +0200769parse_ext_substmt(struct lys_yang_parser_ctx *ctx, struct ly_in *in, enum ly_stmt kw, char *word, size_t word_len,
Radek Krejci0f969882020-08-21 16:56:47 +0200770 struct lysp_stmt **child)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200771{
772 char *buf;
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100773 LY_ERR ret = LY_SUCCESS;
Radek Krejci335332a2019-09-05 13:03:35 +0200774 enum ly_stmt child_kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200775 struct lysp_stmt *stmt, *par_child;
776
777 stmt = calloc(1, sizeof *stmt);
778 LY_CHECK_ERR_RET(!stmt, LOGMEM(NULL), LY_EMEM);
779
Radek Krejcibb9b1982019-04-08 14:24:59 +0200780 /* insert into parent statements */
781 if (!*child) {
782 *child = stmt;
783 } else {
Radek Krejci1e008d22020-08-17 11:37:37 +0200784 for (par_child = *child; par_child->next; par_child = par_child->next) {}
Radek Krejcibb9b1982019-04-08 14:24:59 +0200785 par_child->next = stmt;
786 }
787
Radek Krejci44ceedc2018-10-02 15:54:31 +0200788 stmt->stmt = lydict_insert(ctx->ctx, word, word_len);
Radek Krejci335332a2019-09-05 13:03:35 +0200789 stmt->kw = kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200790
791 /* get optional argument */
Michal Vasko63f3d842020-07-08 10:10:14 +0200792 LY_CHECK_RET(get_argument(ctx, in, Y_MAYBE_STR_ARG, &stmt->flags, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200793
Radek Krejci0ae092d2018-09-20 16:43:19 +0200794 if (word) {
795 if (buf) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200796 stmt->arg = lydict_insert_zc(ctx->ctx, word);
Radek Krejci0ae092d2018-09-20 16:43:19 +0200797 } else {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200798 stmt->arg = lydict_insert(ctx->ctx, word, word_len);
Radek Krejci0ae092d2018-09-20 16:43:19 +0200799 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200800 }
801
Michal Vasko63f3d842020-07-08 10:10:14 +0200802 YANG_READ_SUBSTMT_FOR(ctx, in, child_kw, word, word_len, ret, ) {
803 LY_CHECK_RET(parse_ext_substmt(ctx, in, child_kw, word, word_len, &stmt->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200804 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200805 return ret;
806}
807
Michal Vaskoea5abea2018-09-18 13:10:54 +0200808/**
809 * @brief Parse extension instance.
810 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200811 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +0200812 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200813 * @param[in] ext_name Extension instance substatement name (keyword).
814 * @param[in] ext_name_len Extension instance substatement name length.
815 * @param[in] insubstmt Type of the keyword this extension instance is a substatement of.
816 * @param[in] insubstmt_index Index of the keyword instance this extension instance is a substatement of.
817 * @param[in,out] exts Extension instances to add to.
818 *
819 * @return LY_ERR values.
820 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200821static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200822parse_ext(struct lys_yang_parser_ctx *ctx, struct ly_in *in, const char *ext_name, size_t ext_name_len, LYEXT_SUBSTMT insubstmt,
Radek Krejci0f969882020-08-21 16:56:47 +0200823 LY_ARRAY_COUNT_TYPE insubstmt_index, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200824{
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100825 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200826 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +0200827 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200828 struct lysp_ext_instance *e;
Radek Krejcid6b76452019-09-03 17:03:03 +0200829 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200830
Radek Krejci2c4e7172018-10-19 15:56:26 +0200831 LY_ARRAY_NEW_RET(ctx->ctx, *exts, e, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200832
833 /* store name and insubstmt info */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200834 e->name = lydict_insert(ctx->ctx, ext_name, ext_name_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200835 e->insubstmt = insubstmt;
836 e->insubstmt_index = insubstmt_index;
837
838 /* get optional argument */
Michal Vasko63f3d842020-07-08 10:10:14 +0200839 LY_CHECK_RET(get_argument(ctx, in, Y_MAYBE_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200840
Radek Krejci0ae092d2018-09-20 16:43:19 +0200841 if (word) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200842 INSERT_WORD(ctx, buf, e->argument, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200843 }
844
Michal Vaskod989ba02020-08-24 10:59:24 +0200845 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko63f3d842020-07-08 10:10:14 +0200846 LY_CHECK_RET(parse_ext_substmt(ctx, in, kw, word, word_len, &e->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200847 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200848 return ret;
849}
850
Michal Vaskoea5abea2018-09-18 13:10:54 +0200851/**
852 * @brief Parse a generic text field without specific constraints. Those are contact, organization,
853 * description, etc...
854 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200855 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +0200856 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200857 * @param[in] substmt Type of this substatement.
858 * @param[in] substmt_index Index of this substatement.
859 * @param[in,out] value Place to store the parsed value.
860 * @param[in] arg Type of the YANG keyword argument (of the value).
861 * @param[in,out] exts Extension instances to add to.
862 *
863 * @return LY_ERR values.
864 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200865static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +0200866parse_text_field(struct lys_yang_parser_ctx *ctx, struct ly_in *in, LYEXT_SUBSTMT substmt, uint32_t substmt_index,
Radek Krejci0f969882020-08-21 16:56:47 +0200867 const char **value, enum yang_arg arg, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200868{
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100869 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200870 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +0200871 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +0200872 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200873
874 if (*value) {
David Sedlákb3077192019-06-19 10:55:37 +0200875 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, lyext_substmt2str(substmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200876 return LY_EVALID;
877 }
878
879 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +0200880 LY_CHECK_RET(get_argument(ctx, in, arg, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200881
882 /* store value and spend buf if allocated */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200883 INSERT_WORD(ctx, buf, *value, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200884
Michal Vaskod989ba02020-08-24 10:59:24 +0200885 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200886 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +0200887 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +0200888 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, substmt, substmt_index, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200889 break;
890 default:
David Sedlákb3077192019-06-19 10:55:37 +0200891 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), lyext_substmt2str(substmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200892 return LY_EVALID;
893 }
894 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200895 return ret;
896}
897
Michal Vaskoea5abea2018-09-18 13:10:54 +0200898/**
899 * @brief Parse the yang-version statement.
900 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200901 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +0200902 * @param[in,out] in Input structure.
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100903 * @param[out] version Storage for the parsed information.
Michal Vasko63f3d842020-07-08 10:10:14 +0200904 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200905 *
906 * @return LY_ERR values.
907 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200908static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +0200909parse_yangversion(struct lys_yang_parser_ctx *ctx, struct ly_in *in, uint8_t *version, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200910{
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100911 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200912 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +0200913 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +0200914 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200915
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100916 if (*version) {
David Sedlákb3077192019-06-19 10:55:37 +0200917 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200918 return LY_EVALID;
919 }
920
921 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +0200922 LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200923
Radek Krejci96e48da2020-09-04 13:18:06 +0200924 if ((word_len == 1) && !strncmp(word, "1", word_len)) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100925 *version = LYS_VERSION_1_0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200926 } else if ((word_len == 3) && !strncmp(word, "1.1", word_len)) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100927 *version = LYS_VERSION_1_1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200928 } else {
David Sedlákb3077192019-06-19 10:55:37 +0200929 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200930 free(buf);
931 return LY_EVALID;
932 }
933 free(buf);
934
Michal Vaskod989ba02020-08-24 10:59:24 +0200935 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200936 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +0200937 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +0200938 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_VERSION, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200939 break;
940 default:
David Sedlákb3077192019-06-19 10:55:37 +0200941 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200942 return LY_EVALID;
943 }
944 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200945 return ret;
946}
947
Michal Vaskoea5abea2018-09-18 13:10:54 +0200948/**
949 * @brief Parse the belongs-to statement.
950 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200951 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +0200952 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200953 * @param[in,out] belongsto Place to store the parsed value.
954 * @param[in,out] prefix Place to store the parsed belongs-to prefix value.
955 * @param[in,out] exts Extension instances to add to.
956 *
957 * @return LY_ERR values.
958 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200959static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +0200960parse_belongsto(struct lys_yang_parser_ctx *ctx, struct ly_in *in, const char **belongsto, const char **prefix, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200961{
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100962 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200963 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +0200964 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +0200965 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200966
967 if (*belongsto) {
David Sedlákb3077192019-06-19 10:55:37 +0200968 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200969 return LY_EVALID;
970 }
971
972 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +0200973 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200974
Radek Krejci44ceedc2018-10-02 15:54:31 +0200975 INSERT_WORD(ctx, buf, *belongsto, word, word_len);
Radek Krejcif09e4e82019-06-14 15:08:11 +0200976
Michal Vasko63f3d842020-07-08 10:10:14 +0200977 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200978 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +0200979 case LY_STMT_PREFIX:
Michal Vasko63f3d842020-07-08 10:10:14 +0200980 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_PREFIX, 0, prefix, Y_IDENTIF_ARG, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200981 break;
Radek Krejcid6b76452019-09-03 17:03:03 +0200982 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +0200983 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_BELONGSTO, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200984 break;
985 default:
David Sedlákb3077192019-06-19 10:55:37 +0200986 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200987 return LY_EVALID;
988 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200989 }
Radek Krejcic59bc972018-09-17 16:13:06 +0200990 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +0100991checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +0200992 /* mandatory substatements */
993 if (!*prefix) {
David Sedlákb3077192019-06-19 10:55:37 +0200994 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200995 return LY_EVALID;
996 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200997 return ret;
998}
999
Michal Vaskoea5abea2018-09-18 13:10:54 +02001000/**
1001 * @brief Parse the revision-date statement.
1002 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001003 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02001004 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001005 * @param[in,out] rev Array to store the parsed value in.
1006 * @param[in,out] exts Extension instances to add to.
1007 *
1008 * @return LY_ERR values.
1009 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001010static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02001011parse_revisiondate(struct lys_yang_parser_ctx *ctx, struct ly_in *in, char *rev, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001012{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001013 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001014 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001015 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001016 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001017
1018 if (rev[0]) {
David Sedlákb3077192019-06-19 10:55:37 +02001019 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "revision-date");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001020 return LY_EVALID;
1021 }
1022
1023 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02001024 LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001025
1026 /* check value */
Michal Vaskob36053d2020-03-26 15:49:30 +01001027 if (lysp_check_date((struct lys_parser_ctx *)ctx, word, word_len, "revision-date")) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001028 free(buf);
1029 return LY_EVALID;
1030 }
1031
1032 /* store value and spend buf if allocated */
1033 strncpy(rev, word, word_len);
1034 free(buf);
1035
Michal Vaskod989ba02020-08-24 10:59:24 +02001036 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001037 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001038 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001039 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_REVISIONDATE, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001040 break;
1041 default:
David Sedlákb3077192019-06-19 10:55:37 +02001042 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "revision-date");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001043 return LY_EVALID;
1044 }
1045 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001046 return ret;
1047}
1048
Michal Vaskoea5abea2018-09-18 13:10:54 +02001049/**
1050 * @brief Parse the include statement.
1051 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001052 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001053 * @param[in] module_name Name of the module to check name collisions.
Michal Vasko63f3d842020-07-08 10:10:14 +02001054 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001055 * @param[in,out] includes Parsed includes to add to.
1056 *
1057 * @return LY_ERR values.
1058 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001059static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02001060parse_include(struct lys_yang_parser_ctx *ctx, const char *module_name, struct ly_in *in, struct lysp_include **includes)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001061{
Radek Krejcid33273d2018-10-25 14:55:52 +02001062 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001063 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001064 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001065 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001066 struct lysp_include *inc;
1067
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001068 LY_ARRAY_NEW_RET(ctx->ctx, *includes, inc, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001069
1070 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02001071 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001072
Radek Krejci086c7132018-10-26 15:29:04 +02001073 INSERT_WORD(ctx, buf, inc->name, word, word_len);
1074
Radek Krejcifaa1eac2018-10-30 14:34:55 +01001075 /* submodules share the namespace with the module names, so there must not be
1076 * a module of the same name in the context, no need for revision matching */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001077 if (!strcmp(module_name, inc->name) || ly_ctx_get_module_latest(ctx->ctx, inc->name)) {
David Sedlákb3077192019-06-19 10:55:37 +02001078 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Name collision between module and submodule of name \"%s\".", inc->name);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01001079 return LY_EVALID;
1080 }
1081
Michal Vaskod989ba02020-08-24 10:59:24 +02001082 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001083 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001084 case LY_STMT_DESCRIPTION:
Radek Krejci335332a2019-09-05 13:03:35 +02001085 PARSER_CHECK_STMTVER2_RET(ctx, "description", "include");
Michal Vasko63f3d842020-07-08 10:10:14 +02001086 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_DESCRIPTION, 0, &inc->dsc, Y_STR_ARG, &inc->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001087 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001088 case LY_STMT_REFERENCE:
Radek Krejci335332a2019-09-05 13:03:35 +02001089 PARSER_CHECK_STMTVER2_RET(ctx, "reference", "include");
Michal Vasko63f3d842020-07-08 10:10:14 +02001090 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_REFERENCE, 0, &inc->ref, Y_STR_ARG, &inc->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001091 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001092 case LY_STMT_REVISION_DATE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001093 LY_CHECK_RET(parse_revisiondate(ctx, in, inc->rev, &inc->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001094 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001095 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001096 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &inc->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001097 break;
1098 default:
David Sedlákb3077192019-06-19 10:55:37 +02001099 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "include");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001100 return LY_EVALID;
1101 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001102 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001103 return ret;
1104}
1105
Michal Vaskoea5abea2018-09-18 13:10:54 +02001106/**
1107 * @brief Parse the import statement.
1108 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001109 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001110 * @param[in] module_prefix Prefix of the module to check prefix collisions.
Michal Vasko63f3d842020-07-08 10:10:14 +02001111 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001112 * @param[in,out] imports Parsed imports to add to.
1113 *
1114 * @return LY_ERR values.
1115 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001116static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02001117parse_import(struct lys_yang_parser_ctx *ctx, const char *module_prefix, struct ly_in *in, struct lysp_import **imports)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001118{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001119 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001120 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001121 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001122 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001123 struct lysp_import *imp;
1124
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001125 LY_ARRAY_NEW_RET(ctx->ctx, *imports, imp, LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001126
1127 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02001128 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02001129 INSERT_WORD(ctx, buf, imp->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001130
Michal Vasko63f3d842020-07-08 10:10:14 +02001131 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001132 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001133 case LY_STMT_PREFIX:
Michal Vasko63f3d842020-07-08 10:10:14 +02001134 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_PREFIX, 0, &imp->prefix, Y_IDENTIF_ARG, &imp->exts));
Michal Vaskob36053d2020-03-26 15:49:30 +01001135 LY_CHECK_RET(lysp_check_prefix((struct lys_parser_ctx *)ctx, *imports, module_prefix, &imp->prefix), LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001136 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001137 case LY_STMT_DESCRIPTION:
Radek Krejci335332a2019-09-05 13:03:35 +02001138 PARSER_CHECK_STMTVER2_RET(ctx, "description", "import");
Michal Vasko63f3d842020-07-08 10:10:14 +02001139 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_DESCRIPTION, 0, &imp->dsc, Y_STR_ARG, &imp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001140 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001141 case LY_STMT_REFERENCE:
Radek Krejci335332a2019-09-05 13:03:35 +02001142 PARSER_CHECK_STMTVER2_RET(ctx, "reference", "import");
Michal Vasko63f3d842020-07-08 10:10:14 +02001143 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_REFERENCE, 0, &imp->ref, Y_STR_ARG, &imp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001144 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001145 case LY_STMT_REVISION_DATE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001146 LY_CHECK_RET(parse_revisiondate(ctx, in, imp->rev, &imp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001147 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001148 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001149 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &imp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001150 break;
1151 default:
David Sedlákb3077192019-06-19 10:55:37 +02001152 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "import");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001153 return LY_EVALID;
1154 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001155 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001156 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01001157checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001158 /* mandatory substatements */
David Sedlákb3077192019-06-19 10:55:37 +02001159 LY_CHECK_ERR_RET(!imp->prefix, LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "import"), LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001160
1161 return ret;
1162}
1163
Michal Vaskoea5abea2018-09-18 13:10:54 +02001164/**
1165 * @brief Parse the revision statement.
1166 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001167 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02001168 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001169 * @param[in,out] revs Parsed revisions to add to.
1170 *
1171 * @return LY_ERR values.
1172 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001173static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02001174parse_revision(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_revision **revs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001175{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001176 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001177 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001178 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001179 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001180 struct lysp_revision *rev;
1181
Radek Krejci2c4e7172018-10-19 15:56:26 +02001182 LY_ARRAY_NEW_RET(ctx->ctx, *revs, rev, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001183
1184 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02001185 LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001186
1187 /* check value */
Michal Vaskob36053d2020-03-26 15:49:30 +01001188 if (lysp_check_date((struct lys_parser_ctx *)ctx, word, word_len, "revision")) {
David Sedlák68ef3dc2019-07-22 13:40:19 +02001189 free(buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001190 return LY_EVALID;
1191 }
1192
Radek Krejcib7db73a2018-10-24 14:18:40 +02001193 strncpy(rev->date, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001194 free(buf);
1195
Michal Vaskod989ba02020-08-24 10:59:24 +02001196 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001197 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001198 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02001199 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_DESCRIPTION, 0, &rev->dsc, Y_STR_ARG, &rev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001200 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001201 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001202 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_REFERENCE, 0, &rev->ref, Y_STR_ARG, &rev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001203 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001204 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001205 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &rev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001206 break;
1207 default:
David Sedlákb3077192019-06-19 10:55:37 +02001208 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "revision");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001209 return LY_EVALID;
1210 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001211 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001212 return ret;
1213}
1214
Michal Vaskoea5abea2018-09-18 13:10:54 +02001215/**
1216 * @brief Parse a generic text field that can have more instances such as base.
1217 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001218 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02001219 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001220 * @param[in] substmt Type of this substatement.
1221 * @param[in,out] texts Parsed values to add to.
1222 * @param[in] arg Type of the expected argument.
1223 * @param[in,out] exts Extension instances to add to.
1224 *
1225 * @return LY_ERR values.
1226 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001227static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02001228parse_text_fields(struct lys_yang_parser_ctx *ctx, struct ly_in *in, LYEXT_SUBSTMT substmt, const char ***texts, enum yang_arg arg,
Radek Krejci0f969882020-08-21 16:56:47 +02001229 struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001230{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001231 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001232 char *buf, *word;
Radek Krejci151a5b72018-10-19 14:21:44 +02001233 const char **item;
1234 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001235 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001236
1237 /* allocate new pointer */
Radek Krejci2c4e7172018-10-19 15:56:26 +02001238 LY_ARRAY_NEW_RET(ctx->ctx, *texts, item, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001239
1240 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02001241 LY_CHECK_RET(get_argument(ctx, in, arg, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001242
Radek Krejci151a5b72018-10-19 14:21:44 +02001243 INSERT_WORD(ctx, buf, *item, word, word_len);
Michal Vaskod989ba02020-08-24 10:59:24 +02001244 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001245 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001246 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001247 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, substmt, LY_ARRAY_COUNT(*texts) - 1, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001248 break;
1249 default:
David Sedlákb3077192019-06-19 10:55:37 +02001250 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), lyext_substmt2str(substmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001251 return LY_EVALID;
1252 }
1253 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001254 return ret;
1255}
1256
Michal Vaskoea5abea2018-09-18 13:10:54 +02001257/**
1258 * @brief Parse the config statement.
1259 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001260 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02001261 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001262 * @param[in,out] flags Flags to add to.
1263 * @param[in,out] exts Extension instances to add to.
1264 *
1265 * @return LY_ERR values.
1266 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001267static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02001268parse_config(struct lys_yang_parser_ctx *ctx, struct ly_in *in, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001269{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001270 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001271 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001272 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001273 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001274
1275 if (*flags & LYS_CONFIG_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02001276 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001277 return LY_EVALID;
1278 }
1279
1280 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02001281 LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001282
1283 if ((word_len == 4) && !strncmp(word, "true", word_len)) {
1284 *flags |= LYS_CONFIG_W;
1285 } else if ((word_len == 5) && !strncmp(word, "false", word_len)) {
1286 *flags |= LYS_CONFIG_R;
1287 } else {
David Sedlákb3077192019-06-19 10:55:37 +02001288 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001289 free(buf);
1290 return LY_EVALID;
1291 }
1292 free(buf);
1293
Michal Vaskod989ba02020-08-24 10:59:24 +02001294 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001295 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001296 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001297 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_CONFIG, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001298 break;
1299 default:
David Sedlákb3077192019-06-19 10:55:37 +02001300 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001301 return LY_EVALID;
1302 }
1303 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001304 return ret;
1305}
1306
Michal Vaskoea5abea2018-09-18 13:10:54 +02001307/**
1308 * @brief Parse the mandatory statement.
1309 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001310 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02001311 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001312 * @param[in,out] flags Flags to add to.
1313 * @param[in,out] exts Extension instances to add to.
1314 *
1315 * @return LY_ERR values.
1316 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001317static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02001318parse_mandatory(struct lys_yang_parser_ctx *ctx, struct ly_in *in, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001319{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001320 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001321 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001322 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001323 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001324
1325 if (*flags & LYS_MAND_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02001326 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001327 return LY_EVALID;
1328 }
1329
1330 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02001331 LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001332
1333 if ((word_len == 4) && !strncmp(word, "true", word_len)) {
1334 *flags |= LYS_MAND_TRUE;
1335 } else if ((word_len == 5) && !strncmp(word, "false", word_len)) {
1336 *flags |= LYS_MAND_FALSE;
1337 } else {
David Sedlákb3077192019-06-19 10:55:37 +02001338 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001339 free(buf);
1340 return LY_EVALID;
1341 }
1342 free(buf);
1343
Michal Vaskod989ba02020-08-24 10:59:24 +02001344 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001345 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001346 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001347 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_MANDATORY, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001348 break;
1349 default:
David Sedlákb3077192019-06-19 10:55:37 +02001350 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001351 return LY_EVALID;
1352 }
1353 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001354 return ret;
1355}
1356
Michal Vaskoea5abea2018-09-18 13:10:54 +02001357/**
1358 * @brief Parse a restriction such as range or length.
1359 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001360 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02001361 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001362 * @param[in] restr_kw Type of this particular restriction.
1363 * @param[in,out] exts Extension instances to add to.
1364 *
1365 * @return LY_ERR values.
1366 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001367static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02001368parse_restr(struct lys_yang_parser_ctx *ctx, struct ly_in *in, enum ly_stmt restr_kw, struct lysp_restr *restr)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001369{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001370 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001371 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001372 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001373 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001374
1375 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02001376 LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001377
Michal Vaskob36053d2020-03-26 15:49:30 +01001378 CHECK_NONEMPTY(ctx, word_len, ly_stmt2str(restr_kw));
Radek Krejci44ceedc2018-10-02 15:54:31 +02001379 INSERT_WORD(ctx, buf, restr->arg, word, word_len);
Michal Vaskod989ba02020-08-24 10:59:24 +02001380 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001381 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001382 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02001383 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_DESCRIPTION, 0, &restr->dsc, Y_STR_ARG, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001384 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001385 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001386 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_REFERENCE, 0, &restr->ref, Y_STR_ARG, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001387 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001388 case LY_STMT_ERROR_APP_TAG:
Michal Vasko63f3d842020-07-08 10:10:14 +02001389 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_ERRTAG, 0, &restr->eapptag, Y_STR_ARG, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001390 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001391 case LY_STMT_ERROR_MESSAGE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001392 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_ERRMSG, 0, &restr->emsg, Y_STR_ARG, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001393 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001394 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001395 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001396 break;
1397 default:
David Sedlákb3077192019-06-19 10:55:37 +02001398 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(restr_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001399 return LY_EVALID;
1400 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001401 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001402 return ret;
1403}
1404
Michal Vaskoea5abea2018-09-18 13:10:54 +02001405/**
1406 * @brief Parse a restriction that can have more instances such as must.
1407 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001408 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02001409 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001410 * @param[in] restr_kw Type of this particular restriction.
1411 * @param[in,out] restrs Restrictions to add to.
1412 *
1413 * @return LY_ERR values.
1414 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001415static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02001416parse_restrs(struct lys_yang_parser_ctx *ctx, struct ly_in *in, enum ly_stmt restr_kw, struct lysp_restr **restrs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001417{
1418 struct lysp_restr *restr;
1419
Radek Krejci2c4e7172018-10-19 15:56:26 +02001420 LY_ARRAY_NEW_RET(ctx->ctx, *restrs, restr, LY_EMEM);
Michal Vasko63f3d842020-07-08 10:10:14 +02001421 return parse_restr(ctx, in, restr_kw, restr);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001422}
1423
Michal Vaskoea5abea2018-09-18 13:10:54 +02001424/**
1425 * @brief Parse the status statement.
1426 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001427 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02001428 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001429 * @param[in,out] flags Flags to add to.
1430 * @param[in,out] exts Extension instances to add to.
1431 *
1432 * @return LY_ERR values.
1433 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001434static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02001435parse_status(struct lys_yang_parser_ctx *ctx, struct ly_in *in, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001436{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001437 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001438 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001439 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001440 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001441
1442 if (*flags & LYS_STATUS_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02001443 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001444 return LY_EVALID;
1445 }
1446
1447 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02001448 LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001449
1450 if ((word_len == 7) && !strncmp(word, "current", word_len)) {
1451 *flags |= LYS_STATUS_CURR;
1452 } else if ((word_len == 10) && !strncmp(word, "deprecated", word_len)) {
1453 *flags |= LYS_STATUS_DEPRC;
1454 } else if ((word_len == 8) && !strncmp(word, "obsolete", word_len)) {
1455 *flags |= LYS_STATUS_OBSLT;
1456 } else {
David Sedlákb3077192019-06-19 10:55:37 +02001457 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001458 free(buf);
1459 return LY_EVALID;
1460 }
1461 free(buf);
1462
Michal Vaskod989ba02020-08-24 10:59:24 +02001463 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001464 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001465 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001466 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_STATUS, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001467 break;
1468 default:
David Sedlákb3077192019-06-19 10:55:37 +02001469 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001470 return LY_EVALID;
1471 }
1472 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001473 return ret;
1474}
1475
Michal Vaskoea5abea2018-09-18 13:10:54 +02001476/**
1477 * @brief Parse the when statement.
1478 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001479 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02001480 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001481 * @param[in,out] when_p When pointer to parse to.
1482 *
1483 * @return LY_ERR values.
1484 */
Radek Krejcif09e4e82019-06-14 15:08:11 +02001485LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02001486parse_when(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_when **when_p)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001487{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001488 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001489 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001490 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001491 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001492 struct lysp_when *when;
1493
1494 if (*when_p) {
David Sedlákb3077192019-06-19 10:55:37 +02001495 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "when");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001496 return LY_EVALID;
1497 }
1498
1499 when = calloc(1, sizeof *when);
Radek Krejci44ceedc2018-10-02 15:54:31 +02001500 LY_CHECK_ERR_RET(!when, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001501
1502 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02001503 LY_CHECK_ERR_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len), free(when), LY_EMEM);
Michal Vaskob36053d2020-03-26 15:49:30 +01001504 CHECK_NONEMPTY(ctx, word_len, "when");
Radek Krejci44ceedc2018-10-02 15:54:31 +02001505 INSERT_WORD(ctx, buf, when->cond, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001506
Radek Krejcif09e4e82019-06-14 15:08:11 +02001507 *when_p = when;
1508
Michal Vaskod989ba02020-08-24 10:59:24 +02001509 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001510 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001511 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02001512 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_DESCRIPTION, 0, &when->dsc, Y_STR_ARG, &when->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001513 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001514 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001515 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_REFERENCE, 0, &when->ref, Y_STR_ARG, &when->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001516 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001517 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001518 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &when->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001519 break;
1520 default:
David Sedlákb3077192019-06-19 10:55:37 +02001521 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "when");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001522 return LY_EVALID;
1523 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001524 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001525 return ret;
1526}
1527
Michal Vaskoea5abea2018-09-18 13:10:54 +02001528/**
1529 * @brief Parse the anydata or anyxml statement.
1530 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001531 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02001532 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001533 * @param[in] kw Type of this particular keyword.
1534 * @param[in,out] siblings Siblings to add to.
1535 *
1536 * @return LY_ERR values.
1537 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02001538LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02001539parse_any(struct lys_yang_parser_ctx *ctx, struct ly_in *in, enum ly_stmt kw, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001540{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001541 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001542 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001543 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001544 struct lysp_node_anydata *any;
1545
David Sedlák60adc092019-08-06 15:57:02 +02001546 /* create new structure and insert into siblings */
David Sedlákb9eeb9c2019-09-13 11:17:19 +02001547 LY_LIST_NEW_RET(ctx->ctx, siblings, any, next, LY_EMEM);
David Sedlák60adc092019-08-06 15:57:02 +02001548
Radek Krejcid6b76452019-09-03 17:03:03 +02001549 any->nodetype = kw == LY_STMT_ANYDATA ? LYS_ANYDATA : LYS_ANYXML;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001550 any->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001551
Michal Vasko7fbc8162018-09-17 10:35:16 +02001552 /* get name */
Michal Vasko63f3d842020-07-08 10:10:14 +02001553 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02001554 INSERT_WORD(ctx, buf, any->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001555
1556 /* parse substatements */
Michal Vaskod989ba02020-08-24 10:59:24 +02001557 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001558 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001559 case LY_STMT_CONFIG:
Michal Vasko63f3d842020-07-08 10:10:14 +02001560 LY_CHECK_RET(parse_config(ctx, in, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001561 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001562 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02001563 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_DESCRIPTION, 0, &any->dsc, Y_STR_ARG, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001564 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001565 case LY_STMT_IF_FEATURE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001566 LY_CHECK_RET(parse_text_fields(ctx, in, LYEXT_SUBSTMT_IFFEATURE, &any->iffeatures, Y_STR_ARG, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001567 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001568 case LY_STMT_MANDATORY:
Michal Vasko63f3d842020-07-08 10:10:14 +02001569 LY_CHECK_RET(parse_mandatory(ctx, in, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001570 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001571 case LY_STMT_MUST:
Michal Vasko63f3d842020-07-08 10:10:14 +02001572 LY_CHECK_RET(parse_restrs(ctx, in, kw, &any->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001573 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001574 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001575 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_REFERENCE, 0, &any->ref, Y_STR_ARG, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001576 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001577 case LY_STMT_STATUS:
Michal Vasko63f3d842020-07-08 10:10:14 +02001578 LY_CHECK_RET(parse_status(ctx, in, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001579 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001580 case LY_STMT_WHEN:
Michal Vasko63f3d842020-07-08 10:10:14 +02001581 LY_CHECK_RET(parse_when(ctx, in, &any->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001582 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001583 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001584 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001585 break;
1586 default:
David Sedlákb3077192019-06-19 10:55:37 +02001587 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw),
Radek Krejci0f969882020-08-21 16:56:47 +02001588 (any->nodetype & LYS_ANYDATA) == LYS_ANYDATA ? ly_stmt2str(LY_STMT_ANYDATA) : ly_stmt2str(LY_STMT_ANYXML));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001589 return LY_EVALID;
1590 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001591 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001592 return ret;
1593}
1594
Michal Vaskoea5abea2018-09-18 13:10:54 +02001595/**
1596 * @brief Parse the value or position statement. Substatement of type enum statement.
1597 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001598 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02001599 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001600 * @param[in] val_kw Type of this particular keyword.
1601 * @param[in,out] value Value to write to.
1602 * @param[in,out] flags Flags to write to.
1603 * @param[in,out] exts Extension instances to add to.
1604 *
1605 * @return LY_ERR values.
1606 */
David Sedlákd6ce6d72019-07-16 17:30:18 +02001607LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02001608parse_type_enum_value_pos(struct lys_yang_parser_ctx *ctx, struct ly_in *in, enum ly_stmt val_kw, int64_t *value, uint16_t *flags,
Radek Krejci0f969882020-08-21 16:56:47 +02001609 struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001610{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001611 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001612 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02001613 size_t word_len;
Radek Krejci4ad42aa2019-07-23 16:55:58 +02001614 long int num = 0;
1615 unsigned long int unum = 0;
Radek Krejcid6b76452019-09-03 17:03:03 +02001616 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001617
1618 if (*flags & LYS_SET_VALUE) {
David Sedlákb3077192019-06-19 10:55:37 +02001619 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(val_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001620 return LY_EVALID;
1621 }
1622 *flags |= LYS_SET_VALUE;
1623
1624 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02001625 LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001626
Radek Krejcid6b76452019-09-03 17:03:03 +02001627 if (!word_len || (word[0] == '+') || ((word[0] == '0') && (word_len > 1)) || ((val_kw == LY_STMT_POSITION) && !strncmp(word, "-0", 2))) {
David Sedlákb3077192019-06-19 10:55:37 +02001628 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001629 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001630 }
1631
1632 errno = 0;
Radek Krejcid6b76452019-09-03 17:03:03 +02001633 if (val_kw == LY_STMT_VALUE) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001634 num = strtol(word, &ptr, 10);
Radek Krejci8b764662018-11-14 14:15:13 +01001635 if (num < INT64_C(-2147483648) || num > INT64_C(2147483647)) {
David Sedlákb3077192019-06-19 10:55:37 +02001636 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001637 goto error;
1638 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001639 } else {
1640 unum = strtoul(word, &ptr, 10);
Radek Krejci8b764662018-11-14 14:15:13 +01001641 if (unum > UINT64_C(4294967295)) {
David Sedlákb3077192019-06-19 10:55:37 +02001642 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001643 goto error;
1644 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001645 }
1646 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02001647 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02001648 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001649 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001650 }
1651 if (errno == ERANGE) {
David Sedlákb3077192019-06-19 10:55:37 +02001652 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001653 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001654 }
Radek Krejcid6b76452019-09-03 17:03:03 +02001655 if (val_kw == LY_STMT_VALUE) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001656 *value = num;
1657 } else {
1658 *value = unum;
1659 }
1660 free(buf);
1661
Michal Vaskod989ba02020-08-24 10:59:24 +02001662 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001663 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001664 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001665 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, val_kw == LY_STMT_VALUE ? LYEXT_SUBSTMT_VALUE : LYEXT_SUBSTMT_POSITION, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001666 break;
1667 default:
David Sedlákb3077192019-06-19 10:55:37 +02001668 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(val_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001669 return LY_EVALID;
1670 }
1671 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001672 return ret;
Radek Krejci8b764662018-11-14 14:15:13 +01001673
1674error:
1675 free(buf);
1676 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001677}
1678
Michal Vaskoea5abea2018-09-18 13:10:54 +02001679/**
1680 * @brief Parse the enum or bit statement. Substatement of type statement.
1681 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001682 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02001683 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001684 * @param[in] enum_kw Type of this particular keyword.
1685 * @param[in,out] enums Enums or bits to add to.
1686 *
1687 * @return LY_ERR values.
1688 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001689static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02001690parse_type_enum(struct lys_yang_parser_ctx *ctx, struct ly_in *in, enum ly_stmt enum_kw, struct lysp_type_enum **enums)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001691{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001692 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001693 char *buf, *word;
David Sedlák6544c182019-07-12 13:17:33 +02001694 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001695 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001696 struct lysp_type_enum *enm;
1697
Radek Krejci2c4e7172018-10-19 15:56:26 +02001698 LY_ARRAY_NEW_RET(ctx->ctx, *enums, enm, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001699
1700 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02001701 LY_CHECK_RET(get_argument(ctx, in, enum_kw == LY_STMT_ENUM ? Y_STR_ARG : Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejcid6b76452019-09-03 17:03:03 +02001702 if (enum_kw == LY_STMT_ENUM) {
Michal Vaskob36053d2020-03-26 15:49:30 +01001703 ret = lysp_check_enum_name((struct lys_parser_ctx *)ctx, (const char *)word, word_len);
David Sedlák6544c182019-07-12 13:17:33 +02001704 LY_CHECK_ERR_RET(ret, free(buf), ret);
Radek Krejci335332a2019-09-05 13:03:35 +02001705 } /* else nothing specific for YANG_BIT */
Radek Krejci8b764662018-11-14 14:15:13 +01001706
Radek Krejci44ceedc2018-10-02 15:54:31 +02001707 INSERT_WORD(ctx, buf, enm->name, word, word_len);
Radek Krejci8b764662018-11-14 14:15:13 +01001708 CHECK_UNIQUENESS(ctx, *enums, name, ly_stmt2str(enum_kw), enm->name);
1709
Michal Vaskod989ba02020-08-24 10:59:24 +02001710 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001711 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001712 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02001713 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_DESCRIPTION, 0, &enm->dsc, Y_STR_ARG, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001714 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001715 case LY_STMT_IF_FEATURE:
Radek Krejci335332a2019-09-05 13:03:35 +02001716 PARSER_CHECK_STMTVER2_RET(ctx, "if-feature", ly_stmt2str(enum_kw));
Michal Vasko63f3d842020-07-08 10:10:14 +02001717 LY_CHECK_RET(parse_text_fields(ctx, in, LYEXT_SUBSTMT_IFFEATURE, &enm->iffeatures, Y_STR_ARG, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001718 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001719 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001720 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_REFERENCE, 0, &enm->ref, Y_STR_ARG, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001721 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001722 case LY_STMT_STATUS:
Michal Vasko63f3d842020-07-08 10:10:14 +02001723 LY_CHECK_RET(parse_status(ctx, in, &enm->flags, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001724 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001725 case LY_STMT_VALUE:
1726 LY_CHECK_ERR_RET(enum_kw == LY_STMT_BIT, LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw),
David Sedlák9fb515f2019-07-11 10:33:58 +02001727 ly_stmt2str(enum_kw)), LY_EVALID);
Michal Vasko63f3d842020-07-08 10:10:14 +02001728 LY_CHECK_RET(parse_type_enum_value_pos(ctx, in, kw, &enm->value, &enm->flags, &enm->exts));
David Sedlák9fb515f2019-07-11 10:33:58 +02001729 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001730 case LY_STMT_POSITION:
1731 LY_CHECK_ERR_RET(enum_kw == LY_STMT_ENUM, LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw),
David Sedlák9fb515f2019-07-11 10:33:58 +02001732 ly_stmt2str(enum_kw)), LY_EVALID);
Michal Vasko63f3d842020-07-08 10:10:14 +02001733 LY_CHECK_RET(parse_type_enum_value_pos(ctx, in, kw, &enm->value, &enm->flags, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001734 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001735 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001736 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001737 break;
1738 default:
David Sedlákb3077192019-06-19 10:55:37 +02001739 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(enum_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001740 return LY_EVALID;
1741 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001742 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001743 return ret;
1744}
1745
Michal Vaskoea5abea2018-09-18 13:10:54 +02001746/**
1747 * @brief Parse the fraction-digits statement. Substatement of type statement.
1748 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001749 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02001750 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001751 * @param[in,out] fracdig Value to write to.
1752 * @param[in,out] exts Extension instances to add to.
1753 *
1754 * @return LY_ERR values.
1755 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001756static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02001757parse_type_fracdigits(struct lys_yang_parser_ctx *ctx, struct ly_in *in, uint8_t *fracdig, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001758{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001759 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001760 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02001761 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001762 unsigned long int num;
Radek Krejcid6b76452019-09-03 17:03:03 +02001763 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001764
1765 if (*fracdig) {
David Sedlákb3077192019-06-19 10:55:37 +02001766 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001767 return LY_EVALID;
1768 }
1769
1770 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02001771 LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001772
1773 if (!word_len || (word[0] == '0') || !isdigit(word[0])) {
David Sedlákb3077192019-06-19 10:55:37 +02001774 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001775 free(buf);
1776 return LY_EVALID;
1777 }
1778
1779 errno = 0;
1780 num = strtoul(word, &ptr, 10);
1781 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02001782 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02001783 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001784 free(buf);
1785 return LY_EVALID;
1786 }
1787 if ((errno == ERANGE) || (num > 18)) {
David Sedlákb3077192019-06-19 10:55:37 +02001788 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001789 free(buf);
1790 return LY_EVALID;
1791 }
1792 *fracdig = num;
1793 free(buf);
1794
Michal Vaskod989ba02020-08-24 10:59:24 +02001795 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001796 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001797 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001798 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_FRACDIGITS, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001799 break;
1800 default:
David Sedlákb3077192019-06-19 10:55:37 +02001801 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001802 return LY_EVALID;
1803 }
1804 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001805 return ret;
1806}
1807
Michal Vaskoea5abea2018-09-18 13:10:54 +02001808/**
1809 * @brief Parse the require-instance statement. Substatement of type statement.
1810 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001811 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02001812 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001813 * @param[in,out] reqinst Value to write to.
1814 * @param[in,out] flags Flags to write to.
1815 * @param[in,out] exts Extension instances to add to.
1816 *
1817 * @return LY_ERR values.
1818 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001819static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02001820parse_type_reqinstance(struct lys_yang_parser_ctx *ctx, struct ly_in *in, uint8_t *reqinst, uint16_t *flags,
Radek Krejci0f969882020-08-21 16:56:47 +02001821 struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001822{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001823 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001824 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001825 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001826 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001827
1828 if (*flags & LYS_SET_REQINST) {
David Sedlákb3077192019-06-19 10:55:37 +02001829 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001830 return LY_EVALID;
1831 }
1832 *flags |= LYS_SET_REQINST;
1833
1834 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02001835 LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001836
1837 if ((word_len == 4) && !strncmp(word, "true", word_len)) {
1838 *reqinst = 1;
1839 } else if ((word_len != 5) || strncmp(word, "false", word_len)) {
David Sedlákb3077192019-06-19 10:55:37 +02001840 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001841 free(buf);
1842 return LY_EVALID;
1843 }
1844 free(buf);
1845
Michal Vaskod989ba02020-08-24 10:59:24 +02001846 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001847 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001848 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001849 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_REQINSTANCE, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001850 break;
1851 default:
David Sedlákb3077192019-06-19 10:55:37 +02001852 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001853 return LY_EVALID;
1854 }
1855 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001856 return ret;
1857}
1858
Michal Vaskoea5abea2018-09-18 13:10:54 +02001859/**
1860 * @brief Parse the modifier statement. Substatement of type pattern statement.
1861 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001862 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02001863 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001864 * @param[in,out] pat Value to write to.
1865 * @param[in,out] exts Extension instances to add to.
1866 *
1867 * @return LY_ERR values.
1868 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001869static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02001870parse_type_pattern_modifier(struct lys_yang_parser_ctx *ctx, struct ly_in *in, const char **pat,
Radek Krejci0f969882020-08-21 16:56:47 +02001871 struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001872{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001873 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001874 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001875 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001876 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001877
1878 if ((*pat)[0] == 0x15) {
David Sedlákb3077192019-06-19 10:55:37 +02001879 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001880 return LY_EVALID;
1881 }
1882
1883 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02001884 LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001885
1886 if ((word_len != 12) || strncmp(word, "invert-match", word_len)) {
David Sedlákb3077192019-06-19 10:55:37 +02001887 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001888 free(buf);
1889 return LY_EVALID;
1890 }
1891 free(buf);
1892
1893 /* replace the value in the dictionary */
1894 buf = malloc(strlen(*pat) + 1);
Radek Krejci44ceedc2018-10-02 15:54:31 +02001895 LY_CHECK_ERR_RET(!buf, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001896 strcpy(buf, *pat);
Radek Krejci44ceedc2018-10-02 15:54:31 +02001897 lydict_remove(ctx->ctx, *pat);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001898
1899 assert(buf[0] == 0x06);
1900 buf[0] = 0x15;
Radek Krejci44ceedc2018-10-02 15:54:31 +02001901 *pat = lydict_insert_zc(ctx->ctx, buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001902
Michal Vaskod989ba02020-08-24 10:59:24 +02001903 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001904 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001905 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001906 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_MODIFIER, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001907 break;
1908 default:
David Sedlákb3077192019-06-19 10:55:37 +02001909 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001910 return LY_EVALID;
1911 }
1912 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001913 return ret;
1914}
1915
Michal Vaskoea5abea2018-09-18 13:10:54 +02001916/**
1917 * @brief Parse the pattern statement. Substatement of type statement.
1918 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001919 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02001920 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001921 * @param[in,out] patterns Restrictions to add to.
1922 *
1923 * @return LY_ERR values.
1924 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001925static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02001926parse_type_pattern(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_restr **patterns)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001927{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001928 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001929 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001930 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001931 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001932 struct lysp_restr *restr;
1933
Radek Krejci2c4e7172018-10-19 15:56:26 +02001934 LY_ARRAY_NEW_RET(ctx->ctx, *patterns, restr, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001935
1936 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02001937 LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001938
1939 /* add special meaning first byte */
1940 if (buf) {
1941 buf = realloc(buf, word_len + 2);
1942 word = buf;
1943 } else {
1944 buf = malloc(word_len + 2);
1945 }
Radek Krejci44ceedc2018-10-02 15:54:31 +02001946 LY_CHECK_ERR_RET(!buf, LOGMEM(ctx->ctx), LY_EMEM);
Radek Krejci86d106e2018-10-18 09:53:19 +02001947 memmove(buf + 1, word, word_len);
1948 buf[0] = 0x06; /* pattern's default regular-match flag */
1949 buf[word_len + 1] = '\0'; /* terminating NULL byte */
1950 restr->arg = lydict_insert_zc(ctx->ctx, buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001951
Michal Vaskod989ba02020-08-24 10:59:24 +02001952 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001953 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001954 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02001955 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_DESCRIPTION, 0, &restr->dsc, Y_STR_ARG, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001956 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001957 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001958 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_REFERENCE, 0, &restr->ref, Y_STR_ARG, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001959 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001960 case LY_STMT_ERROR_APP_TAG:
Michal Vasko63f3d842020-07-08 10:10:14 +02001961 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_ERRTAG, 0, &restr->eapptag, Y_STR_ARG, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001962 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001963 case LY_STMT_ERROR_MESSAGE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001964 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_ERRMSG, 0, &restr->emsg, Y_STR_ARG, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001965 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001966 case LY_STMT_MODIFIER:
Radek Krejci335332a2019-09-05 13:03:35 +02001967 PARSER_CHECK_STMTVER2_RET(ctx, "modifier", "pattern");
Michal Vasko63f3d842020-07-08 10:10:14 +02001968 LY_CHECK_RET(parse_type_pattern_modifier(ctx, in, &restr->arg, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001969 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001970 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02001971 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001972 break;
1973 default:
David Sedlákb3077192019-06-19 10:55:37 +02001974 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "pattern");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001975 return LY_EVALID;
1976 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001977 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001978 return ret;
1979}
1980
Michal Vaskoea5abea2018-09-18 13:10:54 +02001981/**
1982 * @brief Parse the type statement.
1983 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001984 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02001985 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001986 * @param[in,out] type Type to wrote to.
1987 *
1988 * @return LY_ERR values.
1989 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001990static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02001991parse_type(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_type *type)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001992{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001993 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001994 char *buf, *word;
Michal Vasko004d3152020-06-11 19:59:22 +02001995 const char *str_path = NULL;
Radek Krejciefd22f62018-09-27 11:47:58 +02001996 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001997 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001998 struct lysp_type *nest_type;
1999
2000 if (type->name) {
David Sedlákb3077192019-06-19 10:55:37 +02002001 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "type");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002002 return LY_EVALID;
2003 }
2004
2005 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02002006 LY_CHECK_RET(get_argument(ctx, in, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02002007 INSERT_WORD(ctx, buf, type->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002008
Michal Vaskod989ba02020-08-24 10:59:24 +02002009 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002010 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002011 case LY_STMT_BASE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002012 LY_CHECK_RET(parse_text_fields(ctx, in, LYEXT_SUBSTMT_BASE, &type->bases, Y_PREF_IDENTIF_ARG, &type->exts));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002013 type->flags |= LYS_SET_BASE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002014 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002015 case LY_STMT_BIT:
Michal Vasko63f3d842020-07-08 10:10:14 +02002016 LY_CHECK_RET(parse_type_enum(ctx, in, kw, &type->bits));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002017 type->flags |= LYS_SET_BIT;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002018 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002019 case LY_STMT_ENUM:
Michal Vasko63f3d842020-07-08 10:10:14 +02002020 LY_CHECK_RET(parse_type_enum(ctx, in, kw, &type->enums));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002021 type->flags |= LYS_SET_ENUM;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002022 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002023 case LY_STMT_FRACTION_DIGITS:
Michal Vasko63f3d842020-07-08 10:10:14 +02002024 LY_CHECK_RET(parse_type_fracdigits(ctx, in, &type->fraction_digits, &type->exts));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002025 type->flags |= LYS_SET_FRDIGITS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002026 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002027 case LY_STMT_LENGTH:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002028 if (type->length) {
David Sedlákb3077192019-06-19 10:55:37 +02002029 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002030 return LY_EVALID;
2031 }
2032 type->length = calloc(1, sizeof *type->length);
Radek Krejci44ceedc2018-10-02 15:54:31 +02002033 LY_CHECK_ERR_RET(!type->length, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002034
Michal Vasko63f3d842020-07-08 10:10:14 +02002035 LY_CHECK_RET(parse_restr(ctx, in, kw, type->length));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002036 type->flags |= LYS_SET_LENGTH;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002037 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002038 case LY_STMT_PATH:
Michal Vasko004d3152020-06-11 19:59:22 +02002039 if (type->path) {
2040 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, lyext_substmt2str(LYEXT_SUBSTMT_PATH));
2041 return LY_EVALID;
2042 }
2043
Michal Vasko63f3d842020-07-08 10:10:14 +02002044 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_PATH, 0, &str_path, Y_STR_ARG, &type->exts));
Michal Vasko6b26e742020-07-17 15:02:10 +02002045 ret = ly_path_parse(ctx->ctx, NULL, str_path, 0, LY_PATH_BEGIN_EITHER, LY_PATH_LREF_TRUE,
Michal Vasko004d3152020-06-11 19:59:22 +02002046 LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_LEAFREF, &type->path);
2047 lydict_remove(ctx->ctx, str_path);
2048 LY_CHECK_RET(ret);
Radek Krejcid505e3d2018-11-13 09:04:17 +01002049 type->flags |= LYS_SET_PATH;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002050 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002051 case LY_STMT_PATTERN:
Michal Vasko63f3d842020-07-08 10:10:14 +02002052 LY_CHECK_RET(parse_type_pattern(ctx, in, &type->patterns));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002053 type->flags |= LYS_SET_PATTERN;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002054 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002055 case LY_STMT_RANGE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002056 if (type->range) {
David Sedlákb3077192019-06-19 10:55:37 +02002057 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002058 return LY_EVALID;
2059 }
2060 type->range = calloc(1, sizeof *type->range);
David Sedlák7a8b2472019-07-11 15:08:34 +02002061 LY_CHECK_ERR_RET(!type->range, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002062
Michal Vasko63f3d842020-07-08 10:10:14 +02002063 LY_CHECK_RET(parse_restr(ctx, in, kw, type->range));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002064 type->flags |= LYS_SET_RANGE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002065 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002066 case LY_STMT_REQUIRE_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002067 LY_CHECK_RET(parse_type_reqinstance(ctx, in, &type->require_instance, &type->flags, &type->exts));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002068 /* LYS_SET_REQINST checked and set inside parse_type_reqinstance() */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002069 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002070 case LY_STMT_TYPE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002071 LY_ARRAY_NEW_RET(ctx->ctx, type->types, nest_type, LY_EMEM);
Michal Vasko63f3d842020-07-08 10:10:14 +02002072 LY_CHECK_RET(parse_type(ctx, in, nest_type));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002073 type->flags |= LYS_SET_TYPE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002074 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002075 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002076 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &type->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002077 break;
2078 default:
David Sedlákb3077192019-06-19 10:55:37 +02002079 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "type");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002080 return LY_EVALID;
2081 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002082 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002083 return ret;
2084}
2085
Michal Vaskoea5abea2018-09-18 13:10:54 +02002086/**
2087 * @brief Parse the leaf statement.
2088 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002089 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02002090 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002091 * @param[in,out] siblings Siblings to add to.
2092 *
2093 * @return LY_ERR values.
2094 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002095LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02002096parse_leaf(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002097{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002098 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002099 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002100 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002101 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002102 struct lysp_node_leaf *leaf;
2103
David Sedlák60adc092019-08-06 15:57:02 +02002104 /* create new leaf structure */
David Sedlákb9eeb9c2019-09-13 11:17:19 +02002105 LY_LIST_NEW_RET(ctx->ctx, siblings, leaf, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002106 leaf->nodetype = LYS_LEAF;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002107 leaf->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002108
Michal Vasko7fbc8162018-09-17 10:35:16 +02002109 /* get name */
Michal Vasko63f3d842020-07-08 10:10:14 +02002110 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02002111 INSERT_WORD(ctx, buf, leaf->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002112
2113 /* parse substatements */
Michal Vasko63f3d842020-07-08 10:10:14 +02002114 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002115 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002116 case LY_STMT_CONFIG:
Michal Vasko63f3d842020-07-08 10:10:14 +02002117 LY_CHECK_RET(parse_config(ctx, in, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002118 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002119 case LY_STMT_DEFAULT:
Michal Vasko63f3d842020-07-08 10:10:14 +02002120 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_DEFAULT, 0, &leaf->dflt, Y_STR_ARG, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002121 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002122 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02002123 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_DESCRIPTION, 0, &leaf->dsc, Y_STR_ARG, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002124 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002125 case LY_STMT_IF_FEATURE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002126 LY_CHECK_RET(parse_text_fields(ctx, in, LYEXT_SUBSTMT_IFFEATURE, &leaf->iffeatures, Y_STR_ARG, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002127 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002128 case LY_STMT_MANDATORY:
Michal Vasko63f3d842020-07-08 10:10:14 +02002129 LY_CHECK_RET(parse_mandatory(ctx, in, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002130 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002131 case LY_STMT_MUST:
Michal Vasko63f3d842020-07-08 10:10:14 +02002132 LY_CHECK_RET(parse_restrs(ctx, in, kw, &leaf->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002133 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002134 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002135 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_REFERENCE, 0, &leaf->ref, Y_STR_ARG, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002136 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002137 case LY_STMT_STATUS:
Michal Vasko63f3d842020-07-08 10:10:14 +02002138 LY_CHECK_RET(parse_status(ctx, in, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002139 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002140 case LY_STMT_TYPE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002141 LY_CHECK_RET(parse_type(ctx, in, &leaf->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002142 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002143 case LY_STMT_UNITS:
Michal Vasko63f3d842020-07-08 10:10:14 +02002144 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_UNITS, 0, &leaf->units, Y_STR_ARG, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002145 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002146 case LY_STMT_WHEN:
Michal Vasko63f3d842020-07-08 10:10:14 +02002147 LY_CHECK_RET(parse_when(ctx, in, &leaf->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002148 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002149 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002150 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002151 break;
2152 default:
David Sedlákb3077192019-06-19 10:55:37 +02002153 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "leaf");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002154 return LY_EVALID;
2155 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002156 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002157 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01002158checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002159 /* mandatory substatements */
2160 if (!leaf->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002161 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "leaf");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002162 return LY_EVALID;
2163 }
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01002164 if ((leaf->flags & LYS_MAND_TRUE) && (leaf->dflt)) {
David Sedlákb3077192019-06-19 10:55:37 +02002165 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMSCOMB, "mandatory", "default", "leaf");
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01002166 return LY_EVALID;
2167 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002168
2169 return ret;
2170}
2171
Michal Vaskoea5abea2018-09-18 13:10:54 +02002172/**
2173 * @brief Parse the max-elements statement.
2174 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002175 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02002176 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002177 * @param[in,out] max Value to write to.
2178 * @param[in,out] flags Flags to write to.
2179 * @param[in,out] exts Extension instances to add to.
2180 *
2181 * @return LY_ERR values.
2182 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002183LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02002184parse_maxelements(struct lys_yang_parser_ctx *ctx, struct ly_in *in, uint32_t *max, uint16_t *flags,
Radek Krejci0f969882020-08-21 16:56:47 +02002185 struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002186{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002187 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002188 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02002189 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002190 unsigned long int num;
Radek Krejcid6b76452019-09-03 17:03:03 +02002191 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002192
2193 if (*flags & LYS_SET_MAX) {
David Sedlákb3077192019-06-19 10:55:37 +02002194 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002195 return LY_EVALID;
2196 }
2197 *flags |= LYS_SET_MAX;
2198
2199 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02002200 LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002201
2202 if (!word_len || (word[0] == '0') || ((word[0] != 'u') && !isdigit(word[0]))) {
David Sedlákb3077192019-06-19 10:55:37 +02002203 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002204 free(buf);
2205 return LY_EVALID;
2206 }
2207
Radek Krejci7f9b6512019-09-18 13:11:09 +02002208 if (ly_strncmp("unbounded", word, word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002209 errno = 0;
2210 num = strtoul(word, &ptr, 10);
2211 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02002212 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02002213 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002214 free(buf);
2215 return LY_EVALID;
2216 }
2217 if ((errno == ERANGE) || (num > UINT32_MAX)) {
David Sedlákb3077192019-06-19 10:55:37 +02002218 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002219 free(buf);
2220 return LY_EVALID;
2221 }
2222
2223 *max = num;
2224 }
2225 free(buf);
2226
Michal Vaskod989ba02020-08-24 10:59:24 +02002227 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002228 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002229 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002230 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_MAX, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002231 break;
2232 default:
David Sedlákb3077192019-06-19 10:55:37 +02002233 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002234 return LY_EVALID;
2235 }
2236 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002237 return ret;
2238}
2239
Michal Vaskoea5abea2018-09-18 13:10:54 +02002240/**
2241 * @brief Parse the min-elements statement.
2242 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002243 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02002244 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002245 * @param[in,out] min Value to write to.
2246 * @param[in,out] flags Flags to write to.
2247 * @param[in,out] exts Extension instances to add to.
2248 *
2249 * @return LY_ERR values.
2250 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002251LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02002252parse_minelements(struct lys_yang_parser_ctx *ctx, struct ly_in *in, uint32_t *min, uint16_t *flags,
Radek Krejci0f969882020-08-21 16:56:47 +02002253 struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002254{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002255 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002256 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02002257 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002258 unsigned long int num;
Radek Krejcid6b76452019-09-03 17:03:03 +02002259 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002260
2261 if (*flags & LYS_SET_MIN) {
David Sedlákb3077192019-06-19 10:55:37 +02002262 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002263 return LY_EVALID;
2264 }
2265 *flags |= LYS_SET_MIN;
2266
2267 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02002268 LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002269
2270 if (!word_len || !isdigit(word[0]) || ((word[0] == '0') && (word_len > 1))) {
David Sedlákb3077192019-06-19 10:55:37 +02002271 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002272 free(buf);
2273 return LY_EVALID;
2274 }
2275
2276 errno = 0;
2277 num = strtoul(word, &ptr, 10);
2278 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02002279 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02002280 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002281 free(buf);
2282 return LY_EVALID;
2283 }
2284 if ((errno == ERANGE) || (num > UINT32_MAX)) {
David Sedlákb3077192019-06-19 10:55:37 +02002285 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002286 free(buf);
2287 return LY_EVALID;
2288 }
2289 *min = num;
2290 free(buf);
2291
Michal Vaskod989ba02020-08-24 10:59:24 +02002292 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002293 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002294 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002295 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_MIN, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002296 break;
2297 default:
David Sedlákb3077192019-06-19 10:55:37 +02002298 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002299 return LY_EVALID;
2300 }
2301 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002302 return ret;
2303}
2304
Michal Vaskoea5abea2018-09-18 13:10:54 +02002305/**
2306 * @brief Parse the ordered-by statement.
2307 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002308 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02002309 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002310 * @param[in,out] flags Flags to write to.
2311 * @param[in,out] exts Extension instances to add to.
2312 *
2313 * @return LY_ERR values.
2314 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002315static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02002316parse_orderedby(struct lys_yang_parser_ctx *ctx, struct ly_in *in, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002317{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002318 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002319 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002320 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002321 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002322
2323 if (*flags & LYS_ORDBY_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02002324 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002325 return LY_EVALID;
2326 }
2327
2328 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02002329 LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002330
2331 if ((word_len == 6) && !strncmp(word, "system", word_len)) {
2332 *flags |= LYS_ORDBY_SYSTEM;
2333 } else if ((word_len == 4) && !strncmp(word, "user", word_len)) {
2334 *flags |= LYS_ORDBY_USER;
2335 } else {
David Sedlákb3077192019-06-19 10:55:37 +02002336 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002337 free(buf);
2338 return LY_EVALID;
2339 }
2340 free(buf);
2341
Michal Vaskod989ba02020-08-24 10:59:24 +02002342 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002343 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002344 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002345 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_ORDEREDBY, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002346 break;
2347 default:
David Sedlákb3077192019-06-19 10:55:37 +02002348 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002349 return LY_EVALID;
2350 }
2351 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002352 return ret;
2353}
2354
Michal Vaskoea5abea2018-09-18 13:10:54 +02002355/**
2356 * @brief Parse the leaf-list statement.
2357 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002358 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02002359 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002360 * @param[in,out] siblings Siblings to add to.
2361 *
2362 * @return LY_ERR values.
2363 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002364LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02002365parse_leaflist(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002366{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002367 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002368 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002369 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002370 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002371 struct lysp_node_leaflist *llist;
2372
David Sedlák60adc092019-08-06 15:57:02 +02002373 /* create new leaf-list structure */
David Sedlákb9eeb9c2019-09-13 11:17:19 +02002374 LY_LIST_NEW_RET(ctx->ctx, siblings, llist, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002375 llist->nodetype = LYS_LEAFLIST;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002376 llist->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002377
Michal Vasko7fbc8162018-09-17 10:35:16 +02002378 /* get name */
Michal Vasko63f3d842020-07-08 10:10:14 +02002379 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02002380 INSERT_WORD(ctx, buf, llist->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002381
2382 /* parse substatements */
Michal Vasko63f3d842020-07-08 10:10:14 +02002383 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002384 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002385 case LY_STMT_CONFIG:
Michal Vasko63f3d842020-07-08 10:10:14 +02002386 LY_CHECK_RET(parse_config(ctx, in, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002387 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002388 case LY_STMT_DEFAULT:
Radek Krejci335332a2019-09-05 13:03:35 +02002389 PARSER_CHECK_STMTVER2_RET(ctx, "default", "leaf-list");
Michal Vasko63f3d842020-07-08 10:10:14 +02002390 LY_CHECK_RET(parse_text_fields(ctx, in, LYEXT_SUBSTMT_DEFAULT, &llist->dflts, Y_STR_ARG, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002391 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002392 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02002393 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_DESCRIPTION, 0, &llist->dsc, Y_STR_ARG, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002394 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002395 case LY_STMT_IF_FEATURE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002396 LY_CHECK_RET(parse_text_fields(ctx, in, LYEXT_SUBSTMT_IFFEATURE, &llist->iffeatures, Y_STR_ARG, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002397 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002398 case LY_STMT_MAX_ELEMENTS:
Michal Vasko63f3d842020-07-08 10:10:14 +02002399 LY_CHECK_RET(parse_maxelements(ctx, in, &llist->max, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002400 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002401 case LY_STMT_MIN_ELEMENTS:
Michal Vasko63f3d842020-07-08 10:10:14 +02002402 LY_CHECK_RET(parse_minelements(ctx, in, &llist->min, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002403 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002404 case LY_STMT_MUST:
Michal Vasko63f3d842020-07-08 10:10:14 +02002405 LY_CHECK_RET(parse_restrs(ctx, in, kw, &llist->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002406 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002407 case LY_STMT_ORDERED_BY:
Michal Vasko63f3d842020-07-08 10:10:14 +02002408 LY_CHECK_RET(parse_orderedby(ctx, in, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002409 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002410 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002411 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_REFERENCE, 0, &llist->ref, Y_STR_ARG, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002412 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002413 case LY_STMT_STATUS:
Michal Vasko63f3d842020-07-08 10:10:14 +02002414 LY_CHECK_RET(parse_status(ctx, in, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002415 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002416 case LY_STMT_TYPE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002417 LY_CHECK_RET(parse_type(ctx, in, &llist->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002418 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002419 case LY_STMT_UNITS:
Michal Vasko63f3d842020-07-08 10:10:14 +02002420 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_UNITS, 0, &llist->units, Y_STR_ARG, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002421 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002422 case LY_STMT_WHEN:
Michal Vasko63f3d842020-07-08 10:10:14 +02002423 LY_CHECK_RET(parse_when(ctx, in, &llist->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002424 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002425 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002426 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002427 break;
2428 default:
David Sedlákb3077192019-06-19 10:55:37 +02002429 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "llist");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002430 return LY_EVALID;
2431 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002432 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002433 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01002434checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002435 /* mandatory substatements */
2436 if (!llist->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002437 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "leaf-list");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002438 return LY_EVALID;
2439 }
Radek Krejci0e5d8382018-11-28 16:37:53 +01002440 if ((llist->min) && (llist->dflts)) {
David Sedlákb3077192019-06-19 10:55:37 +02002441 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMSCOMB, "min-elements", "default", "leaf-list");
Radek Krejci0e5d8382018-11-28 16:37:53 +01002442 return LY_EVALID;
2443 }
Radek Krejcidf6cad12018-11-28 17:10:55 +01002444 if (llist->max && llist->min > llist->max) {
David Sedlákb3077192019-06-19 10:55:37 +02002445 LOGVAL_PARSER(ctx, LYVE_SEMANTICS,
Radek Krejcidf6cad12018-11-28 17:10:55 +01002446 "Invalid combination of min-elements and max-elements: min value %u is bigger than the max value %u.",
2447 llist->min, llist->max);
2448 return LY_EVALID;
2449 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002450
2451 return ret;
2452}
2453
Michal Vaskoea5abea2018-09-18 13:10:54 +02002454/**
2455 * @brief Parse the refine statement.
2456 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002457 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02002458 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002459 * @param[in,out] refines Refines to add to.
2460 *
2461 * @return LY_ERR values.
2462 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002463static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02002464parse_refine(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_refine **refines)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002465{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002466 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002467 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002468 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002469 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002470 struct lysp_refine *rf;
2471
Radek Krejci2c4e7172018-10-19 15:56:26 +02002472 LY_ARRAY_NEW_RET(ctx->ctx, *refines, rf, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002473
2474 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02002475 LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vaskob36053d2020-03-26 15:49:30 +01002476 CHECK_NONEMPTY(ctx, word_len, "refine");
Radek Krejci44ceedc2018-10-02 15:54:31 +02002477 INSERT_WORD(ctx, buf, rf->nodeid, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002478
Michal Vaskod989ba02020-08-24 10:59:24 +02002479 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002480 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002481 case LY_STMT_CONFIG:
Michal Vasko63f3d842020-07-08 10:10:14 +02002482 LY_CHECK_RET(parse_config(ctx, in, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002483 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002484 case LY_STMT_DEFAULT:
Michal Vasko63f3d842020-07-08 10:10:14 +02002485 LY_CHECK_RET(parse_text_fields(ctx, in, LYEXT_SUBSTMT_DEFAULT, &rf->dflts, Y_STR_ARG, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002486 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002487 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02002488 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_DESCRIPTION, 0, &rf->dsc, Y_STR_ARG, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002489 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002490 case LY_STMT_IF_FEATURE:
Radek Krejci335332a2019-09-05 13:03:35 +02002491 PARSER_CHECK_STMTVER2_RET(ctx, "if-feature", "refine");
Michal Vasko63f3d842020-07-08 10:10:14 +02002492 LY_CHECK_RET(parse_text_fields(ctx, in, LYEXT_SUBSTMT_IFFEATURE, &rf->iffeatures, Y_STR_ARG, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002493 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002494 case LY_STMT_MAX_ELEMENTS:
Michal Vasko63f3d842020-07-08 10:10:14 +02002495 LY_CHECK_RET(parse_maxelements(ctx, in, &rf->max, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002496 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002497 case LY_STMT_MIN_ELEMENTS:
Michal Vasko63f3d842020-07-08 10:10:14 +02002498 LY_CHECK_RET(parse_minelements(ctx, in, &rf->min, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002499 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002500 case LY_STMT_MUST:
Michal Vasko63f3d842020-07-08 10:10:14 +02002501 LY_CHECK_RET(parse_restrs(ctx, in, kw, &rf->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002502 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002503 case LY_STMT_MANDATORY:
Michal Vasko63f3d842020-07-08 10:10:14 +02002504 LY_CHECK_RET(parse_mandatory(ctx, in, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002505 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002506 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002507 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_REFERENCE, 0, &rf->ref, Y_STR_ARG, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002508 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002509 case LY_STMT_PRESENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002510 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_PRESENCE, 0, &rf->presence, Y_STR_ARG, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002511 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002512 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002513 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002514 break;
2515 default:
David Sedlákb3077192019-06-19 10:55:37 +02002516 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "refine");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002517 return LY_EVALID;
2518 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002519 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002520 return ret;
2521}
2522
Michal Vaskoea5abea2018-09-18 13:10:54 +02002523/**
2524 * @brief Parse the typedef statement.
2525 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002526 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02002527 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002528 * @param[in,out] typedefs Typedefs to add to.
2529 *
2530 * @return LY_ERR values.
2531 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002532static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02002533parse_typedef(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct ly_in *in, struct lysp_tpdf **typedefs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002534{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002535 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002536 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002537 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002538 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002539 struct lysp_tpdf *tpdf;
2540
Radek Krejci2c4e7172018-10-19 15:56:26 +02002541 LY_ARRAY_NEW_RET(ctx->ctx, *typedefs, tpdf, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002542
2543 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02002544 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02002545 INSERT_WORD(ctx, buf, tpdf->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002546
2547 /* parse substatements */
Michal Vasko63f3d842020-07-08 10:10:14 +02002548 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002549 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002550 case LY_STMT_DEFAULT:
Michal Vasko63f3d842020-07-08 10:10:14 +02002551 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_DEFAULT, 0, &tpdf->dflt, Y_STR_ARG, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002552 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002553 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02002554 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_DESCRIPTION, 0, &tpdf->dsc, Y_STR_ARG, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002555 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002556 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002557 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_REFERENCE, 0, &tpdf->ref, Y_STR_ARG, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002558 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002559 case LY_STMT_STATUS:
Michal Vasko63f3d842020-07-08 10:10:14 +02002560 LY_CHECK_RET(parse_status(ctx, in, &tpdf->flags, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002561 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002562 case LY_STMT_TYPE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002563 LY_CHECK_RET(parse_type(ctx, in, &tpdf->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002564 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002565 case LY_STMT_UNITS:
Michal Vasko63f3d842020-07-08 10:10:14 +02002566 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_UNITS, 0, &tpdf->units, Y_STR_ARG, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002567 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002568 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002569 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002570 break;
2571 default:
David Sedlákb3077192019-06-19 10:55:37 +02002572 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "typedef");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002573 return LY_EVALID;
2574 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002575 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002576 LY_CHECK_RET(ret);
Radek Krejcibbe09a92018-11-08 09:36:54 +01002577checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002578 /* mandatory substatements */
2579 if (!tpdf->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002580 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "typedef");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002581 return LY_EVALID;
2582 }
2583
Radek Krejcibbe09a92018-11-08 09:36:54 +01002584 /* store data for collision check */
Michal Vasko1bf09392020-03-27 12:38:10 +01002585 if (parent && !(parent->nodetype & (LYS_GROUPING | LYS_RPC | LYS_ACTION | LYS_INOUT | LYS_NOTIF))) {
Radek Krejciba03a5a2020-08-27 14:40:41 +02002586 LY_CHECK_RET(ly_set_add(&ctx->tpdfs_nodes, parent, 0, NULL));
Radek Krejcibbe09a92018-11-08 09:36:54 +01002587 }
2588
Michal Vasko7fbc8162018-09-17 10:35:16 +02002589 return ret;
2590}
2591
Michal Vaskoea5abea2018-09-18 13:10:54 +02002592/**
2593 * @brief Parse the input or output statement.
2594 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002595 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02002596 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002597 * @param[in] kw Type of this particular keyword
2598 * @param[in,out] inout_p Input/output pointer to write to.
2599 *
2600 * @return LY_ERR values.
2601 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002602static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02002603parse_inout(struct lys_yang_parser_ctx *ctx, struct ly_in *in, enum ly_stmt inout_kw, struct lysp_node *parent, struct lysp_action_inout *inout_p)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002604{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002605 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002606 char *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002607 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002608 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002609
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002610 if (inout_p->nodetype) {
David Sedlákb3077192019-06-19 10:55:37 +02002611 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(inout_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002612 return LY_EVALID;
2613 }
2614
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002615 /* initiate structure */
Michal Vasko22df3f02020-08-24 13:29:22 +02002616 inout_p->nodetype = &((struct lysp_action *)parent)->input == inout_p ? LYS_INPUT : LYS_OUTPUT;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002617 inout_p->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002618
2619 /* parse substatements */
Michal Vasko63f3d842020-07-08 10:10:14 +02002620 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002621 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002622 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02002623 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", ly_stmt2str(inout_kw));
Radek Krejci0f969882020-08-21 16:56:47 +02002624 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02002625 case LY_STMT_ANYXML:
Michal Vasko22df3f02020-08-24 13:29:22 +02002626 LY_CHECK_RET(parse_any(ctx, in, kw, (struct lysp_node *)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002627 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002628 case LY_STMT_CHOICE:
Michal Vasko22df3f02020-08-24 13:29:22 +02002629 LY_CHECK_RET(parse_choice(ctx, in, (struct lysp_node *)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002630 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002631 case LY_STMT_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02002632 LY_CHECK_RET(parse_container(ctx, in, (struct lysp_node *)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002633 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002634 case LY_STMT_LEAF:
Michal Vasko22df3f02020-08-24 13:29:22 +02002635 LY_CHECK_RET(parse_leaf(ctx, in, (struct lysp_node *)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002636 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002637 case LY_STMT_LEAF_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02002638 LY_CHECK_RET(parse_leaflist(ctx, in, (struct lysp_node *)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002639 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002640 case LY_STMT_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02002641 LY_CHECK_RET(parse_list(ctx, in, (struct lysp_node *)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002642 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002643 case LY_STMT_USES:
Michal Vasko22df3f02020-08-24 13:29:22 +02002644 LY_CHECK_RET(parse_uses(ctx, in, (struct lysp_node *)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002645 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002646 case LY_STMT_TYPEDEF:
Michal Vasko22df3f02020-08-24 13:29:22 +02002647 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)inout_p, in, &inout_p->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002648 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002649 case LY_STMT_MUST:
Radek Krejci335332a2019-09-05 13:03:35 +02002650 PARSER_CHECK_STMTVER2_RET(ctx, "must", ly_stmt2str(inout_kw));
Michal Vasko63f3d842020-07-08 10:10:14 +02002651 LY_CHECK_RET(parse_restrs(ctx, in, kw, &inout_p->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002652 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002653 case LY_STMT_GROUPING:
Michal Vasko22df3f02020-08-24 13:29:22 +02002654 LY_CHECK_RET(parse_grouping(ctx, in, (struct lysp_node *)inout_p, &inout_p->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002655 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002656 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002657 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &inout_p->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002658 break;
2659 default:
David Sedlákb3077192019-06-19 10:55:37 +02002660 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(inout_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002661 return LY_EVALID;
2662 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002663 }
Radek Krejci7fc68292019-06-12 13:51:09 +02002664 LY_CHECK_RET(ret);
Michal Vaskob83af8a2020-01-06 09:49:22 +01002665
Radek Krejci7fc68292019-06-12 13:51:09 +02002666checks:
Radek Krejcie86bf772018-12-14 11:39:53 +01002667 /* finalize parent pointers to the reallocated items */
Michal Vaskob36053d2020-03-26 15:49:30 +01002668 LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, inout_p->groupings, NULL, NULL, NULL));
Radek Krejci7fc68292019-06-12 13:51:09 +02002669
Michal Vaskob83af8a2020-01-06 09:49:22 +01002670 if (!inout_p->data) {
2671 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "data-def-stmt", ly_stmt2str(inout_kw));
2672 return LY_EVALID;
2673 }
2674
Michal Vasko7fbc8162018-09-17 10:35:16 +02002675 return ret;
2676}
2677
Michal Vaskoea5abea2018-09-18 13:10:54 +02002678/**
2679 * @brief Parse the action statement.
2680 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002681 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02002682 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002683 * @param[in,out] actions Actions to add to.
2684 *
2685 * @return LY_ERR values.
2686 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002687LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02002688parse_action(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_node *parent, struct lysp_action **actions)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002689{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002690 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002691 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002692 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002693 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002694 struct lysp_action *act;
2695
Radek Krejci2c4e7172018-10-19 15:56:26 +02002696 LY_ARRAY_NEW_RET(ctx->ctx, *actions, act, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002697
2698 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02002699 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02002700 INSERT_WORD(ctx, buf, act->name, word, word_len);
Michal Vasko1bf09392020-03-27 12:38:10 +01002701 act->nodetype = parent ? LYS_ACTION : LYS_RPC;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002702 act->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002703
Michal Vasko63f3d842020-07-08 10:10:14 +02002704 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002705 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002706 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02002707 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_DESCRIPTION, 0, &act->dsc, Y_STR_ARG, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002708 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002709 case LY_STMT_IF_FEATURE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002710 LY_CHECK_RET(parse_text_fields(ctx, in, LYEXT_SUBSTMT_IFFEATURE, &act->iffeatures, Y_STR_ARG, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002711 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002712 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002713 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_REFERENCE, 0, &act->ref, Y_STR_ARG, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002714 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002715 case LY_STMT_STATUS:
Michal Vasko63f3d842020-07-08 10:10:14 +02002716 LY_CHECK_RET(parse_status(ctx, in, &act->flags, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002717 break;
2718
Radek Krejcid6b76452019-09-03 17:03:03 +02002719 case LY_STMT_INPUT:
Michal Vasko22df3f02020-08-24 13:29:22 +02002720 LY_CHECK_RET(parse_inout(ctx, in, kw, (struct lysp_node *)act, &act->input));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002721 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002722 case LY_STMT_OUTPUT:
Michal Vasko22df3f02020-08-24 13:29:22 +02002723 LY_CHECK_RET(parse_inout(ctx, in, kw, (struct lysp_node *)act, &act->output));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002724 break;
2725
Radek Krejcid6b76452019-09-03 17:03:03 +02002726 case LY_STMT_TYPEDEF:
Michal Vasko22df3f02020-08-24 13:29:22 +02002727 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)act, in, &act->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002728 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002729 case LY_STMT_GROUPING:
Michal Vasko22df3f02020-08-24 13:29:22 +02002730 LY_CHECK_RET(parse_grouping(ctx, in, (struct lysp_node *)act, &act->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002731 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002732 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002733 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002734 break;
2735 default:
David Sedlákb3077192019-06-19 10:55:37 +02002736 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), parent ? "action" : "rpc");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002737 return LY_EVALID;
2738 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002739 }
Radek Krejci7fc68292019-06-12 13:51:09 +02002740 LY_CHECK_RET(ret);
2741checks:
Radek Krejcie86bf772018-12-14 11:39:53 +01002742 /* finalize parent pointers to the reallocated items */
Michal Vaskob36053d2020-03-26 15:49:30 +01002743 LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, act->groupings, NULL, NULL, NULL));
Radek Krejci7fc68292019-06-12 13:51:09 +02002744
Michal Vasko7fbc8162018-09-17 10:35:16 +02002745 return ret;
2746}
2747
Michal Vaskoea5abea2018-09-18 13:10:54 +02002748/**
2749 * @brief Parse the notification statement.
2750 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002751 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02002752 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002753 * @param[in,out] notifs Notifications to add to.
2754 *
2755 * @return LY_ERR values.
2756 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002757LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02002758parse_notif(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_node *parent, struct lysp_notif **notifs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002759{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002760 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002761 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002762 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002763 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002764 struct lysp_notif *notif;
2765
Radek Krejci2c4e7172018-10-19 15:56:26 +02002766 LY_ARRAY_NEW_RET(ctx->ctx, *notifs, notif, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002767
2768 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02002769 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02002770 INSERT_WORD(ctx, buf, notif->name, word, word_len);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002771 notif->nodetype = LYS_NOTIF;
2772 notif->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002773
Michal Vasko63f3d842020-07-08 10:10:14 +02002774 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002775 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002776 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02002777 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_DESCRIPTION, 0, &notif->dsc, Y_STR_ARG, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002778 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002779 case LY_STMT_IF_FEATURE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002780 LY_CHECK_RET(parse_text_fields(ctx, in, LYEXT_SUBSTMT_IFFEATURE, &notif->iffeatures, Y_STR_ARG, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002781 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002782 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002783 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_REFERENCE, 0, &notif->ref, Y_STR_ARG, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002784 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002785 case LY_STMT_STATUS:
Michal Vasko63f3d842020-07-08 10:10:14 +02002786 LY_CHECK_RET(parse_status(ctx, in, &notif->flags, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002787 break;
2788
Radek Krejcid6b76452019-09-03 17:03:03 +02002789 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02002790 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "notification");
Radek Krejci0f969882020-08-21 16:56:47 +02002791 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02002792 case LY_STMT_ANYXML:
Michal Vasko22df3f02020-08-24 13:29:22 +02002793 LY_CHECK_RET(parse_any(ctx, in, kw, (struct lysp_node *)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002794 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002795 case LY_STMT_CHOICE:
Michal Vasko22df3f02020-08-24 13:29:22 +02002796 LY_CHECK_RET(parse_case(ctx, in, (struct lysp_node *)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002797 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002798 case LY_STMT_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02002799 LY_CHECK_RET(parse_container(ctx, in, (struct lysp_node *)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002800 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002801 case LY_STMT_LEAF:
Michal Vasko22df3f02020-08-24 13:29:22 +02002802 LY_CHECK_RET(parse_leaf(ctx, in, (struct lysp_node *)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002803 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002804 case LY_STMT_LEAF_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02002805 LY_CHECK_RET(parse_leaflist(ctx, in, (struct lysp_node *)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002806 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002807 case LY_STMT_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02002808 LY_CHECK_RET(parse_list(ctx, in, (struct lysp_node *)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002809 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002810 case LY_STMT_USES:
Michal Vasko22df3f02020-08-24 13:29:22 +02002811 LY_CHECK_RET(parse_uses(ctx, in, (struct lysp_node *)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002812 break;
2813
Radek Krejcid6b76452019-09-03 17:03:03 +02002814 case LY_STMT_MUST:
Radek Krejci335332a2019-09-05 13:03:35 +02002815 PARSER_CHECK_STMTVER2_RET(ctx, "must", "notification");
Michal Vasko63f3d842020-07-08 10:10:14 +02002816 LY_CHECK_RET(parse_restrs(ctx, in, kw, &notif->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002817 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002818 case LY_STMT_TYPEDEF:
Michal Vasko22df3f02020-08-24 13:29:22 +02002819 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)notif, in, &notif->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002820 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002821 case LY_STMT_GROUPING:
Michal Vasko22df3f02020-08-24 13:29:22 +02002822 LY_CHECK_RET(parse_grouping(ctx, in, (struct lysp_node *)notif, &notif->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002823 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002824 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002825 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002826 break;
2827 default:
David Sedlákb3077192019-06-19 10:55:37 +02002828 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "notification");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002829 return LY_EVALID;
2830 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002831 }
Radek Krejci7fc68292019-06-12 13:51:09 +02002832 LY_CHECK_RET(ret);
2833checks:
Radek Krejcie86bf772018-12-14 11:39:53 +01002834 /* finalize parent pointers to the reallocated items */
Michal Vaskob36053d2020-03-26 15:49:30 +01002835 LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, notif->groupings, NULL, NULL, NULL));
Radek Krejci7fc68292019-06-12 13:51:09 +02002836
Michal Vasko7fbc8162018-09-17 10:35:16 +02002837 return ret;
2838}
2839
Michal Vaskoea5abea2018-09-18 13:10:54 +02002840/**
2841 * @brief Parse the grouping statement.
2842 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002843 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02002844 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002845 * @param[in,out] groupings Groupings to add to.
2846 *
2847 * @return LY_ERR values.
2848 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002849LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02002850parse_grouping(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_node *parent, struct lysp_grp **groupings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002851{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002852 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002853 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002854 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002855 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002856 struct lysp_grp *grp;
2857
Radek Krejci2c4e7172018-10-19 15:56:26 +02002858 LY_ARRAY_NEW_RET(ctx->ctx, *groupings, grp, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002859
2860 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02002861 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02002862 INSERT_WORD(ctx, buf, grp->name, word, word_len);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002863 grp->nodetype = LYS_GROUPING;
2864 grp->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002865
Michal Vasko63f3d842020-07-08 10:10:14 +02002866 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002867 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002868 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02002869 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_DESCRIPTION, 0, &grp->dsc, Y_STR_ARG, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002870 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002871 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002872 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_REFERENCE, 0, &grp->ref, Y_STR_ARG, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002873 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002874 case LY_STMT_STATUS:
Michal Vasko63f3d842020-07-08 10:10:14 +02002875 LY_CHECK_RET(parse_status(ctx, in, &grp->flags, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002876 break;
2877
Radek Krejcid6b76452019-09-03 17:03:03 +02002878 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02002879 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "grouping");
Radek Krejci0f969882020-08-21 16:56:47 +02002880 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02002881 case LY_STMT_ANYXML:
Michal Vasko22df3f02020-08-24 13:29:22 +02002882 LY_CHECK_RET(parse_any(ctx, in, kw, (struct lysp_node *)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002883 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002884 case LY_STMT_CHOICE:
Michal Vasko22df3f02020-08-24 13:29:22 +02002885 LY_CHECK_RET(parse_choice(ctx, in, (struct lysp_node *)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002886 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002887 case LY_STMT_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02002888 LY_CHECK_RET(parse_container(ctx, in, (struct lysp_node *)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002889 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002890 case LY_STMT_LEAF:
Michal Vasko22df3f02020-08-24 13:29:22 +02002891 LY_CHECK_RET(parse_leaf(ctx, in, (struct lysp_node *)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002892 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002893 case LY_STMT_LEAF_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02002894 LY_CHECK_RET(parse_leaflist(ctx, in, (struct lysp_node *)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002895 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002896 case LY_STMT_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02002897 LY_CHECK_RET(parse_list(ctx, in, (struct lysp_node *)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002898 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002899 case LY_STMT_USES:
Michal Vasko22df3f02020-08-24 13:29:22 +02002900 LY_CHECK_RET(parse_uses(ctx, in, (struct lysp_node *)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002901 break;
2902
Radek Krejcid6b76452019-09-03 17:03:03 +02002903 case LY_STMT_TYPEDEF:
Michal Vasko22df3f02020-08-24 13:29:22 +02002904 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)grp, in, &grp->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002905 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002906 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02002907 PARSER_CHECK_STMTVER2_RET(ctx, "action", "grouping");
Michal Vasko22df3f02020-08-24 13:29:22 +02002908 LY_CHECK_RET(parse_action(ctx, in, (struct lysp_node *)grp, &grp->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002909 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002910 case LY_STMT_GROUPING:
Michal Vasko22df3f02020-08-24 13:29:22 +02002911 LY_CHECK_RET(parse_grouping(ctx, in, (struct lysp_node *)grp, &grp->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002912 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002913 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02002914 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "grouping");
Michal Vasko22df3f02020-08-24 13:29:22 +02002915 LY_CHECK_RET(parse_notif(ctx, in, (struct lysp_node *)grp, &grp->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002916 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002917 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002918 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002919 break;
2920 default:
David Sedlákb3077192019-06-19 10:55:37 +02002921 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "grouping");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002922 return LY_EVALID;
2923 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002924 }
Radek Krejci7fc68292019-06-12 13:51:09 +02002925 LY_CHECK_RET(ret);
2926checks:
Radek Krejcie86bf772018-12-14 11:39:53 +01002927 /* finalize parent pointers to the reallocated items */
Michal Vaskob36053d2020-03-26 15:49:30 +01002928 LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, grp->groupings, NULL, grp->actions, grp->notifs));
Radek Krejci7fc68292019-06-12 13:51:09 +02002929
Michal Vasko7fbc8162018-09-17 10:35:16 +02002930 return ret;
2931}
2932
Michal Vaskoea5abea2018-09-18 13:10:54 +02002933/**
David Sedlák0d6de5a2019-07-22 13:25:44 +02002934 * @brief Parse the augment statement.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002935 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002936 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02002937 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002938 * @param[in,out] augments Augments to add to.
2939 *
2940 * @return LY_ERR values.
2941 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002942LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02002943parse_augment(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_node *parent, struct lysp_augment **augments)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002944{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002945 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002946 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002947 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002948 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002949 struct lysp_augment *aug;
2950
Radek Krejci2c4e7172018-10-19 15:56:26 +02002951 LY_ARRAY_NEW_RET(ctx->ctx, *augments, aug, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002952
2953 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02002954 LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vaskob36053d2020-03-26 15:49:30 +01002955 CHECK_NONEMPTY(ctx, word_len, "augment");
Radek Krejci44ceedc2018-10-02 15:54:31 +02002956 INSERT_WORD(ctx, buf, aug->nodeid, word, word_len);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002957 aug->nodetype = LYS_AUGMENT;
2958 aug->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002959
Michal Vasko63f3d842020-07-08 10:10:14 +02002960 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002961 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002962 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02002963 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_DESCRIPTION, 0, &aug->dsc, Y_STR_ARG, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002964 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002965 case LY_STMT_IF_FEATURE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002966 LY_CHECK_RET(parse_text_fields(ctx, in, LYEXT_SUBSTMT_IFFEATURE, &aug->iffeatures, Y_STR_ARG, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002967 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002968 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02002969 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_REFERENCE, 0, &aug->ref, Y_STR_ARG, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002970 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002971 case LY_STMT_STATUS:
Michal Vasko63f3d842020-07-08 10:10:14 +02002972 LY_CHECK_RET(parse_status(ctx, in, &aug->flags, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002973 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002974 case LY_STMT_WHEN:
Michal Vasko63f3d842020-07-08 10:10:14 +02002975 LY_CHECK_RET(parse_when(ctx, in, &aug->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002976 break;
2977
Radek Krejcid6b76452019-09-03 17:03:03 +02002978 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02002979 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "augment");
Radek Krejci0f969882020-08-21 16:56:47 +02002980 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02002981 case LY_STMT_ANYXML:
Michal Vasko22df3f02020-08-24 13:29:22 +02002982 LY_CHECK_RET(parse_any(ctx, in, kw, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002983 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002984 case LY_STMT_CASE:
Michal Vasko22df3f02020-08-24 13:29:22 +02002985 LY_CHECK_RET(parse_case(ctx, in, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002986 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002987 case LY_STMT_CHOICE:
Michal Vasko22df3f02020-08-24 13:29:22 +02002988 LY_CHECK_RET(parse_choice(ctx, in, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002989 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002990 case LY_STMT_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02002991 LY_CHECK_RET(parse_container(ctx, in, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002992 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002993 case LY_STMT_LEAF:
Michal Vasko22df3f02020-08-24 13:29:22 +02002994 LY_CHECK_RET(parse_leaf(ctx, in, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002995 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002996 case LY_STMT_LEAF_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02002997 LY_CHECK_RET(parse_leaflist(ctx, in, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002998 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002999 case LY_STMT_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02003000 LY_CHECK_RET(parse_list(ctx, in, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003001 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003002 case LY_STMT_USES:
Michal Vasko22df3f02020-08-24 13:29:22 +02003003 LY_CHECK_RET(parse_uses(ctx, in, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003004 break;
3005
Radek Krejcid6b76452019-09-03 17:03:03 +02003006 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003007 PARSER_CHECK_STMTVER2_RET(ctx, "action", "augment");
Michal Vasko22df3f02020-08-24 13:29:22 +02003008 LY_CHECK_RET(parse_action(ctx, in, (struct lysp_node *)aug, &aug->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003009 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003010 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003011 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "augment");
Michal Vasko22df3f02020-08-24 13:29:22 +02003012 LY_CHECK_RET(parse_notif(ctx, in, (struct lysp_node *)aug, &aug->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003013 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003014 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003015 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003016 break;
3017 default:
David Sedlákb3077192019-06-19 10:55:37 +02003018 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "augment");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003019 return LY_EVALID;
3020 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003021 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003022 LY_CHECK_RET(ret);
3023checks:
3024 /* finalize parent pointers to the reallocated items */
Michal Vaskob36053d2020-03-26 15:49:30 +01003025 LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, NULL, NULL, aug->actions, aug->notifs));
Radek Krejci7fc68292019-06-12 13:51:09 +02003026
Michal Vasko7fbc8162018-09-17 10:35:16 +02003027 return ret;
3028}
3029
Michal Vaskoea5abea2018-09-18 13:10:54 +02003030/**
3031 * @brief Parse the uses statement.
3032 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003033 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02003034 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003035 * @param[in,out] siblings Siblings to add to.
3036 *
3037 * @return LY_ERR values.
3038 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003039LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02003040parse_uses(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003041{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003042 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003043 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003044 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003045 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003046 struct lysp_node_uses *uses;
3047
David Sedlák60adc092019-08-06 15:57:02 +02003048 /* create uses structure */
David Sedlákb9eeb9c2019-09-13 11:17:19 +02003049 LY_LIST_NEW_RET(ctx->ctx, siblings, uses, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003050 uses->nodetype = LYS_USES;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003051 uses->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003052
Michal Vasko7fbc8162018-09-17 10:35:16 +02003053 /* get name */
Michal Vasko63f3d842020-07-08 10:10:14 +02003054 LY_CHECK_RET(get_argument(ctx, in, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003055 INSERT_WORD(ctx, buf, uses->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003056
3057 /* parse substatements */
Michal Vasko63f3d842020-07-08 10:10:14 +02003058 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003059 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003060 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02003061 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_DESCRIPTION, 0, &uses->dsc, Y_STR_ARG, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003062 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003063 case LY_STMT_IF_FEATURE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003064 LY_CHECK_RET(parse_text_fields(ctx, in, LYEXT_SUBSTMT_IFFEATURE, &uses->iffeatures, Y_STR_ARG, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003065 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003066 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003067 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_REFERENCE, 0, &uses->ref, Y_STR_ARG, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003068 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003069 case LY_STMT_STATUS:
Michal Vasko63f3d842020-07-08 10:10:14 +02003070 LY_CHECK_RET(parse_status(ctx, in, &uses->flags, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003071 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003072 case LY_STMT_WHEN:
Michal Vasko63f3d842020-07-08 10:10:14 +02003073 LY_CHECK_RET(parse_when(ctx, in, &uses->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003074 break;
3075
Radek Krejcid6b76452019-09-03 17:03:03 +02003076 case LY_STMT_REFINE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003077 LY_CHECK_RET(parse_refine(ctx, in, &uses->refines));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003078 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003079 case LY_STMT_AUGMENT:
Michal Vasko22df3f02020-08-24 13:29:22 +02003080 LY_CHECK_RET(parse_augment(ctx, in, (struct lysp_node *)uses, &uses->augments));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003081 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003082 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003083 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003084 break;
3085 default:
David Sedlákb3077192019-06-19 10:55:37 +02003086 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "uses");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003087 return LY_EVALID;
3088 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003089 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003090checks:
3091 /* finalize parent pointers to the reallocated items */
Michal Vaskob36053d2020-03-26 15:49:30 +01003092 LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, NULL, uses->augments, NULL, NULL));
Radek Krejci7fc68292019-06-12 13:51:09 +02003093
Michal Vasko7fbc8162018-09-17 10:35:16 +02003094 return ret;
3095}
3096
Michal Vaskoea5abea2018-09-18 13:10:54 +02003097/**
3098 * @brief Parse the case statement.
3099 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003100 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02003101 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003102 * @param[in,out] siblings Siblings to add to.
3103 *
3104 * @return LY_ERR values.
3105 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003106LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02003107parse_case(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003108{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003109 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003110 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003111 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003112 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003113 struct lysp_node_case *cas;
3114
David Sedlák60adc092019-08-06 15:57:02 +02003115 /* create new case structure */
David Sedlákb9eeb9c2019-09-13 11:17:19 +02003116 LY_LIST_NEW_RET(ctx->ctx, siblings, cas, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003117 cas->nodetype = LYS_CASE;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003118 cas->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003119
Michal Vasko7fbc8162018-09-17 10:35:16 +02003120 /* get name */
Michal Vasko63f3d842020-07-08 10:10:14 +02003121 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003122 INSERT_WORD(ctx, buf, cas->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003123
3124 /* parse substatements */
Michal Vaskod989ba02020-08-24 10:59:24 +02003125 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003126 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003127 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02003128 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_DESCRIPTION, 0, &cas->dsc, Y_STR_ARG, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003129 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003130 case LY_STMT_IF_FEATURE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003131 LY_CHECK_RET(parse_text_fields(ctx, in, LYEXT_SUBSTMT_IFFEATURE, &cas->iffeatures, Y_STR_ARG, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003132 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003133 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003134 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_REFERENCE, 0, &cas->ref, Y_STR_ARG, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003135 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003136 case LY_STMT_STATUS:
Michal Vasko63f3d842020-07-08 10:10:14 +02003137 LY_CHECK_RET(parse_status(ctx, in, &cas->flags, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003138 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003139 case LY_STMT_WHEN:
Michal Vasko63f3d842020-07-08 10:10:14 +02003140 LY_CHECK_RET(parse_when(ctx, in, &cas->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003141 break;
3142
Radek Krejcid6b76452019-09-03 17:03:03 +02003143 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003144 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "case");
Radek Krejci0f969882020-08-21 16:56:47 +02003145 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003146 case LY_STMT_ANYXML:
Michal Vasko22df3f02020-08-24 13:29:22 +02003147 LY_CHECK_RET(parse_any(ctx, in, kw, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003148 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003149 case LY_STMT_CHOICE:
Michal Vasko22df3f02020-08-24 13:29:22 +02003150 LY_CHECK_RET(parse_choice(ctx, in, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003151 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003152 case LY_STMT_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02003153 LY_CHECK_RET(parse_container(ctx, in, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003154 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003155 case LY_STMT_LEAF:
Michal Vasko22df3f02020-08-24 13:29:22 +02003156 LY_CHECK_RET(parse_leaf(ctx, in, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003157 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003158 case LY_STMT_LEAF_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02003159 LY_CHECK_RET(parse_leaflist(ctx, in, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003160 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003161 case LY_STMT_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02003162 LY_CHECK_RET(parse_list(ctx, in, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003163 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003164 case LY_STMT_USES:
Michal Vasko22df3f02020-08-24 13:29:22 +02003165 LY_CHECK_RET(parse_uses(ctx, in, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003166 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003167 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003168 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003169 break;
3170 default:
David Sedlákb3077192019-06-19 10:55:37 +02003171 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "case");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003172 return LY_EVALID;
3173 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003174 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003175 return ret;
3176}
3177
Michal Vaskoea5abea2018-09-18 13:10:54 +02003178/**
3179 * @brief Parse the choice statement.
3180 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003181 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02003182 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003183 * @param[in,out] siblings Siblings to add to.
3184 *
3185 * @return LY_ERR values.
3186 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003187LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02003188parse_choice(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003189{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003190 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003191 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003192 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003193 enum ly_stmt kw;
Radek Krejci44ceedc2018-10-02 15:54:31 +02003194 struct lysp_node_choice *choice;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003195
David Sedlák60adc092019-08-06 15:57:02 +02003196 /* create new choice structure */
David Sedlákb9eeb9c2019-09-13 11:17:19 +02003197 LY_LIST_NEW_RET(ctx->ctx, siblings, choice, next, LY_EMEM);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003198 choice->nodetype = LYS_CHOICE;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003199 choice->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003200
Michal Vasko7fbc8162018-09-17 10:35:16 +02003201 /* get name */
Michal Vasko63f3d842020-07-08 10:10:14 +02003202 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003203 INSERT_WORD(ctx, buf, choice->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003204
3205 /* parse substatements */
Michal Vasko63f3d842020-07-08 10:10:14 +02003206 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003207 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003208 case LY_STMT_CONFIG:
Michal Vasko63f3d842020-07-08 10:10:14 +02003209 LY_CHECK_RET(parse_config(ctx, in, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003210 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003211 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02003212 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_DESCRIPTION, 0, &choice->dsc, Y_STR_ARG, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003213 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003214 case LY_STMT_IF_FEATURE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003215 LY_CHECK_RET(parse_text_fields(ctx, in, LYEXT_SUBSTMT_IFFEATURE, &choice->iffeatures, Y_STR_ARG, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003216 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003217 case LY_STMT_MANDATORY:
Michal Vasko63f3d842020-07-08 10:10:14 +02003218 LY_CHECK_RET(parse_mandatory(ctx, in, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003219 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003220 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003221 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_REFERENCE, 0, &choice->ref, Y_STR_ARG, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003222 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003223 case LY_STMT_STATUS:
Michal Vasko63f3d842020-07-08 10:10:14 +02003224 LY_CHECK_RET(parse_status(ctx, in, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003225 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003226 case LY_STMT_WHEN:
Michal Vasko63f3d842020-07-08 10:10:14 +02003227 LY_CHECK_RET(parse_when(ctx, in, &choice->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003228 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003229 case LY_STMT_DEFAULT:
Michal Vasko63f3d842020-07-08 10:10:14 +02003230 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_DEFAULT, 0, &choice->dflt, Y_PREF_IDENTIF_ARG, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003231 break;
3232
Radek Krejcid6b76452019-09-03 17:03:03 +02003233 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003234 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "choice");
Radek Krejci0f969882020-08-21 16:56:47 +02003235 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003236 case LY_STMT_ANYXML:
Michal Vasko22df3f02020-08-24 13:29:22 +02003237 LY_CHECK_RET(parse_any(ctx, in, kw, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003238 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003239 case LY_STMT_CASE:
Michal Vasko22df3f02020-08-24 13:29:22 +02003240 LY_CHECK_RET(parse_case(ctx, in, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003241 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003242 case LY_STMT_CHOICE:
Radek Krejci335332a2019-09-05 13:03:35 +02003243 PARSER_CHECK_STMTVER2_RET(ctx, "choice", "choice");
Michal Vasko22df3f02020-08-24 13:29:22 +02003244 LY_CHECK_RET(parse_choice(ctx, in, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003245 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003246 case LY_STMT_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02003247 LY_CHECK_RET(parse_container(ctx, in, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003248 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003249 case LY_STMT_LEAF:
Michal Vasko22df3f02020-08-24 13:29:22 +02003250 LY_CHECK_RET(parse_leaf(ctx, in, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003251 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003252 case LY_STMT_LEAF_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02003253 LY_CHECK_RET(parse_leaflist(ctx, in, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003254 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003255 case LY_STMT_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02003256 LY_CHECK_RET(parse_list(ctx, in, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003257 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003258 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003259 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003260 break;
3261 default:
David Sedlákb3077192019-06-19 10:55:37 +02003262 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "choice");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003263 return LY_EVALID;
3264 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003265 }
Radek Krejcia9026eb2018-12-12 16:04:47 +01003266 LY_CHECK_RET(ret);
3267checks:
3268 if ((choice->flags & LYS_MAND_TRUE) && choice->dflt) {
David Sedlákb3077192019-06-19 10:55:37 +02003269 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMSCOMB, "mandatory", "default", "choice");
Radek Krejcia9026eb2018-12-12 16:04:47 +01003270 return LY_EVALID;
3271 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003272 return ret;
3273}
3274
Michal Vaskoea5abea2018-09-18 13:10:54 +02003275/**
3276 * @brief Parse the container statement.
3277 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003278 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02003279 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003280 * @param[in,out] siblings Siblings to add to.
3281 *
3282 * @return LY_ERR values.
3283 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003284LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02003285parse_container(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003286{
3287 LY_ERR ret = 0;
3288 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003289 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003290 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003291 struct lysp_node_container *cont;
3292
David Sedlák60adc092019-08-06 15:57:02 +02003293 /* create new container structure */
David Sedlákb9eeb9c2019-09-13 11:17:19 +02003294 LY_LIST_NEW_RET(ctx->ctx, siblings, cont, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003295 cont->nodetype = LYS_CONTAINER;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003296 cont->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003297
Michal Vasko7fbc8162018-09-17 10:35:16 +02003298 /* get name */
Michal Vasko63f3d842020-07-08 10:10:14 +02003299 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003300 INSERT_WORD(ctx, buf, cont->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003301
3302 /* parse substatements */
Michal Vasko63f3d842020-07-08 10:10:14 +02003303 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003304 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003305 case LY_STMT_CONFIG:
Michal Vasko63f3d842020-07-08 10:10:14 +02003306 LY_CHECK_RET(parse_config(ctx, in, &cont->flags, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003307 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003308 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02003309 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_DESCRIPTION, 0, &cont->dsc, Y_STR_ARG, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003310 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003311 case LY_STMT_IF_FEATURE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003312 LY_CHECK_RET(parse_text_fields(ctx, in, LYEXT_SUBSTMT_IFFEATURE, &cont->iffeatures, Y_STR_ARG, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003313 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003314 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003315 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_REFERENCE, 0, &cont->ref, Y_STR_ARG, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003316 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003317 case LY_STMT_STATUS:
Michal Vasko63f3d842020-07-08 10:10:14 +02003318 LY_CHECK_RET(parse_status(ctx, in, &cont->flags, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003319 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003320 case LY_STMT_WHEN:
Michal Vasko63f3d842020-07-08 10:10:14 +02003321 LY_CHECK_RET(parse_when(ctx, in, &cont->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003322 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003323 case LY_STMT_PRESENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003324 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_PRESENCE, 0, &cont->presence, Y_STR_ARG, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003325 break;
3326
Radek Krejcid6b76452019-09-03 17:03:03 +02003327 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003328 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "container");
Radek Krejci0f969882020-08-21 16:56:47 +02003329 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003330 case LY_STMT_ANYXML:
Michal Vasko22df3f02020-08-24 13:29:22 +02003331 LY_CHECK_RET(parse_any(ctx, in, kw, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003332 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003333 case LY_STMT_CHOICE:
Michal Vasko22df3f02020-08-24 13:29:22 +02003334 LY_CHECK_RET(parse_choice(ctx, in, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003335 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003336 case LY_STMT_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02003337 LY_CHECK_RET(parse_container(ctx, in, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003338 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003339 case LY_STMT_LEAF:
Michal Vasko22df3f02020-08-24 13:29:22 +02003340 LY_CHECK_RET(parse_leaf(ctx, in, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003341 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003342 case LY_STMT_LEAF_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02003343 LY_CHECK_RET(parse_leaflist(ctx, in, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003344 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003345 case LY_STMT_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02003346 LY_CHECK_RET(parse_list(ctx, in, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003347 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003348 case LY_STMT_USES:
Michal Vasko22df3f02020-08-24 13:29:22 +02003349 LY_CHECK_RET(parse_uses(ctx, in, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003350 break;
3351
Radek Krejcid6b76452019-09-03 17:03:03 +02003352 case LY_STMT_TYPEDEF:
Michal Vasko22df3f02020-08-24 13:29:22 +02003353 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)cont, in, &cont->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003354 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003355 case LY_STMT_MUST:
Michal Vasko63f3d842020-07-08 10:10:14 +02003356 LY_CHECK_RET(parse_restrs(ctx, in, kw, &cont->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003357 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003358 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003359 PARSER_CHECK_STMTVER2_RET(ctx, "action", "container");
Michal Vasko22df3f02020-08-24 13:29:22 +02003360 LY_CHECK_RET(parse_action(ctx, in, (struct lysp_node *)cont, &cont->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003361 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003362 case LY_STMT_GROUPING:
Michal Vasko22df3f02020-08-24 13:29:22 +02003363 LY_CHECK_RET(parse_grouping(ctx, in, (struct lysp_node *)cont, &cont->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003364 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003365 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003366 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "container");
Michal Vasko22df3f02020-08-24 13:29:22 +02003367 LY_CHECK_RET(parse_notif(ctx, in, (struct lysp_node *)cont, &cont->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003368 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003369 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003370 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003371 break;
3372 default:
David Sedlákb3077192019-06-19 10:55:37 +02003373 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "container");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003374 return LY_EVALID;
3375 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003376 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003377checks:
Radek Krejcie86bf772018-12-14 11:39:53 +01003378 /* finalize parent pointers to the reallocated items */
Michal Vaskob36053d2020-03-26 15:49:30 +01003379 LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, cont->groupings, NULL, cont->actions, cont->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003380 return ret;
3381}
3382
Michal Vaskoea5abea2018-09-18 13:10:54 +02003383/**
3384 * @brief Parse the list statement.
3385 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003386 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02003387 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003388 * @param[in,out] siblings Siblings to add to.
3389 *
3390 * @return LY_ERR values.
3391 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003392LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02003393parse_list(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003394{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003395 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003396 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003397 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003398 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003399 struct lysp_node_list *list;
3400
David Sedlák60adc092019-08-06 15:57:02 +02003401 /* create new list structure */
David Sedlákb9eeb9c2019-09-13 11:17:19 +02003402 LY_LIST_NEW_RET(ctx->ctx, siblings, list, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003403 list->nodetype = LYS_LIST;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003404 list->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003405
Michal Vasko7fbc8162018-09-17 10:35:16 +02003406 /* get name */
Michal Vasko63f3d842020-07-08 10:10:14 +02003407 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003408 INSERT_WORD(ctx, buf, list->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003409
3410 /* parse substatements */
Michal Vasko63f3d842020-07-08 10:10:14 +02003411 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003412 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003413 case LY_STMT_CONFIG:
Michal Vasko63f3d842020-07-08 10:10:14 +02003414 LY_CHECK_RET(parse_config(ctx, in, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003415 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003416 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02003417 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_DESCRIPTION, 0, &list->dsc, Y_STR_ARG, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003418 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003419 case LY_STMT_IF_FEATURE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003420 LY_CHECK_RET(parse_text_fields(ctx, in, LYEXT_SUBSTMT_IFFEATURE, &list->iffeatures, Y_STR_ARG, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003421 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003422 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003423 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_REFERENCE, 0, &list->ref, Y_STR_ARG, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003424 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003425 case LY_STMT_STATUS:
Michal Vasko63f3d842020-07-08 10:10:14 +02003426 LY_CHECK_RET(parse_status(ctx, in, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003427 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003428 case LY_STMT_WHEN:
Michal Vasko63f3d842020-07-08 10:10:14 +02003429 LY_CHECK_RET(parse_when(ctx, in, &list->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003430 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003431 case LY_STMT_KEY:
Michal Vasko63f3d842020-07-08 10:10:14 +02003432 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_KEY, 0, &list->key, Y_STR_ARG, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003433 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003434 case LY_STMT_MAX_ELEMENTS:
Michal Vasko63f3d842020-07-08 10:10:14 +02003435 LY_CHECK_RET(parse_maxelements(ctx, in, &list->max, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003436 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003437 case LY_STMT_MIN_ELEMENTS:
Michal Vasko63f3d842020-07-08 10:10:14 +02003438 LY_CHECK_RET(parse_minelements(ctx, in, &list->min, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003439 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003440 case LY_STMT_ORDERED_BY:
Michal Vasko63f3d842020-07-08 10:10:14 +02003441 LY_CHECK_RET(parse_orderedby(ctx, in, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003442 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003443 case LY_STMT_UNIQUE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003444 LY_CHECK_RET(parse_text_fields(ctx, in, LYEXT_SUBSTMT_UNIQUE, &list->uniques, Y_STR_ARG, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003445 break;
3446
Radek Krejcid6b76452019-09-03 17:03:03 +02003447 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003448 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "list");
Radek Krejci0f969882020-08-21 16:56:47 +02003449 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003450 case LY_STMT_ANYXML:
Michal Vasko22df3f02020-08-24 13:29:22 +02003451 LY_CHECK_RET(parse_any(ctx, in, kw, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003452 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003453 case LY_STMT_CHOICE:
Michal Vasko22df3f02020-08-24 13:29:22 +02003454 LY_CHECK_RET(parse_choice(ctx, in, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003455 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003456 case LY_STMT_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02003457 LY_CHECK_RET(parse_container(ctx, in, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003458 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003459 case LY_STMT_LEAF:
Michal Vasko22df3f02020-08-24 13:29:22 +02003460 LY_CHECK_RET(parse_leaf(ctx, in, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003461 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003462 case LY_STMT_LEAF_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02003463 LY_CHECK_RET(parse_leaflist(ctx, in, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003464 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003465 case LY_STMT_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02003466 LY_CHECK_RET(parse_list(ctx, in, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003467 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003468 case LY_STMT_USES:
Michal Vasko22df3f02020-08-24 13:29:22 +02003469 LY_CHECK_RET(parse_uses(ctx, in, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003470 break;
3471
Radek Krejcid6b76452019-09-03 17:03:03 +02003472 case LY_STMT_TYPEDEF:
Michal Vasko22df3f02020-08-24 13:29:22 +02003473 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)list, in, &list->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003474 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003475 case LY_STMT_MUST:
Michal Vasko63f3d842020-07-08 10:10:14 +02003476 LY_CHECK_RET(parse_restrs(ctx, in, kw, &list->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003477 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003478 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003479 PARSER_CHECK_STMTVER2_RET(ctx, "action", "list");
Michal Vasko22df3f02020-08-24 13:29:22 +02003480 LY_CHECK_RET(parse_action(ctx, in, (struct lysp_node *)list, &list->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003481 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003482 case LY_STMT_GROUPING:
Michal Vasko22df3f02020-08-24 13:29:22 +02003483 LY_CHECK_RET(parse_grouping(ctx, in, (struct lysp_node *)list, &list->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003484 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003485 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003486 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "list");
Michal Vasko22df3f02020-08-24 13:29:22 +02003487 LY_CHECK_RET(parse_notif(ctx, in, (struct lysp_node *)list, &list->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003488 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003489 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003490 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003491 break;
3492 default:
David Sedlákb3077192019-06-19 10:55:37 +02003493 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "list");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003494 return LY_EVALID;
3495 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003496 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003497 LY_CHECK_RET(ret);
3498checks:
Radek Krejci7fc68292019-06-12 13:51:09 +02003499 /* finalize parent pointers to the reallocated items */
Michal Vaskob36053d2020-03-26 15:49:30 +01003500 LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, list->groupings, NULL, list->actions, list->notifs));
Radek Krejci7fc68292019-06-12 13:51:09 +02003501
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003502 if (list->max && list->min > list->max) {
David Sedlákb3077192019-06-19 10:55:37 +02003503 LOGVAL_PARSER(ctx, LYVE_SEMANTICS,
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003504 "Invalid combination of min-elements and max-elements: min value %u is bigger than the max value %u.",
3505 list->min, list->max);
3506 return LY_EVALID;
3507 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003508
3509 return ret;
3510}
3511
Michal Vaskoea5abea2018-09-18 13:10:54 +02003512/**
3513 * @brief Parse the yin-element statement.
3514 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003515 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02003516 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003517 * @param[in,out] flags Flags to write to.
3518 * @param[in,out] exts Extension instances to add to.
3519 *
3520 * @return LY_ERR values.
3521 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003522static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02003523parse_yinelement(struct lys_yang_parser_ctx *ctx, struct ly_in *in, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003524{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003525 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003526 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003527 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003528 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003529
3530 if (*flags & LYS_YINELEM_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02003531 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003532 return LY_EVALID;
3533 }
3534
3535 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02003536 LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003537
3538 if ((word_len == 4) && !strncmp(word, "true", word_len)) {
3539 *flags |= LYS_YINELEM_TRUE;
3540 } else if ((word_len == 5) && !strncmp(word, "false", word_len)) {
3541 *flags |= LYS_YINELEM_FALSE;
3542 } else {
David Sedlákb3077192019-06-19 10:55:37 +02003543 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003544 free(buf);
3545 return LY_EVALID;
3546 }
3547 free(buf);
3548
Michal Vaskod989ba02020-08-24 10:59:24 +02003549 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003550 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003551 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003552 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_YINELEM, 0, exts));
Michal Vaskod989ba02020-08-24 10:59:24 +02003553 LY_CHECK_RET(ret);
3554 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003555 default:
David Sedlákb3077192019-06-19 10:55:37 +02003556 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003557 return LY_EVALID;
3558 }
3559 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003560 return ret;
3561}
3562
Michal Vaskoea5abea2018-09-18 13:10:54 +02003563/**
David Sedlák2444f8f2019-07-09 11:02:47 +02003564 * @brief Parse the argument statement.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003565 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003566 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02003567 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003568 * @param[in,out] argument Value to write to.
3569 * @param[in,out] flags Flags to write to.
3570 * @param[in,out] exts Extension instances to add to.
3571 *
3572 * @return LY_ERR values.
3573 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003574static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02003575parse_argument(struct lys_yang_parser_ctx *ctx, struct ly_in *in, const char **argument, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003576{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003577 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003578 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003579 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003580 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003581
3582 if (*argument) {
David Sedlákb3077192019-06-19 10:55:37 +02003583 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003584 return LY_EVALID;
3585 }
3586
3587 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02003588 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003589 INSERT_WORD(ctx, buf, *argument, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003590
Michal Vaskod989ba02020-08-24 10:59:24 +02003591 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003592 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003593 case LY_STMT_YIN_ELEMENT:
Michal Vasko63f3d842020-07-08 10:10:14 +02003594 LY_CHECK_RET(parse_yinelement(ctx, in, flags, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003595 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003596 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003597 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_ARGUMENT, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003598 break;
3599 default:
David Sedlákb3077192019-06-19 10:55:37 +02003600 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003601 return LY_EVALID;
3602 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003603 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003604 return ret;
3605}
3606
Michal Vaskoea5abea2018-09-18 13:10:54 +02003607/**
3608 * @brief Parse the extension statement.
3609 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003610 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02003611 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003612 * @param[in,out] extensions Extensions to add to.
3613 *
3614 * @return LY_ERR values.
3615 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003616static LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02003617parse_extension(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_ext **extensions)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003618{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003619 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003620 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003621 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003622 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003623 struct lysp_ext *ex;
3624
Radek Krejci2c4e7172018-10-19 15:56:26 +02003625 LY_ARRAY_NEW_RET(ctx->ctx, *extensions, ex, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003626
3627 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02003628 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003629 INSERT_WORD(ctx, buf, ex->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003630
Michal Vaskod989ba02020-08-24 10:59:24 +02003631 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003632 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003633 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02003634 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_DESCRIPTION, 0, &ex->dsc, Y_STR_ARG, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003635 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003636 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003637 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_REFERENCE, 0, &ex->ref, Y_STR_ARG, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003638 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003639 case LY_STMT_STATUS:
Michal Vasko63f3d842020-07-08 10:10:14 +02003640 LY_CHECK_RET(parse_status(ctx, in, &ex->flags, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003641 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003642 case LY_STMT_ARGUMENT:
Michal Vasko63f3d842020-07-08 10:10:14 +02003643 LY_CHECK_RET(parse_argument(ctx, in, &ex->argument, &ex->flags, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003644 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003645 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003646 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003647 break;
3648 default:
David Sedlákb3077192019-06-19 10:55:37 +02003649 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "extension");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003650 return LY_EVALID;
3651 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003652 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003653 return ret;
3654}
3655
Michal Vaskoea5abea2018-09-18 13:10:54 +02003656/**
3657 * @brief Parse the deviate statement.
3658 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003659 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02003660 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003661 * @param[in,out] deviates Deviates to add to.
3662 *
3663 * @return LY_ERR values.
3664 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003665LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02003666parse_deviate(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_deviate **deviates)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003667{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003668 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003669 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003670 size_t word_len, dev_mod;
Radek Krejcid6b76452019-09-03 17:03:03 +02003671 enum ly_stmt kw;
David Sedlák60adc092019-08-06 15:57:02 +02003672 struct lysp_deviate *d;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003673 struct lysp_deviate_add *d_add = NULL;
3674 struct lysp_deviate_rpl *d_rpl = NULL;
3675 struct lysp_deviate_del *d_del = NULL;
Radek Krejci4ad42aa2019-07-23 16:55:58 +02003676 const char **d_units = NULL, ***d_uniques = NULL, ***d_dflts = NULL;
3677 struct lysp_restr **d_musts = NULL;
3678 uint16_t *d_flags = 0;
3679 uint32_t *d_min = 0, *d_max = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003680
3681 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02003682 LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003683
3684 if ((word_len == 13) && !strncmp(word, "not-supported", word_len)) {
3685 dev_mod = LYS_DEV_NOT_SUPPORTED;
3686 } else if ((word_len == 3) && !strncmp(word, "add", word_len)) {
3687 dev_mod = LYS_DEV_ADD;
3688 } else if ((word_len == 7) && !strncmp(word, "replace", word_len)) {
3689 dev_mod = LYS_DEV_REPLACE;
3690 } else if ((word_len == 6) && !strncmp(word, "delete", word_len)) {
3691 dev_mod = LYS_DEV_DELETE;
3692 } else {
David Sedlákb3077192019-06-19 10:55:37 +02003693 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "deviate");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003694 free(buf);
3695 return LY_EVALID;
3696 }
3697 free(buf);
3698
3699 /* create structure */
3700 switch (dev_mod) {
3701 case LYS_DEV_NOT_SUPPORTED:
3702 d = calloc(1, sizeof *d);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003703 LY_CHECK_ERR_RET(!d, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003704 break;
3705 case LYS_DEV_ADD:
3706 d_add = calloc(1, sizeof *d_add);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003707 LY_CHECK_ERR_RET(!d_add, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003708 d = (struct lysp_deviate *)d_add;
3709 d_units = &d_add->units;
3710 d_uniques = &d_add->uniques;
3711 d_dflts = &d_add->dflts;
3712 d_musts = &d_add->musts;
3713 d_flags = &d_add->flags;
3714 d_min = &d_add->min;
3715 d_max = &d_add->max;
3716 break;
3717 case LYS_DEV_REPLACE:
3718 d_rpl = calloc(1, sizeof *d_rpl);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003719 LY_CHECK_ERR_RET(!d_rpl, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003720 d = (struct lysp_deviate *)d_rpl;
3721 d_units = &d_rpl->units;
3722 d_flags = &d_rpl->flags;
3723 d_min = &d_rpl->min;
3724 d_max = &d_rpl->max;
3725 break;
3726 case LYS_DEV_DELETE:
3727 d_del = calloc(1, sizeof *d_del);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003728 LY_CHECK_ERR_RET(!d_del, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003729 d = (struct lysp_deviate *)d_del;
3730 d_units = &d_del->units;
3731 d_uniques = &d_del->uniques;
3732 d_dflts = &d_del->dflts;
3733 d_musts = &d_del->musts;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003734 break;
3735 default:
3736 assert(0);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003737 LOGINT_RET(ctx->ctx);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003738 }
3739 d->mod = dev_mod;
3740
3741 /* insert into siblings */
David Sedlák60adc092019-08-06 15:57:02 +02003742 LY_LIST_INSERT(deviates, d, next);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003743
Michal Vaskod989ba02020-08-24 10:59:24 +02003744 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003745 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003746 case LY_STMT_CONFIG:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003747 switch (dev_mod) {
3748 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003749 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003750 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003751 return LY_EVALID;
3752 default:
Michal Vasko63f3d842020-07-08 10:10:14 +02003753 LY_CHECK_RET(parse_config(ctx, in, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003754 break;
3755 }
3756 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003757 case LY_STMT_DEFAULT:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003758 switch (dev_mod) {
3759 case LYS_DEV_NOT_SUPPORTED:
David Sedlákb3077192019-06-19 10:55:37 +02003760 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003761 return LY_EVALID;
3762 case LYS_DEV_REPLACE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003763 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_DEFAULT, 0, &d_rpl->dflt, Y_STR_ARG, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003764 break;
3765 default:
Michal Vasko63f3d842020-07-08 10:10:14 +02003766 LY_CHECK_RET(parse_text_fields(ctx, in, LYEXT_SUBSTMT_DEFAULT, d_dflts, Y_STR_ARG, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003767 break;
3768 }
3769 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003770 case LY_STMT_MANDATORY:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003771 switch (dev_mod) {
3772 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003773 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003774 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003775 return LY_EVALID;
3776 default:
Michal Vasko63f3d842020-07-08 10:10:14 +02003777 LY_CHECK_RET(parse_mandatory(ctx, in, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003778 break;
3779 }
3780 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003781 case LY_STMT_MAX_ELEMENTS:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003782 switch (dev_mod) {
3783 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003784 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003785 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003786 return LY_EVALID;
3787 default:
Michal Vasko63f3d842020-07-08 10:10:14 +02003788 LY_CHECK_RET(parse_maxelements(ctx, in, d_max, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003789 break;
3790 }
3791 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003792 case LY_STMT_MIN_ELEMENTS:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003793 switch (dev_mod) {
3794 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003795 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003796 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003797 return LY_EVALID;
3798 default:
Michal Vasko63f3d842020-07-08 10:10:14 +02003799 LY_CHECK_RET(parse_minelements(ctx, in, d_min, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003800 break;
3801 }
3802 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003803 case LY_STMT_MUST:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003804 switch (dev_mod) {
3805 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003806 case LYS_DEV_REPLACE:
David Sedlákb3077192019-06-19 10:55:37 +02003807 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003808 return LY_EVALID;
3809 default:
Michal Vasko63f3d842020-07-08 10:10:14 +02003810 LY_CHECK_RET(parse_restrs(ctx, in, kw, d_musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003811 break;
3812 }
3813 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003814 case LY_STMT_TYPE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003815 switch (dev_mod) {
3816 case LYS_DEV_NOT_SUPPORTED:
3817 case LYS_DEV_ADD:
3818 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003819 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003820 return LY_EVALID;
3821 default:
Radek Krejci2c02f3e2018-10-16 10:54:38 +02003822 if (d_rpl->type) {
David Sedlákb3077192019-06-19 10:55:37 +02003823 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
Radek Krejci2c02f3e2018-10-16 10:54:38 +02003824 return LY_EVALID;
3825 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003826 d_rpl->type = calloc(1, sizeof *d_rpl->type);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003827 LY_CHECK_ERR_RET(!d_rpl->type, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko63f3d842020-07-08 10:10:14 +02003828 LY_CHECK_RET(parse_type(ctx, in, d_rpl->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003829 break;
3830 }
3831 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003832 case LY_STMT_UNIQUE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003833 switch (dev_mod) {
3834 case LYS_DEV_NOT_SUPPORTED:
3835 case LYS_DEV_REPLACE:
David Sedlákb3077192019-06-19 10:55:37 +02003836 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003837 return LY_EVALID;
3838 default:
Michal Vasko63f3d842020-07-08 10:10:14 +02003839 LY_CHECK_RET(parse_text_fields(ctx, in, LYEXT_SUBSTMT_UNIQUE, d_uniques, Y_STR_ARG, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003840 break;
3841 }
3842 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003843 case LY_STMT_UNITS:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003844 switch (dev_mod) {
3845 case LYS_DEV_NOT_SUPPORTED:
David Sedlákb3077192019-06-19 10:55:37 +02003846 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003847 return LY_EVALID;
3848 default:
Michal Vasko63f3d842020-07-08 10:10:14 +02003849 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_UNITS, 0, d_units, Y_STR_ARG, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003850 break;
3851 }
3852 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003853 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003854 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003855 break;
3856 default:
David Sedlákb3077192019-06-19 10:55:37 +02003857 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "deviate");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003858 return LY_EVALID;
3859 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003860 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003861 return ret;
3862}
3863
Michal Vaskoea5abea2018-09-18 13:10:54 +02003864/**
3865 * @brief Parse the deviation statement.
3866 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003867 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02003868 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003869 * @param[in,out] deviations Deviations to add to.
3870 *
3871 * @return LY_ERR values.
3872 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003873LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02003874parse_deviation(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_deviation **deviations)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003875{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003876 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003877 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003878 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003879 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003880 struct lysp_deviation *dev;
3881
Radek Krejci2c4e7172018-10-19 15:56:26 +02003882 LY_ARRAY_NEW_RET(ctx->ctx, *deviations, dev, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003883
3884 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02003885 LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vaskob36053d2020-03-26 15:49:30 +01003886 CHECK_NONEMPTY(ctx, word_len, "deviation");
Radek Krejci44ceedc2018-10-02 15:54:31 +02003887 INSERT_WORD(ctx, buf, dev->nodeid, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003888
Michal Vasko63f3d842020-07-08 10:10:14 +02003889 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003890 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003891 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02003892 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_DESCRIPTION, 0, &dev->dsc, Y_STR_ARG, &dev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003893 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003894 case LY_STMT_DEVIATE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003895 LY_CHECK_RET(parse_deviate(ctx, in, &dev->deviates));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003896 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003897 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003898 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_REFERENCE, 0, &dev->ref, Y_STR_ARG, &dev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003899 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003900 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003901 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &dev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003902 break;
3903 default:
David Sedlákb3077192019-06-19 10:55:37 +02003904 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "deviation");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003905 return LY_EVALID;
3906 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003907 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003908 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01003909checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003910 /* mandatory substatements */
3911 if (!dev->deviates) {
David Sedlákb3077192019-06-19 10:55:37 +02003912 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "deviate", "deviation");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003913 return LY_EVALID;
3914 }
3915
3916 return ret;
3917}
3918
Michal Vaskoea5abea2018-09-18 13:10:54 +02003919/**
3920 * @brief Parse the feature statement.
3921 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003922 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02003923 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003924 * @param[in,out] features Features to add to.
3925 *
3926 * @return LY_ERR values.
3927 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003928LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02003929parse_feature(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_feature **features)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003930{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003931 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003932 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003933 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003934 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003935 struct lysp_feature *feat;
3936
Radek Krejci2c4e7172018-10-19 15:56:26 +02003937 LY_ARRAY_NEW_RET(ctx->ctx, *features, feat, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003938
3939 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02003940 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003941 INSERT_WORD(ctx, buf, feat->name, word, word_len);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01003942
Michal Vaskod989ba02020-08-24 10:59:24 +02003943 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003944 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003945 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02003946 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_DESCRIPTION, 0, &feat->dsc, Y_STR_ARG, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003947 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003948 case LY_STMT_IF_FEATURE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003949 LY_CHECK_RET(parse_text_fields(ctx, in, LYEXT_SUBSTMT_IFFEATURE, &feat->iffeatures, Y_STR_ARG, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003950 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003951 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003952 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_REFERENCE, 0, &feat->ref, Y_STR_ARG, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003953 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003954 case LY_STMT_STATUS:
Michal Vasko63f3d842020-07-08 10:10:14 +02003955 LY_CHECK_RET(parse_status(ctx, in, &feat->flags, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003956 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003957 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02003958 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003959 break;
3960 default:
David Sedlákb3077192019-06-19 10:55:37 +02003961 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "feature");
Radek Krejci2c02f3e2018-10-16 10:54:38 +02003962 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003963 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003964 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003965 return ret;
3966}
3967
Michal Vaskoea5abea2018-09-18 13:10:54 +02003968/**
3969 * @brief Parse the identity statement.
3970 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003971 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02003972 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003973 * @param[in,out] identities Identities to add to.
3974 *
3975 * @return LY_ERR values.
3976 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003977LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02003978parse_identity(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_ident **identities)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003979{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003980 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003981 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003982 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003983 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003984 struct lysp_ident *ident;
3985
Radek Krejci2c4e7172018-10-19 15:56:26 +02003986 LY_ARRAY_NEW_RET(ctx->ctx, *identities, ident, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003987
3988 /* get value */
Michal Vasko63f3d842020-07-08 10:10:14 +02003989 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003990 INSERT_WORD(ctx, buf, ident->name, word, word_len);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01003991
Michal Vaskod989ba02020-08-24 10:59:24 +02003992 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003993 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003994 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02003995 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_DESCRIPTION, 0, &ident->dsc, Y_STR_ARG, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003996 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003997 case LY_STMT_IF_FEATURE:
Radek Krejci335332a2019-09-05 13:03:35 +02003998 PARSER_CHECK_STMTVER2_RET(ctx, "if-feature", "identity");
Michal Vasko63f3d842020-07-08 10:10:14 +02003999 LY_CHECK_RET(parse_text_fields(ctx, in, LYEXT_SUBSTMT_IFFEATURE, &ident->iffeatures, Y_STR_ARG, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004000 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004001 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02004002 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_REFERENCE, 0, &ident->ref, Y_STR_ARG, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004003 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004004 case LY_STMT_STATUS:
Michal Vasko63f3d842020-07-08 10:10:14 +02004005 LY_CHECK_RET(parse_status(ctx, in, &ident->flags, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004006 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004007 case LY_STMT_BASE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004008 if (ident->bases && ctx->mod_version < 2) {
David Sedlákb3077192019-06-19 10:55:37 +02004009 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Identity can be derived from multiple base identities only in YANG 1.1 modules");
Radek Krejci10113652018-11-14 16:56:50 +01004010 return LY_EVALID;
4011 }
Michal Vasko63f3d842020-07-08 10:10:14 +02004012 LY_CHECK_RET(parse_text_fields(ctx, in, LYEXT_SUBSTMT_BASE, &ident->bases, Y_PREF_IDENTIF_ARG, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004013 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004014 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02004015 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004016 break;
4017 default:
David Sedlákb3077192019-06-19 10:55:37 +02004018 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "identity");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004019 return LY_EVALID;
4020 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004021 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004022 return ret;
4023}
4024
Michal Vaskoea5abea2018-09-18 13:10:54 +02004025/**
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004026 * @brief Parse module substatements.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004027 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004028 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02004029 * @param[in,out] in Input structure.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004030 * @param[in,out] mod Module to write to.
4031 *
4032 * @return LY_ERR values.
4033 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004034LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02004035parse_module(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_module *mod)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004036{
4037 LY_ERR ret = 0;
4038 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004039 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004040 enum ly_stmt kw, prev_kw = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004041 enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004042 struct lysp_submodule *dup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004043
4044 /* (sub)module name */
Michal Vasko63f3d842020-07-08 10:10:14 +02004045 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004046 INSERT_WORD(ctx, buf, mod->mod->name, word, word_len);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01004047
Michal Vasko63f3d842020-07-08 10:10:14 +02004048 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004049
Radek Krejcie3846472018-10-15 15:24:51 +02004050#define CHECK_ORDER(SECTION) \
David Sedlákb3077192019-06-19 10:55:37 +02004051 if (mod_stmt > SECTION) {LOGVAL_PARSER(ctx, LY_VCODE_INORD, ly_stmt2str(kw), ly_stmt2str(prev_kw)); return LY_EVALID;}mod_stmt = SECTION
Radek Krejcie3846472018-10-15 15:24:51 +02004052
Michal Vasko7fbc8162018-09-17 10:35:16 +02004053 switch (kw) {
4054 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004055 case LY_STMT_NAMESPACE:
4056 case LY_STMT_PREFIX:
Radek Krejcie3846472018-10-15 15:24:51 +02004057 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4058 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004059 case LY_STMT_YANG_VERSION:
Radek Krejcie3846472018-10-15 15:24:51 +02004060 CHECK_ORDER(Y_MOD_MODULE_HEADER);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004061 break;
4062 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004063 case LY_STMT_INCLUDE:
4064 case LY_STMT_IMPORT:
Radek Krejcie3846472018-10-15 15:24:51 +02004065 CHECK_ORDER(Y_MOD_LINKAGE);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004066 break;
4067 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004068 case LY_STMT_ORGANIZATION:
4069 case LY_STMT_CONTACT:
4070 case LY_STMT_DESCRIPTION:
4071 case LY_STMT_REFERENCE:
Radek Krejcie3846472018-10-15 15:24:51 +02004072 CHECK_ORDER(Y_MOD_META);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004073 break;
4074
4075 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004076 case LY_STMT_REVISION:
Radek Krejcie3846472018-10-15 15:24:51 +02004077 CHECK_ORDER(Y_MOD_REVISION);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004078 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004079 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004080 case LY_STMT_ANYDATA:
4081 case LY_STMT_ANYXML:
4082 case LY_STMT_AUGMENT:
4083 case LY_STMT_CHOICE:
4084 case LY_STMT_CONTAINER:
4085 case LY_STMT_DEVIATION:
4086 case LY_STMT_EXTENSION:
4087 case LY_STMT_FEATURE:
4088 case LY_STMT_GROUPING:
4089 case LY_STMT_IDENTITY:
4090 case LY_STMT_LEAF:
4091 case LY_STMT_LEAF_LIST:
4092 case LY_STMT_LIST:
4093 case LY_STMT_NOTIFICATION:
4094 case LY_STMT_RPC:
4095 case LY_STMT_TYPEDEF:
4096 case LY_STMT_USES:
4097 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004098 mod_stmt = Y_MOD_BODY;
4099 break;
4100 default:
4101 /* error handled in the next switch */
4102 break;
4103 }
Radek Krejcie3846472018-10-15 15:24:51 +02004104#undef CHECK_ORDER
Michal Vasko7fbc8162018-09-17 10:35:16 +02004105
Radek Krejcie3846472018-10-15 15:24:51 +02004106 prev_kw = kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004107 switch (kw) {
4108 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004109 case LY_STMT_YANG_VERSION:
Michal Vasko63f3d842020-07-08 10:10:14 +02004110 LY_CHECK_RET(parse_yangversion(ctx, in, &mod->mod->version, &mod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004111 ctx->mod_version = mod->mod->version;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004112 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004113 case LY_STMT_NAMESPACE:
Michal Vasko63f3d842020-07-08 10:10:14 +02004114 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_NAMESPACE, 0, &mod->mod->ns, Y_STR_ARG, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004115 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004116 case LY_STMT_PREFIX:
Michal Vasko63f3d842020-07-08 10:10:14 +02004117 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_PREFIX, 0, &mod->mod->prefix, Y_IDENTIF_ARG, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004118 break;
4119
4120 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004121 case LY_STMT_INCLUDE:
Michal Vasko63f3d842020-07-08 10:10:14 +02004122 LY_CHECK_RET(parse_include(ctx, mod->mod->name, in, &mod->includes));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004123 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004124 case LY_STMT_IMPORT:
Michal Vasko63f3d842020-07-08 10:10:14 +02004125 LY_CHECK_RET(parse_import(ctx, mod->mod->prefix, in, &mod->imports));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004126 break;
4127
4128 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004129 case LY_STMT_ORGANIZATION:
Michal Vasko63f3d842020-07-08 10:10:14 +02004130 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_ORGANIZATION, 0, &mod->mod->org, Y_STR_ARG, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004131 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004132 case LY_STMT_CONTACT:
Michal Vasko63f3d842020-07-08 10:10:14 +02004133 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_CONTACT, 0, &mod->mod->contact, Y_STR_ARG, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004134 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004135 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02004136 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_DESCRIPTION, 0, &mod->mod->dsc, Y_STR_ARG, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004137 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004138 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02004139 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_REFERENCE, 0, &mod->mod->ref, Y_STR_ARG, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004140 break;
4141
4142 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004143 case LY_STMT_REVISION:
Michal Vasko63f3d842020-07-08 10:10:14 +02004144 LY_CHECK_RET(parse_revision(ctx, in, &mod->revs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004145 break;
4146
4147 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004148 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02004149 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "module");
Radek Krejci0f969882020-08-21 16:56:47 +02004150 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02004151 case LY_STMT_ANYXML:
Michal Vasko63f3d842020-07-08 10:10:14 +02004152 LY_CHECK_RET(parse_any(ctx, in, kw, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004153 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004154 case LY_STMT_CHOICE:
Michal Vasko63f3d842020-07-08 10:10:14 +02004155 LY_CHECK_RET(parse_choice(ctx, in, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004156 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004157 case LY_STMT_CONTAINER:
Michal Vasko63f3d842020-07-08 10:10:14 +02004158 LY_CHECK_RET(parse_container(ctx, in, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004159 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004160 case LY_STMT_LEAF:
Michal Vasko63f3d842020-07-08 10:10:14 +02004161 LY_CHECK_RET(parse_leaf(ctx, in, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004162 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004163 case LY_STMT_LEAF_LIST:
Michal Vasko63f3d842020-07-08 10:10:14 +02004164 LY_CHECK_RET(parse_leaflist(ctx, in, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004165 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004166 case LY_STMT_LIST:
Michal Vasko63f3d842020-07-08 10:10:14 +02004167 LY_CHECK_RET(parse_list(ctx, in, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004168 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004169 case LY_STMT_USES:
Michal Vasko63f3d842020-07-08 10:10:14 +02004170 LY_CHECK_RET(parse_uses(ctx, in, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004171 break;
4172
Radek Krejcid6b76452019-09-03 17:03:03 +02004173 case LY_STMT_AUGMENT:
Michal Vasko63f3d842020-07-08 10:10:14 +02004174 LY_CHECK_RET(parse_augment(ctx, in, NULL, &mod->augments));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004175 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004176 case LY_STMT_DEVIATION:
Michal Vasko63f3d842020-07-08 10:10:14 +02004177 LY_CHECK_RET(parse_deviation(ctx, in, &mod->deviations));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004178 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004179 case LY_STMT_EXTENSION:
Michal Vasko63f3d842020-07-08 10:10:14 +02004180 LY_CHECK_RET(parse_extension(ctx, in, &mod->extensions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004181 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004182 case LY_STMT_FEATURE:
Michal Vasko63f3d842020-07-08 10:10:14 +02004183 LY_CHECK_RET(parse_feature(ctx, in, &mod->features));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004184 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004185 case LY_STMT_GROUPING:
Michal Vasko63f3d842020-07-08 10:10:14 +02004186 LY_CHECK_RET(parse_grouping(ctx, in, NULL, &mod->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004187 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004188 case LY_STMT_IDENTITY:
Michal Vasko63f3d842020-07-08 10:10:14 +02004189 LY_CHECK_RET(parse_identity(ctx, in, &mod->identities));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004190 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004191 case LY_STMT_NOTIFICATION:
Michal Vasko63f3d842020-07-08 10:10:14 +02004192 LY_CHECK_RET(parse_notif(ctx, in, NULL, &mod->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004193 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004194 case LY_STMT_RPC:
Michal Vasko63f3d842020-07-08 10:10:14 +02004195 LY_CHECK_RET(parse_action(ctx, in, NULL, &mod->rpcs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004196 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004197 case LY_STMT_TYPEDEF:
Michal Vasko63f3d842020-07-08 10:10:14 +02004198 LY_CHECK_RET(parse_typedef(ctx, NULL, in, &mod->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004199 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004200 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02004201 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004202 break;
4203
4204 default:
David Sedlákb3077192019-06-19 10:55:37 +02004205 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "module");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004206 return LY_EVALID;
4207 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004208 }
Radek Krejcic59bc972018-09-17 16:13:06 +02004209 LY_CHECK_RET(ret);
Radek Krejcie86bf772018-12-14 11:39:53 +01004210
Radek Krejci6d6556c2018-11-08 09:37:45 +01004211checks:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004212 /* finalize parent pointers to the reallocated items */
Michal Vaskob36053d2020-03-26 15:49:30 +01004213 LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, mod->groupings, mod->augments, mod->rpcs, mod->notifs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004214
Michal Vasko7fbc8162018-09-17 10:35:16 +02004215 /* mandatory substatements */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004216 if (!mod->mod->ns) {
David Sedlákb3077192019-06-19 10:55:37 +02004217 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "namespace", "module");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004218 return LY_EVALID;
4219 } else if (!mod->mod->prefix) {
David Sedlákb3077192019-06-19 10:55:37 +02004220 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "module");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004221 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004222 }
4223
Radek Krejcie9e987e2018-10-31 12:50:27 +01004224 /* submodules share the namespace with the module names, so there must not be
4225 * a submodule of the same name in the context, no need for revision matching */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004226 dup = ly_ctx_get_submodule(ctx->ctx, NULL, mod->mod->name, NULL);
4227 if (dup) {
David Sedlákb3077192019-06-19 10:55:37 +02004228 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Name collision between module and submodule of name \"%s\".", mod->mod->name);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004229 return LY_EVALID;
4230 }
4231
4232 return ret;
4233}
4234
4235/**
4236 * @brief Parse submodule substatements.
4237 *
4238 * @param[in] ctx yang parser context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +02004239 * @param[in,out] in Input structure.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004240 * @param[out] submod Parsed submodule structure.
4241 *
4242 * @return LY_ERR values.
4243 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004244LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02004245parse_submodule(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_submodule *submod)
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004246{
4247 LY_ERR ret = 0;
4248 char *buf, *word;
4249 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004250 enum ly_stmt kw, prev_kw = 0;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004251 enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER;
4252 struct lysp_submodule *dup;
4253
4254 /* submodule name */
Michal Vasko63f3d842020-07-08 10:10:14 +02004255 LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004256 INSERT_WORD(ctx, buf, submod->name, word, word_len);
4257
Michal Vasko63f3d842020-07-08 10:10:14 +02004258 YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, goto checks) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004259
4260#define CHECK_ORDER(SECTION) \
David Sedlákb3077192019-06-19 10:55:37 +02004261 if (mod_stmt > SECTION) {LOGVAL_PARSER(ctx, LY_VCODE_INORD, ly_stmt2str(kw), ly_stmt2str(prev_kw)); return LY_EVALID;}mod_stmt = SECTION
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004262
4263 switch (kw) {
4264 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004265 case LY_STMT_BELONGS_TO:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004266 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4267 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004268 case LY_STMT_YANG_VERSION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004269 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4270 break;
4271 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004272 case LY_STMT_INCLUDE:
4273 case LY_STMT_IMPORT:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004274 CHECK_ORDER(Y_MOD_LINKAGE);
4275 break;
4276 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004277 case LY_STMT_ORGANIZATION:
4278 case LY_STMT_CONTACT:
4279 case LY_STMT_DESCRIPTION:
4280 case LY_STMT_REFERENCE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004281 CHECK_ORDER(Y_MOD_META);
4282 break;
4283
4284 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004285 case LY_STMT_REVISION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004286 CHECK_ORDER(Y_MOD_REVISION);
4287 break;
4288 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004289 case LY_STMT_ANYDATA:
4290 case LY_STMT_ANYXML:
4291 case LY_STMT_AUGMENT:
4292 case LY_STMT_CHOICE:
4293 case LY_STMT_CONTAINER:
4294 case LY_STMT_DEVIATION:
4295 case LY_STMT_EXTENSION:
4296 case LY_STMT_FEATURE:
4297 case LY_STMT_GROUPING:
4298 case LY_STMT_IDENTITY:
4299 case LY_STMT_LEAF:
4300 case LY_STMT_LEAF_LIST:
4301 case LY_STMT_LIST:
4302 case LY_STMT_NOTIFICATION:
4303 case LY_STMT_RPC:
4304 case LY_STMT_TYPEDEF:
4305 case LY_STMT_USES:
4306 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004307 mod_stmt = Y_MOD_BODY;
4308 break;
4309 default:
4310 /* error handled in the next switch */
4311 break;
4312 }
4313#undef CHECK_ORDER
4314
4315 prev_kw = kw;
4316 switch (kw) {
4317 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004318 case LY_STMT_YANG_VERSION:
Michal Vasko63f3d842020-07-08 10:10:14 +02004319 LY_CHECK_RET(parse_yangversion(ctx, in, &submod->version, &submod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004320 ctx->mod_version = submod->version;
4321 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004322 case LY_STMT_BELONGS_TO:
Michal Vasko63f3d842020-07-08 10:10:14 +02004323 LY_CHECK_RET(parse_belongsto(ctx, in, &submod->belongsto, &submod->prefix, &submod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004324 break;
4325
4326 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004327 case LY_STMT_INCLUDE:
Michal Vasko63f3d842020-07-08 10:10:14 +02004328 LY_CHECK_RET(parse_include(ctx, submod->name, in, &submod->includes));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004329 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004330 case LY_STMT_IMPORT:
Michal Vasko63f3d842020-07-08 10:10:14 +02004331 LY_CHECK_RET(parse_import(ctx, submod->prefix, in, &submod->imports));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004332 break;
4333
4334 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004335 case LY_STMT_ORGANIZATION:
Michal Vasko63f3d842020-07-08 10:10:14 +02004336 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_ORGANIZATION, 0, &submod->org, Y_STR_ARG, &submod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004337 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004338 case LY_STMT_CONTACT:
Michal Vasko63f3d842020-07-08 10:10:14 +02004339 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_CONTACT, 0, &submod->contact, Y_STR_ARG, &submod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004340 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004341 case LY_STMT_DESCRIPTION:
Michal Vasko63f3d842020-07-08 10:10:14 +02004342 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_DESCRIPTION, 0, &submod->dsc, Y_STR_ARG, &submod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004343 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004344 case LY_STMT_REFERENCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02004345 LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_REFERENCE, 0, &submod->ref, Y_STR_ARG, &submod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004346 break;
4347
4348 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004349 case LY_STMT_REVISION:
Michal Vasko63f3d842020-07-08 10:10:14 +02004350 LY_CHECK_RET(parse_revision(ctx, in, &submod->revs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004351 break;
4352
4353 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004354 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02004355 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "submodule");
Radek Krejci0f969882020-08-21 16:56:47 +02004356 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02004357 case LY_STMT_ANYXML:
Michal Vasko63f3d842020-07-08 10:10:14 +02004358 LY_CHECK_RET(parse_any(ctx, in, kw, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004359 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004360 case LY_STMT_CHOICE:
Michal Vasko63f3d842020-07-08 10:10:14 +02004361 LY_CHECK_RET(parse_choice(ctx, in, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004362 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004363 case LY_STMT_CONTAINER:
Michal Vasko63f3d842020-07-08 10:10:14 +02004364 LY_CHECK_RET(parse_container(ctx, in, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004365 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004366 case LY_STMT_LEAF:
Michal Vasko63f3d842020-07-08 10:10:14 +02004367 LY_CHECK_RET(parse_leaf(ctx, in, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004368 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004369 case LY_STMT_LEAF_LIST:
Michal Vasko63f3d842020-07-08 10:10:14 +02004370 LY_CHECK_RET(parse_leaflist(ctx, in, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004371 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004372 case LY_STMT_LIST:
Michal Vasko63f3d842020-07-08 10:10:14 +02004373 LY_CHECK_RET(parse_list(ctx, in, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004374 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004375 case LY_STMT_USES:
Michal Vasko63f3d842020-07-08 10:10:14 +02004376 LY_CHECK_RET(parse_uses(ctx, in, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004377 break;
4378
Radek Krejcid6b76452019-09-03 17:03:03 +02004379 case LY_STMT_AUGMENT:
Michal Vasko63f3d842020-07-08 10:10:14 +02004380 LY_CHECK_RET(parse_augment(ctx, in, NULL, &submod->augments));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004381 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004382 case LY_STMT_DEVIATION:
Michal Vasko63f3d842020-07-08 10:10:14 +02004383 LY_CHECK_RET(parse_deviation(ctx, in, &submod->deviations));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004384 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004385 case LY_STMT_EXTENSION:
Michal Vasko63f3d842020-07-08 10:10:14 +02004386 LY_CHECK_RET(parse_extension(ctx, in, &submod->extensions));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004387 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004388 case LY_STMT_FEATURE:
Michal Vasko63f3d842020-07-08 10:10:14 +02004389 LY_CHECK_RET(parse_feature(ctx, in, &submod->features));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004390 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004391 case LY_STMT_GROUPING:
Michal Vasko63f3d842020-07-08 10:10:14 +02004392 LY_CHECK_RET(parse_grouping(ctx, in, NULL, &submod->groupings));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004393 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004394 case LY_STMT_IDENTITY:
Michal Vasko63f3d842020-07-08 10:10:14 +02004395 LY_CHECK_RET(parse_identity(ctx, in, &submod->identities));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004396 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004397 case LY_STMT_NOTIFICATION:
Michal Vasko63f3d842020-07-08 10:10:14 +02004398 LY_CHECK_RET(parse_notif(ctx, in, NULL, &submod->notifs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004399 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004400 case LY_STMT_RPC:
Michal Vasko63f3d842020-07-08 10:10:14 +02004401 LY_CHECK_RET(parse_action(ctx, in, NULL, &submod->rpcs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004402 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004403 case LY_STMT_TYPEDEF:
Michal Vasko63f3d842020-07-08 10:10:14 +02004404 LY_CHECK_RET(parse_typedef(ctx, NULL, in, &submod->typedefs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004405 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004406 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko63f3d842020-07-08 10:10:14 +02004407 LY_CHECK_RET(parse_ext(ctx, in, word, word_len, LYEXT_SUBSTMT_SELF, 0, &submod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004408 break;
4409
4410 default:
David Sedlákb3077192019-06-19 10:55:37 +02004411 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "submodule");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004412 return LY_EVALID;
4413 }
4414 }
4415 LY_CHECK_RET(ret);
4416
4417checks:
4418 /* finalize parent pointers to the reallocated items */
Michal Vaskob36053d2020-03-26 15:49:30 +01004419 LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, submod->groupings, submod->augments,
4420 submod->rpcs, submod->notifs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004421
4422 /* mandatory substatements */
4423 if (!submod->belongsto) {
David Sedlákb3077192019-06-19 10:55:37 +02004424 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "belongs-to", "submodule");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004425 return LY_EVALID;
4426 }
4427
4428 /* submodules share the namespace with the module names, so there must not be
4429 * a submodule of the same name in the context, no need for revision matching */
4430 dup = ly_ctx_get_submodule(ctx->ctx, NULL, submod->name, NULL);
4431 if (dup && strcmp(dup->belongsto, submod->belongsto)) {
David Sedlákb3077192019-06-19 10:55:37 +02004432 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Name collision between submodules of name \"%s\".", dup->name);
Radek Krejcie9e987e2018-10-31 12:50:27 +01004433 return LY_EVALID;
4434 }
4435
Michal Vasko7fbc8162018-09-17 10:35:16 +02004436 return ret;
4437}
4438
Radek Krejcid4557c62018-09-17 11:42:09 +02004439LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01004440yang_parse_submodule(struct lys_yang_parser_ctx **context, struct ly_ctx *ly_ctx, struct lys_parser_ctx *main_ctx,
Radek Krejci0f969882020-08-21 16:56:47 +02004441 struct ly_in *in, struct lysp_submodule **submod)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004442{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004443 LY_ERR ret = LY_SUCCESS;
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004444 char *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004445 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004446 enum ly_stmt kw;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004447 struct lysp_submodule *mod_p = NULL;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004448
David Sedlák1b623122019-08-05 15:27:49 +02004449 /* create context */
4450 *context = calloc(1, sizeof **context);
4451 LY_CHECK_ERR_RET(!(*context), LOGMEM(ly_ctx), LY_EMEM);
Michal Vaskob36053d2020-03-26 15:49:30 +01004452 (*context)->format = LYS_IN_YANG;
Michal Vasko7c8439f2020-08-05 13:25:19 +02004453 (*context)->main_mod = main_ctx->main_mod;
David Sedlák1b623122019-08-05 15:27:49 +02004454 (*context)->ctx = ly_ctx;
Radek Krejci99435242019-09-05 16:19:15 +02004455 (*context)->pos_type = LY_VLOG_LINE;
David Sedlák1b623122019-08-05 15:27:49 +02004456 (*context)->line = 1;
4457
4458 /* map the typedefs and groupings list from main context to the submodule's context */
4459 memcpy(&(*context)->tpdfs_nodes, &main_ctx->tpdfs_nodes, sizeof main_ctx->tpdfs_nodes);
4460 memcpy(&(*context)->grps_nodes, &main_ctx->grps_nodes, sizeof main_ctx->grps_nodes);
4461
Michal Vasko7fbc8162018-09-17 10:35:16 +02004462 /* "module"/"submodule" */
Michal Vasko63f3d842020-07-08 10:10:14 +02004463 ret = get_keyword(*context, in, &kw, &word, &word_len);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004464 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004465
Radek Krejcid6b76452019-09-03 17:03:03 +02004466 if (kw == LY_STMT_MODULE) {
David Sedlák1b623122019-08-05 15:27:49 +02004467 LOGERR((*context)->ctx, LY_EDENIED, "Input data contains module in situation when a submodule is expected.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004468 ret = LY_EINVAL;
4469 goto cleanup;
Radek Krejcid6b76452019-09-03 17:03:03 +02004470 } else if (kw != LY_STMT_SUBMODULE) {
David Sedlák1538a842019-08-08 15:38:51 +02004471 LOGVAL_PARSER(*context, LY_VCODE_MOD_SUBOMD, ly_stmt2str(kw));
Radek Krejci40544fa2019-01-11 09:38:37 +01004472 ret = LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004473 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004474 }
4475
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004476 mod_p = calloc(1, sizeof *mod_p);
David Sedlák1b623122019-08-05 15:27:49 +02004477 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM((*context)->ctx), cleanup);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004478 mod_p->parsing = 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004479
4480 /* substatements */
Michal Vasko63f3d842020-07-08 10:10:14 +02004481 ret = parse_submodule(*context, in, mod_p);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004482 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004483
4484 /* read some trailing spaces or new lines */
Michal Vasko63f3d842020-07-08 10:10:14 +02004485 while (isspace(in->current[0])) {
4486 ++in->current;
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004487 }
Michal Vasko63f3d842020-07-08 10:10:14 +02004488 if (in->current[0]) {
4489 LOGVAL_PARSER(*context, LY_VCODE_TRAILING_SUBMOD, 15, in->current, strlen(in->current) > 15 ? "..." : "");
Radek Krejci40544fa2019-01-11 09:38:37 +01004490 ret = LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004491 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004492 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004493
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004494 mod_p->parsing = 0;
4495 *submod = mod_p;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004496
Radek Krejcibbe09a92018-11-08 09:36:54 +01004497cleanup:
4498 if (ret) {
David Sedlák1b623122019-08-05 15:27:49 +02004499 lysp_submodule_free((*context)->ctx, mod_p);
Michal Vaskob36053d2020-03-26 15:49:30 +01004500 yang_parser_ctx_free(*context);
David Sedlák1b623122019-08-05 15:27:49 +02004501 *context = NULL;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004502 }
4503
4504 return ret;
4505}
4506
4507LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02004508yang_parse_module(struct lys_yang_parser_ctx **context, struct ly_in *in, struct lys_module *mod)
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004509{
4510 LY_ERR ret = LY_SUCCESS;
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004511 char *word;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004512 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004513 enum ly_stmt kw;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004514 struct lysp_module *mod_p = NULL;
4515
David Sedlák1b623122019-08-05 15:27:49 +02004516 /* create context */
4517 *context = calloc(1, sizeof **context);
4518 LY_CHECK_ERR_RET(!(*context), LOGMEM(mod->ctx), LY_EMEM);
Michal Vaskob36053d2020-03-26 15:49:30 +01004519 (*context)->format = LYS_IN_YANG;
Michal Vasko7c8439f2020-08-05 13:25:19 +02004520 (*context)->main_mod = mod;
David Sedlák1b623122019-08-05 15:27:49 +02004521 (*context)->ctx = mod->ctx;
Radek Krejci335332a2019-09-05 13:03:35 +02004522 (*context)->pos_type = LY_VLOG_LINE;
David Sedlák1b623122019-08-05 15:27:49 +02004523 (*context)->line = 1;
4524
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004525 /* "module"/"submodule" */
Michal Vasko63f3d842020-07-08 10:10:14 +02004526 ret = get_keyword(*context, in, &kw, &word, &word_len);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004527 LY_CHECK_GOTO(ret, cleanup);
4528
Radek Krejcid6b76452019-09-03 17:03:03 +02004529 if (kw == LY_STMT_SUBMODULE) {
David Sedlák1b623122019-08-05 15:27:49 +02004530 LOGERR((*context)->ctx, LY_EDENIED, "Input data contains submodule which cannot be parsed directly without its main module.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004531 ret = LY_EINVAL;
4532 goto cleanup;
Radek Krejcid6b76452019-09-03 17:03:03 +02004533 } else if (kw != LY_STMT_MODULE) {
David Sedlák1538a842019-08-08 15:38:51 +02004534 LOGVAL_PARSER((*context), LY_VCODE_MOD_SUBOMD, ly_stmt2str(kw));
Radek Krejci40544fa2019-01-11 09:38:37 +01004535 ret = LY_EVALID;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004536 goto cleanup;
4537 }
4538
4539 mod_p = calloc(1, sizeof *mod_p);
David Sedlák1b623122019-08-05 15:27:49 +02004540 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM((*context)->ctx), cleanup);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004541 mod_p->mod = mod;
4542 mod_p->parsing = 1;
4543
4544 /* substatements */
Michal Vasko63f3d842020-07-08 10:10:14 +02004545 ret = parse_module(*context, in, mod_p);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004546 LY_CHECK_GOTO(ret, cleanup);
4547
4548 /* read some trailing spaces or new lines */
Michal Vasko63f3d842020-07-08 10:10:14 +02004549 while (isspace(in->current[0])) {
4550 ++in->current;
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004551 }
Michal Vasko63f3d842020-07-08 10:10:14 +02004552 if (in->current[0]) {
4553 LOGVAL_PARSER(*context, LY_VCODE_TRAILING_MOD, 15, in->current, strlen(in->current) > 15 ? "..." : "");
Radek Krejci40544fa2019-01-11 09:38:37 +01004554 ret = LY_EVALID;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004555 goto cleanup;
4556 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004557
4558 mod_p->parsing = 0;
4559 mod->parsed = mod_p;
4560
4561cleanup:
4562 if (ret) {
4563 lysp_module_free(mod_p);
Michal Vaskob36053d2020-03-26 15:49:30 +01004564 yang_parser_ctx_free(*context);
David Sedlák1b623122019-08-05 15:27:49 +02004565 *context = NULL;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004566 }
4567
Michal Vasko7fbc8162018-09-17 10:35:16 +02004568 return ret;
4569}