blob: 8511d26a87298e285de174ec0fe5b8324c1b1f36 [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 *
Michal Vasko72c6d642024-02-27 14:59:01 +01006 * Copyright (c) 2018 - 2024 CESNET, z.s.p.o.
Michal Vasko7fbc8162018-09-17 10:35:16 +02007 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
Michal Vasko63f3d842020-07-08 10:10:14 +020014#include "parser_internal.h"
Michal Vasko7fbc8162018-09-17 10:35:16 +020015
Radek Krejcie7b95092019-05-15 11:03:07 +020016#include <assert.h>
17#include <ctype.h>
18#include <errno.h>
19#include <stdint.h>
20#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
23
Michal Vasko7fbc8162018-09-17 10:35:16 +020024#include "context.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020025#include "dict.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020026#include "in_internal.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020027#include "log.h"
Michal Vasko8f702ee2024-02-20 15:44:24 +010028#include "ly_common.h"
Radek Krejcica376bd2020-06-11 16:04:06 +020029#include "parser_schema.h"
Michal Vasko69730152020-10-09 16:30:07 +020030#include "path.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020031#include "set.h"
32#include "tree.h"
Radek Krejci859a15a2021-03-05 20:56:59 +010033#include "tree_edit.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020034#include "tree_schema.h"
Michal Vaskoc636ea42022-09-16 10:20:31 +020035#include "tree_schema_free.h"
Radek Krejci70853c52018-10-15 14:46:16 +020036#include "tree_schema_internal.h"
Radek Krejci44ceedc2018-10-02 15:54:31 +020037
Radek Krejci77114102021-03-10 15:21:57 +010038struct lys_glob_unres;
39
Radek Krejciceaf2122019-01-02 15:03:26 +010040/**
41 * @brief Insert WORD into the libyang context's dictionary and store as TARGET.
Michal Vaskoc0c64ae2022-10-06 10:15:23 +020042 *
Radek Krejciceaf2122019-01-02 15:03:26 +010043 * @param[in] CTX yang parser context to access libyang context.
44 * @param[in] BUF buffer in case the word is not a constant and can be inserted directly (zero-copy)
45 * @param[out] TARGET variable where to store the pointer to the inserted value.
46 * @param[in] WORD string to store.
47 * @param[in] LEN length of the string in WORD to store.
48 */
Michal Vasko12ef5362022-09-16 15:13:58 +020049#define INSERT_WORD_GOTO(CTX, BUF, TARGET, WORD, LEN, RET, LABEL) \
50 if (BUF) {LY_CHECK_GOTO(RET = lydict_insert_zc(PARSER_CTX(CTX), WORD, &(TARGET)), LABEL);}\
51 else {LY_CHECK_GOTO(RET = lydict_insert(PARSER_CTX(CTX), LEN ? WORD : "", LEN, &(TARGET)), LABEL);}
Radek Krejci44ceedc2018-10-02 15:54:31 +020052
Radek Krejciceaf2122019-01-02 15:03:26 +010053/**
Michal Vasko63f3d842020-07-08 10:10:14 +020054 * @brief Read from the IN structure COUNT items. Also updates the indent value in yang parser context
Michal Vaskoc0c64ae2022-10-06 10:15:23 +020055 *
Radek Krejciceaf2122019-01-02 15:03:26 +010056 * @param[in] CTX yang parser context to update its indent value.
Radek Krejciceaf2122019-01-02 15:03:26 +010057 * @param[in] COUNT number of items for which the DATA pointer is supposed to move on.
58 */
Radek Krejcid54412f2020-12-17 20:25:35 +010059#define MOVE_INPUT(CTX, COUNT) ly_in_skip((CTX)->in, COUNT);(CTX)->indent+=COUNT
Radek Krejcid5f2b5f2018-10-11 10:54:36 +020060
Michal Vaskoea5abea2018-09-18 13:10:54 +020061/**
Michal Vaskoc0c64ae2022-10-06 10:15:23 +020062 * @brief Loop through all substatements. Starts a for loop and ::YANG_READ_SUBSTMT_NEXT_ITER must be used at its end.
Michal Vaskoea5abea2018-09-18 13:10:54 +020063 *
Michal Vaskoc8806c82022-12-06 10:31:24 +010064 * @param[in] CTX yang parser context.
Michal Vaskoea5abea2018-09-18 13:10:54 +020065 * @param[out] KW YANG keyword read.
66 * @param[out] WORD Pointer to the keyword itself.
67 * @param[out] WORD_LEN Length of the keyword.
Michal Vasko12ef5362022-09-16 15:13:58 +020068 * @param[out] RET Variable for error storing.
Michal Vaskoc0c64ae2022-10-06 10:15:23 +020069 * @param[in] ERR_LABEL Label to go to on error.
Michal Vaskoea5abea2018-09-18 13:10:54 +020070 */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +020071#define YANG_READ_SUBSTMT_FOR_GOTO(CTX, KW, WORD, WORD_LEN, RET, ERR_LABEL) \
72 ly_bool __loop_end = 0; \
Michal Vasko12ef5362022-09-16 15:13:58 +020073 if ((RET = get_keyword(CTX, &KW, &WORD, &WORD_LEN))) { \
Michal Vaskoc0c64ae2022-10-06 10:15:23 +020074 goto ERR_LABEL; \
aPieceka24a2252021-05-07 10:52:31 +020075 } \
Radek Krejcid6b76452019-09-03 17:03:03 +020076 if (KW == LY_STMT_SYNTAX_SEMICOLON) { \
Michal Vaskoc0c64ae2022-10-06 10:15:23 +020077 __loop_end = 1; \
78 } else if (KW != LY_STMT_SYNTAX_LEFT_BRACE) { \
Michal Vasko193dacd2022-10-13 08:43:05 +020079 LOGVAL_PARSER(CTX, LYVE_SYNTAX_YANG, "Invalid keyword \"%s\", expected \";\" or \"{\".", lyplg_ext_stmt2str(KW)); \
Michal Vasko12ef5362022-09-16 15:13:58 +020080 RET = LY_EVALID; \
Michal Vaskoc0c64ae2022-10-06 10:15:23 +020081 goto ERR_LABEL; \
82 } else { \
83 YANG_READ_SUBSTMT_NEXT_ITER(CTX, KW, WORD, WORD_LEN, NULL, RET, ERR_LABEL); \
Michal Vasko7fbc8162018-09-17 10:35:16 +020084 } \
Michal Vaskoc0c64ae2022-10-06 10:15:23 +020085 while (!__loop_end)
Michal Vasko7fbc8162018-09-17 10:35:16 +020086
Michal Vaskoc0c64ae2022-10-06 10:15:23 +020087/**
88 * @brief Next iteration of ::YANG_READ_SUBSTMT_FOR_GOTO loop.
89 *
Michal Vaskoc8806c82022-12-06 10:31:24 +010090 * @param[in] CTX yang parser context.
Michal Vaskoc0c64ae2022-10-06 10:15:23 +020091 * @param[out] KW YANG keyword read.
92 * @param[out] WORD Pointer to the keyword itself.
93 * @param[out] WORD_LEN Length of the keyword.
94 * @param[in] EXTS Final extension instance array to store.
95 * @param[out] RET Variable for error storing.
96 * @param[in] ERR_LABEL Label to go to on error.
97 */
98#define YANG_READ_SUBSTMT_NEXT_ITER(CTX, KW, WORD, WORD_LEN, EXTS, RET, ERR_LABEL) \
99 if ((RET = get_keyword(CTX, &KW, &WORD, &WORD_LEN))) { \
100 goto ERR_LABEL; \
101 } \
102 if (KW == LY_STMT_SYNTAX_RIGHT_BRACE) { \
Michal Vaskoc8806c82022-12-06 10:31:24 +0100103 if (EXTS && (RET = ly_set_add(&(CTX)->main_ctx->ext_inst, (EXTS), 1, NULL))) { \
Michal Vaskoc0c64ae2022-10-06 10:15:23 +0200104 goto ERR_LABEL; \
105 } \
106 __loop_end = 1; \
107 }
108
Michal Vaskod0625d72022-10-06 15:02:50 +0200109LY_ERR parse_container(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings);
110LY_ERR parse_uses(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings);
111LY_ERR parse_choice(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings);
112LY_ERR parse_case(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings);
113LY_ERR parse_list(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings);
114LY_ERR parse_grouping(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node_grp **groupings);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200115
Michal Vaskoea5abea2018-09-18 13:10:54 +0200116/**
117 * @brief Add another character to dynamic buffer, a low-level function.
118 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200119 * Enlarge if needed. Updates \p input as well as \p buf_used.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200120 *
Radek Krejci404251e2018-10-09 12:06:44 +0200121 * @param[in] ctx libyang context for logging.
Michal Vasko63f3d842020-07-08 10:10:14 +0200122 * @param[in,out] in Input structure.
Radek Krejci44ceedc2018-10-02 15:54:31 +0200123 * @param[in] len Number of bytes to get from the input string and copy into the buffer.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200124 * @param[in,out] buf Buffer to use, can be moved by realloc().
125 * @param[in,out] buf_len Current size of the buffer.
Radek Krejci44ceedc2018-10-02 15:54:31 +0200126 * @param[in,out] buf_used Currently used characters of the buffer.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200127 * @return LY_ERR values.
128 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200129LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +0200130buf_add_char(struct ly_ctx *ctx, struct ly_in *in, size_t len, char **buf, size_t *buf_len, size_t *buf_used)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200131{
Radek Krejcif13b87b2020-12-01 22:02:17 +0100132#define BUF_STEP 16;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200133 if (*buf_len <= (*buf_used) + len) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100134 *buf_len += BUF_STEP;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200135 *buf = ly_realloc(*buf, *buf_len);
136 LY_CHECK_ERR_RET(!*buf, LOGMEM(ctx), LY_EMEM);
137 }
Radek Krejcic0917392019-04-10 13:04:04 +0200138 if (*buf_used) {
Michal Vasko63f3d842020-07-08 10:10:14 +0200139 ly_in_read(in, &(*buf)[*buf_used], len);
Radek Krejcic0917392019-04-10 13:04:04 +0200140 } else {
Michal Vasko63f3d842020-07-08 10:10:14 +0200141 ly_in_read(in, *buf, len);
Radek Krejcic0917392019-04-10 13:04:04 +0200142 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200143
Radek Krejci44ceedc2018-10-02 15:54:31 +0200144 (*buf_used) += len;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200145 return LY_SUCCESS;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100146#undef BUF_STEP
Michal Vasko7fbc8162018-09-17 10:35:16 +0200147}
148
Michal Vaskoea5abea2018-09-18 13:10:54 +0200149/**
Radek Krejci44ceedc2018-10-02 15:54:31 +0200150 * @brief Store a single UTF8 character. It depends whether in a dynamically-allocated buffer or just as a pointer to the data.
151 *
152 * @param[in] ctx yang parser context for logging.
Radek Krejci44ceedc2018-10-02 15:54:31 +0200153 * @param[in] arg Type of the input string to select method of checking character validity.
154 * @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 +0200155 * 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 +0200156 * @param[in,out] word_len Current length of the word pointed to by \p word_p.
157 * @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 +0200158 * @param[in,out] buf_len Current length of \p word_b.
Radek Krejci44ceedc2018-10-02 15:54:31 +0200159 * @param[in] need_buf Flag if the dynamically allocated buffer is required.
David Sedlák40bb13b2019-07-10 14:34:18 +0200160 * @param[in,out] prefix Storage for internally used flag in case of possible prefixed identifiers:
161 * 0 - colon not yet found (no prefix)
162 * 1 - \p c is the colon character
163 * 2 - prefix already processed, now processing the identifier
Michal Vaskoea5abea2018-09-18 13:10:54 +0200164 * @return LY_ERR values.
165 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200166LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +0200167buf_store_char(struct lysp_yang_ctx *ctx, enum yang_arg arg, char **word_p, size_t *word_len,
Radek Krejci857189e2020-09-01 13:26:36 +0200168 char **word_b, size_t *buf_len, ly_bool need_buf, uint8_t *prefix)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200169{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200170 uint32_t c;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200171 size_t len;
172
Radek Krejcif29b7c32019-04-09 16:17:49 +0200173 /* check valid combination of input paremeters - if need_buf specified, word_b must be provided */
174 assert(!need_buf || (need_buf && word_b));
175
Radek Krejci44ceedc2018-10-02 15:54:31 +0200176 /* get UTF8 code point (and number of bytes coding the character) */
Radek Krejci33090f92020-12-17 20:12:46 +0100177 LY_CHECK_ERR_RET(ly_getutf8(&ctx->in->current, &c, &len),
178 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, ctx->in->current[-len]), LY_EVALID);
179 ctx->in->current -= len;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200180 if (c == '\n') {
181 ctx->indent = 0;
Radek Krejcidd713ce2021-01-04 23:12:12 +0100182 LY_IN_NEW_LINE(ctx->in);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200183 } else {
184 /* note - even the multibyte character is count as 1 */
185 ++ctx->indent;
186 }
187
Radek Krejci44ceedc2018-10-02 15:54:31 +0200188 /* check character validity */
189 switch (arg) {
190 case Y_IDENTIF_ARG:
Michal Vaskod0625d72022-10-06 15:02:50 +0200191 LY_CHECK_RET(lysp_check_identifierchar((struct lysp_ctx *)ctx, c, !(*word_len), NULL));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200192 break;
193 case Y_PREF_IDENTIF_ARG:
Michal Vaskod0625d72022-10-06 15:02:50 +0200194 LY_CHECK_RET(lysp_check_identifierchar((struct lysp_ctx *)ctx, c, !(*word_len), prefix));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200195 break;
196 case Y_STR_ARG:
197 case Y_MAYBE_STR_ARG:
Michal Vaskod0625d72022-10-06 15:02:50 +0200198 LY_CHECK_RET(lysp_check_stringchar((struct lysp_ctx *)ctx, c));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200199 break;
200 }
201
Michal Vasko7fbc8162018-09-17 10:35:16 +0200202 if (word_b && *word_b) {
203 /* add another character into buffer */
Radek Krejci33090f92020-12-17 20:12:46 +0100204 if (buf_add_char(PARSER_CTX(ctx), ctx->in, len, word_b, buf_len, word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200205 return LY_EMEM;
206 }
207
208 /* in case of realloc */
209 *word_p = *word_b;
Radek Krejcif29b7c32019-04-09 16:17:49 +0200210 } else if (word_b && need_buf) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200211 /* first time we need a buffer, copy everything read up to now */
212 if (*word_len) {
213 *word_b = malloc(*word_len);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200214 LY_CHECK_ERR_RET(!*word_b, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200215 *buf_len = *word_len;
216 memcpy(*word_b, *word_p, *word_len);
217 }
218
219 /* add this new character into buffer */
Radek Krejci33090f92020-12-17 20:12:46 +0100220 if (buf_add_char(PARSER_CTX(ctx), ctx->in, len, word_b, buf_len, word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200221 return LY_EMEM;
222 }
223
224 /* in case of realloc */
225 *word_p = *word_b;
226 } else {
227 /* just remember the first character pointer */
228 if (!*word_p) {
Radek Krejci33090f92020-12-17 20:12:46 +0100229 *word_p = (char *)ctx->in->current;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200230 }
Radek Krejci44ceedc2018-10-02 15:54:31 +0200231 /* ... and update the word's length */
232 (*word_len) += len;
Radek Krejci33090f92020-12-17 20:12:46 +0100233 ly_in_skip(ctx->in, len);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200234 }
235
236 return LY_SUCCESS;
237}
238
Michal Vaskoea5abea2018-09-18 13:10:54 +0200239/**
240 * @brief Skip YANG comment in data.
241 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200242 * @param[in] ctx yang parser context for logging.
Radek Krejci44ceedc2018-10-02 15:54:31 +0200243 * @param[in] comment Type of the comment to process:
244 * 1 for a one-line comment,
245 * 2 for a block comment.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200246 * @return LY_ERR values.
247 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200248LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +0200249skip_comment(struct lysp_yang_ctx *ctx, uint8_t comment)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200250{
Radek Krejcif13b87b2020-12-01 22:02:17 +0100251 /* internal statuses: */
252#define COMMENT_NO 0 /* comment ended */
253#define COMMENT_LINE 1 /* in line comment */
254#define COMMENT_BLOCK 2 /* in block comment */
255#define COMMENT_BLOCK_END 3 /* in block comment with last read character '*' */
256
Radek Krejci33090f92020-12-17 20:12:46 +0100257 while (ctx->in->current[0] && comment) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200258 switch (comment) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100259 case COMMENT_LINE:
Radek Krejci33090f92020-12-17 20:12:46 +0100260 if (ctx->in->current[0] == '\n') {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100261 comment = COMMENT_NO;
Radek Krejcid54412f2020-12-17 20:25:35 +0100262 LY_IN_NEW_LINE(ctx->in);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200263 }
264 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100265 case COMMENT_BLOCK:
Radek Krejci33090f92020-12-17 20:12:46 +0100266 if (ctx->in->current[0] == '*') {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100267 comment = COMMENT_BLOCK_END;
Radek Krejci33090f92020-12-17 20:12:46 +0100268 } else if (ctx->in->current[0] == '\n') {
Radek Krejcid54412f2020-12-17 20:25:35 +0100269 LY_IN_NEW_LINE(ctx->in);
Radek Krejci15c80ca2018-10-09 11:01:31 +0200270 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200271 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100272 case COMMENT_BLOCK_END:
Radek Krejci33090f92020-12-17 20:12:46 +0100273 if (ctx->in->current[0] == '/') {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100274 comment = COMMENT_NO;
Radek Krejci33090f92020-12-17 20:12:46 +0100275 } else if (ctx->in->current[0] != '*') {
276 if (ctx->in->current[0] == '\n') {
Radek Krejcid54412f2020-12-17 20:25:35 +0100277 LY_IN_NEW_LINE(ctx->in);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200278 }
Radek Krejcif13b87b2020-12-01 22:02:17 +0100279 comment = COMMENT_BLOCK;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200280 }
281 break;
282 default:
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200283 LOGINT_RET(PARSER_CTX(ctx));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200284 }
285
Radek Krejci33090f92020-12-17 20:12:46 +0100286 if (ctx->in->current[0] == '\n') {
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200287 ctx->indent = 0;
288 } else {
289 ++ctx->indent;
290 }
Radek Krejci33090f92020-12-17 20:12:46 +0100291 ++ctx->in->current;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200292 }
293
Radek Krejci33090f92020-12-17 20:12:46 +0100294 if (!ctx->in->current[0] && (comment >= COMMENT_BLOCK)) {
David Sedlákb3077192019-06-19 10:55:37 +0200295 LOGVAL_PARSER(ctx, LYVE_SYNTAX, "Unexpected end-of-input, non-terminated comment.");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200296 return LY_EVALID;
297 }
298
299 return LY_SUCCESS;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100300
301#undef COMMENT_NO
302#undef COMMENT_LINE
303#undef COMMENT_BLOCK
304#undef COMMENT_BLOCK_END
Michal Vasko7fbc8162018-09-17 10:35:16 +0200305}
306
Michal Vaskoea5abea2018-09-18 13:10:54 +0200307/**
308 * @brief Read a quoted string from data.
309 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200310 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200311 * @param[in] arg Type of YANG keyword argument expected.
312 * @param[out] word_p Pointer to the read quoted string.
313 * @param[out] word_b Pointer to a dynamically-allocated buffer holding the read quoted string. If not needed,
314 * set to NULL. Otherwise equal to \p word_p.
315 * @param[out] word_len Length of the read quoted string.
316 * @param[out] buf_len Length of the dynamically-allocated buffer \p word_b.
317 * @param[in] indent Current indent (number of YANG spaces). Needed for correct multi-line string
318 * indenation in the final quoted string.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200319 * @return LY_ERR values.
320 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200321static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +0200322read_qstring(struct lysp_yang_ctx *ctx, enum yang_arg arg, char **word_p, char **word_b,
Radek Krejci0f969882020-08-21 16:56:47 +0200323 size_t *word_len, size_t *buf_len)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200324{
Radek Krejcif13b87b2020-12-01 22:02:17 +0100325 /* string parsing status: */
326#define STRING_ENDED 0 /* string ended */
327#define STRING_SINGLE_QUOTED 1 /* string with ' */
328#define STRING_DOUBLE_QUOTED 2 /* string with " */
329#define STRING_DOUBLE_QUOTED_ESCAPED 3 /* string with " with last character \ */
330#define STRING_PAUSED_NEXTSTRING 4 /* string finished, now skipping whitespaces looking for + */
331#define STRING_PAUSED_CONTINUE 5 /* string continues after +, skipping whitespaces */
332
Radek Krejci1deb5be2020-08-26 16:43:36 +0200333 uint8_t string;
334 uint64_t block_indent = 0, current_indent = 0;
Radek Krejci857189e2020-09-01 13:26:36 +0200335 ly_bool need_buf = 0;
336 uint8_t prefix = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200337 const char *c;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200338 uint64_t trailing_ws = 0; /* current number of stored trailing whitespace characters */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200339
Radek Krejci33090f92020-12-17 20:12:46 +0100340 if (ctx->in->current[0] == '\"') {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100341 string = STRING_DOUBLE_QUOTED;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200342 current_indent = block_indent = ctx->indent + 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200343 } else {
Radek Krejci33090f92020-12-17 20:12:46 +0100344 assert(ctx->in->current[0] == '\'');
Radek Krejcif13b87b2020-12-01 22:02:17 +0100345 string = STRING_SINGLE_QUOTED;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200346 }
Radek Krejci33090f92020-12-17 20:12:46 +0100347 MOVE_INPUT(ctx, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200348
Radek Krejci33090f92020-12-17 20:12:46 +0100349 while (ctx->in->current[0] && string) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200350 switch (string) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100351 case STRING_SINGLE_QUOTED:
Michal Vasko4e55f5a2022-12-14 12:15:00 +0100352 if (ctx->in->current[0] == '\'') {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200353 /* string may be finished, but check for + */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100354 string = STRING_PAUSED_NEXTSTRING;
Radek Krejci33090f92020-12-17 20:12:46 +0100355 MOVE_INPUT(ctx, 1);
Michal Vasko4e55f5a2022-12-14 12:15:00 +0100356 } else {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200357 /* check and store character */
Radek Krejci33090f92020-12-17 20:12:46 +0100358 LY_CHECK_RET(buf_store_char(ctx, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200359 }
360 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100361 case STRING_DOUBLE_QUOTED:
Radek Krejci33090f92020-12-17 20:12:46 +0100362 switch (ctx->in->current[0]) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200363 case '\"':
364 /* string may be finished, but check for + */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100365 string = STRING_PAUSED_NEXTSTRING;
Radek Krejci33090f92020-12-17 20:12:46 +0100366 MOVE_INPUT(ctx, 1);
Radek Krejciff13cd12019-10-25 15:34:24 +0200367 trailing_ws = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200368 break;
369 case '\\':
370 /* special character following */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100371 string = STRING_DOUBLE_QUOTED_ESCAPED;
Radek Krejciff13cd12019-10-25 15:34:24 +0200372
373 /* the backslash sequence is substituted, so we will need a buffer to store the result */
374 need_buf = 1;
375
376 /* move forward to the escaped character */
Radek Krejci33090f92020-12-17 20:12:46 +0100377 ++ctx->in->current;
Radek Krejciff13cd12019-10-25 15:34:24 +0200378
379 /* note that the trailing whitespaces are supposed to be trimmed before substitution of
380 * backslash-escaped characters (RFC 7950, 6.1.3), so we have to zero the trailing whitespaces counter */
381 trailing_ws = 0;
382
383 /* since the backslash-escaped character is handled as first non-whitespace character, stop eating indentation */
384 current_indent = block_indent;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200385 break;
386 case ' ':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200387 if (current_indent < block_indent) {
388 ++current_indent;
Radek Krejci33090f92020-12-17 20:12:46 +0100389 MOVE_INPUT(ctx, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200390 } else {
Michal Vasko90edde42019-11-25 15:25:07 +0100391 /* check and store whitespace character */
Radek Krejci33090f92020-12-17 20:12:46 +0100392 LY_CHECK_RET(buf_store_char(ctx, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
Michal Vasko90edde42019-11-25 15:25:07 +0100393 trailing_ws++;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200394 }
395 break;
396 case '\t':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200397 if (current_indent < block_indent) {
398 assert(need_buf);
Radek Krejcif13b87b2020-12-01 22:02:17 +0100399 current_indent += Y_TAB_SPACES;
400 ctx->indent += Y_TAB_SPACES;
Michal Vaskod989ba02020-08-24 10:59:24 +0200401 for ( ; current_indent > block_indent; --current_indent, --ctx->indent) {
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200402 /* store leftover spaces from the tab */
Radek Krejci33090f92020-12-17 20:12:46 +0100403 c = ctx->in->current;
404 ctx->in->current = " ";
405 LY_CHECK_RET(buf_store_char(ctx, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
406 ctx->in->current = c;
Michal Vasko90edde42019-11-25 15:25:07 +0100407 trailing_ws++;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200408 }
Radek Krejci33090f92020-12-17 20:12:46 +0100409 ++ctx->in->current;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200410 } else {
Michal Vasko90edde42019-11-25 15:25:07 +0100411 /* check and store whitespace character */
Radek Krejci33090f92020-12-17 20:12:46 +0100412 LY_CHECK_RET(buf_store_char(ctx, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
Michal Vasko90edde42019-11-25 15:25:07 +0100413 trailing_ws++;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200414 /* additional characters for indentation - only 1 was count in buf_store_char */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100415 ctx->indent += Y_TAB_SPACES - 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200416 }
417 break;
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200418 case '\r':
Michal Vasko438f3782023-08-09 10:01:15 +0200419 /* newline may be escaped */
420 if ((ctx->in->current[1] != '\n') && strncmp(&ctx->in->current[1], "\\n", 2)) {
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200421 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, ctx->in->current[0]);
422 return LY_EVALID;
423 }
Michal Vasko38266a42023-08-11 11:28:56 +0200424
425 /* skip this character, do not store it */
426 ++ctx->in->current;
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200427 /* fallthrough */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200428 case '\n':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200429 if (block_indent) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200430 /* we will be removing the indents so we need our own buffer */
431 need_buf = 1;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200432
433 /* remove trailing tabs and spaces */
Radek Krejciff13cd12019-10-25 15:34:24 +0200434 (*word_len) = *word_len - trailing_ws;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200435
Radek Krejciff13cd12019-10-25 15:34:24 +0200436 /* restart indentation */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200437 current_indent = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200438 }
439
440 /* check and store character */
Radek Krejci33090f92020-12-17 20:12:46 +0100441 LY_CHECK_RET(buf_store_char(ctx, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200442
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200443 /* reset context indentation counter for possible string after this one */
444 ctx->indent = 0;
Radek Krejciff13cd12019-10-25 15:34:24 +0200445 trailing_ws = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200446 break;
447 default:
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200448 /* first non-whitespace character, stop eating indentation */
449 current_indent = block_indent;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200450
451 /* check and store character */
Radek Krejci33090f92020-12-17 20:12:46 +0100452 LY_CHECK_RET(buf_store_char(ctx, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
Radek Krejciff13cd12019-10-25 15:34:24 +0200453 trailing_ws = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200454 break;
455 }
456 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100457 case STRING_DOUBLE_QUOTED_ESCAPED:
Michal Vasko7fbc8162018-09-17 10:35:16 +0200458 /* string encoded characters */
Radek Krejci33090f92020-12-17 20:12:46 +0100459 c = ctx->in->current;
460 switch (ctx->in->current[0]) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200461 case 'n':
Radek Krejci33090f92020-12-17 20:12:46 +0100462 ctx->in->current = "\n";
Radek Krejcidd713ce2021-01-04 23:12:12 +0100463 /* fix false newline count in buf_store_char() */
464 ctx->in->line--;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200465 break;
466 case 't':
Radek Krejci33090f92020-12-17 20:12:46 +0100467 ctx->in->current = "\t";
Michal Vasko7fbc8162018-09-17 10:35:16 +0200468 break;
469 case '\"':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200470 case '\\':
Michal Vasko63f3d842020-07-08 10:10:14 +0200471 /* ok as is */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200472 break;
473 default:
Michal Vasko63f3d842020-07-08 10:10:14 +0200474 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Double-quoted string unknown special character '\\%c'.",
Radek Krejci33090f92020-12-17 20:12:46 +0100475 ctx->in->current[0]);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200476 return LY_EVALID;
477 }
478
479 /* check and store character */
Radek Krejci33090f92020-12-17 20:12:46 +0100480 LY_CHECK_RET(buf_store_char(ctx, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200481
Radek Krejcif13b87b2020-12-01 22:02:17 +0100482 string = STRING_DOUBLE_QUOTED;
Radek Krejci33090f92020-12-17 20:12:46 +0100483 ctx->in->current = c + 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200484 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100485 case STRING_PAUSED_NEXTSTRING:
Radek Krejci33090f92020-12-17 20:12:46 +0100486 switch (ctx->in->current[0]) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200487 case '+':
488 /* string continues */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100489 string = STRING_PAUSED_CONTINUE;
Radek Krejciefd22f62018-09-27 11:47:58 +0200490 need_buf = 1;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200491 break;
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200492 case '\r':
493 if (ctx->in->current[1] != '\n') {
494 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, ctx->in->current[0]);
495 return LY_EVALID;
496 }
497 MOVE_INPUT(ctx, 1);
498 /* fallthrough */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200499 case '\n':
Radek Krejcidd713ce2021-01-04 23:12:12 +0100500 LY_IN_NEW_LINE(ctx->in);
Radek Krejcicb3e6472021-01-06 08:19:01 +0100501 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200502 case ' ':
503 case '\t':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200504 /* just skip */
505 break;
506 default:
507 /* string is finished */
508 goto string_end;
509 }
Radek Krejci33090f92020-12-17 20:12:46 +0100510 MOVE_INPUT(ctx, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200511 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100512 case STRING_PAUSED_CONTINUE:
Radek Krejci33090f92020-12-17 20:12:46 +0100513 switch (ctx->in->current[0]) {
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200514 case '\r':
515 if (ctx->in->current[1] != '\n') {
516 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, ctx->in->current[0]);
517 return LY_EVALID;
518 }
519 MOVE_INPUT(ctx, 1);
520 /* fallthrough */
Radek Krejci44ceedc2018-10-02 15:54:31 +0200521 case '\n':
Radek Krejcidd713ce2021-01-04 23:12:12 +0100522 LY_IN_NEW_LINE(ctx->in);
Radek Krejcicb3e6472021-01-06 08:19:01 +0100523 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200524 case ' ':
525 case '\t':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200526 /* skip */
Michal Vasko56836262024-05-23 11:15:56 +0200527 MOVE_INPUT(ctx, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200528 break;
529 case '\'':
Radek Krejcif13b87b2020-12-01 22:02:17 +0100530 string = STRING_SINGLE_QUOTED;
Michal Vasko56836262024-05-23 11:15:56 +0200531 MOVE_INPUT(ctx, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200532 break;
533 case '\"':
Radek Krejcif13b87b2020-12-01 22:02:17 +0100534 string = STRING_DOUBLE_QUOTED;
Michal Vasko56836262024-05-23 11:15:56 +0200535 MOVE_INPUT(ctx, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200536 break;
Michal Vasko453b0002024-05-21 15:10:23 +0200537 case '/':
538 if (ctx->in->current[1] == '/') {
539 /* one-line comment */
540 MOVE_INPUT(ctx, 2);
541 LY_CHECK_RET(skip_comment(ctx, 1));
542 break;
543 } else if (ctx->in->current[1] == '*') {
544 /* block comment */
545 MOVE_INPUT(ctx, 2);
546 LY_CHECK_RET(skip_comment(ctx, 2));
547 break;
548 }
549 /* not a comment after all */
550 /* fallthrough */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200551 default:
552 /* it must be quoted again */
David Sedlákb3077192019-06-19 10:55:37 +0200553 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Both string parts divided by '+' must be quoted.");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200554 return LY_EVALID;
555 }
556 break;
557 default:
558 return LY_EINT;
559 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200560 }
561
562string_end:
Michal Vasko69730152020-10-09 16:30:07 +0200563 if ((arg <= Y_PREF_IDENTIF_ARG) && !(*word_len)) {
Radek Krejci4e199f52019-05-28 09:09:28 +0200564 /* empty identifier */
David Sedlákb3077192019-06-19 10:55:37 +0200565 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Statement argument is required.");
Radek Krejci4e199f52019-05-28 09:09:28 +0200566 return LY_EVALID;
567 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200568 return LY_SUCCESS;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100569
570#undef STRING_ENDED
571#undef STRING_SINGLE_QUOTED
572#undef STRING_DOUBLE_QUOTED
573#undef STRING_DOUBLE_QUOTED_ESCAPED
574#undef STRING_PAUSED_NEXTSTRING
575#undef STRING_PAUSED_CONTINUE
Michal Vasko7fbc8162018-09-17 10:35:16 +0200576}
577
Michal Vaskoea5abea2018-09-18 13:10:54 +0200578/**
579 * @brief Get another YANG string from the raw data.
580 *
Michal Vasko72c6d642024-02-27 14:59:01 +0100581 * @param[in] ctx Yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200582 * @param[in] arg Type of YANG keyword argument expected.
Michal Vasko72c6d642024-02-27 14:59:01 +0100583 * @param[out] flags Optional output argument to get flag of the argument's quoting (LYS_*QUOTED - see
Michal Vaskob68ea142021-04-26 13:46:00 +0200584 * [schema node flags](@ref snodeflags))
Michal Vasko72c6d642024-02-27 14:59:01 +0100585 * @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 +0200586 * @param[out] word_b Pointer to a dynamically-allocated buffer holding the read string. If not needed,
Michal Vasko72c6d642024-02-27 14:59:01 +0100587 * set to NULL. Otherwise equal to @p word_p.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200588 * @param[out] word_len Length of the read string.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200589 * @return LY_ERR values.
Michal Vasko7fbc8162018-09-17 10:35:16 +0200590 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200591LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +0200592get_argument(struct lysp_yang_ctx *ctx, enum yang_arg arg, uint16_t *flags, char **word_p,
Radek Krejci0f969882020-08-21 16:56:47 +0200593 char **word_b, size_t *word_len)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200594{
Michal Vaskob68ea142021-04-26 13:46:00 +0200595 LY_ERR ret;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200596 size_t buf_len = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200597 uint8_t prefix = 0;
Michal Vasko1e4b1162024-01-19 09:16:49 +0100598 ly_bool str_end = 0;
Michal Vasko69730152020-10-09 16:30:07 +0200599
Michal Vasko7fbc8162018-09-17 10:35:16 +0200600 /* word buffer - dynamically allocated */
601 *word_b = NULL;
602
603 /* word pointer - just a pointer to data */
604 *word_p = NULL;
605
606 *word_len = 0;
Michal Vasko1e4b1162024-01-19 09:16:49 +0100607 while (!str_end) {
Radek Krejci33090f92020-12-17 20:12:46 +0100608 switch (ctx->in->current[0]) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200609 case '\'':
610 case '\"':
611 if (*word_len) {
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200612 /* invalid - quotes cannot be in unquoted string and only optsep, ; or { can follow it */
Radek Krejci33090f92020-12-17 20:12:46 +0100613 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, 1, ctx->in->current,
Michal Vasko69730152020-10-09 16:30:07 +0200614 "unquoted string character, optsep, semicolon or opening brace");
Michal Vaskob68ea142021-04-26 13:46:00 +0200615 ret = LY_EVALID;
616 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200617 }
Radek Krejcid3ca0632019-04-16 16:54:54 +0200618 if (flags) {
Michal Vasko72c6d642024-02-27 14:59:01 +0100619 (*flags) |= (ctx->in->current[0] == '\'') ? LYS_SINGLEQUOTED : LYS_DOUBLEQUOTED;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200620 }
Michal Vasko1e4b1162024-01-19 09:16:49 +0100621
Michal Vaskob68ea142021-04-26 13:46:00 +0200622 LY_CHECK_GOTO(ret = read_qstring(ctx, arg, word_p, word_b, word_len, &buf_len), error);
Michal Vasko55a16b92021-09-15 08:51:35 +0200623 if (!*word_p) {
624 /* do not return NULL word */
625 *word_p = "";
626 }
Michal Vasko1e4b1162024-01-19 09:16:49 +0100627 str_end = 1;
628 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200629 case '/':
Radek Krejci33090f92020-12-17 20:12:46 +0100630 if (ctx->in->current[1] == '/') {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200631 /* one-line comment */
Radek Krejci33090f92020-12-17 20:12:46 +0100632 MOVE_INPUT(ctx, 2);
Michal Vaskob68ea142021-04-26 13:46:00 +0200633 LY_CHECK_GOTO(ret = skip_comment(ctx, 1), error);
Radek Krejci33090f92020-12-17 20:12:46 +0100634 } else if (ctx->in->current[1] == '*') {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200635 /* block comment */
Radek Krejci33090f92020-12-17 20:12:46 +0100636 MOVE_INPUT(ctx, 2);
Michal Vaskob68ea142021-04-26 13:46:00 +0200637 LY_CHECK_GOTO(ret = skip_comment(ctx, 2), error);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200638 } else {
639 /* not a comment after all */
Michal Vaskob68ea142021-04-26 13:46:00 +0200640 LY_CHECK_GOTO(ret = buf_store_char(ctx, arg, word_p, word_len, word_b, &buf_len, 0, &prefix), error);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200641 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200642 break;
643 case ' ':
644 if (*word_len) {
645 /* word is finished */
Michal Vasko1e4b1162024-01-19 09:16:49 +0100646 str_end = 1;
647 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200648 }
Radek Krejci33090f92020-12-17 20:12:46 +0100649 MOVE_INPUT(ctx, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200650 break;
651 case '\t':
652 if (*word_len) {
653 /* word is finished */
Michal Vasko1e4b1162024-01-19 09:16:49 +0100654 str_end = 1;
655 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200656 }
657 /* tabs count for 8 spaces */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100658 ctx->indent += Y_TAB_SPACES;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200659
Radek Krejci33090f92020-12-17 20:12:46 +0100660 ++ctx->in->current;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200661 break;
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200662 case '\r':
663 if (ctx->in->current[1] != '\n') {
664 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, ctx->in->current[0]);
Michal Vaskob68ea142021-04-26 13:46:00 +0200665 ret = LY_EVALID;
666 goto error;
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200667 }
668 MOVE_INPUT(ctx, 1);
669 /* fallthrough */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200670 case '\n':
671 if (*word_len) {
672 /* word is finished */
Michal Vasko1e4b1162024-01-19 09:16:49 +0100673 str_end = 1;
674 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200675 }
Radek Krejcidd713ce2021-01-04 23:12:12 +0100676 LY_IN_NEW_LINE(ctx->in);
Radek Krejcid54412f2020-12-17 20:25:35 +0100677 MOVE_INPUT(ctx, 1);
678
Michal Vasko7fbc8162018-09-17 10:35:16 +0200679 /* reset indent */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200680 ctx->indent = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200681 break;
682 case ';':
683 case '{':
684 if (*word_len || (arg == Y_MAYBE_STR_ARG)) {
685 /* word is finished */
Michal Vasko1e4b1162024-01-19 09:16:49 +0100686 str_end = 1;
687 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200688 }
689
Radek Krejci33090f92020-12-17 20:12:46 +0100690 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, 1, ctx->in->current, "an argument");
Michal Vaskob68ea142021-04-26 13:46:00 +0200691 ret = LY_EVALID;
692 goto error;
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200693 case '}':
694 /* invalid - braces cannot be in unquoted string (opening braces terminates the string and can follow it) */
Radek Krejci33090f92020-12-17 20:12:46 +0100695 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, 1, ctx->in->current,
Michal Vasko69730152020-10-09 16:30:07 +0200696 "unquoted string character, optsep, semicolon or opening brace");
Michal Vaskob68ea142021-04-26 13:46:00 +0200697 ret = LY_EVALID;
698 goto error;
Michal Vasko1e4b1162024-01-19 09:16:49 +0100699 case '\0':
700 /* unexpected EOF */
701 LOGVAL_PARSER(ctx, LY_VCODE_EOF);
702 ret = LY_EVALID;
703 goto error;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200704 default:
Michal Vaskob68ea142021-04-26 13:46:00 +0200705 LY_CHECK_GOTO(ret = buf_store_char(ctx, arg, word_p, word_len, word_b, &buf_len, 0, &prefix), error);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200706 break;
707 }
Michal Vasko7fbc8162018-09-17 10:35:16 +0200708 }
709
Radek Krejci44ceedc2018-10-02 15:54:31 +0200710 /* terminating NULL byte for buf */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200711 if (*word_b) {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200712 (*word_b) = ly_realloc(*word_b, (*word_len) + 1);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200713 LY_CHECK_ERR_RET(!(*word_b), LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200714 (*word_b)[*word_len] = '\0';
Michal Vasko7fbc8162018-09-17 10:35:16 +0200715 *word_p = *word_b;
716 }
717
718 return LY_SUCCESS;
Michal Vaskob68ea142021-04-26 13:46:00 +0200719
720error:
721 free(*word_b);
722 *word_b = NULL;
723 return ret;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200724}
725
Michal Vaskoea5abea2018-09-18 13:10:54 +0200726/**
727 * @brief Get another YANG keyword from the raw data.
728 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200729 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200730 * @param[out] kw YANG keyword read.
731 * @param[out] word_p Pointer to the keyword in the data. Useful for extension instances.
732 * @param[out] word_len Length of the keyword in the data. Useful for extension instances.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200733 * @return LY_ERR values.
734 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200735LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +0200736get_keyword(struct lysp_yang_ctx *ctx, enum ly_stmt *kw, char **word_p, size_t *word_len)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200737{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200738 uint8_t prefix;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200739 const char *word_start;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200740 size_t len;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200741
742 if (word_p) {
743 *word_p = NULL;
744 *word_len = 0;
745 }
746
747 /* first skip "optsep", comments */
Radek Krejci33090f92020-12-17 20:12:46 +0100748 while (ctx->in->current[0]) {
749 switch (ctx->in->current[0]) {
Radek Krejcidcc7b322018-10-11 14:24:02 +0200750 case '/':
Radek Krejci33090f92020-12-17 20:12:46 +0100751 if (ctx->in->current[1] == '/') {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200752 /* one-line comment */
Radek Krejci33090f92020-12-17 20:12:46 +0100753 MOVE_INPUT(ctx, 2);
754 LY_CHECK_RET(skip_comment(ctx, 1));
755 } else if (ctx->in->current[1] == '*') {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200756 /* block comment */
Radek Krejci33090f92020-12-17 20:12:46 +0100757 MOVE_INPUT(ctx, 2);
758 LY_CHECK_RET(skip_comment(ctx, 2));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200759 } else {
Radek Krejcidcc7b322018-10-11 14:24:02 +0200760 /* error - not a comment after all, keyword cannot start with slash */
David Sedlákb3077192019-06-19 10:55:37 +0200761 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid identifier first character '/'.");
Radek Krejcidcc7b322018-10-11 14:24:02 +0200762 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200763 }
Radek Krejci13028282019-06-11 14:56:48 +0200764 continue;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200765 case '\n':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200766 /* skip whitespaces (optsep) */
Radek Krejcidd713ce2021-01-04 23:12:12 +0100767 LY_IN_NEW_LINE(ctx->in);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200768 ctx->indent = 0;
769 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200770 case ' ':
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200771 /* skip whitespaces (optsep) */
772 ++ctx->indent;
773 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200774 case '\t':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200775 /* skip whitespaces (optsep) */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100776 ctx->indent += Y_TAB_SPACES;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200777 break;
Michal Vasko50dcbc22021-03-25 12:21:20 +0100778 case '\r':
779 /* possible CRLF endline */
780 if (ctx->in->current[1] == '\n') {
781 break;
782 }
Radek Krejcid43298b2021-03-25 16:17:15 +0100783 /* fallthrough */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200784 default:
785 /* either a keyword start or an invalid character */
786 goto keyword_start;
787 }
788
Radek Krejci33090f92020-12-17 20:12:46 +0100789 ly_in_skip(ctx->in, 1);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200790 }
791
792keyword_start:
Radek Krejci33090f92020-12-17 20:12:46 +0100793 word_start = ctx->in->current;
Radek Krejcid54412f2020-12-17 20:25:35 +0100794 *kw = lysp_match_kw(ctx->in, &ctx->indent);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200795
aPiecek93582ed2021-05-25 14:49:06 +0200796 if (*kw == LY_STMT_SYNTAX_SEMICOLON) {
797 goto success;
798 } else if (*kw == LY_STMT_SYNTAX_LEFT_BRACE) {
799 ctx->depth++;
800 if (ctx->depth > LY_MAX_BLOCK_DEPTH) {
Michal Vasko8a67eff2021-12-07 14:04:47 +0100801 LOGERR(PARSER_CTX(ctx), LY_EINVAL, "The maximum number of block nestings has been exceeded.");
aPiecek93582ed2021-05-25 14:49:06 +0200802 return LY_EINVAL;
803 }
804 goto success;
805 } else if (*kw == LY_STMT_SYNTAX_RIGHT_BRACE) {
806 ctx->depth--;
Radek Krejci626df482018-10-11 15:06:31 +0200807 goto success;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200808 }
809
Radek Krejcid6b76452019-09-03 17:03:03 +0200810 if (*kw != LY_STMT_NONE) {
Michal Vasko7fbc8162018-09-17 10:35:16 +0200811 /* make sure we have the whole keyword */
Radek Krejci33090f92020-12-17 20:12:46 +0100812 switch (ctx->in->current[0]) {
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200813 case '\r':
814 if (ctx->in->current[1] != '\n') {
815 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, ctx->in->current[0]);
816 return LY_EVALID;
817 }
818 MOVE_INPUT(ctx, 1);
819 /* fallthrough */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200820 case '\n':
Michal Vasko7fbc8162018-09-17 10:35:16 +0200821 case '\t':
Radek Krejciabdd8062019-06-11 16:44:19 +0200822 case ' ':
823 /* mandatory "sep" is just checked, not eaten so nothing in the context is updated */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200824 break;
Radek Krejci156ccaf2018-10-15 15:49:17 +0200825 case ':':
826 /* keyword is not actually a keyword, but prefix of an extension.
827 * To avoid repeated check of the prefix syntax, move to the point where the colon was read
828 * and we will be checking the keyword (extension instance) itself */
829 prefix = 1;
Radek Krejci33090f92020-12-17 20:12:46 +0100830 MOVE_INPUT(ctx, 1);
Radek Krejci156ccaf2018-10-15 15:49:17 +0200831 goto extension;
Radek Krejcidcc7b322018-10-11 14:24:02 +0200832 case '{':
Michal Vasko946b70c2023-07-19 08:57:31 +0200833 case ';':
834 /* allowed only for input and output statements which are without arguments */
Michal Vasko69730152020-10-09 16:30:07 +0200835 if ((*kw == LY_STMT_INPUT) || (*kw == LY_STMT_OUTPUT)) {
Radek Krejcidcc7b322018-10-11 14:24:02 +0200836 break;
837 }
Radek Krejcif13b87b2020-12-01 22:02:17 +0100838 /* fall through */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200839 default:
Radek Krejci33090f92020-12-17 20:12:46 +0100840 MOVE_INPUT(ctx, 1);
841 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, (int)(ctx->in->current - word_start), word_start,
Michal Vasko69730152020-10-09 16:30:07 +0200842 "a keyword followed by a separator");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200843 return LY_EVALID;
844 }
845 } else {
846 /* still can be an extension */
847 prefix = 0;
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200848
Radek Krejci156ccaf2018-10-15 15:49:17 +0200849extension:
Radek Krejcid54412f2020-12-17 20:25:35 +0100850 while (ctx->in->current[0] && (ctx->in->current[0] != ' ') && (ctx->in->current[0] != '\t') &&
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200851 (ctx->in->current[0] != '\n') && (ctx->in->current[0] != '\r') && (ctx->in->current[0] != '{') &&
852 (ctx->in->current[0] != ';')) {
Radek Krejci1deb5be2020-08-26 16:43:36 +0200853 uint32_t c = 0;
854
Radek Krejci33090f92020-12-17 20:12:46 +0100855 LY_CHECK_ERR_RET(ly_getutf8(&ctx->in->current, &c, &len),
856 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, ctx->in->current[-len]), LY_EVALID);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200857 ++ctx->indent;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200858 /* check character validity */
Michal Vaskod0625d72022-10-06 15:02:50 +0200859 LY_CHECK_RET(lysp_check_identifierchar((struct lysp_ctx *)ctx, c,
Radek Krejci33090f92020-12-17 20:12:46 +0100860 ctx->in->current - len == word_start ? 1 : 0, &prefix));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200861 }
Radek Krejci33090f92020-12-17 20:12:46 +0100862 if (!ctx->in->current[0]) {
David Sedlákb3077192019-06-19 10:55:37 +0200863 LOGVAL_PARSER(ctx, LY_VCODE_EOF);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200864 return LY_EVALID;
865 }
866
867 /* prefix is mandatory for extension instances */
Radek Krejcidcc7b322018-10-11 14:24:02 +0200868 if (prefix != 2) {
Radek Krejci33090f92020-12-17 20:12:46 +0100869 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, (int)(ctx->in->current - word_start), word_start, "a keyword");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200870 return LY_EVALID;
871 }
872
Radek Krejcid6b76452019-09-03 17:03:03 +0200873 *kw = LY_STMT_EXTENSION_INSTANCE;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200874 }
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200875
Radek Krejci626df482018-10-11 15:06:31 +0200876success:
Michal Vasko7fbc8162018-09-17 10:35:16 +0200877 if (word_p) {
878 *word_p = (char *)word_start;
Radek Krejci33090f92020-12-17 20:12:46 +0100879 *word_len = ctx->in->current - word_start;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200880 }
881
882 return LY_SUCCESS;
883}
884
Michal Vaskoea5abea2018-09-18 13:10:54 +0200885/**
886 * @brief Parse extension instance substatements.
887 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200888 * @param[in] ctx yang parser context for logging.
Radek Krejci335332a2019-09-05 13:03:35 +0200889 * @param[in] kw Statement keyword value matching @p word value.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200890 * @param[in] word Extension instance substatement name (keyword).
891 * @param[in] word_len Extension instance substatement name length.
892 * @param[in,out] child Children of this extension instance to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200893 * @return LY_ERR values.
894 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200895static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +0200896parse_ext_substmt(struct lysp_yang_ctx *ctx, enum ly_stmt kw, char *word, size_t word_len,
Radek Krejci0f969882020-08-21 16:56:47 +0200897 struct lysp_stmt **child)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200898{
899 char *buf;
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100900 LY_ERR ret = LY_SUCCESS;
Radek Krejci335332a2019-09-05 13:03:35 +0200901 enum ly_stmt child_kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200902 struct lysp_stmt *stmt, *par_child;
903
904 stmt = calloc(1, sizeof *stmt);
905 LY_CHECK_ERR_RET(!stmt, LOGMEM(NULL), LY_EMEM);
906
Radek Krejcibb9b1982019-04-08 14:24:59 +0200907 /* insert into parent statements */
908 if (!*child) {
909 *child = stmt;
910 } else {
Radek Krejci1e008d22020-08-17 11:37:37 +0200911 for (par_child = *child; par_child->next; par_child = par_child->next) {}
Radek Krejcibb9b1982019-04-08 14:24:59 +0200912 par_child->next = stmt;
913 }
914
Michal Vaskofc2cd072021-02-24 13:17:17 +0100915 /* statement */
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200916 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), word, word_len, &stmt->stmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200917
918 /* get optional argument */
Radek Krejci33090f92020-12-17 20:12:46 +0100919 LY_CHECK_RET(get_argument(ctx, Y_MAYBE_STR_ARG, &stmt->flags, &word, &buf, &word_len));
Radek Krejci0ae092d2018-09-20 16:43:19 +0200920 if (word) {
Michal Vasko12ef5362022-09-16 15:13:58 +0200921 INSERT_WORD_GOTO(ctx, buf, stmt->arg, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200922 }
923
Radek Krejci8df109d2021-04-23 12:19:08 +0200924 stmt->format = LY_VALUE_SCHEMA;
Michal Vasko8a67eff2021-12-07 14:04:47 +0100925 stmt->prefix_data = PARSER_CUR_PMOD(ctx);
Michal Vaskofc2cd072021-02-24 13:17:17 +0100926 stmt->kw = kw;
927
Michal Vaskoc0c64ae2022-10-06 10:15:23 +0200928 YANG_READ_SUBSTMT_FOR_GOTO(ctx, child_kw, word, word_len, ret, cleanup) {
929 LY_CHECK_GOTO(ret = parse_ext_substmt(ctx, child_kw, word, word_len, &stmt->child), cleanup)
930 YANG_READ_SUBSTMT_NEXT_ITER(ctx, child_kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200931 }
Michal Vasko12ef5362022-09-16 15:13:58 +0200932
933cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +0200934 return ret;
935}
936
Michal Vaskoea5abea2018-09-18 13:10:54 +0200937/**
938 * @brief Parse extension instance.
939 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200940 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200941 * @param[in] ext_name Extension instance substatement name (keyword).
942 * @param[in] ext_name_len Extension instance substatement name length.
Michal Vasko193dacd2022-10-13 08:43:05 +0200943 * @param[in] parent Current statement parent.
944 * @param[in] parent_stmt Type of @p parent statement.
945 * @param[in] parent_stmt_index In case of several @p parent_stmt, index of the relevant @p parent statement.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200946 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200947 * @return LY_ERR values.
948 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200949static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +0200950parse_ext(struct lysp_yang_ctx *ctx, const char *ext_name, size_t ext_name_len, const void *parent,
951 enum ly_stmt parent_stmt, LY_ARRAY_COUNT_TYPE parent_stmt_index, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200952{
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100953 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200954 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +0200955 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200956 struct lysp_ext_instance *e;
Radek Krejcid6b76452019-09-03 17:03:03 +0200957 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200958
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200959 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *exts, e, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200960
Michal Vaskofc2cd072021-02-24 13:17:17 +0100961 if (!ly_strnchr(ext_name, ':', ext_name_len)) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +0200962 LOGVAL_PARSER(ctx, LYVE_SYNTAX, "Extension instance \"%.*s\" without the mandatory prefix.",
963 (int)ext_name_len, ext_name);
Michal Vaskofc2cd072021-02-24 13:17:17 +0100964 return LY_EVALID;
965 }
966
967 /* store name */
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200968 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), ext_name, ext_name_len, &e->name));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200969
970 /* get optional argument */
Radek Krejci33090f92020-12-17 20:12:46 +0100971 LY_CHECK_RET(get_argument(ctx, Y_MAYBE_STR_ARG, NULL, &word, &buf, &word_len));
Radek Krejci0ae092d2018-09-20 16:43:19 +0200972 if (word) {
Michal Vasko12ef5362022-09-16 15:13:58 +0200973 INSERT_WORD_GOTO(ctx, buf, e->argument, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200974 }
975
Michal Vaskofc2cd072021-02-24 13:17:17 +0100976 /* store the rest of information */
Radek Krejci8df109d2021-04-23 12:19:08 +0200977 e->format = LY_VALUE_SCHEMA;
aPiecek60d9d672021-04-27 15:49:57 +0200978 e->parsed = NULL;
Michal Vasko8a67eff2021-12-07 14:04:47 +0100979 e->prefix_data = PARSER_CUR_PMOD(ctx);
Michal Vasko193dacd2022-10-13 08:43:05 +0200980 e->parent = (void *)parent;
981 e->parent_stmt = parent_stmt;
982 e->parent_stmt_index = parent_stmt_index;
Michal Vaskofc2cd072021-02-24 13:17:17 +0100983
Michal Vaskoc0c64ae2022-10-06 10:15:23 +0200984 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
985 LY_CHECK_GOTO(ret = parse_ext_substmt(ctx, kw, word, word_len, &e->child), cleanup)
986 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200987 }
Michal Vasko12ef5362022-09-16 15:13:58 +0200988
989cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +0200990 return ret;
991}
992
Michal Vaskoea5abea2018-09-18 13:10:54 +0200993/**
994 * @brief Parse a generic text field without specific constraints. Those are contact, organization,
995 * description, etc...
996 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200997 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +0200998 * @param[in] parent Current statement parent.
999 * @param[in] parent_stmt Type of statement in @p value.
1000 * @param[in] parent_stmt_index In case of several @p parent_stmt, index of the relevant @p parent statement.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001001 * @param[in,out] value Place to store the parsed value.
1002 * @param[in] arg Type of the YANG keyword argument (of the value).
Michal Vasko72c6d642024-02-27 14:59:01 +01001003 * @param[out] flags Optional output argument to get flag of the argument's quoting (LYS_*QUOTED - see
1004 * [schema node flags](@ref snodeflags))
Michal Vaskoea5abea2018-09-18 13:10:54 +02001005 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001006 * @return LY_ERR values.
1007 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001008static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02001009parse_text_field(struct lysp_yang_ctx *ctx, const void *parent, enum ly_stmt parent_stmt, uint32_t parent_stmt_index,
Michal Vasko72c6d642024-02-27 14:59:01 +01001010 const char **value, enum yang_arg arg, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001011{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001012 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001013 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001014 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001015 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001016
1017 if (*value) {
Michal Vasko193dacd2022-10-13 08:43:05 +02001018 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, lyplg_ext_stmt2str(parent_stmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001019 return LY_EVALID;
1020 }
1021
1022 /* get value */
Michal Vasko72c6d642024-02-27 14:59:01 +01001023 LY_CHECK_RET(get_argument(ctx, arg, flags, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001024
1025 /* store value and spend buf if allocated */
Michal Vasko12ef5362022-09-16 15:13:58 +02001026 INSERT_WORD_GOTO(ctx, buf, *value, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001027
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001028 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001029 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001030 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001031 LY_CHECK_RET(parse_ext(ctx, word, word_len, parent, parent_stmt, parent_stmt_index, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001032 break;
1033 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001034 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), lyplg_ext_stmt2str(parent_stmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001035 return LY_EVALID;
1036 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001037 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001038 }
Michal Vasko12ef5362022-09-16 15:13:58 +02001039
1040cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001041 return ret;
1042}
1043
Michal Vaskoea5abea2018-09-18 13:10:54 +02001044/**
1045 * @brief Parse the yang-version statement.
1046 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001047 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02001048 * @param[in,out] mod Module to fill.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001049 * @return LY_ERR values.
1050 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001051static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02001052parse_yangversion(struct lysp_yang_ctx *ctx, struct lysp_module *mod)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001053{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001054 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001055 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001056 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001057 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001058
Michal Vasko193dacd2022-10-13 08:43:05 +02001059 if (mod->version) {
David Sedlákb3077192019-06-19 10:55:37 +02001060 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001061 return LY_EVALID;
1062 }
1063
1064 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001065 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001066
Radek Krejci96e48da2020-09-04 13:18:06 +02001067 if ((word_len == 1) && !strncmp(word, "1", word_len)) {
Michal Vasko193dacd2022-10-13 08:43:05 +02001068 mod->version = LYS_VERSION_1_0;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001069 } else if ((word_len == ly_strlen_const("1.1")) && !strncmp(word, "1.1", word_len)) {
Michal Vasko193dacd2022-10-13 08:43:05 +02001070 mod->version = LYS_VERSION_1_1;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001071 } else {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02001072 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001073 free(buf);
1074 return LY_EVALID;
1075 }
1076 free(buf);
1077
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001078 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001079 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001080 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001081 LY_CHECK_RET(parse_ext(ctx, word, word_len, mod, LY_STMT_YANG_VERSION, 0, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001082 break;
1083 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001084 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001085 return LY_EVALID;
1086 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001087 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001088 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001089
1090cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001091 return ret;
1092}
1093
Michal Vaskoea5abea2018-09-18 13:10:54 +02001094/**
1095 * @brief Parse the belongs-to statement.
1096 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001097 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02001098 * @param[in,out] submod Submodule to fill.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001099 * @return LY_ERR values.
1100 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001101static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02001102parse_belongsto(struct lysp_yang_ctx *ctx, struct lysp_submodule *submod)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001103{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001104 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001105 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001106 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001107 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001108
Michal Vasko193dacd2022-10-13 08:43:05 +02001109 if (submod->prefix) {
David Sedlákb3077192019-06-19 10:55:37 +02001110 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001111 return LY_EVALID;
1112 }
1113
Michal Vaskoc3781c32020-10-06 14:04:08 +02001114 /* get value, it must match the main module */
Radek Krejci33090f92020-12-17 20:12:46 +01001115 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko8a67eff2021-12-07 14:04:47 +01001116 if (ly_strncmp(PARSER_CUR_PMOD(ctx)->mod->name, word, word_len)) {
Michal Vaskoc3781c32020-10-06 14:04:08 +02001117 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Submodule \"belongs-to\" value \"%.*s\" does not match its module name \"%s\".",
Michal Vasko8a67eff2021-12-07 14:04:47 +01001118 (int)word_len, word, PARSER_CUR_PMOD(ctx)->mod->name);
Michal Vaskoc3781c32020-10-06 14:04:08 +02001119 free(buf);
1120 return LY_EVALID;
1121 }
1122 free(buf);
Radek Krejcif09e4e82019-06-14 15:08:11 +02001123
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001124 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001125 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001126 case LY_STMT_PREFIX:
Michal Vasko72c6d642024-02-27 14:59:01 +01001127 LY_CHECK_RET(parse_text_field(ctx, submod->prefix, LY_STMT_PREFIX, 0, &submod->prefix, Y_IDENTIF_ARG, NULL,
1128 &submod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001129 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001130 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001131 LY_CHECK_RET(parse_ext(ctx, word, word_len, submod, LY_STMT_BELONGS_TO, 0, &submod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001132 break;
1133 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001134 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001135 return LY_EVALID;
1136 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001137 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001138 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001139
Michal Vasko7fbc8162018-09-17 10:35:16 +02001140 /* mandatory substatements */
Michal Vasko193dacd2022-10-13 08:43:05 +02001141 if (!submod->prefix) {
David Sedlákb3077192019-06-19 10:55:37 +02001142 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001143 return LY_EVALID;
1144 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001145
1146cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001147 return ret;
1148}
1149
Michal Vaskoea5abea2018-09-18 13:10:54 +02001150/**
1151 * @brief Parse the revision-date statement.
1152 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001153 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02001154 * @param[in,out] rev Buffer to store the parsed value in.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001155 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001156 * @return LY_ERR values.
1157 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001158static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001159parse_revisiondate(struct lysp_yang_ctx *ctx, char *rev, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001160{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001161 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001162 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001163 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001164 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001165
1166 if (rev[0]) {
David Sedlákb3077192019-06-19 10:55:37 +02001167 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "revision-date");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001168 return LY_EVALID;
1169 }
1170
1171 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001172 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001173
1174 /* check value */
Michal Vaskod0625d72022-10-06 15:02:50 +02001175 if (lysp_check_date((struct lysp_ctx *)ctx, word, word_len, "revision-date")) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001176 free(buf);
1177 return LY_EVALID;
1178 }
1179
1180 /* store value and spend buf if allocated */
1181 strncpy(rev, word, word_len);
1182 free(buf);
1183
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001184 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001185 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001186 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001187 LY_CHECK_RET(parse_ext(ctx, word, word_len, rev, LY_STMT_REVISION_DATE, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001188 break;
1189 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001190 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "revision-date");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001191 return LY_EVALID;
1192 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001193 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001194 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001195
1196cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001197 return ret;
1198}
1199
Michal Vaskoea5abea2018-09-18 13:10:54 +02001200/**
1201 * @brief Parse the include statement.
1202 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001203 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001204 * @param[in] module_name Name of the module to check name collisions.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001205 * @param[in,out] includes Parsed includes to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001206 * @return LY_ERR values.
1207 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001208static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001209parse_include(struct lysp_yang_ctx *ctx, const char *module_name, struct lysp_include **includes)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001210{
Radek Krejcid33273d2018-10-25 14:55:52 +02001211 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001212 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001213 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001214 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001215 struct lysp_include *inc;
1216
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001217 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *includes, inc, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001218
1219 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001220 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001221
Michal Vasko12ef5362022-09-16 15:13:58 +02001222 INSERT_WORD_GOTO(ctx, buf, inc->name, word, word_len, ret, cleanup);
Radek Krejci086c7132018-10-26 15:29:04 +02001223
Radek Krejcifaa1eac2018-10-30 14:34:55 +01001224 /* submodules share the namespace with the module names, so there must not be
1225 * a module of the same name in the context, no need for revision matching */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001226 if (!strcmp(module_name, inc->name) || ly_ctx_get_module_latest(PARSER_CTX(ctx), inc->name)) {
Radek Krejci854e1552020-12-21 15:05:23 +01001227 LOGVAL_PARSER(ctx, LY_VCODE_NAME2_COL, "module", "submodule", inc->name);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01001228 return LY_EVALID;
1229 }
1230
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001231 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001232 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001233 case LY_STMT_DESCRIPTION:
Radek Krejci335332a2019-09-05 13:03:35 +02001234 PARSER_CHECK_STMTVER2_RET(ctx, "description", "include");
Michal Vasko72c6d642024-02-27 14:59:01 +01001235 LY_CHECK_RET(parse_text_field(ctx, inc->dsc, LY_STMT_DESCRIPTION, 0, &inc->dsc, Y_STR_ARG, NULL, &inc->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001236 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001237 case LY_STMT_REFERENCE:
Radek Krejci335332a2019-09-05 13:03:35 +02001238 PARSER_CHECK_STMTVER2_RET(ctx, "reference", "include");
Michal Vasko72c6d642024-02-27 14:59:01 +01001239 LY_CHECK_RET(parse_text_field(ctx, inc->ref, LY_STMT_REFERENCE, 0, &inc->ref, Y_STR_ARG, NULL, &inc->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001240 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001241 case LY_STMT_REVISION_DATE:
Radek Krejci33090f92020-12-17 20:12:46 +01001242 LY_CHECK_RET(parse_revisiondate(ctx, inc->rev, &inc->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001243 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001244 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001245 LY_CHECK_RET(parse_ext(ctx, word, word_len, inc, LY_STMT_INCLUDE, 0, &inc->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001246 break;
1247 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001248 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "include");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001249 return LY_EVALID;
1250 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001251 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, inc->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001252 }
Michal Vasko12ef5362022-09-16 15:13:58 +02001253
1254cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001255 return ret;
1256}
1257
Michal Vaskoea5abea2018-09-18 13:10:54 +02001258/**
1259 * @brief Parse the import statement.
1260 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001261 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001262 * @param[in] module_prefix Prefix of the module to check prefix collisions.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001263 * @param[in,out] imports Parsed imports to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001264 * @return LY_ERR values.
1265 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001266static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001267parse_import(struct lysp_yang_ctx *ctx, const char *module_prefix, struct lysp_import **imports)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001268{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001269 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001270 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001271 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001272 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001273 struct lysp_import *imp;
1274
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001275 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *imports, imp, LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001276
1277 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001278 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02001279 INSERT_WORD_GOTO(ctx, buf, imp->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001280
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001281 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001282 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001283 case LY_STMT_PREFIX:
Michal Vasko72c6d642024-02-27 14:59:01 +01001284 LY_CHECK_RET(parse_text_field(ctx, imp->prefix, LY_STMT_PREFIX, 0, &imp->prefix, Y_IDENTIF_ARG, NULL, &imp->exts));
Michal Vaskod0625d72022-10-06 15:02:50 +02001285 LY_CHECK_RET(lysp_check_prefix((struct lysp_ctx *)ctx, *imports, module_prefix, &imp->prefix), LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001286 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001287 case LY_STMT_DESCRIPTION:
Radek Krejci335332a2019-09-05 13:03:35 +02001288 PARSER_CHECK_STMTVER2_RET(ctx, "description", "import");
Michal Vasko72c6d642024-02-27 14:59:01 +01001289 LY_CHECK_RET(parse_text_field(ctx, imp->dsc, LY_STMT_DESCRIPTION, 0, &imp->dsc, Y_STR_ARG, NULL, &imp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001290 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001291 case LY_STMT_REFERENCE:
Radek Krejci335332a2019-09-05 13:03:35 +02001292 PARSER_CHECK_STMTVER2_RET(ctx, "reference", "import");
Michal Vasko72c6d642024-02-27 14:59:01 +01001293 LY_CHECK_RET(parse_text_field(ctx, imp->ref, LY_STMT_REFERENCE, 0, &imp->ref, Y_STR_ARG, NULL, &imp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001294 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001295 case LY_STMT_REVISION_DATE:
Radek Krejci33090f92020-12-17 20:12:46 +01001296 LY_CHECK_RET(parse_revisiondate(ctx, imp->rev, &imp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001297 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001298 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001299 LY_CHECK_RET(parse_ext(ctx, word, word_len, imp, LY_STMT_IMPORT, 0, &imp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001300 break;
1301 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001302 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "import");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001303 return LY_EVALID;
1304 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001305 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, imp->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001306 }
Michal Vasko12ef5362022-09-16 15:13:58 +02001307
Michal Vasko7fbc8162018-09-17 10:35:16 +02001308 /* mandatory substatements */
David Sedlákb3077192019-06-19 10:55:37 +02001309 LY_CHECK_ERR_RET(!imp->prefix, LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "import"), LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001310
Michal Vasko12ef5362022-09-16 15:13:58 +02001311cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001312 return ret;
1313}
1314
Michal Vaskoea5abea2018-09-18 13:10:54 +02001315/**
1316 * @brief Parse the revision statement.
1317 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001318 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001319 * @param[in,out] revs Parsed revisions to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001320 * @return LY_ERR values.
1321 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001322static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001323parse_revision(struct lysp_yang_ctx *ctx, struct lysp_revision **revs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001324{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001325 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001326 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001327 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001328 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001329 struct lysp_revision *rev;
1330
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001331 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *revs, rev, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001332
1333 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001334 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001335
1336 /* check value */
Michal Vaskod0625d72022-10-06 15:02:50 +02001337 if (lysp_check_date((struct lysp_ctx *)ctx, word, word_len, "revision")) {
David Sedlák68ef3dc2019-07-22 13:40:19 +02001338 free(buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001339 return LY_EVALID;
1340 }
1341
Radek Krejcib7db73a2018-10-24 14:18:40 +02001342 strncpy(rev->date, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001343 free(buf);
1344
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001345 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001346 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001347 case LY_STMT_DESCRIPTION:
Michal Vasko72c6d642024-02-27 14:59:01 +01001348 LY_CHECK_RET(parse_text_field(ctx, rev->dsc, LY_STMT_DESCRIPTION, 0, &rev->dsc, Y_STR_ARG, NULL, &rev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001349 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001350 case LY_STMT_REFERENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01001351 LY_CHECK_RET(parse_text_field(ctx, rev->ref, LY_STMT_REFERENCE, 0, &rev->ref, Y_STR_ARG, NULL, &rev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001352 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001353 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001354 LY_CHECK_RET(parse_ext(ctx, word, word_len, rev, LY_STMT_REVISION, 0, &rev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001355 break;
1356 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001357 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "revision");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001358 return LY_EVALID;
1359 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001360 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, rev->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001361 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001362
1363cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001364 return ret;
1365}
1366
Michal Vaskoea5abea2018-09-18 13:10:54 +02001367/**
1368 * @brief Parse a generic text field that can have more instances such as base.
1369 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001370 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02001371 * @param[in] parent Current statement parent.
1372 * @param[in] parent_stmt Type of @p parent statement.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001373 * @param[in,out] texts Parsed values to add to.
1374 * @param[in] arg Type of the expected argument.
1375 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001376 * @return LY_ERR values.
1377 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001378static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02001379parse_text_fields(struct lysp_yang_ctx *ctx, enum ly_stmt parent_stmt, const char ***texts, enum yang_arg arg,
Radek Krejci0f969882020-08-21 16:56:47 +02001380 struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001381{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001382 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001383 char *buf, *word;
Radek Krejci151a5b72018-10-19 14:21:44 +02001384 const char **item;
1385 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001386 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001387
1388 /* allocate new pointer */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001389 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *texts, item, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001390
1391 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001392 LY_CHECK_RET(get_argument(ctx, arg, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001393
Michal Vasko12ef5362022-09-16 15:13:58 +02001394 INSERT_WORD_GOTO(ctx, buf, *item, word, word_len, ret, cleanup);
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001395 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001396 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001397 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001398 LY_CHECK_RET(parse_ext(ctx, word, word_len, *texts, parent_stmt, LY_ARRAY_COUNT(*texts) - 1, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001399 break;
1400 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001401 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), lyplg_ext_stmt2str(parent_stmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001402 return LY_EVALID;
1403 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001404 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001405 }
Michal Vasko12ef5362022-09-16 15:13:58 +02001406
1407cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001408 return ret;
1409}
1410
Michal Vaskoea5abea2018-09-18 13:10:54 +02001411/**
Michal Vasko7f45cf22020-10-01 12:49:44 +02001412 * @brief Parse a generic text field that can have more instances such as base.
1413 *
1414 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02001415 * @param[in] parent_stmt Type of statement stored in @p qnames.
Michal Vasko7f45cf22020-10-01 12:49:44 +02001416 * @param[in,out] qnames Parsed qnames to add to.
1417 * @param[in] arg Type of the expected argument.
1418 * @param[in,out] exts Extension instances to add to.
Michal Vasko7f45cf22020-10-01 12:49:44 +02001419 * @return LY_ERR values.
1420 */
1421static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02001422parse_qnames(struct lysp_yang_ctx *ctx, enum ly_stmt parent_stmt, struct lysp_qname **qnames, enum yang_arg arg,
1423 struct lysp_ext_instance **exts)
Michal Vasko7f45cf22020-10-01 12:49:44 +02001424{
1425 LY_ERR ret = LY_SUCCESS;
1426 char *buf, *word;
1427 struct lysp_qname *item;
1428 size_t word_len;
1429 enum ly_stmt kw;
1430
1431 /* allocate new pointer */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001432 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *qnames, item, LY_EMEM);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001433
1434 /* get value */
Michal Vasko72c6d642024-02-27 14:59:01 +01001435 LY_CHECK_RET(get_argument(ctx, arg, &item->flags, &word, &buf, &word_len));
Michal Vasko7f45cf22020-10-01 12:49:44 +02001436
Michal Vasko12ef5362022-09-16 15:13:58 +02001437 INSERT_WORD_GOTO(ctx, buf, item->str, word, word_len, ret, cleanup);
Michal Vasko8a67eff2021-12-07 14:04:47 +01001438 item->mod = PARSER_CUR_PMOD(ctx);
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001439 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7f45cf22020-10-01 12:49:44 +02001440 switch (kw) {
1441 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001442 LY_CHECK_RET(parse_ext(ctx, word, word_len, *qnames, parent_stmt, LY_ARRAY_COUNT(*qnames) - 1, exts));
Michal Vasko7f45cf22020-10-01 12:49:44 +02001443 break;
1444 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001445 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), lyplg_ext_stmt2str(parent_stmt));
Michal Vasko7f45cf22020-10-01 12:49:44 +02001446 return LY_EVALID;
1447 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001448 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001449 }
Michal Vasko12ef5362022-09-16 15:13:58 +02001450
1451cleanup:
Michal Vasko7f45cf22020-10-01 12:49:44 +02001452 return ret;
1453}
1454
1455/**
Michal Vaskoea5abea2018-09-18 13:10:54 +02001456 * @brief Parse the config statement.
1457 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001458 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001459 * @param[in,out] flags Flags to add to.
1460 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001461 * @return LY_ERR values.
1462 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001463static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001464parse_config(struct lysp_yang_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001465{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001466 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001467 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001468 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001469 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001470
1471 if (*flags & LYS_CONFIG_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02001472 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001473 return LY_EVALID;
1474 }
1475
1476 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001477 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001478
Radek Krejcif13b87b2020-12-01 22:02:17 +01001479 if ((word_len == ly_strlen_const("true")) && !strncmp(word, "true", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001480 *flags |= LYS_CONFIG_W;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001481 } else if ((word_len == ly_strlen_const("false")) && !strncmp(word, "false", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001482 *flags |= LYS_CONFIG_R;
1483 } else {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02001484 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001485 free(buf);
1486 return LY_EVALID;
1487 }
1488 free(buf);
1489
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001490 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001491 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001492 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001493 LY_CHECK_RET(parse_ext(ctx, word, word_len, flags, LY_STMT_CONFIG, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001494 break;
1495 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001496 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001497 return LY_EVALID;
1498 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001499 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001500 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001501
1502cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001503 return ret;
1504}
1505
Michal Vaskoea5abea2018-09-18 13:10:54 +02001506/**
1507 * @brief Parse the mandatory statement.
1508 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001509 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001510 * @param[in,out] flags Flags to add to.
1511 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001512 * @return LY_ERR values.
1513 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001514static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001515parse_mandatory(struct lysp_yang_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001516{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001517 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001518 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001519 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001520 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001521
1522 if (*flags & LYS_MAND_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02001523 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001524 return LY_EVALID;
1525 }
1526
1527 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001528 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001529
Radek Krejcif13b87b2020-12-01 22:02:17 +01001530 if ((word_len == ly_strlen_const("true")) && !strncmp(word, "true", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001531 *flags |= LYS_MAND_TRUE;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001532 } else if ((word_len == ly_strlen_const("false")) && !strncmp(word, "false", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001533 *flags |= LYS_MAND_FALSE;
1534 } else {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02001535 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001536 free(buf);
1537 return LY_EVALID;
1538 }
1539 free(buf);
1540
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001541 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001542 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001543 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001544 LY_CHECK_RET(parse_ext(ctx, word, word_len, flags, LY_STMT_MANDATORY, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001545 break;
1546 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001547 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001548 return LY_EVALID;
1549 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001550 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001551 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001552
1553cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001554 return ret;
1555}
1556
Michal Vaskoea5abea2018-09-18 13:10:54 +02001557/**
1558 * @brief Parse a restriction such as range or length.
1559 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001560 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001561 * @param[in] restr_kw Type of this particular restriction.
1562 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001563 * @return LY_ERR values.
1564 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001565static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001566parse_restr(struct lysp_yang_ctx *ctx, enum ly_stmt restr_kw, struct lysp_restr *restr)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001567{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001568 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001569 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001570 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001571 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001572
1573 /* get value */
Michal Vasko72c6d642024-02-27 14:59:01 +01001574 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, &restr->arg.flags, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001575
Michal Vasko193dacd2022-10-13 08:43:05 +02001576 CHECK_NONEMPTY(ctx, word_len, lyplg_ext_stmt2str(restr_kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02001577 INSERT_WORD_GOTO(ctx, buf, restr->arg.str, word, word_len, ret, cleanup);
Michal Vasko8a67eff2021-12-07 14:04:47 +01001578 restr->arg.mod = PARSER_CUR_PMOD(ctx);
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001579 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001580 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001581 case LY_STMT_DESCRIPTION:
Michal Vasko72c6d642024-02-27 14:59:01 +01001582 LY_CHECK_RET(parse_text_field(ctx, restr->dsc, LY_STMT_DESCRIPTION, 0, &restr->dsc, Y_STR_ARG, NULL, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001583 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001584 case LY_STMT_REFERENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01001585 LY_CHECK_RET(parse_text_field(ctx, restr->ref, LY_STMT_REFERENCE, 0, &restr->ref, Y_STR_ARG, NULL, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001586 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001587 case LY_STMT_ERROR_APP_TAG:
Michal Vasko72c6d642024-02-27 14:59:01 +01001588 LY_CHECK_RET(parse_text_field(ctx, restr, LY_STMT_ERROR_APP_TAG, 0, &restr->eapptag, Y_STR_ARG, NULL,
Michal Vasko193dacd2022-10-13 08:43:05 +02001589 &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001590 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001591 case LY_STMT_ERROR_MESSAGE:
Michal Vasko72c6d642024-02-27 14:59:01 +01001592 LY_CHECK_RET(parse_text_field(ctx, restr, LY_STMT_ERROR_MESSAGE, 0, &restr->emsg, Y_STR_ARG, NULL, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001593 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001594 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001595 LY_CHECK_RET(parse_ext(ctx, word, word_len, restr, restr_kw, 0, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001596 break;
1597 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001598 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), lyplg_ext_stmt2str(restr_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001599 return LY_EVALID;
1600 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001601 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, restr->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001602 }
Michal Vasko12ef5362022-09-16 15:13:58 +02001603
1604cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001605 return ret;
1606}
1607
Michal Vaskoea5abea2018-09-18 13:10:54 +02001608/**
1609 * @brief Parse a restriction that can have more instances such as must.
1610 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001611 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001612 * @param[in] restr_kw Type of this particular restriction.
1613 * @param[in,out] restrs Restrictions to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001614 * @return LY_ERR values.
1615 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001616static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001617parse_restrs(struct lysp_yang_ctx *ctx, enum ly_stmt restr_kw, struct lysp_restr **restrs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001618{
1619 struct lysp_restr *restr;
1620
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001621 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *restrs, restr, LY_EMEM);
Radek Krejci33090f92020-12-17 20:12:46 +01001622 return parse_restr(ctx, restr_kw, restr);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001623}
1624
Michal Vaskoea5abea2018-09-18 13:10:54 +02001625/**
1626 * @brief Parse the status statement.
1627 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001628 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001629 * @param[in,out] flags Flags to add to.
1630 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001631 * @return LY_ERR values.
1632 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001633static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001634parse_status(struct lysp_yang_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001635{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001636 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001637 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001638 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001639 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001640
1641 if (*flags & LYS_STATUS_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02001642 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001643 return LY_EVALID;
1644 }
1645
1646 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001647 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001648
Radek Krejcif13b87b2020-12-01 22:02:17 +01001649 if ((word_len == ly_strlen_const("current")) && !strncmp(word, "current", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001650 *flags |= LYS_STATUS_CURR;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001651 } else if ((word_len == ly_strlen_const("deprecated")) && !strncmp(word, "deprecated", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001652 *flags |= LYS_STATUS_DEPRC;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001653 } else if ((word_len == ly_strlen_const("obsolete")) && !strncmp(word, "obsolete", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001654 *flags |= LYS_STATUS_OBSLT;
1655 } else {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02001656 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001657 free(buf);
1658 return LY_EVALID;
1659 }
1660 free(buf);
1661
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001662 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001663 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001664 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001665 LY_CHECK_RET(parse_ext(ctx, word, word_len, flags, LY_STMT_STATUS, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001666 break;
1667 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001668 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001669 return LY_EVALID;
1670 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001671 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001672 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001673
1674cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001675 return ret;
1676}
1677
Michal Vaskoea5abea2018-09-18 13:10:54 +02001678/**
1679 * @brief Parse the when statement.
1680 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001681 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001682 * @param[in,out] when_p When pointer to parse to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001683 * @return LY_ERR values.
1684 */
Radek Krejcif09e4e82019-06-14 15:08:11 +02001685LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001686parse_when(struct lysp_yang_ctx *ctx, struct lysp_when **when_p)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001687{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001688 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001689 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001690 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001691 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001692 struct lysp_when *when;
Michal Vaskoc636ea42022-09-16 10:20:31 +02001693 struct lysf_ctx fctx = {.ctx = PARSER_CTX(ctx)};
Michal Vasko7fbc8162018-09-17 10:35:16 +02001694
1695 if (*when_p) {
David Sedlákb3077192019-06-19 10:55:37 +02001696 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "when");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001697 return LY_EVALID;
1698 }
1699
1700 when = calloc(1, sizeof *when);
Michal Vasko12ef5362022-09-16 15:13:58 +02001701 LY_CHECK_ERR_GOTO(!when, LOGMEM(PARSER_CTX(ctx)); ret = LY_EMEM, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001702
1703 /* get value */
Michal Vasko12ef5362022-09-16 15:13:58 +02001704 LY_CHECK_GOTO(ret = get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len), cleanup);
Michal Vaskob36053d2020-03-26 15:49:30 +01001705 CHECK_NONEMPTY(ctx, word_len, "when");
Michal Vasko12ef5362022-09-16 15:13:58 +02001706 INSERT_WORD_GOTO(ctx, buf, when->cond, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001707
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001708 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001709 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001710 case LY_STMT_DESCRIPTION:
Michal Vasko72c6d642024-02-27 14:59:01 +01001711 ret = parse_text_field(ctx, when->dsc, LY_STMT_DESCRIPTION, 0, &when->dsc, Y_STR_ARG, NULL, &when->exts);
1712 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001713 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001714 case LY_STMT_REFERENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01001715 ret = parse_text_field(ctx, when->ref, LY_STMT_REFERENCE, 0, &when->ref, Y_STR_ARG, NULL, &when->exts);
1716 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001717 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001718 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001719 LY_CHECK_GOTO(ret = parse_ext(ctx, word, word_len, *when_p, LY_STMT_WHEN, 0, &when->exts), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001720 break;
1721 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001722 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "when");
Michal Vasko12ef5362022-09-16 15:13:58 +02001723 ret = LY_EVALID;
1724 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001725 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001726 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, when->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001727 }
Michal Vasko12ef5362022-09-16 15:13:58 +02001728
1729cleanup:
1730 if (ret) {
Michal Vaskoc636ea42022-09-16 10:20:31 +02001731 lysp_when_free(&fctx, when);
Michal Vasko12ef5362022-09-16 15:13:58 +02001732 free(when);
1733 } else {
1734 *when_p = when;
1735 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001736 return ret;
1737}
1738
Michal Vaskoea5abea2018-09-18 13:10:54 +02001739/**
1740 * @brief Parse the anydata or anyxml statement.
1741 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001742 * @param[in] ctx yang parser context for logging.
Radek Krejci39b7fc22021-02-26 23:29:18 +01001743 * @param[in] any_kw Type of this particular keyword.
Michal Vasko193dacd2022-10-13 08:43:05 +02001744 * @param[in] parent Node parent.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001745 * @param[in,out] siblings Siblings to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001746 * @return LY_ERR values.
1747 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02001748LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001749parse_any(struct lysp_yang_ctx *ctx, enum ly_stmt any_kw, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001750{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001751 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001752 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001753 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001754 struct lysp_node_anydata *any;
Radek Krejci39b7fc22021-02-26 23:29:18 +01001755 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001756
David Sedlák60adc092019-08-06 15:57:02 +02001757 /* create new structure and insert into siblings */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001758 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, any, next, LY_EMEM);
David Sedlák60adc092019-08-06 15:57:02 +02001759
Radek Krejci39b7fc22021-02-26 23:29:18 +01001760 any->nodetype = any_kw == LY_STMT_ANYDATA ? LYS_ANYDATA : LYS_ANYXML;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001761 any->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001762
Michal Vasko7fbc8162018-09-17 10:35:16 +02001763 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01001764 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02001765 INSERT_WORD_GOTO(ctx, buf, any->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001766
1767 /* parse substatements */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001768 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001769 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001770 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01001771 LY_CHECK_RET(parse_config(ctx, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001772 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001773 case LY_STMT_DESCRIPTION:
Michal Vasko72c6d642024-02-27 14:59:01 +01001774 LY_CHECK_RET(parse_text_field(ctx, any->dsc, LY_STMT_DESCRIPTION, 0, &any->dsc, Y_STR_ARG, NULL, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001775 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001776 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001777 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &any->iffeatures, Y_STR_ARG, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001778 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001779 case LY_STMT_MANDATORY:
Radek Krejci33090f92020-12-17 20:12:46 +01001780 LY_CHECK_RET(parse_mandatory(ctx, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001781 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001782 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01001783 LY_CHECK_RET(parse_restrs(ctx, kw, &any->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001784 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001785 case LY_STMT_REFERENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01001786 LY_CHECK_RET(parse_text_field(ctx, any->ref, LY_STMT_REFERENCE, 0, &any->ref, Y_STR_ARG, NULL, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001787 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001788 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01001789 LY_CHECK_RET(parse_status(ctx, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001790 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001791 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01001792 LY_CHECK_RET(parse_when(ctx, &any->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001793 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001794 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001795 LY_CHECK_RET(parse_ext(ctx, word, word_len, any, any_kw, 0, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001796 break;
1797 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001798 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), lyplg_ext_stmt2str(any_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001799 return LY_EVALID;
1800 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001801 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, any->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001802 }
Michal Vasko12ef5362022-09-16 15:13:58 +02001803
1804cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001805 return ret;
1806}
1807
Michal Vaskoea5abea2018-09-18 13:10:54 +02001808/**
1809 * @brief Parse the value or position statement. Substatement of type enum statement.
1810 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001811 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001812 * @param[in] val_kw Type of this particular keyword.
Michal Vasko193dacd2022-10-13 08:43:05 +02001813 * @param[in,out] enm Structure to fill.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001814 * @return LY_ERR values.
1815 */
David Sedlákd6ce6d72019-07-16 17:30:18 +02001816LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02001817parse_type_enum_value_pos(struct lysp_yang_ctx *ctx, enum ly_stmt val_kw, struct lysp_type_enum *enm)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001818{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001819 LY_ERR ret = LY_SUCCESS;
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001820 char *buf = NULL, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02001821 size_t word_len;
Michal Vasko2bf4af42023-01-04 12:08:38 +01001822 long long num = 0;
1823 unsigned long long unum = 0;
Radek Krejcid6b76452019-09-03 17:03:03 +02001824 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001825
Michal Vasko193dacd2022-10-13 08:43:05 +02001826 if (enm->flags & LYS_SET_VALUE) {
1827 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, lyplg_ext_stmt2str(val_kw));
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001828 ret = LY_EVALID;
1829 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001830 }
Michal Vasko193dacd2022-10-13 08:43:05 +02001831 enm->flags |= LYS_SET_VALUE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001832
1833 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001834 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001835
Radek Krejcid6b76452019-09-03 17:03:03 +02001836 if (!word_len || (word[0] == '+') || ((word[0] == '0') && (word_len > 1)) || ((val_kw == LY_STMT_POSITION) && !strncmp(word, "-0", 2))) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02001837 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, lyplg_ext_stmt2str(val_kw));
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001838 ret = LY_EVALID;
1839 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001840 }
1841
1842 errno = 0;
Radek Krejcid6b76452019-09-03 17:03:03 +02001843 if (val_kw == LY_STMT_VALUE) {
Michal Vasko73d77ab2021-07-23 12:45:55 +02001844 num = strtoll(word, &ptr, LY_BASE_DEC);
Michal Vasko69730152020-10-09 16:30:07 +02001845 if ((num < INT64_C(-2147483648)) || (num > INT64_C(2147483647))) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02001846 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, lyplg_ext_stmt2str(val_kw));
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001847 ret = LY_EVALID;
1848 goto cleanup;
Radek Krejci8b764662018-11-14 14:15:13 +01001849 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001850 } else {
Michal Vasko73d77ab2021-07-23 12:45:55 +02001851 unum = strtoull(word, &ptr, LY_BASE_DEC);
Radek Krejci8b764662018-11-14 14:15:13 +01001852 if (unum > UINT64_C(4294967295)) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02001853 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, lyplg_ext_stmt2str(val_kw));
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001854 ret = LY_EVALID;
1855 goto cleanup;
Radek Krejci8b764662018-11-14 14:15:13 +01001856 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001857 }
1858 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02001859 if ((size_t)(ptr - word) != word_len) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02001860 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, lyplg_ext_stmt2str(val_kw));
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001861 ret = LY_EVALID;
1862 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001863 }
1864 if (errno == ERANGE) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02001865 LOGVAL_PARSER(ctx, LY_VCODE_OOB, (int)word_len, word, lyplg_ext_stmt2str(val_kw));
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001866 ret = LY_EVALID;
1867 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001868 }
Radek Krejcid6b76452019-09-03 17:03:03 +02001869 if (val_kw == LY_STMT_VALUE) {
Michal Vasko193dacd2022-10-13 08:43:05 +02001870 enm->value = num;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001871 } else {
Michal Vasko193dacd2022-10-13 08:43:05 +02001872 enm->value = unum;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001873 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001874
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001875 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001876 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001877 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001878 ret = parse_ext(ctx, word, word_len, enm, val_kw, 0, &enm->exts);
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001879 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001880 break;
1881 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001882 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), lyplg_ext_stmt2str(val_kw));
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001883 ret = LY_EVALID;
1884 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001885 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001886 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001887 }
Radek Krejci8b764662018-11-14 14:15:13 +01001888
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001889cleanup:
Radek Krejci8b764662018-11-14 14:15:13 +01001890 free(buf);
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001891 return ret;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001892}
1893
Michal Vaskoea5abea2018-09-18 13:10:54 +02001894/**
1895 * @brief Parse the enum or bit statement. Substatement of type statement.
1896 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001897 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001898 * @param[in] enum_kw Type of this particular keyword.
1899 * @param[in,out] enums Enums or bits to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001900 * @return LY_ERR values.
1901 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001902static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001903parse_type_enum(struct lysp_yang_ctx *ctx, enum ly_stmt enum_kw, struct lysp_type_enum **enums)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001904{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001905 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001906 char *buf, *word;
David Sedlák6544c182019-07-12 13:17:33 +02001907 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001908 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001909 struct lysp_type_enum *enm;
1910
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001911 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *enums, enm, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001912
1913 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001914 LY_CHECK_RET(get_argument(ctx, enum_kw == LY_STMT_ENUM ? Y_STR_ARG : Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Radek Krejcid6b76452019-09-03 17:03:03 +02001915 if (enum_kw == LY_STMT_ENUM) {
Michal Vaskod0625d72022-10-06 15:02:50 +02001916 ret = lysp_check_enum_name((struct lysp_ctx *)ctx, (const char *)word, word_len);
David Sedlák6544c182019-07-12 13:17:33 +02001917 LY_CHECK_ERR_RET(ret, free(buf), ret);
Radek Krejci335332a2019-09-05 13:03:35 +02001918 } /* else nothing specific for YANG_BIT */
Radek Krejci8b764662018-11-14 14:15:13 +01001919
Michal Vasko12ef5362022-09-16 15:13:58 +02001920 INSERT_WORD_GOTO(ctx, buf, enm->name, word, word_len, ret, cleanup);
Michal Vasko193dacd2022-10-13 08:43:05 +02001921 CHECK_UNIQUENESS(ctx, *enums, name, lyplg_ext_stmt2str(enum_kw), enm->name);
Radek Krejci8b764662018-11-14 14:15:13 +01001922
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001923 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001924 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001925 case LY_STMT_DESCRIPTION:
Michal Vasko72c6d642024-02-27 14:59:01 +01001926 LY_CHECK_RET(parse_text_field(ctx, enm->dsc, LY_STMT_DESCRIPTION, 0, &enm->dsc, Y_STR_ARG, NULL, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001927 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001928 case LY_STMT_IF_FEATURE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001929 PARSER_CHECK_STMTVER2_RET(ctx, "if-feature", lyplg_ext_stmt2str(enum_kw));
Radek Krejcifc596f92021-02-26 22:40:26 +01001930 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &enm->iffeatures, Y_STR_ARG, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001931 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001932 case LY_STMT_REFERENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01001933 LY_CHECK_RET(parse_text_field(ctx, enm->ref, LY_STMT_REFERENCE, 0, &enm->ref, Y_STR_ARG, NULL, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001934 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001935 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01001936 LY_CHECK_RET(parse_status(ctx, &enm->flags, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001937 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001938 case LY_STMT_VALUE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001939 LY_CHECK_ERR_RET(enum_kw == LY_STMT_BIT, LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw),
1940 lyplg_ext_stmt2str(enum_kw)), LY_EVALID);
1941 LY_CHECK_RET(parse_type_enum_value_pos(ctx, kw, enm));
David Sedlák9fb515f2019-07-11 10:33:58 +02001942 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001943 case LY_STMT_POSITION:
Michal Vasko193dacd2022-10-13 08:43:05 +02001944 LY_CHECK_ERR_RET(enum_kw == LY_STMT_ENUM, LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw),
1945 lyplg_ext_stmt2str(enum_kw)), LY_EVALID);
1946 LY_CHECK_RET(parse_type_enum_value_pos(ctx, kw, enm));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001947 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001948 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001949 LY_CHECK_RET(parse_ext(ctx, word, word_len, enm, enum_kw, 0, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001950 break;
1951 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001952 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), lyplg_ext_stmt2str(enum_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001953 return LY_EVALID;
1954 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001955 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, enm->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001956 }
Michal Vasko12ef5362022-09-16 15:13:58 +02001957
1958cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001959 return ret;
1960}
1961
Michal Vaskoea5abea2018-09-18 13:10:54 +02001962/**
1963 * @brief Parse the fraction-digits statement. Substatement of type statement.
1964 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001965 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02001966 * @param[in,out] type Type to fill.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001967 * @return LY_ERR values.
1968 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001969static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02001970parse_type_fracdigits(struct lysp_yang_ctx *ctx, struct lysp_type *type)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001971{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001972 LY_ERR ret = LY_SUCCESS;
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001973 char *buf = NULL, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02001974 size_t word_len;
Michal Vasko2bf4af42023-01-04 12:08:38 +01001975 unsigned long long num;
Radek Krejcid6b76452019-09-03 17:03:03 +02001976 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001977
Michal Vasko193dacd2022-10-13 08:43:05 +02001978 if (type->fraction_digits) {
David Sedlákb3077192019-06-19 10:55:37 +02001979 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001980 return LY_EVALID;
1981 }
1982
1983 /* get value */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001984 LY_CHECK_GOTO(ret = get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001985
1986 if (!word_len || (word[0] == '0') || !isdigit(word[0])) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02001987 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, "fraction-digits");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001988 ret = LY_EVALID;
1989 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001990 }
1991
1992 errno = 0;
Michal Vasko73d77ab2021-07-23 12:45:55 +02001993 num = strtoull(word, &ptr, LY_BASE_DEC);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001994 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02001995 if ((size_t)(ptr - word) != word_len) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02001996 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, "fraction-digits");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001997 ret = LY_EVALID;
1998 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001999 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01002000 if ((errno == ERANGE) || (num > LY_TYPE_DEC64_FD_MAX)) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02002001 LOGVAL_PARSER(ctx, LY_VCODE_OOB, (int)word_len, word, "fraction-digits");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002002 ret = LY_EVALID;
2003 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002004 }
Michal Vasko193dacd2022-10-13 08:43:05 +02002005 type->fraction_digits = num;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002006
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002007 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002008 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002009 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002010 LY_CHECK_GOTO(ret = parse_ext(ctx, word, word_len, type, LY_STMT_FRACTION_DIGITS, 0, &type->exts), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002011 break;
2012 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002013 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "fraction-digits");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002014 ret = LY_EVALID;
2015 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002016 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002017 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002018 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002019
2020cleanup:
2021 free(buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002022 return ret;
2023}
2024
Michal Vaskoea5abea2018-09-18 13:10:54 +02002025/**
2026 * @brief Parse the require-instance statement. Substatement of type statement.
2027 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002028 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02002029 * @param[in,out] type Type to fill.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002030 * @return LY_ERR values.
2031 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002032static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02002033parse_type_reqinstance(struct lysp_yang_ctx *ctx, struct lysp_type *type)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002034{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002035 LY_ERR ret = LY_SUCCESS;
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002036 char *buf = NULL, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002037 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002038 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002039
Michal Vasko193dacd2022-10-13 08:43:05 +02002040 if (type->flags & LYS_SET_REQINST) {
David Sedlákb3077192019-06-19 10:55:37 +02002041 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002042 return LY_EVALID;
2043 }
Michal Vasko193dacd2022-10-13 08:43:05 +02002044 type->flags |= LYS_SET_REQINST;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002045
2046 /* get value */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002047 LY_CHECK_GOTO(ret = get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002048
Radek Krejcif13b87b2020-12-01 22:02:17 +01002049 if ((word_len == ly_strlen_const("true")) && !strncmp(word, "true", word_len)) {
Michal Vasko193dacd2022-10-13 08:43:05 +02002050 type->require_instance = 1;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002051 } else if ((word_len != ly_strlen_const("false")) || strncmp(word, "false", word_len)) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02002052 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, "require-instance");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002053 ret = LY_EVALID;
2054 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002055 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002056
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002057 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002058 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002059 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002060 LY_CHECK_GOTO(ret = parse_ext(ctx, word, word_len, type, LY_STMT_REQUIRE_INSTANCE, 0, &type->exts), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002061 break;
2062 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002063 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "require-instance");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002064 ret = LY_EVALID;
2065 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002066 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002067 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002068 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002069
2070cleanup:
2071 free(buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002072 return ret;
2073}
2074
Michal Vaskoea5abea2018-09-18 13:10:54 +02002075/**
2076 * @brief Parse the modifier statement. Substatement of type pattern statement.
2077 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002078 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02002079 * @param[in,out] restr Restriction to fill.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002080 * @return LY_ERR values.
2081 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002082static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02002083parse_type_pattern_modifier(struct lysp_yang_ctx *ctx, struct lysp_restr *restr)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002084{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002085 LY_ERR ret = LY_SUCCESS;
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002086 char *buf = NULL, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002087 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002088 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002089
Michal Vasko193dacd2022-10-13 08:43:05 +02002090 if (restr->arg.str[0] == LYSP_RESTR_PATTERN_NACK) {
David Sedlákb3077192019-06-19 10:55:37 +02002091 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002092 return LY_EVALID;
2093 }
2094
2095 /* get value */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002096 LY_CHECK_GOTO(ret = get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002097
Radek Krejcif13b87b2020-12-01 22:02:17 +01002098 if ((word_len != ly_strlen_const("invert-match")) || strncmp(word, "invert-match", word_len)) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02002099 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, "modifier");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002100 ret = LY_EVALID;
2101 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002102 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002103
2104 /* replace the value in the dictionary */
Michal Vasko193dacd2022-10-13 08:43:05 +02002105 buf = malloc(strlen(restr->arg.str) + 1);
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002106 LY_CHECK_ERR_GOTO(!buf, LOGMEM(PARSER_CTX(ctx)); ret = LY_EMEM, cleanup);
Michal Vasko193dacd2022-10-13 08:43:05 +02002107 strcpy(buf, restr->arg.str);
2108 lydict_remove(PARSER_CTX(ctx), restr->arg.str);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002109
Radek Krejcif13b87b2020-12-01 22:02:17 +01002110 assert(buf[0] == LYSP_RESTR_PATTERN_ACK);
2111 buf[0] = LYSP_RESTR_PATTERN_NACK;
Michal Vasko183ea332022-11-08 10:50:03 +01002112 ret = lydict_insert_zc(PARSER_CTX(ctx), buf, &restr->arg.str);
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002113 buf = NULL;
Michal Vasko183ea332022-11-08 10:50:03 +01002114 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002115
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002116 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002117 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002118 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002119 LY_CHECK_GOTO(ret = parse_ext(ctx, word, word_len, restr, LY_STMT_MODIFIER, 0, &restr->exts), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002120 break;
2121 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002122 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "modifier");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002123 ret = LY_EVALID;
2124 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002125 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002126 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002127 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002128
2129cleanup:
2130 free(buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002131 return ret;
2132}
2133
Michal Vaskoea5abea2018-09-18 13:10:54 +02002134/**
2135 * @brief Parse the pattern statement. Substatement of type statement.
2136 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002137 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002138 * @param[in,out] patterns Restrictions to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002139 * @return LY_ERR values.
2140 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002141static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002142parse_type_pattern(struct lysp_yang_ctx *ctx, struct lysp_restr **patterns)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002143{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002144 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002145 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002146 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002147 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002148 struct lysp_restr *restr;
2149
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002150 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *patterns, restr, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002151
2152 /* get value */
Michal Vasko72c6d642024-02-27 14:59:01 +01002153 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, &restr->arg.flags, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002154
2155 /* add special meaning first byte */
2156 if (buf) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01002157 buf = ly_realloc(buf, word_len + 2);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002158 word = buf;
2159 } else {
2160 buf = malloc(word_len + 2);
2161 }
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002162 LY_CHECK_ERR_RET(!buf, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko54720192021-06-11 13:55:59 +02002163 if (word_len) {
2164 memmove(buf + 1, word, word_len);
2165 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01002166 buf[0] = LYSP_RESTR_PATTERN_ACK; /* pattern's default regular-match flag */
Radek Krejci86d106e2018-10-18 09:53:19 +02002167 buf[word_len + 1] = '\0'; /* terminating NULL byte */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002168 LY_CHECK_RET(lydict_insert_zc(PARSER_CTX(ctx), buf, &restr->arg.str));
Michal Vasko8a67eff2021-12-07 14:04:47 +01002169 restr->arg.mod = PARSER_CUR_PMOD(ctx);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002170
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002171 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002172 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002173 case LY_STMT_DESCRIPTION:
Michal Vasko72c6d642024-02-27 14:59:01 +01002174 LY_CHECK_RET(parse_text_field(ctx, restr->dsc, LY_STMT_DESCRIPTION, 0, &restr->dsc, Y_STR_ARG, NULL, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002175 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002176 case LY_STMT_REFERENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01002177 LY_CHECK_RET(parse_text_field(ctx, restr->ref, LY_STMT_REFERENCE, 0, &restr->ref, Y_STR_ARG, NULL, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002178 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002179 case LY_STMT_ERROR_APP_TAG:
Michal Vasko72c6d642024-02-27 14:59:01 +01002180 LY_CHECK_RET(parse_text_field(ctx, restr, LY_STMT_ERROR_APP_TAG, 0, &restr->eapptag, Y_STR_ARG, NULL, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002181 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002182 case LY_STMT_ERROR_MESSAGE:
Michal Vasko72c6d642024-02-27 14:59:01 +01002183 LY_CHECK_RET(parse_text_field(ctx, restr, LY_STMT_ERROR_MESSAGE, 0, &restr->emsg, Y_STR_ARG, NULL, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002184 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002185 case LY_STMT_MODIFIER:
Radek Krejci335332a2019-09-05 13:03:35 +02002186 PARSER_CHECK_STMTVER2_RET(ctx, "modifier", "pattern");
Michal Vasko193dacd2022-10-13 08:43:05 +02002187 LY_CHECK_RET(parse_type_pattern_modifier(ctx, restr));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002188 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002189 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002190 LY_CHECK_RET(parse_ext(ctx, word, word_len, restr, LY_STMT_PATTERN, 0, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002191 break;
2192 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002193 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "pattern");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002194 return LY_EVALID;
2195 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002196 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, restr->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002197 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002198
2199cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002200 return ret;
2201}
2202
Michal Vaskoea5abea2018-09-18 13:10:54 +02002203/**
2204 * @brief Parse the type statement.
2205 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002206 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002207 * @param[in,out] type Type to wrote to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002208 * @return LY_ERR values.
2209 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002210static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002211parse_type(struct lysp_yang_ctx *ctx, struct lysp_type *type)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002212{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002213 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002214 char *buf, *word;
Michal Vasko004d3152020-06-11 19:59:22 +02002215 const char *str_path = NULL;
Radek Krejciefd22f62018-09-27 11:47:58 +02002216 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002217 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002218 struct lysp_type *nest_type;
2219
2220 if (type->name) {
David Sedlákb3077192019-06-19 10:55:37 +02002221 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "type");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002222 return LY_EVALID;
2223 }
2224
2225 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002226 LY_CHECK_RET(get_argument(ctx, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02002227 INSERT_WORD_GOTO(ctx, buf, type->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002228
Michal Vaskoe9c050f2020-10-06 14:01:23 +02002229 /* set module */
Michal Vasko8a67eff2021-12-07 14:04:47 +01002230 type->pmod = PARSER_CUR_PMOD(ctx);
Michal Vaskoe9c050f2020-10-06 14:01:23 +02002231
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002232 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002233 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002234 case LY_STMT_BASE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002235 LY_CHECK_RET(parse_text_fields(ctx, LY_STMT_BASE, &type->bases, Y_PREF_IDENTIF_ARG, &type->exts));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002236 type->flags |= LYS_SET_BASE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002237 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002238 case LY_STMT_BIT:
Radek Krejci33090f92020-12-17 20:12:46 +01002239 LY_CHECK_RET(parse_type_enum(ctx, kw, &type->bits));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002240 type->flags |= LYS_SET_BIT;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002241 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002242 case LY_STMT_ENUM:
Radek Krejci33090f92020-12-17 20:12:46 +01002243 LY_CHECK_RET(parse_type_enum(ctx, kw, &type->enums));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002244 type->flags |= LYS_SET_ENUM;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002245 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002246 case LY_STMT_FRACTION_DIGITS:
Michal Vasko193dacd2022-10-13 08:43:05 +02002247 LY_CHECK_RET(parse_type_fracdigits(ctx, type));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002248 type->flags |= LYS_SET_FRDIGITS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002249 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002250 case LY_STMT_LENGTH:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002251 if (type->length) {
Michal Vasko193dacd2022-10-13 08:43:05 +02002252 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, lyplg_ext_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002253 return LY_EVALID;
2254 }
2255 type->length = calloc(1, sizeof *type->length);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002256 LY_CHECK_ERR_RET(!type->length, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002257
Radek Krejci33090f92020-12-17 20:12:46 +01002258 LY_CHECK_RET(parse_restr(ctx, kw, type->length));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002259 type->flags |= LYS_SET_LENGTH;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002260 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002261 case LY_STMT_PATH:
Michal Vasko004d3152020-06-11 19:59:22 +02002262 if (type->path) {
Michal Vasko193dacd2022-10-13 08:43:05 +02002263 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, lyplg_ext_stmt2str(LY_STMT_PATH));
Michal Vasko004d3152020-06-11 19:59:22 +02002264 return LY_EVALID;
2265 }
2266
aPiecek4bb1e372021-05-07 11:01:00 +02002267 /* Usually, in the parser_yang.c, the result of the parsing is stored directly in the
2268 * corresponding structure, so in case of failure, the lysp_module_free function will take
2269 * care of removing the parsed value from the dictionary. But in this case, it is not possible
2270 * to rely on lysp_module_free because the result of the parsing is stored in a local variable.
2271 */
Michal Vasko72c6d642024-02-27 14:59:01 +01002272 LY_CHECK_ERR_RET(ret = parse_text_field(ctx, type, LY_STMT_PATH, 0, &str_path, Y_STR_ARG, NULL, &type->exts),
aPiecek4bb1e372021-05-07 11:01:00 +02002273 lydict_remove(PARSER_CTX(ctx), str_path), ret);
Michal Vaskoed725d72021-06-23 12:03:45 +02002274 ret = ly_path_parse(PARSER_CTX(ctx), NULL, str_path, 0, 1, LY_PATH_BEGIN_EITHER,
Michal Vasko69730152020-10-09 16:30:07 +02002275 LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_LEAFREF, &type->path);
aPiecek4bb1e372021-05-07 11:01:00 +02002276 /* Moreover, even if successful, the string is removed from the dictionary. */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002277 lydict_remove(PARSER_CTX(ctx), str_path);
Michal Vasko004d3152020-06-11 19:59:22 +02002278 LY_CHECK_RET(ret);
Radek Krejcid505e3d2018-11-13 09:04:17 +01002279 type->flags |= LYS_SET_PATH;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002280 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002281 case LY_STMT_PATTERN:
Radek Krejci33090f92020-12-17 20:12:46 +01002282 LY_CHECK_RET(parse_type_pattern(ctx, &type->patterns));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002283 type->flags |= LYS_SET_PATTERN;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002284 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002285 case LY_STMT_RANGE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002286 if (type->range) {
Michal Vasko193dacd2022-10-13 08:43:05 +02002287 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, lyplg_ext_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002288 return LY_EVALID;
2289 }
2290 type->range = calloc(1, sizeof *type->range);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002291 LY_CHECK_ERR_RET(!type->range, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002292
Radek Krejci33090f92020-12-17 20:12:46 +01002293 LY_CHECK_RET(parse_restr(ctx, kw, type->range));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002294 type->flags |= LYS_SET_RANGE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002295 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002296 case LY_STMT_REQUIRE_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002297 LY_CHECK_RET(parse_type_reqinstance(ctx, type));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002298 /* LYS_SET_REQINST checked and set inside parse_type_reqinstance() */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002299 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002300 case LY_STMT_TYPE:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002301 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), type->types, nest_type, LY_EMEM);
Radek Krejci33090f92020-12-17 20:12:46 +01002302 LY_CHECK_RET(parse_type(ctx, nest_type));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002303 type->flags |= LYS_SET_TYPE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002304 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002305 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002306 LY_CHECK_RET(parse_ext(ctx, word, word_len, type, LY_STMT_TYPE, 0, &type->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002307 break;
2308 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002309 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "type");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002310 return LY_EVALID;
2311 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002312 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, type->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002313 }
Michal Vasko12ef5362022-09-16 15:13:58 +02002314
2315cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002316 return ret;
2317}
2318
Michal Vaskoea5abea2018-09-18 13:10:54 +02002319/**
2320 * @brief Parse the leaf statement.
2321 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002322 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002323 * @param[in,out] siblings Siblings to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002324 * @return LY_ERR values.
2325 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002326LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002327parse_leaf(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002328{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002329 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002330 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002331 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002332 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002333 struct lysp_node_leaf *leaf;
2334
David Sedlák60adc092019-08-06 15:57:02 +02002335 /* create new leaf structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002336 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, leaf, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002337 leaf->nodetype = LYS_LEAF;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002338 leaf->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002339
Michal Vasko7fbc8162018-09-17 10:35:16 +02002340 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01002341 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02002342 INSERT_WORD_GOTO(ctx, buf, leaf->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002343
2344 /* parse substatements */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002345 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002346 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002347 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01002348 LY_CHECK_RET(parse_config(ctx, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002349 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002350 case LY_STMT_DEFAULT:
Michal Vasko72c6d642024-02-27 14:59:01 +01002351 LY_CHECK_RET(parse_text_field(ctx, &leaf->dflt, LY_STMT_DEFAULT, 0, &leaf->dflt.str, Y_STR_ARG,
2352 &leaf->dflt.flags, &leaf->exts));
Michal Vasko8a67eff2021-12-07 14:04:47 +01002353 leaf->dflt.mod = PARSER_CUR_PMOD(ctx);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002354 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002355 case LY_STMT_DESCRIPTION:
Michal Vasko72c6d642024-02-27 14:59:01 +01002356 LY_CHECK_RET(parse_text_field(ctx, leaf->dsc, LY_STMT_DESCRIPTION, 0, &leaf->dsc, Y_STR_ARG, NULL, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002357 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002358 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002359 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &leaf->iffeatures, Y_STR_ARG, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002360 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002361 case LY_STMT_MANDATORY:
Radek Krejci33090f92020-12-17 20:12:46 +01002362 LY_CHECK_RET(parse_mandatory(ctx, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002363 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002364 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01002365 LY_CHECK_RET(parse_restrs(ctx, kw, &leaf->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002366 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002367 case LY_STMT_REFERENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01002368 LY_CHECK_RET(parse_text_field(ctx, leaf->ref, LY_STMT_REFERENCE, 0, &leaf->ref, Y_STR_ARG, NULL, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002369 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002370 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002371 LY_CHECK_RET(parse_status(ctx, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002372 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002373 case LY_STMT_TYPE:
Radek Krejci33090f92020-12-17 20:12:46 +01002374 LY_CHECK_RET(parse_type(ctx, &leaf->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002375 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002376 case LY_STMT_UNITS:
Michal Vasko72c6d642024-02-27 14:59:01 +01002377 LY_CHECK_RET(parse_text_field(ctx, leaf->units, LY_STMT_UNITS, 0, &leaf->units, Y_STR_ARG, NULL, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002378 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002379 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01002380 LY_CHECK_RET(parse_when(ctx, &leaf->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002381 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002382 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002383 LY_CHECK_RET(parse_ext(ctx, word, word_len, leaf, LY_STMT_LEAF, 0, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002384 break;
2385 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002386 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "leaf");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002387 return LY_EVALID;
2388 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002389 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, leaf->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002390 }
Michal Vasko12ef5362022-09-16 15:13:58 +02002391
Michal Vasko7fbc8162018-09-17 10:35:16 +02002392 /* mandatory substatements */
2393 if (!leaf->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002394 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "leaf");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002395 return LY_EVALID;
2396 }
2397
Michal Vasko12ef5362022-09-16 15:13:58 +02002398cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002399 return ret;
2400}
2401
Michal Vaskoea5abea2018-09-18 13:10:54 +02002402/**
2403 * @brief Parse the max-elements statement.
2404 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002405 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002406 * @param[in,out] max Value to write to.
2407 * @param[in,out] flags Flags to write to.
2408 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002409 * @return LY_ERR values.
2410 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002411LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002412parse_maxelements(struct lysp_yang_ctx *ctx, uint32_t *max, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002413{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002414 LY_ERR ret = LY_SUCCESS;
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002415 char *buf = NULL, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02002416 size_t word_len;
Michal Vasko2bf4af42023-01-04 12:08:38 +01002417 unsigned long long num;
Radek Krejcid6b76452019-09-03 17:03:03 +02002418 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002419
2420 if (*flags & LYS_SET_MAX) {
David Sedlákb3077192019-06-19 10:55:37 +02002421 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002422 return LY_EVALID;
2423 }
2424 *flags |= LYS_SET_MAX;
2425
2426 /* get value */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002427 LY_CHECK_GOTO(ret = get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002428
2429 if (!word_len || (word[0] == '0') || ((word[0] != 'u') && !isdigit(word[0]))) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02002430 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, "max-elements");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002431 ret = LY_EVALID;
2432 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002433 }
2434
Radek Krejci7f9b6512019-09-18 13:11:09 +02002435 if (ly_strncmp("unbounded", word, word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002436 errno = 0;
Michal Vasko73d77ab2021-07-23 12:45:55 +02002437 num = strtoull(word, &ptr, LY_BASE_DEC);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002438 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02002439 if ((size_t)(ptr - word) != word_len) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02002440 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, "max-elements");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002441 ret = LY_EVALID;
2442 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002443 }
2444 if ((errno == ERANGE) || (num > UINT32_MAX)) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02002445 LOGVAL_PARSER(ctx, LY_VCODE_OOB, (int)word_len, word, "max-elements");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002446 ret = LY_EVALID;
2447 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002448 }
2449
2450 *max = num;
Radek Krejcid6315102021-02-02 15:26:34 +01002451 } else {
2452 /* unbounded */
2453 *max = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002454 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002455
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002456 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002457 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002458 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002459 LY_CHECK_GOTO(ret = parse_ext(ctx, word, word_len, max, LY_STMT_MAX_ELEMENTS, 0, exts), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002460 break;
2461 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002462 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "max-elements");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002463 ret = LY_EVALID;
2464 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002465 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002466 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002467 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002468
2469cleanup:
2470 free(buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002471 return ret;
2472}
2473
Michal Vaskoea5abea2018-09-18 13:10:54 +02002474/**
2475 * @brief Parse the min-elements statement.
2476 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002477 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002478 * @param[in,out] min Value to write to.
2479 * @param[in,out] flags Flags to write to.
2480 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002481 * @return LY_ERR values.
2482 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002483LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002484parse_minelements(struct lysp_yang_ctx *ctx, uint32_t *min, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002485{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002486 LY_ERR ret = LY_SUCCESS;
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002487 char *buf = NULL, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02002488 size_t word_len;
Michal Vasko2bf4af42023-01-04 12:08:38 +01002489 unsigned long long num;
Radek Krejcid6b76452019-09-03 17:03:03 +02002490 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002491
2492 if (*flags & LYS_SET_MIN) {
David Sedlákb3077192019-06-19 10:55:37 +02002493 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002494 return LY_EVALID;
2495 }
2496 *flags |= LYS_SET_MIN;
2497
2498 /* get value */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002499 LY_CHECK_GOTO(ret = get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002500
2501 if (!word_len || !isdigit(word[0]) || ((word[0] == '0') && (word_len > 1))) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02002502 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, "min-elements");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002503 ret = LY_EVALID;
2504 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002505 }
2506
2507 errno = 0;
Michal Vasko73d77ab2021-07-23 12:45:55 +02002508 num = strtoull(word, &ptr, LY_BASE_DEC);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002509 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02002510 if ((size_t)(ptr - word) != word_len) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02002511 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, "min-elements");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002512 ret = LY_EVALID;
2513 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002514 }
2515 if ((errno == ERANGE) || (num > UINT32_MAX)) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02002516 LOGVAL_PARSER(ctx, LY_VCODE_OOB, (int)word_len, word, "min-elements");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002517 ret = LY_EVALID;
2518 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002519 }
2520 *min = num;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002521
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002522 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002523 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002524 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002525 LY_CHECK_GOTO(ret = parse_ext(ctx, word, word_len, min, LY_STMT_MIN_ELEMENTS, 0, exts), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002526 break;
2527 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002528 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "min-elements");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002529 ret = LY_EVALID;
2530 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002531 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002532 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002533 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002534
2535cleanup:
2536 free(buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002537 return ret;
2538}
2539
Michal Vaskoea5abea2018-09-18 13:10:54 +02002540/**
2541 * @brief Parse the ordered-by statement.
2542 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002543 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02002544 * @param[in,out] llist List or leaf-list to fill.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002545 * @return LY_ERR values.
2546 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002547static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02002548parse_orderedby(struct lysp_yang_ctx *ctx, struct lysp_node *llist)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002549{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002550 LY_ERR ret = LY_SUCCESS;
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002551 char *buf = NULL, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002552 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002553 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002554
Michal Vasko193dacd2022-10-13 08:43:05 +02002555 if (llist->flags & LYS_ORDBY_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02002556 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002557 return LY_EVALID;
2558 }
2559
2560 /* get value */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002561 LY_CHECK_GOTO(ret = get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002562
Radek Krejcif13b87b2020-12-01 22:02:17 +01002563 if ((word_len == ly_strlen_const("system")) && !strncmp(word, "system", word_len)) {
Michal Vasko193dacd2022-10-13 08:43:05 +02002564 llist->flags |= LYS_ORDBY_SYSTEM;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002565 } else if ((word_len == ly_strlen_const("user")) && !strncmp(word, "user", word_len)) {
Michal Vasko193dacd2022-10-13 08:43:05 +02002566 llist->flags |= LYS_ORDBY_USER;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002567 } else {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02002568 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, "ordered-by");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002569 ret = LY_EVALID;
2570 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002571 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002572
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002573 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002574 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002575 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002576 LY_CHECK_GOTO(ret = parse_ext(ctx, word, word_len, llist, LY_STMT_ORDERED_BY, 0, &llist->exts), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002577 break;
2578 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002579 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "ordered-by");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002580 ret = LY_EVALID;
2581 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002582 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002583 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002584 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002585
2586cleanup:
2587 free(buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002588 return ret;
2589}
2590
Michal Vaskoea5abea2018-09-18 13:10:54 +02002591/**
2592 * @brief Parse the leaf-list statement.
2593 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002594 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002595 * @param[in,out] siblings Siblings to add to.
2596 *
2597 * @return LY_ERR values.
2598 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002599LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002600parse_leaflist(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002601{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002602 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002603 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002604 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002605 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002606 struct lysp_node_leaflist *llist;
2607
David Sedlák60adc092019-08-06 15:57:02 +02002608 /* create new leaf-list structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002609 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, llist, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002610 llist->nodetype = LYS_LEAFLIST;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002611 llist->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002612
Michal Vasko7fbc8162018-09-17 10:35:16 +02002613 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01002614 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02002615 INSERT_WORD_GOTO(ctx, buf, llist->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002616
2617 /* parse substatements */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002618 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002619 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002620 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01002621 LY_CHECK_RET(parse_config(ctx, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002622 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002623 case LY_STMT_DEFAULT:
Radek Krejci335332a2019-09-05 13:03:35 +02002624 PARSER_CHECK_STMTVER2_RET(ctx, "default", "leaf-list");
Radek Krejcifc596f92021-02-26 22:40:26 +01002625 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_DEFAULT, &llist->dflts, Y_STR_ARG, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002626 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002627 case LY_STMT_DESCRIPTION:
Michal Vasko72c6d642024-02-27 14:59:01 +01002628 LY_CHECK_RET(parse_text_field(ctx, llist->dsc, LY_STMT_DESCRIPTION, 0, &llist->dsc, Y_STR_ARG, NULL, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002629 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002630 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002631 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &llist->iffeatures, Y_STR_ARG, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002632 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002633 case LY_STMT_MAX_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01002634 LY_CHECK_RET(parse_maxelements(ctx, &llist->max, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002635 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002636 case LY_STMT_MIN_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01002637 LY_CHECK_RET(parse_minelements(ctx, &llist->min, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002638 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002639 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01002640 LY_CHECK_RET(parse_restrs(ctx, kw, &llist->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002641 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002642 case LY_STMT_ORDERED_BY:
Michal Vasko193dacd2022-10-13 08:43:05 +02002643 LY_CHECK_RET(parse_orderedby(ctx, &llist->node));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002644 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002645 case LY_STMT_REFERENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01002646 LY_CHECK_RET(parse_text_field(ctx, llist->ref, LY_STMT_REFERENCE, 0, &llist->ref, Y_STR_ARG, NULL, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002647 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002648 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002649 LY_CHECK_RET(parse_status(ctx, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002650 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002651 case LY_STMT_TYPE:
Radek Krejci33090f92020-12-17 20:12:46 +01002652 LY_CHECK_RET(parse_type(ctx, &llist->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002653 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002654 case LY_STMT_UNITS:
Michal Vasko72c6d642024-02-27 14:59:01 +01002655 LY_CHECK_RET(parse_text_field(ctx, llist->units, LY_STMT_UNITS, 0, &llist->units, Y_STR_ARG, NULL, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002656 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002657 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01002658 LY_CHECK_RET(parse_when(ctx, &llist->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002659 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002660 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002661 LY_CHECK_RET(parse_ext(ctx, word, word_len, llist, LY_STMT_LEAF_LIST, 0, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002662 break;
2663 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002664 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "llist");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002665 return LY_EVALID;
2666 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002667 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, llist->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002668 }
Michal Vasko12ef5362022-09-16 15:13:58 +02002669
Michal Vasko7fbc8162018-09-17 10:35:16 +02002670 /* mandatory substatements */
2671 if (!llist->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002672 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "leaf-list");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002673 return LY_EVALID;
2674 }
2675
Michal Vasko12ef5362022-09-16 15:13:58 +02002676cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002677 return ret;
2678}
2679
Michal Vaskoea5abea2018-09-18 13:10:54 +02002680/**
2681 * @brief Parse the refine statement.
2682 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002683 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002684 * @param[in,out] refines Refines to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002685 * @return LY_ERR values.
2686 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002687static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002688parse_refine(struct lysp_yang_ctx *ctx, struct lysp_refine **refines)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002689{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002690 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002691 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002692 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002693 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002694 struct lysp_refine *rf;
2695
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002696 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *refines, rf, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002697
2698 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002699 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vaskob36053d2020-03-26 15:49:30 +01002700 CHECK_NONEMPTY(ctx, word_len, "refine");
Michal Vasko12ef5362022-09-16 15:13:58 +02002701 INSERT_WORD_GOTO(ctx, buf, rf->nodeid, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002702
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002703 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002704 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002705 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01002706 LY_CHECK_RET(parse_config(ctx, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002707 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002708 case LY_STMT_DEFAULT:
Radek Krejcifc596f92021-02-26 22:40:26 +01002709 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_DEFAULT, &rf->dflts, Y_STR_ARG, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002710 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002711 case LY_STMT_DESCRIPTION:
Michal Vasko72c6d642024-02-27 14:59:01 +01002712 LY_CHECK_RET(parse_text_field(ctx, rf->dsc, LY_STMT_DESCRIPTION, 0, &rf->dsc, Y_STR_ARG, NULL, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002713 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002714 case LY_STMT_IF_FEATURE:
Radek Krejci335332a2019-09-05 13:03:35 +02002715 PARSER_CHECK_STMTVER2_RET(ctx, "if-feature", "refine");
Radek Krejcifc596f92021-02-26 22:40:26 +01002716 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &rf->iffeatures, Y_STR_ARG, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002717 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002718 case LY_STMT_MAX_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01002719 LY_CHECK_RET(parse_maxelements(ctx, &rf->max, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002720 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002721 case LY_STMT_MIN_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01002722 LY_CHECK_RET(parse_minelements(ctx, &rf->min, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002723 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002724 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01002725 LY_CHECK_RET(parse_restrs(ctx, kw, &rf->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002726 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002727 case LY_STMT_MANDATORY:
Radek Krejci33090f92020-12-17 20:12:46 +01002728 LY_CHECK_RET(parse_mandatory(ctx, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002729 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002730 case LY_STMT_REFERENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01002731 LY_CHECK_RET(parse_text_field(ctx, rf->ref, LY_STMT_REFERENCE, 0, &rf->ref, Y_STR_ARG, NULL, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002732 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002733 case LY_STMT_PRESENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01002734 LY_CHECK_RET(parse_text_field(ctx, rf->presence, LY_STMT_PRESENCE, 0, &rf->presence, Y_STR_ARG, NULL, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002735 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002736 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002737 LY_CHECK_RET(parse_ext(ctx, word, word_len, rf, LY_STMT_REFINE, 0, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002738 break;
2739 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002740 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "refine");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002741 return LY_EVALID;
2742 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002743 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, rf->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002744 }
Michal Vasko12ef5362022-09-16 15:13:58 +02002745
2746cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002747 return ret;
2748}
2749
Michal Vaskoea5abea2018-09-18 13:10:54 +02002750/**
2751 * @brief Parse the typedef statement.
2752 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002753 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002754 * @param[in,out] typedefs Typedefs to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002755 * @return LY_ERR values.
2756 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002757static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002758parse_typedef(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_tpdf **typedefs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002759{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002760 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002761 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002762 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002763 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002764 struct lysp_tpdf *tpdf;
2765
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002766 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *typedefs, tpdf, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002767
2768 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002769 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02002770 INSERT_WORD_GOTO(ctx, buf, tpdf->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002771
2772 /* parse substatements */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002773 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002774 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002775 case LY_STMT_DEFAULT:
Michal Vasko72c6d642024-02-27 14:59:01 +01002776 LY_CHECK_RET(parse_text_field(ctx, &tpdf->dflt, LY_STMT_DEFAULT, 0, &tpdf->dflt.str, Y_STR_ARG,
2777 &tpdf->dflt.flags, &tpdf->exts));
Michal Vasko8a67eff2021-12-07 14:04:47 +01002778 tpdf->dflt.mod = PARSER_CUR_PMOD(ctx);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002779 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002780 case LY_STMT_DESCRIPTION:
Michal Vasko72c6d642024-02-27 14:59:01 +01002781 LY_CHECK_RET(parse_text_field(ctx, tpdf->dsc, LY_STMT_DESCRIPTION, 0, &tpdf->dsc, Y_STR_ARG, NULL, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002782 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002783 case LY_STMT_REFERENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01002784 LY_CHECK_RET(parse_text_field(ctx, tpdf->ref, LY_STMT_REFERENCE, 0, &tpdf->ref, Y_STR_ARG, NULL, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002785 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002786 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002787 LY_CHECK_RET(parse_status(ctx, &tpdf->flags, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002788 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002789 case LY_STMT_TYPE:
Radek Krejci33090f92020-12-17 20:12:46 +01002790 LY_CHECK_RET(parse_type(ctx, &tpdf->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002791 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002792 case LY_STMT_UNITS:
Michal Vasko72c6d642024-02-27 14:59:01 +01002793 LY_CHECK_RET(parse_text_field(ctx, tpdf->units, LY_STMT_UNITS, 0, &tpdf->units, Y_STR_ARG, NULL, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002794 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002795 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002796 LY_CHECK_RET(parse_ext(ctx, word, word_len, tpdf, LY_STMT_TYPEDEF, 0, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002797 break;
2798 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002799 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "typedef");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002800 return LY_EVALID;
2801 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002802 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, tpdf->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002803 }
Michal Vasko12ef5362022-09-16 15:13:58 +02002804
Michal Vasko7fbc8162018-09-17 10:35:16 +02002805 /* mandatory substatements */
2806 if (!tpdf->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002807 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "typedef");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002808 return LY_EVALID;
2809 }
2810
Radek Krejcibbe09a92018-11-08 09:36:54 +01002811 /* store data for collision check */
aPiecek75b83a02021-06-29 10:34:38 +02002812 if (parent) {
aPiecek8d4e75d2021-06-24 14:47:06 +02002813 assert(ctx->main_ctx);
2814 LY_CHECK_RET(ly_set_add(&ctx->main_ctx->tpdfs_nodes, parent, 0, NULL));
Radek Krejcibbe09a92018-11-08 09:36:54 +01002815 }
2816
Michal Vasko12ef5362022-09-16 15:13:58 +02002817cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002818 return ret;
2819}
2820
Michal Vaskoea5abea2018-09-18 13:10:54 +02002821/**
2822 * @brief Parse the input or output statement.
2823 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002824 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002825 * @param[in] kw Type of this particular keyword
2826 * @param[in,out] inout_p Input/output pointer to write to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002827 * @return LY_ERR values.
2828 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002829static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002830parse_inout(struct lysp_yang_ctx *ctx, enum ly_stmt inout_kw, struct lysp_node *parent,
Radek Krejci2a9fc652021-01-22 17:44:34 +01002831 struct lysp_node_action_inout *inout_p)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002832{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002833 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002834 char *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002835 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002836 enum ly_stmt kw;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002837 ly_bool input = &((struct lysp_node_action *)parent)->input == inout_p ? 1 : 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002838
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002839 if (inout_p->nodetype) {
Michal Vasko193dacd2022-10-13 08:43:05 +02002840 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, lyplg_ext_stmt2str(inout_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002841 return LY_EVALID;
2842 }
2843
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002844 /* initiate structure */
Radek Krejci2a9fc652021-01-22 17:44:34 +01002845 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), input ? "input" : "output", 0, &inout_p->name));
2846 inout_p->nodetype = input ? LYS_INPUT : LYS_OUTPUT;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002847 inout_p->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002848
2849 /* parse substatements */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002850 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002851 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002852 case LY_STMT_ANYDATA:
Michal Vasko193dacd2022-10-13 08:43:05 +02002853 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", lyplg_ext_stmt2str(inout_kw));
Radek Krejci0f969882020-08-21 16:56:47 +02002854 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02002855 case LY_STMT_ANYXML:
Radek Krejci01180ac2021-01-27 08:48:22 +01002856 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002857 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002858 case LY_STMT_CHOICE:
Radek Krejci01180ac2021-01-27 08:48:22 +01002859 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002860 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002861 case LY_STMT_CONTAINER:
Radek Krejci01180ac2021-01-27 08:48:22 +01002862 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002863 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002864 case LY_STMT_LEAF:
Radek Krejci01180ac2021-01-27 08:48:22 +01002865 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002866 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002867 case LY_STMT_LEAF_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01002868 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002869 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002870 case LY_STMT_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01002871 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002872 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002873 case LY_STMT_USES:
Radek Krejci01180ac2021-01-27 08:48:22 +01002874 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002875 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002876 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01002877 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)inout_p, &inout_p->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002878 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002879 case LY_STMT_MUST:
Michal Vasko193dacd2022-10-13 08:43:05 +02002880 PARSER_CHECK_STMTVER2_RET(ctx, "must", lyplg_ext_stmt2str(inout_kw));
Radek Krejci33090f92020-12-17 20:12:46 +01002881 LY_CHECK_RET(parse_restrs(ctx, kw, &inout_p->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002882 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002883 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01002884 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)inout_p, &inout_p->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002885 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002886 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002887 LY_CHECK_RET(parse_ext(ctx, word, word_len, inout_p, inout_kw, 0, &inout_p->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002888 break;
2889 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002890 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), lyplg_ext_stmt2str(inout_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002891 return LY_EVALID;
2892 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002893 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, inout_p->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002894 }
Michal Vaskob83af8a2020-01-06 09:49:22 +01002895
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002896cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002897 return ret;
2898}
2899
Michal Vaskoea5abea2018-09-18 13:10:54 +02002900/**
2901 * @brief Parse the action statement.
2902 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002903 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002904 * @param[in,out] actions Actions to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002905 * @return LY_ERR values.
2906 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002907LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002908parse_action(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node_action **actions)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002909{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002910 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002911 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002912 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002913 enum ly_stmt kw;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002914 struct lysp_node_action *act;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002915
Radek Krejci2a9fc652021-01-22 17:44:34 +01002916 LY_LIST_NEW_RET(PARSER_CTX(ctx), actions, act, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002917
2918 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002919 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02002920 INSERT_WORD_GOTO(ctx, buf, act->name, word, word_len, ret, cleanup);
Michal Vasko1bf09392020-03-27 12:38:10 +01002921 act->nodetype = parent ? LYS_ACTION : LYS_RPC;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002922 act->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002923
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002924 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002925 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002926 case LY_STMT_DESCRIPTION:
Michal Vasko72c6d642024-02-27 14:59:01 +01002927 LY_CHECK_RET(parse_text_field(ctx, act->dsc, LY_STMT_DESCRIPTION, 0, &act->dsc, Y_STR_ARG, NULL, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002928 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002929 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002930 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &act->iffeatures, Y_STR_ARG, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002931 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002932 case LY_STMT_REFERENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01002933 LY_CHECK_RET(parse_text_field(ctx, act->ref, LY_STMT_REFERENCE, 0, &act->ref, Y_STR_ARG, NULL, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002934 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002935 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002936 LY_CHECK_RET(parse_status(ctx, &act->flags, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002937 break;
2938
Radek Krejcid6b76452019-09-03 17:03:03 +02002939 case LY_STMT_INPUT:
Radek Krejci33090f92020-12-17 20:12:46 +01002940 LY_CHECK_RET(parse_inout(ctx, kw, (struct lysp_node *)act, &act->input));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002941 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002942 case LY_STMT_OUTPUT:
Radek Krejci33090f92020-12-17 20:12:46 +01002943 LY_CHECK_RET(parse_inout(ctx, kw, (struct lysp_node *)act, &act->output));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002944 break;
2945
Radek Krejcid6b76452019-09-03 17:03:03 +02002946 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01002947 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)act, &act->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002948 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002949 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01002950 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)act, &act->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002951 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002952 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002953 LY_CHECK_RET(parse_ext(ctx, word, word_len, act, parent ? LY_STMT_ACTION : LY_STMT_RPC, 0, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002954 break;
2955 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002956 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), parent ? "action" : "rpc");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002957 return LY_EVALID;
2958 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002959 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, act->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002960 }
Michal Vasko7f45cf22020-10-01 12:49:44 +02002961
2962 /* always initialize inout, they are technically present (needed for later deviations/refines) */
2963 if (!act->input.nodetype) {
2964 act->input.nodetype = LYS_INPUT;
2965 act->input.parent = (struct lysp_node *)act;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002966 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), "input", 0, &act->input.name));
Michal Vasko7f45cf22020-10-01 12:49:44 +02002967 }
2968 if (!act->output.nodetype) {
2969 act->output.nodetype = LYS_OUTPUT;
2970 act->output.parent = (struct lysp_node *)act;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002971 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), "output", 0, &act->output.name));
Michal Vasko7f45cf22020-10-01 12:49:44 +02002972 }
2973
Michal Vasko12ef5362022-09-16 15:13:58 +02002974cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002975 return ret;
2976}
2977
Michal Vaskoea5abea2018-09-18 13:10:54 +02002978/**
2979 * @brief Parse the notification statement.
2980 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002981 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002982 * @param[in,out] notifs Notifications to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002983 * @return LY_ERR values.
2984 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002985LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002986parse_notif(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node_notif **notifs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002987{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002988 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002989 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002990 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002991 enum ly_stmt kw;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002992 struct lysp_node_notif *notif;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002993
Radek Krejci2a9fc652021-01-22 17:44:34 +01002994 LY_LIST_NEW_RET(PARSER_CTX(ctx), notifs, notif, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002995
2996 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002997 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02002998 INSERT_WORD_GOTO(ctx, buf, notif->name, word, word_len, ret, cleanup);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002999 notif->nodetype = LYS_NOTIF;
3000 notif->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003001
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003002 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003003 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003004 case LY_STMT_DESCRIPTION:
Michal Vasko72c6d642024-02-27 14:59:01 +01003005 LY_CHECK_RET(parse_text_field(ctx, notif->dsc, LY_STMT_DESCRIPTION, 0, &notif->dsc, Y_STR_ARG, NULL,
3006 &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003007 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003008 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003009 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &notif->iffeatures, Y_STR_ARG, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003010 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003011 case LY_STMT_REFERENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01003012 LY_CHECK_RET(parse_text_field(ctx, notif->ref, LY_STMT_REFERENCE, 0, &notif->ref, Y_STR_ARG, NULL,
3013 &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003014 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003015 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003016 LY_CHECK_RET(parse_status(ctx, &notif->flags, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003017 break;
3018
Radek Krejcid6b76452019-09-03 17:03:03 +02003019 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003020 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "notification");
Radek Krejci0f969882020-08-21 16:56:47 +02003021 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003022 case LY_STMT_ANYXML:
Radek Krejci01180ac2021-01-27 08:48:22 +01003023 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003024 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003025 case LY_STMT_CHOICE:
Michal Vasko5ae1e1a2021-02-08 09:53:26 +01003026 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003027 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003028 case LY_STMT_CONTAINER:
Radek Krejci01180ac2021-01-27 08:48:22 +01003029 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003030 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003031 case LY_STMT_LEAF:
Radek Krejci01180ac2021-01-27 08:48:22 +01003032 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003033 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003034 case LY_STMT_LEAF_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01003035 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003036 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003037 case LY_STMT_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01003038 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003039 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003040 case LY_STMT_USES:
Radek Krejci01180ac2021-01-27 08:48:22 +01003041 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003042 break;
3043
Radek Krejcid6b76452019-09-03 17:03:03 +02003044 case LY_STMT_MUST:
Radek Krejci335332a2019-09-05 13:03:35 +02003045 PARSER_CHECK_STMTVER2_RET(ctx, "must", "notification");
Radek Krejci33090f92020-12-17 20:12:46 +01003046 LY_CHECK_RET(parse_restrs(ctx, kw, &notif->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003047 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003048 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01003049 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)notif, &notif->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003050 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003051 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01003052 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)notif, &notif->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003053 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003054 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003055 LY_CHECK_RET(parse_ext(ctx, word, word_len, notif, LY_STMT_NOTIFICATION, 0, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003056 break;
3057 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003058 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "notification");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003059 return LY_EVALID;
3060 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003061 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, notif->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003062 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003063
Michal Vasko12ef5362022-09-16 15:13:58 +02003064cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003065 return ret;
3066}
3067
Michal Vaskoea5abea2018-09-18 13:10:54 +02003068/**
3069 * @brief Parse the grouping statement.
3070 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003071 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003072 * @param[in,out] groupings Groupings to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003073 * @return LY_ERR values.
3074 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003075LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02003076parse_grouping(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node_grp **groupings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003077{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003078 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003079 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003080 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003081 enum ly_stmt kw;
Radek Krejci2a9fc652021-01-22 17:44:34 +01003082 struct lysp_node_grp *grp;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003083
Radek Krejci2a9fc652021-01-22 17:44:34 +01003084 LY_LIST_NEW_RET(PARSER_CTX(ctx), groupings, grp, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003085
3086 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003087 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02003088 INSERT_WORD_GOTO(ctx, buf, grp->name, word, word_len, ret, cleanup);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003089 grp->nodetype = LYS_GROUPING;
3090 grp->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003091
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003092 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003093 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003094 case LY_STMT_DESCRIPTION:
Michal Vasko72c6d642024-02-27 14:59:01 +01003095 LY_CHECK_RET(parse_text_field(ctx, grp->dsc, LY_STMT_DESCRIPTION, 0, &grp->dsc, Y_STR_ARG, NULL, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003096 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003097 case LY_STMT_REFERENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01003098 LY_CHECK_RET(parse_text_field(ctx, grp->ref, LY_STMT_REFERENCE, 0, &grp->ref, Y_STR_ARG, NULL, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003099 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003100 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003101 LY_CHECK_RET(parse_status(ctx, &grp->flags, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003102 break;
3103
Radek Krejcid6b76452019-09-03 17:03:03 +02003104 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003105 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "grouping");
Radek Krejci0f969882020-08-21 16:56:47 +02003106 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003107 case LY_STMT_ANYXML:
Radek Krejci01180ac2021-01-27 08:48:22 +01003108 LY_CHECK_RET(parse_any(ctx, kw, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003109 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003110 case LY_STMT_CHOICE:
Radek Krejci01180ac2021-01-27 08:48:22 +01003111 LY_CHECK_RET(parse_choice(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003112 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003113 case LY_STMT_CONTAINER:
Radek Krejci01180ac2021-01-27 08:48:22 +01003114 LY_CHECK_RET(parse_container(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003115 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003116 case LY_STMT_LEAF:
Radek Krejci01180ac2021-01-27 08:48:22 +01003117 LY_CHECK_RET(parse_leaf(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003118 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003119 case LY_STMT_LEAF_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01003120 LY_CHECK_RET(parse_leaflist(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003121 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003122 case LY_STMT_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01003123 LY_CHECK_RET(parse_list(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003124 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003125 case LY_STMT_USES:
Radek Krejci01180ac2021-01-27 08:48:22 +01003126 LY_CHECK_RET(parse_uses(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003127 break;
3128
Radek Krejcid6b76452019-09-03 17:03:03 +02003129 case LY_STMT_TYPEDEF:
Radek Krejci2a9fc652021-01-22 17:44:34 +01003130 LY_CHECK_RET(parse_typedef(ctx, &grp->node, &grp->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003131 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003132 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003133 PARSER_CHECK_STMTVER2_RET(ctx, "action", "grouping");
Radek Krejci2a9fc652021-01-22 17:44:34 +01003134 LY_CHECK_RET(parse_action(ctx, &grp->node, &grp->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003135 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003136 case LY_STMT_GROUPING:
Radek Krejci2a9fc652021-01-22 17:44:34 +01003137 LY_CHECK_RET(parse_grouping(ctx, &grp->node, &grp->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003138 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003139 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003140 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "grouping");
Radek Krejci2a9fc652021-01-22 17:44:34 +01003141 LY_CHECK_RET(parse_notif(ctx, &grp->node, &grp->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003142 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003143 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003144 LY_CHECK_RET(parse_ext(ctx, word, word_len, grp, LY_STMT_GROUPING, 0, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003145 break;
3146 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003147 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "grouping");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003148 return LY_EVALID;
3149 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003150 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, grp->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003151 }
Michal Vasko12ef5362022-09-16 15:13:58 +02003152
aPiecek63e080d2021-06-29 13:53:28 +02003153 /* store data for collision check */
3154 if (parent) {
3155 assert(ctx->main_ctx);
3156 LY_CHECK_RET(ly_set_add(&ctx->main_ctx->grps_nodes, parent, 0, NULL));
3157 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003158
Michal Vasko12ef5362022-09-16 15:13:58 +02003159cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003160 return ret;
3161}
3162
Michal Vaskoea5abea2018-09-18 13:10:54 +02003163/**
David Sedlák0d6de5a2019-07-22 13:25:44 +02003164 * @brief Parse the augment statement.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003165 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003166 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003167 * @param[in,out] augments Augments to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003168 * @return LY_ERR values.
3169 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003170LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02003171parse_augment(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node_augment **augments)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003172{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003173 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003174 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003175 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003176 enum ly_stmt kw;
Radek Krejci2a9fc652021-01-22 17:44:34 +01003177 struct lysp_node_augment *aug;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003178
Radek Krejci2a9fc652021-01-22 17:44:34 +01003179 LY_LIST_NEW_RET(PARSER_CTX(ctx), augments, aug, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003180
3181 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003182 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vaskob36053d2020-03-26 15:49:30 +01003183 CHECK_NONEMPTY(ctx, word_len, "augment");
Michal Vasko12ef5362022-09-16 15:13:58 +02003184 INSERT_WORD_GOTO(ctx, buf, aug->nodeid, word, word_len, ret, cleanup);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003185 aug->nodetype = LYS_AUGMENT;
3186 aug->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003187
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003188 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003189 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003190 case LY_STMT_DESCRIPTION:
Michal Vasko72c6d642024-02-27 14:59:01 +01003191 LY_CHECK_RET(parse_text_field(ctx, aug->dsc, LY_STMT_DESCRIPTION, 0, &aug->dsc, Y_STR_ARG, NULL, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003192 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003193 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003194 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &aug->iffeatures, Y_STR_ARG, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003195 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003196 case LY_STMT_REFERENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01003197 LY_CHECK_RET(parse_text_field(ctx, aug->ref, LY_STMT_REFERENCE, 0, &aug->ref, Y_STR_ARG, NULL, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003198 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003199 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003200 LY_CHECK_RET(parse_status(ctx, &aug->flags, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003201 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003202 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003203 LY_CHECK_RET(parse_when(ctx, &aug->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003204 break;
3205
Radek Krejcid6b76452019-09-03 17:03:03 +02003206 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003207 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "augment");
Radek Krejci0f969882020-08-21 16:56:47 +02003208 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003209 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003210 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003211 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003212 case LY_STMT_CASE:
Radek Krejci33090f92020-12-17 20:12:46 +01003213 LY_CHECK_RET(parse_case(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003214 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003215 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01003216 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003217 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003218 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003219 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003220 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003221 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003222 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003223 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003224 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003225 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003226 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003227 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003228 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003229 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003230 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01003231 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003232 break;
3233
Radek Krejcid6b76452019-09-03 17:03:03 +02003234 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003235 PARSER_CHECK_STMTVER2_RET(ctx, "action", "augment");
Radek Krejci33090f92020-12-17 20:12:46 +01003236 LY_CHECK_RET(parse_action(ctx, (struct lysp_node *)aug, &aug->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003237 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003238 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003239 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "augment");
Radek Krejci33090f92020-12-17 20:12:46 +01003240 LY_CHECK_RET(parse_notif(ctx, (struct lysp_node *)aug, &aug->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003241 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003242 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003243 LY_CHECK_RET(parse_ext(ctx, word, word_len, aug, LY_STMT_AUGMENT, 0, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003244 break;
3245 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003246 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "augment");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003247 return LY_EVALID;
3248 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003249 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, aug->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003250 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003251
Michal Vasko12ef5362022-09-16 15:13:58 +02003252cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003253 return ret;
3254}
3255
Michal Vaskoea5abea2018-09-18 13:10:54 +02003256/**
3257 * @brief Parse the uses statement.
3258 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003259 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003260 * @param[in,out] siblings Siblings to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003261 * @return LY_ERR values.
3262 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003263LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02003264parse_uses(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003265{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003266 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003267 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003268 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003269 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003270 struct lysp_node_uses *uses;
3271
David Sedlák60adc092019-08-06 15:57:02 +02003272 /* create uses structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003273 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, uses, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003274 uses->nodetype = LYS_USES;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003275 uses->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003276
Michal Vasko7fbc8162018-09-17 10:35:16 +02003277 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003278 LY_CHECK_RET(get_argument(ctx, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02003279 INSERT_WORD_GOTO(ctx, buf, uses->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003280
3281 /* parse substatements */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003282 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003283 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003284 case LY_STMT_DESCRIPTION:
Michal Vasko72c6d642024-02-27 14:59:01 +01003285 LY_CHECK_RET(parse_text_field(ctx, uses->dsc, LY_STMT_DESCRIPTION, 0, &uses->dsc, Y_STR_ARG, NULL, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003286 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003287 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003288 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &uses->iffeatures, Y_STR_ARG, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003289 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003290 case LY_STMT_REFERENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01003291 LY_CHECK_RET(parse_text_field(ctx, uses->ref, LY_STMT_REFERENCE, 0, &uses->ref, Y_STR_ARG, NULL, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003292 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003293 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003294 LY_CHECK_RET(parse_status(ctx, &uses->flags, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003295 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003296 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003297 LY_CHECK_RET(parse_when(ctx, &uses->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003298 break;
3299
Radek Krejcid6b76452019-09-03 17:03:03 +02003300 case LY_STMT_REFINE:
Radek Krejci33090f92020-12-17 20:12:46 +01003301 LY_CHECK_RET(parse_refine(ctx, &uses->refines));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003302 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003303 case LY_STMT_AUGMENT:
Radek Krejci33090f92020-12-17 20:12:46 +01003304 LY_CHECK_RET(parse_augment(ctx, (struct lysp_node *)uses, &uses->augments));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003305 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003306 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003307 LY_CHECK_RET(parse_ext(ctx, word, word_len, uses, LY_STMT_USES, 0, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003308 break;
3309 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003310 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "uses");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003311 return LY_EVALID;
3312 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003313 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, uses->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003314 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003315
Michal Vasko12ef5362022-09-16 15:13:58 +02003316cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003317 return ret;
3318}
3319
Michal Vaskoea5abea2018-09-18 13:10:54 +02003320/**
3321 * @brief Parse the case statement.
3322 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003323 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003324 * @param[in,out] siblings Siblings to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003325 * @return LY_ERR values.
3326 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003327LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02003328parse_case(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003329{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003330 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003331 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003332 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003333 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003334 struct lysp_node_case *cas;
3335
David Sedlák60adc092019-08-06 15:57:02 +02003336 /* create new case structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003337 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, cas, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003338 cas->nodetype = LYS_CASE;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003339 cas->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003340
Michal Vasko7fbc8162018-09-17 10:35:16 +02003341 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003342 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02003343 INSERT_WORD_GOTO(ctx, buf, cas->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003344
3345 /* parse substatements */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003346 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003347 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003348 case LY_STMT_DESCRIPTION:
Michal Vasko72c6d642024-02-27 14:59:01 +01003349 LY_CHECK_RET(parse_text_field(ctx, cas->dsc, LY_STMT_DESCRIPTION, 0, &cas->dsc, Y_STR_ARG, NULL, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003350 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003351 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003352 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &cas->iffeatures, Y_STR_ARG, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003353 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003354 case LY_STMT_REFERENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01003355 LY_CHECK_RET(parse_text_field(ctx, cas->ref, LY_STMT_REFERENCE, 0, &cas->ref, Y_STR_ARG, NULL, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003356 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003357 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003358 LY_CHECK_RET(parse_status(ctx, &cas->flags, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003359 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003360 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003361 LY_CHECK_RET(parse_when(ctx, &cas->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003362 break;
3363
Radek Krejcid6b76452019-09-03 17:03:03 +02003364 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003365 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "case");
Radek Krejci0f969882020-08-21 16:56:47 +02003366 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003367 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003368 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003369 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003370 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01003371 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003372 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003373 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003374 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003375 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003376 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003377 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003378 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003379 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003380 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003381 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003382 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003383 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003384 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003385 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01003386 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003387 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003388 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003389 LY_CHECK_RET(parse_ext(ctx, word, word_len, cas, LY_STMT_CASE, 0, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003390 break;
3391 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003392 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "case");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003393 return LY_EVALID;
3394 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003395 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, cas->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003396 }
Michal Vasko12ef5362022-09-16 15:13:58 +02003397
3398cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003399 return ret;
3400}
3401
Michal Vaskoea5abea2018-09-18 13:10:54 +02003402/**
3403 * @brief Parse the choice statement.
3404 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003405 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003406 * @param[in,out] siblings Siblings to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003407 * @return LY_ERR values.
3408 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003409LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02003410parse_choice(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003411{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003412 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003413 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003414 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003415 enum ly_stmt kw;
Radek Krejci44ceedc2018-10-02 15:54:31 +02003416 struct lysp_node_choice *choice;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003417
David Sedlák60adc092019-08-06 15:57:02 +02003418 /* create new choice structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003419 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, choice, next, LY_EMEM);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003420 choice->nodetype = LYS_CHOICE;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003421 choice->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003422
Michal Vasko7fbc8162018-09-17 10:35:16 +02003423 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003424 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02003425 INSERT_WORD_GOTO(ctx, buf, choice->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003426
3427 /* parse substatements */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003428 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003429 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003430 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01003431 LY_CHECK_RET(parse_config(ctx, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003432 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003433 case LY_STMT_DESCRIPTION:
Michal Vasko72c6d642024-02-27 14:59:01 +01003434 LY_CHECK_RET(parse_text_field(ctx, choice->dsc, LY_STMT_DESCRIPTION, 0, &choice->dsc, Y_STR_ARG, NULL,
3435 &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003436 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003437 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003438 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &choice->iffeatures, Y_STR_ARG, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003439 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003440 case LY_STMT_MANDATORY:
Radek Krejci33090f92020-12-17 20:12:46 +01003441 LY_CHECK_RET(parse_mandatory(ctx, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003442 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003443 case LY_STMT_REFERENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01003444 LY_CHECK_RET(parse_text_field(ctx, choice->ref, LY_STMT_REFERENCE, 0, &choice->ref, Y_STR_ARG, NULL,
3445 &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003446 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003447 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003448 LY_CHECK_RET(parse_status(ctx, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003449 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003450 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003451 LY_CHECK_RET(parse_when(ctx, &choice->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003452 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003453 case LY_STMT_DEFAULT:
Michal Vasko193dacd2022-10-13 08:43:05 +02003454 LY_CHECK_RET(parse_text_field(ctx, &choice->dflt, LY_STMT_DEFAULT, 0, &choice->dflt.str, Y_PREF_IDENTIF_ARG,
Michal Vasko72c6d642024-02-27 14:59:01 +01003455 &choice->dflt.flags, &choice->exts));
Michal Vasko8a67eff2021-12-07 14:04:47 +01003456 choice->dflt.mod = PARSER_CUR_PMOD(ctx);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003457 break;
3458
Radek Krejcid6b76452019-09-03 17:03:03 +02003459 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003460 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "choice");
Radek Krejci0f969882020-08-21 16:56:47 +02003461 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003462 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003463 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003464 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003465 case LY_STMT_CASE:
Radek Krejci33090f92020-12-17 20:12:46 +01003466 LY_CHECK_RET(parse_case(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003467 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003468 case LY_STMT_CHOICE:
Radek Krejci335332a2019-09-05 13:03:35 +02003469 PARSER_CHECK_STMTVER2_RET(ctx, "choice", "choice");
Radek Krejci33090f92020-12-17 20:12:46 +01003470 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003471 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003472 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003473 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003474 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003475 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003476 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003477 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003478 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003479 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003480 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003481 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003482 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003483 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003484 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003485 LY_CHECK_RET(parse_ext(ctx, word, word_len, choice, LY_STMT_CHOICE, 0, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003486 break;
3487 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003488 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "choice");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003489 return LY_EVALID;
3490 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003491 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, choice->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003492 }
Michal Vasko12ef5362022-09-16 15:13:58 +02003493
3494cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003495 return ret;
3496}
3497
Michal Vaskoea5abea2018-09-18 13:10:54 +02003498/**
3499 * @brief Parse the container statement.
3500 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003501 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003502 * @param[in,out] siblings Siblings to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003503 * @return LY_ERR values.
3504 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003505LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02003506parse_container(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003507{
3508 LY_ERR ret = 0;
3509 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003510 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003511 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003512 struct lysp_node_container *cont;
3513
David Sedlák60adc092019-08-06 15:57:02 +02003514 /* create new container structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003515 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, cont, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003516 cont->nodetype = LYS_CONTAINER;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003517 cont->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003518
Michal Vasko7fbc8162018-09-17 10:35:16 +02003519 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003520 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02003521 INSERT_WORD_GOTO(ctx, buf, cont->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003522
3523 /* parse substatements */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003524 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003525 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003526 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01003527 LY_CHECK_RET(parse_config(ctx, &cont->flags, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003528 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003529 case LY_STMT_DESCRIPTION:
Michal Vasko72c6d642024-02-27 14:59:01 +01003530 LY_CHECK_RET(parse_text_field(ctx, cont->dsc, LY_STMT_DESCRIPTION, 0, &cont->dsc, Y_STR_ARG, NULL, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003531 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003532 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003533 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &cont->iffeatures, Y_STR_ARG, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003534 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003535 case LY_STMT_REFERENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01003536 LY_CHECK_RET(parse_text_field(ctx, cont->ref, LY_STMT_REFERENCE, 0, &cont->ref, Y_STR_ARG, NULL, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003537 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003538 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003539 LY_CHECK_RET(parse_status(ctx, &cont->flags, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003540 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003541 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003542 LY_CHECK_RET(parse_when(ctx, &cont->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003543 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003544 case LY_STMT_PRESENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01003545 LY_CHECK_RET(parse_text_field(ctx, cont->presence, LY_STMT_PRESENCE, 0, &cont->presence, Y_STR_ARG, NULL,
3546 &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003547 break;
3548
Radek Krejcid6b76452019-09-03 17:03:03 +02003549 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003550 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "container");
Radek Krejci0f969882020-08-21 16:56:47 +02003551 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003552 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003553 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003554 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003555 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01003556 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003557 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003558 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003559 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003560 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003561 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003562 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003563 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003564 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003565 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003566 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003567 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003568 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003569 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003570 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01003571 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003572 break;
3573
Radek Krejcid6b76452019-09-03 17:03:03 +02003574 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01003575 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)cont, &cont->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003576 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003577 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01003578 LY_CHECK_RET(parse_restrs(ctx, kw, &cont->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003579 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003580 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003581 PARSER_CHECK_STMTVER2_RET(ctx, "action", "container");
Radek Krejci33090f92020-12-17 20:12:46 +01003582 LY_CHECK_RET(parse_action(ctx, (struct lysp_node *)cont, &cont->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003583 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003584 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01003585 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)cont, &cont->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003586 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003587 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003588 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "container");
Radek Krejci33090f92020-12-17 20:12:46 +01003589 LY_CHECK_RET(parse_notif(ctx, (struct lysp_node *)cont, &cont->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003590 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003591 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003592 LY_CHECK_RET(parse_ext(ctx, word, word_len, cont, LY_STMT_CONTAINER, 0, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003593 break;
3594 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003595 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "container");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003596 return LY_EVALID;
3597 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003598 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, cont->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003599 }
Radek Krejci2a9fc652021-01-22 17:44:34 +01003600
Michal Vasko12ef5362022-09-16 15:13:58 +02003601cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003602 return ret;
3603}
3604
Michal Vaskoea5abea2018-09-18 13:10:54 +02003605/**
3606 * @brief Parse the list statement.
3607 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003608 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003609 * @param[in,out] siblings Siblings to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003610 * @return LY_ERR values.
3611 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003612LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02003613parse_list(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003614{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003615 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003616 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003617 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003618 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003619 struct lysp_node_list *list;
3620
David Sedlák60adc092019-08-06 15:57:02 +02003621 /* create new list structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003622 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, list, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003623 list->nodetype = LYS_LIST;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003624 list->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003625
Michal Vasko7fbc8162018-09-17 10:35:16 +02003626 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003627 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02003628 INSERT_WORD_GOTO(ctx, buf, list->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003629
3630 /* parse substatements */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003631 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003632 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003633 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01003634 LY_CHECK_RET(parse_config(ctx, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003635 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003636 case LY_STMT_DESCRIPTION:
Michal Vasko72c6d642024-02-27 14:59:01 +01003637 LY_CHECK_RET(parse_text_field(ctx, list->dsc, LY_STMT_DESCRIPTION, 0, &list->dsc, Y_STR_ARG, NULL, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003638 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003639 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003640 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &list->iffeatures, Y_STR_ARG, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003641 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003642 case LY_STMT_REFERENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01003643 LY_CHECK_RET(parse_text_field(ctx, list->ref, LY_STMT_REFERENCE, 0, &list->ref, Y_STR_ARG, NULL, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003644 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003645 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003646 LY_CHECK_RET(parse_status(ctx, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003647 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003648 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003649 LY_CHECK_RET(parse_when(ctx, &list->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003650 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003651 case LY_STMT_KEY:
Michal Vasko72c6d642024-02-27 14:59:01 +01003652 LY_CHECK_RET(parse_text_field(ctx, list, LY_STMT_KEY, 0, &list->key, Y_STR_ARG, NULL, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003653 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003654 case LY_STMT_MAX_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01003655 LY_CHECK_RET(parse_maxelements(ctx, &list->max, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003656 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003657 case LY_STMT_MIN_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01003658 LY_CHECK_RET(parse_minelements(ctx, &list->min, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003659 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003660 case LY_STMT_ORDERED_BY:
Michal Vasko193dacd2022-10-13 08:43:05 +02003661 LY_CHECK_RET(parse_orderedby(ctx, &list->node));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003662 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003663 case LY_STMT_UNIQUE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003664 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_UNIQUE, &list->uniques, Y_STR_ARG, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003665 break;
3666
Radek Krejcid6b76452019-09-03 17:03:03 +02003667 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003668 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "list");
Radek Krejci0f969882020-08-21 16:56:47 +02003669 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003670 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003671 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003672 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003673 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01003674 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003675 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003676 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003677 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003678 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003679 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003680 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003681 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003682 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003683 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003684 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003685 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003686 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003687 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003688 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01003689 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003690 break;
3691
Radek Krejcid6b76452019-09-03 17:03:03 +02003692 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01003693 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)list, &list->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003694 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003695 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01003696 LY_CHECK_RET(parse_restrs(ctx, kw, &list->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003697 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003698 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003699 PARSER_CHECK_STMTVER2_RET(ctx, "action", "list");
Radek Krejci33090f92020-12-17 20:12:46 +01003700 LY_CHECK_RET(parse_action(ctx, (struct lysp_node *)list, &list->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003701 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003702 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01003703 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)list, &list->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003704 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003705 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003706 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "list");
Radek Krejci33090f92020-12-17 20:12:46 +01003707 LY_CHECK_RET(parse_notif(ctx, (struct lysp_node *)list, &list->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003708 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003709 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003710 LY_CHECK_RET(parse_ext(ctx, word, word_len, list, LY_STMT_LIST, 0, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003711 break;
3712 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003713 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "list");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003714 return LY_EVALID;
3715 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003716 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, list->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003717 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003718
Michal Vasko12ef5362022-09-16 15:13:58 +02003719cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003720 return ret;
3721}
3722
Michal Vaskoea5abea2018-09-18 13:10:54 +02003723/**
3724 * @brief Parse the yin-element statement.
3725 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003726 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02003727 * @param[in,out] ext Extension to fill.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003728 * @return LY_ERR values.
3729 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003730static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02003731parse_yinelement(struct lysp_yang_ctx *ctx, struct lysp_ext *ext)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003732{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003733 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003734 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003735 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003736 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003737
Michal Vasko193dacd2022-10-13 08:43:05 +02003738 if (ext->flags & LYS_YINELEM_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02003739 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003740 return LY_EVALID;
3741 }
3742
3743 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003744 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003745
Radek Krejcif13b87b2020-12-01 22:02:17 +01003746 if ((word_len == ly_strlen_const("true")) && !strncmp(word, "true", word_len)) {
Michal Vasko193dacd2022-10-13 08:43:05 +02003747 ext->flags |= LYS_YINELEM_TRUE;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003748 } else if ((word_len == ly_strlen_const("false")) && !strncmp(word, "false", word_len)) {
Michal Vasko193dacd2022-10-13 08:43:05 +02003749 ext->flags |= LYS_YINELEM_FALSE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003750 } else {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02003751 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003752 free(buf);
3753 return LY_EVALID;
3754 }
3755 free(buf);
3756
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003757 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003758 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003759 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003760 LY_CHECK_RET(parse_ext(ctx, word, word_len, ext, LY_STMT_YIN_ELEMENT, 0, &ext->exts));
Michal Vaskod989ba02020-08-24 10:59:24 +02003761 LY_CHECK_RET(ret);
3762 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003763 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003764 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003765 return LY_EVALID;
3766 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003767 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003768 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003769
3770cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003771 return ret;
3772}
3773
Michal Vaskoea5abea2018-09-18 13:10:54 +02003774/**
David Sedlák2444f8f2019-07-09 11:02:47 +02003775 * @brief Parse the argument statement.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003776 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003777 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02003778 * @param[in,out] ext Extension to fill.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003779 * @return LY_ERR values.
3780 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003781static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02003782parse_argument(struct lysp_yang_ctx *ctx, struct lysp_ext *ext)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003783{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003784 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003785 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003786 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003787 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003788
Michal Vasko193dacd2022-10-13 08:43:05 +02003789 if (ext->argname) {
David Sedlákb3077192019-06-19 10:55:37 +02003790 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003791 return LY_EVALID;
3792 }
3793
3794 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003795 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko193dacd2022-10-13 08:43:05 +02003796 INSERT_WORD_GOTO(ctx, buf, ext->argname, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003797
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003798 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003799 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003800 case LY_STMT_YIN_ELEMENT:
Michal Vasko193dacd2022-10-13 08:43:05 +02003801 LY_CHECK_RET(parse_yinelement(ctx, ext));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003802 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003803 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003804 LY_CHECK_RET(parse_ext(ctx, word, word_len, ext, LY_STMT_ARGUMENT, 0, &ext->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003805 break;
3806 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003807 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003808 return LY_EVALID;
3809 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003810 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003811 }
Michal Vasko12ef5362022-09-16 15:13:58 +02003812
3813cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003814 return ret;
3815}
3816
Michal Vaskoea5abea2018-09-18 13:10:54 +02003817/**
3818 * @brief Parse the extension statement.
3819 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003820 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003821 * @param[in,out] extensions Extensions to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003822 * @return LY_ERR values.
3823 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003824static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02003825parse_extension(struct lysp_yang_ctx *ctx, struct lysp_ext **extensions)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003826{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003827 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003828 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003829 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003830 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003831 struct lysp_ext *ex;
3832
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003833 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *extensions, ex, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003834
3835 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003836 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02003837 INSERT_WORD_GOTO(ctx, buf, ex->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003838
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003839 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003840 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003841 case LY_STMT_DESCRIPTION:
Michal Vasko72c6d642024-02-27 14:59:01 +01003842 LY_CHECK_RET(parse_text_field(ctx, ex->dsc, LY_STMT_DESCRIPTION, 0, &ex->dsc, Y_STR_ARG, NULL, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003843 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003844 case LY_STMT_REFERENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01003845 LY_CHECK_RET(parse_text_field(ctx, ex->ref, LY_STMT_REFERENCE, 0, &ex->ref, Y_STR_ARG, NULL, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003846 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003847 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003848 LY_CHECK_RET(parse_status(ctx, &ex->flags, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003849 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003850 case LY_STMT_ARGUMENT:
Michal Vasko193dacd2022-10-13 08:43:05 +02003851 LY_CHECK_RET(parse_argument(ctx, ex));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003852 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003853 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003854 LY_CHECK_RET(parse_ext(ctx, word, word_len, ex, LY_STMT_EXTENSION, 0, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003855 break;
3856 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003857 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "extension");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003858 return LY_EVALID;
3859 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003860 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, ex->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003861 }
Michal Vasko12ef5362022-09-16 15:13:58 +02003862
3863cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003864 return ret;
3865}
3866
Michal Vaskoea5abea2018-09-18 13:10:54 +02003867/**
3868 * @brief Parse the deviate statement.
3869 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003870 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003871 * @param[in,out] deviates Deviates to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003872 * @return LY_ERR values.
3873 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003874LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02003875parse_deviate(struct lysp_yang_ctx *ctx, struct lysp_deviate **deviates)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003876{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003877 LY_ERR ret = LY_SUCCESS;
Michal Vasko12ef5362022-09-16 15:13:58 +02003878 char *buf = NULL, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003879 size_t word_len, dev_mod;
Radek Krejcid6b76452019-09-03 17:03:03 +02003880 enum ly_stmt kw;
Michal Vaskoc636ea42022-09-16 10:20:31 +02003881 struct lysf_ctx fctx = {.ctx = PARSER_CTX(ctx)};
Michal Vasko12ef5362022-09-16 15:13:58 +02003882 struct lysp_deviate *d = NULL;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003883 struct lysp_deviate_add *d_add = NULL;
3884 struct lysp_deviate_rpl *d_rpl = NULL;
3885 struct lysp_deviate_del *d_del = NULL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003886 const char **d_units = NULL;
3887 struct lysp_qname **d_uniques = NULL, **d_dflts = NULL;
Radek Krejci4ad42aa2019-07-23 16:55:58 +02003888 struct lysp_restr **d_musts = NULL;
3889 uint16_t *d_flags = 0;
3890 uint32_t *d_min = 0, *d_max = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003891
3892 /* get value */
Michal Vasko12ef5362022-09-16 15:13:58 +02003893 LY_CHECK_GOTO(ret = get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003894
Radek Krejcif13b87b2020-12-01 22:02:17 +01003895 if ((word_len == ly_strlen_const("not-supported")) && !strncmp(word, "not-supported", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003896 dev_mod = LYS_DEV_NOT_SUPPORTED;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003897 } else if ((word_len == ly_strlen_const("add")) && !strncmp(word, "add", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003898 dev_mod = LYS_DEV_ADD;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003899 } else if ((word_len == ly_strlen_const("replace")) && !strncmp(word, "replace", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003900 dev_mod = LYS_DEV_REPLACE;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003901 } else if ((word_len == ly_strlen_const("delete")) && !strncmp(word, "delete", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003902 dev_mod = LYS_DEV_DELETE;
3903 } else {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02003904 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, "deviate");
Michal Vasko12ef5362022-09-16 15:13:58 +02003905 ret = LY_EVALID;
3906 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003907 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003908
3909 /* create structure */
3910 switch (dev_mod) {
3911 case LYS_DEV_NOT_SUPPORTED:
3912 d = calloc(1, sizeof *d);
Michal Vasko12ef5362022-09-16 15:13:58 +02003913 LY_CHECK_ERR_GOTO(!d, LOGMEM(PARSER_CTX(ctx)); ret = LY_EMEM, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003914 break;
3915 case LYS_DEV_ADD:
3916 d_add = calloc(1, sizeof *d_add);
Michal Vasko12ef5362022-09-16 15:13:58 +02003917 LY_CHECK_ERR_GOTO(!d_add, LOGMEM(PARSER_CTX(ctx)); ret = LY_EMEM, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003918 d = (struct lysp_deviate *)d_add;
3919 d_units = &d_add->units;
3920 d_uniques = &d_add->uniques;
3921 d_dflts = &d_add->dflts;
3922 d_musts = &d_add->musts;
3923 d_flags = &d_add->flags;
3924 d_min = &d_add->min;
3925 d_max = &d_add->max;
3926 break;
3927 case LYS_DEV_REPLACE:
3928 d_rpl = calloc(1, sizeof *d_rpl);
Michal Vasko12ef5362022-09-16 15:13:58 +02003929 LY_CHECK_ERR_GOTO(!d_rpl, LOGMEM(PARSER_CTX(ctx)); ret = LY_EMEM, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003930 d = (struct lysp_deviate *)d_rpl;
3931 d_units = &d_rpl->units;
3932 d_flags = &d_rpl->flags;
3933 d_min = &d_rpl->min;
3934 d_max = &d_rpl->max;
3935 break;
3936 case LYS_DEV_DELETE:
3937 d_del = calloc(1, sizeof *d_del);
Michal Vasko12ef5362022-09-16 15:13:58 +02003938 LY_CHECK_ERR_GOTO(!d_del, LOGMEM(PARSER_CTX(ctx)); ret = LY_EMEM, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003939 d = (struct lysp_deviate *)d_del;
3940 d_units = &d_del->units;
3941 d_uniques = &d_del->uniques;
3942 d_dflts = &d_del->dflts;
3943 d_musts = &d_del->musts;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003944 break;
3945 default:
3946 assert(0);
Michal Vasko12ef5362022-09-16 15:13:58 +02003947 LOGINT(PARSER_CTX(ctx));
3948 ret = LY_EINT;
3949 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003950 }
3951 d->mod = dev_mod;
3952
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003953 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003954 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003955 case LY_STMT_CONFIG:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003956 switch (dev_mod) {
3957 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003958 case LYS_DEV_DELETE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003959 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), lyplg_ext_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02003960 ret = LY_EVALID;
3961 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003962 default:
Michal Vasko12ef5362022-09-16 15:13:58 +02003963 LY_CHECK_GOTO(ret = parse_config(ctx, d_flags, &d->exts), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003964 break;
3965 }
3966 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003967 case LY_STMT_DEFAULT:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003968 switch (dev_mod) {
3969 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko193dacd2022-10-13 08:43:05 +02003970 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), lyplg_ext_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02003971 ret = LY_EVALID;
3972 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003973 case LYS_DEV_REPLACE:
Michal Vasko72c6d642024-02-27 14:59:01 +01003974 ret = parse_text_field(ctx, &d_rpl->dflt, LY_STMT_DEFAULT, 0, &d_rpl->dflt.str, Y_STR_ARG,
3975 &d_rpl->dflt.flags, &d->exts);
Michal Vasko193dacd2022-10-13 08:43:05 +02003976 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko8a67eff2021-12-07 14:04:47 +01003977 d_rpl->dflt.mod = PARSER_CUR_PMOD(ctx);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003978 break;
3979 default:
Michal Vasko12ef5362022-09-16 15:13:58 +02003980 LY_CHECK_GOTO(ret = parse_qnames(ctx, LY_STMT_DEFAULT, d_dflts, Y_STR_ARG, &d->exts), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003981 break;
3982 }
3983 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003984 case LY_STMT_MANDATORY:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003985 switch (dev_mod) {
3986 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003987 case LYS_DEV_DELETE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003988 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), lyplg_ext_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02003989 ret = LY_EVALID;
3990 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003991 default:
Michal Vasko12ef5362022-09-16 15:13:58 +02003992 LY_CHECK_GOTO(ret = parse_mandatory(ctx, d_flags, &d->exts), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003993 break;
3994 }
3995 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003996 case LY_STMT_MAX_ELEMENTS:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003997 switch (dev_mod) {
3998 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003999 case LYS_DEV_DELETE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004000 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), lyplg_ext_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02004001 ret = LY_EVALID;
4002 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004003 default:
Michal Vasko12ef5362022-09-16 15:13:58 +02004004 LY_CHECK_GOTO(ret = parse_maxelements(ctx, d_max, d_flags, &d->exts), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004005 break;
4006 }
4007 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004008 case LY_STMT_MIN_ELEMENTS:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004009 switch (dev_mod) {
4010 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02004011 case LYS_DEV_DELETE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004012 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), lyplg_ext_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02004013 ret = LY_EVALID;
4014 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004015 default:
Michal Vasko12ef5362022-09-16 15:13:58 +02004016 LY_CHECK_GOTO(ret = parse_minelements(ctx, d_min, d_flags, &d->exts), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004017 break;
4018 }
4019 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004020 case LY_STMT_MUST:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004021 switch (dev_mod) {
4022 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02004023 case LYS_DEV_REPLACE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004024 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), lyplg_ext_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02004025 ret = LY_EVALID;
4026 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004027 default:
Michal Vasko12ef5362022-09-16 15:13:58 +02004028 LY_CHECK_GOTO(ret = parse_restrs(ctx, kw, d_musts), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004029 break;
4030 }
4031 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004032 case LY_STMT_TYPE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004033 switch (dev_mod) {
4034 case LYS_DEV_NOT_SUPPORTED:
4035 case LYS_DEV_ADD:
4036 case LYS_DEV_DELETE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004037 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), lyplg_ext_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02004038 ret = LY_EVALID;
4039 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004040 default:
Radek Krejci2c02f3e2018-10-16 10:54:38 +02004041 if (d_rpl->type) {
Michal Vasko193dacd2022-10-13 08:43:05 +02004042 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, lyplg_ext_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02004043 ret = LY_EVALID;
4044 goto cleanup;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02004045 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004046 d_rpl->type = calloc(1, sizeof *d_rpl->type);
Michal Vasko12ef5362022-09-16 15:13:58 +02004047 LY_CHECK_ERR_GOTO(!d_rpl->type, LOGMEM(PARSER_CTX(ctx)); ret = LY_EMEM, cleanup);
4048 LY_CHECK_GOTO(ret = parse_type(ctx, d_rpl->type), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004049 break;
4050 }
4051 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004052 case LY_STMT_UNIQUE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004053 switch (dev_mod) {
4054 case LYS_DEV_NOT_SUPPORTED:
4055 case LYS_DEV_REPLACE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004056 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), lyplg_ext_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02004057 ret = LY_EVALID;
4058 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004059 default:
Michal Vasko12ef5362022-09-16 15:13:58 +02004060 LY_CHECK_GOTO(ret = parse_qnames(ctx, LY_STMT_UNIQUE, d_uniques, Y_STR_ARG, &d->exts), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004061 break;
4062 }
4063 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004064 case LY_STMT_UNITS:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004065 switch (dev_mod) {
4066 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko193dacd2022-10-13 08:43:05 +02004067 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), lyplg_ext_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02004068 ret = LY_EVALID;
4069 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004070 default:
Michal Vasko72c6d642024-02-27 14:59:01 +01004071 ret = parse_text_field(ctx, *d_units, LY_STMT_UNITS, 0, d_units, Y_STR_ARG, NULL, &d->exts);
4072 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004073 break;
4074 }
4075 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004076 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004077 LY_CHECK_GOTO(ret = parse_ext(ctx, word, word_len, d, LY_STMT_DEVIATE, 0, &d->exts), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004078 break;
4079 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02004080 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "deviate");
Michal Vasko12ef5362022-09-16 15:13:58 +02004081 ret = LY_EVALID;
4082 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004083 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004084 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, d->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004085 }
Michal Vasko12ef5362022-09-16 15:13:58 +02004086
4087cleanup:
4088 free(buf);
4089 if (ret) {
Michal Vaskoc636ea42022-09-16 10:20:31 +02004090 lysp_deviate_free(&fctx, d);
Michal Vasko12ef5362022-09-16 15:13:58 +02004091 free(d);
4092 } else {
4093 /* insert into siblings */
4094 LY_LIST_INSERT(deviates, d, next);
4095 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004096 return ret;
4097}
4098
Michal Vaskoea5abea2018-09-18 13:10:54 +02004099/**
4100 * @brief Parse the deviation statement.
4101 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004102 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004103 * @param[in,out] deviations Deviations to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004104 * @return LY_ERR values.
4105 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004106LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02004107parse_deviation(struct lysp_yang_ctx *ctx, struct lysp_deviation **deviations)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004108{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004109 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004110 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004111 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004112 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004113 struct lysp_deviation *dev;
Michal Vaskoc636ea42022-09-16 10:20:31 +02004114 struct lysf_ctx fctx = {.ctx = PARSER_CTX(ctx)};
Michal Vasko7fbc8162018-09-17 10:35:16 +02004115
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004116 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *deviations, dev, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004117
4118 /* get value */
Michal Vasko12ef5362022-09-16 15:13:58 +02004119 LY_CHECK_GOTO(ret = get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len), cleanup);
Michal Vaskob36053d2020-03-26 15:49:30 +01004120 CHECK_NONEMPTY(ctx, word_len, "deviation");
Michal Vasko12ef5362022-09-16 15:13:58 +02004121 INSERT_WORD_GOTO(ctx, buf, dev->nodeid, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004122
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004123 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004124 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02004125 case LY_STMT_DESCRIPTION:
Michal Vasko72c6d642024-02-27 14:59:01 +01004126 ret = parse_text_field(ctx, dev->dsc, LY_STMT_DESCRIPTION, 0, &dev->dsc, Y_STR_ARG, NULL, &dev->exts);
4127 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004128 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004129 case LY_STMT_DEVIATE:
Michal Vasko12ef5362022-09-16 15:13:58 +02004130 LY_CHECK_GOTO(ret = parse_deviate(ctx, &dev->deviates), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004131 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004132 case LY_STMT_REFERENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01004133 ret = parse_text_field(ctx, dev->ref, LY_STMT_REFERENCE, 0, &dev->ref, Y_STR_ARG, NULL, &dev->exts);
4134 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004135 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004136 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004137 LY_CHECK_GOTO(ret = parse_ext(ctx, word, word_len, dev, LY_STMT_DEVIATION, 0, &dev->exts), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004138 break;
4139 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02004140 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "deviation");
Michal Vasko12ef5362022-09-16 15:13:58 +02004141 ret = LY_EVALID;
4142 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004143 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004144 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, dev->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004145 }
Michal Vasko12ef5362022-09-16 15:13:58 +02004146
Michal Vasko7fbc8162018-09-17 10:35:16 +02004147 /* mandatory substatements */
4148 if (!dev->deviates) {
David Sedlákb3077192019-06-19 10:55:37 +02004149 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "deviate", "deviation");
Michal Vasko12ef5362022-09-16 15:13:58 +02004150 ret = LY_EVALID;
4151 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004152 }
4153
Michal Vasko12ef5362022-09-16 15:13:58 +02004154cleanup:
4155 if (ret) {
Michal Vaskoc636ea42022-09-16 10:20:31 +02004156 lysp_deviation_free(&fctx, dev);
Michal Vasko12ef5362022-09-16 15:13:58 +02004157 LY_ARRAY_DECREMENT_FREE(*deviations);
4158 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004159 return ret;
4160}
4161
Michal Vaskoea5abea2018-09-18 13:10:54 +02004162/**
4163 * @brief Parse the feature statement.
4164 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004165 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004166 * @param[in,out] features Features to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004167 * @return LY_ERR values.
4168 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004169LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02004170parse_feature(struct lysp_yang_ctx *ctx, struct lysp_feature **features)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004171{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004172 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004173 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004174 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004175 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004176 struct lysp_feature *feat;
4177
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004178 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *features, feat, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004179
4180 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01004181 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02004182 INSERT_WORD_GOTO(ctx, buf, feat->name, word, word_len, ret, cleanup);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01004183
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004184 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004185 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02004186 case LY_STMT_DESCRIPTION:
Michal Vasko72c6d642024-02-27 14:59:01 +01004187 LY_CHECK_RET(parse_text_field(ctx, feat->dsc, LY_STMT_DESCRIPTION, 0, &feat->dsc, Y_STR_ARG, NULL, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004188 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004189 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01004190 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &feat->iffeatures, Y_STR_ARG, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004191 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004192 case LY_STMT_REFERENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01004193 LY_CHECK_RET(parse_text_field(ctx, feat->ref, LY_STMT_REFERENCE, 0, &feat->ref, Y_STR_ARG, NULL, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004194 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004195 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01004196 LY_CHECK_RET(parse_status(ctx, &feat->flags, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004197 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004198 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004199 LY_CHECK_RET(parse_ext(ctx, word, word_len, feat, LY_STMT_FEATURE, 0, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004200 break;
4201 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02004202 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "feature");
Radek Krejci2c02f3e2018-10-16 10:54:38 +02004203 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004204 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004205 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, feat->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004206 }
Michal Vasko12ef5362022-09-16 15:13:58 +02004207
4208cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004209 return ret;
4210}
4211
Michal Vaskoea5abea2018-09-18 13:10:54 +02004212/**
4213 * @brief Parse the identity statement.
4214 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004215 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004216 * @param[in,out] identities Identities to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004217 * @return LY_ERR values.
4218 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004219LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02004220parse_identity(struct lysp_yang_ctx *ctx, struct lysp_ident **identities)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004221{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004222 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004223 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004224 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004225 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004226 struct lysp_ident *ident;
4227
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004228 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *identities, ident, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004229
4230 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01004231 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02004232 INSERT_WORD_GOTO(ctx, buf, ident->name, word, word_len, ret, cleanup);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01004233
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004234 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004235 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02004236 case LY_STMT_DESCRIPTION:
Michal Vasko72c6d642024-02-27 14:59:01 +01004237 LY_CHECK_RET(parse_text_field(ctx, ident->dsc, LY_STMT_DESCRIPTION, 0, &ident->dsc, Y_STR_ARG, NULL, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004238 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004239 case LY_STMT_IF_FEATURE:
Radek Krejci335332a2019-09-05 13:03:35 +02004240 PARSER_CHECK_STMTVER2_RET(ctx, "if-feature", "identity");
Radek Krejcifc596f92021-02-26 22:40:26 +01004241 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &ident->iffeatures, Y_STR_ARG, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004242 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004243 case LY_STMT_REFERENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01004244 LY_CHECK_RET(parse_text_field(ctx, ident->ref, LY_STMT_REFERENCE, 0, &ident->ref, Y_STR_ARG, NULL, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004245 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004246 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01004247 LY_CHECK_RET(parse_status(ctx, &ident->flags, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004248 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004249 case LY_STMT_BASE:
Michal Vasko8a67eff2021-12-07 14:04:47 +01004250 if (ident->bases && (PARSER_CUR_PMOD(ctx)->version < LYS_VERSION_1_1)) {
Michal Vasko72c6d642024-02-27 14:59:01 +01004251 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG,
4252 "Identity can be derived from multiple base identities only in YANG 1.1 modules");
Radek Krejci10113652018-11-14 16:56:50 +01004253 return LY_EVALID;
4254 }
Radek Krejcifc596f92021-02-26 22:40:26 +01004255 LY_CHECK_RET(parse_text_fields(ctx, LY_STMT_BASE, &ident->bases, Y_PREF_IDENTIF_ARG, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004256 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004257 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004258 LY_CHECK_RET(parse_ext(ctx, word, word_len, ident, LY_STMT_IDENTITY, 0, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004259 break;
4260 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02004261 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "identity");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004262 return LY_EVALID;
4263 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004264 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, ident->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004265 }
Michal Vasko12ef5362022-09-16 15:13:58 +02004266
4267cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004268 return ret;
4269}
4270
Michal Vaskoea5abea2018-09-18 13:10:54 +02004271/**
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004272 * @brief Parse module substatements.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004273 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004274 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004275 * @param[in,out] mod Module to write to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004276 * @return LY_ERR values.
4277 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004278LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02004279parse_module(struct lysp_yang_ctx *ctx, struct lysp_module *mod)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004280{
4281 LY_ERR ret = 0;
4282 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004283 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004284 enum ly_stmt kw, prev_kw = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004285 enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER;
Michal Vasko8dc31992021-02-22 10:30:47 +01004286 const struct lysp_submodule *dup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004287
Michal Vaskoc3781c32020-10-06 14:04:08 +02004288 mod->is_submod = 0;
4289
4290 /* module name */
Radek Krejci33090f92020-12-17 20:12:46 +01004291 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02004292 INSERT_WORD_GOTO(ctx, buf, mod->mod->name, word, word_len, ret, cleanup);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01004293
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004294 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004295
Radek Krejcie3846472018-10-15 15:24:51 +02004296#define CHECK_ORDER(SECTION) \
Michal Vasko193dacd2022-10-13 08:43:05 +02004297 if (mod_stmt > SECTION) {\
4298 LOGVAL_PARSER(ctx, LY_VCODE_INORD, lyplg_ext_stmt2str(kw), lyplg_ext_stmt2str(prev_kw)); return LY_EVALID;\
4299 } mod_stmt = SECTION
Radek Krejcie3846472018-10-15 15:24:51 +02004300
Michal Vasko7fbc8162018-09-17 10:35:16 +02004301 switch (kw) {
4302 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004303 case LY_STMT_NAMESPACE:
4304 case LY_STMT_PREFIX:
Radek Krejcie3846472018-10-15 15:24:51 +02004305 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4306 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004307 case LY_STMT_YANG_VERSION:
Radek Krejcie3846472018-10-15 15:24:51 +02004308 CHECK_ORDER(Y_MOD_MODULE_HEADER);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004309 break;
4310 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004311 case LY_STMT_INCLUDE:
4312 case LY_STMT_IMPORT:
Radek Krejcie3846472018-10-15 15:24:51 +02004313 CHECK_ORDER(Y_MOD_LINKAGE);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004314 break;
4315 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004316 case LY_STMT_ORGANIZATION:
4317 case LY_STMT_CONTACT:
4318 case LY_STMT_DESCRIPTION:
4319 case LY_STMT_REFERENCE:
Radek Krejcie3846472018-10-15 15:24:51 +02004320 CHECK_ORDER(Y_MOD_META);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004321 break;
4322
4323 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004324 case LY_STMT_REVISION:
Radek Krejcie3846472018-10-15 15:24:51 +02004325 CHECK_ORDER(Y_MOD_REVISION);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004326 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004327 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004328 case LY_STMT_ANYDATA:
4329 case LY_STMT_ANYXML:
4330 case LY_STMT_AUGMENT:
4331 case LY_STMT_CHOICE:
4332 case LY_STMT_CONTAINER:
4333 case LY_STMT_DEVIATION:
4334 case LY_STMT_EXTENSION:
4335 case LY_STMT_FEATURE:
4336 case LY_STMT_GROUPING:
4337 case LY_STMT_IDENTITY:
4338 case LY_STMT_LEAF:
4339 case LY_STMT_LEAF_LIST:
4340 case LY_STMT_LIST:
4341 case LY_STMT_NOTIFICATION:
4342 case LY_STMT_RPC:
4343 case LY_STMT_TYPEDEF:
4344 case LY_STMT_USES:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004345 mod_stmt = Y_MOD_BODY;
4346 break;
Michal Vasko59a24f62021-12-14 11:18:32 +01004347 case LY_STMT_EXTENSION_INSTANCE:
4348 /* no place in the statement order defined */
4349 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004350 default:
4351 /* error handled in the next switch */
4352 break;
4353 }
Radek Krejcie3846472018-10-15 15:24:51 +02004354#undef CHECK_ORDER
Michal Vasko7fbc8162018-09-17 10:35:16 +02004355
Radek Krejcie3846472018-10-15 15:24:51 +02004356 prev_kw = kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004357 switch (kw) {
4358 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004359 case LY_STMT_YANG_VERSION:
Michal Vasko193dacd2022-10-13 08:43:05 +02004360 LY_CHECK_RET(parse_yangversion(ctx, mod));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004361 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004362 case LY_STMT_NAMESPACE:
Michal Vasko72c6d642024-02-27 14:59:01 +01004363 LY_CHECK_RET(parse_text_field(ctx, mod, LY_STMT_NAMESPACE, 0, &mod->mod->ns, Y_STR_ARG, NULL, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004364 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004365 case LY_STMT_PREFIX:
Michal Vasko72c6d642024-02-27 14:59:01 +01004366 LY_CHECK_RET(parse_text_field(ctx, mod->mod->prefix, LY_STMT_PREFIX, 0, &mod->mod->prefix, Y_IDENTIF_ARG,
4367 NULL, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004368 break;
4369
4370 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004371 case LY_STMT_INCLUDE:
Radek Krejci33090f92020-12-17 20:12:46 +01004372 LY_CHECK_RET(parse_include(ctx, mod->mod->name, &mod->includes));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004373 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004374 case LY_STMT_IMPORT:
Radek Krejci33090f92020-12-17 20:12:46 +01004375 LY_CHECK_RET(parse_import(ctx, mod->mod->prefix, &mod->imports));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004376 break;
4377
4378 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004379 case LY_STMT_ORGANIZATION:
Michal Vasko72c6d642024-02-27 14:59:01 +01004380 LY_CHECK_RET(parse_text_field(ctx, mod, LY_STMT_ORGANIZATION, 0, &mod->mod->org, Y_STR_ARG, NULL, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004381 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004382 case LY_STMT_CONTACT:
Michal Vasko72c6d642024-02-27 14:59:01 +01004383 LY_CHECK_RET(parse_text_field(ctx, mod, LY_STMT_CONTACT, 0, &mod->mod->contact, Y_STR_ARG, NULL, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004384 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004385 case LY_STMT_DESCRIPTION:
Michal Vasko72c6d642024-02-27 14:59:01 +01004386 LY_CHECK_RET(parse_text_field(ctx, mod->mod->dsc, LY_STMT_DESCRIPTION, 0, &mod->mod->dsc, Y_STR_ARG, NULL,
4387 &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004388 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004389 case LY_STMT_REFERENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01004390 LY_CHECK_RET(parse_text_field(ctx, mod->mod->ref, LY_STMT_REFERENCE, 0, &mod->mod->ref, Y_STR_ARG, NULL,
4391 &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004392 break;
4393
4394 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004395 case LY_STMT_REVISION:
Radek Krejci33090f92020-12-17 20:12:46 +01004396 LY_CHECK_RET(parse_revision(ctx, &mod->revs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004397 break;
4398
4399 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004400 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02004401 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "module");
Radek Krejci0f969882020-08-21 16:56:47 +02004402 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02004403 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01004404 LY_CHECK_RET(parse_any(ctx, kw, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004405 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004406 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01004407 LY_CHECK_RET(parse_choice(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004408 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004409 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01004410 LY_CHECK_RET(parse_container(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004411 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004412 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01004413 LY_CHECK_RET(parse_leaf(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004414 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004415 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01004416 LY_CHECK_RET(parse_leaflist(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004417 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004418 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01004419 LY_CHECK_RET(parse_list(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004420 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004421 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01004422 LY_CHECK_RET(parse_uses(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004423 break;
4424
Radek Krejcid6b76452019-09-03 17:03:03 +02004425 case LY_STMT_AUGMENT:
Radek Krejci33090f92020-12-17 20:12:46 +01004426 LY_CHECK_RET(parse_augment(ctx, NULL, &mod->augments));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004427 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004428 case LY_STMT_DEVIATION:
Radek Krejci33090f92020-12-17 20:12:46 +01004429 LY_CHECK_RET(parse_deviation(ctx, &mod->deviations));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004430 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004431 case LY_STMT_EXTENSION:
Radek Krejci33090f92020-12-17 20:12:46 +01004432 LY_CHECK_RET(parse_extension(ctx, &mod->extensions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004433 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004434 case LY_STMT_FEATURE:
Radek Krejci33090f92020-12-17 20:12:46 +01004435 LY_CHECK_RET(parse_feature(ctx, &mod->features));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004436 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004437 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01004438 LY_CHECK_RET(parse_grouping(ctx, NULL, &mod->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004439 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004440 case LY_STMT_IDENTITY:
Radek Krejci33090f92020-12-17 20:12:46 +01004441 LY_CHECK_RET(parse_identity(ctx, &mod->identities));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004442 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004443 case LY_STMT_NOTIFICATION:
Radek Krejci33090f92020-12-17 20:12:46 +01004444 LY_CHECK_RET(parse_notif(ctx, NULL, &mod->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004445 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004446 case LY_STMT_RPC:
Radek Krejci33090f92020-12-17 20:12:46 +01004447 LY_CHECK_RET(parse_action(ctx, NULL, &mod->rpcs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004448 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004449 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01004450 LY_CHECK_RET(parse_typedef(ctx, NULL, &mod->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004451 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004452 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004453 LY_CHECK_RET(parse_ext(ctx, word, word_len, mod, LY_STMT_MODULE, 0, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004454 break;
4455
4456 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02004457 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "module");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004458 return LY_EVALID;
4459 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004460 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, mod->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004461 }
Radek Krejcie86bf772018-12-14 11:39:53 +01004462
Michal Vasko7fbc8162018-09-17 10:35:16 +02004463 /* mandatory substatements */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004464 if (!mod->mod->ns) {
David Sedlákb3077192019-06-19 10:55:37 +02004465 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "namespace", "module");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004466 return LY_EVALID;
4467 } else if (!mod->mod->prefix) {
David Sedlákb3077192019-06-19 10:55:37 +02004468 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "module");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004469 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004470 }
4471
Radek Krejcie9e987e2018-10-31 12:50:27 +01004472 /* submodules share the namespace with the module names, so there must not be
4473 * a submodule of the same name in the context, no need for revision matching */
Michal Vasko8dc31992021-02-22 10:30:47 +01004474 dup = ly_ctx_get_submodule_latest(PARSER_CTX(ctx), mod->mod->name);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004475 if (dup) {
Radek Krejci854e1552020-12-21 15:05:23 +01004476 LOGVAL_PARSER(ctx, LY_VCODE_NAME2_COL, "module", "submodule", mod->mod->name);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004477 return LY_EVALID;
4478 }
4479
Michal Vasko12ef5362022-09-16 15:13:58 +02004480cleanup:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004481 return ret;
4482}
4483
4484/**
4485 * @brief Parse submodule substatements.
4486 *
4487 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004488 * @param[out] submod Parsed submodule structure.
4489 *
4490 * @return LY_ERR values.
4491 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004492LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02004493parse_submodule(struct lysp_yang_ctx *ctx, struct lysp_submodule *submod)
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004494{
4495 LY_ERR ret = 0;
4496 char *buf, *word;
4497 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004498 enum ly_stmt kw, prev_kw = 0;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004499 enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER;
Michal Vasko8dc31992021-02-22 10:30:47 +01004500 const struct lysp_submodule *dup;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004501
Michal Vaskoc3781c32020-10-06 14:04:08 +02004502 submod->is_submod = 1;
4503
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004504 /* submodule name */
Radek Krejci33090f92020-12-17 20:12:46 +01004505 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02004506 INSERT_WORD_GOTO(ctx, buf, submod->name, word, word_len, ret, cleanup);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004507
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004508 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004509
4510#define CHECK_ORDER(SECTION) \
Michal Vasko193dacd2022-10-13 08:43:05 +02004511 if (mod_stmt > SECTION) {LOGVAL_PARSER(ctx, LY_VCODE_INORD, lyplg_ext_stmt2str(kw), lyplg_ext_stmt2str(prev_kw)); return LY_EVALID;}mod_stmt = SECTION
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004512
4513 switch (kw) {
4514 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004515 case LY_STMT_BELONGS_TO:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004516 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4517 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004518 case LY_STMT_YANG_VERSION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004519 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4520 break;
4521 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004522 case LY_STMT_INCLUDE:
4523 case LY_STMT_IMPORT:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004524 CHECK_ORDER(Y_MOD_LINKAGE);
4525 break;
4526 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004527 case LY_STMT_ORGANIZATION:
4528 case LY_STMT_CONTACT:
4529 case LY_STMT_DESCRIPTION:
4530 case LY_STMT_REFERENCE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004531 CHECK_ORDER(Y_MOD_META);
4532 break;
4533
4534 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004535 case LY_STMT_REVISION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004536 CHECK_ORDER(Y_MOD_REVISION);
4537 break;
4538 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004539 case LY_STMT_ANYDATA:
4540 case LY_STMT_ANYXML:
4541 case LY_STMT_AUGMENT:
4542 case LY_STMT_CHOICE:
4543 case LY_STMT_CONTAINER:
4544 case LY_STMT_DEVIATION:
4545 case LY_STMT_EXTENSION:
4546 case LY_STMT_FEATURE:
4547 case LY_STMT_GROUPING:
4548 case LY_STMT_IDENTITY:
4549 case LY_STMT_LEAF:
4550 case LY_STMT_LEAF_LIST:
4551 case LY_STMT_LIST:
4552 case LY_STMT_NOTIFICATION:
4553 case LY_STMT_RPC:
4554 case LY_STMT_TYPEDEF:
4555 case LY_STMT_USES:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004556 mod_stmt = Y_MOD_BODY;
4557 break;
Michal Vasko59a24f62021-12-14 11:18:32 +01004558 case LY_STMT_EXTENSION_INSTANCE:
4559 /* no place in the statement order defined */
4560 break;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004561 default:
4562 /* error handled in the next switch */
4563 break;
4564 }
4565#undef CHECK_ORDER
4566
4567 prev_kw = kw;
4568 switch (kw) {
4569 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004570 case LY_STMT_YANG_VERSION:
Michal Vasko193dacd2022-10-13 08:43:05 +02004571 LY_CHECK_RET(parse_yangversion(ctx, (struct lysp_module *)submod));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004572 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004573 case LY_STMT_BELONGS_TO:
Michal Vasko193dacd2022-10-13 08:43:05 +02004574 LY_CHECK_RET(parse_belongsto(ctx, submod));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004575 break;
4576
4577 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004578 case LY_STMT_INCLUDE:
Radek Krejcieeee95c2021-01-19 10:57:22 +01004579 if (submod->version == LYS_VERSION_1_1) {
4580 LOGWRN(PARSER_CTX(ctx), "YANG version 1.1 expects all includes in main module, includes in submodules (%s) are not necessary.",
4581 submod->name);
4582 }
Radek Krejci33090f92020-12-17 20:12:46 +01004583 LY_CHECK_RET(parse_include(ctx, submod->name, &submod->includes));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004584 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004585 case LY_STMT_IMPORT:
Radek Krejci33090f92020-12-17 20:12:46 +01004586 LY_CHECK_RET(parse_import(ctx, submod->prefix, &submod->imports));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004587 break;
4588
4589 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004590 case LY_STMT_ORGANIZATION:
Michal Vasko72c6d642024-02-27 14:59:01 +01004591 LY_CHECK_RET(parse_text_field(ctx, submod, LY_STMT_ORGANIZATION, 0, &submod->org, Y_STR_ARG, NULL, &submod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004592 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004593 case LY_STMT_CONTACT:
Michal Vasko72c6d642024-02-27 14:59:01 +01004594 LY_CHECK_RET(parse_text_field(ctx, submod, LY_STMT_CONTACT, 0, &submod->contact, Y_STR_ARG, NULL, &submod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004595 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004596 case LY_STMT_DESCRIPTION:
Michal Vasko72c6d642024-02-27 14:59:01 +01004597 LY_CHECK_RET(parse_text_field(ctx, submod->dsc, LY_STMT_DESCRIPTION, 0, &submod->dsc, Y_STR_ARG, NULL, &submod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004598 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004599 case LY_STMT_REFERENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01004600 LY_CHECK_RET(parse_text_field(ctx, submod->ref, LY_STMT_REFERENCE, 0, &submod->ref, Y_STR_ARG, NULL, &submod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004601 break;
4602
4603 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004604 case LY_STMT_REVISION:
Radek Krejci33090f92020-12-17 20:12:46 +01004605 LY_CHECK_RET(parse_revision(ctx, &submod->revs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004606 break;
4607
4608 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004609 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02004610 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "submodule");
Radek Krejci0f969882020-08-21 16:56:47 +02004611 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02004612 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01004613 LY_CHECK_RET(parse_any(ctx, kw, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004614 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004615 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01004616 LY_CHECK_RET(parse_choice(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004617 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004618 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01004619 LY_CHECK_RET(parse_container(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004620 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004621 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01004622 LY_CHECK_RET(parse_leaf(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004623 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004624 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01004625 LY_CHECK_RET(parse_leaflist(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004626 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004627 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01004628 LY_CHECK_RET(parse_list(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004629 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004630 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01004631 LY_CHECK_RET(parse_uses(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004632 break;
4633
Radek Krejcid6b76452019-09-03 17:03:03 +02004634 case LY_STMT_AUGMENT:
Radek Krejci33090f92020-12-17 20:12:46 +01004635 LY_CHECK_RET(parse_augment(ctx, NULL, &submod->augments));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004636 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004637 case LY_STMT_DEVIATION:
Radek Krejci33090f92020-12-17 20:12:46 +01004638 LY_CHECK_RET(parse_deviation(ctx, &submod->deviations));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004639 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004640 case LY_STMT_EXTENSION:
Radek Krejci33090f92020-12-17 20:12:46 +01004641 LY_CHECK_RET(parse_extension(ctx, &submod->extensions));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004642 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004643 case LY_STMT_FEATURE:
Radek Krejci33090f92020-12-17 20:12:46 +01004644 LY_CHECK_RET(parse_feature(ctx, &submod->features));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004645 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004646 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01004647 LY_CHECK_RET(parse_grouping(ctx, NULL, &submod->groupings));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004648 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004649 case LY_STMT_IDENTITY:
Radek Krejci33090f92020-12-17 20:12:46 +01004650 LY_CHECK_RET(parse_identity(ctx, &submod->identities));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004651 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004652 case LY_STMT_NOTIFICATION:
Radek Krejci33090f92020-12-17 20:12:46 +01004653 LY_CHECK_RET(parse_notif(ctx, NULL, &submod->notifs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004654 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004655 case LY_STMT_RPC:
Radek Krejci33090f92020-12-17 20:12:46 +01004656 LY_CHECK_RET(parse_action(ctx, NULL, &submod->rpcs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004657 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004658 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01004659 LY_CHECK_RET(parse_typedef(ctx, NULL, &submod->typedefs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004660 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004661 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004662 LY_CHECK_RET(parse_ext(ctx, word, word_len, submod, LY_STMT_SUBMODULE, 0, &submod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004663 break;
4664
4665 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02004666 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "submodule");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004667 return LY_EVALID;
4668 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004669 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, submod->exts, ret, cleanup);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004670 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004671
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004672 /* mandatory substatements */
Michal Vaskoc3781c32020-10-06 14:04:08 +02004673 if (!submod->prefix) {
David Sedlákb3077192019-06-19 10:55:37 +02004674 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "belongs-to", "submodule");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004675 return LY_EVALID;
4676 }
4677
4678 /* submodules share the namespace with the module names, so there must not be
4679 * a submodule of the same name in the context, no need for revision matching */
Michal Vasko8dc31992021-02-22 10:30:47 +01004680 dup = ly_ctx_get_submodule_latest(PARSER_CTX(ctx), submod->name);
Michal Vaskoc3781c32020-10-06 14:04:08 +02004681 /* main modules may have different revisions */
4682 if (dup && strcmp(dup->mod->name, submod->mod->name)) {
Radek Krejci854e1552020-12-21 15:05:23 +01004683 LOGVAL_PARSER(ctx, LY_VCODE_NAME_COL, "submodules", dup->name);
Radek Krejcie9e987e2018-10-31 12:50:27 +01004684 return LY_EVALID;
4685 }
4686
Michal Vasko12ef5362022-09-16 15:13:58 +02004687cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004688 return ret;
4689}
4690
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004691/**
4692 * @brief Skip any redundant characters, namely whitespaces and comments.
4693 *
4694 * @param[in] ctx Yang parser context.
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004695 * @return LY_SUCCESS on success.
4696 * @return LY_EVALID on invalid comment.
4697 */
4698static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02004699skip_redundant_chars(struct lysp_yang_ctx *ctx)
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004700{
4701 /* read some trailing spaces, new lines, or comments */
Radek Krejci33090f92020-12-17 20:12:46 +01004702 while (ctx->in->current[0]) {
4703 if (!strncmp(ctx->in->current, "//", 2)) {
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004704 /* one-line comment */
Radek Krejci33090f92020-12-17 20:12:46 +01004705 ly_in_skip(ctx->in, 2);
4706 LY_CHECK_RET(skip_comment(ctx, 1));
4707 } else if (!strncmp(ctx->in->current, "/*", 2)) {
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004708 /* block comment */
Radek Krejci33090f92020-12-17 20:12:46 +01004709 ly_in_skip(ctx->in, 2);
4710 LY_CHECK_RET(skip_comment(ctx, 2));
4711 } else if (isspace(ctx->in->current[0])) {
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004712 /* whitespace */
Radek Krejcidd713ce2021-01-04 23:12:12 +01004713 if (ctx->in->current[0] == '\n') {
4714 LY_IN_NEW_LINE(ctx->in);
4715 }
Radek Krejci33090f92020-12-17 20:12:46 +01004716 ly_in_skip(ctx->in, 1);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004717 } else {
4718 break;
4719 }
4720 }
4721
4722 return LY_SUCCESS;
4723}
4724
Radek Krejcid4557c62018-09-17 11:42:09 +02004725LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02004726yang_parse_submodule(struct lysp_yang_ctx **context, struct ly_ctx *ly_ctx, struct lysp_ctx *main_ctx,
Radek Krejci0f969882020-08-21 16:56:47 +02004727 struct ly_in *in, struct lysp_submodule **submod)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004728{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004729 LY_ERR ret = LY_SUCCESS;
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004730 char *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004731 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004732 enum ly_stmt kw;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004733 struct lysp_submodule *mod_p = NULL;
Michal Vaskoc636ea42022-09-16 10:20:31 +02004734 struct lysf_ctx fctx = {.ctx = ly_ctx};
Michal Vasko7fbc8162018-09-17 10:35:16 +02004735
aPiecek56be92a2021-07-01 07:18:10 +02004736 assert(context && ly_ctx && main_ctx && in && submod);
4737
David Sedlák1b623122019-08-05 15:27:49 +02004738 /* create context */
4739 *context = calloc(1, sizeof **context);
4740 LY_CHECK_ERR_RET(!(*context), LOGMEM(ly_ctx), LY_EMEM);
Michal Vaskob36053d2020-03-26 15:49:30 +01004741 (*context)->format = LYS_IN_YANG;
Radek Krejci33090f92020-12-17 20:12:46 +01004742 (*context)->in = in;
aPiecek56be92a2021-07-01 07:18:10 +02004743 (*context)->main_ctx = main_ctx;
David Sedlák1b623122019-08-05 15:27:49 +02004744
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004745 mod_p = calloc(1, sizeof *mod_p);
4746 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(ly_ctx); ret = LY_EMEM, cleanup);
Michal Vasko8a67eff2021-12-07 14:04:47 +01004747 mod_p->mod = PARSER_CUR_PMOD(main_ctx)->mod;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004748 mod_p->parsing = 1;
Michal Vasko8a67eff2021-12-07 14:04:47 +01004749
4750 /* use main context parsed mods adding the current one */
4751 (*context)->parsed_mods = main_ctx->parsed_mods;
4752 ly_set_add((*context)->parsed_mods, mod_p, 1, NULL);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004753
Michal Vasko7a266772024-01-23 11:02:38 +01004754 ly_log_location(NULL, NULL, NULL, in);
Radek Krejci2efc45b2020-12-22 16:25:44 +01004755
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004756 /* skip redundant but valid characters at the beginning */
Radek Krejci33090f92020-12-17 20:12:46 +01004757 ret = skip_redundant_chars(*context);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004758 LY_CHECK_GOTO(ret, cleanup);
4759
Michal Vasko7fbc8162018-09-17 10:35:16 +02004760 /* "module"/"submodule" */
Radek Krejci33090f92020-12-17 20:12:46 +01004761 ret = get_keyword(*context, &kw, &word, &word_len);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004762 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004763
Radek Krejcid6b76452019-09-03 17:03:03 +02004764 if (kw == LY_STMT_MODULE) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004765 LOGERR(ly_ctx, LY_EDENIED, "Input data contains module in situation when a submodule is expected.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004766 ret = LY_EINVAL;
4767 goto cleanup;
Radek Krejcid6b76452019-09-03 17:03:03 +02004768 } else if (kw != LY_STMT_SUBMODULE) {
Michal Vasko193dacd2022-10-13 08:43:05 +02004769 LOGVAL_PARSER(*context, LY_VCODE_MOD_SUBOMD, lyplg_ext_stmt2str(kw));
Radek Krejci40544fa2019-01-11 09:38:37 +01004770 ret = LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004771 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004772 }
4773
Michal Vasko7fbc8162018-09-17 10:35:16 +02004774 /* substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01004775 ret = parse_submodule(*context, mod_p);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004776 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004777
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004778 /* skip redundant but valid characters at the end */
Radek Krejci33090f92020-12-17 20:12:46 +01004779 ret = skip_redundant_chars(*context);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004780 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko63f3d842020-07-08 10:10:14 +02004781 if (in->current[0]) {
4782 LOGVAL_PARSER(*context, LY_VCODE_TRAILING_SUBMOD, 15, in->current, strlen(in->current) > 15 ? "..." : "");
Radek Krejci40544fa2019-01-11 09:38:37 +01004783 ret = LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004784 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004785 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004786
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004787 mod_p->parsing = 0;
4788 *submod = mod_p;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004789
Radek Krejcibbe09a92018-11-08 09:36:54 +01004790cleanup:
Michal Vasko7a266772024-01-23 11:02:38 +01004791 ly_log_location_revert(0, 0, 0, 1);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004792 if (ret) {
Michal Vaskoc636ea42022-09-16 10:20:31 +02004793 lysp_module_free(&fctx, (struct lysp_module *)mod_p);
Michal Vaskod0625d72022-10-06 15:02:50 +02004794 lysp_yang_ctx_free(*context);
David Sedlák1b623122019-08-05 15:27:49 +02004795 *context = NULL;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004796 }
4797
4798 return ret;
4799}
4800
4801LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02004802yang_parse_module(struct lysp_yang_ctx **context, struct ly_in *in, struct lys_module *mod)
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004803{
4804 LY_ERR ret = LY_SUCCESS;
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004805 char *word;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004806 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004807 enum ly_stmt kw;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004808 struct lysp_module *mod_p = NULL;
Michal Vaskoc636ea42022-09-16 10:20:31 +02004809 struct lysf_ctx fctx = {.ctx = mod->ctx};
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004810
David Sedlák1b623122019-08-05 15:27:49 +02004811 /* create context */
4812 *context = calloc(1, sizeof **context);
4813 LY_CHECK_ERR_RET(!(*context), LOGMEM(mod->ctx), LY_EMEM);
Michal Vaskob36053d2020-03-26 15:49:30 +01004814 (*context)->format = LYS_IN_YANG;
Michal Vasko8a67eff2021-12-07 14:04:47 +01004815 LY_CHECK_ERR_RET(ly_set_new(&(*context)->parsed_mods), free(*context); LOGMEM(mod->ctx), LY_EMEM);
Radek Krejci33090f92020-12-17 20:12:46 +01004816 (*context)->in = in;
Michal Vaskod0625d72022-10-06 15:02:50 +02004817 (*context)->main_ctx = (struct lysp_ctx *)(*context);
David Sedlák1b623122019-08-05 15:27:49 +02004818
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004819 mod_p = calloc(1, sizeof *mod_p);
4820 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(mod->ctx), cleanup);
4821 mod_p->mod = mod;
Michal Vasko8a67eff2021-12-07 14:04:47 +01004822 ly_set_add((*context)->parsed_mods, mod_p, 1, NULL);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004823
Michal Vasko7a266772024-01-23 11:02:38 +01004824 ly_log_location(NULL, NULL, NULL, in);
Radek Krejci2efc45b2020-12-22 16:25:44 +01004825
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004826 /* skip redundant but valid characters at the beginning */
Radek Krejci33090f92020-12-17 20:12:46 +01004827 ret = skip_redundant_chars(*context);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004828 LY_CHECK_GOTO(ret, cleanup);
4829
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004830 /* "module"/"submodule" */
Radek Krejci33090f92020-12-17 20:12:46 +01004831 ret = get_keyword(*context, &kw, &word, &word_len);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004832 LY_CHECK_GOTO(ret, cleanup);
4833
Radek Krejcid6b76452019-09-03 17:03:03 +02004834 if (kw == LY_STMT_SUBMODULE) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004835 LOGERR(mod->ctx, LY_EDENIED, "Input data contains submodule which cannot be parsed directly without its main module.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004836 ret = LY_EINVAL;
4837 goto cleanup;
Radek Krejcid6b76452019-09-03 17:03:03 +02004838 } else if (kw != LY_STMT_MODULE) {
Michal Vasko193dacd2022-10-13 08:43:05 +02004839 LOGVAL_PARSER((*context), LY_VCODE_MOD_SUBOMD, lyplg_ext_stmt2str(kw));
Radek Krejci40544fa2019-01-11 09:38:37 +01004840 ret = LY_EVALID;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004841 goto cleanup;
4842 }
4843
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004844 /* substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01004845 ret = parse_module(*context, mod_p);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004846 LY_CHECK_GOTO(ret, cleanup);
4847
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004848 /* skip redundant but valid characters at the end */
Radek Krejci33090f92020-12-17 20:12:46 +01004849 ret = skip_redundant_chars(*context);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004850 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko63f3d842020-07-08 10:10:14 +02004851 if (in->current[0]) {
4852 LOGVAL_PARSER(*context, LY_VCODE_TRAILING_MOD, 15, in->current, strlen(in->current) > 15 ? "..." : "");
Radek Krejci40544fa2019-01-11 09:38:37 +01004853 ret = LY_EVALID;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004854 goto cleanup;
4855 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004856
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004857 mod->parsed = mod_p;
4858
4859cleanup:
Michal Vasko7a266772024-01-23 11:02:38 +01004860 ly_log_location_revert(0, 0, 0, 1);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004861 if (ret) {
Michal Vaskoc636ea42022-09-16 10:20:31 +02004862 lysp_module_free(&fctx, mod_p);
Michal Vaskod0625d72022-10-06 15:02:50 +02004863 lysp_yang_ctx_free(*context);
David Sedlák1b623122019-08-05 15:27:49 +02004864 *context = NULL;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004865 }
4866
Michal Vasko7fbc8162018-09-17 10:35:16 +02004867 return ret;
4868}