blob: 9fef72a3fccac3afd060c01673357063951fcec4 [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);
Michal Vaskof1918e12024-07-15 10:14:17 +0200841 /* fall through */
842 case '\0':
Radek Krejci33090f92020-12-17 20:12:46 +0100843 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, (int)(ctx->in->current - word_start), word_start,
Michal Vasko69730152020-10-09 16:30:07 +0200844 "a keyword followed by a separator");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200845 return LY_EVALID;
846 }
847 } else {
848 /* still can be an extension */
849 prefix = 0;
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200850
Radek Krejci156ccaf2018-10-15 15:49:17 +0200851extension:
Radek Krejcid54412f2020-12-17 20:25:35 +0100852 while (ctx->in->current[0] && (ctx->in->current[0] != ' ') && (ctx->in->current[0] != '\t') &&
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200853 (ctx->in->current[0] != '\n') && (ctx->in->current[0] != '\r') && (ctx->in->current[0] != '{') &&
854 (ctx->in->current[0] != ';')) {
Radek Krejci1deb5be2020-08-26 16:43:36 +0200855 uint32_t c = 0;
856
Radek Krejci33090f92020-12-17 20:12:46 +0100857 LY_CHECK_ERR_RET(ly_getutf8(&ctx->in->current, &c, &len),
858 LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, ctx->in->current[-len]), LY_EVALID);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200859 ++ctx->indent;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200860 /* check character validity */
Michal Vaskod0625d72022-10-06 15:02:50 +0200861 LY_CHECK_RET(lysp_check_identifierchar((struct lysp_ctx *)ctx, c,
Radek Krejci33090f92020-12-17 20:12:46 +0100862 ctx->in->current - len == word_start ? 1 : 0, &prefix));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200863 }
Radek Krejci33090f92020-12-17 20:12:46 +0100864 if (!ctx->in->current[0]) {
David Sedlákb3077192019-06-19 10:55:37 +0200865 LOGVAL_PARSER(ctx, LY_VCODE_EOF);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200866 return LY_EVALID;
867 }
868
869 /* prefix is mandatory for extension instances */
Radek Krejcidcc7b322018-10-11 14:24:02 +0200870 if (prefix != 2) {
Radek Krejci33090f92020-12-17 20:12:46 +0100871 LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, (int)(ctx->in->current - word_start), word_start, "a keyword");
Michal Vasko7fbc8162018-09-17 10:35:16 +0200872 return LY_EVALID;
873 }
874
Radek Krejcid6b76452019-09-03 17:03:03 +0200875 *kw = LY_STMT_EXTENSION_INSTANCE;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200876 }
Michal Vaskoa6ecd0c2021-04-09 11:58:26 +0200877
Radek Krejci626df482018-10-11 15:06:31 +0200878success:
Michal Vasko7fbc8162018-09-17 10:35:16 +0200879 if (word_p) {
880 *word_p = (char *)word_start;
Radek Krejci33090f92020-12-17 20:12:46 +0100881 *word_len = ctx->in->current - word_start;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200882 }
883
884 return LY_SUCCESS;
885}
886
Michal Vaskoea5abea2018-09-18 13:10:54 +0200887/**
888 * @brief Parse extension instance substatements.
889 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200890 * @param[in] ctx yang parser context for logging.
Radek Krejci335332a2019-09-05 13:03:35 +0200891 * @param[in] kw Statement keyword value matching @p word value.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200892 * @param[in] word Extension instance substatement name (keyword).
893 * @param[in] word_len Extension instance substatement name length.
894 * @param[in,out] child Children of this extension instance to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200895 * @return LY_ERR values.
896 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200897static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +0200898parse_ext_substmt(struct lysp_yang_ctx *ctx, enum ly_stmt kw, char *word, size_t word_len,
Radek Krejci0f969882020-08-21 16:56:47 +0200899 struct lysp_stmt **child)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200900{
901 char *buf;
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100902 LY_ERR ret = LY_SUCCESS;
Radek Krejci335332a2019-09-05 13:03:35 +0200903 enum ly_stmt child_kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200904 struct lysp_stmt *stmt, *par_child;
905
906 stmt = calloc(1, sizeof *stmt);
907 LY_CHECK_ERR_RET(!stmt, LOGMEM(NULL), LY_EMEM);
908
Radek Krejcibb9b1982019-04-08 14:24:59 +0200909 /* insert into parent statements */
910 if (!*child) {
911 *child = stmt;
912 } else {
Radek Krejci1e008d22020-08-17 11:37:37 +0200913 for (par_child = *child; par_child->next; par_child = par_child->next) {}
Radek Krejcibb9b1982019-04-08 14:24:59 +0200914 par_child->next = stmt;
915 }
916
Michal Vaskofc2cd072021-02-24 13:17:17 +0100917 /* statement */
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200918 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), word, word_len, &stmt->stmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200919
920 /* get optional argument */
Radek Krejci33090f92020-12-17 20:12:46 +0100921 LY_CHECK_RET(get_argument(ctx, Y_MAYBE_STR_ARG, &stmt->flags, &word, &buf, &word_len));
Radek Krejci0ae092d2018-09-20 16:43:19 +0200922 if (word) {
Michal Vasko12ef5362022-09-16 15:13:58 +0200923 INSERT_WORD_GOTO(ctx, buf, stmt->arg, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200924 }
925
Radek Krejci8df109d2021-04-23 12:19:08 +0200926 stmt->format = LY_VALUE_SCHEMA;
Michal Vasko8a67eff2021-12-07 14:04:47 +0100927 stmt->prefix_data = PARSER_CUR_PMOD(ctx);
Michal Vaskofc2cd072021-02-24 13:17:17 +0100928 stmt->kw = kw;
929
Michal Vaskoc0c64ae2022-10-06 10:15:23 +0200930 YANG_READ_SUBSTMT_FOR_GOTO(ctx, child_kw, word, word_len, ret, cleanup) {
931 LY_CHECK_GOTO(ret = parse_ext_substmt(ctx, child_kw, word, word_len, &stmt->child), cleanup)
932 YANG_READ_SUBSTMT_NEXT_ITER(ctx, child_kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200933 }
Michal Vasko12ef5362022-09-16 15:13:58 +0200934
935cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +0200936 return ret;
937}
938
Michal Vaskoea5abea2018-09-18 13:10:54 +0200939/**
940 * @brief Parse extension instance.
941 *
Radek Krejci44ceedc2018-10-02 15:54:31 +0200942 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200943 * @param[in] ext_name Extension instance substatement name (keyword).
944 * @param[in] ext_name_len Extension instance substatement name length.
Michal Vasko193dacd2022-10-13 08:43:05 +0200945 * @param[in] parent Current statement parent.
946 * @param[in] parent_stmt Type of @p parent statement.
947 * @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 +0200948 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200949 * @return LY_ERR values.
950 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200951static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +0200952parse_ext(struct lysp_yang_ctx *ctx, const char *ext_name, size_t ext_name_len, const void *parent,
953 enum ly_stmt parent_stmt, LY_ARRAY_COUNT_TYPE parent_stmt_index, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +0200954{
Radek Krejci6d9b9b52018-11-02 12:43:39 +0100955 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200956 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +0200957 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200958 struct lysp_ext_instance *e;
Radek Krejcid6b76452019-09-03 17:03:03 +0200959 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +0200960
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200961 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *exts, e, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200962
Michal Vaskofc2cd072021-02-24 13:17:17 +0100963 if (!ly_strnchr(ext_name, ':', ext_name_len)) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +0200964 LOGVAL_PARSER(ctx, LYVE_SYNTAX, "Extension instance \"%.*s\" without the mandatory prefix.",
965 (int)ext_name_len, ext_name);
Michal Vaskofc2cd072021-02-24 13:17:17 +0100966 return LY_EVALID;
967 }
968
969 /* store name */
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200970 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), ext_name, ext_name_len, &e->name));
Michal Vasko7fbc8162018-09-17 10:35:16 +0200971
972 /* get optional argument */
Radek Krejci33090f92020-12-17 20:12:46 +0100973 LY_CHECK_RET(get_argument(ctx, Y_MAYBE_STR_ARG, NULL, &word, &buf, &word_len));
Radek Krejci0ae092d2018-09-20 16:43:19 +0200974 if (word) {
Michal Vasko12ef5362022-09-16 15:13:58 +0200975 INSERT_WORD_GOTO(ctx, buf, e->argument, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200976 }
977
Michal Vaskofc2cd072021-02-24 13:17:17 +0100978 /* store the rest of information */
Radek Krejci8df109d2021-04-23 12:19:08 +0200979 e->format = LY_VALUE_SCHEMA;
aPiecek60d9d672021-04-27 15:49:57 +0200980 e->parsed = NULL;
Michal Vasko8a67eff2021-12-07 14:04:47 +0100981 e->prefix_data = PARSER_CUR_PMOD(ctx);
Michal Vasko193dacd2022-10-13 08:43:05 +0200982 e->parent = (void *)parent;
983 e->parent_stmt = parent_stmt;
984 e->parent_stmt_index = parent_stmt_index;
Michal Vaskofc2cd072021-02-24 13:17:17 +0100985
Michal Vaskoc0c64ae2022-10-06 10:15:23 +0200986 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko35274cf2024-07-04 12:01:00 +0200987 switch (kw) {
988 case LY_STMT_EXTENSION_INSTANCE:
989 LY_CHECK_GOTO(parse_ext(ctx, word, word_len, e, LY_STMT_EXTENSION_INSTANCE, 0, &e->exts), cleanup);
990 break;
991 default:
992 /* just store all the statements */
993 LY_CHECK_GOTO(ret = parse_ext_substmt(ctx, kw, word, word_len, &e->child), cleanup)
994 break;
995 }
996 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, e->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +0200997 }
Michal Vasko12ef5362022-09-16 15:13:58 +0200998
999cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001000 return ret;
1001}
1002
Michal Vaskoea5abea2018-09-18 13:10:54 +02001003/**
1004 * @brief Parse a generic text field without specific constraints. Those are contact, organization,
1005 * description, etc...
1006 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001007 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02001008 * @param[in] parent Current statement parent.
1009 * @param[in] parent_stmt Type of statement in @p value.
1010 * @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 +02001011 * @param[in,out] value Place to store the parsed value.
1012 * @param[in] arg Type of the YANG keyword argument (of the value).
Michal Vasko72c6d642024-02-27 14:59:01 +01001013 * @param[out] flags Optional output argument to get flag of the argument's quoting (LYS_*QUOTED - see
1014 * [schema node flags](@ref snodeflags))
Michal Vaskoea5abea2018-09-18 13:10:54 +02001015 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001016 * @return LY_ERR values.
1017 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001018static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02001019parse_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 +01001020 const char **value, enum yang_arg arg, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001021{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001022 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001023 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001024 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001025 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001026
1027 if (*value) {
Michal Vasko193dacd2022-10-13 08:43:05 +02001028 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, lyplg_ext_stmt2str(parent_stmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001029 return LY_EVALID;
1030 }
1031
1032 /* get value */
Michal Vasko72c6d642024-02-27 14:59:01 +01001033 LY_CHECK_RET(get_argument(ctx, arg, flags, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001034
1035 /* store value and spend buf if allocated */
Michal Vasko12ef5362022-09-16 15:13:58 +02001036 INSERT_WORD_GOTO(ctx, buf, *value, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001037
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001038 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001039 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001040 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001041 LY_CHECK_RET(parse_ext(ctx, word, word_len, parent, parent_stmt, parent_stmt_index, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001042 break;
1043 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001044 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), lyplg_ext_stmt2str(parent_stmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001045 return LY_EVALID;
1046 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001047 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001048 }
Michal Vasko12ef5362022-09-16 15:13:58 +02001049
1050cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001051 return ret;
1052}
1053
Michal Vaskoea5abea2018-09-18 13:10:54 +02001054/**
1055 * @brief Parse the yang-version statement.
1056 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001057 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02001058 * @param[in,out] mod Module to fill.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001059 * @return LY_ERR values.
1060 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001061static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02001062parse_yangversion(struct lysp_yang_ctx *ctx, struct lysp_module *mod)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001063{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001064 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001065 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001066 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001067 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001068
Michal Vasko193dacd2022-10-13 08:43:05 +02001069 if (mod->version) {
David Sedlákb3077192019-06-19 10:55:37 +02001070 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001071 return LY_EVALID;
1072 }
1073
1074 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001075 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001076
Radek Krejci96e48da2020-09-04 13:18:06 +02001077 if ((word_len == 1) && !strncmp(word, "1", word_len)) {
Michal Vasko193dacd2022-10-13 08:43:05 +02001078 mod->version = LYS_VERSION_1_0;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001079 } else if ((word_len == ly_strlen_const("1.1")) && !strncmp(word, "1.1", word_len)) {
Michal Vasko193dacd2022-10-13 08:43:05 +02001080 mod->version = LYS_VERSION_1_1;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001081 } else {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02001082 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001083 free(buf);
1084 return LY_EVALID;
1085 }
1086 free(buf);
1087
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001088 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001089 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001090 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001091 LY_CHECK_RET(parse_ext(ctx, word, word_len, mod, LY_STMT_YANG_VERSION, 0, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001092 break;
1093 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001094 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001095 return LY_EVALID;
1096 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001097 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001098 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001099
1100cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001101 return ret;
1102}
1103
Michal Vaskoea5abea2018-09-18 13:10:54 +02001104/**
1105 * @brief Parse the belongs-to statement.
1106 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001107 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02001108 * @param[in,out] submod Submodule to fill.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001109 * @return LY_ERR values.
1110 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001111static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02001112parse_belongsto(struct lysp_yang_ctx *ctx, struct lysp_submodule *submod)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001113{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001114 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001115 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001116 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001117 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001118
Michal Vasko193dacd2022-10-13 08:43:05 +02001119 if (submod->prefix) {
David Sedlákb3077192019-06-19 10:55:37 +02001120 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001121 return LY_EVALID;
1122 }
1123
Michal Vaskoc3781c32020-10-06 14:04:08 +02001124 /* get value, it must match the main module */
Radek Krejci33090f92020-12-17 20:12:46 +01001125 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko8a67eff2021-12-07 14:04:47 +01001126 if (ly_strncmp(PARSER_CUR_PMOD(ctx)->mod->name, word, word_len)) {
Michal Vaskoc3781c32020-10-06 14:04:08 +02001127 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 +01001128 (int)word_len, word, PARSER_CUR_PMOD(ctx)->mod->name);
Michal Vaskoc3781c32020-10-06 14:04:08 +02001129 free(buf);
1130 return LY_EVALID;
1131 }
1132 free(buf);
Radek Krejcif09e4e82019-06-14 15:08:11 +02001133
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001134 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001135 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001136 case LY_STMT_PREFIX:
Michal Vasko72c6d642024-02-27 14:59:01 +01001137 LY_CHECK_RET(parse_text_field(ctx, submod->prefix, LY_STMT_PREFIX, 0, &submod->prefix, Y_IDENTIF_ARG, NULL,
1138 &submod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001139 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001140 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001141 LY_CHECK_RET(parse_ext(ctx, word, word_len, submod, LY_STMT_BELONGS_TO, 0, &submod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001142 break;
1143 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001144 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001145 return LY_EVALID;
1146 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001147 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001148 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001149
Michal Vasko7fbc8162018-09-17 10:35:16 +02001150 /* mandatory substatements */
Michal Vasko193dacd2022-10-13 08:43:05 +02001151 if (!submod->prefix) {
David Sedlákb3077192019-06-19 10:55:37 +02001152 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001153 return LY_EVALID;
1154 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001155
1156cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001157 return ret;
1158}
1159
Michal Vaskoea5abea2018-09-18 13:10:54 +02001160/**
1161 * @brief Parse the revision-date statement.
1162 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001163 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02001164 * @param[in,out] rev Buffer to store the parsed value in.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001165 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001166 * @return LY_ERR values.
1167 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001168static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001169parse_revisiondate(struct lysp_yang_ctx *ctx, char *rev, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001170{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001171 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001172 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001173 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001174 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001175
1176 if (rev[0]) {
David Sedlákb3077192019-06-19 10:55:37 +02001177 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "revision-date");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001178 return LY_EVALID;
1179 }
1180
1181 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001182 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001183
1184 /* check value */
Michal Vaskod0625d72022-10-06 15:02:50 +02001185 if (lysp_check_date((struct lysp_ctx *)ctx, word, word_len, "revision-date")) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001186 free(buf);
1187 return LY_EVALID;
1188 }
1189
1190 /* store value and spend buf if allocated */
1191 strncpy(rev, word, word_len);
1192 free(buf);
1193
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001194 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001195 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001196 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001197 LY_CHECK_RET(parse_ext(ctx, word, word_len, rev, LY_STMT_REVISION_DATE, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001198 break;
1199 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001200 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "revision-date");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001201 return LY_EVALID;
1202 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001203 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001204 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001205
1206cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001207 return ret;
1208}
1209
Michal Vaskoea5abea2018-09-18 13:10:54 +02001210/**
1211 * @brief Parse the include statement.
1212 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001213 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001214 * @param[in] module_name Name of the module to check name collisions.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001215 * @param[in,out] includes Parsed includes to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001216 * @return LY_ERR values.
1217 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001218static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001219parse_include(struct lysp_yang_ctx *ctx, const char *module_name, struct lysp_include **includes)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001220{
Radek Krejcid33273d2018-10-25 14:55:52 +02001221 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001222 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001223 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001224 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001225 struct lysp_include *inc;
1226
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001227 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *includes, inc, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001228
1229 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001230 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001231
Michal Vasko12ef5362022-09-16 15:13:58 +02001232 INSERT_WORD_GOTO(ctx, buf, inc->name, word, word_len, ret, cleanup);
Radek Krejci086c7132018-10-26 15:29:04 +02001233
Radek Krejcifaa1eac2018-10-30 14:34:55 +01001234 /* submodules share the namespace with the module names, so there must not be
1235 * a module of the same name in the context, no need for revision matching */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001236 if (!strcmp(module_name, inc->name) || ly_ctx_get_module_latest(PARSER_CTX(ctx), inc->name)) {
Radek Krejci854e1552020-12-21 15:05:23 +01001237 LOGVAL_PARSER(ctx, LY_VCODE_NAME2_COL, "module", "submodule", inc->name);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01001238 return LY_EVALID;
1239 }
1240
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001241 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001242 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001243 case LY_STMT_DESCRIPTION:
Radek Krejci335332a2019-09-05 13:03:35 +02001244 PARSER_CHECK_STMTVER2_RET(ctx, "description", "include");
Michal Vasko72c6d642024-02-27 14:59:01 +01001245 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 +02001246 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001247 case LY_STMT_REFERENCE:
Radek Krejci335332a2019-09-05 13:03:35 +02001248 PARSER_CHECK_STMTVER2_RET(ctx, "reference", "include");
Michal Vasko72c6d642024-02-27 14:59:01 +01001249 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 +02001250 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001251 case LY_STMT_REVISION_DATE:
Radek Krejci33090f92020-12-17 20:12:46 +01001252 LY_CHECK_RET(parse_revisiondate(ctx, inc->rev, &inc->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001253 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001254 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001255 LY_CHECK_RET(parse_ext(ctx, word, word_len, inc, LY_STMT_INCLUDE, 0, &inc->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001256 break;
1257 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001258 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "include");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001259 return LY_EVALID;
1260 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001261 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, inc->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001262 }
Michal Vasko12ef5362022-09-16 15:13:58 +02001263
1264cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001265 return ret;
1266}
1267
Michal Vaskoea5abea2018-09-18 13:10:54 +02001268/**
1269 * @brief Parse the import statement.
1270 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001271 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001272 * @param[in] module_prefix Prefix of the module to check prefix collisions.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001273 * @param[in,out] imports Parsed imports to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001274 * @return LY_ERR values.
1275 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001276static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001277parse_import(struct lysp_yang_ctx *ctx, const char *module_prefix, struct lysp_import **imports)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001278{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001279 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001280 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001281 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001282 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001283 struct lysp_import *imp;
1284
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001285 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *imports, imp, LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001286
1287 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001288 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02001289 INSERT_WORD_GOTO(ctx, buf, imp->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001290
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001291 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001292 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001293 case LY_STMT_PREFIX:
Michal Vasko72c6d642024-02-27 14:59:01 +01001294 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 +02001295 LY_CHECK_RET(lysp_check_prefix((struct lysp_ctx *)ctx, *imports, module_prefix, &imp->prefix), LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001296 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001297 case LY_STMT_DESCRIPTION:
Radek Krejci335332a2019-09-05 13:03:35 +02001298 PARSER_CHECK_STMTVER2_RET(ctx, "description", "import");
Michal Vasko72c6d642024-02-27 14:59:01 +01001299 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 +02001300 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001301 case LY_STMT_REFERENCE:
Radek Krejci335332a2019-09-05 13:03:35 +02001302 PARSER_CHECK_STMTVER2_RET(ctx, "reference", "import");
Michal Vasko72c6d642024-02-27 14:59:01 +01001303 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 +02001304 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001305 case LY_STMT_REVISION_DATE:
Radek Krejci33090f92020-12-17 20:12:46 +01001306 LY_CHECK_RET(parse_revisiondate(ctx, imp->rev, &imp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001307 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001308 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001309 LY_CHECK_RET(parse_ext(ctx, word, word_len, imp, LY_STMT_IMPORT, 0, &imp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001310 break;
1311 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001312 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "import");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001313 return LY_EVALID;
1314 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001315 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, imp->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001316 }
Michal Vasko12ef5362022-09-16 15:13:58 +02001317
Michal Vasko7fbc8162018-09-17 10:35:16 +02001318 /* mandatory substatements */
David Sedlákb3077192019-06-19 10:55:37 +02001319 LY_CHECK_ERR_RET(!imp->prefix, LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "import"), LY_EVALID);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001320
Michal Vasko12ef5362022-09-16 15:13:58 +02001321cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001322 return ret;
1323}
1324
Michal Vaskoea5abea2018-09-18 13:10:54 +02001325/**
1326 * @brief Parse the revision statement.
1327 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001328 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001329 * @param[in,out] revs Parsed revisions to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001330 * @return LY_ERR values.
1331 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001332static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001333parse_revision(struct lysp_yang_ctx *ctx, struct lysp_revision **revs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001334{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001335 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001336 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001337 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001338 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001339 struct lysp_revision *rev;
1340
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001341 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *revs, rev, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001342
1343 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001344 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001345
1346 /* check value */
Michal Vaskod0625d72022-10-06 15:02:50 +02001347 if (lysp_check_date((struct lysp_ctx *)ctx, word, word_len, "revision")) {
David Sedlák68ef3dc2019-07-22 13:40:19 +02001348 free(buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001349 return LY_EVALID;
1350 }
1351
Radek Krejcib7db73a2018-10-24 14:18:40 +02001352 strncpy(rev->date, word, word_len);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001353 free(buf);
1354
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001355 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001356 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001357 case LY_STMT_DESCRIPTION:
Michal Vasko72c6d642024-02-27 14:59:01 +01001358 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 +02001359 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001360 case LY_STMT_REFERENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01001361 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 +02001362 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001363 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001364 LY_CHECK_RET(parse_ext(ctx, word, word_len, rev, LY_STMT_REVISION, 0, &rev->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001365 break;
1366 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001367 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "revision");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001368 return LY_EVALID;
1369 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001370 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, rev->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001371 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001372
1373cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001374 return ret;
1375}
1376
Michal Vaskoea5abea2018-09-18 13:10:54 +02001377/**
1378 * @brief Parse a generic text field that can have more instances such as base.
1379 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001380 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02001381 * @param[in] parent Current statement parent.
1382 * @param[in] parent_stmt Type of @p parent statement.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001383 * @param[in,out] texts Parsed values to add to.
1384 * @param[in] arg Type of the expected argument.
1385 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001386 * @return LY_ERR values.
1387 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001388static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02001389parse_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 +02001390 struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001391{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001392 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001393 char *buf, *word;
Radek Krejci151a5b72018-10-19 14:21:44 +02001394 const char **item;
1395 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001396 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001397
1398 /* allocate new pointer */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001399 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *texts, item, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001400
1401 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001402 LY_CHECK_RET(get_argument(ctx, arg, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001403
Michal Vasko12ef5362022-09-16 15:13:58 +02001404 INSERT_WORD_GOTO(ctx, buf, *item, word, word_len, ret, cleanup);
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001405 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001406 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001407 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001408 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 +02001409 break;
1410 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001411 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), lyplg_ext_stmt2str(parent_stmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001412 return LY_EVALID;
1413 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001414 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001415 }
Michal Vasko12ef5362022-09-16 15:13:58 +02001416
1417cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001418 return ret;
1419}
1420
Michal Vaskoea5abea2018-09-18 13:10:54 +02001421/**
Michal Vasko7f45cf22020-10-01 12:49:44 +02001422 * @brief Parse a generic text field that can have more instances such as base.
1423 *
1424 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02001425 * @param[in] parent_stmt Type of statement stored in @p qnames.
Michal Vasko7f45cf22020-10-01 12:49:44 +02001426 * @param[in,out] qnames Parsed qnames to add to.
1427 * @param[in] arg Type of the expected argument.
1428 * @param[in,out] exts Extension instances to add to.
Michal Vasko7f45cf22020-10-01 12:49:44 +02001429 * @return LY_ERR values.
1430 */
1431static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02001432parse_qnames(struct lysp_yang_ctx *ctx, enum ly_stmt parent_stmt, struct lysp_qname **qnames, enum yang_arg arg,
1433 struct lysp_ext_instance **exts)
Michal Vasko7f45cf22020-10-01 12:49:44 +02001434{
1435 LY_ERR ret = LY_SUCCESS;
1436 char *buf, *word;
1437 struct lysp_qname *item;
1438 size_t word_len;
1439 enum ly_stmt kw;
1440
1441 /* allocate new pointer */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001442 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *qnames, item, LY_EMEM);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001443
1444 /* get value */
Michal Vasko72c6d642024-02-27 14:59:01 +01001445 LY_CHECK_RET(get_argument(ctx, arg, &item->flags, &word, &buf, &word_len));
Michal Vasko7f45cf22020-10-01 12:49:44 +02001446
Michal Vasko12ef5362022-09-16 15:13:58 +02001447 INSERT_WORD_GOTO(ctx, buf, item->str, word, word_len, ret, cleanup);
Michal Vasko8a67eff2021-12-07 14:04:47 +01001448 item->mod = PARSER_CUR_PMOD(ctx);
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001449 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7f45cf22020-10-01 12:49:44 +02001450 switch (kw) {
1451 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001452 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 +02001453 break;
1454 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001455 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), lyplg_ext_stmt2str(parent_stmt));
Michal Vasko7f45cf22020-10-01 12:49:44 +02001456 return LY_EVALID;
1457 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001458 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001459 }
Michal Vasko12ef5362022-09-16 15:13:58 +02001460
1461cleanup:
Michal Vasko7f45cf22020-10-01 12:49:44 +02001462 return ret;
1463}
1464
1465/**
Michal Vaskoea5abea2018-09-18 13:10:54 +02001466 * @brief Parse the config statement.
1467 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001468 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001469 * @param[in,out] flags Flags to add to.
1470 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001471 * @return LY_ERR values.
1472 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001473static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001474parse_config(struct lysp_yang_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001475{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001476 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001477 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001478 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001479 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001480
1481 if (*flags & LYS_CONFIG_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02001482 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001483 return LY_EVALID;
1484 }
1485
1486 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001487 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001488
Radek Krejcif13b87b2020-12-01 22:02:17 +01001489 if ((word_len == ly_strlen_const("true")) && !strncmp(word, "true", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001490 *flags |= LYS_CONFIG_W;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001491 } else if ((word_len == ly_strlen_const("false")) && !strncmp(word, "false", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001492 *flags |= LYS_CONFIG_R;
1493 } else {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02001494 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001495 free(buf);
1496 return LY_EVALID;
1497 }
1498 free(buf);
1499
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001500 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001501 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001502 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001503 LY_CHECK_RET(parse_ext(ctx, word, word_len, flags, LY_STMT_CONFIG, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001504 break;
1505 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001506 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001507 return LY_EVALID;
1508 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001509 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001510 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001511
1512cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001513 return ret;
1514}
1515
Michal Vaskoea5abea2018-09-18 13:10:54 +02001516/**
1517 * @brief Parse the mandatory statement.
1518 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001519 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001520 * @param[in,out] flags Flags to add to.
1521 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001522 * @return LY_ERR values.
1523 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001524static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001525parse_mandatory(struct lysp_yang_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001526{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001527 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001528 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001529 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001530 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001531
1532 if (*flags & LYS_MAND_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02001533 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001534 return LY_EVALID;
1535 }
1536
1537 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001538 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001539
Radek Krejcif13b87b2020-12-01 22:02:17 +01001540 if ((word_len == ly_strlen_const("true")) && !strncmp(word, "true", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001541 *flags |= LYS_MAND_TRUE;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001542 } else if ((word_len == ly_strlen_const("false")) && !strncmp(word, "false", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001543 *flags |= LYS_MAND_FALSE;
1544 } else {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02001545 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001546 free(buf);
1547 return LY_EVALID;
1548 }
1549 free(buf);
1550
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001551 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001552 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001553 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001554 LY_CHECK_RET(parse_ext(ctx, word, word_len, flags, LY_STMT_MANDATORY, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001555 break;
1556 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001557 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001558 return LY_EVALID;
1559 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001560 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001561 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001562
1563cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001564 return ret;
1565}
1566
Michal Vaskoea5abea2018-09-18 13:10:54 +02001567/**
1568 * @brief Parse a restriction such as range or length.
1569 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001570 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001571 * @param[in] restr_kw Type of this particular restriction.
1572 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001573 * @return LY_ERR values.
1574 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001575static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001576parse_restr(struct lysp_yang_ctx *ctx, enum ly_stmt restr_kw, struct lysp_restr *restr)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001577{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001578 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001579 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001580 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001581 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001582
1583 /* get value */
Michal Vasko72c6d642024-02-27 14:59:01 +01001584 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, &restr->arg.flags, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001585
Michal Vasko193dacd2022-10-13 08:43:05 +02001586 CHECK_NONEMPTY(ctx, word_len, lyplg_ext_stmt2str(restr_kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02001587 INSERT_WORD_GOTO(ctx, buf, restr->arg.str, word, word_len, ret, cleanup);
Michal Vasko8a67eff2021-12-07 14:04:47 +01001588 restr->arg.mod = PARSER_CUR_PMOD(ctx);
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001589 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001590 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001591 case LY_STMT_DESCRIPTION:
Michal Vasko72c6d642024-02-27 14:59:01 +01001592 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 +02001593 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001594 case LY_STMT_REFERENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01001595 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 +02001596 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001597 case LY_STMT_ERROR_APP_TAG:
Michal Vasko72c6d642024-02-27 14:59:01 +01001598 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 +02001599 &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001600 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001601 case LY_STMT_ERROR_MESSAGE:
Michal Vasko72c6d642024-02-27 14:59:01 +01001602 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 +02001603 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001604 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001605 LY_CHECK_RET(parse_ext(ctx, word, word_len, restr, restr_kw, 0, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001606 break;
1607 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001608 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), lyplg_ext_stmt2str(restr_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001609 return LY_EVALID;
1610 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001611 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, restr->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001612 }
Michal Vasko12ef5362022-09-16 15:13:58 +02001613
1614cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001615 return ret;
1616}
1617
Michal Vaskoea5abea2018-09-18 13:10:54 +02001618/**
1619 * @brief Parse a restriction that can have more instances such as must.
1620 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001621 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001622 * @param[in] restr_kw Type of this particular restriction.
1623 * @param[in,out] restrs Restrictions to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001624 * @return LY_ERR values.
1625 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001626static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001627parse_restrs(struct lysp_yang_ctx *ctx, enum ly_stmt restr_kw, struct lysp_restr **restrs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001628{
1629 struct lysp_restr *restr;
1630
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001631 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *restrs, restr, LY_EMEM);
Radek Krejci33090f92020-12-17 20:12:46 +01001632 return parse_restr(ctx, restr_kw, restr);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001633}
1634
Michal Vaskoea5abea2018-09-18 13:10:54 +02001635/**
1636 * @brief Parse the status statement.
1637 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001638 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001639 * @param[in,out] flags Flags to add to.
1640 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001641 * @return LY_ERR values.
1642 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001643static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001644parse_status(struct lysp_yang_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001645{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001646 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001647 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001648 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001649 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001650
1651 if (*flags & LYS_STATUS_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02001652 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001653 return LY_EVALID;
1654 }
1655
1656 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001657 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001658
Radek Krejcif13b87b2020-12-01 22:02:17 +01001659 if ((word_len == ly_strlen_const("current")) && !strncmp(word, "current", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001660 *flags |= LYS_STATUS_CURR;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001661 } else if ((word_len == ly_strlen_const("deprecated")) && !strncmp(word, "deprecated", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001662 *flags |= LYS_STATUS_DEPRC;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001663 } else if ((word_len == ly_strlen_const("obsolete")) && !strncmp(word, "obsolete", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001664 *flags |= LYS_STATUS_OBSLT;
1665 } else {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02001666 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001667 free(buf);
1668 return LY_EVALID;
1669 }
1670 free(buf);
1671
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001672 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001673 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001674 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001675 LY_CHECK_RET(parse_ext(ctx, word, word_len, flags, LY_STMT_STATUS, 0, exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001676 break;
1677 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001678 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001679 return LY_EVALID;
1680 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001681 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001682 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001683
1684cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001685 return ret;
1686}
1687
Michal Vaskoea5abea2018-09-18 13:10:54 +02001688/**
1689 * @brief Parse the when statement.
1690 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001691 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001692 * @param[in,out] when_p When pointer to parse to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001693 * @return LY_ERR values.
1694 */
Radek Krejcif09e4e82019-06-14 15:08:11 +02001695LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001696parse_when(struct lysp_yang_ctx *ctx, struct lysp_when **when_p)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001697{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001698 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001699 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001700 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001701 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001702 struct lysp_when *when;
Michal Vaskoc636ea42022-09-16 10:20:31 +02001703 struct lysf_ctx fctx = {.ctx = PARSER_CTX(ctx)};
Michal Vasko7fbc8162018-09-17 10:35:16 +02001704
1705 if (*when_p) {
David Sedlákb3077192019-06-19 10:55:37 +02001706 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "when");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001707 return LY_EVALID;
1708 }
1709
1710 when = calloc(1, sizeof *when);
Michal Vasko12ef5362022-09-16 15:13:58 +02001711 LY_CHECK_ERR_GOTO(!when, LOGMEM(PARSER_CTX(ctx)); ret = LY_EMEM, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001712
1713 /* get value */
Michal Vasko12ef5362022-09-16 15:13:58 +02001714 LY_CHECK_GOTO(ret = get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len), cleanup);
Michal Vaskob36053d2020-03-26 15:49:30 +01001715 CHECK_NONEMPTY(ctx, word_len, "when");
Michal Vasko12ef5362022-09-16 15:13:58 +02001716 INSERT_WORD_GOTO(ctx, buf, when->cond, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001717
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001718 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001719 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001720 case LY_STMT_DESCRIPTION:
Michal Vasko72c6d642024-02-27 14:59:01 +01001721 ret = parse_text_field(ctx, when->dsc, LY_STMT_DESCRIPTION, 0, &when->dsc, Y_STR_ARG, NULL, &when->exts);
1722 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001723 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001724 case LY_STMT_REFERENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01001725 ret = parse_text_field(ctx, when->ref, LY_STMT_REFERENCE, 0, &when->ref, Y_STR_ARG, NULL, &when->exts);
1726 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001727 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001728 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001729 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 +02001730 break;
1731 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001732 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "when");
Michal Vasko12ef5362022-09-16 15:13:58 +02001733 ret = LY_EVALID;
1734 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001735 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001736 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, when->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001737 }
Michal Vasko12ef5362022-09-16 15:13:58 +02001738
1739cleanup:
1740 if (ret) {
Michal Vaskoc636ea42022-09-16 10:20:31 +02001741 lysp_when_free(&fctx, when);
Michal Vasko12ef5362022-09-16 15:13:58 +02001742 free(when);
1743 } else {
1744 *when_p = when;
1745 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001746 return ret;
1747}
1748
Michal Vaskoea5abea2018-09-18 13:10:54 +02001749/**
1750 * @brief Parse the anydata or anyxml statement.
1751 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001752 * @param[in] ctx yang parser context for logging.
Radek Krejci39b7fc22021-02-26 23:29:18 +01001753 * @param[in] any_kw Type of this particular keyword.
Michal Vasko193dacd2022-10-13 08:43:05 +02001754 * @param[in] parent Node parent.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001755 * @param[in,out] siblings Siblings to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001756 * @return LY_ERR values.
1757 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02001758LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001759parse_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 +02001760{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001761 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001762 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02001763 size_t word_len;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001764 struct lysp_node_anydata *any;
Radek Krejci39b7fc22021-02-26 23:29:18 +01001765 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001766
David Sedlák60adc092019-08-06 15:57:02 +02001767 /* create new structure and insert into siblings */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001768 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, any, next, LY_EMEM);
David Sedlák60adc092019-08-06 15:57:02 +02001769
Radek Krejci39b7fc22021-02-26 23:29:18 +01001770 any->nodetype = any_kw == LY_STMT_ANYDATA ? LYS_ANYDATA : LYS_ANYXML;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001771 any->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001772
Michal Vasko7fbc8162018-09-17 10:35:16 +02001773 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01001774 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02001775 INSERT_WORD_GOTO(ctx, buf, any->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001776
1777 /* parse substatements */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001778 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001779 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001780 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01001781 LY_CHECK_RET(parse_config(ctx, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001782 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001783 case LY_STMT_DESCRIPTION:
Michal Vasko72c6d642024-02-27 14:59:01 +01001784 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 +02001785 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001786 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01001787 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &any->iffeatures, Y_STR_ARG, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001788 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001789 case LY_STMT_MANDATORY:
Radek Krejci33090f92020-12-17 20:12:46 +01001790 LY_CHECK_RET(parse_mandatory(ctx, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001791 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001792 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01001793 LY_CHECK_RET(parse_restrs(ctx, kw, &any->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001794 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001795 case LY_STMT_REFERENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01001796 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 +02001797 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001798 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01001799 LY_CHECK_RET(parse_status(ctx, &any->flags, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001800 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001801 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01001802 LY_CHECK_RET(parse_when(ctx, &any->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001803 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001804 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001805 LY_CHECK_RET(parse_ext(ctx, word, word_len, any, any_kw, 0, &any->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001806 break;
1807 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001808 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), lyplg_ext_stmt2str(any_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001809 return LY_EVALID;
1810 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001811 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, any->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001812 }
Michal Vasko12ef5362022-09-16 15:13:58 +02001813
1814cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001815 return ret;
1816}
1817
Michal Vaskoea5abea2018-09-18 13:10:54 +02001818/**
1819 * @brief Parse the value or position statement. Substatement of type enum statement.
1820 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001821 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001822 * @param[in] val_kw Type of this particular keyword.
Michal Vasko193dacd2022-10-13 08:43:05 +02001823 * @param[in,out] enm Structure to fill.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001824 * @return LY_ERR values.
1825 */
David Sedlákd6ce6d72019-07-16 17:30:18 +02001826LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02001827parse_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 +02001828{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001829 LY_ERR ret = LY_SUCCESS;
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001830 char *buf = NULL, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02001831 size_t word_len;
Michal Vasko2bf4af42023-01-04 12:08:38 +01001832 long long num = 0;
1833 unsigned long long unum = 0;
Radek Krejcid6b76452019-09-03 17:03:03 +02001834 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001835
Michal Vasko193dacd2022-10-13 08:43:05 +02001836 if (enm->flags & LYS_SET_VALUE) {
1837 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, 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 }
Michal Vasko193dacd2022-10-13 08:43:05 +02001841 enm->flags |= LYS_SET_VALUE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001842
1843 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001844 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001845
Radek Krejcid6b76452019-09-03 17:03:03 +02001846 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 +02001847 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, lyplg_ext_stmt2str(val_kw));
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001848 ret = LY_EVALID;
1849 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001850 }
1851
1852 errno = 0;
Radek Krejcid6b76452019-09-03 17:03:03 +02001853 if (val_kw == LY_STMT_VALUE) {
Michal Vasko73d77ab2021-07-23 12:45:55 +02001854 num = strtoll(word, &ptr, LY_BASE_DEC);
Michal Vasko69730152020-10-09 16:30:07 +02001855 if ((num < INT64_C(-2147483648)) || (num > INT64_C(2147483647))) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02001856 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, lyplg_ext_stmt2str(val_kw));
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001857 ret = LY_EVALID;
1858 goto cleanup;
Radek Krejci8b764662018-11-14 14:15:13 +01001859 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001860 } else {
Michal Vasko73d77ab2021-07-23 12:45:55 +02001861 unum = strtoull(word, &ptr, LY_BASE_DEC);
Radek Krejci8b764662018-11-14 14:15:13 +01001862 if (unum > UINT64_C(4294967295)) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02001863 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, lyplg_ext_stmt2str(val_kw));
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001864 ret = LY_EVALID;
1865 goto cleanup;
Radek Krejci8b764662018-11-14 14:15:13 +01001866 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001867 }
1868 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02001869 if ((size_t)(ptr - word) != word_len) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02001870 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, lyplg_ext_stmt2str(val_kw));
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001871 ret = LY_EVALID;
1872 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001873 }
1874 if (errno == ERANGE) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02001875 LOGVAL_PARSER(ctx, LY_VCODE_OOB, (int)word_len, word, lyplg_ext_stmt2str(val_kw));
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001876 ret = LY_EVALID;
1877 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001878 }
Radek Krejcid6b76452019-09-03 17:03:03 +02001879 if (val_kw == LY_STMT_VALUE) {
Michal Vasko193dacd2022-10-13 08:43:05 +02001880 enm->value = num;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001881 } else {
Michal Vasko193dacd2022-10-13 08:43:05 +02001882 enm->value = unum;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001883 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001884
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001885 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001886 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001887 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001888 ret = parse_ext(ctx, word, word_len, enm, val_kw, 0, &enm->exts);
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001889 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001890 break;
1891 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001892 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), lyplg_ext_stmt2str(val_kw));
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001893 ret = LY_EVALID;
1894 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001895 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001896 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001897 }
Radek Krejci8b764662018-11-14 14:15:13 +01001898
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001899cleanup:
Radek Krejci8b764662018-11-14 14:15:13 +01001900 free(buf);
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001901 return ret;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001902}
1903
Michal Vaskoea5abea2018-09-18 13:10:54 +02001904/**
1905 * @brief Parse the enum or bit statement. Substatement of type statement.
1906 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001907 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001908 * @param[in] enum_kw Type of this particular keyword.
1909 * @param[in,out] enums Enums or bits to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001910 * @return LY_ERR values.
1911 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001912static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02001913parse_type_enum(struct lysp_yang_ctx *ctx, enum ly_stmt enum_kw, struct lysp_type_enum **enums)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001914{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001915 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001916 char *buf, *word;
David Sedlák6544c182019-07-12 13:17:33 +02001917 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02001918 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001919 struct lysp_type_enum *enm;
1920
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001921 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *enums, enm, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001922
1923 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01001924 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 +02001925 if (enum_kw == LY_STMT_ENUM) {
Michal Vaskod0625d72022-10-06 15:02:50 +02001926 ret = lysp_check_enum_name((struct lysp_ctx *)ctx, (const char *)word, word_len);
David Sedlák6544c182019-07-12 13:17:33 +02001927 LY_CHECK_ERR_RET(ret, free(buf), ret);
Radek Krejci335332a2019-09-05 13:03:35 +02001928 } /* else nothing specific for YANG_BIT */
Radek Krejci8b764662018-11-14 14:15:13 +01001929
Michal Vasko12ef5362022-09-16 15:13:58 +02001930 INSERT_WORD_GOTO(ctx, buf, enm->name, word, word_len, ret, cleanup);
Michal Vasko193dacd2022-10-13 08:43:05 +02001931 CHECK_UNIQUENESS(ctx, *enums, name, lyplg_ext_stmt2str(enum_kw), enm->name);
Radek Krejci8b764662018-11-14 14:15:13 +01001932
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001933 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02001934 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02001935 case LY_STMT_DESCRIPTION:
Michal Vasko72c6d642024-02-27 14:59:01 +01001936 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 +02001937 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001938 case LY_STMT_IF_FEATURE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001939 PARSER_CHECK_STMTVER2_RET(ctx, "if-feature", lyplg_ext_stmt2str(enum_kw));
Radek Krejcifc596f92021-02-26 22:40:26 +01001940 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &enm->iffeatures, Y_STR_ARG, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001941 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001942 case LY_STMT_REFERENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01001943 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 +02001944 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001945 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01001946 LY_CHECK_RET(parse_status(ctx, &enm->flags, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001947 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001948 case LY_STMT_VALUE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001949 LY_CHECK_ERR_RET(enum_kw == LY_STMT_BIT, LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw),
1950 lyplg_ext_stmt2str(enum_kw)), LY_EVALID);
1951 LY_CHECK_RET(parse_type_enum_value_pos(ctx, kw, enm));
David Sedlák9fb515f2019-07-11 10:33:58 +02001952 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001953 case LY_STMT_POSITION:
Michal Vasko193dacd2022-10-13 08:43:05 +02001954 LY_CHECK_ERR_RET(enum_kw == LY_STMT_ENUM, LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw),
1955 lyplg_ext_stmt2str(enum_kw)), LY_EVALID);
1956 LY_CHECK_RET(parse_type_enum_value_pos(ctx, kw, enm));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001957 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02001958 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02001959 LY_CHECK_RET(parse_ext(ctx, word, word_len, enm, enum_kw, 0, &enm->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001960 break;
1961 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02001962 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), lyplg_ext_stmt2str(enum_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001963 return LY_EVALID;
1964 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001965 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, enm->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001966 }
Michal Vasko12ef5362022-09-16 15:13:58 +02001967
1968cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02001969 return ret;
1970}
1971
Michal Vaskoea5abea2018-09-18 13:10:54 +02001972/**
1973 * @brief Parse the fraction-digits statement. Substatement of type statement.
1974 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02001975 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02001976 * @param[in,out] type Type to fill.
Michal Vaskoea5abea2018-09-18 13:10:54 +02001977 * @return LY_ERR values.
1978 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001979static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02001980parse_type_fracdigits(struct lysp_yang_ctx *ctx, struct lysp_type *type)
Michal Vasko7fbc8162018-09-17 10:35:16 +02001981{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01001982 LY_ERR ret = LY_SUCCESS;
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001983 char *buf = NULL, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02001984 size_t word_len;
Michal Vasko2bf4af42023-01-04 12:08:38 +01001985 unsigned long long num;
Radek Krejcid6b76452019-09-03 17:03:03 +02001986 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02001987
Michal Vasko193dacd2022-10-13 08:43:05 +02001988 if (type->fraction_digits) {
David Sedlákb3077192019-06-19 10:55:37 +02001989 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001990 return LY_EVALID;
1991 }
1992
1993 /* get value */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001994 LY_CHECK_GOTO(ret = get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001995
1996 if (!word_len || (word[0] == '0') || !isdigit(word[0])) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02001997 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, "fraction-digits");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02001998 ret = LY_EVALID;
1999 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002000 }
2001
2002 errno = 0;
Michal Vasko73d77ab2021-07-23 12:45:55 +02002003 num = strtoull(word, &ptr, LY_BASE_DEC);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002004 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02002005 if ((size_t)(ptr - word) != word_len) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02002006 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, "fraction-digits");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002007 ret = LY_EVALID;
2008 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002009 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01002010 if ((errno == ERANGE) || (num > LY_TYPE_DEC64_FD_MAX)) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02002011 LOGVAL_PARSER(ctx, LY_VCODE_OOB, (int)word_len, word, "fraction-digits");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002012 ret = LY_EVALID;
2013 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002014 }
Michal Vasko193dacd2022-10-13 08:43:05 +02002015 type->fraction_digits = num;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002016
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002017 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002018 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002019 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002020 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 +02002021 break;
2022 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002023 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "fraction-digits");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002024 ret = LY_EVALID;
2025 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002026 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002027 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002028 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002029
2030cleanup:
2031 free(buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002032 return ret;
2033}
2034
Michal Vaskoea5abea2018-09-18 13:10:54 +02002035/**
2036 * @brief Parse the require-instance statement. Substatement of type statement.
2037 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002038 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02002039 * @param[in,out] type Type to fill.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002040 * @return LY_ERR values.
2041 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002042static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02002043parse_type_reqinstance(struct lysp_yang_ctx *ctx, struct lysp_type *type)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002044{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002045 LY_ERR ret = LY_SUCCESS;
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002046 char *buf = NULL, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002047 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002048 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002049
Michal Vasko193dacd2022-10-13 08:43:05 +02002050 if (type->flags & LYS_SET_REQINST) {
David Sedlákb3077192019-06-19 10:55:37 +02002051 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002052 return LY_EVALID;
2053 }
Michal Vasko193dacd2022-10-13 08:43:05 +02002054 type->flags |= LYS_SET_REQINST;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002055
2056 /* get value */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002057 LY_CHECK_GOTO(ret = get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002058
Radek Krejcif13b87b2020-12-01 22:02:17 +01002059 if ((word_len == ly_strlen_const("true")) && !strncmp(word, "true", word_len)) {
Michal Vasko193dacd2022-10-13 08:43:05 +02002060 type->require_instance = 1;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002061 } else if ((word_len != ly_strlen_const("false")) || strncmp(word, "false", word_len)) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02002062 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, "require-instance");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002063 ret = LY_EVALID;
2064 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002065 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002066
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002067 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002068 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002069 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002070 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 +02002071 break;
2072 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002073 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "require-instance");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002074 ret = LY_EVALID;
2075 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002076 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002077 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002078 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002079
2080cleanup:
2081 free(buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002082 return ret;
2083}
2084
Michal Vaskoea5abea2018-09-18 13:10:54 +02002085/**
2086 * @brief Parse the modifier statement. Substatement of type pattern statement.
2087 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002088 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02002089 * @param[in,out] restr Restriction to fill.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002090 * @return LY_ERR values.
2091 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002092static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02002093parse_type_pattern_modifier(struct lysp_yang_ctx *ctx, struct lysp_restr *restr)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002094{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002095 LY_ERR ret = LY_SUCCESS;
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002096 char *buf = NULL, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002097 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002098 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002099
Michal Vasko193dacd2022-10-13 08:43:05 +02002100 if (restr->arg.str[0] == LYSP_RESTR_PATTERN_NACK) {
David Sedlákb3077192019-06-19 10:55:37 +02002101 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002102 return LY_EVALID;
2103 }
2104
2105 /* get value */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002106 LY_CHECK_GOTO(ret = get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002107
Radek Krejcif13b87b2020-12-01 22:02:17 +01002108 if ((word_len != ly_strlen_const("invert-match")) || strncmp(word, "invert-match", word_len)) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02002109 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, "modifier");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002110 ret = LY_EVALID;
2111 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002112 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002113
2114 /* replace the value in the dictionary */
Michal Vasko193dacd2022-10-13 08:43:05 +02002115 buf = malloc(strlen(restr->arg.str) + 1);
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002116 LY_CHECK_ERR_GOTO(!buf, LOGMEM(PARSER_CTX(ctx)); ret = LY_EMEM, cleanup);
Michal Vasko193dacd2022-10-13 08:43:05 +02002117 strcpy(buf, restr->arg.str);
2118 lydict_remove(PARSER_CTX(ctx), restr->arg.str);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002119
Radek Krejcif13b87b2020-12-01 22:02:17 +01002120 assert(buf[0] == LYSP_RESTR_PATTERN_ACK);
2121 buf[0] = LYSP_RESTR_PATTERN_NACK;
Michal Vasko183ea332022-11-08 10:50:03 +01002122 ret = lydict_insert_zc(PARSER_CTX(ctx), buf, &restr->arg.str);
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002123 buf = NULL;
Michal Vasko183ea332022-11-08 10:50:03 +01002124 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002125
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002126 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002127 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002128 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002129 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 +02002130 break;
2131 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002132 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "modifier");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002133 ret = LY_EVALID;
2134 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002135 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002136 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002137 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002138
2139cleanup:
2140 free(buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002141 return ret;
2142}
2143
Michal Vaskoea5abea2018-09-18 13:10:54 +02002144/**
2145 * @brief Parse the pattern statement. Substatement of type statement.
2146 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002147 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002148 * @param[in,out] patterns Restrictions to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002149 * @return LY_ERR values.
2150 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002151static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002152parse_type_pattern(struct lysp_yang_ctx *ctx, struct lysp_restr **patterns)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002153{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002154 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002155 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002156 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002157 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002158 struct lysp_restr *restr;
2159
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002160 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *patterns, restr, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002161
2162 /* get value */
Michal Vasko72c6d642024-02-27 14:59:01 +01002163 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, &restr->arg.flags, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002164
2165 /* add special meaning first byte */
2166 if (buf) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01002167 buf = ly_realloc(buf, word_len + 2);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002168 word = buf;
2169 } else {
2170 buf = malloc(word_len + 2);
2171 }
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002172 LY_CHECK_ERR_RET(!buf, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko54720192021-06-11 13:55:59 +02002173 if (word_len) {
2174 memmove(buf + 1, word, word_len);
2175 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01002176 buf[0] = LYSP_RESTR_PATTERN_ACK; /* pattern's default regular-match flag */
Radek Krejci86d106e2018-10-18 09:53:19 +02002177 buf[word_len + 1] = '\0'; /* terminating NULL byte */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002178 LY_CHECK_RET(lydict_insert_zc(PARSER_CTX(ctx), buf, &restr->arg.str));
Michal Vasko8a67eff2021-12-07 14:04:47 +01002179 restr->arg.mod = PARSER_CUR_PMOD(ctx);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002180
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002181 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002182 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002183 case LY_STMT_DESCRIPTION:
Michal Vasko72c6d642024-02-27 14:59:01 +01002184 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 +02002185 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002186 case LY_STMT_REFERENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01002187 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 +02002188 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002189 case LY_STMT_ERROR_APP_TAG:
Michal Vasko72c6d642024-02-27 14:59:01 +01002190 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 +02002191 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002192 case LY_STMT_ERROR_MESSAGE:
Michal Vasko72c6d642024-02-27 14:59:01 +01002193 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 +02002194 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002195 case LY_STMT_MODIFIER:
Radek Krejci335332a2019-09-05 13:03:35 +02002196 PARSER_CHECK_STMTVER2_RET(ctx, "modifier", "pattern");
Michal Vasko193dacd2022-10-13 08:43:05 +02002197 LY_CHECK_RET(parse_type_pattern_modifier(ctx, restr));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002198 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002199 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002200 LY_CHECK_RET(parse_ext(ctx, word, word_len, restr, LY_STMT_PATTERN, 0, &restr->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002201 break;
2202 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002203 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "pattern");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002204 return LY_EVALID;
2205 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002206 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, restr->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002207 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002208
2209cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002210 return ret;
2211}
2212
Michal Vaskoea5abea2018-09-18 13:10:54 +02002213/**
2214 * @brief Parse the type statement.
2215 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002216 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002217 * @param[in,out] type Type to wrote to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002218 * @return LY_ERR values.
2219 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002220static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002221parse_type(struct lysp_yang_ctx *ctx, struct lysp_type *type)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002222{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002223 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002224 char *buf, *word;
Michal Vasko004d3152020-06-11 19:59:22 +02002225 const char *str_path = NULL;
Radek Krejciefd22f62018-09-27 11:47:58 +02002226 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002227 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002228 struct lysp_type *nest_type;
2229
2230 if (type->name) {
David Sedlákb3077192019-06-19 10:55:37 +02002231 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "type");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002232 return LY_EVALID;
2233 }
2234
2235 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002236 LY_CHECK_RET(get_argument(ctx, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02002237 INSERT_WORD_GOTO(ctx, buf, type->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002238
Michal Vaskoe9c050f2020-10-06 14:01:23 +02002239 /* set module */
Michal Vasko8a67eff2021-12-07 14:04:47 +01002240 type->pmod = PARSER_CUR_PMOD(ctx);
Michal Vaskoe9c050f2020-10-06 14:01:23 +02002241
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002242 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002243 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002244 case LY_STMT_BASE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002245 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 +01002246 type->flags |= LYS_SET_BASE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002247 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002248 case LY_STMT_BIT:
Radek Krejci33090f92020-12-17 20:12:46 +01002249 LY_CHECK_RET(parse_type_enum(ctx, kw, &type->bits));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002250 type->flags |= LYS_SET_BIT;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002251 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002252 case LY_STMT_ENUM:
Radek Krejci33090f92020-12-17 20:12:46 +01002253 LY_CHECK_RET(parse_type_enum(ctx, kw, &type->enums));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002254 type->flags |= LYS_SET_ENUM;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002255 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002256 case LY_STMT_FRACTION_DIGITS:
Michal Vasko193dacd2022-10-13 08:43:05 +02002257 LY_CHECK_RET(parse_type_fracdigits(ctx, type));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002258 type->flags |= LYS_SET_FRDIGITS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002259 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002260 case LY_STMT_LENGTH:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002261 if (type->length) {
Michal Vasko193dacd2022-10-13 08:43:05 +02002262 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, lyplg_ext_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002263 return LY_EVALID;
2264 }
2265 type->length = calloc(1, sizeof *type->length);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002266 LY_CHECK_ERR_RET(!type->length, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002267
Radek Krejci33090f92020-12-17 20:12:46 +01002268 LY_CHECK_RET(parse_restr(ctx, kw, type->length));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002269 type->flags |= LYS_SET_LENGTH;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002270 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002271 case LY_STMT_PATH:
Michal Vasko004d3152020-06-11 19:59:22 +02002272 if (type->path) {
Michal Vasko193dacd2022-10-13 08:43:05 +02002273 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, lyplg_ext_stmt2str(LY_STMT_PATH));
Michal Vasko004d3152020-06-11 19:59:22 +02002274 return LY_EVALID;
2275 }
2276
aPiecek4bb1e372021-05-07 11:01:00 +02002277 /* Usually, in the parser_yang.c, the result of the parsing is stored directly in the
2278 * corresponding structure, so in case of failure, the lysp_module_free function will take
2279 * care of removing the parsed value from the dictionary. But in this case, it is not possible
2280 * to rely on lysp_module_free because the result of the parsing is stored in a local variable.
2281 */
Michal Vasko72c6d642024-02-27 14:59:01 +01002282 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 +02002283 lydict_remove(PARSER_CTX(ctx), str_path), ret);
Michal Vaskoed725d72021-06-23 12:03:45 +02002284 ret = ly_path_parse(PARSER_CTX(ctx), NULL, str_path, 0, 1, LY_PATH_BEGIN_EITHER,
Michal Vasko69730152020-10-09 16:30:07 +02002285 LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_LEAFREF, &type->path);
aPiecek4bb1e372021-05-07 11:01:00 +02002286 /* Moreover, even if successful, the string is removed from the dictionary. */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002287 lydict_remove(PARSER_CTX(ctx), str_path);
Michal Vasko004d3152020-06-11 19:59:22 +02002288 LY_CHECK_RET(ret);
Radek Krejcid505e3d2018-11-13 09:04:17 +01002289 type->flags |= LYS_SET_PATH;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002290 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002291 case LY_STMT_PATTERN:
Radek Krejci33090f92020-12-17 20:12:46 +01002292 LY_CHECK_RET(parse_type_pattern(ctx, &type->patterns));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002293 type->flags |= LYS_SET_PATTERN;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002294 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002295 case LY_STMT_RANGE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002296 if (type->range) {
Michal Vasko193dacd2022-10-13 08:43:05 +02002297 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, lyplg_ext_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002298 return LY_EVALID;
2299 }
2300 type->range = calloc(1, sizeof *type->range);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002301 LY_CHECK_ERR_RET(!type->range, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002302
Radek Krejci33090f92020-12-17 20:12:46 +01002303 LY_CHECK_RET(parse_restr(ctx, kw, type->range));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002304 type->flags |= LYS_SET_RANGE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002305 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002306 case LY_STMT_REQUIRE_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002307 LY_CHECK_RET(parse_type_reqinstance(ctx, type));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002308 /* LYS_SET_REQINST checked and set inside parse_type_reqinstance() */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002309 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002310 case LY_STMT_TYPE:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002311 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), type->types, nest_type, LY_EMEM);
Radek Krejci33090f92020-12-17 20:12:46 +01002312 LY_CHECK_RET(parse_type(ctx, nest_type));
Radek Krejcid505e3d2018-11-13 09:04:17 +01002313 type->flags |= LYS_SET_TYPE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002314 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002315 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002316 LY_CHECK_RET(parse_ext(ctx, word, word_len, type, LY_STMT_TYPE, 0, &type->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002317 break;
2318 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002319 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "type");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002320 return LY_EVALID;
2321 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002322 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, type->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002323 }
Michal Vasko12ef5362022-09-16 15:13:58 +02002324
2325cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002326 return ret;
2327}
2328
Michal Vaskoea5abea2018-09-18 13:10:54 +02002329/**
2330 * @brief Parse the leaf statement.
2331 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002332 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002333 * @param[in,out] siblings Siblings to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002334 * @return LY_ERR values.
2335 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002336LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002337parse_leaf(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002338{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002339 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002340 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002341 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002342 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002343 struct lysp_node_leaf *leaf;
2344
David Sedlák60adc092019-08-06 15:57:02 +02002345 /* create new leaf structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002346 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, leaf, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002347 leaf->nodetype = LYS_LEAF;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002348 leaf->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002349
Michal Vasko7fbc8162018-09-17 10:35:16 +02002350 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01002351 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02002352 INSERT_WORD_GOTO(ctx, buf, leaf->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002353
2354 /* parse substatements */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002355 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002356 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002357 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01002358 LY_CHECK_RET(parse_config(ctx, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002359 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002360 case LY_STMT_DEFAULT:
Michal Vasko72c6d642024-02-27 14:59:01 +01002361 LY_CHECK_RET(parse_text_field(ctx, &leaf->dflt, LY_STMT_DEFAULT, 0, &leaf->dflt.str, Y_STR_ARG,
2362 &leaf->dflt.flags, &leaf->exts));
Michal Vasko8a67eff2021-12-07 14:04:47 +01002363 leaf->dflt.mod = PARSER_CUR_PMOD(ctx);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002364 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002365 case LY_STMT_DESCRIPTION:
Michal Vasko72c6d642024-02-27 14:59:01 +01002366 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 +02002367 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002368 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002369 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &leaf->iffeatures, Y_STR_ARG, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002370 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002371 case LY_STMT_MANDATORY:
Radek Krejci33090f92020-12-17 20:12:46 +01002372 LY_CHECK_RET(parse_mandatory(ctx, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002373 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002374 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01002375 LY_CHECK_RET(parse_restrs(ctx, kw, &leaf->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002376 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002377 case LY_STMT_REFERENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01002378 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 +02002379 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002380 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002381 LY_CHECK_RET(parse_status(ctx, &leaf->flags, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002382 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002383 case LY_STMT_TYPE:
Radek Krejci33090f92020-12-17 20:12:46 +01002384 LY_CHECK_RET(parse_type(ctx, &leaf->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002385 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002386 case LY_STMT_UNITS:
Michal Vasko72c6d642024-02-27 14:59:01 +01002387 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 +02002388 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002389 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01002390 LY_CHECK_RET(parse_when(ctx, &leaf->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002391 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002392 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002393 LY_CHECK_RET(parse_ext(ctx, word, word_len, leaf, LY_STMT_LEAF, 0, &leaf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002394 break;
2395 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002396 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "leaf");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002397 return LY_EVALID;
2398 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002399 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, leaf->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002400 }
Michal Vasko12ef5362022-09-16 15:13:58 +02002401
Michal Vasko7fbc8162018-09-17 10:35:16 +02002402 /* mandatory substatements */
2403 if (!leaf->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002404 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "leaf");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002405 return LY_EVALID;
2406 }
2407
Michal Vasko12ef5362022-09-16 15:13:58 +02002408cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002409 return ret;
2410}
2411
Michal Vaskoea5abea2018-09-18 13:10:54 +02002412/**
2413 * @brief Parse the max-elements statement.
2414 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002415 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002416 * @param[in,out] max Value to write to.
2417 * @param[in,out] flags Flags to write to.
2418 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002419 * @return LY_ERR values.
2420 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002421LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002422parse_maxelements(struct lysp_yang_ctx *ctx, uint32_t *max, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002423{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002424 LY_ERR ret = LY_SUCCESS;
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002425 char *buf = NULL, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02002426 size_t word_len;
Michal Vasko2bf4af42023-01-04 12:08:38 +01002427 unsigned long long num;
Radek Krejcid6b76452019-09-03 17:03:03 +02002428 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002429
2430 if (*flags & LYS_SET_MAX) {
David Sedlákb3077192019-06-19 10:55:37 +02002431 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002432 return LY_EVALID;
2433 }
2434 *flags |= LYS_SET_MAX;
2435
2436 /* get value */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002437 LY_CHECK_GOTO(ret = get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002438
2439 if (!word_len || (word[0] == '0') || ((word[0] != 'u') && !isdigit(word[0]))) {
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
Radek Krejci7f9b6512019-09-18 13:11:09 +02002445 if (ly_strncmp("unbounded", word, word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002446 errno = 0;
Michal Vasko73d77ab2021-07-23 12:45:55 +02002447 num = strtoull(word, &ptr, LY_BASE_DEC);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002448 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02002449 if ((size_t)(ptr - word) != word_len) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02002450 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, "max-elements");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002451 ret = LY_EVALID;
2452 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002453 }
2454 if ((errno == ERANGE) || (num > UINT32_MAX)) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02002455 LOGVAL_PARSER(ctx, LY_VCODE_OOB, (int)word_len, word, "max-elements");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002456 ret = LY_EVALID;
2457 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002458 }
2459
2460 *max = num;
Radek Krejcid6315102021-02-02 15:26:34 +01002461 } else {
2462 /* unbounded */
2463 *max = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002464 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002465
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002466 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002467 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002468 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002469 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 +02002470 break;
2471 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002472 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "max-elements");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002473 ret = LY_EVALID;
2474 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002475 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002476 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002477 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002478
2479cleanup:
2480 free(buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002481 return ret;
2482}
2483
Michal Vaskoea5abea2018-09-18 13:10:54 +02002484/**
2485 * @brief Parse the min-elements statement.
2486 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002487 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002488 * @param[in,out] min Value to write to.
2489 * @param[in,out] flags Flags to write to.
2490 * @param[in,out] exts Extension instances to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002491 * @return LY_ERR values.
2492 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002493LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002494parse_minelements(struct lysp_yang_ctx *ctx, uint32_t *min, uint16_t *flags, struct lysp_ext_instance **exts)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002495{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002496 LY_ERR ret = LY_SUCCESS;
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002497 char *buf = NULL, *word, *ptr;
Radek Krejciefd22f62018-09-27 11:47:58 +02002498 size_t word_len;
Michal Vasko2bf4af42023-01-04 12:08:38 +01002499 unsigned long long num;
Radek Krejcid6b76452019-09-03 17:03:03 +02002500 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002501
2502 if (*flags & LYS_SET_MIN) {
David Sedlákb3077192019-06-19 10:55:37 +02002503 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002504 return LY_EVALID;
2505 }
2506 *flags |= LYS_SET_MIN;
2507
2508 /* get value */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002509 LY_CHECK_GOTO(ret = get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002510
2511 if (!word_len || !isdigit(word[0]) || ((word[0] == '0') && (word_len > 1))) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02002512 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, "min-elements");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002513 ret = LY_EVALID;
2514 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002515 }
2516
2517 errno = 0;
Michal Vasko73d77ab2021-07-23 12:45:55 +02002518 num = strtoull(word, &ptr, LY_BASE_DEC);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002519 /* we have not parsed the whole argument */
Radek Krejciefd22f62018-09-27 11:47:58 +02002520 if ((size_t)(ptr - word) != word_len) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02002521 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, "min-elements");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002522 ret = LY_EVALID;
2523 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002524 }
2525 if ((errno == ERANGE) || (num > UINT32_MAX)) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02002526 LOGVAL_PARSER(ctx, LY_VCODE_OOB, (int)word_len, word, "min-elements");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002527 ret = LY_EVALID;
2528 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002529 }
2530 *min = num;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002531
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002532 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002533 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002534 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002535 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 +02002536 break;
2537 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002538 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "min-elements");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002539 ret = LY_EVALID;
2540 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002541 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002542 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002543 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002544
2545cleanup:
2546 free(buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002547 return ret;
2548}
2549
Michal Vaskoea5abea2018-09-18 13:10:54 +02002550/**
2551 * @brief Parse the ordered-by statement.
2552 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002553 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02002554 * @param[in,out] llist List or leaf-list to fill.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002555 * @return LY_ERR values.
2556 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002557static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02002558parse_orderedby(struct lysp_yang_ctx *ctx, struct lysp_node *llist)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002559{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002560 LY_ERR ret = LY_SUCCESS;
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002561 char *buf = NULL, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002562 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002563 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002564
Michal Vasko193dacd2022-10-13 08:43:05 +02002565 if (llist->flags & LYS_ORDBY_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02002566 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002567 return LY_EVALID;
2568 }
2569
2570 /* get value */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002571 LY_CHECK_GOTO(ret = get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002572
Radek Krejcif13b87b2020-12-01 22:02:17 +01002573 if ((word_len == ly_strlen_const("system")) && !strncmp(word, "system", word_len)) {
Michal Vasko193dacd2022-10-13 08:43:05 +02002574 llist->flags |= LYS_ORDBY_SYSTEM;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002575 } else if ((word_len == ly_strlen_const("user")) && !strncmp(word, "user", word_len)) {
Michal Vasko193dacd2022-10-13 08:43:05 +02002576 llist->flags |= LYS_ORDBY_USER;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002577 } else {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02002578 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, "ordered-by");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002579 ret = LY_EVALID;
2580 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002581 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02002582
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002583 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002584 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002585 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002586 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 +02002587 break;
2588 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002589 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "ordered-by");
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002590 ret = LY_EVALID;
2591 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002592 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002593 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002594 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002595
2596cleanup:
2597 free(buf);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002598 return ret;
2599}
2600
Michal Vaskoea5abea2018-09-18 13:10:54 +02002601/**
2602 * @brief Parse the leaf-list statement.
2603 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002604 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002605 * @param[in,out] siblings Siblings to add to.
2606 *
2607 * @return LY_ERR values.
2608 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002609LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002610parse_leaflist(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002611{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002612 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002613 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002614 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002615 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002616 struct lysp_node_leaflist *llist;
2617
David Sedlák60adc092019-08-06 15:57:02 +02002618 /* create new leaf-list structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002619 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, llist, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002620 llist->nodetype = LYS_LEAFLIST;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002621 llist->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002622
Michal Vasko7fbc8162018-09-17 10:35:16 +02002623 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01002624 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02002625 INSERT_WORD_GOTO(ctx, buf, llist->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002626
2627 /* parse substatements */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002628 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002629 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002630 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01002631 LY_CHECK_RET(parse_config(ctx, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002632 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002633 case LY_STMT_DEFAULT:
Radek Krejci335332a2019-09-05 13:03:35 +02002634 PARSER_CHECK_STMTVER2_RET(ctx, "default", "leaf-list");
Radek Krejcifc596f92021-02-26 22:40:26 +01002635 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_DEFAULT, &llist->dflts, Y_STR_ARG, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002636 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002637 case LY_STMT_DESCRIPTION:
Michal Vasko72c6d642024-02-27 14:59:01 +01002638 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 +02002639 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002640 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002641 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &llist->iffeatures, Y_STR_ARG, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002642 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002643 case LY_STMT_MAX_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01002644 LY_CHECK_RET(parse_maxelements(ctx, &llist->max, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002645 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002646 case LY_STMT_MIN_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01002647 LY_CHECK_RET(parse_minelements(ctx, &llist->min, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002648 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002649 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01002650 LY_CHECK_RET(parse_restrs(ctx, kw, &llist->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002651 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002652 case LY_STMT_ORDERED_BY:
Michal Vasko193dacd2022-10-13 08:43:05 +02002653 LY_CHECK_RET(parse_orderedby(ctx, &llist->node));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002654 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002655 case LY_STMT_REFERENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01002656 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 +02002657 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002658 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002659 LY_CHECK_RET(parse_status(ctx, &llist->flags, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002660 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002661 case LY_STMT_TYPE:
Radek Krejci33090f92020-12-17 20:12:46 +01002662 LY_CHECK_RET(parse_type(ctx, &llist->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002663 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002664 case LY_STMT_UNITS:
Michal Vasko72c6d642024-02-27 14:59:01 +01002665 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 +02002666 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002667 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01002668 LY_CHECK_RET(parse_when(ctx, &llist->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002669 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002670 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002671 LY_CHECK_RET(parse_ext(ctx, word, word_len, llist, LY_STMT_LEAF_LIST, 0, &llist->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002672 break;
2673 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002674 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "llist");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002675 return LY_EVALID;
2676 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002677 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, llist->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002678 }
Michal Vasko12ef5362022-09-16 15:13:58 +02002679
Michal Vasko7fbc8162018-09-17 10:35:16 +02002680 /* mandatory substatements */
2681 if (!llist->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002682 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "leaf-list");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002683 return LY_EVALID;
2684 }
2685
Michal Vasko12ef5362022-09-16 15:13:58 +02002686cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002687 return ret;
2688}
2689
Michal Vaskoea5abea2018-09-18 13:10:54 +02002690/**
2691 * @brief Parse the refine statement.
2692 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002693 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002694 * @param[in,out] refines Refines to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002695 * @return LY_ERR values.
2696 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002697static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002698parse_refine(struct lysp_yang_ctx *ctx, struct lysp_refine **refines)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002699{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002700 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002701 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002702 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002703 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002704 struct lysp_refine *rf;
2705
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002706 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *refines, rf, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002707
2708 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002709 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vaskob36053d2020-03-26 15:49:30 +01002710 CHECK_NONEMPTY(ctx, word_len, "refine");
Michal Vasko12ef5362022-09-16 15:13:58 +02002711 INSERT_WORD_GOTO(ctx, buf, rf->nodeid, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002712
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002713 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002714 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002715 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01002716 LY_CHECK_RET(parse_config(ctx, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002717 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002718 case LY_STMT_DEFAULT:
Radek Krejcifc596f92021-02-26 22:40:26 +01002719 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_DEFAULT, &rf->dflts, Y_STR_ARG, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002720 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002721 case LY_STMT_DESCRIPTION:
Michal Vasko72c6d642024-02-27 14:59:01 +01002722 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 +02002723 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002724 case LY_STMT_IF_FEATURE:
Radek Krejci335332a2019-09-05 13:03:35 +02002725 PARSER_CHECK_STMTVER2_RET(ctx, "if-feature", "refine");
Radek Krejcifc596f92021-02-26 22:40:26 +01002726 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &rf->iffeatures, Y_STR_ARG, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002727 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002728 case LY_STMT_MAX_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01002729 LY_CHECK_RET(parse_maxelements(ctx, &rf->max, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002730 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002731 case LY_STMT_MIN_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01002732 LY_CHECK_RET(parse_minelements(ctx, &rf->min, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002733 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002734 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01002735 LY_CHECK_RET(parse_restrs(ctx, kw, &rf->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002736 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002737 case LY_STMT_MANDATORY:
Radek Krejci33090f92020-12-17 20:12:46 +01002738 LY_CHECK_RET(parse_mandatory(ctx, &rf->flags, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002739 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002740 case LY_STMT_REFERENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01002741 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 +02002742 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002743 case LY_STMT_PRESENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01002744 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 +02002745 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002746 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002747 LY_CHECK_RET(parse_ext(ctx, word, word_len, rf, LY_STMT_REFINE, 0, &rf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002748 break;
2749 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002750 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "refine");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002751 return LY_EVALID;
2752 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002753 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, rf->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002754 }
Michal Vasko12ef5362022-09-16 15:13:58 +02002755
2756cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002757 return ret;
2758}
2759
Michal Vaskoea5abea2018-09-18 13:10:54 +02002760/**
2761 * @brief Parse the typedef statement.
2762 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002763 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002764 * @param[in,out] typedefs Typedefs to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002765 * @return LY_ERR values.
2766 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002767static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002768parse_typedef(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_tpdf **typedefs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002769{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002770 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002771 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002772 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002773 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002774 struct lysp_tpdf *tpdf;
2775
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002776 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *typedefs, tpdf, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002777
2778 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002779 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02002780 INSERT_WORD_GOTO(ctx, buf, tpdf->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002781
2782 /* parse substatements */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002783 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002784 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002785 case LY_STMT_DEFAULT:
Michal Vasko72c6d642024-02-27 14:59:01 +01002786 LY_CHECK_RET(parse_text_field(ctx, &tpdf->dflt, LY_STMT_DEFAULT, 0, &tpdf->dflt.str, Y_STR_ARG,
2787 &tpdf->dflt.flags, &tpdf->exts));
Michal Vasko8a67eff2021-12-07 14:04:47 +01002788 tpdf->dflt.mod = PARSER_CUR_PMOD(ctx);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002789 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002790 case LY_STMT_DESCRIPTION:
Michal Vasko72c6d642024-02-27 14:59:01 +01002791 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 +02002792 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002793 case LY_STMT_REFERENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01002794 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 +02002795 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002796 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002797 LY_CHECK_RET(parse_status(ctx, &tpdf->flags, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002798 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002799 case LY_STMT_TYPE:
Radek Krejci33090f92020-12-17 20:12:46 +01002800 LY_CHECK_RET(parse_type(ctx, &tpdf->type));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002801 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002802 case LY_STMT_UNITS:
Michal Vasko72c6d642024-02-27 14:59:01 +01002803 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 +02002804 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002805 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002806 LY_CHECK_RET(parse_ext(ctx, word, word_len, tpdf, LY_STMT_TYPEDEF, 0, &tpdf->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002807 break;
2808 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002809 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "typedef");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002810 return LY_EVALID;
2811 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002812 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, tpdf->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002813 }
Michal Vasko12ef5362022-09-16 15:13:58 +02002814
Michal Vasko7fbc8162018-09-17 10:35:16 +02002815 /* mandatory substatements */
2816 if (!tpdf->type.name) {
David Sedlákb3077192019-06-19 10:55:37 +02002817 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "type", "typedef");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002818 return LY_EVALID;
2819 }
2820
Radek Krejcibbe09a92018-11-08 09:36:54 +01002821 /* store data for collision check */
aPiecek75b83a02021-06-29 10:34:38 +02002822 if (parent) {
aPiecek8d4e75d2021-06-24 14:47:06 +02002823 assert(ctx->main_ctx);
2824 LY_CHECK_RET(ly_set_add(&ctx->main_ctx->tpdfs_nodes, parent, 0, NULL));
Radek Krejcibbe09a92018-11-08 09:36:54 +01002825 }
2826
Michal Vasko12ef5362022-09-16 15:13:58 +02002827cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002828 return ret;
2829}
2830
Michal Vaskoea5abea2018-09-18 13:10:54 +02002831/**
2832 * @brief Parse the input or output statement.
2833 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002834 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002835 * @param[in] kw Type of this particular keyword
2836 * @param[in,out] inout_p Input/output pointer to write to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002837 * @return LY_ERR values.
2838 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002839static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002840parse_inout(struct lysp_yang_ctx *ctx, enum ly_stmt inout_kw, struct lysp_node *parent,
Radek Krejci2a9fc652021-01-22 17:44:34 +01002841 struct lysp_node_action_inout *inout_p)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002842{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002843 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002844 char *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002845 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002846 enum ly_stmt kw;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002847 ly_bool input = &((struct lysp_node_action *)parent)->input == inout_p ? 1 : 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002848
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002849 if (inout_p->nodetype) {
Michal Vasko193dacd2022-10-13 08:43:05 +02002850 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, lyplg_ext_stmt2str(inout_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002851 return LY_EVALID;
2852 }
2853
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002854 /* initiate structure */
Radek Krejci2a9fc652021-01-22 17:44:34 +01002855 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), input ? "input" : "output", 0, &inout_p->name));
2856 inout_p->nodetype = input ? LYS_INPUT : LYS_OUTPUT;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002857 inout_p->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002858
2859 /* parse substatements */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002860 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002861 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002862 case LY_STMT_ANYDATA:
Michal Vasko193dacd2022-10-13 08:43:05 +02002863 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", lyplg_ext_stmt2str(inout_kw));
Radek Krejci0f969882020-08-21 16:56:47 +02002864 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02002865 case LY_STMT_ANYXML:
Radek Krejci01180ac2021-01-27 08:48:22 +01002866 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002867 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002868 case LY_STMT_CHOICE:
Radek Krejci01180ac2021-01-27 08:48:22 +01002869 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002870 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002871 case LY_STMT_CONTAINER:
Radek Krejci01180ac2021-01-27 08:48:22 +01002872 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002873 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002874 case LY_STMT_LEAF:
Radek Krejci01180ac2021-01-27 08:48:22 +01002875 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002876 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002877 case LY_STMT_LEAF_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01002878 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002879 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002880 case LY_STMT_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01002881 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002882 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002883 case LY_STMT_USES:
Radek Krejci01180ac2021-01-27 08:48:22 +01002884 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)inout_p, &inout_p->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002885 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002886 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01002887 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)inout_p, &inout_p->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002888 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002889 case LY_STMT_MUST:
Michal Vasko193dacd2022-10-13 08:43:05 +02002890 PARSER_CHECK_STMTVER2_RET(ctx, "must", lyplg_ext_stmt2str(inout_kw));
Radek Krejci33090f92020-12-17 20:12:46 +01002891 LY_CHECK_RET(parse_restrs(ctx, kw, &inout_p->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002892 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002893 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01002894 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)inout_p, &inout_p->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002895 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002896 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002897 LY_CHECK_RET(parse_ext(ctx, word, word_len, inout_p, inout_kw, 0, &inout_p->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002898 break;
2899 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002900 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), lyplg_ext_stmt2str(inout_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002901 return LY_EVALID;
2902 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002903 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, inout_p->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002904 }
Michal Vaskob83af8a2020-01-06 09:49:22 +01002905
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002906cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002907 return ret;
2908}
2909
Michal Vaskoea5abea2018-09-18 13:10:54 +02002910/**
2911 * @brief Parse the action statement.
2912 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002913 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002914 * @param[in,out] actions Actions to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002915 * @return LY_ERR values.
2916 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002917LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002918parse_action(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node_action **actions)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002919{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002920 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002921 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02002922 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02002923 enum ly_stmt kw;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002924 struct lysp_node_action *act;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002925
Radek Krejci2a9fc652021-01-22 17:44:34 +01002926 LY_LIST_NEW_RET(PARSER_CTX(ctx), actions, act, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002927
2928 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01002929 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02002930 INSERT_WORD_GOTO(ctx, buf, act->name, word, word_len, ret, cleanup);
Michal Vasko1bf09392020-03-27 12:38:10 +01002931 act->nodetype = parent ? LYS_ACTION : LYS_RPC;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002932 act->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002933
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002934 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02002935 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02002936 case LY_STMT_DESCRIPTION:
Michal Vasko72c6d642024-02-27 14:59:01 +01002937 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 +02002938 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002939 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01002940 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &act->iffeatures, Y_STR_ARG, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002941 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002942 case LY_STMT_REFERENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01002943 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 +02002944 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002945 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01002946 LY_CHECK_RET(parse_status(ctx, &act->flags, &act->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002947 break;
2948
Radek Krejcid6b76452019-09-03 17:03:03 +02002949 case LY_STMT_INPUT:
Radek Krejci33090f92020-12-17 20:12:46 +01002950 LY_CHECK_RET(parse_inout(ctx, kw, (struct lysp_node *)act, &act->input));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002951 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002952 case LY_STMT_OUTPUT:
Radek Krejci33090f92020-12-17 20:12:46 +01002953 LY_CHECK_RET(parse_inout(ctx, kw, (struct lysp_node *)act, &act->output));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002954 break;
2955
Radek Krejcid6b76452019-09-03 17:03:03 +02002956 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01002957 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)act, &act->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002958 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002959 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01002960 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)act, &act->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002961 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02002962 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02002963 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 +02002964 break;
2965 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02002966 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), parent ? "action" : "rpc");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002967 return LY_EVALID;
2968 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002969 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, act->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002970 }
Michal Vasko7f45cf22020-10-01 12:49:44 +02002971
2972 /* always initialize inout, they are technically present (needed for later deviations/refines) */
2973 if (!act->input.nodetype) {
2974 act->input.nodetype = LYS_INPUT;
2975 act->input.parent = (struct lysp_node *)act;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002976 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), "input", 0, &act->input.name));
Michal Vasko7f45cf22020-10-01 12:49:44 +02002977 }
2978 if (!act->output.nodetype) {
2979 act->output.nodetype = LYS_OUTPUT;
2980 act->output.parent = (struct lysp_node *)act;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002981 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), "output", 0, &act->output.name));
Michal Vasko7f45cf22020-10-01 12:49:44 +02002982 }
2983
Michal Vasko12ef5362022-09-16 15:13:58 +02002984cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02002985 return ret;
2986}
2987
Michal Vaskoea5abea2018-09-18 13:10:54 +02002988/**
2989 * @brief Parse the notification statement.
2990 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02002991 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002992 * @param[in,out] notifs Notifications to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02002993 * @return LY_ERR values.
2994 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002995LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02002996parse_notif(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node_notif **notifs)
Michal Vasko7fbc8162018-09-17 10:35:16 +02002997{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01002998 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02002999 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003000 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003001 enum ly_stmt kw;
Radek Krejci2a9fc652021-01-22 17:44:34 +01003002 struct lysp_node_notif *notif;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003003
Radek Krejci2a9fc652021-01-22 17:44:34 +01003004 LY_LIST_NEW_RET(PARSER_CTX(ctx), notifs, notif, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003005
3006 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003007 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02003008 INSERT_WORD_GOTO(ctx, buf, notif->name, word, word_len, ret, cleanup);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003009 notif->nodetype = LYS_NOTIF;
3010 notif->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003011
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003012 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003013 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003014 case LY_STMT_DESCRIPTION:
Michal Vasko72c6d642024-02-27 14:59:01 +01003015 LY_CHECK_RET(parse_text_field(ctx, notif->dsc, LY_STMT_DESCRIPTION, 0, &notif->dsc, Y_STR_ARG, NULL,
3016 &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003017 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003018 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003019 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &notif->iffeatures, Y_STR_ARG, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003020 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003021 case LY_STMT_REFERENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01003022 LY_CHECK_RET(parse_text_field(ctx, notif->ref, LY_STMT_REFERENCE, 0, &notif->ref, Y_STR_ARG, NULL,
3023 &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003024 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003025 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003026 LY_CHECK_RET(parse_status(ctx, &notif->flags, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003027 break;
3028
Radek Krejcid6b76452019-09-03 17:03:03 +02003029 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003030 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "notification");
Radek Krejci0f969882020-08-21 16:56:47 +02003031 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003032 case LY_STMT_ANYXML:
Radek Krejci01180ac2021-01-27 08:48:22 +01003033 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003034 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003035 case LY_STMT_CHOICE:
Michal Vasko5ae1e1a2021-02-08 09:53:26 +01003036 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003037 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003038 case LY_STMT_CONTAINER:
Radek Krejci01180ac2021-01-27 08:48:22 +01003039 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003040 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003041 case LY_STMT_LEAF:
Radek Krejci01180ac2021-01-27 08:48:22 +01003042 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003043 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003044 case LY_STMT_LEAF_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01003045 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003046 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003047 case LY_STMT_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01003048 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003049 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003050 case LY_STMT_USES:
Radek Krejci01180ac2021-01-27 08:48:22 +01003051 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)notif, &notif->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003052 break;
3053
Radek Krejcid6b76452019-09-03 17:03:03 +02003054 case LY_STMT_MUST:
Radek Krejci335332a2019-09-05 13:03:35 +02003055 PARSER_CHECK_STMTVER2_RET(ctx, "must", "notification");
Radek Krejci33090f92020-12-17 20:12:46 +01003056 LY_CHECK_RET(parse_restrs(ctx, kw, &notif->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003057 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003058 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01003059 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)notif, &notif->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003060 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003061 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01003062 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)notif, &notif->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003063 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003064 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003065 LY_CHECK_RET(parse_ext(ctx, word, word_len, notif, LY_STMT_NOTIFICATION, 0, &notif->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003066 break;
3067 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003068 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "notification");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003069 return LY_EVALID;
3070 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003071 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, notif->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003072 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003073
Michal Vasko12ef5362022-09-16 15:13:58 +02003074cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003075 return ret;
3076}
3077
Michal Vaskoea5abea2018-09-18 13:10:54 +02003078/**
3079 * @brief Parse the grouping statement.
3080 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003081 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003082 * @param[in,out] groupings Groupings to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003083 * @return LY_ERR values.
3084 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003085LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02003086parse_grouping(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node_grp **groupings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003087{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003088 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003089 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003090 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003091 enum ly_stmt kw;
Radek Krejci2a9fc652021-01-22 17:44:34 +01003092 struct lysp_node_grp *grp;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003093
Radek Krejci2a9fc652021-01-22 17:44:34 +01003094 LY_LIST_NEW_RET(PARSER_CTX(ctx), groupings, grp, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003095
3096 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003097 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02003098 INSERT_WORD_GOTO(ctx, buf, grp->name, word, word_len, ret, cleanup);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003099 grp->nodetype = LYS_GROUPING;
3100 grp->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003101
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003102 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003103 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003104 case LY_STMT_DESCRIPTION:
Michal Vasko72c6d642024-02-27 14:59:01 +01003105 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 +02003106 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003107 case LY_STMT_REFERENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01003108 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 +02003109 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003110 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003111 LY_CHECK_RET(parse_status(ctx, &grp->flags, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003112 break;
3113
Radek Krejcid6b76452019-09-03 17:03:03 +02003114 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003115 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "grouping");
Radek Krejci0f969882020-08-21 16:56:47 +02003116 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003117 case LY_STMT_ANYXML:
Radek Krejci01180ac2021-01-27 08:48:22 +01003118 LY_CHECK_RET(parse_any(ctx, kw, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003119 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003120 case LY_STMT_CHOICE:
Radek Krejci01180ac2021-01-27 08:48:22 +01003121 LY_CHECK_RET(parse_choice(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003122 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003123 case LY_STMT_CONTAINER:
Radek Krejci01180ac2021-01-27 08:48:22 +01003124 LY_CHECK_RET(parse_container(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003125 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003126 case LY_STMT_LEAF:
Radek Krejci01180ac2021-01-27 08:48:22 +01003127 LY_CHECK_RET(parse_leaf(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003128 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003129 case LY_STMT_LEAF_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01003130 LY_CHECK_RET(parse_leaflist(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003131 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003132 case LY_STMT_LIST:
Radek Krejci01180ac2021-01-27 08:48:22 +01003133 LY_CHECK_RET(parse_list(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003134 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003135 case LY_STMT_USES:
Radek Krejci01180ac2021-01-27 08:48:22 +01003136 LY_CHECK_RET(parse_uses(ctx, &grp->node, &grp->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003137 break;
3138
Radek Krejcid6b76452019-09-03 17:03:03 +02003139 case LY_STMT_TYPEDEF:
Radek Krejci2a9fc652021-01-22 17:44:34 +01003140 LY_CHECK_RET(parse_typedef(ctx, &grp->node, &grp->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003141 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003142 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003143 PARSER_CHECK_STMTVER2_RET(ctx, "action", "grouping");
Radek Krejci2a9fc652021-01-22 17:44:34 +01003144 LY_CHECK_RET(parse_action(ctx, &grp->node, &grp->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003145 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003146 case LY_STMT_GROUPING:
Radek Krejci2a9fc652021-01-22 17:44:34 +01003147 LY_CHECK_RET(parse_grouping(ctx, &grp->node, &grp->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003148 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003149 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003150 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "grouping");
Radek Krejci2a9fc652021-01-22 17:44:34 +01003151 LY_CHECK_RET(parse_notif(ctx, &grp->node, &grp->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003152 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003153 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003154 LY_CHECK_RET(parse_ext(ctx, word, word_len, grp, LY_STMT_GROUPING, 0, &grp->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003155 break;
3156 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003157 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "grouping");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003158 return LY_EVALID;
3159 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003160 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, grp->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003161 }
Michal Vasko12ef5362022-09-16 15:13:58 +02003162
aPiecek63e080d2021-06-29 13:53:28 +02003163 /* store data for collision check */
3164 if (parent) {
3165 assert(ctx->main_ctx);
3166 LY_CHECK_RET(ly_set_add(&ctx->main_ctx->grps_nodes, parent, 0, NULL));
3167 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003168
Michal Vasko12ef5362022-09-16 15:13:58 +02003169cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003170 return ret;
3171}
3172
Michal Vaskoea5abea2018-09-18 13:10:54 +02003173/**
David Sedlák0d6de5a2019-07-22 13:25:44 +02003174 * @brief Parse the augment statement.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003175 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003176 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003177 * @param[in,out] augments Augments to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003178 * @return LY_ERR values.
3179 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003180LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02003181parse_augment(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node_augment **augments)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003182{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003183 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003184 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003185 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003186 enum ly_stmt kw;
Radek Krejci2a9fc652021-01-22 17:44:34 +01003187 struct lysp_node_augment *aug;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003188
Radek Krejci2a9fc652021-01-22 17:44:34 +01003189 LY_LIST_NEW_RET(PARSER_CTX(ctx), augments, aug, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003190
3191 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003192 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vaskob36053d2020-03-26 15:49:30 +01003193 CHECK_NONEMPTY(ctx, word_len, "augment");
Michal Vasko12ef5362022-09-16 15:13:58 +02003194 INSERT_WORD_GOTO(ctx, buf, aug->nodeid, word, word_len, ret, cleanup);
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003195 aug->nodetype = LYS_AUGMENT;
3196 aug->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003197
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003198 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003199 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003200 case LY_STMT_DESCRIPTION:
Michal Vasko72c6d642024-02-27 14:59:01 +01003201 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 +02003202 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003203 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003204 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &aug->iffeatures, Y_STR_ARG, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003205 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003206 case LY_STMT_REFERENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01003207 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 +02003208 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003209 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003210 LY_CHECK_RET(parse_status(ctx, &aug->flags, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003211 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003212 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003213 LY_CHECK_RET(parse_when(ctx, &aug->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003214 break;
3215
Radek Krejcid6b76452019-09-03 17:03:03 +02003216 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003217 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "augment");
Radek Krejci0f969882020-08-21 16:56:47 +02003218 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003219 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003220 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003221 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003222 case LY_STMT_CASE:
Radek Krejci33090f92020-12-17 20:12:46 +01003223 LY_CHECK_RET(parse_case(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003224 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003225 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01003226 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003227 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003228 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003229 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003230 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003231 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003232 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003233 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003234 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003235 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003236 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003237 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003238 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003239 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003240 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01003241 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)aug, &aug->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003242 break;
3243
Radek Krejcid6b76452019-09-03 17:03:03 +02003244 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003245 PARSER_CHECK_STMTVER2_RET(ctx, "action", "augment");
Radek Krejci33090f92020-12-17 20:12:46 +01003246 LY_CHECK_RET(parse_action(ctx, (struct lysp_node *)aug, &aug->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003247 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003248 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003249 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "augment");
Radek Krejci33090f92020-12-17 20:12:46 +01003250 LY_CHECK_RET(parse_notif(ctx, (struct lysp_node *)aug, &aug->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003251 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003252 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003253 LY_CHECK_RET(parse_ext(ctx, word, word_len, aug, LY_STMT_AUGMENT, 0, &aug->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003254 break;
3255 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003256 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "augment");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003257 return LY_EVALID;
3258 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003259 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, aug->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003260 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003261
Michal Vasko12ef5362022-09-16 15:13:58 +02003262cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003263 return ret;
3264}
3265
Michal Vaskoea5abea2018-09-18 13:10:54 +02003266/**
3267 * @brief Parse the uses statement.
3268 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003269 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003270 * @param[in,out] siblings Siblings to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003271 * @return LY_ERR values.
3272 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003273LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02003274parse_uses(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003275{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003276 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003277 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003278 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003279 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003280 struct lysp_node_uses *uses;
3281
David Sedlák60adc092019-08-06 15:57:02 +02003282 /* create uses structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003283 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, uses, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003284 uses->nodetype = LYS_USES;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003285 uses->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003286
Michal Vasko7fbc8162018-09-17 10:35:16 +02003287 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003288 LY_CHECK_RET(get_argument(ctx, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02003289 INSERT_WORD_GOTO(ctx, buf, uses->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003290
3291 /* parse substatements */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003292 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003293 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003294 case LY_STMT_DESCRIPTION:
Michal Vasko72c6d642024-02-27 14:59:01 +01003295 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 +02003296 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003297 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003298 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &uses->iffeatures, Y_STR_ARG, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003299 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003300 case LY_STMT_REFERENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01003301 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 +02003302 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003303 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003304 LY_CHECK_RET(parse_status(ctx, &uses->flags, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003305 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003306 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003307 LY_CHECK_RET(parse_when(ctx, &uses->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003308 break;
3309
Radek Krejcid6b76452019-09-03 17:03:03 +02003310 case LY_STMT_REFINE:
Radek Krejci33090f92020-12-17 20:12:46 +01003311 LY_CHECK_RET(parse_refine(ctx, &uses->refines));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003312 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003313 case LY_STMT_AUGMENT:
Radek Krejci33090f92020-12-17 20:12:46 +01003314 LY_CHECK_RET(parse_augment(ctx, (struct lysp_node *)uses, &uses->augments));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003315 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003316 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003317 LY_CHECK_RET(parse_ext(ctx, word, word_len, uses, LY_STMT_USES, 0, &uses->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003318 break;
3319 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003320 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "uses");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003321 return LY_EVALID;
3322 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003323 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, uses->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003324 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003325
Michal Vasko12ef5362022-09-16 15:13:58 +02003326cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003327 return ret;
3328}
3329
Michal Vaskoea5abea2018-09-18 13:10:54 +02003330/**
3331 * @brief Parse the case statement.
3332 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003333 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003334 * @param[in,out] siblings Siblings to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003335 * @return LY_ERR values.
3336 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003337LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02003338parse_case(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003339{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003340 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003341 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003342 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003343 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003344 struct lysp_node_case *cas;
3345
David Sedlák60adc092019-08-06 15:57:02 +02003346 /* create new case structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003347 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, cas, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003348 cas->nodetype = LYS_CASE;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003349 cas->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003350
Michal Vasko7fbc8162018-09-17 10:35:16 +02003351 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003352 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02003353 INSERT_WORD_GOTO(ctx, buf, cas->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003354
3355 /* parse substatements */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003356 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003357 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003358 case LY_STMT_DESCRIPTION:
Michal Vasko72c6d642024-02-27 14:59:01 +01003359 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 +02003360 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003361 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003362 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &cas->iffeatures, Y_STR_ARG, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003363 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003364 case LY_STMT_REFERENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01003365 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 +02003366 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003367 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003368 LY_CHECK_RET(parse_status(ctx, &cas->flags, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003369 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003370 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003371 LY_CHECK_RET(parse_when(ctx, &cas->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003372 break;
3373
Radek Krejcid6b76452019-09-03 17:03:03 +02003374 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003375 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "case");
Radek Krejci0f969882020-08-21 16:56:47 +02003376 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003377 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003378 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003379 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003380 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01003381 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003382 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003383 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003384 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003385 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003386 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003387 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003388 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003389 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003390 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003391 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003392 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003393 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003394 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003395 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01003396 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)cas, &cas->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003397 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003398 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003399 LY_CHECK_RET(parse_ext(ctx, word, word_len, cas, LY_STMT_CASE, 0, &cas->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003400 break;
3401 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003402 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "case");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003403 return LY_EVALID;
3404 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003405 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, cas->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003406 }
Michal Vasko12ef5362022-09-16 15:13:58 +02003407
3408cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003409 return ret;
3410}
3411
Michal Vaskoea5abea2018-09-18 13:10:54 +02003412/**
3413 * @brief Parse the choice statement.
3414 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003415 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003416 * @param[in,out] siblings Siblings to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003417 * @return LY_ERR values.
3418 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003419LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02003420parse_choice(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003421{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003422 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003423 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003424 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003425 enum ly_stmt kw;
Radek Krejci44ceedc2018-10-02 15:54:31 +02003426 struct lysp_node_choice *choice;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003427
David Sedlák60adc092019-08-06 15:57:02 +02003428 /* create new choice structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003429 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, choice, next, LY_EMEM);
Radek Krejci44ceedc2018-10-02 15:54:31 +02003430 choice->nodetype = LYS_CHOICE;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003431 choice->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003432
Michal Vasko7fbc8162018-09-17 10:35:16 +02003433 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003434 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02003435 INSERT_WORD_GOTO(ctx, buf, choice->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003436
3437 /* parse substatements */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003438 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003439 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003440 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01003441 LY_CHECK_RET(parse_config(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_DESCRIPTION:
Michal Vasko72c6d642024-02-27 14:59:01 +01003444 LY_CHECK_RET(parse_text_field(ctx, choice->dsc, LY_STMT_DESCRIPTION, 0, &choice->dsc, 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_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003448 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &choice->iffeatures, Y_STR_ARG, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003449 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003450 case LY_STMT_MANDATORY:
Radek Krejci33090f92020-12-17 20:12:46 +01003451 LY_CHECK_RET(parse_mandatory(ctx, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003452 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003453 case LY_STMT_REFERENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01003454 LY_CHECK_RET(parse_text_field(ctx, choice->ref, LY_STMT_REFERENCE, 0, &choice->ref, Y_STR_ARG, NULL,
3455 &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003456 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003457 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003458 LY_CHECK_RET(parse_status(ctx, &choice->flags, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003459 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003460 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003461 LY_CHECK_RET(parse_when(ctx, &choice->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003462 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003463 case LY_STMT_DEFAULT:
Michal Vasko193dacd2022-10-13 08:43:05 +02003464 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 +01003465 &choice->dflt.flags, &choice->exts));
Michal Vasko8a67eff2021-12-07 14:04:47 +01003466 choice->dflt.mod = PARSER_CUR_PMOD(ctx);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003467 break;
3468
Radek Krejcid6b76452019-09-03 17:03:03 +02003469 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003470 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "choice");
Radek Krejci0f969882020-08-21 16:56:47 +02003471 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003472 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003473 LY_CHECK_RET(parse_any(ctx, kw, (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_CASE:
Radek Krejci33090f92020-12-17 20:12:46 +01003476 LY_CHECK_RET(parse_case(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_CHOICE:
Radek Krejci335332a2019-09-05 13:03:35 +02003479 PARSER_CHECK_STMTVER2_RET(ctx, "choice", "choice");
Radek Krejci33090f92020-12-17 20:12:46 +01003480 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003481 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003482 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003483 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003484 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003485 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003486 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003487 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003488 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003489 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003490 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003491 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003492 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)choice, &choice->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003493 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003494 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003495 LY_CHECK_RET(parse_ext(ctx, word, word_len, choice, LY_STMT_CHOICE, 0, &choice->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003496 break;
3497 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003498 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "choice");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003499 return LY_EVALID;
3500 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003501 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, choice->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003502 }
Michal Vasko12ef5362022-09-16 15:13:58 +02003503
3504cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003505 return ret;
3506}
3507
Michal Vaskoea5abea2018-09-18 13:10:54 +02003508/**
3509 * @brief Parse the container statement.
3510 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003511 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003512 * @param[in,out] siblings Siblings to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003513 * @return LY_ERR values.
3514 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003515LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02003516parse_container(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003517{
3518 LY_ERR ret = 0;
3519 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003520 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003521 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003522 struct lysp_node_container *cont;
3523
David Sedlák60adc092019-08-06 15:57:02 +02003524 /* create new container structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003525 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, cont, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003526 cont->nodetype = LYS_CONTAINER;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003527 cont->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003528
Michal Vasko7fbc8162018-09-17 10:35:16 +02003529 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003530 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02003531 INSERT_WORD_GOTO(ctx, buf, cont->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003532
3533 /* parse substatements */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003534 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003535 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003536 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01003537 LY_CHECK_RET(parse_config(ctx, &cont->flags, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003538 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003539 case LY_STMT_DESCRIPTION:
Michal Vasko72c6d642024-02-27 14:59:01 +01003540 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 +02003541 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003542 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003543 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &cont->iffeatures, Y_STR_ARG, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003544 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003545 case LY_STMT_REFERENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01003546 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 +02003547 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003548 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003549 LY_CHECK_RET(parse_status(ctx, &cont->flags, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003550 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003551 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003552 LY_CHECK_RET(parse_when(ctx, &cont->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003553 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003554 case LY_STMT_PRESENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01003555 LY_CHECK_RET(parse_text_field(ctx, cont->presence, LY_STMT_PRESENCE, 0, &cont->presence, Y_STR_ARG, NULL,
3556 &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003557 break;
3558
Radek Krejcid6b76452019-09-03 17:03:03 +02003559 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003560 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "container");
Radek Krejci0f969882020-08-21 16:56:47 +02003561 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003562 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003563 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003564 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003565 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01003566 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003567 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003568 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003569 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003570 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003571 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003572 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003573 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003574 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003575 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003576 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003577 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003578 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003579 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003580 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01003581 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)cont, &cont->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003582 break;
3583
Radek Krejcid6b76452019-09-03 17:03:03 +02003584 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01003585 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)cont, &cont->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003586 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003587 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01003588 LY_CHECK_RET(parse_restrs(ctx, kw, &cont->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003589 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003590 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003591 PARSER_CHECK_STMTVER2_RET(ctx, "action", "container");
Radek Krejci33090f92020-12-17 20:12:46 +01003592 LY_CHECK_RET(parse_action(ctx, (struct lysp_node *)cont, &cont->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003593 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003594 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01003595 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)cont, &cont->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003596 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003597 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003598 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "container");
Radek Krejci33090f92020-12-17 20:12:46 +01003599 LY_CHECK_RET(parse_notif(ctx, (struct lysp_node *)cont, &cont->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003600 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003601 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003602 LY_CHECK_RET(parse_ext(ctx, word, word_len, cont, LY_STMT_CONTAINER, 0, &cont->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003603 break;
3604 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003605 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "container");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003606 return LY_EVALID;
3607 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003608 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, cont->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003609 }
Radek Krejci2a9fc652021-01-22 17:44:34 +01003610
Michal Vasko12ef5362022-09-16 15:13:58 +02003611cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003612 return ret;
3613}
3614
Michal Vaskoea5abea2018-09-18 13:10:54 +02003615/**
3616 * @brief Parse the list statement.
3617 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003618 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003619 * @param[in,out] siblings Siblings to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003620 * @return LY_ERR values.
3621 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003622LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02003623parse_list(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003624{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003625 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003626 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003627 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003628 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003629 struct lysp_node_list *list;
3630
David Sedlák60adc092019-08-06 15:57:02 +02003631 /* create new list structure */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003632 LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, list, next, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003633 list->nodetype = LYS_LIST;
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003634 list->parent = parent;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003635
Michal Vasko7fbc8162018-09-17 10:35:16 +02003636 /* get name */
Radek Krejci33090f92020-12-17 20:12:46 +01003637 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02003638 INSERT_WORD_GOTO(ctx, buf, list->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003639
3640 /* parse substatements */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003641 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003642 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003643 case LY_STMT_CONFIG:
Radek Krejci33090f92020-12-17 20:12:46 +01003644 LY_CHECK_RET(parse_config(ctx, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003645 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003646 case LY_STMT_DESCRIPTION:
Michal Vasko72c6d642024-02-27 14:59:01 +01003647 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 +02003648 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003649 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003650 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &list->iffeatures, Y_STR_ARG, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003651 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003652 case LY_STMT_REFERENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01003653 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 +02003654 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003655 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003656 LY_CHECK_RET(parse_status(ctx, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003657 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003658 case LY_STMT_WHEN:
Radek Krejci33090f92020-12-17 20:12:46 +01003659 LY_CHECK_RET(parse_when(ctx, &list->when));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003660 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003661 case LY_STMT_KEY:
Michal Vasko72c6d642024-02-27 14:59:01 +01003662 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 +02003663 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003664 case LY_STMT_MAX_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01003665 LY_CHECK_RET(parse_maxelements(ctx, &list->max, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003666 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003667 case LY_STMT_MIN_ELEMENTS:
Radek Krejci33090f92020-12-17 20:12:46 +01003668 LY_CHECK_RET(parse_minelements(ctx, &list->min, &list->flags, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003669 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003670 case LY_STMT_ORDERED_BY:
Michal Vasko193dacd2022-10-13 08:43:05 +02003671 LY_CHECK_RET(parse_orderedby(ctx, &list->node));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003672 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003673 case LY_STMT_UNIQUE:
Radek Krejcifc596f92021-02-26 22:40:26 +01003674 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_UNIQUE, &list->uniques, Y_STR_ARG, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003675 break;
3676
Radek Krejcid6b76452019-09-03 17:03:03 +02003677 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02003678 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "list");
Radek Krejci0f969882020-08-21 16:56:47 +02003679 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02003680 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01003681 LY_CHECK_RET(parse_any(ctx, kw, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003682 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003683 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01003684 LY_CHECK_RET(parse_choice(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003685 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003686 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01003687 LY_CHECK_RET(parse_container(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003688 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003689 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01003690 LY_CHECK_RET(parse_leaf(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003691 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003692 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003693 LY_CHECK_RET(parse_leaflist(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003694 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003695 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01003696 LY_CHECK_RET(parse_list(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003697 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003698 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01003699 LY_CHECK_RET(parse_uses(ctx, (struct lysp_node *)list, &list->child));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003700 break;
3701
Radek Krejcid6b76452019-09-03 17:03:03 +02003702 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01003703 LY_CHECK_RET(parse_typedef(ctx, (struct lysp_node *)list, &list->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003704 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003705 case LY_STMT_MUST:
Radek Krejci33090f92020-12-17 20:12:46 +01003706 LY_CHECK_RET(parse_restrs(ctx, kw, &list->musts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003707 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003708 case LY_STMT_ACTION:
Radek Krejci335332a2019-09-05 13:03:35 +02003709 PARSER_CHECK_STMTVER2_RET(ctx, "action", "list");
Radek Krejci33090f92020-12-17 20:12:46 +01003710 LY_CHECK_RET(parse_action(ctx, (struct lysp_node *)list, &list->actions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003711 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003712 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01003713 LY_CHECK_RET(parse_grouping(ctx, (struct lysp_node *)list, &list->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003714 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003715 case LY_STMT_NOTIFICATION:
Radek Krejci335332a2019-09-05 13:03:35 +02003716 PARSER_CHECK_STMTVER2_RET(ctx, "notification", "list");
Radek Krejci33090f92020-12-17 20:12:46 +01003717 LY_CHECK_RET(parse_notif(ctx, (struct lysp_node *)list, &list->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003718 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003719 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003720 LY_CHECK_RET(parse_ext(ctx, word, word_len, list, LY_STMT_LIST, 0, &list->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003721 break;
3722 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003723 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "list");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003724 return LY_EVALID;
3725 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003726 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, list->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003727 }
Radek Krejci7fc68292019-06-12 13:51:09 +02003728
Michal Vasko12ef5362022-09-16 15:13:58 +02003729cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003730 return ret;
3731}
3732
Michal Vaskoea5abea2018-09-18 13:10:54 +02003733/**
3734 * @brief Parse the yin-element statement.
3735 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003736 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02003737 * @param[in,out] ext Extension to fill.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003738 * @return LY_ERR values.
3739 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003740static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02003741parse_yinelement(struct lysp_yang_ctx *ctx, struct lysp_ext *ext)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003742{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003743 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003744 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003745 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003746 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003747
Michal Vasko193dacd2022-10-13 08:43:05 +02003748 if (ext->flags & LYS_YINELEM_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +02003749 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003750 return LY_EVALID;
3751 }
3752
3753 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003754 LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003755
Radek Krejcif13b87b2020-12-01 22:02:17 +01003756 if ((word_len == ly_strlen_const("true")) && !strncmp(word, "true", word_len)) {
Michal Vasko193dacd2022-10-13 08:43:05 +02003757 ext->flags |= LYS_YINELEM_TRUE;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003758 } else if ((word_len == ly_strlen_const("false")) && !strncmp(word, "false", word_len)) {
Michal Vasko193dacd2022-10-13 08:43:05 +02003759 ext->flags |= LYS_YINELEM_FALSE;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003760 } else {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02003761 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003762 free(buf);
3763 return LY_EVALID;
3764 }
3765 free(buf);
3766
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003767 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003768 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003769 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003770 LY_CHECK_RET(parse_ext(ctx, word, word_len, ext, LY_STMT_YIN_ELEMENT, 0, &ext->exts));
Michal Vaskod989ba02020-08-24 10:59:24 +02003771 LY_CHECK_RET(ret);
3772 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003773 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003774 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003775 return LY_EVALID;
3776 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003777 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003778 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003779
3780cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003781 return ret;
3782}
3783
Michal Vaskoea5abea2018-09-18 13:10:54 +02003784/**
David Sedlák2444f8f2019-07-09 11:02:47 +02003785 * @brief Parse the argument statement.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003786 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003787 * @param[in] ctx yang parser context for logging.
Michal Vasko193dacd2022-10-13 08:43:05 +02003788 * @param[in,out] ext Extension to fill.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003789 * @return LY_ERR values.
3790 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003791static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02003792parse_argument(struct lysp_yang_ctx *ctx, struct lysp_ext *ext)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003793{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003794 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003795 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003796 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003797 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003798
Michal Vasko193dacd2022-10-13 08:43:05 +02003799 if (ext->argname) {
David Sedlákb3077192019-06-19 10:55:37 +02003800 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003801 return LY_EVALID;
3802 }
3803
3804 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003805 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko193dacd2022-10-13 08:43:05 +02003806 INSERT_WORD_GOTO(ctx, buf, ext->argname, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003807
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003808 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003809 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003810 case LY_STMT_YIN_ELEMENT:
Michal Vasko193dacd2022-10-13 08:43:05 +02003811 LY_CHECK_RET(parse_yinelement(ctx, ext));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003812 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003813 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003814 LY_CHECK_RET(parse_ext(ctx, word, word_len, ext, LY_STMT_ARGUMENT, 0, &ext->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003815 break;
3816 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003817 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003818 return LY_EVALID;
3819 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003820 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, NULL, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003821 }
Michal Vasko12ef5362022-09-16 15:13:58 +02003822
3823cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003824 return ret;
3825}
3826
Michal Vaskoea5abea2018-09-18 13:10:54 +02003827/**
3828 * @brief Parse the extension statement.
3829 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003830 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003831 * @param[in,out] extensions Extensions to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003832 * @return LY_ERR values.
3833 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003834static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02003835parse_extension(struct lysp_yang_ctx *ctx, struct lysp_ext **extensions)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003836{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003837 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003838 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003839 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02003840 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003841 struct lysp_ext *ex;
3842
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003843 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *extensions, ex, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003844
3845 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01003846 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02003847 INSERT_WORD_GOTO(ctx, buf, ex->name, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003848
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003849 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003850 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003851 case LY_STMT_DESCRIPTION:
Michal Vasko72c6d642024-02-27 14:59:01 +01003852 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 +02003853 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003854 case LY_STMT_REFERENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01003855 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 +02003856 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003857 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01003858 LY_CHECK_RET(parse_status(ctx, &ex->flags, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003859 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003860 case LY_STMT_ARGUMENT:
Michal Vasko193dacd2022-10-13 08:43:05 +02003861 LY_CHECK_RET(parse_argument(ctx, ex));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003862 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003863 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003864 LY_CHECK_RET(parse_ext(ctx, word, word_len, ex, LY_STMT_EXTENSION, 0, &ex->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003865 break;
3866 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02003867 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "extension");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003868 return LY_EVALID;
3869 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003870 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, ex->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003871 }
Michal Vasko12ef5362022-09-16 15:13:58 +02003872
3873cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003874 return ret;
3875}
3876
Michal Vaskoea5abea2018-09-18 13:10:54 +02003877/**
3878 * @brief Parse the deviate statement.
3879 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02003880 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003881 * @param[in,out] deviates Deviates to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02003882 * @return LY_ERR values.
3883 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02003884LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02003885parse_deviate(struct lysp_yang_ctx *ctx, struct lysp_deviate **deviates)
Michal Vasko7fbc8162018-09-17 10:35:16 +02003886{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01003887 LY_ERR ret = LY_SUCCESS;
Michal Vasko12ef5362022-09-16 15:13:58 +02003888 char *buf = NULL, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02003889 size_t word_len, dev_mod;
Radek Krejcid6b76452019-09-03 17:03:03 +02003890 enum ly_stmt kw;
Michal Vaskoc636ea42022-09-16 10:20:31 +02003891 struct lysf_ctx fctx = {.ctx = PARSER_CTX(ctx)};
Michal Vasko12ef5362022-09-16 15:13:58 +02003892 struct lysp_deviate *d = NULL;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003893 struct lysp_deviate_add *d_add = NULL;
3894 struct lysp_deviate_rpl *d_rpl = NULL;
3895 struct lysp_deviate_del *d_del = NULL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003896 const char **d_units = NULL;
3897 struct lysp_qname **d_uniques = NULL, **d_dflts = NULL;
Radek Krejci4ad42aa2019-07-23 16:55:58 +02003898 struct lysp_restr **d_musts = NULL;
3899 uint16_t *d_flags = 0;
3900 uint32_t *d_min = 0, *d_max = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003901
3902 /* get value */
Michal Vasko12ef5362022-09-16 15:13:58 +02003903 LY_CHECK_GOTO(ret = get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003904
Radek Krejcif13b87b2020-12-01 22:02:17 +01003905 if ((word_len == ly_strlen_const("not-supported")) && !strncmp(word, "not-supported", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003906 dev_mod = LYS_DEV_NOT_SUPPORTED;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003907 } else if ((word_len == ly_strlen_const("add")) && !strncmp(word, "add", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003908 dev_mod = LYS_DEV_ADD;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003909 } else if ((word_len == ly_strlen_const("replace")) && !strncmp(word, "replace", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003910 dev_mod = LYS_DEV_REPLACE;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003911 } else if ((word_len == ly_strlen_const("delete")) && !strncmp(word, "delete", word_len)) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003912 dev_mod = LYS_DEV_DELETE;
3913 } else {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02003914 LOGVAL_PARSER(ctx, LY_VCODE_INVAL, (int)word_len, word, "deviate");
Michal Vasko12ef5362022-09-16 15:13:58 +02003915 ret = LY_EVALID;
3916 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003917 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02003918
3919 /* create structure */
3920 switch (dev_mod) {
3921 case LYS_DEV_NOT_SUPPORTED:
3922 d = calloc(1, sizeof *d);
Michal Vasko12ef5362022-09-16 15:13:58 +02003923 LY_CHECK_ERR_GOTO(!d, LOGMEM(PARSER_CTX(ctx)); ret = LY_EMEM, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003924 break;
3925 case LYS_DEV_ADD:
3926 d_add = calloc(1, sizeof *d_add);
Michal Vasko12ef5362022-09-16 15:13:58 +02003927 LY_CHECK_ERR_GOTO(!d_add, LOGMEM(PARSER_CTX(ctx)); ret = LY_EMEM, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003928 d = (struct lysp_deviate *)d_add;
3929 d_units = &d_add->units;
3930 d_uniques = &d_add->uniques;
3931 d_dflts = &d_add->dflts;
3932 d_musts = &d_add->musts;
3933 d_flags = &d_add->flags;
3934 d_min = &d_add->min;
3935 d_max = &d_add->max;
3936 break;
3937 case LYS_DEV_REPLACE:
3938 d_rpl = calloc(1, sizeof *d_rpl);
Michal Vasko12ef5362022-09-16 15:13:58 +02003939 LY_CHECK_ERR_GOTO(!d_rpl, LOGMEM(PARSER_CTX(ctx)); ret = LY_EMEM, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003940 d = (struct lysp_deviate *)d_rpl;
3941 d_units = &d_rpl->units;
3942 d_flags = &d_rpl->flags;
3943 d_min = &d_rpl->min;
3944 d_max = &d_rpl->max;
3945 break;
3946 case LYS_DEV_DELETE:
3947 d_del = calloc(1, sizeof *d_del);
Michal Vasko12ef5362022-09-16 15:13:58 +02003948 LY_CHECK_ERR_GOTO(!d_del, LOGMEM(PARSER_CTX(ctx)); ret = LY_EMEM, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003949 d = (struct lysp_deviate *)d_del;
3950 d_units = &d_del->units;
3951 d_uniques = &d_del->uniques;
3952 d_dflts = &d_del->dflts;
3953 d_musts = &d_del->musts;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003954 break;
3955 default:
3956 assert(0);
Michal Vasko12ef5362022-09-16 15:13:58 +02003957 LOGINT(PARSER_CTX(ctx));
3958 ret = LY_EINT;
3959 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003960 }
3961 d->mod = dev_mod;
3962
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02003963 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02003964 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02003965 case LY_STMT_CONFIG:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003966 switch (dev_mod) {
3967 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003968 case LYS_DEV_DELETE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003969 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), lyplg_ext_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02003970 ret = LY_EVALID;
3971 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003972 default:
Michal Vasko12ef5362022-09-16 15:13:58 +02003973 LY_CHECK_GOTO(ret = parse_config(ctx, d_flags, &d->exts), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003974 break;
3975 }
3976 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003977 case LY_STMT_DEFAULT:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003978 switch (dev_mod) {
3979 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko193dacd2022-10-13 08:43:05 +02003980 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), lyplg_ext_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02003981 ret = LY_EVALID;
3982 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02003983 case LYS_DEV_REPLACE:
Michal Vasko72c6d642024-02-27 14:59:01 +01003984 ret = parse_text_field(ctx, &d_rpl->dflt, LY_STMT_DEFAULT, 0, &d_rpl->dflt.str, Y_STR_ARG,
3985 &d_rpl->dflt.flags, &d->exts);
Michal Vasko193dacd2022-10-13 08:43:05 +02003986 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko8a67eff2021-12-07 14:04:47 +01003987 d_rpl->dflt.mod = PARSER_CUR_PMOD(ctx);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003988 break;
3989 default:
Michal Vasko12ef5362022-09-16 15:13:58 +02003990 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 +02003991 break;
3992 }
3993 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02003994 case LY_STMT_MANDATORY:
Michal Vasko7fbc8162018-09-17 10:35:16 +02003995 switch (dev_mod) {
3996 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02003997 case LYS_DEV_DELETE:
Michal Vasko193dacd2022-10-13 08:43:05 +02003998 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), lyplg_ext_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02003999 ret = LY_EVALID;
4000 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004001 default:
Michal Vasko12ef5362022-09-16 15:13:58 +02004002 LY_CHECK_GOTO(ret = parse_mandatory(ctx, d_flags, &d->exts), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004003 break;
4004 }
4005 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004006 case LY_STMT_MAX_ELEMENTS:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004007 switch (dev_mod) {
4008 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02004009 case LYS_DEV_DELETE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004010 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), lyplg_ext_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02004011 ret = LY_EVALID;
4012 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004013 default:
Michal Vasko12ef5362022-09-16 15:13:58 +02004014 LY_CHECK_GOTO(ret = parse_maxelements(ctx, d_max, d_flags, &d->exts), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004015 break;
4016 }
4017 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004018 case LY_STMT_MIN_ELEMENTS:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004019 switch (dev_mod) {
4020 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02004021 case LYS_DEV_DELETE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004022 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), lyplg_ext_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02004023 ret = LY_EVALID;
4024 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004025 default:
Michal Vasko12ef5362022-09-16 15:13:58 +02004026 LY_CHECK_GOTO(ret = parse_minelements(ctx, d_min, d_flags, &d->exts), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004027 break;
4028 }
4029 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004030 case LY_STMT_MUST:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004031 switch (dev_mod) {
4032 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02004033 case LYS_DEV_REPLACE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004034 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), lyplg_ext_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02004035 ret = LY_EVALID;
4036 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004037 default:
Michal Vasko12ef5362022-09-16 15:13:58 +02004038 LY_CHECK_GOTO(ret = parse_restrs(ctx, kw, d_musts), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004039 break;
4040 }
4041 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004042 case LY_STMT_TYPE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004043 switch (dev_mod) {
4044 case LYS_DEV_NOT_SUPPORTED:
4045 case LYS_DEV_ADD:
4046 case LYS_DEV_DELETE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004047 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), lyplg_ext_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02004048 ret = LY_EVALID;
4049 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004050 default:
Radek Krejci2c02f3e2018-10-16 10:54:38 +02004051 if (d_rpl->type) {
Michal Vasko193dacd2022-10-13 08:43:05 +02004052 LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, lyplg_ext_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02004053 ret = LY_EVALID;
4054 goto cleanup;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02004055 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004056 d_rpl->type = calloc(1, sizeof *d_rpl->type);
Michal Vasko12ef5362022-09-16 15:13:58 +02004057 LY_CHECK_ERR_GOTO(!d_rpl->type, LOGMEM(PARSER_CTX(ctx)); ret = LY_EMEM, cleanup);
4058 LY_CHECK_GOTO(ret = parse_type(ctx, d_rpl->type), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004059 break;
4060 }
4061 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004062 case LY_STMT_UNIQUE:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004063 switch (dev_mod) {
4064 case LYS_DEV_NOT_SUPPORTED:
4065 case LYS_DEV_REPLACE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004066 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), lyplg_ext_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02004067 ret = LY_EVALID;
4068 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004069 default:
Michal Vasko12ef5362022-09-16 15:13:58 +02004070 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 +02004071 break;
4072 }
4073 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004074 case LY_STMT_UNITS:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004075 switch (dev_mod) {
4076 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko193dacd2022-10-13 08:43:05 +02004077 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), lyplg_ext_stmt2str(kw));
Michal Vasko12ef5362022-09-16 15:13:58 +02004078 ret = LY_EVALID;
4079 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004080 default:
Michal Vasko72c6d642024-02-27 14:59:01 +01004081 ret = parse_text_field(ctx, *d_units, LY_STMT_UNITS, 0, d_units, Y_STR_ARG, NULL, &d->exts);
4082 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004083 break;
4084 }
4085 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004086 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004087 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 +02004088 break;
4089 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02004090 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "deviate");
Michal Vasko12ef5362022-09-16 15:13:58 +02004091 ret = LY_EVALID;
4092 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004093 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004094 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, d->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004095 }
Michal Vasko12ef5362022-09-16 15:13:58 +02004096
4097cleanup:
4098 free(buf);
4099 if (ret) {
Michal Vaskoc636ea42022-09-16 10:20:31 +02004100 lysp_deviate_free(&fctx, d);
Michal Vasko12ef5362022-09-16 15:13:58 +02004101 free(d);
4102 } else {
4103 /* insert into siblings */
4104 LY_LIST_INSERT(deviates, d, next);
4105 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004106 return ret;
4107}
4108
Michal Vaskoea5abea2018-09-18 13:10:54 +02004109/**
4110 * @brief Parse the deviation statement.
4111 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004112 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004113 * @param[in,out] deviations Deviations to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004114 * @return LY_ERR values.
4115 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004116LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02004117parse_deviation(struct lysp_yang_ctx *ctx, struct lysp_deviation **deviations)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004118{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004119 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004120 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004121 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004122 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004123 struct lysp_deviation *dev;
Michal Vaskoc636ea42022-09-16 10:20:31 +02004124 struct lysf_ctx fctx = {.ctx = PARSER_CTX(ctx)};
Michal Vasko7fbc8162018-09-17 10:35:16 +02004125
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004126 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *deviations, dev, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004127
4128 /* get value */
Michal Vasko12ef5362022-09-16 15:13:58 +02004129 LY_CHECK_GOTO(ret = get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len), cleanup);
Michal Vaskob36053d2020-03-26 15:49:30 +01004130 CHECK_NONEMPTY(ctx, word_len, "deviation");
Michal Vasko12ef5362022-09-16 15:13:58 +02004131 INSERT_WORD_GOTO(ctx, buf, dev->nodeid, word, word_len, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004132
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004133 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004134 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02004135 case LY_STMT_DESCRIPTION:
Michal Vasko72c6d642024-02-27 14:59:01 +01004136 ret = parse_text_field(ctx, dev->dsc, LY_STMT_DESCRIPTION, 0, &dev->dsc, Y_STR_ARG, NULL, &dev->exts);
4137 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004138 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004139 case LY_STMT_DEVIATE:
Michal Vasko12ef5362022-09-16 15:13:58 +02004140 LY_CHECK_GOTO(ret = parse_deviate(ctx, &dev->deviates), cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004141 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004142 case LY_STMT_REFERENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01004143 ret = parse_text_field(ctx, dev->ref, LY_STMT_REFERENCE, 0, &dev->ref, Y_STR_ARG, NULL, &dev->exts);
4144 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004145 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004146 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004147 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 +02004148 break;
4149 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02004150 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "deviation");
Michal Vasko12ef5362022-09-16 15:13:58 +02004151 ret = LY_EVALID;
4152 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004153 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004154 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, dev->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004155 }
Michal Vasko12ef5362022-09-16 15:13:58 +02004156
Michal Vasko7fbc8162018-09-17 10:35:16 +02004157 /* mandatory substatements */
4158 if (!dev->deviates) {
David Sedlákb3077192019-06-19 10:55:37 +02004159 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "deviate", "deviation");
Michal Vasko12ef5362022-09-16 15:13:58 +02004160 ret = LY_EVALID;
4161 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004162 }
4163
Michal Vasko12ef5362022-09-16 15:13:58 +02004164cleanup:
4165 if (ret) {
Michal Vaskoc636ea42022-09-16 10:20:31 +02004166 lysp_deviation_free(&fctx, dev);
Michal Vasko12ef5362022-09-16 15:13:58 +02004167 LY_ARRAY_DECREMENT_FREE(*deviations);
4168 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004169 return ret;
4170}
4171
Michal Vaskoea5abea2018-09-18 13:10:54 +02004172/**
4173 * @brief Parse the feature statement.
4174 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004175 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004176 * @param[in,out] features Features to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004177 * @return LY_ERR values.
4178 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004179LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02004180parse_feature(struct lysp_yang_ctx *ctx, struct lysp_feature **features)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004181{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004182 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004183 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004184 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004185 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004186 struct lysp_feature *feat;
4187
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004188 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *features, feat, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004189
4190 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01004191 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02004192 INSERT_WORD_GOTO(ctx, buf, feat->name, word, word_len, ret, cleanup);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01004193
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004194 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004195 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02004196 case LY_STMT_DESCRIPTION:
Michal Vasko72c6d642024-02-27 14:59:01 +01004197 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 +02004198 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004199 case LY_STMT_IF_FEATURE:
Radek Krejcifc596f92021-02-26 22:40:26 +01004200 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &feat->iffeatures, Y_STR_ARG, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004201 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004202 case LY_STMT_REFERENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01004203 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 +02004204 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004205 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01004206 LY_CHECK_RET(parse_status(ctx, &feat->flags, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004207 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004208 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004209 LY_CHECK_RET(parse_ext(ctx, word, word_len, feat, LY_STMT_FEATURE, 0, &feat->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004210 break;
4211 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02004212 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "feature");
Radek Krejci2c02f3e2018-10-16 10:54:38 +02004213 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004214 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004215 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, feat->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004216 }
Michal Vasko12ef5362022-09-16 15:13:58 +02004217
4218cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004219 return ret;
4220}
4221
Michal Vaskoea5abea2018-09-18 13:10:54 +02004222/**
4223 * @brief Parse the identity statement.
4224 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004225 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004226 * @param[in,out] identities Identities to add to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004227 * @return LY_ERR values.
4228 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004229LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02004230parse_identity(struct lysp_yang_ctx *ctx, struct lysp_ident **identities)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004231{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004232 LY_ERR ret = LY_SUCCESS;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004233 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004234 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004235 enum ly_stmt kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004236 struct lysp_ident *ident;
4237
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004238 LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *identities, ident, LY_EMEM);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004239
4240 /* get value */
Radek Krejci33090f92020-12-17 20:12:46 +01004241 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02004242 INSERT_WORD_GOTO(ctx, buf, ident->name, word, word_len, ret, cleanup);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01004243
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004244 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004245 switch (kw) {
Radek Krejcid6b76452019-09-03 17:03:03 +02004246 case LY_STMT_DESCRIPTION:
Michal Vasko72c6d642024-02-27 14:59:01 +01004247 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 +02004248 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004249 case LY_STMT_IF_FEATURE:
Radek Krejci335332a2019-09-05 13:03:35 +02004250 PARSER_CHECK_STMTVER2_RET(ctx, "if-feature", "identity");
Radek Krejcifc596f92021-02-26 22:40:26 +01004251 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_IF_FEATURE, &ident->iffeatures, Y_STR_ARG, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004252 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004253 case LY_STMT_REFERENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01004254 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 +02004255 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004256 case LY_STMT_STATUS:
Radek Krejci33090f92020-12-17 20:12:46 +01004257 LY_CHECK_RET(parse_status(ctx, &ident->flags, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004258 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004259 case LY_STMT_BASE:
Michal Vasko8a67eff2021-12-07 14:04:47 +01004260 if (ident->bases && (PARSER_CUR_PMOD(ctx)->version < LYS_VERSION_1_1)) {
Michal Vasko72c6d642024-02-27 14:59:01 +01004261 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG,
4262 "Identity can be derived from multiple base identities only in YANG 1.1 modules");
Radek Krejci10113652018-11-14 16:56:50 +01004263 return LY_EVALID;
4264 }
Radek Krejcifc596f92021-02-26 22:40:26 +01004265 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 +02004266 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004267 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004268 LY_CHECK_RET(parse_ext(ctx, word, word_len, ident, LY_STMT_IDENTITY, 0, &ident->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004269 break;
4270 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02004271 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "identity");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004272 return LY_EVALID;
4273 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004274 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, ident->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004275 }
Michal Vasko12ef5362022-09-16 15:13:58 +02004276
4277cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004278 return ret;
4279}
4280
Michal Vaskoea5abea2018-09-18 13:10:54 +02004281/**
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004282 * @brief Parse module substatements.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004283 *
Radek Krejci44ceedc2018-10-02 15:54:31 +02004284 * @param[in] ctx yang parser context for logging.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004285 * @param[in,out] mod Module to write to.
Michal Vaskoea5abea2018-09-18 13:10:54 +02004286 * @return LY_ERR values.
4287 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004288LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02004289parse_module(struct lysp_yang_ctx *ctx, struct lysp_module *mod)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004290{
4291 LY_ERR ret = 0;
4292 char *buf, *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004293 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004294 enum ly_stmt kw, prev_kw = 0;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004295 enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER;
Michal Vasko8dc31992021-02-22 10:30:47 +01004296 const struct lysp_submodule *dup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004297
Michal Vaskoc3781c32020-10-06 14:04:08 +02004298 mod->is_submod = 0;
4299
4300 /* module name */
Radek Krejci33090f92020-12-17 20:12:46 +01004301 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02004302 INSERT_WORD_GOTO(ctx, buf, mod->mod->name, word, word_len, ret, cleanup);
Radek Krejcifaa1eac2018-10-30 14:34:55 +01004303
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004304 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Michal Vasko7fbc8162018-09-17 10:35:16 +02004305
Radek Krejcie3846472018-10-15 15:24:51 +02004306#define CHECK_ORDER(SECTION) \
Michal Vasko193dacd2022-10-13 08:43:05 +02004307 if (mod_stmt > SECTION) {\
4308 LOGVAL_PARSER(ctx, LY_VCODE_INORD, lyplg_ext_stmt2str(kw), lyplg_ext_stmt2str(prev_kw)); return LY_EVALID;\
4309 } mod_stmt = SECTION
Radek Krejcie3846472018-10-15 15:24:51 +02004310
Michal Vasko7fbc8162018-09-17 10:35:16 +02004311 switch (kw) {
4312 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004313 case LY_STMT_NAMESPACE:
4314 case LY_STMT_PREFIX:
Radek Krejcie3846472018-10-15 15:24:51 +02004315 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4316 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004317 case LY_STMT_YANG_VERSION:
Radek Krejcie3846472018-10-15 15:24:51 +02004318 CHECK_ORDER(Y_MOD_MODULE_HEADER);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004319 break;
4320 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004321 case LY_STMT_INCLUDE:
4322 case LY_STMT_IMPORT:
Radek Krejcie3846472018-10-15 15:24:51 +02004323 CHECK_ORDER(Y_MOD_LINKAGE);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004324 break;
4325 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004326 case LY_STMT_ORGANIZATION:
4327 case LY_STMT_CONTACT:
4328 case LY_STMT_DESCRIPTION:
4329 case LY_STMT_REFERENCE:
Radek Krejcie3846472018-10-15 15:24:51 +02004330 CHECK_ORDER(Y_MOD_META);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004331 break;
4332
4333 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004334 case LY_STMT_REVISION:
Radek Krejcie3846472018-10-15 15:24:51 +02004335 CHECK_ORDER(Y_MOD_REVISION);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004336 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004337 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004338 case LY_STMT_ANYDATA:
4339 case LY_STMT_ANYXML:
4340 case LY_STMT_AUGMENT:
4341 case LY_STMT_CHOICE:
4342 case LY_STMT_CONTAINER:
4343 case LY_STMT_DEVIATION:
4344 case LY_STMT_EXTENSION:
4345 case LY_STMT_FEATURE:
4346 case LY_STMT_GROUPING:
4347 case LY_STMT_IDENTITY:
4348 case LY_STMT_LEAF:
4349 case LY_STMT_LEAF_LIST:
4350 case LY_STMT_LIST:
4351 case LY_STMT_NOTIFICATION:
4352 case LY_STMT_RPC:
4353 case LY_STMT_TYPEDEF:
4354 case LY_STMT_USES:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004355 mod_stmt = Y_MOD_BODY;
4356 break;
Michal Vasko59a24f62021-12-14 11:18:32 +01004357 case LY_STMT_EXTENSION_INSTANCE:
4358 /* no place in the statement order defined */
4359 break;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004360 default:
4361 /* error handled in the next switch */
4362 break;
4363 }
Radek Krejcie3846472018-10-15 15:24:51 +02004364#undef CHECK_ORDER
Michal Vasko7fbc8162018-09-17 10:35:16 +02004365
Radek Krejcie3846472018-10-15 15:24:51 +02004366 prev_kw = kw;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004367 switch (kw) {
4368 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004369 case LY_STMT_YANG_VERSION:
Michal Vasko193dacd2022-10-13 08:43:05 +02004370 LY_CHECK_RET(parse_yangversion(ctx, mod));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004371 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004372 case LY_STMT_NAMESPACE:
Michal Vasko72c6d642024-02-27 14:59:01 +01004373 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 +02004374 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004375 case LY_STMT_PREFIX:
Michal Vasko72c6d642024-02-27 14:59:01 +01004376 LY_CHECK_RET(parse_text_field(ctx, mod->mod->prefix, LY_STMT_PREFIX, 0, &mod->mod->prefix, Y_IDENTIF_ARG,
4377 NULL, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004378 break;
4379
4380 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004381 case LY_STMT_INCLUDE:
Radek Krejci33090f92020-12-17 20:12:46 +01004382 LY_CHECK_RET(parse_include(ctx, mod->mod->name, &mod->includes));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004383 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004384 case LY_STMT_IMPORT:
Radek Krejci33090f92020-12-17 20:12:46 +01004385 LY_CHECK_RET(parse_import(ctx, mod->mod->prefix, &mod->imports));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004386 break;
4387
4388 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004389 case LY_STMT_ORGANIZATION:
Michal Vasko72c6d642024-02-27 14:59:01 +01004390 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 +02004391 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004392 case LY_STMT_CONTACT:
Michal Vasko72c6d642024-02-27 14:59:01 +01004393 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 +02004394 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004395 case LY_STMT_DESCRIPTION:
Michal Vasko72c6d642024-02-27 14:59:01 +01004396 LY_CHECK_RET(parse_text_field(ctx, mod->mod->dsc, LY_STMT_DESCRIPTION, 0, &mod->mod->dsc, Y_STR_ARG, NULL,
4397 &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004398 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004399 case LY_STMT_REFERENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01004400 LY_CHECK_RET(parse_text_field(ctx, mod->mod->ref, LY_STMT_REFERENCE, 0, &mod->mod->ref, Y_STR_ARG, NULL,
4401 &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004402 break;
4403
4404 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004405 case LY_STMT_REVISION:
Radek Krejci33090f92020-12-17 20:12:46 +01004406 LY_CHECK_RET(parse_revision(ctx, &mod->revs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004407 break;
4408
4409 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004410 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02004411 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "module");
Radek Krejci0f969882020-08-21 16:56:47 +02004412 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02004413 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01004414 LY_CHECK_RET(parse_any(ctx, kw, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004415 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004416 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01004417 LY_CHECK_RET(parse_choice(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004418 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004419 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01004420 LY_CHECK_RET(parse_container(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004421 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004422 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01004423 LY_CHECK_RET(parse_leaf(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004424 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004425 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01004426 LY_CHECK_RET(parse_leaflist(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004427 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004428 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01004429 LY_CHECK_RET(parse_list(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004430 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004431 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01004432 LY_CHECK_RET(parse_uses(ctx, NULL, &mod->data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004433 break;
4434
Radek Krejcid6b76452019-09-03 17:03:03 +02004435 case LY_STMT_AUGMENT:
Radek Krejci33090f92020-12-17 20:12:46 +01004436 LY_CHECK_RET(parse_augment(ctx, NULL, &mod->augments));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004437 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004438 case LY_STMT_DEVIATION:
Radek Krejci33090f92020-12-17 20:12:46 +01004439 LY_CHECK_RET(parse_deviation(ctx, &mod->deviations));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004440 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004441 case LY_STMT_EXTENSION:
Radek Krejci33090f92020-12-17 20:12:46 +01004442 LY_CHECK_RET(parse_extension(ctx, &mod->extensions));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004443 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004444 case LY_STMT_FEATURE:
Radek Krejci33090f92020-12-17 20:12:46 +01004445 LY_CHECK_RET(parse_feature(ctx, &mod->features));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004446 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004447 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01004448 LY_CHECK_RET(parse_grouping(ctx, NULL, &mod->groupings));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004449 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004450 case LY_STMT_IDENTITY:
Radek Krejci33090f92020-12-17 20:12:46 +01004451 LY_CHECK_RET(parse_identity(ctx, &mod->identities));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004452 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004453 case LY_STMT_NOTIFICATION:
Radek Krejci33090f92020-12-17 20:12:46 +01004454 LY_CHECK_RET(parse_notif(ctx, NULL, &mod->notifs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004455 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004456 case LY_STMT_RPC:
Radek Krejci33090f92020-12-17 20:12:46 +01004457 LY_CHECK_RET(parse_action(ctx, NULL, &mod->rpcs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004458 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004459 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01004460 LY_CHECK_RET(parse_typedef(ctx, NULL, &mod->typedefs));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004461 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004462 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004463 LY_CHECK_RET(parse_ext(ctx, word, word_len, mod, LY_STMT_MODULE, 0, &mod->exts));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004464 break;
4465
4466 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02004467 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "module");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004468 return LY_EVALID;
4469 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004470 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, mod->exts, ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004471 }
Radek Krejcie86bf772018-12-14 11:39:53 +01004472
Michal Vasko7fbc8162018-09-17 10:35:16 +02004473 /* mandatory substatements */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004474 if (!mod->mod->ns) {
David Sedlákb3077192019-06-19 10:55:37 +02004475 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "namespace", "module");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004476 return LY_EVALID;
4477 } else if (!mod->mod->prefix) {
David Sedlákb3077192019-06-19 10:55:37 +02004478 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "prefix", "module");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004479 return LY_EVALID;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004480 }
4481
Radek Krejcie9e987e2018-10-31 12:50:27 +01004482 /* submodules share the namespace with the module names, so there must not be
4483 * a submodule of the same name in the context, no need for revision matching */
Michal Vasko8dc31992021-02-22 10:30:47 +01004484 dup = ly_ctx_get_submodule_latest(PARSER_CTX(ctx), mod->mod->name);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004485 if (dup) {
Radek Krejci854e1552020-12-21 15:05:23 +01004486 LOGVAL_PARSER(ctx, LY_VCODE_NAME2_COL, "module", "submodule", mod->mod->name);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004487 return LY_EVALID;
4488 }
4489
Michal Vasko12ef5362022-09-16 15:13:58 +02004490cleanup:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004491 return ret;
4492}
4493
4494/**
4495 * @brief Parse submodule substatements.
4496 *
4497 * @param[in] ctx yang parser context for logging.
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004498 * @param[out] submod Parsed submodule structure.
4499 *
4500 * @return LY_ERR values.
4501 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02004502LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02004503parse_submodule(struct lysp_yang_ctx *ctx, struct lysp_submodule *submod)
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004504{
4505 LY_ERR ret = 0;
4506 char *buf, *word;
4507 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004508 enum ly_stmt kw, prev_kw = 0;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004509 enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER;
Michal Vasko8dc31992021-02-22 10:30:47 +01004510 const struct lysp_submodule *dup;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004511
Michal Vaskoc3781c32020-10-06 14:04:08 +02004512 submod->is_submod = 1;
4513
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004514 /* submodule name */
Radek Krejci33090f92020-12-17 20:12:46 +01004515 LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
Michal Vasko12ef5362022-09-16 15:13:58 +02004516 INSERT_WORD_GOTO(ctx, buf, submod->name, word, word_len, ret, cleanup);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004517
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004518 YANG_READ_SUBSTMT_FOR_GOTO(ctx, kw, word, word_len, ret, cleanup) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004519
4520#define CHECK_ORDER(SECTION) \
Michal Vasko193dacd2022-10-13 08:43:05 +02004521 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 +01004522
4523 switch (kw) {
4524 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004525 case LY_STMT_BELONGS_TO:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004526 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4527 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004528 case LY_STMT_YANG_VERSION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004529 CHECK_ORDER(Y_MOD_MODULE_HEADER);
4530 break;
4531 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004532 case LY_STMT_INCLUDE:
4533 case LY_STMT_IMPORT:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004534 CHECK_ORDER(Y_MOD_LINKAGE);
4535 break;
4536 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004537 case LY_STMT_ORGANIZATION:
4538 case LY_STMT_CONTACT:
4539 case LY_STMT_DESCRIPTION:
4540 case LY_STMT_REFERENCE:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004541 CHECK_ORDER(Y_MOD_META);
4542 break;
4543
4544 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004545 case LY_STMT_REVISION:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004546 CHECK_ORDER(Y_MOD_REVISION);
4547 break;
4548 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004549 case LY_STMT_ANYDATA:
4550 case LY_STMT_ANYXML:
4551 case LY_STMT_AUGMENT:
4552 case LY_STMT_CHOICE:
4553 case LY_STMT_CONTAINER:
4554 case LY_STMT_DEVIATION:
4555 case LY_STMT_EXTENSION:
4556 case LY_STMT_FEATURE:
4557 case LY_STMT_GROUPING:
4558 case LY_STMT_IDENTITY:
4559 case LY_STMT_LEAF:
4560 case LY_STMT_LEAF_LIST:
4561 case LY_STMT_LIST:
4562 case LY_STMT_NOTIFICATION:
4563 case LY_STMT_RPC:
4564 case LY_STMT_TYPEDEF:
4565 case LY_STMT_USES:
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004566 mod_stmt = Y_MOD_BODY;
4567 break;
Michal Vasko59a24f62021-12-14 11:18:32 +01004568 case LY_STMT_EXTENSION_INSTANCE:
4569 /* no place in the statement order defined */
4570 break;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004571 default:
4572 /* error handled in the next switch */
4573 break;
4574 }
4575#undef CHECK_ORDER
4576
4577 prev_kw = kw;
4578 switch (kw) {
4579 /* module header */
Radek Krejcid6b76452019-09-03 17:03:03 +02004580 case LY_STMT_YANG_VERSION:
Michal Vasko193dacd2022-10-13 08:43:05 +02004581 LY_CHECK_RET(parse_yangversion(ctx, (struct lysp_module *)submod));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004582 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004583 case LY_STMT_BELONGS_TO:
Michal Vasko193dacd2022-10-13 08:43:05 +02004584 LY_CHECK_RET(parse_belongsto(ctx, submod));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004585 break;
4586
4587 /* linkage */
Radek Krejcid6b76452019-09-03 17:03:03 +02004588 case LY_STMT_INCLUDE:
Radek Krejcieeee95c2021-01-19 10:57:22 +01004589 if (submod->version == LYS_VERSION_1_1) {
4590 LOGWRN(PARSER_CTX(ctx), "YANG version 1.1 expects all includes in main module, includes in submodules (%s) are not necessary.",
4591 submod->name);
4592 }
Radek Krejci33090f92020-12-17 20:12:46 +01004593 LY_CHECK_RET(parse_include(ctx, submod->name, &submod->includes));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004594 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004595 case LY_STMT_IMPORT:
Radek Krejci33090f92020-12-17 20:12:46 +01004596 LY_CHECK_RET(parse_import(ctx, submod->prefix, &submod->imports));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004597 break;
4598
4599 /* meta */
Radek Krejcid6b76452019-09-03 17:03:03 +02004600 case LY_STMT_ORGANIZATION:
Michal Vasko72c6d642024-02-27 14:59:01 +01004601 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 +01004602 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004603 case LY_STMT_CONTACT:
Michal Vasko72c6d642024-02-27 14:59:01 +01004604 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 +01004605 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004606 case LY_STMT_DESCRIPTION:
Michal Vasko72c6d642024-02-27 14:59:01 +01004607 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 +01004608 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004609 case LY_STMT_REFERENCE:
Michal Vasko72c6d642024-02-27 14:59:01 +01004610 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 +01004611 break;
4612
4613 /* revision */
Radek Krejcid6b76452019-09-03 17:03:03 +02004614 case LY_STMT_REVISION:
Radek Krejci33090f92020-12-17 20:12:46 +01004615 LY_CHECK_RET(parse_revision(ctx, &submod->revs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004616 break;
4617
4618 /* body */
Radek Krejcid6b76452019-09-03 17:03:03 +02004619 case LY_STMT_ANYDATA:
Radek Krejci335332a2019-09-05 13:03:35 +02004620 PARSER_CHECK_STMTVER2_RET(ctx, "anydata", "submodule");
Radek Krejci0f969882020-08-21 16:56:47 +02004621 /* fall through */
Radek Krejcid6b76452019-09-03 17:03:03 +02004622 case LY_STMT_ANYXML:
Radek Krejci33090f92020-12-17 20:12:46 +01004623 LY_CHECK_RET(parse_any(ctx, kw, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004624 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004625 case LY_STMT_CHOICE:
Radek Krejci33090f92020-12-17 20:12:46 +01004626 LY_CHECK_RET(parse_choice(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004627 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004628 case LY_STMT_CONTAINER:
Radek Krejci33090f92020-12-17 20:12:46 +01004629 LY_CHECK_RET(parse_container(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004630 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004631 case LY_STMT_LEAF:
Radek Krejci33090f92020-12-17 20:12:46 +01004632 LY_CHECK_RET(parse_leaf(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004633 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004634 case LY_STMT_LEAF_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01004635 LY_CHECK_RET(parse_leaflist(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004636 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004637 case LY_STMT_LIST:
Radek Krejci33090f92020-12-17 20:12:46 +01004638 LY_CHECK_RET(parse_list(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004639 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004640 case LY_STMT_USES:
Radek Krejci33090f92020-12-17 20:12:46 +01004641 LY_CHECK_RET(parse_uses(ctx, NULL, &submod->data));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004642 break;
4643
Radek Krejcid6b76452019-09-03 17:03:03 +02004644 case LY_STMT_AUGMENT:
Radek Krejci33090f92020-12-17 20:12:46 +01004645 LY_CHECK_RET(parse_augment(ctx, NULL, &submod->augments));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004646 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004647 case LY_STMT_DEVIATION:
Radek Krejci33090f92020-12-17 20:12:46 +01004648 LY_CHECK_RET(parse_deviation(ctx, &submod->deviations));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004649 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004650 case LY_STMT_EXTENSION:
Radek Krejci33090f92020-12-17 20:12:46 +01004651 LY_CHECK_RET(parse_extension(ctx, &submod->extensions));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004652 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004653 case LY_STMT_FEATURE:
Radek Krejci33090f92020-12-17 20:12:46 +01004654 LY_CHECK_RET(parse_feature(ctx, &submod->features));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004655 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004656 case LY_STMT_GROUPING:
Radek Krejci33090f92020-12-17 20:12:46 +01004657 LY_CHECK_RET(parse_grouping(ctx, NULL, &submod->groupings));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004658 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004659 case LY_STMT_IDENTITY:
Radek Krejci33090f92020-12-17 20:12:46 +01004660 LY_CHECK_RET(parse_identity(ctx, &submod->identities));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004661 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004662 case LY_STMT_NOTIFICATION:
Radek Krejci33090f92020-12-17 20:12:46 +01004663 LY_CHECK_RET(parse_notif(ctx, NULL, &submod->notifs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004664 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004665 case LY_STMT_RPC:
Radek Krejci33090f92020-12-17 20:12:46 +01004666 LY_CHECK_RET(parse_action(ctx, NULL, &submod->rpcs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004667 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004668 case LY_STMT_TYPEDEF:
Radek Krejci33090f92020-12-17 20:12:46 +01004669 LY_CHECK_RET(parse_typedef(ctx, NULL, &submod->typedefs));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004670 break;
Radek Krejcid6b76452019-09-03 17:03:03 +02004671 case LY_STMT_EXTENSION_INSTANCE:
Michal Vasko193dacd2022-10-13 08:43:05 +02004672 LY_CHECK_RET(parse_ext(ctx, word, word_len, submod, LY_STMT_SUBMODULE, 0, &submod->exts));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004673 break;
4674
4675 default:
Michal Vasko193dacd2022-10-13 08:43:05 +02004676 LOGVAL_PARSER(ctx, LY_VCODE_INCHILDSTMT, lyplg_ext_stmt2str(kw), "submodule");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004677 return LY_EVALID;
4678 }
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02004679 YANG_READ_SUBSTMT_NEXT_ITER(ctx, kw, word, word_len, submod->exts, ret, cleanup);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004680 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004681
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004682 /* mandatory substatements */
Michal Vaskoc3781c32020-10-06 14:04:08 +02004683 if (!submod->prefix) {
David Sedlákb3077192019-06-19 10:55:37 +02004684 LOGVAL_PARSER(ctx, LY_VCODE_MISSTMT, "belongs-to", "submodule");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004685 return LY_EVALID;
4686 }
4687
4688 /* submodules share the namespace with the module names, so there must not be
4689 * a submodule of the same name in the context, no need for revision matching */
Michal Vasko8dc31992021-02-22 10:30:47 +01004690 dup = ly_ctx_get_submodule_latest(PARSER_CTX(ctx), submod->name);
Michal Vaskoc3781c32020-10-06 14:04:08 +02004691 /* main modules may have different revisions */
4692 if (dup && strcmp(dup->mod->name, submod->mod->name)) {
Radek Krejci854e1552020-12-21 15:05:23 +01004693 LOGVAL_PARSER(ctx, LY_VCODE_NAME_COL, "submodules", dup->name);
Radek Krejcie9e987e2018-10-31 12:50:27 +01004694 return LY_EVALID;
4695 }
4696
Michal Vasko12ef5362022-09-16 15:13:58 +02004697cleanup:
Michal Vasko7fbc8162018-09-17 10:35:16 +02004698 return ret;
4699}
4700
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004701/**
4702 * @brief Skip any redundant characters, namely whitespaces and comments.
4703 *
4704 * @param[in] ctx Yang parser context.
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004705 * @return LY_SUCCESS on success.
4706 * @return LY_EVALID on invalid comment.
4707 */
4708static LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02004709skip_redundant_chars(struct lysp_yang_ctx *ctx)
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004710{
4711 /* read some trailing spaces, new lines, or comments */
Radek Krejci33090f92020-12-17 20:12:46 +01004712 while (ctx->in->current[0]) {
4713 if (!strncmp(ctx->in->current, "//", 2)) {
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004714 /* one-line comment */
Radek Krejci33090f92020-12-17 20:12:46 +01004715 ly_in_skip(ctx->in, 2);
4716 LY_CHECK_RET(skip_comment(ctx, 1));
4717 } else if (!strncmp(ctx->in->current, "/*", 2)) {
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004718 /* block comment */
Radek Krejci33090f92020-12-17 20:12:46 +01004719 ly_in_skip(ctx->in, 2);
4720 LY_CHECK_RET(skip_comment(ctx, 2));
4721 } else if (isspace(ctx->in->current[0])) {
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004722 /* whitespace */
Radek Krejcidd713ce2021-01-04 23:12:12 +01004723 if (ctx->in->current[0] == '\n') {
4724 LY_IN_NEW_LINE(ctx->in);
4725 }
Radek Krejci33090f92020-12-17 20:12:46 +01004726 ly_in_skip(ctx->in, 1);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004727 } else {
4728 break;
4729 }
4730 }
4731
4732 return LY_SUCCESS;
4733}
4734
Radek Krejcid4557c62018-09-17 11:42:09 +02004735LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02004736yang_parse_submodule(struct lysp_yang_ctx **context, struct ly_ctx *ly_ctx, struct lysp_ctx *main_ctx,
Radek Krejci0f969882020-08-21 16:56:47 +02004737 struct ly_in *in, struct lysp_submodule **submod)
Michal Vasko7fbc8162018-09-17 10:35:16 +02004738{
Radek Krejci6d9b9b52018-11-02 12:43:39 +01004739 LY_ERR ret = LY_SUCCESS;
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004740 char *word;
Radek Krejciefd22f62018-09-27 11:47:58 +02004741 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004742 enum ly_stmt kw;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004743 struct lysp_submodule *mod_p = NULL;
Michal Vaskoc636ea42022-09-16 10:20:31 +02004744 struct lysf_ctx fctx = {.ctx = ly_ctx};
Michal Vasko7fbc8162018-09-17 10:35:16 +02004745
aPiecek56be92a2021-07-01 07:18:10 +02004746 assert(context && ly_ctx && main_ctx && in && submod);
4747
David Sedlák1b623122019-08-05 15:27:49 +02004748 /* create context */
4749 *context = calloc(1, sizeof **context);
4750 LY_CHECK_ERR_RET(!(*context), LOGMEM(ly_ctx), LY_EMEM);
Michal Vaskob36053d2020-03-26 15:49:30 +01004751 (*context)->format = LYS_IN_YANG;
Radek Krejci33090f92020-12-17 20:12:46 +01004752 (*context)->in = in;
aPiecek56be92a2021-07-01 07:18:10 +02004753 (*context)->main_ctx = main_ctx;
David Sedlák1b623122019-08-05 15:27:49 +02004754
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004755 mod_p = calloc(1, sizeof *mod_p);
4756 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(ly_ctx); ret = LY_EMEM, cleanup);
Michal Vasko8a67eff2021-12-07 14:04:47 +01004757 mod_p->mod = PARSER_CUR_PMOD(main_ctx)->mod;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004758 mod_p->parsing = 1;
Michal Vasko8a67eff2021-12-07 14:04:47 +01004759
4760 /* use main context parsed mods adding the current one */
4761 (*context)->parsed_mods = main_ctx->parsed_mods;
4762 ly_set_add((*context)->parsed_mods, mod_p, 1, NULL);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004763
Michal Vasko7a266772024-01-23 11:02:38 +01004764 ly_log_location(NULL, NULL, NULL, in);
Radek Krejci2efc45b2020-12-22 16:25:44 +01004765
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004766 /* skip redundant but valid characters at the beginning */
Radek Krejci33090f92020-12-17 20:12:46 +01004767 ret = skip_redundant_chars(*context);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004768 LY_CHECK_GOTO(ret, cleanup);
4769
Michal Vasko7fbc8162018-09-17 10:35:16 +02004770 /* "module"/"submodule" */
Radek Krejci33090f92020-12-17 20:12:46 +01004771 ret = get_keyword(*context, &kw, &word, &word_len);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004772 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004773
Radek Krejcid6b76452019-09-03 17:03:03 +02004774 if (kw == LY_STMT_MODULE) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004775 LOGERR(ly_ctx, LY_EDENIED, "Input data contains module in situation when a submodule is expected.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004776 ret = LY_EINVAL;
4777 goto cleanup;
Radek Krejcid6b76452019-09-03 17:03:03 +02004778 } else if (kw != LY_STMT_SUBMODULE) {
Michal Vasko193dacd2022-10-13 08:43:05 +02004779 LOGVAL_PARSER(*context, LY_VCODE_MOD_SUBOMD, lyplg_ext_stmt2str(kw));
Radek Krejci40544fa2019-01-11 09:38:37 +01004780 ret = LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004781 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004782 }
4783
Michal Vasko7fbc8162018-09-17 10:35:16 +02004784 /* substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01004785 ret = parse_submodule(*context, mod_p);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004786 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004787
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004788 /* skip redundant but valid characters at the end */
Radek Krejci33090f92020-12-17 20:12:46 +01004789 ret = skip_redundant_chars(*context);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004790 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko63f3d842020-07-08 10:10:14 +02004791 if (in->current[0]) {
4792 LOGVAL_PARSER(*context, LY_VCODE_TRAILING_SUBMOD, 15, in->current, strlen(in->current) > 15 ? "..." : "");
Radek Krejci40544fa2019-01-11 09:38:37 +01004793 ret = LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004794 goto cleanup;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004795 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02004796
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004797 mod_p->parsing = 0;
4798 *submod = mod_p;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004799
Radek Krejcibbe09a92018-11-08 09:36:54 +01004800cleanup:
Michal Vasko7a266772024-01-23 11:02:38 +01004801 ly_log_location_revert(0, 0, 0, 1);
Radek Krejcibbe09a92018-11-08 09:36:54 +01004802 if (ret) {
Michal Vaskoc636ea42022-09-16 10:20:31 +02004803 lysp_module_free(&fctx, (struct lysp_module *)mod_p);
Michal Vaskod0625d72022-10-06 15:02:50 +02004804 lysp_yang_ctx_free(*context);
David Sedlák1b623122019-08-05 15:27:49 +02004805 *context = NULL;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004806 }
4807
4808 return ret;
4809}
4810
4811LY_ERR
Michal Vaskod0625d72022-10-06 15:02:50 +02004812yang_parse_module(struct lysp_yang_ctx **context, struct ly_in *in, struct lys_module *mod)
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004813{
4814 LY_ERR ret = LY_SUCCESS;
Radek Krejci0a1d0d42019-05-16 15:14:51 +02004815 char *word;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004816 size_t word_len;
Radek Krejcid6b76452019-09-03 17:03:03 +02004817 enum ly_stmt kw;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004818 struct lysp_module *mod_p = NULL;
Michal Vaskoc636ea42022-09-16 10:20:31 +02004819 struct lysf_ctx fctx = {.ctx = mod->ctx};
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004820
David Sedlák1b623122019-08-05 15:27:49 +02004821 /* create context */
4822 *context = calloc(1, sizeof **context);
4823 LY_CHECK_ERR_RET(!(*context), LOGMEM(mod->ctx), LY_EMEM);
Michal Vaskob36053d2020-03-26 15:49:30 +01004824 (*context)->format = LYS_IN_YANG;
Michal Vasko8a67eff2021-12-07 14:04:47 +01004825 LY_CHECK_ERR_RET(ly_set_new(&(*context)->parsed_mods), free(*context); LOGMEM(mod->ctx), LY_EMEM);
Radek Krejci33090f92020-12-17 20:12:46 +01004826 (*context)->in = in;
Michal Vaskod0625d72022-10-06 15:02:50 +02004827 (*context)->main_ctx = (struct lysp_ctx *)(*context);
David Sedlák1b623122019-08-05 15:27:49 +02004828
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004829 mod_p = calloc(1, sizeof *mod_p);
4830 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(mod->ctx), cleanup);
4831 mod_p->mod = mod;
Michal Vasko8a67eff2021-12-07 14:04:47 +01004832 ly_set_add((*context)->parsed_mods, mod_p, 1, NULL);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004833
Michal Vasko7a266772024-01-23 11:02:38 +01004834 ly_log_location(NULL, NULL, NULL, in);
Radek Krejci2efc45b2020-12-22 16:25:44 +01004835
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004836 /* skip redundant but valid characters at the beginning */
Radek Krejci33090f92020-12-17 20:12:46 +01004837 ret = skip_redundant_chars(*context);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004838 LY_CHECK_GOTO(ret, cleanup);
4839
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004840 /* "module"/"submodule" */
Radek Krejci33090f92020-12-17 20:12:46 +01004841 ret = get_keyword(*context, &kw, &word, &word_len);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004842 LY_CHECK_GOTO(ret, cleanup);
4843
Radek Krejcid6b76452019-09-03 17:03:03 +02004844 if (kw == LY_STMT_SUBMODULE) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004845 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 +01004846 ret = LY_EINVAL;
4847 goto cleanup;
Radek Krejcid6b76452019-09-03 17:03:03 +02004848 } else if (kw != LY_STMT_MODULE) {
Michal Vasko193dacd2022-10-13 08:43:05 +02004849 LOGVAL_PARSER((*context), LY_VCODE_MOD_SUBOMD, lyplg_ext_stmt2str(kw));
Radek Krejci40544fa2019-01-11 09:38:37 +01004850 ret = LY_EVALID;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004851 goto cleanup;
4852 }
4853
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004854 /* substatements */
Radek Krejci33090f92020-12-17 20:12:46 +01004855 ret = parse_module(*context, mod_p);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004856 LY_CHECK_GOTO(ret, cleanup);
4857
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004858 /* skip redundant but valid characters at the end */
Radek Krejci33090f92020-12-17 20:12:46 +01004859 ret = skip_redundant_chars(*context);
Michal Vasko69e6bbb2020-11-04 17:11:46 +01004860 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko63f3d842020-07-08 10:10:14 +02004861 if (in->current[0]) {
4862 LOGVAL_PARSER(*context, LY_VCODE_TRAILING_MOD, 15, in->current, strlen(in->current) > 15 ? "..." : "");
Radek Krejci40544fa2019-01-11 09:38:37 +01004863 ret = LY_EVALID;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004864 goto cleanup;
4865 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004866
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004867 mod->parsed = mod_p;
4868
4869cleanup:
Michal Vasko7a266772024-01-23 11:02:38 +01004870 ly_log_location_revert(0, 0, 0, 1);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004871 if (ret) {
Michal Vaskoc636ea42022-09-16 10:20:31 +02004872 lysp_module_free(&fctx, mod_p);
Michal Vaskod0625d72022-10-06 15:02:50 +02004873 lysp_yang_ctx_free(*context);
David Sedlák1b623122019-08-05 15:27:49 +02004874 *context = NULL;
Radek Krejcibbe09a92018-11-08 09:36:54 +01004875 }
4876
Michal Vasko7fbc8162018-09-17 10:35:16 +02004877 return ret;
4878}