blob: df7886616f74b418cd0ed40c2ba071d7f3e78556 [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 Vasko7fbc8162018-09-17 10:35:16 +020014
Radek Krejcie7b95092019-05-15 11:03:07 +020015#include <assert.h>
16#include <ctype.h>
17#include <errno.h>
18#include <stdint.h>
19#include <stdio.h>
20#include <stdlib.h>
21#include <string.h>
22
Radek Krejci535ea9f2020-05-29 16:01:05 +020023#include "common.h"
Michal Vasko7fbc8162018-09-17 10:35:16 +020024#include "context.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020025#include "dict.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020026#include "log.h"
Michal Vasko004d3152020-06-11 19:59:22 +020027#include "path.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020028#include "set.h"
29#include "tree.h"
30#include "tree_schema.h"
Radek Krejci70853c52018-10-15 14:46:16 +020031#include "tree_schema_internal.h"
Radek Krejci44ceedc2018-10-02 15:54:31 +020032
Radek Krejciceaf2122019-01-02 15:03:26 +010033/**
34 * @brief Insert WORD into the libyang context's dictionary and store as TARGET.
35 * @param[in] CTX yang parser context to access libyang context.
36 * @param[in] BUF buffer in case the word is not a constant and can be inserted directly (zero-copy)
37 * @param[out] TARGET variable where to store the pointer to the inserted value.
38 * @param[in] WORD string to store.
39 * @param[in] LEN length of the string in WORD to store.
40 */
Radek Krejci9fcacc12018-10-11 15:59:11 +020041#define INSERT_WORD(CTX, BUF, TARGET, WORD, LEN) \
42 if (BUF) {(TARGET) = lydict_insert_zc((CTX)->ctx, WORD);}\
Radek Krejcif09e4e82019-06-14 15:08:11 +020043 else {(TARGET) = lydict_insert((CTX)->ctx, LEN ? WORD : "", LEN);}
Radek Krejci44ceedc2018-10-02 15:54:31 +020044
Radek Krejciceaf2122019-01-02 15:03:26 +010045/**
46 * @brief Move the DATA pointer by COUNT items. Also updates the indent value in yang parser context
47 * @param[in] CTX yang parser context to update its indent value.
48 * @param[in,out] DATA pointer to move
49 * @param[in] COUNT number of items for which the DATA pointer is supposed to move on.
50 */
Radek Krejci2b610482019-01-02 13:36:09 +010051#define MOVE_INPUT(CTX, DATA, COUNT) (*(DATA))+=COUNT;(CTX)->indent+=COUNT
Radek Krejcid5f2b5f2018-10-11 10:54:36 +020052
Michal Vaskoea5abea2018-09-18 13:10:54 +020053/**
54 * @brief Loop through all substatements providing, return if there are none.
55 *
Radek Krejci44ceedc2018-10-02 15:54:31 +020056 * @param[in] CTX yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +020057 * @param[in] DATA Raw data to read from.
58 * @param[out] KW YANG keyword read.
59 * @param[out] WORD Pointer to the keyword itself.
60 * @param[out] WORD_LEN Length of the keyword.
61 * @param[out] ERR Variable for error storing.
62 *
63 * @return In case there are no substatements or a fatal error encountered.
64 */
Radek Krejci6d6556c2018-11-08 09:37:45 +010065#define YANG_READ_SUBSTMT_FOR(CTX, DATA, KW, WORD, WORD_LEN, ERR, CHECKGOTO) \
Radek Krejci6d9b9b52018-11-02 12:43:39 +010066 LY_CHECK_RET(get_keyword(CTX, DATA, &KW, &WORD, &WORD_LEN)); \
Radek Krejcid6b76452019-09-03 17:03:03 +020067 if (KW == LY_STMT_SYNTAX_SEMICOLON) { \
Radek Krejci6d6556c2018-11-08 09:37:45 +010068 CHECKGOTO; \
Radek Krejci6d9b9b52018-11-02 12:43:39 +010069 return LY_SUCCESS; \
Michal Vasko7fbc8162018-09-17 10:35:16 +020070 } \
Radek Krejcid6b76452019-09-03 17:03:03 +020071 if (KW != LY_STMT_SYNTAX_LEFT_BRACE) { \
David Sedlákb3077192019-06-19 10:55:37 +020072 LOGVAL_PARSER(CTX, LYVE_SYNTAX_YANG, "Invalid keyword \"%s\", expected \";\" or \"{\".", ly_stmt2str(KW)); \
Michal Vasko7fbc8162018-09-17 10:35:16 +020073 return LY_EVALID; \
74 } \
75 for (ERR = get_keyword(CTX, DATA, &KW, &WORD, &WORD_LEN); \
Radek Krejcid6b76452019-09-03 17:03:03 +020076 !ERR && (KW != LY_STMT_SYNTAX_RIGHT_BRACE); \
Michal Vasko7fbc8162018-09-17 10:35:16 +020077 ERR = get_keyword(CTX, DATA, &KW, &WORD, &WORD_LEN))
78
Michal Vaskob36053d2020-03-26 15:49:30 +010079LY_ERR parse_container(struct lys_yang_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings);
80LY_ERR parse_uses(struct lys_yang_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings);
81LY_ERR parse_choice(struct lys_yang_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings);
82LY_ERR parse_case(struct lys_yang_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings);
83LY_ERR parse_list(struct lys_yang_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings);
84LY_ERR parse_grouping(struct lys_yang_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_grp **groupings);
Michal Vasko7fbc8162018-09-17 10:35:16 +020085
Michal Vaskoea5abea2018-09-18 13:10:54 +020086/**
87 * @brief Add another character to dynamic buffer, a low-level function.
88 *
Radek Krejci44ceedc2018-10-02 15:54:31 +020089 * Enlarge if needed. Updates \p input as well as \p buf_used.
Michal Vaskoea5abea2018-09-18 13:10:54 +020090 *
Radek Krejci404251e2018-10-09 12:06:44 +020091 * @param[in] ctx libyang context for logging.
Radek Krejci44ceedc2018-10-02 15:54:31 +020092 * @param[in, out] input Input string to process.
93 * @param[in] len Number of bytes to get from the input string and copy into the buffer.
Michal Vaskoea5abea2018-09-18 13:10:54 +020094 * @param[in,out] buf Buffer to use, can be moved by realloc().
95 * @param[in,out] buf_len Current size of the buffer.
Radek Krejci44ceedc2018-10-02 15:54:31 +020096 * @param[in,out] buf_used Currently used characters of the buffer.
Michal Vaskoea5abea2018-09-18 13:10:54 +020097 *
98 * @return LY_ERR values.
99 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200100LY_ERR
Radek Krejci44ceedc2018-10-02 15:54:31 +0200101buf_add_char(struct ly_ctx *ctx, const char **input, size_t len, char **buf, size_t *buf_len, size_t *buf_used)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200102{
Radek Krejci44ceedc2018-10-02 15:54:31 +0200103 if (*buf_len <= (*buf_used) + len) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200104 *buf_len += 16;
105 *buf = ly_realloc(*buf, *buf_len);
106 LY_CHECK_ERR_RET(!*buf, LOGMEM(ctx), LY_EMEM);
107 }
Radek Krejcic0917392019-04-10 13:04:04 +0200108 if (*buf_used) {
109 memcpy(&(*buf)[*buf_used], *input, len);
110 } else {
111 memcpy(*buf, *input, len);
112 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200113
Radek Krejci44ceedc2018-10-02 15:54:31 +0200114 (*buf_used) += len;
115 (*input) += len;
116
Michal Vasko7fbc8162018-09-17 10:35:16 +0200117 return LY_SUCCESS;
118}
119
Michal Vaskoea5abea2018-09-18 13:10:54 +0200120/**
Radek Krejci44ceedc2018-10-02 15:54:31 +0200121 * @brief Store a single UTF8 character. It depends whether in a dynamically-allocated buffer or just as a pointer to the data.
122 *
123 * @param[in] ctx yang parser context for logging.
124 * @param[in,out] input Pointer to the string where to get the character to store. Automatically moved to the next character
125 * when function returns.
126 * @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 Vaskob36053d2020-03-26 15:49:30 +0100141buf_store_char(struct lys_yang_parser_ctx *ctx, const char **input, enum yang_arg arg, char **word_p,
David Sedlák40bb13b2019-07-10 14:34:18 +0200142 size_t *word_len, char **word_b, size_t *buf_len, int need_buf, int *prefix)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200143{
Radek Krejci44ceedc2018-10-02 15:54:31 +0200144 unsigned int c;
145 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) */
151 LY_CHECK_ERR_RET(ly_getutf8(input, &c, &len),
David Sedlákb3077192019-06-19 10:55:37 +0200152 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, (*input)[-len]), LY_EVALID);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200153 (*input) -= 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 */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200177 if (buf_add_char(ctx->ctx, input, 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 */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200193 if (buf_add_char(ctx->ctx, input, 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) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200202 *word_p = (char *)(*input);
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;
206 (*input) += 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.
216 * @param[in,out] data Data to read from, automatically moved after the comment.
217 * @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 *
221 * @return LY_ERR values.
222 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200223LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100224skip_comment(struct lys_yang_parser_ctx *ctx, const char **data, int comment)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200225{
Radek Krejci80dd33e2018-09-26 15:57:18 +0200226 /* internal statuses: 0 - comment ended,
227 * 1 - in line comment,
228 * 2 - in block comment,
229 * 3 - in block comment with last read character '*'
230 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200231 while (**data && comment) {
232 switch (comment) {
233 case 1:
234 if (**data == '\n') {
235 comment = 0;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200236 ++ctx->line;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200237 }
238 break;
239 case 2:
240 if (**data == '*') {
Radek Krejci15c80ca2018-10-09 11:01:31 +0200241 comment = 3;
242 } else if (**data == '\n') {
243 ++ctx->line;
244 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200245 break;
246 case 3:
247 if (**data == '/') {
248 comment = 0;
Radek Krejci5b930492019-06-11 14:54:08 +0200249 } else if (**data != '*') {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200250 if (**data == '\n') {
251 ++ctx->line;
252 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200253 comment = 2;
254 }
255 break;
256 default:
Radek Krejci44ceedc2018-10-02 15:54:31 +0200257 LOGINT_RET(ctx->ctx);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200258 }
259
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200260 if (**data == '\n') {
261 ctx->indent = 0;
262 } else {
263 ++ctx->indent;
264 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200265 ++(*data);
266 }
267
268 if (!**data && (comment > 1)) {
David Sedlákb3077192019-06-19 10:55:37 +0200269 LOGVAL_PARSER(ctx, LYVE_SYNTAX, "Unexpected end-of-input, non-terminated comment.");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200270 return LY_EVALID;
271 }
272
273 return LY_SUCCESS;
274}
275
Michal Vaskoea5abea2018-09-18 13:10:54 +0200276/**
277 * @brief Read a quoted string from data.
278 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200279 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200280 * @param[in,out] data Data to read from, always moved to currently handled character.
281 * @param[in] arg Type of YANG keyword argument expected.
282 * @param[out] word_p Pointer to the read quoted string.
283 * @param[out] word_b Pointer to a dynamically-allocated buffer holding the read quoted string. If not needed,
284 * set to NULL. Otherwise equal to \p word_p.
285 * @param[out] word_len Length of the read quoted string.
286 * @param[out] buf_len Length of the dynamically-allocated buffer \p word_b.
287 * @param[in] indent Current indent (number of YANG spaces). Needed for correct multi-line string
288 * indenation in the final quoted string.
289 *
290 * @return LY_ERR values.
291 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200292static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100293read_qstring(struct lys_yang_parser_ctx *ctx, const char **data, enum yang_arg arg, char **word_p, char **word_b,
294 size_t *word_len, size_t *buf_len)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200295{
296 /* string: 0 - string ended, 1 - string with ', 2 - string with ", 3 - string with " with last character \,
297 * 4 - string finished, now skipping whitespaces looking for +,
298 * 5 - string continues after +, skipping whitespaces */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200299 unsigned int string, block_indent = 0, current_indent = 0, need_buf = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200300 const char *c;
David Sedlák40bb13b2019-07-10 14:34:18 +0200301 int prefix = 0;
Michal Vasko90edde42019-11-25 15:25:07 +0100302 unsigned int trailing_ws = 0; /* current number of stored trailing whitespace characters */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200303
304 if (**data == '\"') {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200305 string = 2;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200306 current_indent = block_indent = ctx->indent + 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200307 } else {
308 assert(**data == '\'');
309 string = 1;
310 }
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200311 MOVE_INPUT(ctx, data, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200312
313 while (**data && string) {
314 switch (string) {
315 case 1:
316 switch (**data) {
317 case '\'':
318 /* string may be finished, but check for + */
319 string = 4;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200320 MOVE_INPUT(ctx, data, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200321 break;
322 default:
323 /* check and store character */
David Sedlák40bb13b2019-07-10 14:34:18 +0200324 LY_CHECK_RET(buf_store_char(ctx, data, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200325 break;
326 }
327 break;
328 case 2:
329 switch (**data) {
330 case '\"':
331 /* string may be finished, but check for + */
332 string = 4;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200333 MOVE_INPUT(ctx, data, 1);
Radek Krejciff13cd12019-10-25 15:34:24 +0200334 trailing_ws = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200335 break;
336 case '\\':
337 /* special character following */
338 string = 3;
Radek Krejciff13cd12019-10-25 15:34:24 +0200339
340 /* the backslash sequence is substituted, so we will need a buffer to store the result */
341 need_buf = 1;
342
343 /* move forward to the escaped character */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200344 ++(*data);
Radek Krejciff13cd12019-10-25 15:34:24 +0200345
346 /* note that the trailing whitespaces are supposed to be trimmed before substitution of
347 * backslash-escaped characters (RFC 7950, 6.1.3), so we have to zero the trailing whitespaces counter */
348 trailing_ws = 0;
349
350 /* since the backslash-escaped character is handled as first non-whitespace character, stop eating indentation */
351 current_indent = block_indent;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200352 break;
353 case ' ':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200354 if (current_indent < block_indent) {
355 ++current_indent;
356 MOVE_INPUT(ctx, data, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200357 } else {
Michal Vasko90edde42019-11-25 15:25:07 +0100358 /* check and store whitespace character */
David Sedlák40bb13b2019-07-10 14:34:18 +0200359 LY_CHECK_RET(buf_store_char(ctx, data, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
Michal Vasko90edde42019-11-25 15:25:07 +0100360 trailing_ws++;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200361 }
362 break;
363 case '\t':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200364 if (current_indent < block_indent) {
365 assert(need_buf);
366 current_indent += 8;
367 ctx->indent += 8;
368 for (; current_indent > block_indent; --current_indent, --ctx->indent) {
369 /* store leftover spaces from the tab */
370 c = " ";
David Sedlák40bb13b2019-07-10 14:34:18 +0200371 LY_CHECK_RET(buf_store_char(ctx, &c, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
Michal Vasko90edde42019-11-25 15:25:07 +0100372 trailing_ws++;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200373 }
Radek Krejci44ceedc2018-10-02 15:54:31 +0200374 ++(*data);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200375 } else {
Michal Vasko90edde42019-11-25 15:25:07 +0100376 /* check and store whitespace character */
David Sedlák40bb13b2019-07-10 14:34:18 +0200377 LY_CHECK_RET(buf_store_char(ctx, data, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
Michal Vasko90edde42019-11-25 15:25:07 +0100378 trailing_ws++;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200379 /* additional characters for indentation - only 1 was count in buf_store_char */
380 ctx->indent += 7;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200381 }
382 break;
383 case '\n':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200384 if (block_indent) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200385 /* we will be removing the indents so we need our own buffer */
386 need_buf = 1;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200387
388 /* remove trailing tabs and spaces */
Radek Krejciff13cd12019-10-25 15:34:24 +0200389 (*word_len) = *word_len - trailing_ws;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200390
Radek Krejciff13cd12019-10-25 15:34:24 +0200391 /* restart indentation */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200392 current_indent = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200393 }
394
395 /* check and store character */
David Sedlák40bb13b2019-07-10 14:34:18 +0200396 LY_CHECK_RET(buf_store_char(ctx, data, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200397
398 /* maintain line number */
399 ++ctx->line;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200400
401 /* reset context indentation counter for possible string after this one */
402 ctx->indent = 0;
Radek Krejciff13cd12019-10-25 15:34:24 +0200403 trailing_ws = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200404 break;
405 default:
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200406 /* first non-whitespace character, stop eating indentation */
407 current_indent = block_indent;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200408
409 /* check and store character */
David Sedlák40bb13b2019-07-10 14:34:18 +0200410 LY_CHECK_RET(buf_store_char(ctx, data, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
Radek Krejciff13cd12019-10-25 15:34:24 +0200411 trailing_ws = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200412 break;
413 }
414 break;
415 case 3:
416 /* string encoded characters */
417 switch (**data) {
418 case 'n':
419 c = "\n";
420 break;
421 case 't':
422 c = "\t";
423 break;
424 case '\"':
425 c = *data;
426 break;
427 case '\\':
428 c = *data;
429 break;
430 default:
David Sedlákb3077192019-06-19 10:55:37 +0200431 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Double-quoted string unknown special character '\\%c'.", **data);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200432 return LY_EVALID;
433 }
434
435 /* check and store character */
David Sedlák40bb13b2019-07-10 14:34:18 +0200436 LY_CHECK_RET(buf_store_char(ctx, &c, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200437
438 string = 2;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200439 ++(*data);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200440 break;
441 case 4:
442 switch (**data) {
443 case '+':
444 /* string continues */
445 string = 5;
Radek Krejciefd22f62018-09-27 11:47:58 +0200446 need_buf = 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200447 break;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200448 case '\n':
449 ++ctx->line;
450 /* fallthrough */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200451 case ' ':
452 case '\t':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200453 /* just skip */
454 break;
455 default:
456 /* string is finished */
457 goto string_end;
458 }
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200459 MOVE_INPUT(ctx, data, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200460 break;
461 case 5:
462 switch (**data) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200463 case '\n':
464 ++ctx->line;
465 /* fallthrough */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200466 case ' ':
467 case '\t':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200468 /* skip */
469 break;
470 case '\'':
471 string = 1;
472 break;
473 case '\"':
474 string = 2;
475 break;
476 default:
477 /* it must be quoted again */
David Sedlákb3077192019-06-19 10:55:37 +0200478 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Both string parts divided by '+' must be quoted.");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200479 return LY_EVALID;
480 }
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200481 MOVE_INPUT(ctx, data, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200482 break;
483 default:
484 return LY_EINT;
485 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200486 }
487
488string_end:
Radek Krejci4e199f52019-05-28 09:09:28 +0200489 if (arg <= Y_PREF_IDENTIF_ARG && !(*word_len)) {
490 /* empty identifier */
David Sedlákb3077192019-06-19 10:55:37 +0200491 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Statement argument is required.");
Radek Krejci4e199f52019-05-28 09:09:28 +0200492 return LY_EVALID;
493 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200494 return LY_SUCCESS;
495}
496
Michal Vaskoea5abea2018-09-18 13:10:54 +0200497/**
498 * @brief Get another YANG string from the raw data.
499 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200500 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200501 * @param[in,out] data Data to read from, always moved to currently handled character.
502 * @param[in] arg Type of YANG keyword argument expected.
Radek Krejcid3ca0632019-04-16 16:54:54 +0200503 * @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 +0200504 * @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 +0200505 * @param[out] word_b Pointer to a dynamically-allocated buffer holding the read string. If not needed,
506 * set to NULL. Otherwise equal to \p word_p.
507 * @param[out] word_len Length of the read string.
508 *
509 * @return LY_ERR values.
Michal Vasko7fbc8162018-09-17 10:35:16 +0200510 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200511LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100512get_argument(struct lys_yang_parser_ctx *ctx, const char **data, enum yang_arg arg,
Radek Krejcid3ca0632019-04-16 16:54:54 +0200513 uint16_t *flags, char **word_p, char **word_b, size_t *word_len)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200514{
Radek Krejci44ceedc2018-10-02 15:54:31 +0200515 size_t buf_len = 0;
David Sedlák40bb13b2019-07-10 14:34:18 +0200516 int prefix = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200517 /* word buffer - dynamically allocated */
518 *word_b = NULL;
519
520 /* word pointer - just a pointer to data */
521 *word_p = NULL;
522
523 *word_len = 0;
524 while (**data) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200525 switch (**data) {
526 case '\'':
527 case '\"':
528 if (*word_len) {
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200529 /* invalid - quotes cannot be in unquoted string and only optsep, ; or { can follow it */
David Sedlákb3077192019-06-19 10:55:37 +0200530 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, 1, *data,
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200531 "unquoted string character, optsep, semicolon or opening brace");
532 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200533 }
Radek Krejcid3ca0632019-04-16 16:54:54 +0200534 if (flags) {
535 (*flags) |= (**data) == '\'' ? LYS_SINGLEQUOTED : LYS_DOUBLEQUOTED;
536 }
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100537 LY_CHECK_RET(read_qstring(ctx, data, arg, word_p, word_b, word_len, &buf_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200538 goto str_end;
539 case '/':
Radek Krejci44ceedc2018-10-02 15:54:31 +0200540 if ((*data)[1] == '/') {
541 /* one-line comment */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200542 MOVE_INPUT(ctx, data, 2);
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100543 LY_CHECK_RET(skip_comment(ctx, data, 1));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200544 } else if ((*data)[1] == '*') {
545 /* block comment */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200546 MOVE_INPUT(ctx, data, 2);
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100547 LY_CHECK_RET(skip_comment(ctx, data, 2));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200548 } else {
549 /* not a comment after all */
David Sedlák40bb13b2019-07-10 14:34:18 +0200550 LY_CHECK_RET(buf_store_char(ctx, data, arg, word_p, word_len, word_b, &buf_len, 0, &prefix));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200551 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200552 break;
553 case ' ':
554 if (*word_len) {
555 /* word is finished */
556 goto str_end;
557 }
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200558 MOVE_INPUT(ctx, data, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200559 break;
560 case '\t':
561 if (*word_len) {
562 /* word is finished */
563 goto str_end;
564 }
565 /* tabs count for 8 spaces */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200566 ctx->indent += 8;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200567
568 ++(*data);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200569 break;
570 case '\n':
571 if (*word_len) {
572 /* word is finished */
573 goto str_end;
574 }
575 /* reset indent */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200576 ctx->indent = 0;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200577
578 /* track line numbers */
579 ++ctx->line;
580
581 ++(*data);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200582 break;
583 case ';':
584 case '{':
585 if (*word_len || (arg == Y_MAYBE_STR_ARG)) {
586 /* word is finished */
587 goto str_end;
588 }
589
David Sedlákb3077192019-06-19 10:55:37 +0200590 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, 1, *data, "an argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200591 return LY_EVALID;
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200592 case '}':
593 /* invalid - braces cannot be in unquoted string (opening braces terminates the string and can follow it) */
David Sedlákb3077192019-06-19 10:55:37 +0200594 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, 1, *data,
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200595 "unquoted string character, optsep, semicolon or opening brace");
596 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200597 default:
David Sedlák40bb13b2019-07-10 14:34:18 +0200598 LY_CHECK_RET(buf_store_char(ctx, data, arg, word_p, word_len, word_b, &buf_len, 0, &prefix));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200599 break;
600 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200601 }
602
Radek Krejci0a1d0d42019-05-16 15:14:51 +0200603 /* unexpected end of loop */
David Sedlákb3077192019-06-19 10:55:37 +0200604 LOGVAL_PARSER(ctx, LY_VCODE_EOF);
Radek Krejci0a1d0d42019-05-16 15:14:51 +0200605 return LY_EVALID;
606
Michal Vasko7fbc8162018-09-17 10:35:16 +0200607str_end:
Radek Krejci44ceedc2018-10-02 15:54:31 +0200608 /* terminating NULL byte for buf */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200609 if (*word_b) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200610 (*word_b) = ly_realloc(*word_b, (*word_len) + 1);
611 LY_CHECK_ERR_RET(!(*word_b), LOGMEM(ctx->ctx), LY_EMEM);
612 (*word_b)[*word_len] = '\0';
Michal Vasko7fbc8162018-09-17 10:35:16 +0200613 *word_p = *word_b;
614 }
615
616 return LY_SUCCESS;
617}
618
Michal Vaskoea5abea2018-09-18 13:10:54 +0200619/**
620 * @brief Get another YANG keyword from the raw data.
621 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200622 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200623 * @param[in,out] data Data to read from, always moved to currently handled character.
624 * @param[out] kw YANG keyword read.
625 * @param[out] word_p Pointer to the keyword in the data. Useful for extension instances.
626 * @param[out] word_len Length of the keyword in the data. Useful for extension instances.
627 *
628 * @return LY_ERR values.
629 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200630LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100631get_keyword(struct lys_yang_parser_ctx *ctx, const char **data, enum ly_stmt *kw, char **word_p, size_t *word_len)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200632{
Michal Vasko7fbc8162018-09-17 10:35:16 +0200633 int prefix;
634 const char *word_start;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200635 unsigned int c;
636 size_t len;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200637
638 if (word_p) {
639 *word_p = NULL;
640 *word_len = 0;
641 }
642
643 /* first skip "optsep", comments */
644 while (**data) {
Radek Krejcidcc7b322018-10-11 14:24:02 +0200645 switch (**data) {
646 case '/':
647 if ((*data)[1] == '/') {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200648 /* one-line comment */
Radek Krejcidcc7b322018-10-11 14:24:02 +0200649 MOVE_INPUT(ctx, data, 2);
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100650 LY_CHECK_RET(skip_comment(ctx, data, 1));
Radek Krejcidcc7b322018-10-11 14:24:02 +0200651 } else if ((*data)[1] == '*') {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200652 /* block comment */
Radek Krejcidcc7b322018-10-11 14:24:02 +0200653 MOVE_INPUT(ctx, data, 2);
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100654 LY_CHECK_RET(skip_comment(ctx, data, 2));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200655 } else {
Radek Krejcidcc7b322018-10-11 14:24:02 +0200656 /* error - not a comment after all, keyword cannot start with slash */
David Sedlákb3077192019-06-19 10:55:37 +0200657 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid identifier first character '/'.");
Radek Krejcidcc7b322018-10-11 14:24:02 +0200658 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200659 }
Radek Krejci13028282019-06-11 14:56:48 +0200660 continue;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200661 case '\n':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200662 /* skip whitespaces (optsep) */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200663 ++ctx->line;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200664 ctx->indent = 0;
665 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200666 case ' ':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200667 /* skip whitespaces (optsep) */
668 ++ctx->indent;
669 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200670 case '\t':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200671 /* skip whitespaces (optsep) */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200672 ctx->indent += 8;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200673 break;
674 default:
675 /* either a keyword start or an invalid character */
676 goto keyword_start;
677 }
678
679 ++(*data);
680 }
681
682keyword_start:
683 word_start = *data;
David Sedlák5f8f0332019-06-18 16:34:30 +0200684 *kw = lysp_match_kw(ctx, data);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200685
Radek Krejcid6b76452019-09-03 17:03:03 +0200686 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 +0200687 goto success;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200688 }
689
Radek Krejcid6b76452019-09-03 17:03:03 +0200690 if (*kw != LY_STMT_NONE) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200691 /* make sure we have the whole keyword */
692 switch (**data) {
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200693 case '\n':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200694 case '\t':
Radek Krejciabdd8062019-06-11 16:44:19 +0200695 case ' ':
696 /* mandatory "sep" is just checked, not eaten so nothing in the context is updated */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200697 break;
Radek Krejci156ccaf2018-10-15 15:49:17 +0200698 case ':':
699 /* keyword is not actually a keyword, but prefix of an extension.
700 * To avoid repeated check of the prefix syntax, move to the point where the colon was read
701 * and we will be checking the keyword (extension instance) itself */
702 prefix = 1;
703 MOVE_INPUT(ctx, data, 1);
704 goto extension;
Radek Krejcidcc7b322018-10-11 14:24:02 +0200705 case '{':
706 /* allowed only for input and output statements which can be without arguments */
Radek Krejcid6b76452019-09-03 17:03:03 +0200707 if (*kw == LY_STMT_INPUT || *kw == LY_STMT_OUTPUT) {
Radek Krejcidcc7b322018-10-11 14:24:02 +0200708 break;
709 }
710 /* fallthrough */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200711 default:
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200712 MOVE_INPUT(ctx, data, 1);
David Sedlákb3077192019-06-19 10:55:37 +0200713 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, (int)(*data - word_start), word_start,
Radek Krejci44ceedc2018-10-02 15:54:31 +0200714 "a keyword followed by a separator");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200715 return LY_EVALID;
716 }
717 } else {
718 /* still can be an extension */
719 prefix = 0;
Radek Krejci156ccaf2018-10-15 15:49:17 +0200720extension:
Michal Vasko7fbc8162018-09-17 10:35:16 +0200721 while (**data && (**data != ' ') && (**data != '\t') && (**data != '\n') && (**data != '{') && (**data != ';')) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200722 LY_CHECK_ERR_RET(ly_getutf8(data, &c, &len),
David Sedlákb3077192019-06-19 10:55:37 +0200723 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, (*data)[-len]), LY_EVALID);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200724 ++ctx->indent;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200725 /* check character validity */
Michal Vaskob36053d2020-03-26 15:49:30 +0100726 LY_CHECK_RET(lysp_check_identifierchar((struct lys_parser_ctx *)ctx, c, *data - len == word_start ? 1 : 0, &prefix));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200727 }
728 if (!**data) {
David Sedlákb3077192019-06-19 10:55:37 +0200729 LOGVAL_PARSER(ctx, LY_VCODE_EOF);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200730 return LY_EVALID;
731 }
732
733 /* prefix is mandatory for extension instances */
Radek Krejcidcc7b322018-10-11 14:24:02 +0200734 if (prefix != 2) {
David Sedlákb3077192019-06-19 10:55:37 +0200735 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, (int)(*data - word_start), word_start, "a keyword");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200736 return LY_EVALID;
737 }
738
Radek Krejcid6b76452019-09-03 17:03:03 +0200739 *kw = LY_STMT_EXTENSION_INSTANCE;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200740 }
Radek Krejci626df482018-10-11 15:06:31 +0200741success:
Michal Vasko7fbc8162018-09-17 10:35:16 +0200742 if (word_p) {
743 *word_p = (char *)word_start;
744 *word_len = *data - word_start;
745 }
746
747 return LY_SUCCESS;
748}
749
Michal Vaskoea5abea2018-09-18 13:10:54 +0200750/**
751 * @brief Parse extension instance substatements.
752 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200753 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200754 * @param[in,out] data Data to read from, always moved to currently handled character.
Radek Krejci335332a2019-09-05 13:03:35 +0200755 * @param[in] kw Statement keyword value matching @p word value.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200756 * @param[in] word Extension instance substatement name (keyword).
757 * @param[in] word_len Extension instance substatement name length.
758 * @param[in,out] child Children of this extension instance to add to.
759 *
760 * @return LY_ERR values.
761 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200762static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100763parse_ext_substmt(struct lys_yang_parser_ctx *ctx, const char **data, enum ly_stmt kw, char *word, size_t word_len,
Michal Vasko7fbc8162018-09-17 10:35:16 +0200764 struct lysp_stmt **child)
765{
766 char *buf;
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100767 LY_ERR ret = LY_SUCCESS;
Radek Krejci335332a2019-09-05 13:03:35 +0200768 enum ly_stmt child_kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200769 struct lysp_stmt *stmt, *par_child;
770
771 stmt = calloc(1, sizeof *stmt);
772 LY_CHECK_ERR_RET(!stmt, LOGMEM(NULL), LY_EMEM);
773
Radek Krejcibb9b1982019-04-08 14:24:59 +0200774 /* insert into parent statements */
775 if (!*child) {
776 *child = stmt;
777 } else {
778 for (par_child = *child; par_child->next; par_child = par_child->next);
779 par_child->next = stmt;
780 }
781
Radek Krejci44ceedc2018-10-02 15:54:31 +0200782 stmt->stmt = lydict_insert(ctx->ctx, word, word_len);
Radek Krejci335332a2019-09-05 13:03:35 +0200783 stmt->kw = kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200784
785 /* get optional argument */
Radek Krejcid3ca0632019-04-16 16:54:54 +0200786 LY_CHECK_RET(get_argument(ctx, data, Y_MAYBE_STR_ARG, &stmt->flags, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200787
Radek Krejci0ae092d2018-09-20 16:43:19 +0200788 if (word) {
789 if (buf) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200790 stmt->arg = lydict_insert_zc(ctx->ctx, word);
Radek Krejci0ae092d2018-09-20 16:43:19 +0200791 } else {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200792 stmt->arg = lydict_insert(ctx->ctx, word, word_len);
Radek Krejci0ae092d2018-09-20 16:43:19 +0200793 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200794 }
795
Radek Krejci335332a2019-09-05 13:03:35 +0200796 YANG_READ_SUBSTMT_FOR(ctx, data, child_kw, word, word_len, ret, ) {
797 LY_CHECK_RET(parse_ext_substmt(ctx, data, child_kw, word, word_len, &stmt->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200798 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200799 return ret;
800}
801
Michal Vaskoea5abea2018-09-18 13:10:54 +0200802/**
803 * @brief Parse extension instance.
804 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200805 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200806 * @param[in,out] data Data to read from, always moved to currently handled character.
807 * @param[in] ext_name Extension instance substatement name (keyword).
808 * @param[in] ext_name_len Extension instance substatement name length.
809 * @param[in] insubstmt Type of the keyword this extension instance is a substatement of.
810 * @param[in] insubstmt_index Index of the keyword instance this extension instance is a substatement of.
811 * @param[in,out] exts Extension instances to add to.
812 *
813 * @return LY_ERR values.
814 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200815static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100816parse_ext(struct lys_yang_parser_ctx *ctx, const char **data, const char *ext_name, int ext_name_len, LYEXT_SUBSTMT insubstmt,
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200817 LY_ARRAY_SIZE_TYPE insubstmt_index, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200818{
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100819 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200820 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +0200821 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200822 struct lysp_ext_instance *e;
Radek Krejcid6b76452019-09-03 17:03:03 +0200823 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200824
Radek Krejci2c4e7172018-10-19 15:56:26 +0200825 LY_ARRAY_NEW_RET(ctx->ctx, *exts, e, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200826
827 /* store name and insubstmt info */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200828 e->name = lydict_insert(ctx->ctx, ext_name, ext_name_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200829 e->insubstmt = insubstmt;
830 e->insubstmt_index = insubstmt_index;
831
832 /* get optional argument */
Radek Krejcid3ca0632019-04-16 16:54:54 +0200833 LY_CHECK_RET(get_argument(ctx, data, Y_MAYBE_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200834
Radek Krejci0ae092d2018-09-20 16:43:19 +0200835 if (word) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200836 INSERT_WORD(ctx, buf, e->argument, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200837 }
838
Radek Krejci6d6556c2018-11-08 09:37:45 +0100839 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Radek Krejci335332a2019-09-05 13:03:35 +0200840 LY_CHECK_RET(parse_ext_substmt(ctx, data, kw, word, word_len, &e->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200841 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200842 return ret;
843}
844
Michal Vaskoea5abea2018-09-18 13:10:54 +0200845/**
846 * @brief Parse a generic text field without specific constraints. Those are contact, organization,
847 * description, etc...
848 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200849 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200850 * @param[in,out] data Data to read from, always moved to currently handled character.
851 * @param[in] substmt Type of this substatement.
852 * @param[in] substmt_index Index of this substatement.
853 * @param[in,out] value Place to store the parsed value.
854 * @param[in] arg Type of the YANG keyword argument (of the value).
855 * @param[in,out] exts Extension instances to add to.
856 *
857 * @return LY_ERR values.
858 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200859static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100860parse_text_field(struct lys_yang_parser_ctx *ctx, const char **data, LYEXT_SUBSTMT substmt, uint32_t substmt_index,
Michal Vasko7fbc8162018-09-17 10:35:16 +0200861 const char **value, enum yang_arg arg, struct lysp_ext_instance **exts)
862{
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100863 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200864 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +0200865 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +0200866 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200867
868 if (*value) {
David Sedlákb3077192019-06-19 10:55:37 +0200869 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, lyext_substmt2str(substmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200870 return LY_EVALID;
871 }
872
873 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +0200874 LY_CHECK_RET(get_argument(ctx, data, arg, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200875
876 /* store value and spend buf if allocated */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200877 INSERT_WORD(ctx, buf, *value, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200878
Radek Krejci6d6556c2018-11-08 09:37:45 +0100879 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200880 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +0200881 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100882 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, substmt, substmt_index, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200883 break;
884 default:
David Sedlákb3077192019-06-19 10:55:37 +0200885 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), lyext_substmt2str(substmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200886 return LY_EVALID;
887 }
888 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200889 return ret;
890}
891
Michal Vaskoea5abea2018-09-18 13:10:54 +0200892/**
893 * @brief Parse the yang-version statement.
894 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200895 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200896 * @param[in,out] data Data to read from, always moved to currently handled character.
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100897 * @param[out] version Storage for the parsed information.
898 * @param[in, out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200899 *
900 * @return LY_ERR values.
901 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200902static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100903parse_yangversion(struct lys_yang_parser_ctx *ctx, const char **data, uint8_t *version, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200904{
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100905 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200906 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +0200907 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +0200908 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200909
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100910 if (*version) {
David Sedlákb3077192019-06-19 10:55:37 +0200911 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200912 return LY_EVALID;
913 }
914
915 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +0200916 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200917
918 if ((word_len == 3) && !strncmp(word, "1.0", word_len)) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100919 *version = LYS_VERSION_1_0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200920 } else if ((word_len == 3) && !strncmp(word, "1.1", word_len)) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100921 *version = LYS_VERSION_1_1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200922 } else {
David Sedlákb3077192019-06-19 10:55:37 +0200923 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200924 free(buf);
925 return LY_EVALID;
926 }
927 free(buf);
928
Radek Krejci6d6556c2018-11-08 09:37:45 +0100929 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200930 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +0200931 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100932 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_VERSION, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200933 break;
934 default:
David Sedlákb3077192019-06-19 10:55:37 +0200935 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200936 return LY_EVALID;
937 }
938 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200939 return ret;
940}
941
Michal Vaskoea5abea2018-09-18 13:10:54 +0200942/**
943 * @brief Parse the belongs-to statement.
944 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200945 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200946 * @param[in,out] data Data to read from, always moved to currently handled character.
947 * @param[in,out] belongsto Place to store the parsed value.
948 * @param[in,out] prefix Place to store the parsed belongs-to prefix value.
949 * @param[in,out] exts Extension instances to add to.
950 *
951 * @return LY_ERR values.
952 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200953static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100954parse_belongsto(struct lys_yang_parser_ctx *ctx, const char **data, const char **belongsto, const char **prefix, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200955{
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100956 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200957 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +0200958 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +0200959 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200960
961 if (*belongsto) {
David Sedlákb3077192019-06-19 10:55:37 +0200962 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200963 return LY_EVALID;
964 }
965
966 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +0200967 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200968
Radek Krejci44ceedc2018-10-02 15:54:31 +0200969 INSERT_WORD(ctx, buf, *belongsto, word, word_len);
Radek Krejcif09e4e82019-06-14 15:08:11 +0200970
Radek Krejci6d6556c2018-11-08 09:37:45 +0100971 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200972 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +0200973 case LY_STMT_PREFIX:
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100974 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_PREFIX, 0, prefix, Y_IDENTIF_ARG, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200975 break;
Radek Krejcid6b76452019-09-03 17:03:03 +0200976 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100977 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_BELONGSTO, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200978 break;
979 default:
David Sedlákb3077192019-06-19 10:55:37 +0200980 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200981 return LY_EVALID;
982 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200983 }
Radek Krejcic59bc972018-09-17 16:13:06 +0200984 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +0100985checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +0200986 /* mandatory substatements */
987 if (!*prefix) {
David Sedlákb3077192019-06-19 10:55:37 +0200988 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200989 return LY_EVALID;
990 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200991 return ret;
992}
993
Michal Vaskoea5abea2018-09-18 13:10:54 +0200994/**
995 * @brief Parse the revision-date statement.
996 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200997 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200998 * @param[in,out] data Data to read from, always moved to currently handled character.
999 * @param[in,out] rev Array to store the parsed value in.
1000 * @param[in,out] exts Extension instances to add to.
1001 *
1002 * @return LY_ERR values.
1003 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001004static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001005parse_revisiondate(struct lys_yang_parser_ctx *ctx, const char **data, char *rev, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001006{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001007 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001008 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001009 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001010 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001011
1012 if (rev[0]) {
David Sedlákb3077192019-06-19 10:55:37 +02001013 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "revision-date");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001014 return LY_EVALID;
1015 }
1016
1017 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001018 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001019
1020 /* check value */
Michal Vaskob36053d2020-03-26 15:49:30 +01001021 if (lysp_check_date((struct lys_parser_ctx *)ctx, word, word_len, "revision-date")) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001022 free(buf);
1023 return LY_EVALID;
1024 }
1025
1026 /* store value and spend buf if allocated */
1027 strncpy(rev, word, word_len);
1028 free(buf);
1029
Radek Krejci6d6556c2018-11-08 09:37:45 +01001030 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001031 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001032 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001033 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_REVISIONDATE, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001034 break;
1035 default:
David Sedlákb3077192019-06-19 10:55:37 +02001036 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "revision-date");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001037 return LY_EVALID;
1038 }
1039 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001040 return ret;
1041}
1042
Michal Vaskoea5abea2018-09-18 13:10:54 +02001043/**
1044 * @brief Parse the include statement.
1045 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001046 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001047 * @param[in] module_name Name of the module to check name collisions.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001048 * @param[in,out] data Data to read from, always moved to currently handled character.
1049 * @param[in,out] includes Parsed includes to add to.
1050 *
1051 * @return LY_ERR values.
1052 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001053static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001054parse_include(struct lys_yang_parser_ctx *ctx, const char *module_name, const char **data, struct lysp_include **includes)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001055{
Radek Krejcid33273d2018-10-25 14:55:52 +02001056 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001057 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001058 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001059 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001060 struct lysp_include *inc;
1061
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001062 LY_ARRAY_NEW_RET(ctx->ctx, *includes, inc, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001063
1064 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001065 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001066
Radek Krejci086c7132018-10-26 15:29:04 +02001067 INSERT_WORD(ctx, buf, inc->name, word, word_len);
1068
Radek Krejcifaa1eac2018-10-30 14:34:55 +01001069 /* submodules share the namespace with the module names, so there must not be
1070 * a module of the same name in the context, no need for revision matching */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001071 if (!strcmp(module_name, inc->name) || ly_ctx_get_module_latest(ctx->ctx, inc->name)) {
David Sedlákb3077192019-06-19 10:55:37 +02001072 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Name collision between module and submodule of name \"%s\".", inc->name);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01001073 return LY_EVALID;
1074 }
1075
Radek Krejci6d6556c2018-11-08 09:37:45 +01001076 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001077 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001078 case LY_STMT_DESCRIPTION:
Radek Krejci335332a2019-09-05 13:03:35 +02001079 PARSER_CHECK_STMTVER2_RET(ctx, "description", "include");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001080 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &inc->dsc, Y_STR_ARG, &inc->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001081 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001082 case LY_STMT_REFERENCE:
Radek Krejci335332a2019-09-05 13:03:35 +02001083 PARSER_CHECK_STMTVER2_RET(ctx, "reference", "include");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001084 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &inc->ref, Y_STR_ARG, &inc->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001085 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001086 case LY_STMT_REVISION_DATE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001087 LY_CHECK_RET(parse_revisiondate(ctx, data, inc->rev, &inc->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001088 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001089 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001090 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &inc->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001091 break;
1092 default:
David Sedlákb3077192019-06-19 10:55:37 +02001093 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "include");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001094 return LY_EVALID;
1095 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001096 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001097 return ret;
1098}
1099
Michal Vaskoea5abea2018-09-18 13:10:54 +02001100/**
1101 * @brief Parse the import statement.
1102 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001103 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001104 * @param[in] module_prefix Prefix of the module to check prefix collisions.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001105 * @param[in,out] data Data to read from, always moved to currently handled character.
1106 * @param[in,out] imports Parsed imports to add to.
1107 *
1108 * @return LY_ERR values.
1109 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001110static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001111parse_import(struct lys_yang_parser_ctx *ctx, const char *module_prefix, const char **data, struct lysp_import **imports)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001112{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001113 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001114 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001115 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001116 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001117 struct lysp_import *imp;
1118
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001119 LY_ARRAY_NEW_RET(ctx->ctx, *imports, imp, LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001120
1121 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001122 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02001123 INSERT_WORD(ctx, buf, imp->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001124
Radek Krejci6d6556c2018-11-08 09:37:45 +01001125 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001126 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001127 case LY_STMT_PREFIX:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001128 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_PREFIX, 0, &imp->prefix, Y_IDENTIF_ARG, &imp->exts));
Michal Vaskob36053d2020-03-26 15:49:30 +01001129 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 +02001130 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001131 case LY_STMT_DESCRIPTION:
Radek Krejci335332a2019-09-05 13:03:35 +02001132 PARSER_CHECK_STMTVER2_RET(ctx, "description", "import");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001133 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &imp->dsc, Y_STR_ARG, &imp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001134 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001135 case LY_STMT_REFERENCE:
Radek Krejci335332a2019-09-05 13:03:35 +02001136 PARSER_CHECK_STMTVER2_RET(ctx, "reference", "import");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001137 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &imp->ref, Y_STR_ARG, &imp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001138 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001139 case LY_STMT_REVISION_DATE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001140 LY_CHECK_RET(parse_revisiondate(ctx, data, imp->rev, &imp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001141 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001142 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001143 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &imp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001144 break;
1145 default:
David Sedlákb3077192019-06-19 10:55:37 +02001146 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "import");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001147 return LY_EVALID;
1148 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001149 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001150 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01001151checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001152 /* mandatory substatements */
David Sedlákb3077192019-06-19 10:55:37 +02001153 LY_CHECK_ERR_RET(!imp->prefix, LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "import"), LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001154
1155 return ret;
1156}
1157
Michal Vaskoea5abea2018-09-18 13:10:54 +02001158/**
1159 * @brief Parse the revision statement.
1160 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001161 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001162 * @param[in,out] data Data to read from, always moved to currently handled character.
1163 * @param[in,out] revs Parsed revisions to add to.
1164 *
1165 * @return LY_ERR values.
1166 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001167static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001168parse_revision(struct lys_yang_parser_ctx *ctx, const char **data, struct lysp_revision **revs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001169{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001170 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001171 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001172 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001173 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001174 struct lysp_revision *rev;
1175
Radek Krejci2c4e7172018-10-19 15:56:26 +02001176 LY_ARRAY_NEW_RET(ctx->ctx, *revs, rev, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001177
1178 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001179 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001180
1181 /* check value */
Michal Vaskob36053d2020-03-26 15:49:30 +01001182 if (lysp_check_date((struct lys_parser_ctx *)ctx, word, word_len, "revision")) {
David Sedlák68ef3dc2019-07-22 13:40:19 +02001183 free(buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001184 return LY_EVALID;
1185 }
1186
Radek Krejcib7db73a2018-10-24 14:18:40 +02001187 strncpy(rev->date, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001188 free(buf);
1189
Radek Krejci6d6556c2018-11-08 09:37:45 +01001190 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001191 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001192 case LY_STMT_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001193 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &rev->dsc, Y_STR_ARG, &rev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001194 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001195 case LY_STMT_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001196 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &rev->ref, Y_STR_ARG, &rev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001197 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001198 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001199 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &rev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001200 break;
1201 default:
David Sedlákb3077192019-06-19 10:55:37 +02001202 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "revision");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001203 return LY_EVALID;
1204 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001205 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001206 return ret;
1207}
1208
Michal Vaskoea5abea2018-09-18 13:10:54 +02001209/**
1210 * @brief Parse a generic text field that can have more instances such as base.
1211 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001212 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001213 * @param[in,out] data Data to read from, always moved to currently handled character.
1214 * @param[in] substmt Type of this substatement.
1215 * @param[in,out] texts Parsed values to add to.
1216 * @param[in] arg Type of the expected argument.
1217 * @param[in,out] exts Extension instances to add to.
1218 *
1219 * @return LY_ERR values.
1220 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001221static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001222parse_text_fields(struct lys_yang_parser_ctx *ctx, const char **data, LYEXT_SUBSTMT substmt, const char ***texts, enum yang_arg arg,
Michal Vasko7fbc8162018-09-17 10:35:16 +02001223 struct lysp_ext_instance **exts)
1224{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001225 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001226 char *buf, *word;
Radek Krejci151a5b72018-10-19 14:21:44 +02001227 const char **item;
1228 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001229 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001230
1231 /* allocate new pointer */
Radek Krejci2c4e7172018-10-19 15:56:26 +02001232 LY_ARRAY_NEW_RET(ctx->ctx, *texts, item, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001233
1234 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001235 LY_CHECK_RET(get_argument(ctx, data, arg, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001236
Radek Krejci151a5b72018-10-19 14:21:44 +02001237 INSERT_WORD(ctx, buf, *item, word, word_len);
Radek Krejci6d6556c2018-11-08 09:37:45 +01001238 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001239 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001240 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001241 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, substmt, LY_ARRAY_SIZE(*texts) - 1, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001242 break;
1243 default:
David Sedlákb3077192019-06-19 10:55:37 +02001244 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), lyext_substmt2str(substmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001245 return LY_EVALID;
1246 }
1247 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001248 return ret;
1249}
1250
Michal Vaskoea5abea2018-09-18 13:10:54 +02001251/**
1252 * @brief Parse the config statement.
1253 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001254 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001255 * @param[in,out] data Data to read from, always moved to currently handled character.
1256 * @param[in,out] flags Flags to add to.
1257 * @param[in,out] exts Extension instances to add to.
1258 *
1259 * @return LY_ERR values.
1260 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001261static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001262parse_config(struct lys_yang_parser_ctx *ctx, const char **data, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001263{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001264 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001265 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001266 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001267 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001268
1269 if (*flags & LYS_CONFIG_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02001270 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001271 return LY_EVALID;
1272 }
1273
1274 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001275 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001276
1277 if ((word_len == 4) && !strncmp(word, "true", word_len)) {
1278 *flags |= LYS_CONFIG_W;
1279 } else if ((word_len == 5) && !strncmp(word, "false", word_len)) {
1280 *flags |= LYS_CONFIG_R;
1281 } else {
David Sedlákb3077192019-06-19 10:55:37 +02001282 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001283 free(buf);
1284 return LY_EVALID;
1285 }
1286 free(buf);
1287
Radek Krejci6d6556c2018-11-08 09:37:45 +01001288 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001289 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001290 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001291 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_CONFIG, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001292 break;
1293 default:
David Sedlákb3077192019-06-19 10:55:37 +02001294 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001295 return LY_EVALID;
1296 }
1297 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001298 return ret;
1299}
1300
Michal Vaskoea5abea2018-09-18 13:10:54 +02001301/**
1302 * @brief Parse the mandatory statement.
1303 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001304 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001305 * @param[in,out] data Data to read from, always moved to currently handled character.
1306 * @param[in,out] flags Flags to add to.
1307 * @param[in,out] exts Extension instances to add to.
1308 *
1309 * @return LY_ERR values.
1310 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001311static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001312parse_mandatory(struct lys_yang_parser_ctx *ctx, const char **data, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001313{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001314 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001315 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001316 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001317 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001318
1319 if (*flags & LYS_MAND_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02001320 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001321 return LY_EVALID;
1322 }
1323
1324 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001325 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001326
1327 if ((word_len == 4) && !strncmp(word, "true", word_len)) {
1328 *flags |= LYS_MAND_TRUE;
1329 } else if ((word_len == 5) && !strncmp(word, "false", word_len)) {
1330 *flags |= LYS_MAND_FALSE;
1331 } else {
David Sedlákb3077192019-06-19 10:55:37 +02001332 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001333 free(buf);
1334 return LY_EVALID;
1335 }
1336 free(buf);
1337
Radek Krejci6d6556c2018-11-08 09:37:45 +01001338 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001339 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001340 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001341 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_MANDATORY, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001342 break;
1343 default:
David Sedlákb3077192019-06-19 10:55:37 +02001344 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001345 return LY_EVALID;
1346 }
1347 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001348 return ret;
1349}
1350
Michal Vaskoea5abea2018-09-18 13:10:54 +02001351/**
1352 * @brief Parse a restriction such as range or length.
1353 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001354 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001355 * @param[in,out] data Data to read from, always moved to currently handled character.
1356 * @param[in] restr_kw Type of this particular restriction.
1357 * @param[in,out] exts Extension instances to add to.
1358 *
1359 * @return LY_ERR values.
1360 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001361static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001362parse_restr(struct lys_yang_parser_ctx *ctx, const char **data, enum ly_stmt restr_kw, struct lysp_restr *restr)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001363{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001364 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001365 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001366 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001367 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001368
1369 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001370 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001371
Michal Vaskob36053d2020-03-26 15:49:30 +01001372 CHECK_NONEMPTY(ctx, word_len, ly_stmt2str(restr_kw));
Radek Krejci44ceedc2018-10-02 15:54:31 +02001373 INSERT_WORD(ctx, buf, restr->arg, word, word_len);
Radek Krejci6d6556c2018-11-08 09:37:45 +01001374 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001375 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001376 case LY_STMT_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001377 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &restr->dsc, Y_STR_ARG, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001378 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001379 case LY_STMT_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001380 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &restr->ref, Y_STR_ARG, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001381 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001382 case LY_STMT_ERROR_APP_TAG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001383 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_ERRTAG, 0, &restr->eapptag, 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_ERROR_MESSAGE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001386 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_ERRMSG, 0, &restr->emsg, 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_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001389 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001390 break;
1391 default:
David Sedlákb3077192019-06-19 10:55:37 +02001392 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(restr_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001393 return LY_EVALID;
1394 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001395 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001396 return ret;
1397}
1398
Michal Vaskoea5abea2018-09-18 13:10:54 +02001399/**
1400 * @brief Parse a restriction that can have more instances such as must.
1401 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001402 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001403 * @param[in,out] data Data to read from, always moved to currently handled character.
1404 * @param[in] restr_kw Type of this particular restriction.
1405 * @param[in,out] restrs Restrictions to add to.
1406 *
1407 * @return LY_ERR values.
1408 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001409static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001410parse_restrs(struct lys_yang_parser_ctx *ctx, const char **data, enum ly_stmt restr_kw, struct lysp_restr **restrs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001411{
1412 struct lysp_restr *restr;
1413
Radek Krejci2c4e7172018-10-19 15:56:26 +02001414 LY_ARRAY_NEW_RET(ctx->ctx, *restrs, restr, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001415 return parse_restr(ctx, data, restr_kw, restr);
1416}
1417
Michal Vaskoea5abea2018-09-18 13:10:54 +02001418/**
1419 * @brief Parse the status statement.
1420 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001421 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001422 * @param[in,out] data Data to read from, always moved to currently handled character.
1423 * @param[in,out] flags Flags to add to.
1424 * @param[in,out] exts Extension instances to add to.
1425 *
1426 * @return LY_ERR values.
1427 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001428static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001429parse_status(struct lys_yang_parser_ctx *ctx, const char **data, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001430{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001431 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001432 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001433 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001434 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001435
1436 if (*flags & LYS_STATUS_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02001437 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001438 return LY_EVALID;
1439 }
1440
1441 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001442 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001443
1444 if ((word_len == 7) && !strncmp(word, "current", word_len)) {
1445 *flags |= LYS_STATUS_CURR;
1446 } else if ((word_len == 10) && !strncmp(word, "deprecated", word_len)) {
1447 *flags |= LYS_STATUS_DEPRC;
1448 } else if ((word_len == 8) && !strncmp(word, "obsolete", word_len)) {
1449 *flags |= LYS_STATUS_OBSLT;
1450 } else {
David Sedlákb3077192019-06-19 10:55:37 +02001451 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001452 free(buf);
1453 return LY_EVALID;
1454 }
1455 free(buf);
1456
Radek Krejci6d6556c2018-11-08 09:37:45 +01001457 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001458 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001459 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001460 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_STATUS, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001461 break;
1462 default:
David Sedlákb3077192019-06-19 10:55:37 +02001463 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001464 return LY_EVALID;
1465 }
1466 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001467 return ret;
1468}
1469
Michal Vaskoea5abea2018-09-18 13:10:54 +02001470/**
1471 * @brief Parse the when statement.
1472 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001473 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001474 * @param[in,out] data Data to read from, always moved to currently handled character.
1475 * @param[in,out] when_p When pointer to parse to.
1476 *
1477 * @return LY_ERR values.
1478 */
Radek Krejcif09e4e82019-06-14 15:08:11 +02001479LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001480parse_when(struct lys_yang_parser_ctx *ctx, const char **data, struct lysp_when **when_p)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001481{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001482 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001483 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001484 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001485 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001486 struct lysp_when *when;
1487
1488 if (*when_p) {
David Sedlákb3077192019-06-19 10:55:37 +02001489 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "when");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001490 return LY_EVALID;
1491 }
1492
1493 when = calloc(1, sizeof *when);
Radek Krejci44ceedc2018-10-02 15:54:31 +02001494 LY_CHECK_ERR_RET(!when, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001495
1496 /* get value */
Radek Krejci2f54df52019-06-21 10:59:19 +02001497 LY_CHECK_ERR_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len), free(when), LY_EMEM);
Michal Vaskob36053d2020-03-26 15:49:30 +01001498 CHECK_NONEMPTY(ctx, word_len, "when");
Radek Krejci44ceedc2018-10-02 15:54:31 +02001499 INSERT_WORD(ctx, buf, when->cond, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001500
Radek Krejcif09e4e82019-06-14 15:08:11 +02001501 *when_p = when;
1502
Radek Krejci6d6556c2018-11-08 09:37:45 +01001503 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001504 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001505 case LY_STMT_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001506 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &when->dsc, Y_STR_ARG, &when->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001507 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001508 case LY_STMT_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001509 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &when->ref, Y_STR_ARG, &when->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001510 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001511 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001512 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &when->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001513 break;
1514 default:
David Sedlákb3077192019-06-19 10:55:37 +02001515 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "when");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001516 return LY_EVALID;
1517 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001518 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001519 return ret;
1520}
1521
Michal Vaskoea5abea2018-09-18 13:10:54 +02001522/**
1523 * @brief Parse the anydata or anyxml statement.
1524 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001525 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001526 * @param[in,out] data Data to read from, always moved to currently handled character.
1527 * @param[in] kw Type of this particular keyword.
1528 * @param[in,out] siblings Siblings to add to.
1529 *
1530 * @return LY_ERR values.
1531 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02001532LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001533parse_any(struct lys_yang_parser_ctx *ctx, const char **data, enum ly_stmt kw, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001534{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001535 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001536 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001537 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001538 struct lysp_node_anydata *any;
1539
David Sedlák60adc092019-08-06 15:57:02 +02001540 /* create new structure and insert into siblings */
David Sedlákb9eeb9c2019-09-13 11:17:19 +02001541 LY_LIST_NEW_RET(ctx->ctx, siblings, any, next, LY_EMEM);
David Sedlák60adc092019-08-06 15:57:02 +02001542
Radek Krejcid6b76452019-09-03 17:03:03 +02001543 any->nodetype = kw == LY_STMT_ANYDATA ? LYS_ANYDATA : LYS_ANYXML;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001544 any->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001545
Michal Vasko7fbc8162018-09-17 10:35:16 +02001546 /* get name */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001547 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02001548 INSERT_WORD(ctx, buf, any->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001549
1550 /* parse substatements */
Radek Krejci6d6556c2018-11-08 09:37:45 +01001551 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001552 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001553 case LY_STMT_CONFIG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001554 LY_CHECK_RET(parse_config(ctx, data, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001555 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001556 case LY_STMT_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001557 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &any->dsc, Y_STR_ARG, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001558 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001559 case LY_STMT_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001560 LY_CHECK_RET(parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &any->iffeatures, Y_STR_ARG, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001561 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001562 case LY_STMT_MANDATORY:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001563 LY_CHECK_RET(parse_mandatory(ctx, data, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001564 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001565 case LY_STMT_MUST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001566 LY_CHECK_RET(parse_restrs(ctx, data, kw, &any->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001567 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001568 case LY_STMT_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001569 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &any->ref, Y_STR_ARG, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001570 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001571 case LY_STMT_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001572 LY_CHECK_RET(parse_status(ctx, data, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001573 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001574 case LY_STMT_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001575 LY_CHECK_RET(parse_when(ctx, data, &any->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001576 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001577 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001578 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001579 break;
1580 default:
David Sedlákb3077192019-06-19 10:55:37 +02001581 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw),
Radek Krejcid6b76452019-09-03 17:03:03 +02001582 (any->nodetype & LYS_ANYDATA) == LYS_ANYDATA ? ly_stmt2str(LY_STMT_ANYDATA) : ly_stmt2str(LY_STMT_ANYXML));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001583 return LY_EVALID;
1584 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001585 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001586 return ret;
1587}
1588
Michal Vaskoea5abea2018-09-18 13:10:54 +02001589/**
1590 * @brief Parse the value or position statement. Substatement of type enum statement.
1591 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001592 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001593 * @param[in,out] data Data to read from, always moved to currently handled character.
1594 * @param[in] val_kw Type of this particular keyword.
1595 * @param[in,out] value Value to write to.
1596 * @param[in,out] flags Flags to write to.
1597 * @param[in,out] exts Extension instances to add to.
1598 *
1599 * @return LY_ERR values.
1600 */
David Sedlákd6ce6d72019-07-16 17:30:18 +02001601LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001602parse_type_enum_value_pos(struct lys_yang_parser_ctx *ctx, const char **data, enum ly_stmt val_kw, int64_t *value, uint16_t *flags,
Michal Vasko7fbc8162018-09-17 10:35:16 +02001603 struct lysp_ext_instance **exts)
1604{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001605 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001606 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02001607 size_t word_len;
Radek Krejci4ad42aa2019-07-23 16:55:58 +02001608 long int num = 0;
1609 unsigned long int unum = 0;
Radek Krejcid6b76452019-09-03 17:03:03 +02001610 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001611
1612 if (*flags & LYS_SET_VALUE) {
David Sedlákb3077192019-06-19 10:55:37 +02001613 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(val_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001614 return LY_EVALID;
1615 }
1616 *flags |= LYS_SET_VALUE;
1617
1618 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001619 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001620
Radek Krejcid6b76452019-09-03 17:03:03 +02001621 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 +02001622 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001623 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001624 }
1625
1626 errno = 0;
Radek Krejcid6b76452019-09-03 17:03:03 +02001627 if (val_kw == LY_STMT_VALUE) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001628 num = strtol(word, &ptr, 10);
Radek Krejci8b764662018-11-14 14:15:13 +01001629 if (num < INT64_C(-2147483648) || num > INT64_C(2147483647)) {
David Sedlákb3077192019-06-19 10:55:37 +02001630 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001631 goto error;
1632 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001633 } else {
1634 unum = strtoul(word, &ptr, 10);
Radek Krejci8b764662018-11-14 14:15:13 +01001635 if (unum > UINT64_C(4294967295)) {
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 }
1640 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02001641 if ((size_t)(ptr - word) != word_len) {
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;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001644 }
1645 if (errno == ERANGE) {
David Sedlákb3077192019-06-19 10:55:37 +02001646 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, ly_stmt2str(val_kw));
Radek Krejci8b764662018-11-14 14:15:13 +01001647 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001648 }
Radek Krejcid6b76452019-09-03 17:03:03 +02001649 if (val_kw == LY_STMT_VALUE) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001650 *value = num;
1651 } else {
1652 *value = unum;
1653 }
1654 free(buf);
1655
Radek Krejci6d6556c2018-11-08 09:37:45 +01001656 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001657 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001658 case LY_STMT_EXTENSION_INSTANCE:
1659 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, val_kw == LY_STMT_VALUE ? LYEXT_SUBSTMT_VALUE : LYEXT_SUBSTMT_POSITION, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001660 break;
1661 default:
David Sedlákb3077192019-06-19 10:55:37 +02001662 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(val_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001663 return LY_EVALID;
1664 }
1665 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001666 return ret;
Radek Krejci8b764662018-11-14 14:15:13 +01001667
1668error:
1669 free(buf);
1670 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001671}
1672
Michal Vaskoea5abea2018-09-18 13:10:54 +02001673/**
1674 * @brief Parse the enum or bit statement. Substatement of type statement.
1675 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001676 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001677 * @param[in,out] data Data to read from, always moved to currently handled character.
1678 * @param[in] enum_kw Type of this particular keyword.
1679 * @param[in,out] enums Enums or bits to add to.
1680 *
1681 * @return LY_ERR values.
1682 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001683static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001684parse_type_enum(struct lys_yang_parser_ctx *ctx, const char **data, enum ly_stmt enum_kw, struct lysp_type_enum **enums)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001685{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001686 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001687 char *buf, *word;
David Sedlák6544c182019-07-12 13:17:33 +02001688 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001689 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001690 struct lysp_type_enum *enm;
1691
Radek Krejci2c4e7172018-10-19 15:56:26 +02001692 LY_ARRAY_NEW_RET(ctx->ctx, *enums, enm, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001693
1694 /* get value */
Radek Krejcid6b76452019-09-03 17:03:03 +02001695 LY_CHECK_RET(get_argument(ctx, data, enum_kw == LY_STMT_ENUM ? Y_STR_ARG : Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
1696 if (enum_kw == LY_STMT_ENUM) {
Michal Vaskob36053d2020-03-26 15:49:30 +01001697 ret = lysp_check_enum_name((struct lys_parser_ctx *)ctx, (const char *)word, word_len);
David Sedlák6544c182019-07-12 13:17:33 +02001698 LY_CHECK_ERR_RET(ret, free(buf), ret);
Radek Krejci335332a2019-09-05 13:03:35 +02001699 } /* else nothing specific for YANG_BIT */
Radek Krejci8b764662018-11-14 14:15:13 +01001700
Radek Krejci44ceedc2018-10-02 15:54:31 +02001701 INSERT_WORD(ctx, buf, enm->name, word, word_len);
Radek Krejci8b764662018-11-14 14:15:13 +01001702 CHECK_UNIQUENESS(ctx, *enums, name, ly_stmt2str(enum_kw), enm->name);
1703
Radek Krejci6d6556c2018-11-08 09:37:45 +01001704 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001705 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001706 case LY_STMT_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001707 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &enm->dsc, Y_STR_ARG, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001708 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001709 case LY_STMT_IF_FEATURE:
Radek Krejci335332a2019-09-05 13:03:35 +02001710 PARSER_CHECK_STMTVER2_RET(ctx, "if-feature", ly_stmt2str(enum_kw));
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001711 LY_CHECK_RET(parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &enm->iffeatures, Y_STR_ARG, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001712 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001713 case LY_STMT_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001714 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &enm->ref, Y_STR_ARG, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001715 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001716 case LY_STMT_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001717 LY_CHECK_RET(parse_status(ctx, data, &enm->flags, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001718 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001719 case LY_STMT_VALUE:
1720 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 +02001721 ly_stmt2str(enum_kw)), LY_EVALID);
1722 LY_CHECK_RET(parse_type_enum_value_pos(ctx, data, kw, &enm->value, &enm->flags, &enm->exts));
1723 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001724 case LY_STMT_POSITION:
1725 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 +02001726 ly_stmt2str(enum_kw)), LY_EVALID);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001727 LY_CHECK_RET(parse_type_enum_value_pos(ctx, data, kw, &enm->value, &enm->flags, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001728 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001729 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001730 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001731 break;
1732 default:
David Sedlákb3077192019-06-19 10:55:37 +02001733 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(enum_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001734 return LY_EVALID;
1735 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001736 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001737 return ret;
1738}
1739
Michal Vaskoea5abea2018-09-18 13:10:54 +02001740/**
1741 * @brief Parse the fraction-digits statement. Substatement of type statement.
1742 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001743 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001744 * @param[in,out] data Data to read from, always moved to currently handled character.
1745 * @param[in,out] fracdig Value to write to.
1746 * @param[in,out] exts Extension instances to add to.
1747 *
1748 * @return LY_ERR values.
1749 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001750static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001751parse_type_fracdigits(struct lys_yang_parser_ctx *ctx, const char **data, uint8_t *fracdig, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001752{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001753 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001754 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02001755 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001756 unsigned long int num;
Radek Krejcid6b76452019-09-03 17:03:03 +02001757 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001758
1759 if (*fracdig) {
David Sedlákb3077192019-06-19 10:55:37 +02001760 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001761 return LY_EVALID;
1762 }
1763
1764 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001765 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001766
1767 if (!word_len || (word[0] == '0') || !isdigit(word[0])) {
David Sedlákb3077192019-06-19 10:55:37 +02001768 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001769 free(buf);
1770 return LY_EVALID;
1771 }
1772
1773 errno = 0;
1774 num = strtoul(word, &ptr, 10);
1775 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02001776 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02001777 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001778 free(buf);
1779 return LY_EVALID;
1780 }
1781 if ((errno == ERANGE) || (num > 18)) {
David Sedlákb3077192019-06-19 10:55:37 +02001782 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001783 free(buf);
1784 return LY_EVALID;
1785 }
1786 *fracdig = num;
1787 free(buf);
1788
Radek Krejci6d6556c2018-11-08 09:37:45 +01001789 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001790 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001791 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001792 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_FRACDIGITS, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001793 break;
1794 default:
David Sedlákb3077192019-06-19 10:55:37 +02001795 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001796 return LY_EVALID;
1797 }
1798 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001799 return ret;
1800}
1801
Michal Vaskoea5abea2018-09-18 13:10:54 +02001802/**
1803 * @brief Parse the require-instance statement. Substatement of type statement.
1804 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001805 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001806 * @param[in,out] data Data to read from, always moved to currently handled character.
1807 * @param[in,out] reqinst Value to write to.
1808 * @param[in,out] flags Flags to write to.
1809 * @param[in,out] exts Extension instances to add to.
1810 *
1811 * @return LY_ERR values.
1812 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001813static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001814parse_type_reqinstance(struct lys_yang_parser_ctx *ctx, const char **data, uint8_t *reqinst, uint16_t *flags,
Michal Vasko7fbc8162018-09-17 10:35:16 +02001815 struct lysp_ext_instance **exts)
1816{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001817 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001818 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001819 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001820 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001821
1822 if (*flags & LYS_SET_REQINST) {
David Sedlákb3077192019-06-19 10:55:37 +02001823 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001824 return LY_EVALID;
1825 }
1826 *flags |= LYS_SET_REQINST;
1827
1828 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001829 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001830
1831 if ((word_len == 4) && !strncmp(word, "true", word_len)) {
1832 *reqinst = 1;
1833 } else if ((word_len != 5) || strncmp(word, "false", word_len)) {
David Sedlákb3077192019-06-19 10:55:37 +02001834 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001835 free(buf);
1836 return LY_EVALID;
1837 }
1838 free(buf);
1839
Radek Krejci6d6556c2018-11-08 09:37:45 +01001840 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001841 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001842 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001843 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_REQINSTANCE, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001844 break;
1845 default:
David Sedlákb3077192019-06-19 10:55:37 +02001846 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001847 return LY_EVALID;
1848 }
1849 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001850 return ret;
1851}
1852
Michal Vaskoea5abea2018-09-18 13:10:54 +02001853/**
1854 * @brief Parse the modifier statement. Substatement of type pattern statement.
1855 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001856 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001857 * @param[in,out] data Data to read from, always moved to currently handled character.
1858 * @param[in,out] pat Value to write to.
1859 * @param[in,out] exts Extension instances to add to.
1860 *
1861 * @return LY_ERR values.
1862 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001863static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001864parse_type_pattern_modifier(struct lys_yang_parser_ctx *ctx, const char **data, const char **pat, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001865{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001866 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001867 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001868 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001869 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001870
1871 if ((*pat)[0] == 0x15) {
David Sedlákb3077192019-06-19 10:55:37 +02001872 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001873 return LY_EVALID;
1874 }
1875
1876 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001877 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001878
1879 if ((word_len != 12) || strncmp(word, "invert-match", word_len)) {
David Sedlákb3077192019-06-19 10:55:37 +02001880 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001881 free(buf);
1882 return LY_EVALID;
1883 }
1884 free(buf);
1885
1886 /* replace the value in the dictionary */
1887 buf = malloc(strlen(*pat) + 1);
Radek Krejci44ceedc2018-10-02 15:54:31 +02001888 LY_CHECK_ERR_RET(!buf, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001889 strcpy(buf, *pat);
Radek Krejci44ceedc2018-10-02 15:54:31 +02001890 lydict_remove(ctx->ctx, *pat);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001891
1892 assert(buf[0] == 0x06);
1893 buf[0] = 0x15;
Radek Krejci44ceedc2018-10-02 15:54:31 +02001894 *pat = lydict_insert_zc(ctx->ctx, buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001895
Radek Krejci6d6556c2018-11-08 09:37:45 +01001896 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001897 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001898 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001899 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_MODIFIER, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001900 break;
1901 default:
David Sedlákb3077192019-06-19 10:55:37 +02001902 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001903 return LY_EVALID;
1904 }
1905 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001906 return ret;
1907}
1908
Michal Vaskoea5abea2018-09-18 13:10:54 +02001909/**
1910 * @brief Parse the pattern statement. Substatement of type statement.
1911 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001912 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001913 * @param[in,out] data Data to read from, always moved to currently handled character.
1914 * @param[in,out] patterns Restrictions to add to.
1915 *
1916 * @return LY_ERR values.
1917 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001918static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001919parse_type_pattern(struct lys_yang_parser_ctx *ctx, const char **data, struct lysp_restr **patterns)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001920{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001921 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001922 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001923 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001924 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001925 struct lysp_restr *restr;
1926
Radek Krejci2c4e7172018-10-19 15:56:26 +02001927 LY_ARRAY_NEW_RET(ctx->ctx, *patterns, restr, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001928
1929 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001930 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001931
1932 /* add special meaning first byte */
1933 if (buf) {
1934 buf = realloc(buf, word_len + 2);
1935 word = buf;
1936 } else {
1937 buf = malloc(word_len + 2);
1938 }
Radek Krejci44ceedc2018-10-02 15:54:31 +02001939 LY_CHECK_ERR_RET(!buf, LOGMEM(ctx->ctx), LY_EMEM);
Radek Krejci86d106e2018-10-18 09:53:19 +02001940 memmove(buf + 1, word, word_len);
1941 buf[0] = 0x06; /* pattern's default regular-match flag */
1942 buf[word_len + 1] = '\0'; /* terminating NULL byte */
1943 restr->arg = lydict_insert_zc(ctx->ctx, buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001944
Radek Krejci6d6556c2018-11-08 09:37:45 +01001945 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001946 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001947 case LY_STMT_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001948 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &restr->dsc, Y_STR_ARG, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001949 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001950 case LY_STMT_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001951 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &restr->ref, Y_STR_ARG, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001952 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001953 case LY_STMT_ERROR_APP_TAG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001954 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_ERRTAG, 0, &restr->eapptag, Y_STR_ARG, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001955 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001956 case LY_STMT_ERROR_MESSAGE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001957 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_ERRMSG, 0, &restr->emsg, Y_STR_ARG, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001958 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001959 case LY_STMT_MODIFIER:
Radek Krejci335332a2019-09-05 13:03:35 +02001960 PARSER_CHECK_STMTVER2_RET(ctx, "modifier", "pattern");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001961 LY_CHECK_RET(parse_type_pattern_modifier(ctx, data, &restr->arg, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001962 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001963 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001964 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001965 break;
1966 default:
David Sedlákb3077192019-06-19 10:55:37 +02001967 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "pattern");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001968 return LY_EVALID;
1969 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001970 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001971 return ret;
1972}
1973
Michal Vaskoea5abea2018-09-18 13:10:54 +02001974/**
1975 * @brief Parse the type statement.
1976 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001977 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001978 * @param[in,out] data Data to read from, always moved to currently handled character.
1979 * @param[in,out] type Type to wrote to.
1980 *
1981 * @return LY_ERR values.
1982 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001983static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001984parse_type(struct lys_yang_parser_ctx *ctx, const char **data, struct lysp_type *type)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001985{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001986 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001987 char *buf, *word;
Michal Vasko004d3152020-06-11 19:59:22 +02001988 const char *str_path = NULL;
Radek Krejciefd22f62018-09-27 11:47:58 +02001989 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001990 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001991 struct lysp_type *nest_type;
1992
1993 if (type->name) {
David Sedlákb3077192019-06-19 10:55:37 +02001994 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "type");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001995 return LY_EVALID;
1996 }
1997
1998 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001999 LY_CHECK_RET(get_argument(ctx, data, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02002000 INSERT_WORD(ctx, buf, type->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002001
Radek Krejci6d6556c2018-11-08 09:37:45 +01002002 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002003 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002004 case LY_STMT_BASE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002005 LY_CHECK_RET(parse_text_fields(ctx, data, LYEXT_SUBSTMT_BASE, &type->bases, Y_PREF_IDENTIF_ARG, &type->exts));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002006 type->flags |= LYS_SET_BASE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002007 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002008 case LY_STMT_BIT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002009 LY_CHECK_RET(parse_type_enum(ctx, data, kw, &type->bits));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002010 type->flags |= LYS_SET_BIT;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002011 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002012 case LY_STMT_ENUM:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002013 LY_CHECK_RET(parse_type_enum(ctx, data, kw, &type->enums));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002014 type->flags |= LYS_SET_ENUM;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002015 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002016 case LY_STMT_FRACTION_DIGITS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002017 LY_CHECK_RET(parse_type_fracdigits(ctx, data, &type->fraction_digits, &type->exts));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002018 type->flags |= LYS_SET_FRDIGITS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002019 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002020 case LY_STMT_LENGTH:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002021 if (type->length) {
David Sedlákb3077192019-06-19 10:55:37 +02002022 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002023 return LY_EVALID;
2024 }
2025 type->length = calloc(1, sizeof *type->length);
Radek Krejci44ceedc2018-10-02 15:54:31 +02002026 LY_CHECK_ERR_RET(!type->length, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002027
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002028 LY_CHECK_RET(parse_restr(ctx, data, kw, type->length));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002029 type->flags |= LYS_SET_LENGTH;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002030 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002031 case LY_STMT_PATH:
Michal Vasko004d3152020-06-11 19:59:22 +02002032 if (type->path) {
2033 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, lyext_substmt2str(LYEXT_SUBSTMT_PATH));
2034 return LY_EVALID;
2035 }
2036
2037 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_PATH, 0, &str_path, Y_STR_ARG, &type->exts));
2038 ret = ly_path_parse(ctx->ctx, str_path, 0, LY_PATH_BEGIN_EITHER, LY_PATH_LREF_TRUE,
2039 LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_LEAFREF, &type->path);
2040 lydict_remove(ctx->ctx, str_path);
2041 LY_CHECK_RET(ret);
Radek Krejcid505e3d2018-11-13 09:04:17 +01002042 type->flags |= LYS_SET_PATH;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002043 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002044 case LY_STMT_PATTERN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002045 LY_CHECK_RET(parse_type_pattern(ctx, data, &type->patterns));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002046 type->flags |= LYS_SET_PATTERN;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002047 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002048 case LY_STMT_RANGE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002049 if (type->range) {
David Sedlákb3077192019-06-19 10:55:37 +02002050 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002051 return LY_EVALID;
2052 }
2053 type->range = calloc(1, sizeof *type->range);
David Sedlák7a8b2472019-07-11 15:08:34 +02002054 LY_CHECK_ERR_RET(!type->range, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002055
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002056 LY_CHECK_RET(parse_restr(ctx, data, kw, type->range));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002057 type->flags |= LYS_SET_RANGE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002058 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002059 case LY_STMT_REQUIRE_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002060 LY_CHECK_RET(parse_type_reqinstance(ctx, data, &type->require_instance, &type->flags, &type->exts));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002061 /* LYS_SET_REQINST checked and set inside parse_type_reqinstance() */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002062 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002063 case LY_STMT_TYPE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002064 LY_ARRAY_NEW_RET(ctx->ctx, type->types, nest_type, LY_EMEM);
2065 LY_CHECK_RET(parse_type(ctx, data, nest_type));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002066 type->flags |= LYS_SET_TYPE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002067 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002068 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002069 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &type->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002070 break;
2071 default:
David Sedlákb3077192019-06-19 10:55:37 +02002072 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "type");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002073 return LY_EVALID;
2074 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002075 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002076 return ret;
2077}
2078
Michal Vaskoea5abea2018-09-18 13:10:54 +02002079/**
2080 * @brief Parse the leaf statement.
2081 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002082 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002083 * @param[in,out] data Data to read from, always moved to currently handled character.
2084 * @param[in,out] siblings Siblings to add to.
2085 *
2086 * @return LY_ERR values.
2087 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002088LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01002089parse_leaf(struct lys_yang_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002090{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002091 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002092 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002093 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002094 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002095 struct lysp_node_leaf *leaf;
2096
David Sedlák60adc092019-08-06 15:57:02 +02002097 /* create new leaf structure */
David Sedlákb9eeb9c2019-09-13 11:17:19 +02002098 LY_LIST_NEW_RET(ctx->ctx, siblings, leaf, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002099 leaf->nodetype = LYS_LEAF;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002100 leaf->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002101
Michal Vasko7fbc8162018-09-17 10:35:16 +02002102 /* get name */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002103 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02002104 INSERT_WORD(ctx, buf, leaf->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002105
2106 /* parse substatements */
Radek Krejci6d6556c2018-11-08 09:37:45 +01002107 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002108 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002109 case LY_STMT_CONFIG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002110 LY_CHECK_RET(parse_config(ctx, data, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002111 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002112 case LY_STMT_DEFAULT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002113 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DEFAULT, 0, &leaf->dflt, Y_STR_ARG, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002114 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002115 case LY_STMT_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002116 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &leaf->dsc, Y_STR_ARG, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002117 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002118 case LY_STMT_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002119 LY_CHECK_RET(parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &leaf->iffeatures, Y_STR_ARG, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002120 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002121 case LY_STMT_MANDATORY:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002122 LY_CHECK_RET(parse_mandatory(ctx, data, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002123 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002124 case LY_STMT_MUST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002125 LY_CHECK_RET(parse_restrs(ctx, data, kw, &leaf->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002126 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002127 case LY_STMT_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002128 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &leaf->ref, Y_STR_ARG, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002129 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002130 case LY_STMT_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002131 LY_CHECK_RET(parse_status(ctx, data, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002132 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002133 case LY_STMT_TYPE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002134 LY_CHECK_RET(parse_type(ctx, data, &leaf->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002135 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002136 case LY_STMT_UNITS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002137 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_UNITS, 0, &leaf->units, Y_STR_ARG, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002138 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002139 case LY_STMT_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002140 LY_CHECK_RET(parse_when(ctx, data, &leaf->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002141 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002142 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002143 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002144 break;
2145 default:
David Sedlákb3077192019-06-19 10:55:37 +02002146 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "leaf");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002147 return LY_EVALID;
2148 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002149 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002150 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01002151checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002152 /* mandatory substatements */
2153 if (!leaf->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002154 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "leaf");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002155 return LY_EVALID;
2156 }
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01002157 if ((leaf->flags & LYS_MAND_TRUE) && (leaf->dflt)) {
David Sedlákb3077192019-06-19 10:55:37 +02002158 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMSCOMB, "mandatory", "default", "leaf");
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01002159 return LY_EVALID;
2160 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002161
2162 return ret;
2163}
2164
Michal Vaskoea5abea2018-09-18 13:10:54 +02002165/**
2166 * @brief Parse the max-elements statement.
2167 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002168 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002169 * @param[in,out] data Data to read from, always moved to currently handled character.
2170 * @param[in,out] max Value to write to.
2171 * @param[in,out] flags Flags to write to.
2172 * @param[in,out] exts Extension instances to add to.
2173 *
2174 * @return LY_ERR values.
2175 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002176LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01002177parse_maxelements(struct lys_yang_parser_ctx *ctx, const char **data, uint32_t *max, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002178{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002179 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002180 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02002181 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002182 unsigned long int num;
Radek Krejcid6b76452019-09-03 17:03:03 +02002183 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002184
2185 if (*flags & LYS_SET_MAX) {
David Sedlákb3077192019-06-19 10:55:37 +02002186 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002187 return LY_EVALID;
2188 }
2189 *flags |= LYS_SET_MAX;
2190
2191 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002192 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002193
2194 if (!word_len || (word[0] == '0') || ((word[0] != 'u') && !isdigit(word[0]))) {
David Sedlákb3077192019-06-19 10:55:37 +02002195 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002196 free(buf);
2197 return LY_EVALID;
2198 }
2199
Radek Krejci7f9b6512019-09-18 13:11:09 +02002200 if (ly_strncmp("unbounded", word, word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002201 errno = 0;
2202 num = strtoul(word, &ptr, 10);
2203 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02002204 if ((size_t)(ptr - word) != word_len) {
David Sedlákb3077192019-06-19 10:55:37 +02002205 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002206 free(buf);
2207 return LY_EVALID;
2208 }
2209 if ((errno == ERANGE) || (num > UINT32_MAX)) {
David Sedlákb3077192019-06-19 10:55:37 +02002210 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002211 free(buf);
2212 return LY_EVALID;
2213 }
2214
2215 *max = num;
2216 }
2217 free(buf);
2218
Radek Krejci6d6556c2018-11-08 09:37:45 +01002219 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002220 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002221 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002222 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_MAX, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002223 break;
2224 default:
David Sedlákb3077192019-06-19 10:55:37 +02002225 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002226 return LY_EVALID;
2227 }
2228 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002229 return ret;
2230}
2231
Michal Vaskoea5abea2018-09-18 13:10:54 +02002232/**
2233 * @brief Parse the min-elements statement.
2234 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002235 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002236 * @param[in,out] data Data to read from, always moved to currently handled character.
2237 * @param[in,out] min Value to write to.
2238 * @param[in,out] flags Flags to write to.
2239 * @param[in,out] exts Extension instances to add to.
2240 *
2241 * @return LY_ERR values.
2242 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002243LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01002244parse_minelements(struct lys_yang_parser_ctx *ctx, const char **data, uint32_t *min, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002245{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002246 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002247 char *buf, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02002248 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002249 unsigned long int num;
Radek Krejcid6b76452019-09-03 17:03:03 +02002250 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002251
2252 if (*flags & LYS_SET_MIN) {
David Sedlákb3077192019-06-19 10:55:37 +02002253 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002254 return LY_EVALID;
2255 }
2256 *flags |= LYS_SET_MIN;
2257
2258 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002259 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002260
2261 if (!word_len || !isdigit(word[0]) || ((word[0] == '0') && (word_len > 1))) {
David Sedlákb3077192019-06-19 10:55:37 +02002262 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002263 free(buf);
2264 return LY_EVALID;
2265 }
2266
2267 errno = 0;
2268 num = strtoul(word, &ptr, 10);
2269 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02002270 if ((size_t)(ptr - word) != word_len) {
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 if ((errno == ERANGE) || (num > UINT32_MAX)) {
David Sedlákb3077192019-06-19 10:55:37 +02002276 LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002277 free(buf);
2278 return LY_EVALID;
2279 }
2280 *min = num;
2281 free(buf);
2282
Radek Krejci6d6556c2018-11-08 09:37:45 +01002283 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002284 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002285 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002286 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_MIN, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002287 break;
2288 default:
David Sedlákb3077192019-06-19 10:55:37 +02002289 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002290 return LY_EVALID;
2291 }
2292 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002293 return ret;
2294}
2295
Michal Vaskoea5abea2018-09-18 13:10:54 +02002296/**
2297 * @brief Parse the ordered-by statement.
2298 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002299 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002300 * @param[in,out] data Data to read from, always moved to currently handled character.
2301 * @param[in,out] flags Flags to write to.
2302 * @param[in,out] exts Extension instances to add to.
2303 *
2304 * @return LY_ERR values.
2305 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002306static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01002307parse_orderedby(struct lys_yang_parser_ctx *ctx, const char **data, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002308{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002309 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002310 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002311 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002312 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002313
2314 if (*flags & LYS_ORDBY_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02002315 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002316 return LY_EVALID;
2317 }
2318
2319 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002320 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002321
2322 if ((word_len == 6) && !strncmp(word, "system", word_len)) {
2323 *flags |= LYS_ORDBY_SYSTEM;
2324 } else if ((word_len == 4) && !strncmp(word, "user", word_len)) {
2325 *flags |= LYS_ORDBY_USER;
2326 } else {
David Sedlákb3077192019-06-19 10:55:37 +02002327 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002328 free(buf);
2329 return LY_EVALID;
2330 }
2331 free(buf);
2332
Radek Krejci6d6556c2018-11-08 09:37:45 +01002333 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002334 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002335 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002336 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_ORDEREDBY, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002337 break;
2338 default:
David Sedlákb3077192019-06-19 10:55:37 +02002339 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002340 return LY_EVALID;
2341 }
2342 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002343 return ret;
2344}
2345
Michal Vaskoea5abea2018-09-18 13:10:54 +02002346/**
2347 * @brief Parse the leaf-list statement.
2348 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002349 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002350 * @param[in,out] data Data to read from, always moved to currently handled character.
2351 * @param[in,out] siblings Siblings to add to.
2352 *
2353 * @return LY_ERR values.
2354 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002355LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01002356parse_leaflist(struct lys_yang_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002357{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002358 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002359 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002360 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002361 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002362 struct lysp_node_leaflist *llist;
2363
David Sedlák60adc092019-08-06 15:57:02 +02002364 /* create new leaf-list structure */
David Sedlákb9eeb9c2019-09-13 11:17:19 +02002365 LY_LIST_NEW_RET(ctx->ctx, siblings, llist, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002366 llist->nodetype = LYS_LEAFLIST;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002367 llist->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002368
Michal Vasko7fbc8162018-09-17 10:35:16 +02002369 /* get name */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002370 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02002371 INSERT_WORD(ctx, buf, llist->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002372
2373 /* parse substatements */
Radek Krejci6d6556c2018-11-08 09:37:45 +01002374 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002375 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002376 case LY_STMT_CONFIG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002377 LY_CHECK_RET(parse_config(ctx, data, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002378 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002379 case LY_STMT_DEFAULT:
Radek Krejci335332a2019-09-05 13:03:35 +02002380 PARSER_CHECK_STMTVER2_RET(ctx, "default", "leaf-list");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002381 LY_CHECK_RET(parse_text_fields(ctx, data, LYEXT_SUBSTMT_DEFAULT, &llist->dflts, Y_STR_ARG, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002382 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002383 case LY_STMT_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002384 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &llist->dsc, Y_STR_ARG, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002385 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002386 case LY_STMT_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002387 LY_CHECK_RET(parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &llist->iffeatures, Y_STR_ARG, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002388 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002389 case LY_STMT_MAX_ELEMENTS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002390 LY_CHECK_RET(parse_maxelements(ctx, data, &llist->max, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002391 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002392 case LY_STMT_MIN_ELEMENTS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002393 LY_CHECK_RET(parse_minelements(ctx, data, &llist->min, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002394 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002395 case LY_STMT_MUST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002396 LY_CHECK_RET(parse_restrs(ctx, data, kw, &llist->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002397 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002398 case LY_STMT_ORDERED_BY:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002399 LY_CHECK_RET(parse_orderedby(ctx, data, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002400 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002401 case LY_STMT_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002402 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &llist->ref, Y_STR_ARG, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002403 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002404 case LY_STMT_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002405 LY_CHECK_RET(parse_status(ctx, data, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002406 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002407 case LY_STMT_TYPE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002408 LY_CHECK_RET(parse_type(ctx, data, &llist->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002409 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002410 case LY_STMT_UNITS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002411 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_UNITS, 0, &llist->units, 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_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002414 LY_CHECK_RET(parse_when(ctx, data, &llist->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002415 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002416 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002417 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002418 break;
2419 default:
David Sedlákb3077192019-06-19 10:55:37 +02002420 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "llist");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002421 return LY_EVALID;
2422 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002423 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002424 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01002425checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002426 /* mandatory substatements */
2427 if (!llist->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002428 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "leaf-list");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002429 return LY_EVALID;
2430 }
Radek Krejci0e5d8382018-11-28 16:37:53 +01002431 if ((llist->min) && (llist->dflts)) {
David Sedlákb3077192019-06-19 10:55:37 +02002432 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMSCOMB, "min-elements", "default", "leaf-list");
Radek Krejci0e5d8382018-11-28 16:37:53 +01002433 return LY_EVALID;
2434 }
Radek Krejcidf6cad12018-11-28 17:10:55 +01002435 if (llist->max && llist->min > llist->max) {
David Sedlákb3077192019-06-19 10:55:37 +02002436 LOGVAL_PARSER(ctx, LYVE_SEMANTICS,
Radek Krejcidf6cad12018-11-28 17:10:55 +01002437 "Invalid combination of min-elements and max-elements: min value %u is bigger than the max value %u.",
2438 llist->min, llist->max);
2439 return LY_EVALID;
2440 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002441
2442 return ret;
2443}
2444
Michal Vaskoea5abea2018-09-18 13:10:54 +02002445/**
2446 * @brief Parse the refine statement.
2447 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002448 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002449 * @param[in,out] data Data to read from, always moved to currently handled character.
2450 * @param[in,out] refines Refines to add to.
2451 *
2452 * @return LY_ERR values.
2453 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002454static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01002455parse_refine(struct lys_yang_parser_ctx *ctx, const char **data, struct lysp_refine **refines)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002456{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002457 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002458 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002459 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002460 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002461 struct lysp_refine *rf;
2462
Radek Krejci2c4e7172018-10-19 15:56:26 +02002463 LY_ARRAY_NEW_RET(ctx->ctx, *refines, rf, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002464
2465 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002466 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vaskob36053d2020-03-26 15:49:30 +01002467 CHECK_NONEMPTY(ctx, word_len, "refine");
Radek Krejci44ceedc2018-10-02 15:54:31 +02002468 INSERT_WORD(ctx, buf, rf->nodeid, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002469
Radek Krejci6d6556c2018-11-08 09:37:45 +01002470 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002471 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002472 case LY_STMT_CONFIG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002473 LY_CHECK_RET(parse_config(ctx, data, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002474 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002475 case LY_STMT_DEFAULT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002476 LY_CHECK_RET(parse_text_fields(ctx, data, LYEXT_SUBSTMT_DEFAULT, &rf->dflts, Y_STR_ARG, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002477 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002478 case LY_STMT_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002479 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &rf->dsc, Y_STR_ARG, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002480 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002481 case LY_STMT_IF_FEATURE:
Radek Krejci335332a2019-09-05 13:03:35 +02002482 PARSER_CHECK_STMTVER2_RET(ctx, "if-feature", "refine");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002483 LY_CHECK_RET(parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &rf->iffeatures, Y_STR_ARG, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002484 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002485 case LY_STMT_MAX_ELEMENTS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002486 LY_CHECK_RET(parse_maxelements(ctx, data, &rf->max, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002487 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002488 case LY_STMT_MIN_ELEMENTS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002489 LY_CHECK_RET(parse_minelements(ctx, data, &rf->min, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002490 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002491 case LY_STMT_MUST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002492 LY_CHECK_RET(parse_restrs(ctx, data, kw, &rf->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002493 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002494 case LY_STMT_MANDATORY:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002495 LY_CHECK_RET(parse_mandatory(ctx, data, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002496 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002497 case LY_STMT_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002498 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &rf->ref, Y_STR_ARG, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002499 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002500 case LY_STMT_PRESENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002501 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_PRESENCE, 0, &rf->presence, Y_STR_ARG, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002502 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002503 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002504 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002505 break;
2506 default:
David Sedlákb3077192019-06-19 10:55:37 +02002507 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "refine");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002508 return LY_EVALID;
2509 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002510 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002511 return ret;
2512}
2513
Michal Vaskoea5abea2018-09-18 13:10:54 +02002514/**
2515 * @brief Parse the typedef statement.
2516 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002517 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002518 * @param[in,out] data Data to read from, always moved to currently handled character.
2519 * @param[in,out] typedefs Typedefs to add to.
2520 *
2521 * @return LY_ERR values.
2522 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002523static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01002524parse_typedef(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, const char **data, struct lysp_tpdf **typedefs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002525{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002526 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002527 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002528 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002529 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002530 struct lysp_tpdf *tpdf;
2531
Radek Krejci2c4e7172018-10-19 15:56:26 +02002532 LY_ARRAY_NEW_RET(ctx->ctx, *typedefs, tpdf, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002533
2534 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002535 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02002536 INSERT_WORD(ctx, buf, tpdf->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002537
2538 /* parse substatements */
Radek Krejci6d6556c2018-11-08 09:37:45 +01002539 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002540 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002541 case LY_STMT_DEFAULT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002542 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DEFAULT, 0, &tpdf->dflt, Y_STR_ARG, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002543 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002544 case LY_STMT_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002545 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &tpdf->dsc, Y_STR_ARG, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002546 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002547 case LY_STMT_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002548 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &tpdf->ref, Y_STR_ARG, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002549 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002550 case LY_STMT_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002551 LY_CHECK_RET(parse_status(ctx, data, &tpdf->flags, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002552 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002553 case LY_STMT_TYPE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002554 LY_CHECK_RET(parse_type(ctx, data, &tpdf->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002555 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002556 case LY_STMT_UNITS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002557 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_UNITS, 0, &tpdf->units, 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_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002560 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002561 break;
2562 default:
David Sedlákb3077192019-06-19 10:55:37 +02002563 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "typedef");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002564 return LY_EVALID;
2565 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002566 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002567 LY_CHECK_RET(ret);
Radek Krejcibbe09a92018-11-08 09:36:54 +01002568checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002569 /* mandatory substatements */
2570 if (!tpdf->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002571 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "typedef");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002572 return LY_EVALID;
2573 }
2574
Radek Krejcibbe09a92018-11-08 09:36:54 +01002575 /* store data for collision check */
Michal Vasko1bf09392020-03-27 12:38:10 +01002576 if (parent && !(parent->nodetype & (LYS_GROUPING | LYS_RPC | LYS_ACTION | LYS_INOUT | LYS_NOTIF))) {
David Sedláke8b74df2019-08-14 14:18:22 +02002577 LY_CHECK_RET(ly_set_add(&ctx->tpdfs_nodes, parent, 0) == -1, LY_EMEM);
Radek Krejcibbe09a92018-11-08 09:36:54 +01002578 }
2579
Michal Vasko7fbc8162018-09-17 10:35:16 +02002580 return ret;
2581}
2582
Michal Vaskoea5abea2018-09-18 13:10:54 +02002583/**
2584 * @brief Parse the input or output statement.
2585 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002586 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002587 * @param[in,out] data Data to read from, always moved to currently handled character.
2588 * @param[in] kw Type of this particular keyword
2589 * @param[in,out] inout_p Input/output pointer to write to.
2590 *
2591 * @return LY_ERR values.
2592 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002593static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01002594parse_inout(struct lys_yang_parser_ctx *ctx, const char **data, enum ly_stmt inout_kw, struct lysp_node *parent, struct lysp_action_inout *inout_p)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002595{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002596 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002597 char *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002598 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002599 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002600
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002601 if (inout_p->nodetype) {
David Sedlákb3077192019-06-19 10:55:37 +02002602 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(inout_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002603 return LY_EVALID;
2604 }
2605
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002606 /* initiate structure */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002607 inout_p->nodetype = &((struct lysp_action*)parent)->input == inout_p ? LYS_INPUT : LYS_OUTPUT;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002608 inout_p->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002609
2610 /* parse substatements */
Radek Krejci7fc68292019-06-12 13:51:09 +02002611 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002612 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002613 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02002614 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", ly_stmt2str(inout_kw));
Radek Krejci10113652018-11-14 16:56:50 +01002615 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02002616 case LY_STMT_ANYXML:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002617 LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002618 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002619 case LY_STMT_CHOICE:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002620 LY_CHECK_RET(parse_choice(ctx, data, (struct lysp_node*)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002621 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002622 case LY_STMT_CONTAINER:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002623 LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)inout_p, &inout_p->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002624 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002625 case LY_STMT_LEAF:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002626 LY_CHECK_RET(parse_leaf(ctx, data, (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_LEAF_LIST:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002629 LY_CHECK_RET(parse_leaflist(ctx, data, (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_LIST:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002632 LY_CHECK_RET(parse_list(ctx, data, (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_USES:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002635 LY_CHECK_RET(parse_uses(ctx, data, (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_TYPEDEF:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002638 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node*)inout_p, data, &inout_p->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002639 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002640 case LY_STMT_MUST:
Radek Krejci335332a2019-09-05 13:03:35 +02002641 PARSER_CHECK_STMTVER2_RET(ctx, "must", ly_stmt2str(inout_kw));
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002642 LY_CHECK_RET(parse_restrs(ctx, data, kw, &inout_p->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002643 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002644 case LY_STMT_GROUPING:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002645 LY_CHECK_RET(parse_grouping(ctx, data, (struct lysp_node*)inout_p, &inout_p->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002646 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002647 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002648 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &inout_p->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002649 break;
2650 default:
David Sedlákb3077192019-06-19 10:55:37 +02002651 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(inout_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002652 return LY_EVALID;
2653 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002654 }
Radek Krejci7fc68292019-06-12 13:51:09 +02002655 LY_CHECK_RET(ret);
Michal Vaskob83af8a2020-01-06 09:49:22 +01002656
Radek Krejci7fc68292019-06-12 13:51:09 +02002657checks:
Radek Krejcie86bf772018-12-14 11:39:53 +01002658 /* finalize parent pointers to the reallocated items */
Michal Vaskob36053d2020-03-26 15:49:30 +01002659 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 +02002660
Michal Vaskob83af8a2020-01-06 09:49:22 +01002661 if (!inout_p->data) {
2662 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "data-def-stmt", ly_stmt2str(inout_kw));
2663 return LY_EVALID;
2664 }
2665
Michal Vasko7fbc8162018-09-17 10:35:16 +02002666 return ret;
2667}
2668
Michal Vaskoea5abea2018-09-18 13:10:54 +02002669/**
2670 * @brief Parse the action statement.
2671 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002672 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002673 * @param[in,out] data Data to read from, always moved to currently handled character.
2674 * @param[in,out] actions Actions to add to.
2675 *
2676 * @return LY_ERR values.
2677 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002678LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01002679parse_action(struct lys_yang_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_action **actions)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002680{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002681 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002682 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002683 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002684 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002685 struct lysp_action *act;
2686
Radek Krejci2c4e7172018-10-19 15:56:26 +02002687 LY_ARRAY_NEW_RET(ctx->ctx, *actions, act, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002688
2689 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002690 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02002691 INSERT_WORD(ctx, buf, act->name, word, word_len);
Michal Vasko1bf09392020-03-27 12:38:10 +01002692 act->nodetype = parent ? LYS_ACTION : LYS_RPC;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002693 act->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002694
Radek Krejci7fc68292019-06-12 13:51:09 +02002695 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002696 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002697 case LY_STMT_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002698 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &act->dsc, Y_STR_ARG, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002699 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002700 case LY_STMT_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002701 LY_CHECK_RET(parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &act->iffeatures, Y_STR_ARG, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002702 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002703 case LY_STMT_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002704 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &act->ref, Y_STR_ARG, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002705 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002706 case LY_STMT_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002707 LY_CHECK_RET(parse_status(ctx, data, &act->flags, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002708 break;
2709
Radek Krejcid6b76452019-09-03 17:03:03 +02002710 case LY_STMT_INPUT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002711 LY_CHECK_RET(parse_inout(ctx, data, kw, (struct lysp_node*)act, &act->input));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002712 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002713 case LY_STMT_OUTPUT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002714 LY_CHECK_RET(parse_inout(ctx, data, kw, (struct lysp_node*)act, &act->output));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002715 break;
2716
Radek Krejcid6b76452019-09-03 17:03:03 +02002717 case LY_STMT_TYPEDEF:
Radek Krejcibbe09a92018-11-08 09:36:54 +01002718 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node*)act, data, &act->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002719 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002720 case LY_STMT_GROUPING:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002721 LY_CHECK_RET(parse_grouping(ctx, data, (struct lysp_node*)act, &act->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002722 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002723 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002724 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002725 break;
2726 default:
David Sedlákb3077192019-06-19 10:55:37 +02002727 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), parent ? "action" : "rpc");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002728 return LY_EVALID;
2729 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002730 }
Radek Krejci7fc68292019-06-12 13:51:09 +02002731 LY_CHECK_RET(ret);
2732checks:
Radek Krejcie86bf772018-12-14 11:39:53 +01002733 /* finalize parent pointers to the reallocated items */
Michal Vaskob36053d2020-03-26 15:49:30 +01002734 LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, act->groupings, NULL, NULL, NULL));
Radek Krejci7fc68292019-06-12 13:51:09 +02002735
Michal Vasko7fbc8162018-09-17 10:35:16 +02002736 return ret;
2737}
2738
Michal Vaskoea5abea2018-09-18 13:10:54 +02002739/**
2740 * @brief Parse the notification statement.
2741 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002742 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002743 * @param[in,out] data Data to read from, always moved to currently handled character.
2744 * @param[in,out] notifs Notifications to add to.
2745 *
2746 * @return LY_ERR values.
2747 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002748LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01002749parse_notif(struct lys_yang_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_notif **notifs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002750{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002751 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002752 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002753 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002754 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002755 struct lysp_notif *notif;
2756
Radek Krejci2c4e7172018-10-19 15:56:26 +02002757 LY_ARRAY_NEW_RET(ctx->ctx, *notifs, notif, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002758
2759 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002760 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02002761 INSERT_WORD(ctx, buf, notif->name, word, word_len);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002762 notif->nodetype = LYS_NOTIF;
2763 notif->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002764
Radek Krejci7fc68292019-06-12 13:51:09 +02002765 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002766 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002767 case LY_STMT_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002768 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &notif->dsc, Y_STR_ARG, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002769 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002770 case LY_STMT_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002771 LY_CHECK_RET(parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &notif->iffeatures, Y_STR_ARG, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002772 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002773 case LY_STMT_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002774 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &notif->ref, Y_STR_ARG, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002775 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002776 case LY_STMT_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002777 LY_CHECK_RET(parse_status(ctx, data, &notif->flags, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002778 break;
2779
Radek Krejcid6b76452019-09-03 17:03:03 +02002780 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02002781 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "notification");
Radek Krejci10113652018-11-14 16:56:50 +01002782 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02002783 case LY_STMT_ANYXML:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002784 LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002785 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002786 case LY_STMT_CHOICE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002787 LY_CHECK_RET(parse_case(ctx, data, (struct lysp_node*)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002788 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002789 case LY_STMT_CONTAINER:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002790 LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002791 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002792 case LY_STMT_LEAF:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002793 LY_CHECK_RET(parse_leaf(ctx, data, (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_LEAF_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002796 LY_CHECK_RET(parse_leaflist(ctx, data, (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_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002799 LY_CHECK_RET(parse_list(ctx, data, (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_USES:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002802 LY_CHECK_RET(parse_uses(ctx, data, (struct lysp_node*)notif, &notif->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002803 break;
2804
Radek Krejcid6b76452019-09-03 17:03:03 +02002805 case LY_STMT_MUST:
Radek Krejci335332a2019-09-05 13:03:35 +02002806 PARSER_CHECK_STMTVER2_RET(ctx, "must", "notification");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002807 LY_CHECK_RET(parse_restrs(ctx, data, kw, &notif->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002808 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002809 case LY_STMT_TYPEDEF:
Radek Krejcibbe09a92018-11-08 09:36:54 +01002810 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node*)notif, data, &notif->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002811 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002812 case LY_STMT_GROUPING:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002813 LY_CHECK_RET(parse_grouping(ctx, data, (struct lysp_node*)notif, &notif->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002814 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002815 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002816 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002817 break;
2818 default:
David Sedlákb3077192019-06-19 10:55:37 +02002819 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "notification");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002820 return LY_EVALID;
2821 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002822 }
Radek Krejci7fc68292019-06-12 13:51:09 +02002823 LY_CHECK_RET(ret);
2824checks:
Radek Krejcie86bf772018-12-14 11:39:53 +01002825 /* finalize parent pointers to the reallocated items */
Michal Vaskob36053d2020-03-26 15:49:30 +01002826 LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, notif->groupings, NULL, NULL, NULL));
Radek Krejci7fc68292019-06-12 13:51:09 +02002827
Michal Vasko7fbc8162018-09-17 10:35:16 +02002828 return ret;
2829}
2830
Michal Vaskoea5abea2018-09-18 13:10:54 +02002831/**
2832 * @brief Parse the grouping statement.
2833 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002834 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002835 * @param[in,out] data Data to read from, always moved to currently handled character.
2836 * @param[in,out] groupings Groupings to add to.
2837 *
2838 * @return LY_ERR values.
2839 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002840LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01002841parse_grouping(struct lys_yang_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_grp **groupings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002842{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002843 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002844 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002845 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002846 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002847 struct lysp_grp *grp;
2848
Radek Krejci2c4e7172018-10-19 15:56:26 +02002849 LY_ARRAY_NEW_RET(ctx->ctx, *groupings, grp, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002850
2851 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002852 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02002853 INSERT_WORD(ctx, buf, grp->name, word, word_len);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002854 grp->nodetype = LYS_GROUPING;
2855 grp->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002856
Radek Krejci7fc68292019-06-12 13:51:09 +02002857 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002858 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002859 case LY_STMT_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002860 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &grp->dsc, Y_STR_ARG, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002861 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002862 case LY_STMT_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002863 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &grp->ref, Y_STR_ARG, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002864 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002865 case LY_STMT_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002866 LY_CHECK_RET(parse_status(ctx, data, &grp->flags, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002867 break;
2868
Radek Krejcid6b76452019-09-03 17:03:03 +02002869 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02002870 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "grouping");
Radek Krejci10113652018-11-14 16:56:50 +01002871 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02002872 case LY_STMT_ANYXML:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002873 LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002874 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002875 case LY_STMT_CHOICE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002876 LY_CHECK_RET(parse_choice(ctx, data, (struct lysp_node*)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002877 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002878 case LY_STMT_CONTAINER:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002879 LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002880 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002881 case LY_STMT_LEAF:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002882 LY_CHECK_RET(parse_leaf(ctx, data, (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_LEAF_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002885 LY_CHECK_RET(parse_leaflist(ctx, data, (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_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002888 LY_CHECK_RET(parse_list(ctx, data, (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_USES:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002891 LY_CHECK_RET(parse_uses(ctx, data, (struct lysp_node*)grp, &grp->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002892 break;
2893
Radek Krejcid6b76452019-09-03 17:03:03 +02002894 case LY_STMT_TYPEDEF:
Radek Krejcibbe09a92018-11-08 09:36:54 +01002895 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node*)grp, data, &grp->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002896 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002897 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02002898 PARSER_CHECK_STMTVER2_RET(ctx, "action", "grouping");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002899 LY_CHECK_RET(parse_action(ctx, data, (struct lysp_node*)grp, &grp->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002900 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002901 case LY_STMT_GROUPING:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002902 LY_CHECK_RET(parse_grouping(ctx, data, (struct lysp_node*)grp, &grp->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002903 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002904 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02002905 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "grouping");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002906 LY_CHECK_RET(parse_notif(ctx, data, (struct lysp_node*)grp, &grp->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002907 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002908 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002909 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002910 break;
2911 default:
David Sedlákb3077192019-06-19 10:55:37 +02002912 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "grouping");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002913 return LY_EVALID;
2914 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002915 }
Radek Krejci7fc68292019-06-12 13:51:09 +02002916 LY_CHECK_RET(ret);
2917checks:
Radek Krejcie86bf772018-12-14 11:39:53 +01002918 /* finalize parent pointers to the reallocated items */
Michal Vaskob36053d2020-03-26 15:49:30 +01002919 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 +02002920
Michal Vasko7fbc8162018-09-17 10:35:16 +02002921 return ret;
2922}
2923
Michal Vaskoea5abea2018-09-18 13:10:54 +02002924/**
David Sedlák0d6de5a2019-07-22 13:25:44 +02002925 * @brief Parse the augment statement.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002926 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002927 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002928 * @param[in,out] data Data to read from, always moved to currently handled character.
2929 * @param[in,out] augments Augments to add to.
2930 *
2931 * @return LY_ERR values.
2932 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002933LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01002934parse_augment(struct lys_yang_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_augment **augments)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002935{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002936 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002937 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002938 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002939 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002940 struct lysp_augment *aug;
2941
Radek Krejci2c4e7172018-10-19 15:56:26 +02002942 LY_ARRAY_NEW_RET(ctx->ctx, *augments, aug, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002943
2944 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002945 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vaskob36053d2020-03-26 15:49:30 +01002946 CHECK_NONEMPTY(ctx, word_len, "augment");
Radek Krejci44ceedc2018-10-02 15:54:31 +02002947 INSERT_WORD(ctx, buf, aug->nodeid, word, word_len);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002948 aug->nodetype = LYS_AUGMENT;
2949 aug->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002950
Radek Krejci7fc68292019-06-12 13:51:09 +02002951 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002952 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002953 case LY_STMT_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002954 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &aug->dsc, Y_STR_ARG, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002955 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002956 case LY_STMT_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002957 LY_CHECK_RET(parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &aug->iffeatures, Y_STR_ARG, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002958 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002959 case LY_STMT_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002960 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &aug->ref, Y_STR_ARG, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002961 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002962 case LY_STMT_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002963 LY_CHECK_RET(parse_status(ctx, data, &aug->flags, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002964 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002965 case LY_STMT_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002966 LY_CHECK_RET(parse_when(ctx, data, &aug->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002967 break;
2968
Radek Krejcid6b76452019-09-03 17:03:03 +02002969 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02002970 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "augment");
Radek Krejci10113652018-11-14 16:56:50 +01002971 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02002972 case LY_STMT_ANYXML:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002973 LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002974 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002975 case LY_STMT_CASE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002976 LY_CHECK_RET(parse_case(ctx, data, (struct lysp_node*)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002977 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002978 case LY_STMT_CHOICE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002979 LY_CHECK_RET(parse_choice(ctx, data, (struct lysp_node*)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002980 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002981 case LY_STMT_CONTAINER:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002982 LY_CHECK_RET(parse_container(ctx, data, (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_LEAF:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002985 LY_CHECK_RET(parse_leaf(ctx, data, (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_LEAF_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002988 LY_CHECK_RET(parse_leaflist(ctx, data, (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_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002991 LY_CHECK_RET(parse_list(ctx, data, (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_USES:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002994 LY_CHECK_RET(parse_uses(ctx, data, (struct lysp_node*)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002995 break;
2996
Radek Krejcid6b76452019-09-03 17:03:03 +02002997 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02002998 PARSER_CHECK_STMTVER2_RET(ctx, "action", "augment");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002999 LY_CHECK_RET(parse_action(ctx, data, (struct lysp_node*)aug, &aug->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003000 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003001 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003002 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "augment");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003003 LY_CHECK_RET(parse_notif(ctx, data, (struct lysp_node*)aug, &aug->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003004 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003005 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003006 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003007 break;
3008 default:
David Sedlákb3077192019-06-19 10:55:37 +02003009 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "augment");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003010 return LY_EVALID;
3011 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003012 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003013 LY_CHECK_RET(ret);
3014checks:
3015 /* finalize parent pointers to the reallocated items */
Michal Vaskob36053d2020-03-26 15:49:30 +01003016 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 +02003017
Michal Vasko7fbc8162018-09-17 10:35:16 +02003018 return ret;
3019}
3020
Michal Vaskoea5abea2018-09-18 13:10:54 +02003021/**
3022 * @brief Parse the uses statement.
3023 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003024 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003025 * @param[in,out] data Data to read from, always moved to currently handled character.
3026 * @param[in,out] siblings Siblings to add to.
3027 *
3028 * @return LY_ERR values.
3029 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003030LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01003031parse_uses(struct lys_yang_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003032{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003033 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003034 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003035 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003036 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003037 struct lysp_node_uses *uses;
3038
David Sedlák60adc092019-08-06 15:57:02 +02003039 /* create uses structure */
David Sedlákb9eeb9c2019-09-13 11:17:19 +02003040 LY_LIST_NEW_RET(ctx->ctx, siblings, uses, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003041 uses->nodetype = LYS_USES;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003042 uses->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003043
Michal Vasko7fbc8162018-09-17 10:35:16 +02003044 /* get name */
Radek Krejcid3ca0632019-04-16 16:54:54 +02003045 LY_CHECK_RET(get_argument(ctx, data, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003046 INSERT_WORD(ctx, buf, uses->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003047
3048 /* parse substatements */
Radek Krejci7fc68292019-06-12 13:51:09 +02003049 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003050 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003051 case LY_STMT_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003052 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &uses->dsc, Y_STR_ARG, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003053 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003054 case LY_STMT_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003055 LY_CHECK_RET(parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &uses->iffeatures, Y_STR_ARG, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003056 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003057 case LY_STMT_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003058 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &uses->ref, Y_STR_ARG, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003059 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003060 case LY_STMT_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003061 LY_CHECK_RET(parse_status(ctx, data, &uses->flags, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003062 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003063 case LY_STMT_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003064 LY_CHECK_RET(parse_when(ctx, data, &uses->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003065 break;
3066
Radek Krejcid6b76452019-09-03 17:03:03 +02003067 case LY_STMT_REFINE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003068 LY_CHECK_RET(parse_refine(ctx, data, &uses->refines));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003069 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003070 case LY_STMT_AUGMENT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003071 LY_CHECK_RET(parse_augment(ctx, data, (struct lysp_node*)uses, &uses->augments));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003072 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003073 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003074 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003075 break;
3076 default:
David Sedlákb3077192019-06-19 10:55:37 +02003077 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "uses");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003078 return LY_EVALID;
3079 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003080 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003081checks:
3082 /* finalize parent pointers to the reallocated items */
Michal Vaskob36053d2020-03-26 15:49:30 +01003083 LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, NULL, uses->augments, NULL, NULL));
Radek Krejci7fc68292019-06-12 13:51:09 +02003084
Michal Vasko7fbc8162018-09-17 10:35:16 +02003085 return ret;
3086}
3087
Michal Vaskoea5abea2018-09-18 13:10:54 +02003088/**
3089 * @brief Parse the case statement.
3090 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003091 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003092 * @param[in,out] data Data to read from, always moved to currently handled character.
3093 * @param[in,out] siblings Siblings to add to.
3094 *
3095 * @return LY_ERR values.
3096 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003097LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01003098parse_case(struct lys_yang_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003099{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003100 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003101 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003102 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003103 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003104 struct lysp_node_case *cas;
3105
David Sedlák60adc092019-08-06 15:57:02 +02003106 /* create new case structure */
David Sedlákb9eeb9c2019-09-13 11:17:19 +02003107 LY_LIST_NEW_RET(ctx->ctx, siblings, cas, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003108 cas->nodetype = LYS_CASE;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003109 cas->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003110
Michal Vasko7fbc8162018-09-17 10:35:16 +02003111 /* get name */
Radek Krejcid3ca0632019-04-16 16:54:54 +02003112 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003113 INSERT_WORD(ctx, buf, cas->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003114
3115 /* parse substatements */
Radek Krejci6d6556c2018-11-08 09:37:45 +01003116 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003117 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003118 case LY_STMT_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003119 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &cas->dsc, Y_STR_ARG, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003120 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003121 case LY_STMT_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003122 LY_CHECK_RET(parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &cas->iffeatures, Y_STR_ARG, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003123 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003124 case LY_STMT_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003125 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &cas->ref, Y_STR_ARG, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003126 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003127 case LY_STMT_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003128 LY_CHECK_RET(parse_status(ctx, data, &cas->flags, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003129 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003130 case LY_STMT_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003131 LY_CHECK_RET(parse_when(ctx, data, &cas->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003132 break;
3133
Radek Krejcid6b76452019-09-03 17:03:03 +02003134 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003135 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "case");
Radek Krejci10113652018-11-14 16:56:50 +01003136 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003137 case LY_STMT_ANYXML:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003138 LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003139 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003140 case LY_STMT_CHOICE:
Radek Krejcia9026eb2018-12-12 16:04:47 +01003141 LY_CHECK_RET(parse_choice(ctx, data, (struct lysp_node*)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003142 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003143 case LY_STMT_CONTAINER:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003144 LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003145 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003146 case LY_STMT_LEAF:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003147 LY_CHECK_RET(parse_leaf(ctx, data, (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_LEAF_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003150 LY_CHECK_RET(parse_leaflist(ctx, data, (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_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003153 LY_CHECK_RET(parse_list(ctx, data, (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_USES:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003156 LY_CHECK_RET(parse_uses(ctx, data, (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_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003159 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003160 break;
3161 default:
David Sedlákb3077192019-06-19 10:55:37 +02003162 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "case");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003163 return LY_EVALID;
3164 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003165 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003166 return ret;
3167}
3168
Michal Vaskoea5abea2018-09-18 13:10:54 +02003169/**
3170 * @brief Parse the choice statement.
3171 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003172 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003173 * @param[in,out] data Data to read from, always moved to currently handled character.
3174 * @param[in,out] siblings Siblings to add to.
3175 *
3176 * @return LY_ERR values.
3177 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003178LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01003179parse_choice(struct lys_yang_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003180{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003181 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003182 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003183 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003184 enum ly_stmt kw;
Radek Krejci44ceedc2018-10-02 15:54:31 +02003185 struct lysp_node_choice *choice;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003186
David Sedlák60adc092019-08-06 15:57:02 +02003187 /* create new choice structure */
David Sedlákb9eeb9c2019-09-13 11:17:19 +02003188 LY_LIST_NEW_RET(ctx->ctx, siblings, choice, next, LY_EMEM);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003189 choice->nodetype = LYS_CHOICE;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003190 choice->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003191
Michal Vasko7fbc8162018-09-17 10:35:16 +02003192 /* get name */
Radek Krejcid3ca0632019-04-16 16:54:54 +02003193 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003194 INSERT_WORD(ctx, buf, choice->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003195
3196 /* parse substatements */
Radek Krejcia9026eb2018-12-12 16:04:47 +01003197 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003198 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003199 case LY_STMT_CONFIG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003200 LY_CHECK_RET(parse_config(ctx, data, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003201 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003202 case LY_STMT_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003203 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &choice->dsc, Y_STR_ARG, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003204 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003205 case LY_STMT_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003206 LY_CHECK_RET(parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &choice->iffeatures, Y_STR_ARG, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003207 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003208 case LY_STMT_MANDATORY:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003209 LY_CHECK_RET(parse_mandatory(ctx, data, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003210 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003211 case LY_STMT_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003212 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &choice->ref, 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_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003215 LY_CHECK_RET(parse_status(ctx, data, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003216 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003217 case LY_STMT_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003218 LY_CHECK_RET(parse_when(ctx, data, &choice->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003219 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003220 case LY_STMT_DEFAULT:
Radek Krejcia9026eb2018-12-12 16:04:47 +01003221 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DEFAULT, 0, &choice->dflt, Y_PREF_IDENTIF_ARG, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003222 break;
3223
Radek Krejcid6b76452019-09-03 17:03:03 +02003224 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003225 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "choice");
Radek Krejci10113652018-11-14 16:56:50 +01003226 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003227 case LY_STMT_ANYXML:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003228 LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003229 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003230 case LY_STMT_CASE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003231 LY_CHECK_RET(parse_case(ctx, data, (struct lysp_node*)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003232 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003233 case LY_STMT_CHOICE:
Radek Krejci335332a2019-09-05 13:03:35 +02003234 PARSER_CHECK_STMTVER2_RET(ctx, "choice", "choice");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003235 LY_CHECK_RET(parse_choice(ctx, data, (struct lysp_node*)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003236 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003237 case LY_STMT_CONTAINER:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003238 LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003239 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003240 case LY_STMT_LEAF:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003241 LY_CHECK_RET(parse_leaf(ctx, data, (struct lysp_node*)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003242 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003243 case LY_STMT_LEAF_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003244 LY_CHECK_RET(parse_leaflist(ctx, data, (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_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003247 LY_CHECK_RET(parse_list(ctx, data, (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_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003250 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003251 break;
3252 default:
David Sedlákb3077192019-06-19 10:55:37 +02003253 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "choice");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003254 return LY_EVALID;
3255 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003256 }
Radek Krejcia9026eb2018-12-12 16:04:47 +01003257 LY_CHECK_RET(ret);
3258checks:
3259 if ((choice->flags & LYS_MAND_TRUE) && choice->dflt) {
David Sedlákb3077192019-06-19 10:55:37 +02003260 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMSCOMB, "mandatory", "default", "choice");
Radek Krejcia9026eb2018-12-12 16:04:47 +01003261 return LY_EVALID;
3262 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003263 return ret;
3264}
3265
Michal Vaskoea5abea2018-09-18 13:10:54 +02003266/**
3267 * @brief Parse the container statement.
3268 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003269 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003270 * @param[in,out] data Data to read from, always moved to currently handled character.
3271 * @param[in,out] siblings Siblings to add to.
3272 *
3273 * @return LY_ERR values.
3274 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003275LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01003276parse_container(struct lys_yang_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003277{
3278 LY_ERR ret = 0;
3279 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003280 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003281 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003282 struct lysp_node_container *cont;
3283
David Sedlák60adc092019-08-06 15:57:02 +02003284 /* create new container structure */
David Sedlákb9eeb9c2019-09-13 11:17:19 +02003285 LY_LIST_NEW_RET(ctx->ctx, siblings, cont, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003286 cont->nodetype = LYS_CONTAINER;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003287 cont->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003288
Michal Vasko7fbc8162018-09-17 10:35:16 +02003289 /* get name */
Radek Krejcid3ca0632019-04-16 16:54:54 +02003290 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003291 INSERT_WORD(ctx, buf, cont->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003292
3293 /* parse substatements */
Radek Krejci7fc68292019-06-12 13:51:09 +02003294 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003295 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003296 case LY_STMT_CONFIG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003297 LY_CHECK_RET(parse_config(ctx, data, &cont->flags, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003298 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003299 case LY_STMT_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003300 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &cont->dsc, Y_STR_ARG, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003301 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003302 case LY_STMT_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003303 LY_CHECK_RET(parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &cont->iffeatures, Y_STR_ARG, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003304 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003305 case LY_STMT_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003306 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &cont->ref, Y_STR_ARG, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003307 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003308 case LY_STMT_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003309 LY_CHECK_RET(parse_status(ctx, data, &cont->flags, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003310 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003311 case LY_STMT_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003312 LY_CHECK_RET(parse_when(ctx, data, &cont->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003313 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003314 case LY_STMT_PRESENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003315 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_PRESENCE, 0, &cont->presence, Y_STR_ARG, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003316 break;
3317
Radek Krejcid6b76452019-09-03 17:03:03 +02003318 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003319 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "container");
Radek Krejci10113652018-11-14 16:56:50 +01003320 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003321 case LY_STMT_ANYXML:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003322 LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003323 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003324 case LY_STMT_CHOICE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003325 LY_CHECK_RET(parse_choice(ctx, data, (struct lysp_node*)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003326 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003327 case LY_STMT_CONTAINER:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003328 LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003329 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003330 case LY_STMT_LEAF:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003331 LY_CHECK_RET(parse_leaf(ctx, data, (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_LEAF_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003334 LY_CHECK_RET(parse_leaflist(ctx, data, (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_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003337 LY_CHECK_RET(parse_list(ctx, data, (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_USES:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003340 LY_CHECK_RET(parse_uses(ctx, data, (struct lysp_node*)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003341 break;
3342
Radek Krejcid6b76452019-09-03 17:03:03 +02003343 case LY_STMT_TYPEDEF:
Radek Krejcibbe09a92018-11-08 09:36:54 +01003344 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node*)cont, data, &cont->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003345 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003346 case LY_STMT_MUST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003347 LY_CHECK_RET(parse_restrs(ctx, data, kw, &cont->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003348 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003349 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003350 PARSER_CHECK_STMTVER2_RET(ctx, "action", "container");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003351 LY_CHECK_RET(parse_action(ctx, data, (struct lysp_node*)cont, &cont->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003352 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003353 case LY_STMT_GROUPING:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003354 LY_CHECK_RET(parse_grouping(ctx, data, (struct lysp_node*)cont, &cont->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003355 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003356 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003357 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "container");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003358 LY_CHECK_RET(parse_notif(ctx, data, (struct lysp_node*)cont, &cont->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003359 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003360 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003361 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003362 break;
3363 default:
David Sedlákb3077192019-06-19 10:55:37 +02003364 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "container");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003365 return LY_EVALID;
3366 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003367 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003368checks:
Radek Krejcie86bf772018-12-14 11:39:53 +01003369 /* finalize parent pointers to the reallocated items */
Michal Vaskob36053d2020-03-26 15:49:30 +01003370 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 +02003371 return ret;
3372}
3373
Michal Vaskoea5abea2018-09-18 13:10:54 +02003374/**
3375 * @brief Parse the list statement.
3376 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003377 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003378 * @param[in,out] data Data to read from, always moved to currently handled character.
3379 * @param[in,out] siblings Siblings to add to.
3380 *
3381 * @return LY_ERR values.
3382 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003383LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01003384parse_list(struct lys_yang_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003385{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003386 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003387 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003388 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003389 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003390 struct lysp_node_list *list;
3391
David Sedlák60adc092019-08-06 15:57:02 +02003392 /* create new list structure */
David Sedlákb9eeb9c2019-09-13 11:17:19 +02003393 LY_LIST_NEW_RET(ctx->ctx, siblings, list, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003394 list->nodetype = LYS_LIST;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003395 list->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003396
Michal Vasko7fbc8162018-09-17 10:35:16 +02003397 /* get name */
Radek Krejcid3ca0632019-04-16 16:54:54 +02003398 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003399 INSERT_WORD(ctx, buf, list->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003400
3401 /* parse substatements */
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003402 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003403 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003404 case LY_STMT_CONFIG:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003405 LY_CHECK_RET(parse_config(ctx, data, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003406 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003407 case LY_STMT_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003408 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &list->dsc, Y_STR_ARG, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003409 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003410 case LY_STMT_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003411 LY_CHECK_RET(parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &list->iffeatures, Y_STR_ARG, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003412 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003413 case LY_STMT_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003414 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &list->ref, Y_STR_ARG, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003415 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003416 case LY_STMT_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003417 LY_CHECK_RET(parse_status(ctx, data, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003418 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003419 case LY_STMT_WHEN:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003420 LY_CHECK_RET(parse_when(ctx, data, &list->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003421 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003422 case LY_STMT_KEY:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003423 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_KEY, 0, &list->key, 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_MAX_ELEMENTS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003426 LY_CHECK_RET(parse_maxelements(ctx, data, &list->max, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003427 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003428 case LY_STMT_MIN_ELEMENTS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003429 LY_CHECK_RET(parse_minelements(ctx, data, &list->min, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003430 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003431 case LY_STMT_ORDERED_BY:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003432 LY_CHECK_RET(parse_orderedby(ctx, data, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003433 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003434 case LY_STMT_UNIQUE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003435 LY_CHECK_RET(parse_text_fields(ctx, data, LYEXT_SUBSTMT_UNIQUE, &list->uniques, Y_STR_ARG, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003436 break;
3437
Radek Krejcid6b76452019-09-03 17:03:03 +02003438 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003439 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "list");
Radek Krejci10113652018-11-14 16:56:50 +01003440 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003441 case LY_STMT_ANYXML:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003442 LY_CHECK_RET(parse_any(ctx, data, kw, (struct lysp_node*)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003443 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003444 case LY_STMT_CHOICE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003445 LY_CHECK_RET(parse_choice(ctx, data, (struct lysp_node*)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003446 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003447 case LY_STMT_CONTAINER:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003448 LY_CHECK_RET(parse_container(ctx, data, (struct lysp_node*)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003449 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003450 case LY_STMT_LEAF:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003451 LY_CHECK_RET(parse_leaf(ctx, data, (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_LEAF_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003454 LY_CHECK_RET(parse_leaflist(ctx, data, (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_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003457 LY_CHECK_RET(parse_list(ctx, data, (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_USES:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003460 LY_CHECK_RET(parse_uses(ctx, data, (struct lysp_node*)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003461 break;
3462
Radek Krejcid6b76452019-09-03 17:03:03 +02003463 case LY_STMT_TYPEDEF:
Radek Krejcibbe09a92018-11-08 09:36:54 +01003464 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node*)list, data, &list->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003465 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003466 case LY_STMT_MUST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003467 LY_CHECK_RET(parse_restrs(ctx, data, kw, &list->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003468 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003469 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003470 PARSER_CHECK_STMTVER2_RET(ctx, "action", "list");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003471 LY_CHECK_RET(parse_action(ctx, data, (struct lysp_node*)list, &list->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003472 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003473 case LY_STMT_GROUPING:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003474 LY_CHECK_RET(parse_grouping(ctx, data, (struct lysp_node*)list, &list->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003475 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003476 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003477 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "list");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003478 LY_CHECK_RET(parse_notif(ctx, data, (struct lysp_node*)list, &list->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003479 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003480 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003481 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003482 break;
3483 default:
David Sedlákb3077192019-06-19 10:55:37 +02003484 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "list");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003485 return LY_EVALID;
3486 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003487 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003488 LY_CHECK_RET(ret);
3489checks:
Radek Krejci7fc68292019-06-12 13:51:09 +02003490 /* finalize parent pointers to the reallocated items */
Michal Vaskob36053d2020-03-26 15:49:30 +01003491 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 +02003492
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003493 if (list->max && list->min > list->max) {
David Sedlákb3077192019-06-19 10:55:37 +02003494 LOGVAL_PARSER(ctx, LYVE_SEMANTICS,
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003495 "Invalid combination of min-elements and max-elements: min value %u is bigger than the max value %u.",
3496 list->min, list->max);
3497 return LY_EVALID;
3498 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003499
3500 return ret;
3501}
3502
Michal Vaskoea5abea2018-09-18 13:10:54 +02003503/**
3504 * @brief Parse the yin-element statement.
3505 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003506 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003507 * @param[in,out] data Data to read from, always moved to currently handled character.
3508 * @param[in,out] flags Flags to write to.
3509 * @param[in,out] exts Extension instances to add to.
3510 *
3511 * @return LY_ERR values.
3512 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003513static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01003514parse_yinelement(struct lys_yang_parser_ctx *ctx, const char **data, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003515{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003516 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003517 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003518 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003519 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003520
3521 if (*flags & LYS_YINELEM_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02003522 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003523 return LY_EVALID;
3524 }
3525
3526 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02003527 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003528
3529 if ((word_len == 4) && !strncmp(word, "true", word_len)) {
3530 *flags |= LYS_YINELEM_TRUE;
3531 } else if ((word_len == 5) && !strncmp(word, "false", word_len)) {
3532 *flags |= LYS_YINELEM_FALSE;
3533 } else {
David Sedlákb3077192019-06-19 10:55:37 +02003534 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003535 free(buf);
3536 return LY_EVALID;
3537 }
3538 free(buf);
3539
Radek Krejci6d6556c2018-11-08 09:37:45 +01003540 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003541 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003542 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003543 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_YINELEM, 0, exts));
3544 LY_CHECK_RET(ret); break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003545 default:
David Sedlákb3077192019-06-19 10:55:37 +02003546 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003547 return LY_EVALID;
3548 }
3549 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003550 return ret;
3551}
3552
Michal Vaskoea5abea2018-09-18 13:10:54 +02003553/**
David Sedlák2444f8f2019-07-09 11:02:47 +02003554 * @brief Parse the argument statement.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003555 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003556 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003557 * @param[in,out] data Data to read from, always moved to currently handled character.
3558 * @param[in,out] argument Value to write to.
3559 * @param[in,out] flags Flags to write to.
3560 * @param[in,out] exts Extension instances to add to.
3561 *
3562 * @return LY_ERR values.
3563 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003564static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01003565parse_argument(struct lys_yang_parser_ctx *ctx, const char **data, const char **argument, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003566{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003567 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003568 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003569 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003570 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003571
3572 if (*argument) {
David Sedlákb3077192019-06-19 10:55:37 +02003573 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003574 return LY_EVALID;
3575 }
3576
3577 /* get value */
David Sedlák2444f8f2019-07-09 11:02:47 +02003578 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003579 INSERT_WORD(ctx, buf, *argument, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003580
Radek Krejci6d6556c2018-11-08 09:37:45 +01003581 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003582 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003583 case LY_STMT_YIN_ELEMENT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003584 LY_CHECK_RET(parse_yinelement(ctx, data, flags, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003585 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003586 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003587 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_ARGUMENT, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003588 break;
3589 default:
David Sedlákb3077192019-06-19 10:55:37 +02003590 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003591 return LY_EVALID;
3592 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003593 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003594 return ret;
3595}
3596
Michal Vaskoea5abea2018-09-18 13:10:54 +02003597/**
3598 * @brief Parse the extension statement.
3599 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003600 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003601 * @param[in,out] data Data to read from, always moved to currently handled character.
3602 * @param[in,out] extensions Extensions to add to.
3603 *
3604 * @return LY_ERR values.
3605 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003606static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01003607parse_extension(struct lys_yang_parser_ctx *ctx, const char **data, struct lysp_ext **extensions)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003608{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003609 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003610 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003611 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003612 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003613 struct lysp_ext *ex;
3614
Radek Krejci2c4e7172018-10-19 15:56:26 +02003615 LY_ARRAY_NEW_RET(ctx->ctx, *extensions, ex, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003616
3617 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02003618 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003619 INSERT_WORD(ctx, buf, ex->name, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003620
Radek Krejci6d6556c2018-11-08 09:37:45 +01003621 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003622 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003623 case LY_STMT_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003624 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &ex->dsc, Y_STR_ARG, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003625 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003626 case LY_STMT_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003627 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &ex->ref, Y_STR_ARG, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003628 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003629 case LY_STMT_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003630 LY_CHECK_RET(parse_status(ctx, data, &ex->flags, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003631 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003632 case LY_STMT_ARGUMENT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003633 LY_CHECK_RET(parse_argument(ctx, data, &ex->argument, &ex->flags, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003634 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003635 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003636 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003637 break;
3638 default:
David Sedlákb3077192019-06-19 10:55:37 +02003639 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "extension");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003640 return LY_EVALID;
3641 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003642 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003643 return ret;
3644}
3645
Michal Vaskoea5abea2018-09-18 13:10:54 +02003646/**
3647 * @brief Parse the deviate statement.
3648 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003649 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003650 * @param[in,out] data Data to read from, always moved to currently handled character.
3651 * @param[in,out] deviates Deviates to add to.
3652 *
3653 * @return LY_ERR values.
3654 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003655LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01003656parse_deviate(struct lys_yang_parser_ctx *ctx, const char **data, struct lysp_deviate **deviates)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003657{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003658 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003659 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003660 size_t word_len, dev_mod;
Radek Krejcid6b76452019-09-03 17:03:03 +02003661 enum ly_stmt kw;
David Sedlák60adc092019-08-06 15:57:02 +02003662 struct lysp_deviate *d;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003663 struct lysp_deviate_add *d_add = NULL;
3664 struct lysp_deviate_rpl *d_rpl = NULL;
3665 struct lysp_deviate_del *d_del = NULL;
Radek Krejci4ad42aa2019-07-23 16:55:58 +02003666 const char **d_units = NULL, ***d_uniques = NULL, ***d_dflts = NULL;
3667 struct lysp_restr **d_musts = NULL;
3668 uint16_t *d_flags = 0;
3669 uint32_t *d_min = 0, *d_max = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003670
3671 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02003672 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003673
3674 if ((word_len == 13) && !strncmp(word, "not-supported", word_len)) {
3675 dev_mod = LYS_DEV_NOT_SUPPORTED;
3676 } else if ((word_len == 3) && !strncmp(word, "add", word_len)) {
3677 dev_mod = LYS_DEV_ADD;
3678 } else if ((word_len == 7) && !strncmp(word, "replace", word_len)) {
3679 dev_mod = LYS_DEV_REPLACE;
3680 } else if ((word_len == 6) && !strncmp(word, "delete", word_len)) {
3681 dev_mod = LYS_DEV_DELETE;
3682 } else {
David Sedlákb3077192019-06-19 10:55:37 +02003683 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "deviate");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003684 free(buf);
3685 return LY_EVALID;
3686 }
3687 free(buf);
3688
3689 /* create structure */
3690 switch (dev_mod) {
3691 case LYS_DEV_NOT_SUPPORTED:
3692 d = calloc(1, sizeof *d);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003693 LY_CHECK_ERR_RET(!d, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003694 break;
3695 case LYS_DEV_ADD:
3696 d_add = calloc(1, sizeof *d_add);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003697 LY_CHECK_ERR_RET(!d_add, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003698 d = (struct lysp_deviate *)d_add;
3699 d_units = &d_add->units;
3700 d_uniques = &d_add->uniques;
3701 d_dflts = &d_add->dflts;
3702 d_musts = &d_add->musts;
3703 d_flags = &d_add->flags;
3704 d_min = &d_add->min;
3705 d_max = &d_add->max;
3706 break;
3707 case LYS_DEV_REPLACE:
3708 d_rpl = calloc(1, sizeof *d_rpl);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003709 LY_CHECK_ERR_RET(!d_rpl, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003710 d = (struct lysp_deviate *)d_rpl;
3711 d_units = &d_rpl->units;
3712 d_flags = &d_rpl->flags;
3713 d_min = &d_rpl->min;
3714 d_max = &d_rpl->max;
3715 break;
3716 case LYS_DEV_DELETE:
3717 d_del = calloc(1, sizeof *d_del);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003718 LY_CHECK_ERR_RET(!d_del, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003719 d = (struct lysp_deviate *)d_del;
3720 d_units = &d_del->units;
3721 d_uniques = &d_del->uniques;
3722 d_dflts = &d_del->dflts;
3723 d_musts = &d_del->musts;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003724 break;
3725 default:
3726 assert(0);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003727 LOGINT_RET(ctx->ctx);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003728 }
3729 d->mod = dev_mod;
3730
3731 /* insert into siblings */
David Sedlák60adc092019-08-06 15:57:02 +02003732 LY_LIST_INSERT(deviates, d, next);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003733
Radek Krejci6d6556c2018-11-08 09:37:45 +01003734 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003735 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003736 case LY_STMT_CONFIG:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003737 switch (dev_mod) {
3738 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003739 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003740 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003741 return LY_EVALID;
3742 default:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003743 LY_CHECK_RET(parse_config(ctx, data, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003744 break;
3745 }
3746 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003747 case LY_STMT_DEFAULT:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003748 switch (dev_mod) {
3749 case LYS_DEV_NOT_SUPPORTED:
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 case LYS_DEV_REPLACE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003753 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DEFAULT, 0, &d_rpl->dflt, Y_STR_ARG, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003754 break;
3755 default:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003756 LY_CHECK_RET(parse_text_fields(ctx, data, LYEXT_SUBSTMT_DEFAULT, d_dflts, Y_STR_ARG, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003757 break;
3758 }
3759 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003760 case LY_STMT_MANDATORY:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003761 switch (dev_mod) {
3762 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003763 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003764 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003765 return LY_EVALID;
3766 default:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003767 LY_CHECK_RET(parse_mandatory(ctx, data, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003768 break;
3769 }
3770 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003771 case LY_STMT_MAX_ELEMENTS:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003772 switch (dev_mod) {
3773 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003774 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003775 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003776 return LY_EVALID;
3777 default:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003778 LY_CHECK_RET(parse_maxelements(ctx, data, d_max, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003779 break;
3780 }
3781 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003782 case LY_STMT_MIN_ELEMENTS:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003783 switch (dev_mod) {
3784 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003785 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003786 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003787 return LY_EVALID;
3788 default:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003789 LY_CHECK_RET(parse_minelements(ctx, data, d_min, d_flags, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003790 break;
3791 }
3792 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003793 case LY_STMT_MUST:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003794 switch (dev_mod) {
3795 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003796 case LYS_DEV_REPLACE:
David Sedlákb3077192019-06-19 10:55:37 +02003797 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003798 return LY_EVALID;
3799 default:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003800 LY_CHECK_RET(parse_restrs(ctx, data, kw, d_musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003801 break;
3802 }
3803 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003804 case LY_STMT_TYPE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003805 switch (dev_mod) {
3806 case LYS_DEV_NOT_SUPPORTED:
3807 case LYS_DEV_ADD:
3808 case LYS_DEV_DELETE:
David Sedlákb3077192019-06-19 10:55:37 +02003809 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003810 return LY_EVALID;
3811 default:
Radek Krejci2c02f3e2018-10-16 10:54:38 +02003812 if (d_rpl->type) {
David Sedlákb3077192019-06-19 10:55:37 +02003813 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
Radek Krejci2c02f3e2018-10-16 10:54:38 +02003814 return LY_EVALID;
3815 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003816 d_rpl->type = calloc(1, sizeof *d_rpl->type);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003817 LY_CHECK_ERR_RET(!d_rpl->type, LOGMEM(ctx->ctx), LY_EMEM);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003818 LY_CHECK_RET(parse_type(ctx, data, d_rpl->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003819 break;
3820 }
3821 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003822 case LY_STMT_UNIQUE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003823 switch (dev_mod) {
3824 case LYS_DEV_NOT_SUPPORTED:
3825 case LYS_DEV_REPLACE:
David Sedlákb3077192019-06-19 10:55:37 +02003826 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003827 return LY_EVALID;
3828 default:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003829 LY_CHECK_RET(parse_text_fields(ctx, data, LYEXT_SUBSTMT_UNIQUE, d_uniques, Y_STR_ARG, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003830 break;
3831 }
3832 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003833 case LY_STMT_UNITS:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003834 switch (dev_mod) {
3835 case LYS_DEV_NOT_SUPPORTED:
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:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003839 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_UNITS, 0, d_units, 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_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003844 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &d->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003845 break;
3846 default:
David Sedlákb3077192019-06-19 10:55:37 +02003847 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "deviate");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003848 return LY_EVALID;
3849 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003850 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003851 return ret;
3852}
3853
Michal Vaskoea5abea2018-09-18 13:10:54 +02003854/**
3855 * @brief Parse the deviation statement.
3856 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003857 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003858 * @param[in,out] data Data to read from, always moved to currently handled character.
3859 * @param[in,out] deviations Deviations to add to.
3860 *
3861 * @return LY_ERR values.
3862 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003863LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01003864parse_deviation(struct lys_yang_parser_ctx *ctx, const char **data, struct lysp_deviation **deviations)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003865{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003866 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003867 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003868 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003869 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003870 struct lysp_deviation *dev;
3871
Radek Krejci2c4e7172018-10-19 15:56:26 +02003872 LY_ARRAY_NEW_RET(ctx->ctx, *deviations, dev, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003873
3874 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02003875 LY_CHECK_RET(get_argument(ctx, data, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vaskob36053d2020-03-26 15:49:30 +01003876 CHECK_NONEMPTY(ctx, word_len, "deviation");
Radek Krejci44ceedc2018-10-02 15:54:31 +02003877 INSERT_WORD(ctx, buf, dev->nodeid, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003878
Radek Krejci6d6556c2018-11-08 09:37:45 +01003879 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003880 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003881 case LY_STMT_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003882 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &dev->dsc, Y_STR_ARG, &dev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003883 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003884 case LY_STMT_DEVIATE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003885 LY_CHECK_RET(parse_deviate(ctx, data, &dev->deviates));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003886 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003887 case LY_STMT_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003888 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &dev->ref, Y_STR_ARG, &dev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003889 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003890 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003891 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &dev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003892 break;
3893 default:
David Sedlákb3077192019-06-19 10:55:37 +02003894 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "deviation");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003895 return LY_EVALID;
3896 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003897 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003898 LY_CHECK_RET(ret);
Radek Krejci6d6556c2018-11-08 09:37:45 +01003899checks:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003900 /* mandatory substatements */
3901 if (!dev->deviates) {
David Sedlákb3077192019-06-19 10:55:37 +02003902 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "deviate", "deviation");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003903 return LY_EVALID;
3904 }
3905
3906 return ret;
3907}
3908
Michal Vaskoea5abea2018-09-18 13:10:54 +02003909/**
3910 * @brief Parse the feature statement.
3911 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003912 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003913 * @param[in,out] data Data to read from, always moved to currently handled character.
3914 * @param[in,out] features Features to add to.
3915 *
3916 * @return LY_ERR values.
3917 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003918LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01003919parse_feature(struct lys_yang_parser_ctx *ctx, const char **data, struct lysp_feature **features)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003920{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003921 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003922 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003923 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003924 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003925 struct lysp_feature *feat;
3926
Radek Krejci2c4e7172018-10-19 15:56:26 +02003927 LY_ARRAY_NEW_RET(ctx->ctx, *features, feat, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003928
3929 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02003930 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003931 INSERT_WORD(ctx, buf, feat->name, word, word_len);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01003932
Radek Krejci6d6556c2018-11-08 09:37:45 +01003933 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003934 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003935 case LY_STMT_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003936 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &feat->dsc, Y_STR_ARG, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003937 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003938 case LY_STMT_IF_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003939 LY_CHECK_RET(parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &feat->iffeatures, Y_STR_ARG, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003940 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003941 case LY_STMT_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003942 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &feat->ref, Y_STR_ARG, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003943 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003944 case LY_STMT_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003945 LY_CHECK_RET(parse_status(ctx, data, &feat->flags, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003946 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003947 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003948 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003949 break;
3950 default:
David Sedlákb3077192019-06-19 10:55:37 +02003951 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "feature");
Radek Krejci2c02f3e2018-10-16 10:54:38 +02003952 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003953 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003954 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003955 return ret;
3956}
3957
Michal Vaskoea5abea2018-09-18 13:10:54 +02003958/**
3959 * @brief Parse the identity statement.
3960 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003961 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003962 * @param[in,out] data Data to read from, always moved to currently handled character.
3963 * @param[in,out] identities Identities to add to.
3964 *
3965 * @return LY_ERR values.
3966 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003967LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01003968parse_identity(struct lys_yang_parser_ctx *ctx, const char **data, struct lysp_ident **identities)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003969{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003970 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003971 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003972 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003973 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003974 struct lysp_ident *ident;
3975
Radek Krejci2c4e7172018-10-19 15:56:26 +02003976 LY_ARRAY_NEW_RET(ctx->ctx, *identities, ident, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003977
3978 /* get value */
Radek Krejcid3ca0632019-04-16 16:54:54 +02003979 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci44ceedc2018-10-02 15:54:31 +02003980 INSERT_WORD(ctx, buf, ident->name, word, word_len);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01003981
Radek Krejci6d6556c2018-11-08 09:37:45 +01003982 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret,) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003983 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003984 case LY_STMT_DESCRIPTION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003985 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &ident->dsc, Y_STR_ARG, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003986 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003987 case LY_STMT_IF_FEATURE:
Radek Krejci335332a2019-09-05 13:03:35 +02003988 PARSER_CHECK_STMTVER2_RET(ctx, "if-feature", "identity");
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003989 LY_CHECK_RET(parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &ident->iffeatures, Y_STR_ARG, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003990 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003991 case LY_STMT_REFERENCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003992 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &ident->ref, Y_STR_ARG, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003993 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003994 case LY_STMT_STATUS:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003995 LY_CHECK_RET(parse_status(ctx, data, &ident->flags, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003996 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003997 case LY_STMT_BASE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01003998 if (ident->bases && ctx->mod_version < 2) {
David Sedlákb3077192019-06-19 10:55:37 +02003999 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 +01004000 return LY_EVALID;
4001 }
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004002 LY_CHECK_RET(parse_text_fields(ctx, data, LYEXT_SUBSTMT_BASE, &ident->bases, Y_PREF_IDENTIF_ARG, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004003 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004004 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004005 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004006 break;
4007 default:
David Sedlákb3077192019-06-19 10:55:37 +02004008 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "identity");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004009 return LY_EVALID;
4010 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004011 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004012 return ret;
4013}
4014
Michal Vaskoea5abea2018-09-18 13:10:54 +02004015/**
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004016 * @brief Parse module substatements.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004017 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004018 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004019 * @param[in,out] data Data to read from, always moved to currently handled character.
4020 * @param[in,out] mod Module to write to.
4021 *
4022 * @return LY_ERR values.
4023 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004024LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01004025parse_module(struct lys_yang_parser_ctx *ctx, const char **data, struct lysp_module *mod)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004026{
4027 LY_ERR ret = 0;
4028 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004029 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004030 enum ly_stmt kw, prev_kw = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004031 enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004032 struct lysp_submodule *dup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004033
4034 /* (sub)module name */
Radek Krejcid3ca0632019-04-16 16:54:54 +02004035 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004036 INSERT_WORD(ctx, buf, mod->mod->name, word, word_len);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01004037
Radek Krejci6d6556c2018-11-08 09:37:45 +01004038 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004039
Radek Krejcie3846472018-10-15 15:24:51 +02004040#define CHECK_ORDER(SECTION) \
David Sedlákb3077192019-06-19 10:55:37 +02004041 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 +02004042
Michal Vasko7fbc8162018-09-17 10:35:16 +02004043 switch (kw) {
4044 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004045 case LY_STMT_NAMESPACE:
4046 case LY_STMT_PREFIX:
Radek Krejcie3846472018-10-15 15:24:51 +02004047 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4048 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004049 case LY_STMT_YANG_VERSION:
Radek Krejcie3846472018-10-15 15:24:51 +02004050 CHECK_ORDER(Y_MOD_MODULE_HEADER);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004051 break;
4052 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004053 case LY_STMT_INCLUDE:
4054 case LY_STMT_IMPORT:
Radek Krejcie3846472018-10-15 15:24:51 +02004055 CHECK_ORDER(Y_MOD_LINKAGE);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004056 break;
4057 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004058 case LY_STMT_ORGANIZATION:
4059 case LY_STMT_CONTACT:
4060 case LY_STMT_DESCRIPTION:
4061 case LY_STMT_REFERENCE:
Radek Krejcie3846472018-10-15 15:24:51 +02004062 CHECK_ORDER(Y_MOD_META);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004063 break;
4064
4065 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004066 case LY_STMT_REVISION:
Radek Krejcie3846472018-10-15 15:24:51 +02004067 CHECK_ORDER(Y_MOD_REVISION);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004068 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004069 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004070 case LY_STMT_ANYDATA:
4071 case LY_STMT_ANYXML:
4072 case LY_STMT_AUGMENT:
4073 case LY_STMT_CHOICE:
4074 case LY_STMT_CONTAINER:
4075 case LY_STMT_DEVIATION:
4076 case LY_STMT_EXTENSION:
4077 case LY_STMT_FEATURE:
4078 case LY_STMT_GROUPING:
4079 case LY_STMT_IDENTITY:
4080 case LY_STMT_LEAF:
4081 case LY_STMT_LEAF_LIST:
4082 case LY_STMT_LIST:
4083 case LY_STMT_NOTIFICATION:
4084 case LY_STMT_RPC:
4085 case LY_STMT_TYPEDEF:
4086 case LY_STMT_USES:
4087 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004088 mod_stmt = Y_MOD_BODY;
4089 break;
4090 default:
4091 /* error handled in the next switch */
4092 break;
4093 }
Radek Krejcie3846472018-10-15 15:24:51 +02004094#undef CHECK_ORDER
Michal Vasko7fbc8162018-09-17 10:35:16 +02004095
Radek Krejcie3846472018-10-15 15:24:51 +02004096 prev_kw = kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004097 switch (kw) {
4098 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004099 case LY_STMT_YANG_VERSION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004100 LY_CHECK_RET(parse_yangversion(ctx, data, &mod->mod->version, &mod->exts));
4101 ctx->mod_version = mod->mod->version;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004102 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004103 case LY_STMT_NAMESPACE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004104 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_NAMESPACE, 0, &mod->mod->ns, Y_STR_ARG, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004105 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004106 case LY_STMT_PREFIX:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004107 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_PREFIX, 0, &mod->mod->prefix, Y_IDENTIF_ARG, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004108 break;
4109
4110 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004111 case LY_STMT_INCLUDE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004112 LY_CHECK_RET(parse_include(ctx, mod->mod->name, data, &mod->includes));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004113 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004114 case LY_STMT_IMPORT:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004115 LY_CHECK_RET(parse_import(ctx, mod->mod->prefix, data, &mod->imports));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004116 break;
4117
4118 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004119 case LY_STMT_ORGANIZATION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004120 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_ORGANIZATION, 0, &mod->mod->org, Y_STR_ARG, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004121 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004122 case LY_STMT_CONTACT:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004123 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_CONTACT, 0, &mod->mod->contact, Y_STR_ARG, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004124 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004125 case LY_STMT_DESCRIPTION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004126 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &mod->mod->dsc, Y_STR_ARG, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004127 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004128 case LY_STMT_REFERENCE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004129 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &mod->mod->ref, Y_STR_ARG, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004130 break;
4131
4132 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004133 case LY_STMT_REVISION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004134 LY_CHECK_RET(parse_revision(ctx, data, &mod->revs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004135 break;
4136
4137 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004138 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02004139 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "module");
Radek Krejci10113652018-11-14 16:56:50 +01004140 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02004141 case LY_STMT_ANYXML:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004142 LY_CHECK_RET(parse_any(ctx, data, kw, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004143 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004144 case LY_STMT_CHOICE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004145 LY_CHECK_RET(parse_choice(ctx, data, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004146 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004147 case LY_STMT_CONTAINER:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004148 LY_CHECK_RET(parse_container(ctx, data, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004149 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004150 case LY_STMT_LEAF:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004151 LY_CHECK_RET(parse_leaf(ctx, data, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004152 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004153 case LY_STMT_LEAF_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004154 LY_CHECK_RET(parse_leaflist(ctx, data, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004155 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004156 case LY_STMT_LIST:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004157 LY_CHECK_RET(parse_list(ctx, data, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004158 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004159 case LY_STMT_USES:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004160 LY_CHECK_RET(parse_uses(ctx, data, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004161 break;
4162
Radek Krejcid6b76452019-09-03 17:03:03 +02004163 case LY_STMT_AUGMENT:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004164 LY_CHECK_RET(parse_augment(ctx, data, NULL, &mod->augments));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004165 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004166 case LY_STMT_DEVIATION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004167 LY_CHECK_RET(parse_deviation(ctx, data, &mod->deviations));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004168 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004169 case LY_STMT_EXTENSION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004170 LY_CHECK_RET(parse_extension(ctx, data, &mod->extensions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004171 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004172 case LY_STMT_FEATURE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004173 LY_CHECK_RET(parse_feature(ctx, data, &mod->features));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004174 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004175 case LY_STMT_GROUPING:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004176 LY_CHECK_RET(parse_grouping(ctx, data, NULL, &mod->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004177 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004178 case LY_STMT_IDENTITY:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004179 LY_CHECK_RET(parse_identity(ctx, data, &mod->identities));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004180 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004181 case LY_STMT_NOTIFICATION:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004182 LY_CHECK_RET(parse_notif(ctx, data, NULL, &mod->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004183 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004184 case LY_STMT_RPC:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004185 LY_CHECK_RET(parse_action(ctx, data, NULL, &mod->rpcs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004186 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004187 case LY_STMT_TYPEDEF:
Radek Krejcibbe09a92018-11-08 09:36:54 +01004188 LY_CHECK_RET(parse_typedef(ctx, NULL, data, &mod->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004189 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004190 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004191 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004192 break;
4193
4194 default:
David Sedlákb3077192019-06-19 10:55:37 +02004195 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "module");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004196 return LY_EVALID;
4197 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004198 }
Radek Krejcic59bc972018-09-17 16:13:06 +02004199 LY_CHECK_RET(ret);
Radek Krejcie86bf772018-12-14 11:39:53 +01004200
Radek Krejci6d6556c2018-11-08 09:37:45 +01004201checks:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004202 /* finalize parent pointers to the reallocated items */
Michal Vaskob36053d2020-03-26 15:49:30 +01004203 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 +01004204
Michal Vasko7fbc8162018-09-17 10:35:16 +02004205 /* mandatory substatements */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004206 if (!mod->mod->ns) {
David Sedlákb3077192019-06-19 10:55:37 +02004207 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "namespace", "module");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004208 return LY_EVALID;
4209 } else if (!mod->mod->prefix) {
David Sedlákb3077192019-06-19 10:55:37 +02004210 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "module");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004211 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004212 }
4213
Radek Krejcie9e987e2018-10-31 12:50:27 +01004214 /* submodules share the namespace with the module names, so there must not be
4215 * a submodule of the same name in the context, no need for revision matching */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004216 dup = ly_ctx_get_submodule(ctx->ctx, NULL, mod->mod->name, NULL);
4217 if (dup) {
David Sedlákb3077192019-06-19 10:55:37 +02004218 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 +01004219 return LY_EVALID;
4220 }
4221
4222 return ret;
4223}
4224
4225/**
4226 * @brief Parse submodule substatements.
4227 *
4228 * @param[in] ctx yang parser context for logging.
4229 * @param[in,out] data Data to read from, always moved to currently handled character.
4230 * @param[out] submod Parsed submodule structure.
4231 *
4232 * @return LY_ERR values.
4233 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004234LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01004235parse_submodule(struct lys_yang_parser_ctx *ctx, const char **data, struct lysp_submodule *submod)
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004236{
4237 LY_ERR ret = 0;
4238 char *buf, *word;
4239 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004240 enum ly_stmt kw, prev_kw = 0;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004241 enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER;
4242 struct lysp_submodule *dup;
4243
4244 /* submodule name */
Radek Krejcid3ca0632019-04-16 16:54:54 +02004245 LY_CHECK_RET(get_argument(ctx, data, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004246 INSERT_WORD(ctx, buf, submod->name, word, word_len);
4247
4248 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret, goto checks) {
4249
4250#define CHECK_ORDER(SECTION) \
David Sedlákb3077192019-06-19 10:55:37 +02004251 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 +01004252
4253 switch (kw) {
4254 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004255 case LY_STMT_BELONGS_TO:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004256 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4257 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004258 case LY_STMT_YANG_VERSION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004259 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4260 break;
4261 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004262 case LY_STMT_INCLUDE:
4263 case LY_STMT_IMPORT:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004264 CHECK_ORDER(Y_MOD_LINKAGE);
4265 break;
4266 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004267 case LY_STMT_ORGANIZATION:
4268 case LY_STMT_CONTACT:
4269 case LY_STMT_DESCRIPTION:
4270 case LY_STMT_REFERENCE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004271 CHECK_ORDER(Y_MOD_META);
4272 break;
4273
4274 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004275 case LY_STMT_REVISION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004276 CHECK_ORDER(Y_MOD_REVISION);
4277 break;
4278 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004279 case LY_STMT_ANYDATA:
4280 case LY_STMT_ANYXML:
4281 case LY_STMT_AUGMENT:
4282 case LY_STMT_CHOICE:
4283 case LY_STMT_CONTAINER:
4284 case LY_STMT_DEVIATION:
4285 case LY_STMT_EXTENSION:
4286 case LY_STMT_FEATURE:
4287 case LY_STMT_GROUPING:
4288 case LY_STMT_IDENTITY:
4289 case LY_STMT_LEAF:
4290 case LY_STMT_LEAF_LIST:
4291 case LY_STMT_LIST:
4292 case LY_STMT_NOTIFICATION:
4293 case LY_STMT_RPC:
4294 case LY_STMT_TYPEDEF:
4295 case LY_STMT_USES:
4296 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004297 mod_stmt = Y_MOD_BODY;
4298 break;
4299 default:
4300 /* error handled in the next switch */
4301 break;
4302 }
4303#undef CHECK_ORDER
4304
4305 prev_kw = kw;
4306 switch (kw) {
4307 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004308 case LY_STMT_YANG_VERSION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004309 LY_CHECK_RET(parse_yangversion(ctx, data, &submod->version, &submod->exts));
4310 ctx->mod_version = submod->version;
4311 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004312 case LY_STMT_BELONGS_TO:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004313 LY_CHECK_RET(parse_belongsto(ctx, data, &submod->belongsto, &submod->prefix, &submod->exts));
4314 break;
4315
4316 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004317 case LY_STMT_INCLUDE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004318 LY_CHECK_RET(parse_include(ctx, submod->name, data, &submod->includes));
4319 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004320 case LY_STMT_IMPORT:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004321 LY_CHECK_RET(parse_import(ctx, submod->prefix, data, &submod->imports));
4322 break;
4323
4324 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004325 case LY_STMT_ORGANIZATION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004326 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_ORGANIZATION, 0, &submod->org, Y_STR_ARG, &submod->exts));
4327 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004328 case LY_STMT_CONTACT:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004329 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_CONTACT, 0, &submod->contact, Y_STR_ARG, &submod->exts));
4330 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004331 case LY_STMT_DESCRIPTION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004332 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &submod->dsc, Y_STR_ARG, &submod->exts));
4333 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004334 case LY_STMT_REFERENCE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004335 LY_CHECK_RET(parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &submod->ref, Y_STR_ARG, &submod->exts));
4336 break;
4337
4338 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004339 case LY_STMT_REVISION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004340 LY_CHECK_RET(parse_revision(ctx, data, &submod->revs));
4341 break;
4342
4343 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004344 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02004345 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "submodule");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004346 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02004347 case LY_STMT_ANYXML:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004348 LY_CHECK_RET(parse_any(ctx, data, kw, NULL, &submod->data));
4349 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004350 case LY_STMT_CHOICE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004351 LY_CHECK_RET(parse_choice(ctx, data, NULL, &submod->data));
4352 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004353 case LY_STMT_CONTAINER:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004354 LY_CHECK_RET(parse_container(ctx, data, NULL, &submod->data));
4355 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004356 case LY_STMT_LEAF:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004357 LY_CHECK_RET(parse_leaf(ctx, data, NULL, &submod->data));
4358 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004359 case LY_STMT_LEAF_LIST:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004360 LY_CHECK_RET(parse_leaflist(ctx, data, NULL, &submod->data));
4361 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004362 case LY_STMT_LIST:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004363 LY_CHECK_RET(parse_list(ctx, data, NULL, &submod->data));
4364 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004365 case LY_STMT_USES:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004366 LY_CHECK_RET(parse_uses(ctx, data, NULL, &submod->data));
4367 break;
4368
Radek Krejcid6b76452019-09-03 17:03:03 +02004369 case LY_STMT_AUGMENT:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004370 LY_CHECK_RET(parse_augment(ctx, data, NULL, &submod->augments));
4371 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004372 case LY_STMT_DEVIATION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004373 LY_CHECK_RET(parse_deviation(ctx, data, &submod->deviations));
4374 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004375 case LY_STMT_EXTENSION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004376 LY_CHECK_RET(parse_extension(ctx, data, &submod->extensions));
4377 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004378 case LY_STMT_FEATURE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004379 LY_CHECK_RET(parse_feature(ctx, data, &submod->features));
4380 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004381 case LY_STMT_GROUPING:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004382 LY_CHECK_RET(parse_grouping(ctx, data, NULL, &submod->groupings));
4383 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004384 case LY_STMT_IDENTITY:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004385 LY_CHECK_RET(parse_identity(ctx, data, &submod->identities));
4386 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004387 case LY_STMT_NOTIFICATION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004388 LY_CHECK_RET(parse_notif(ctx, data, NULL, &submod->notifs));
4389 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004390 case LY_STMT_RPC:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004391 LY_CHECK_RET(parse_action(ctx, data, NULL, &submod->rpcs));
4392 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004393 case LY_STMT_TYPEDEF:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004394 LY_CHECK_RET(parse_typedef(ctx, NULL, data, &submod->typedefs));
4395 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004396 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004397 LY_CHECK_RET(parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &submod->exts));
4398 break;
4399
4400 default:
David Sedlákb3077192019-06-19 10:55:37 +02004401 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "submodule");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004402 return LY_EVALID;
4403 }
4404 }
4405 LY_CHECK_RET(ret);
4406
4407checks:
4408 /* finalize parent pointers to the reallocated items */
Michal Vaskob36053d2020-03-26 15:49:30 +01004409 LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, submod->groupings, submod->augments,
4410 submod->rpcs, submod->notifs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004411
4412 /* mandatory substatements */
4413 if (!submod->belongsto) {
David Sedlákb3077192019-06-19 10:55:37 +02004414 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "belongs-to", "submodule");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004415 return LY_EVALID;
4416 }
4417
4418 /* submodules share the namespace with the module names, so there must not be
4419 * a submodule of the same name in the context, no need for revision matching */
4420 dup = ly_ctx_get_submodule(ctx->ctx, NULL, submod->name, NULL);
4421 if (dup && strcmp(dup->belongsto, submod->belongsto)) {
David Sedlákb3077192019-06-19 10:55:37 +02004422 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Name collision between submodules of name \"%s\".", dup->name);
Radek Krejcie9e987e2018-10-31 12:50:27 +01004423 return LY_EVALID;
4424 }
4425
Michal Vasko7fbc8162018-09-17 10:35:16 +02004426 return ret;
4427}
4428
Radek Krejcid4557c62018-09-17 11:42:09 +02004429LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01004430yang_parse_submodule(struct lys_yang_parser_ctx **context, struct ly_ctx *ly_ctx, struct lys_parser_ctx *main_ctx,
4431 const char *data, struct lysp_submodule **submod)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004432{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004433 LY_ERR ret = LY_SUCCESS;
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004434 char *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004435 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004436 enum ly_stmt kw;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004437 struct lysp_submodule *mod_p = NULL;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004438
David Sedlák1b623122019-08-05 15:27:49 +02004439 /* create context */
4440 *context = calloc(1, sizeof **context);
4441 LY_CHECK_ERR_RET(!(*context), LOGMEM(ly_ctx), LY_EMEM);
Michal Vaskob36053d2020-03-26 15:49:30 +01004442 (*context)->format = LYS_IN_YANG;
David Sedlák1b623122019-08-05 15:27:49 +02004443 (*context)->ctx = ly_ctx;
Radek Krejci99435242019-09-05 16:19:15 +02004444 (*context)->pos_type = LY_VLOG_LINE;
David Sedlák1b623122019-08-05 15:27:49 +02004445 (*context)->line = 1;
4446
4447 /* map the typedefs and groupings list from main context to the submodule's context */
4448 memcpy(&(*context)->tpdfs_nodes, &main_ctx->tpdfs_nodes, sizeof main_ctx->tpdfs_nodes);
4449 memcpy(&(*context)->grps_nodes, &main_ctx->grps_nodes, sizeof main_ctx->grps_nodes);
4450
Michal Vasko7fbc8162018-09-17 10:35:16 +02004451 /* "module"/"submodule" */
David Sedlák1b623122019-08-05 15:27:49 +02004452 ret = get_keyword(*context, &data, &kw, &word, &word_len);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004453 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004454
Radek Krejcid6b76452019-09-03 17:03:03 +02004455 if (kw == LY_STMT_MODULE) {
David Sedlák1b623122019-08-05 15:27:49 +02004456 LOGERR((*context)->ctx, LY_EDENIED, "Input data contains module in situation when a submodule is expected.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004457 ret = LY_EINVAL;
4458 goto cleanup;
Radek Krejcid6b76452019-09-03 17:03:03 +02004459 } else if (kw != LY_STMT_SUBMODULE) {
David Sedlák1538a842019-08-08 15:38:51 +02004460 LOGVAL_PARSER(*context, LY_VCODE_MOD_SUBOMD, ly_stmt2str(kw));
Radek Krejci40544fa2019-01-11 09:38:37 +01004461 ret = LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004462 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004463 }
4464
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004465 mod_p = calloc(1, sizeof *mod_p);
David Sedlák1b623122019-08-05 15:27:49 +02004466 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM((*context)->ctx), cleanup);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004467 mod_p->parsing = 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004468
4469 /* substatements */
David Sedlák1b623122019-08-05 15:27:49 +02004470 ret = parse_submodule(*context, &data, mod_p);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004471 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004472
4473 /* read some trailing spaces or new lines */
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004474 while(*data && isspace(*data)) {
4475 data++;
4476 }
4477 if (*data) {
David Sedlák1538a842019-08-08 15:38:51 +02004478 LOGVAL_PARSER(*context, LY_VCODE_TRAILING_SUBMOD, 15, data, strlen(data) > 15 ? "..." : "");
Radek Krejci40544fa2019-01-11 09:38:37 +01004479 ret = LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004480 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004481 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004482
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004483 mod_p->parsing = 0;
4484 *submod = mod_p;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004485
Radek Krejcibbe09a92018-11-08 09:36:54 +01004486cleanup:
4487 if (ret) {
David Sedlák1b623122019-08-05 15:27:49 +02004488 lysp_submodule_free((*context)->ctx, mod_p);
Michal Vaskob36053d2020-03-26 15:49:30 +01004489 yang_parser_ctx_free(*context);
David Sedlák1b623122019-08-05 15:27:49 +02004490 *context = NULL;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004491 }
4492
4493 return ret;
4494}
4495
4496LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01004497yang_parse_module(struct lys_yang_parser_ctx **context, const char *data, struct lys_module *mod)
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004498{
4499 LY_ERR ret = LY_SUCCESS;
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004500 char *word;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004501 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004502 enum ly_stmt kw;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004503 struct lysp_module *mod_p = NULL;
4504
David Sedlák1b623122019-08-05 15:27:49 +02004505 /* create context */
4506 *context = calloc(1, sizeof **context);
4507 LY_CHECK_ERR_RET(!(*context), LOGMEM(mod->ctx), LY_EMEM);
Michal Vaskob36053d2020-03-26 15:49:30 +01004508 (*context)->format = LYS_IN_YANG;
David Sedlák1b623122019-08-05 15:27:49 +02004509 (*context)->ctx = mod->ctx;
Radek Krejci335332a2019-09-05 13:03:35 +02004510 (*context)->pos_type = LY_VLOG_LINE;
David Sedlák1b623122019-08-05 15:27:49 +02004511 (*context)->line = 1;
4512
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004513 /* "module"/"submodule" */
David Sedlák1b623122019-08-05 15:27:49 +02004514 ret = get_keyword(*context, &data, &kw, &word, &word_len);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004515 LY_CHECK_GOTO(ret, cleanup);
4516
Radek Krejcid6b76452019-09-03 17:03:03 +02004517 if (kw == LY_STMT_SUBMODULE) {
David Sedlák1b623122019-08-05 15:27:49 +02004518 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 +01004519 ret = LY_EINVAL;
4520 goto cleanup;
Radek Krejcid6b76452019-09-03 17:03:03 +02004521 } else if (kw != LY_STMT_MODULE) {
David Sedlák1538a842019-08-08 15:38:51 +02004522 LOGVAL_PARSER((*context), LY_VCODE_MOD_SUBOMD, ly_stmt2str(kw));
Radek Krejci40544fa2019-01-11 09:38:37 +01004523 ret = LY_EVALID;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004524 goto cleanup;
4525 }
4526
4527 mod_p = calloc(1, sizeof *mod_p);
David Sedlák1b623122019-08-05 15:27:49 +02004528 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM((*context)->ctx), cleanup);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004529 mod_p->mod = mod;
4530 mod_p->parsing = 1;
4531
4532 /* substatements */
David Sedlák1b623122019-08-05 15:27:49 +02004533 ret = parse_module(*context, &data, mod_p);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004534 LY_CHECK_GOTO(ret, cleanup);
4535
4536 /* read some trailing spaces or new lines */
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004537 while(*data && isspace(*data)) {
4538 data++;
4539 }
4540 if (*data) {
David Sedlák1538a842019-08-08 15:38:51 +02004541 LOGVAL_PARSER(*context, LY_VCODE_TRAILING_MOD, 15, data, strlen(data) > 15 ? "..." : "");
Radek Krejci40544fa2019-01-11 09:38:37 +01004542 ret = LY_EVALID;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004543 goto cleanup;
4544 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004545
4546 mod_p->parsing = 0;
4547 mod->parsed = mod_p;
4548
4549cleanup:
4550 if (ret) {
4551 lysp_module_free(mod_p);
Michal Vaskob36053d2020-03-26 15:49:30 +01004552 yang_parser_ctx_free(*context);
David Sedlák1b623122019-08-05 15:27:49 +02004553 *context = NULL;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004554 }
4555
Michal Vasko7fbc8162018-09-17 10:35:16 +02004556 return ret;
4557}